Directories and links v0.1.0

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

RemoveDirectory

pub const RemoveDirectory: int32 = 0x0080;

AT_REMOVEDIR: make UnlinkAt remove a directory rather than a file.

Source

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. 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.

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.

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 const SymlinkNoFollow: int32 = 0x0020;

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

Source

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 = 0x0040;

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

Getdirentries64

pub func Getdirentries64(
    fd: FileDescriptor,
    buffer: *var opaque,
    count: uint,
    position: *var int64
) -> 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 64-bit-inode dirent records: the inode at offset 0 (8 bytes), a seek cookie at 8 (8 bytes), the record's length at 16 (2 bytes), the name's length at 18 (2 bytes), the entry type at 20 (1 byte), and the name from 21. The kernel writes as many whole records as fit.

Source

Parameters

NameTypeDescription
fdFileDescriptoran open descriptor
buffer*var opaquereceives the packed directory records
countuinthow many bytes buffer holds
position*var int64receives the position of the block read

Returns: the bytes filled, zero at the end of the directory, 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 for a file or RemoveDirectory for an empty directory. Removing a name does not free the file. 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

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. path must be non-null and terminated.

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. Within one file system this replaces any existing newPath atomically. Across file systems it reports EXDEV. 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