Directories and links v0.1.0
RemoveDirectory
pub const RemoveDirectory: int32 = 0x0800;
AT_REMOVEDIR: make UnlinkAt remove a directory rather than a file.
Getcwd
pub func Getcwd(buffer: *var char8, size: uint) -> int64
Writes the current working directory's absolute path into buffer, NUL-terminated.
Returns zero, or a negative error number — unlike Linux's, which returns the length. 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.
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.
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
SymlinkNoFollow
pub const SymlinkNoFollow: int32 = 0x0200;
AT_SYMLINK_NOFOLLOW: act on a symbolic link itself rather than on what it points at.
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
Getdirentries
pub func Getdirentries(
fd: FileDescriptor,
buffer: *var opaque,
count: uint,
position: *var int64
) -> int64
Reads directory entries from fd into buffer.
The buffer holds packed dirent records: the file number 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), padding at 19, the name's length at 20
(2 bytes), padding at 22, and the name from 24. The kernel writes as many whole records as fit.
The fourth parameter is named as macOS names Getdirentries64's, which is the same output.
buffer must be writable for count bytes, and position must be null or writable for an int64.
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | an open directory descriptor |
buffer | *var opaque | receives the packed records |
count | uint | how many bytes buffer holds |
position | *var int64 | receives the position of the block read, and may be null |
Returns: the bytes filled, zero at the end of the directory, 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 for a file or RemoveDirectory for an empty directory. Removing a name does not free the file:
anything still holding it open keeps using it.
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
MkdirAt
pub func MkdirAt(
directoryFd: FileDescriptor,
path: *char8,
mode: FileMode
) -> int64
Creates a directory named path with permission bits mode.
Reports EEXIST when the name is taken, which is how this is used as a lock.
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 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.
Within one file system this replaces any existing newPath atomically. 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