os
The os module provides access to resources from the host operating system,
particularly to the filesystem and standard I/O. cwd provides an
implementation of fs::fs which is backed by the host filesystem, and this
module provides equivalents of various fs functions which infer the current
working directory as the fs parameter. This module also provides various
non-filesystem related features from the host, such as the hostname, environment
variables, and so on.
Submodules
Index
Types
type amode;
type mcl;
type resolve_flag;
type status;
Constants
const BUFSZ: size;
Globals
let args: []str;
let cwd: *fs::fs;
let stderr: io::handle;
let stderr_file: io::file;
let stdin: io::handle;
let stdin_file: io::file;
let stdout: io::handle;
let stdout_file: io::file;
Functions
fn access(str, amode) (bool | fs::error);
fn chdir((*fs::fs | str)) (void | fs::error);
fn chmod(str, fs::mode) (void | fs::error);
fn chown(str, uint, uint) (void | fs::error);
fn chroot(str) (void | fs::error);
fn cpucount() (size | errors::error);
fn create(str, fs::mode, fs::flag...) (io::file | fs::error);
fn dirfdfs_set_getdents_bufsz(*fs::fs, size) void;
fn dirfdopen(io::file, resolve_flag...) *fs::fs;
fn dirfile(*fs::fs) io::file;
fn dirfs_clone(*fs::fs, resolve_flag...) *fs::fs;
fn diropen(str) (*fs::fs | fs::error);
fn exists(str) bool;
fn exit(int) never;
fn finish(*fs::iterator) void;
fn getcwd() str;
fn getenv(const str) (str | void);
fn getenvs() []str;
fn hostname() const str;
fn iter(str) (*fs::iterator | fs::error);
fn link(str, str) (void | fs::error);
fn machine() const str;
fn mkblk(str, fs::mode, uint, uint) (void | fs::error);
fn mkchr(str, fs::mode, uint, uint) (void | fs::error);
fn mkdir(str, fs::mode) (void | fs::error);
fn mkdirs(str, fs::mode) (void | fs::error);
fn mkfifo(str, fs::mode) (void | fs::error);
fn mlock(*opaque, size, mcl) (void | errors::error);
fn mlockall(mcl) (void | errors::error);
fn move(str, str) (void | fs::error);
fn munlock(*opaque, size) (void | errors::error);
fn munlockall() (void | errors::error);
fn open(str, fs::flag...) (io::file | fs::error);
fn readdir(str) ([]fs::dirent | fs::error);
fn readlink(str) (str | fs::error);
fn realpath(str) (str | fs::error);
fn release() const str;
fn remove(str) (void | fs::error);
fn rename(str, str) (void | fs::error);
fn resolve(str) str;
fn rmdir(str) (void | fs::error);
fn rmdirall(str) (void | fs::error);
fn setenv(const str, const str) (void | errors::invalid);
fn stat(str) (fs::filestat | fs::error);
fn symlink(str, str) (void | fs::error);
fn sysname() const str;
fn tryenv(const str, str) str;
fn unsetenv(const str) (void | errors::invalid);
fn version() const str;
Types
type amode
type amode = enum {
F_OK = rt::F_OK,
R_OK = rt::R_OK,
W_OK = rt::W_OK,
X_OK = rt::X_OK,
};
Access modes for access.
type mcl
type mcl = enum uint {
CURRENT = 1,
FUTURE = 2,
ONFAULT = 4,
};
Flags for the mlock family of operations.
type resolve_flag
type resolve_flag = enum {
NORMAL,
BENEATH,
IN_ROOT,
NO_SYMLINKS,
NO_XDEV,
};
Controls how symlinks are followed (or not) in a dirfd filesystem. Support
for this feature varies, you should gate usage of this enum behind a build
tag.
Note that on Linux, specifying BENEATH or IN_ROOT will also disable magic
symlinks.
type status
type status = enum {
SUCCESS = 0,
FAILURE = 1,
};
Values that may be passed to exit to indicate successful or unsuccessful
termination, respectively.
Constants
def BUFSZ
def BUFSZ: size;
The recommended buffer size for reading from disk.
Globals
let args
let args: []str;
The command line arguments provided to the program. By convention, the first
member is usually the name of the program.
let cwd
let cwd: *fs::fs;
Provides an implementation of fs::fs for the current working directory.
let stderr
let stderr: io::handle;
The standard error. This handle is unbuffered.
let stderr_file
let stderr_file: io::file;
The standard error, as an io::file. This handle is unbuffered.
let stdin
let stdin: io::handle;
The standard input. This handle is buffered.
let stdin_file
let stdin_file: io::file;
The standard input, as an io::file. This handle is unbuffered.
let stdout
let stdout: io::handle;
The standard output. This handle is buffered.
let stdout_file
let stdout_file: io::file;
The standard output, as an io::file. This handle is unbuffered.
Functions
fn access
fn access(path: str, mode: amode) (bool | fs::error);
Returns true if the given mode of access is permissible. The use of this
function is discouraged as it can allow for a race condition to occur betwen
testing for the desired access mode and actually using the file should the
permissions of the file change between these operations. It is recommended
instead to attempt to use the file directly and to handle any errors that
should occur at that time.
fn chdir
fn chdir(target: (*fs::fs | str)) (void | fs::error);
Change the current working directory.
fn chmod
fn chmod(path: str, mode: fs::mode) (void | fs::error);
Changes mode flags on a file or directory. Type bits are discared.
fn chown
fn chown(path: str, uid: uint, gid: uint) (void | fs::error);
Changes ownership of a file.
fn chroot
fn chroot(target: str) (void | fs::error);
Changes the root directory of the process. Generally requires the caller to
have root or otherwise elevated permissions.
This function is not appropriate for sandboxing.
fn cpucount
fn cpucount() (size | errors::error);
Returns the number of usable CPUs.
fn create
fn create(path: str, mode: fs::mode, flags: fs::flag...) (io::file | fs::error);
Creates a new file with the given mode if it doesn't already exist and opens
it for writing.
If no flags are provided, fs::flag::WRONLY and fs::flag::TRUNC are
used when opening the file.
Only the permission bits of the mode are used. If other bits are set, they
are discarded.
fn dirfdfs_set_getdents_bufsz
fn dirfdfs_set_getdents_bufsz(fs: *fs::fs, sz: size) void;
Sets the buffer size to use with the getdents(2) system call, for use with
fs::iter. A larger buffer requires a larger runtime allocation, but can
scan large directories faster. The default buffer size is 32 KiB.
This function is not portable.
fn dirfdopen
fn dirfdopen(fd: io::file, resolve: resolve_flag...) *fs::fs;
Opens a file descriptor as an fs::fs. This file descriptor must be a
directory file. The file will be closed when the fs is closed.
fn dirfile
fn dirfile(fs: *fs::fs) io::file;
Returns an io::file for this filesystem. This function is not portable.
fn dirfs_clone
fn dirfs_clone(fs: *fs::fs, resolve: resolve_flag...) *fs::fs;
Clones a dirfd filesystem, optionally adding additional resolve_flag
constraints.
fn diropen
fn diropen(path: str) (*fs::fs | fs::error);
Opens a directory as a filesystem.
fn exists
fn exists(path: str) bool;
Returns true if a node exists at the given path, or false if not.
Note that testing for file existence before using the file can often lead to
race conditions. If possible, prefer to simply attempt to use the file (e.g.
via "open"), and handle the resulting error should the file not exist.
fn exit
fn exit(status: int) never;
Exit the program with the provided status code.
fn finish
fn finish(iter: *fs::iterator) void;
Frees state associated with a directory iterator.
fn getcwd
fn getcwd() str;
Returns the current working directory. The return value is statically
allocated and must be duplicated (see strings::dup) before calling getcwd
again.
fn getenv
fn getenv(name: const str) (str | void);
Looks up an environment variable and returns its value, or void if unset.
fn getenvs
fn getenvs() []str;
Returns a slice of the environment strings in the form KEY=VALUE.
fn hostname
fn hostname() const str;
Returns the host system hostname
fn iter
fn iter(path: str) (*fs::iterator | fs::error);
Creates an fs::iterator for a given directory to read its contents. The
user should call fs::next to enumerate entries, and fs::finish when
done using the object.
fn link
fn link(old: str, new: str) (void | fs::error);
Creates a new (hard) link at 'new' for the file at 'old'.
fn machine
fn machine() const str;
Returns the host CPU architecture
fn mkblk
fn mkblk(path: str, mode: fs::mode, major: uint, minor: uint) (void | fs::error);
Makes a block device node. This function is only available on Unix systems.
fn mkchr
fn mkchr(path: str, mode: fs::mode, major: uint, minor: uint) (void | fs::error);
Makes a character device node. This function is only available on Unix systems.
fn mkdir
fn mkdir(path: str, mode: fs::mode) (void | fs::error);
Creates a directory.
fn mkdirs
fn mkdirs(path: str, mode: fs::mode) (void | fs::error);
Creates a directory, and all non-extant directories in its path.
fn mkfifo
fn mkfifo(path: str, mode: fs::mode) (void | fs::error);
Makes a FIFO node. This function is only available on Unix systems.
fn mlock
fn mlock(addr: *opaque, length: size, flags: mcl) (void | errors::error);
Locks a region of memory so that it will not be written to swap.
fn mlockall
fn mlockall(flags: mcl) (void | errors::error);
Locks the entire process's address space so that it will not be written to
swap.
fn move
fn move(oldpath: str, newpath: str) (void | fs::error);
Moves a file. This will use rename if possible, and will fall back to
copy and remove if necessary.
fn munlock
fn munlock(addr: *opaque, length: size) (void | errors::error);
Unlocks memory previously locked with mlock.
fn munlockall
fn munlockall() (void | errors::error);
Unlocks all locked memory in the process's address space.
fn open
fn open(path: str, flags: fs::flag...) (io::file | fs::error);
Opens a file.
If no flags are provided, fs::flag::RDONLY is used when opening the
file.
fs::flag::CREATE isn't very useful with this function, since the new
file's mode is set to zero. For this use-case, use create instead.
fn readdir
fn readdir(path: str) ([]fs::dirent | fs::error);
Reads all entries from a directory. The caller must free the return value
with fs::dirents_free.
fn readlink
fn readlink(path: str) (str | fs::error);
Returns the path referred to by a symbolic link. The return value is
statically allocated and will be overwritten on subsequent calls.
fn realpath
fn realpath(path: str) (str | fs::error);
Canonicalizes a path in this filesystem by resolving all symlinks and
collapsing any "." or ".." path components.
This function is a thin shim over fs::realpath, and the return value is
statically allocated by fs::realpath. Thus, calls to this function or to
fs::realpath will overwrite the return value of either function.
fn release
fn release() const str;
Returns the host kernel version
fn remove
fn remove(path: str) (void | fs::error);
Removes a file.
fn rename
fn rename(oldpath: str, newpath: str) (void | fs::error);
Renames a file. This generally only works if the source and destination path
are both on the same filesystem. See move for an implementation which
falls back on a "copy & remove" procedure in this situation.
fn resolve
fn resolve(path: str) str;
Resolves a path to its absolute, normalized value. Relative paths will be
rooted (if supported by the host filesystem), and "." and ".." components
will be reduced. This function does not follow symlinks; see realpath if
you need this behavior. The return value is statically allocated; use
strings::dup to extend its lifetime.
fn rmdir
fn rmdir(path: str) (void | fs::error);
Removes a directory. The target directory must be empty; see rmdirall to
remove its contents as well.
fn rmdirall
fn rmdirall(path: str) (void | fs::error);
Removes a directory, and anything in it.
fn setenv
fn setenv(name: const str, value: const str) (void | errors::invalid);
Sets an environment variable, overwriting it if it's already set. The name
may not contain '=' or '\0', and the value may not contain '\0'.
fn stat
fn stat(path: str) (fs::filestat | fs::error);
Returns file information for a given path. If the target is a symlink,
information is returned about the link, not its target.
fn symlink
fn symlink(target: str, path: str) (void | fs::error);
Creates a new symbolic link at 'path' which points to 'target'.
fn sysname
fn sysname() const str;
Returns the host kernel name
fn tryenv
fn tryenv(name: const str, default: str) str;
Looks up an environment variable and returns its value, or a default value if
unset.
fn unsetenv
fn unsetenv(name: const str) (void | errors::invalid);
Unsets an environment variable. Does nothing if the variable isn't set. The
name may not contain '=' or '\0'.
fn version
fn version() const str;
Returns the host operating system version