System.File

lib/System/File.gob

Files, open: a Read and a Write over a descriptor, with what can be done to one in its type.

The mode is a type parameter. A File ReadOnly has no Write instance, so writing through one is a type error rather than EBADF at run time, and an operation that does not care about the mode, such as close, is polymorphic in it. The modes are matched by capability classes rather than listed one by one, so an operation that needs to write constrains CanWrite m and holds for every mode that can. File's constructor is not exported, so a program that gives a type of its own a capability gets no File of it.

Append is a mode of its own, not an option of create, because on Linux pwrite to a file opened to append ignores its offset and appends. Appending has CanWrite and not CanWriteAt, so writeAt on it is a type error rather than a write in the wrong place.

Positional and sequential operations coexist. readAt and writeAt neither use nor move the position that Read and Write advance, and any number of tasks may call them on one File at once; sequential reads and writes are each whole with respect to others of their direction.

Closing is scope-based. use closes a file when its body returns. A file used after close, by a task or a closure that kept it, answers Closed from every operation; the descriptor number is never reused under it (Turkey.FD). close is idempotent.

Directories are handles too. A Dir is an open directory, and the ...In functions take a name relative to one, resolved by the system against the directory already open (openat and its kin) rather than from the root again. A directory renamed while it is open stays the same Dir, and a walk that opens each subdirectory from its parent, refusing to follow a symbolic link, cannot be led out of the tree it started in: that is how removeTree and list walk. Every function that takes a path is the ...In function of workingDir(). A Dir shares File's lifetime rules.

Every descriptor is opened close-on-exec, so a process started later does not inherit it by accident.

type ReadOnly
type WriteOnly
type ReadWrite
type Appending
class CanRead m
class CanWrite m
class CanWriteAt m
type File m

An open file: its descriptor, shared safely between tasks, and the path it was opened by, for messages. stdoutFirst is set for the standard error, and for the standard input on a terminal: the standard output's buffer is written out before each operation, so that a prompt appears before the read waits, and the two output streams come out in the order they were written.

type Create = Truncate | Exclusive

What create does to a file that exists already: empties it, or fails with AlreadyExists. A sum rather than flags, since the flags it replaces have combinations that mean nothing.

fun openRead : fun(String) -> Either IOError (File ReadOnly)
fun create : fun(String, Create) -> Either IOError (File WriteOnly)

A file to write, made if it does not exist, with permissions 0o666 as the umask narrows them.

fun createExecutable : fun(String, Create) -> Either IOError (File WriteOnly)

create, with permissions 0o777 as the umask narrows them: a program the caller is about to write. A file that exists keeps the permissions it has, so a caller that needs these removes it first.

fun openAppend : fun(String) -> Either IOError (File Appending)

A file every write goes to the end of, made if it does not exist.

fun openReadWrite : fun(String) -> Either IOError (File ReadWrite)

An existing file, to read and write.

fun close : fun(File m) -> Either IOError Unit

Close f. A file system may report a failed write only here, so the answer is worth reading after writing. Closing a closed file does nothing. If another task is still in an operation on f, the descriptor closes when it finishes, and a failure then is not reported.

fun path : fun(File m) -> String
fun use : fun(Either IOError (File m), fun(File m) -> Either IOError a) -> Either IOError a

body of the file opening opened, which is then closed: the body's error, if it failed; otherwise the close's, if that failed; otherwise the body's result.

File.use(File.openRead("notes.txt"), fun(f) = IO.readAll(f))
fun readAt : [CanRead m] fun(File m, Int, Buffer) -> Either IOError Int

Fill into's room from offset in the file, without using or moving its position: how many bytes were read, fewer than the room only at the end of the file. Any number of tasks may do this on one File at once. A FIFO, a pipe or a terminal has no offsets, and answers NotSeekable.

fun writeAt : [CanWriteAt m] fun(File m, Int, Buffer) -> Either IOError Unit

Write all of from's window at offset in the file, without using or moving its position. Not for an Appending file, where Linux would append it instead.

fun readAll : fun(String) -> Either IOError Buffer

The file's bytes, in a buffer sized by what fstat says the file holds, and grown if it has grown since. Read transfer bytes per system call.

fun readText : fun(String) -> Either (Either InvalidUTF8 IOError) String

The file's text, if it is well-formed UTF-8.

fun writeAll : fun(String, Buffer) -> Either IOError Unit

Replace the file's contents with bytes' window, making it if need be.

fun writeText : fun(String, String) -> Either IOError Unit

Replace the file's contents with text, making it if need be.

fun lock : fun(File m) -> Either IOError Unit

Take the exclusive lock on f's file, waiting while another holder has it. The lock is advisory, flock's: it keeps out only those that ask for it, across processes and across Files opened separately in this one. It is released by unlock, or when f is closed.

On Darwin the task waits and its worker runs others, and closing f ends the wait with Closed. On Linux, which reports no release of a lock, the task's thread waits in flock, and the other tasks go on without it on another thread (Turkey.Monitor), so a second File in the same program gets a lock this program holds once the first lets it go, however many workers there are. tryLock does not wait.

fun tryLock : fun(File m) -> Either IOError Bool

Take the exclusive lock if nobody holds it: whether it was taken.

fun unlock : fun(File m) -> Either IOError Unit
type FileKind = Regular | Directory | Fifo | Symlink | Socket | CharacterDevice | BlockDevice
type Stat = Stat { size : Int, kind : FileKind, modified : Instant, permissions : Int }

What the system says of a file: its size in bytes, its kind, when its contents were last changed, and its permission bits, such as 0o644.

fun stat : fun(File m) -> Either IOError Stat

What f is now.

fun metadata : fun(String) -> Either IOError Stat

What path names, following symbolic links: NotFound if nothing does.

fun symlinkMetadata : fun(String) -> Either IOError Stat

What path names, describing a symbolic link rather than following it.

fun metadataIn : fun(Dir, String) -> Either IOError Stat
fun symlinkMetadataIn : fun(Dir, String) -> Either IOError Stat
type Dir

An open directory, and the path it was opened by, for messages. owned is false only for workingDir(), whose descriptor is not one to close.

fun workingDir : fun() -> Dir

The working directory, as a Dir that relative names resolve against when they are used: changing the working directory changes what it means.

fun openDir : fun(String) -> Either IOError Dir
fun openDirIn : fun(Dir, String) -> Either IOError Dir

The directory name in d, following a symbolic link to one.

fun closeDir : fun(Dir) -> Either IOError Unit

Close d, as close closes a file. Closing workingDir() does nothing.

fun dirPath : fun(Dir) -> String
fun useDir : fun(Either IOError Dir, fun(Dir) -> Either IOError a) -> Either IOError a

use, for a directory.

fun list : fun(Dir) -> Either IOError (Array String)

The names in d, without "." and "..", sorted by their bytes, so that two listings of the same directory come out in the same order. A name that is not UTF-8 fails the listing.

fun listPath : fun(String) -> Either IOError (Array String)
fun openReadIn : fun(Dir, String) -> Either IOError (File ReadOnly)
fun createIn : fun(Dir, String, Create) -> Either IOError (File WriteOnly)

create, of name in d.

fun createDir : fun(String) -> Either IOError Unit

Make the directory path, with permissions 0o777 as the umask narrows them. Its parent must exist; AlreadyExists if something is there.

fun createDirIn : fun(Dir, String) -> Either IOError Unit
fun createDirAll : fun(String) -> Either IOError Unit

Make the directory path and every missing directory above it, as mkdir -p does. A directory that is there already, or that another process makes meanwhile, is not an error; anything else in the way is AlreadyExists.

fun createDirAllIn : fun(Dir, String) -> Either IOError Unit
fun removeFile : fun(String) -> Either IOError Unit

Remove the file path, or the symbolic link, not what it points to.

fun removeFileIn : fun(Dir, String) -> Either IOError Unit
fun removeDir : fun(String) -> Either IOError Unit

Remove the empty directory path: NotEmpty if it has entries.

fun removeDirIn : fun(Dir, String) -> Either IOError Unit
fun removeTree : fun(String) -> Either IOError Unit

Remove path and, if it is a directory, everything in it. A symbolic link is removed, never followed, wherever it is in the tree: the walk opens each directory from its parent with O_NOFOLLOW, so a link swapped in for a directory while the walk runs fails the walk rather than leading it outside.

fun removeTreeIn : fun(Dir, String) -> Either IOError Unit
fun rename : fun(String, String) -> Either IOError Unit

Rename from to to, replacing whatever to named, in one step: anyone opening to meanwhile finds the old file or the new one, never neither and never half of one. Both must be on one file system, or it fails with Other for EXDEV. Writing a file under a staging name and renaming it into place is how to publish it whole.

fun renameIn : fun(Dir, String, Dir, String) -> Either IOError Unit
fun readLinkIn : fun(Dir, String) -> Either IOError String
fun temporaryDir : fun(String) -> Either IOError String

A new, empty directory, made for this caller alone (permissions 0o700), in $TMPDIR, or /tmp if that is unset: its path, whose last component is prefix and six characters chosen to make it unique. Nothing removes it but removeTree.

fun stdin : fun() -> Buffered (File ReadOnly)

The standard input, read through the one buffer the program has for it.

fun stderr : fun() -> File WriteOnly

The standard error, unbuffered.

instance CanRead ReadOnly
instance CanRead ReadWrite
instance CanWrite WriteOnly
instance CanWrite ReadWrite
instance CanWrite Appending
instance CanWriteAt WriteOnly
instance CanWriteAt ReadWrite
instance Read (File m) : CanRead m
instance Write (File m) : CanWrite m