Fs

GitHub   Edit on GitHub

Utilities for high-level file system interactions. Utilizes WASI Preview 1 for underlying API

Added in 0.7.0 No other changes yet.
1
from "fs" include Fs
1
Fs.Utf8.readFile(Path.fromString("baz.txt"))
1
Fs.Utf8.writeFile(Path.fromString("baz.txt"), "Hello World\n")
1
Fs.copy(Path.fromString("foo.txt"), Path.fromString("foocopy.txt"))

Types

Type declarations included in the Fs module.

Fs.FileError

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
enum FileError {
NoPreopenedDirectories,
PermissionDenied,
AddressInUse,
AddressNotAvailable,
AddressFamilyNotSupported,
ResourceUnavailableOrOperationWouldBlock,
ConnectionAlreadyInProgress,
BadFileDescriptor,
BadMessage,
DeviceOrResourceBusy,
OperationCanceled,
NoChildProcesses,
ConnectionAborted,
ConnectionRefused,
ConnectionReset,
ResourceDeadlockWouldOccur,
DestinationAddressRequired,
MathematicsArgumentOutOfDomainOfFunction,
FileExists,
BadAddress,
FileTooLarge,
HostIsUnreachable,
IdentifierRemoved,
IllegalByteSequence,
OperationInProgress,
InterruptedFunction,
InvalidArgument,
IOError,
SocketIsConnected,
IsADirectory,
TooManyLevelsOfSymbolicLinks,
FileDescriptorValueTooLarge,
TooManyLinks,
MessageTooLarge,
FilenameTooLong,
NetworkIsDown,
ConnectionAbortedByNetwork,
NetworkUnreachable,
TooManyFilesOpenInSystem,
NoBufferSpaceAvailable,
NoSuchDevice,
NoSuchFileOrDirectory,
ExecutableFileFormatError,
NoLocksAvailable,
NotEnoughSpace,
NoMessageOfTheDesiredType,
ProtocolNotAvailable,
NoSpaceLeftOnDevice,
FunctionNotSupported,
TheSocketIsNotConnected,
NotADirectoryOrASymbolicLinkToADirectory,
DirectoryNotEmpty,
StateNotRecoverable,
NotASocket,
NotSupportedOrOperationNotSupportedOnSocket,
InappropriateIOControlOperation,
NoSuchDeviceOrAddress,
ValueTooLargeToBeStoredInDataType,
PreviousOwnerDied,
OperationNotPermitted,
BrokenPipe,
ProtocolError,
ProtocolNotSupported,
ProtocolWrongTypeForSocket,
ResultTooLarge,
ReadOnlyFileSystem,
InvalidSeek,
NoSuchProcess,
ConnectionTimedOut,
TextFileBusy,
CrossDeviceLink,
ExtensionCapabilitiesInsufficient,
UnknownFileError(Number),
}

Potential errors that can be raised from system interactions.

Fs.SpecialFileType

1
2
3
4
5
6
7
enum SpecialFileType {
Unknown,
BlockDevice,
CharacterDevice,
SocketDatagram,
SocketStream,
}

Represents non-standard system file types.

Fs.FileType

1
2
3
4
5
6
enum FileType {
File,
Directory,
SymbolicLink,
Special(SpecialFileType),
}

Represents different system file types.

Fs.Stats

1
2
3
4
5
6
7
record Stats {
fileType: FileType,
size: Number,
accessedTimestamp: Number,
modifiedTimestamp: Number,
changedTimestamp: Number,
}

Represents metadata about a file.

Fields:

name type description
fileType FileType
size Number File size in bytes
accessedTimestamp Number Last accessed timestamp in nanoseconds
modifiedTimestamp Number Last modified timestamp in nanoseconds
changedTimestamp Number Last file status change timestamp in nanoseconds

Fs.DirectoryEntry

1
2
3
4
record DirectoryEntry {
name: String,
fileType: FileType,
}

Represents information about an item in a directory.

Fs.RemoveMode

1
2
3
4
5
enum RemoveMode {
RemoveFile,
RemoveEmptyDirectory,
RemoveRecursive,
}

The type of removal operation to perform when calling remove.

Fs.CopyMode

1
2
3
4
enum CopyMode {
CopyFile,
CopyRecursive,
}

The type of copy operation to perform when calling copy.

Fs.WriteMode

1
2
3
4
enum WriteMode {
Truncate,
Append,
}

The type of write operation to perform when calling writeFile.


Values

Functions and constants included in the Fs module.

Fs.remove

Added in 0.7.0 No other changes yet.
1
2
3
remove:
(?removeMode: RemoveMode, ?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<Void, FileError>

Removes a file or directory.

Parameters:

param type description
?removeMode RemoveMode The type of removal to perform; RemoveFile by default
?baseDirPath Option<Path.Path> The path to the directory in which path resolution starts
path Path.Path The path of the file or directory to remove

Returns:

type description
Result<Void, FileError> Ok(void) if the operation succeeds, Err(err) if a file system error is encountered

Examples:

1
Fs.remove(Path.fromString("file.txt")) // removes a file
1
Fs.remove(removeMode=Fs.RemoveEmptyDirectory, Path.fromString("dir")) // removes an empty directory
1
Fs.remove(removeMode=Fs.RemoveRecursive, Path.fromString("dir")) // removes the directory and its contents

Fs.readDir

Added in 0.7.0 No other changes yet.
1
2
3
readDir:
(?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<List<DirectoryEntry>, FileError>

Reads the contents of a directory.

Parameters:

param type description
?baseDirPath Option<Path.Path> The path to the directory in which resolution should begin
path Path.Path The path to the directory to read

Returns:

type description
Result<List<DirectoryEntry>, FileError> Ok(contents) containing the directory contents or Err(err) if a file system error is encountered

Fs.createDir

Added in 0.7.0 No other changes yet.
1
2
3
createDir:
(?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<Void, FileError>

Creates a new empty directory at the given path.

Parameters:

param type description
?baseDirPath Option<Path.Path> The path to the directory in which resolution should begin
path Path.Path The path to create the new directory, relative to the base directory

Returns:

type description
Result<Void, FileError> Ok(void) if the operation succeeds, Err(err) if a file system error is encountered
Added in 0.7.0 No other changes yet.
1
2
3
createSymlink:
(linkContents: Path.Path, ?targetBaseDirPath: Option<Path.Path>,
targetPath: Path.Path) => Result<Void, FileError>

Creates a new symbolic link with the given contents.

Parameters:

param type description
linkContents Path.Path The path to store into the link
?targetBaseDirPath Option<Path.Path> The path to the directory in which the target path resolution starts
targetPath Path.Path The path to the target of the link

Returns:

type description
Result<Void, FileError> Ok(void) if the operation succeeds, Err(err) if a file system error or relativization error is encountered

Fs.stats

Added in 0.7.0 No other changes yet.
1
2
3
stats:
(?followSymlink: Bool, ?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<Stats, FileError>

Queries information about a file.

Parameters:

param type description
?followSymlink Bool Whether to follow symlinks or not; if true then the stats of a valid symlink’s underlying file will be returned. true by default
?baseDirPath Option<Path.Path> The path to the directory in which the path resolution starts
path Path.Path The path of the file to query

Returns:

type description
Result<Stats, FileError> Ok(stats) containing metadata or Err(err) if a file system error is encountered

Fs.exists

Added in 0.7.0 No other changes yet.
1
exists: (?baseDirPath: Option<Path.Path>, path: Path.Path) => Bool

Polls whether or not a file or directory exists at the given path.

Parameters:

param type description
?baseDirPath Option<Path.Path> The path to the directory in which the path resolution starts
path Path.Path The path of the file to query

Returns:

type description
Bool true if a file or directory exists at the path or false otherwise
Added in 0.7.0 No other changes yet.
1
2
3
readLink:
(?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<Path.Path, FileError>

Reads the contents of a symbolic link.

Parameters:

param type description
?baseDirPath Option<Path.Path> The path to the directory to begin path resolution
path Path.Path The path to the link to read

Returns:

type description
Result<Path.Path, FileError> Ok(path) containing the link contents or Err(err) if a file system error is encountered

Fs.copy

1
2
3
4
5
copy:
(?copyMode: CopyMode, ?followSymlink: Bool,
?sourceBaseDirPath: Option<Path.Path>, sourcePath: Path.Path,
?targetBaseDirPath: Option<Path.Path>, targetPath: Path.Path) =>
Result<Void, FileError>

Copies a file or directory.

Parameters:

param type description
?copyMode CopyMode The type of copy to perform; CopyFile by default
?followSymlink Bool Whether to follow symlinks or not; if true then the stats of a valid symlink’s underlying file will be returned. true by default
?sourceBaseDirPath Option<Path.Path> The path to the directory in which the source path resolution starts
sourcePath Path.Path The path of the file or directory to copy
?targetBaseDirPath Option<Path.Path> The path to the directory in which the target path resolution starts
targetPath Path.Path The path to copy the file or directory to

Returns:

type description
Result<Void, FileError> Ok(void) if the operation succeeds, Err(err) if a file system error is encountered

Fs.rename

Added in 0.7.0 No other changes yet.
1
2
3
4
rename:
(?sourceBaseDirPath: Option<Path.Path>, sourcePath: Path.Path,
?targetBaseDirPath: Option<Path.Path>, targetPath: Path.Path) =>
Result<Void, FileError>

Renames a file or directory.

Parameters:

param type description
?sourceBaseDirPath Option<Path.Path> The path to the directory in which the source path resolution starts
sourcePath Path.Path The path of the file to rename
?targetBaseDirPath Option<Path.Path> The path to the directory in which the target path resolution starts
targetPath Path.Path The new path of the file

Returns:

type description
Result<Void, FileError> Ok(void) if the operation succeeds, Err(err) if a file system error is encountered

Fs.Binary

Functionality for reading and writing Bytes to files.

Added in 0.7.0 No other changes yet.

Values

Functions and constants included in the Fs.Binary module.

Fs.Binary.readFile

Added in 0.7.0 No other changes yet.
1
2
3
readFile:
(?sync: Bool, ?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<Bytes, FileError>

Read the contents of a file as Bytes.

Parameters:

param type description
?sync Bool Whether to synchronously read; true by default
?baseDirPath Option<Path.Path> The path to the directory to begin path resolution
path Path.Path The file path to read from

Returns:

type description
Result<Bytes, FileError> Ok(contents) containing the bytes read if successful or Err(err) if a file system error is encountered

Fs.Binary.writeFile

Added in 0.7.0 No other changes yet.
1
2
3
writeFile:
(?writeMode: WriteMode, ?sync: Bool, ?baseDirPath: Option<Path.Path>,
path: Path.Path, data: Bytes) => Result<Void, FileError>

Write Bytes to a file.

Parameters:

param type description
?writeMode WriteMode The type of write operation to perform; Truncate by default
?sync Bool Whether to synchronously write; true by default
?baseDirPath Option<Path.Path> The path to the directory to begin path resolution
path Path.Path The file path to write to
data Bytes The bytes to write to the file

Returns:

type description
Result<Void, FileError> Ok(void) if the operation is successful or Err(err) if a file system error is encountered

Fs.Utf8

Functionality for reading and writing Strings to files.

Added in 0.7.0 No other changes yet.

Values

Functions and constants included in the Fs.Utf8 module.

Fs.Utf8.readFile

Added in 0.7.0 No other changes yet.
1
2
3
readFile:
(?sync: Bool, ?baseDirPath: Option<Path.Path>, path: Path.Path) =>
Result<String, FileError>

Read the contents of a file as a String.

Parameters:

param type description
?sync Bool Whether to synchronously read; true by default
?baseDirPath Option<Path.Path> The path to the directory to begin path resolution
path Path.Path The file path to read from

Returns:

type description
Result<String, FileError> Ok(contents) containing the string read if successful or Err(err) if a file system error is encountered

Fs.Utf8.writeFile

Added in 0.7.0 No other changes yet.
1
2
3
writeFile:
(?writeMode: WriteMode, ?sync: Bool, ?baseDirPath: Option<Path.Path>,
path: Path.Path, data: String) => Result<Void, FileError>

Write a String to a file.

Parameters:

param type description
?writeMode WriteMode The type of write operation to perform; Truncate by default
?sync Bool Whether to synchronously write; true by default
?baseDirPath Option<Path.Path> The path to the directory to begin path resolution
path Path.Path The file path to write to
data String The string to write to the file

Returns:

type description
Result<Void, FileError> Ok(void) if the operation is successful or Err(err) if a file system error is encountered
This is a notification!