System.File
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.
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.
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.
A file to write, made if it does not exist, with permissions 0o666 as the umask narrows them.
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.
A file every write goes to the end of, made if it does not exist.
An existing file, to read and write.
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.
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))
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.
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.
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.
The file's text, if it is well-formed UTF-8.
Replace the file's contents with bytes' window, making it if need be.
Replace the file's contents with text, making it if need be.
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.
Take the exclusive lock if nobody holds it: whether it was taken.
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.
What f is now.
What path names, following symbolic links: NotFound if nothing does.
What path names, describing a symbolic link rather than following it.
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.
The working directory, as a Dir that relative names resolve against when
they are used: changing the working directory changes what it means.
The directory name in d, following a symbolic link to one.
Close d, as close closes a file. Closing workingDir() does nothing.
use, for a directory.
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.
create, of name in d.
Make the directory path, with permissions 0o777 as the umask narrows
them. Its parent must exist; AlreadyExists if something is there.
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.
Remove the file path, or the symbolic link, not what it points to.
Remove the empty directory path: NotEmpty if it has entries.
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.
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.
What the symbolic link path points to, as it is written in the link:
possibly relative, and not checked to exist. NotFound if there is no such
link, and Other with EINVAL if path is not one, or if what it points
to is not UTF-8.
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.
The standard input, read through the one buffer the program has for it.
The standard error, unbuffered.