Directories and links v0.1.0
Getdents64
pub func Getdents64(
fd: FileDescriptor,
buffer: *var opaque,
count: uint
) -> int64
Reads directory entries from fd into buffer, and returns the bytes filled, zero at the end, or a negative
error number.
The buffer holds packed linux_dirent64 records: inode at offset 0 (8 bytes), a seek cookie at 8 (8 bytes),
the record's length at 16 (2 bytes), the entry type at 18 (1 byte), and the NUL-terminated name from 19. The
kernel writes as many whole records as fit; call again for the rest.
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | an open descriptor |
buffer | *var opaque | receives the packed directory records |
count | uint | how many bytes buffer holds |
Returns: the bytes filled, zero at the end of the directory, or a negative error number
LinkAt
pub func LinkAt(
oldDirectoryFd: FileDescriptor,
oldPath: *char8,
newDirectoryFd: FileDescriptor,
newPath: *char8,
flags: int32
) -> int64
Creates a hard link: newPath becomes a second name for the file oldPath names.
Returns zero, or a negative error number. flags is zero to leave a symbolic link at oldPath unfollowed,
or FollowSymlinks to link what it points at.
Parameters
| Name | Type | Description |
|---|---|---|
oldDirectoryFd | FileDescriptor | the directory oldPath is relative to |
oldPath | *char8 | the existing name |
newDirectoryFd | FileDescriptor | the directory newPath is relative to |
newPath | *char8 | the new name for the same file |
flags | int32 | zero, or FollowSymlinks to link what a final link points at |
Returns: zero, or a negative error number
FollowSymlinks
pub const FollowSymlinks: int32 = 0x0400;
AT_SYMLINK_FOLLOW: make LinkAt link a symbolic link's target rather than the link.
SymlinkAt
pub func SymlinkAt(
target: *char8,
newDirectoryFd: FileDescriptor,
linkPath: *char8
) -> int64
Creates a symbolic link at linkPath holding target as its text.
Returns zero, or a negative error number. The target is stored, not checked: a dangling link is created as
readily as a good one, which is a property callers rely on.
Parameters
| Name | Type | Description |
|---|---|---|
target | *char8 | the text the link will hold, which need not exist |
newDirectoryFd | FileDescriptor | the directory linkPath is relative to |
linkPath | *char8 | the link to create |
Returns: zero, or a negative error number
SymlinkNoFollow
pub const SymlinkNoFollow: int32 = 0x0100;
AT_SYMLINK_NOFOLLOW: act on a symbolic link itself rather than on what it points at.
Getcwd
pub func Getcwd(buffer: *var char8, size: uint) -> int64
Writes the current working directory's absolute path into buffer, NUL-terminated.
Returns the length written including the terminator, or a negative error number; ERANGE where the buffer is
too small for the answer.
buffer must be writable for size bytes.
Parameters
| Name | Type | Description |
|---|---|---|
buffer | *var char8 | receives the working directory as a null-terminated path |
size | uint | how many bytes buffer holds |
Returns: the bytes written including the terminator, or a negative error number
Chdir
pub func Chdir(path: *char8) -> int64
Makes path the process's working directory.
Returns zero, or a negative error number. The change is process-wide and immediately visible to every thread,
which is why nothing above this should call it casually.
Parameters
| Name | Type | Description |
|---|---|---|
path | *char8 | the directory to move to |
Returns: zero, or a negative error number
ReadlinkAt
pub func ReadlinkAt(
directoryFd: FileDescriptor,
path: *char8,
buffer: *var char8,
size: uint
) -> int64
Reads the text a symbolic link holds into buffer.
directoryFd is AtFdCwd for an ordinary path. Returns the number of bytes written, or a negative error
number. The text is not NUL-terminated, and is silently truncated where it does not fit, so a caller that
filled the buffer exactly cannot tell truncation from a perfect fit and should retry with more room.
Parameters
| Name | Type | Description |
|---|---|---|
directoryFd | FileDescriptor | the directory path is relative to, or AtFdCwd for an ordinary path |
path | *char8 | the symbolic link to read |
buffer | *var char8 | receives the link's text, without a terminator |
size | uint | how many bytes buffer holds |
Returns: the bytes written, which equals size when the text was truncated, or a negative error number
UnlinkAt
pub func UnlinkAt(
directoryFd: FileDescriptor,
path: *char8,
flags: int32
) -> int64
Removes the name path from its directory.
flags is zero to remove a file, or RemoveDirectory to remove an empty directory. Returns zero, or a negative
error number.
Removing a name does not free the file: anything still holding it open keeps reading and writing it, and the
storage goes only when the last descriptor closes.
path must be non-null and terminated.
Parameters
| Name | Type | Description |
|---|---|---|
directoryFd | FileDescriptor | the directory path is relative to, or AtFdCwd for an ordinary path |
path | *char8 | the name to remove |
flags | int32 | zero for a file, or RemoveDirectory for a directory |
Returns: zero, or a negative error number
RemoveDirectory
pub const RemoveDirectory: int32 = 512;
AT_REMOVEDIR: make UnlinkAt remove a directory rather than a file.
MkdirAt
pub func MkdirAt(
directoryFd: FileDescriptor,
path: *char8,
mode: FileMode
) -> int64
Creates a directory named path with permission bits mode.
Returns zero, or a negative error number — EEXIST when the name is taken, which is how this is used as a lock.
path must be non-null and terminated. The mode is masked by the process umask.
Parameters
| Name | Type | Description |
|---|---|---|
directoryFd | FileDescriptor | the directory path is relative to, or AtFdCwd for an ordinary path |
path | *char8 | the directory to create |
mode | FileMode | the permission bits for it |
Returns: zero, or a negative error number
RenameAt
pub func RenameAt(
oldDirectoryFd: FileDescriptor,
oldPath: *char8,
newDirectoryFd: FileDescriptor,
newPath: *char8
) -> int64
Renames oldPath to newPath, each resolved against its own directory descriptor.
Returns zero, or a negative error number. Within one file system this replaces any existing newPath atomically,
which is what makes it the way to publish a file that was written under a temporary name. Across file systems it
reports EXDEV rather than copying.
Both paths must be non-null and terminated.
Parameters
| Name | Type | Description |
|---|---|---|
oldDirectoryFd | FileDescriptor | the directory oldPath is relative to |
oldPath | *char8 | the name to move |
newDirectoryFd | FileDescriptor | the directory newPath is relative to |
newPath | *char8 | the name to move it to |
Returns: zero, or a negative error number