Directories and links v0.1.0

Source
The working directory, directory entries, removal, renaming and links.

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.

Source

Parameters

NameTypeDescription
fdFileDescriptoran open descriptor
buffer*var opaquereceives the packed directory records
countuinthow many bytes buffer holds

Returns: the bytes filled, zero at the end of the directory, or a negative error number

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.

Source

Parameters

NameTypeDescription
oldDirectoryFdFileDescriptorthe directory oldPath is relative to
oldPath*char8the existing name
newDirectoryFdFileDescriptorthe directory newPath is relative to
newPath*char8the new name for the same file
flagsint32zero, or FollowSymlinks to link what a final link points at

Returns: zero, or a negative error number

pub const FollowSymlinks: int32 = 0x0400;

AT_SYMLINK_FOLLOW: make LinkAt link a symbolic link's target rather than the link.

Source

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.

Source

Parameters

NameTypeDescription
target*char8the text the link will hold, which need not exist
newDirectoryFdFileDescriptorthe directory linkPath is relative to
linkPath*char8the link to create

Returns: zero, or a negative error number

pub const SymlinkNoFollow: int32 = 0x0100;

AT_SYMLINK_NOFOLLOW: act on a symbolic link itself rather than on what it points at.

Source

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.

Source

Parameters

NameTypeDescription
buffer*var char8receives the working directory as a null-terminated path
sizeuinthow 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.

Source

Parameters

NameTypeDescription
path*char8the directory to move to

Returns: zero, or a negative error number

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.

Source

Parameters

NameTypeDescription
directoryFdFileDescriptorthe directory path is relative to, or AtFdCwd for an ordinary path
path*char8the symbolic link to read
buffer*var char8receives the link's text, without a terminator
sizeuinthow many bytes buffer holds

Returns: the bytes written, which equals size when the text was truncated, or a negative error number

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.

Source

Parameters

NameTypeDescription
directoryFdFileDescriptorthe directory path is relative to, or AtFdCwd for an ordinary path
path*char8the name to remove
flagsint32zero 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.

Source

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.

Source

Parameters

NameTypeDescription
directoryFdFileDescriptorthe directory path is relative to, or AtFdCwd for an ordinary path
path*char8the directory to create
modeFileModethe 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.

Source

Parameters

NameTypeDescription
oldDirectoryFdFileDescriptorthe directory oldPath is relative to
oldPath*char8the name to move
newDirectoryFdFileDescriptorthe directory newPath is relative to
newPath*char8the name to move it to

Returns: zero, or a negative error number