ubyte[] read(string filepath, ssize_t size = -1, ssize_t offset = 0)

Reads the contents of the specified file as a byte array.

Parameters

path

Path to the file.

size

Maximum number of bytes to read. If size < 0, the entire file is read.

offset

Byte offset from which to start reading.

Returns

the file bytes.

void write(string filepath, ubyte[] data)

Writes bytes to the specified file. Always overwrites the file completely. The function is intended only for writing to regular files.

Parameters

path

Path to the file.

data

Byte array.

void copy(
    string src, 
    string dst, 
    bool sync = false, 
    bool keepMetaData = false
)

Copies a file to a new location.

Parameters

src

Source file path.

dst

Destination path.

sync

Synchronize (or not) a state with storage device.

keepMetaData

Whether to copy access mode, timestamps, x-attributes.

bool isEmptyDir(string dirPath)

Checks if the path is an empty directory.

FileEntry[] getFiles(
    string dir, 
    Flag!"hidden" hidden = Yes.hidden, 
    Flag!"recursively" recursively = No.recursively, 
    Flag!"absPath" saveAbsolutePath = Yes.absPath, 
    Flag!"enableFilter" enableFilter = No.enableFilter, 
    FileType[] includedTypes = []
)

Reads entries of the directory.

Parameters

dir

Directory for listing files.

hidden

Whether to include hidden files. Yes, by default.

recursively

Whether to search in subdirectories. No, by default.

saveAbsolutePath

Whether to store an absolute path or not. If Yes, a relative path of dir is converted to absolute. The parameter has no effect if dir is an absolute path. Has value Yes, by default.

enableFilter

Whether to filter by type. No, by default.

includedTypes

Types for including to final result. Used in conjunction with the previous parameter.

FileEntry[] getRegularFiles(
    string dir, 
    Flag!"hidden" hidden = Yes.hidden, 
    Flag!"recursively" recursively = No.recursively, 
    Flag!"absPath" saveAbsolutePath = Yes.absPath
)

Gets regular files from the directory.

Parameters

dir

Directory for listing regular files.

hidden

Whether to include hidden files. Yes, by default.

recursively

Whether to search in subdirectories. No, by default.

saveAbsolutePath

Whether to write the full path. Yes, by default.

FileEntry[] getDirectories(
    string dir, 
    Flag!"hidden" hidden = Yes.hidden, 
    Flag!"recursively" recursively = No.recursively, 
    Flag!"absPath" saveAbsolutePath = Yes.absPath
)

Gets directories from the directory.

Parameters

dir

Directory for listing directories.

hidden

Whether to include hidden files. Yes, by default.

recursively

Whether to search in subdirectories. No, by default.

saveAbsolutePath

Whether to write the full path. Yes, by default.

FileEntry[] getSymlinks(
    string dir, 
    Flag!"hidden" hidden = Yes.hidden, 
    Flag!"recursively" recursively = No.recursively, 
    Flag!"absPath" saveAbsolutePath = Yes.absPath
)

Gets symlinks from the directory.

Parameters

dir

Directory for listing symlinks.

hidden

Whether to include hidden files. Yes, by default.

recursively

Whether to search in subdirectories. No, by default.

saveAbsolutePath

Whether to write the full path. Yes, by default.

string readLinkRecurse(string link)

Returns read symlink path. If the symlink points to other symlink, the function returns path of the last symlink of the link chain.

bool isBrokenSymlink(string link, Flag!"recurse" recurse = No.recurse)

Checks if a file is a broken symbolic link is broken (whether indicates a non-existent location).

Parameters

link

Path to link.

recurse

Whether to check all nested symbolic links in a chain.

bool isPipe(in string path)

Checks if a path is a pipe file (FIFO).

bool isPipe(const FileEntry entry)

Checks if a FileEntry object related to a pipe file.

bool isPipe(ref const FileEntry entry)

Checks if a FileEntry object related to a pipe file.

bool isSocket(in string path)

Checks if a path is a socket.

bool isSocket(const FileEntry entry)

Checks if a FileEntry object related to a socket file.

bool isSocket(ref const FileEntry entry)

Checks if a FileEntry object related to a socket file.

bool isSymlink(in string path)

Checks if a path is a symbolic link.

bool isSymlink(const FileEntry entry)

Checks if a FileEntry object related to a symbolic link.

bool isSymlink(ref const FileEntry entry)

Checks if a FileEntry object related to a symbolic link.

bool isRegularFile(in string path)

Checks if a path is a regular file.

bool isRegularFile(const FileEntry entry)

Checks if a FileEntry object related to a regular file.

bool isRegularFile(ref const FileEntry entry)

Checks if a FileEntry object related to a regular file.

bool isBlockDevice(in string path)

Checks if a path is a block device.

bool isBlockDevice(const FileEntry entry)

Checks if a FileEntry object related to a block device.

bool isBlockDevice(ref const FileEntry entry)

Checks if a FileEntry object related to a block device.

bool isDir(in string path)

Checks if a path is a directory.

bool isDir(const FileEntry entry)

Checks if a FileEntry object related to a directory.

bool isSymlinkToDir(in string path)

Checks if the file is a symbolic link to a directory. The check is recursive (if the link points to a link).

Parameters

path

path fo file

Returns

whether a symbolic link is a link to a directory.

bool isCharDevice(in string path)

Checks if a path is a character device.

string getAbsolutePath(string path)

Returns absolute path for any raw path. Trailing slashes are removed.

void copyRecurse(
    string from, 
    string to, 
    Flag!"followLinks" followLinks = No.followLinks, 
    Flag!"keepMetaData" keepMetaData = No.keepMetaData, 
    Flag!"passErrors" passErrors = No.passErrors, 
    Flag!"printErrors" printErrors = Yes.printErrors
)

Function for copying files and directories. Files can have multiple hard links, and that is accounted for.

Parameters

from

source file

to

destination

followLinks

whether to follow symbolic links

keepMetaData

Whether to copy access mode, timestamps, x-attributes.

passErrors

if No, the 'copyRecurse()' function can throw exceptions else errors are ignored

printErrors

print information about errors or not (the parameter works if passErrors is Yes)

void copyMetaData(string src, string dst)

Copies access mode, timestamps, x-attributes from the 1st file to the 2nd file.

extern(C)
int statx( int dfd, const char* filename, int flags, uint mask, statx_t* stx )

This function returns information about a file, storing it in the buffer pointed to by statxbuf. System call of Linux 4.11+, see more: man statx This function is used in the high-level FileStat structure type.

auto STATX_TYPE

See

man statx

auto STATX_DEFAULT

Mask for stat field by default.

statx_t getStatX(string filepath)

Returns file information as statx_t. Wrapper for low level statx call.

stat_t getStat(string filepath)

High-level function for getting stat_t from file path.

FileType getFileTypeFromFileMode(uint mode) nothrow

Gets FileType by stat field 'mode'.

FileType getFileTypeFromFileMask(uint mask) nothrow

Gets FileType by the file mask obtained from stat field 'mode'.

string makeUnixFileModeLine(in uint mode)

The function returns file permissions in a string form (like -rwxrwxrwx)

string makeUnixFileModeLine(const FileStat st)

The function returns file permissions in a string form (like -rwxrwxrwx)

void hardlink(string original, string link)

Creates a new hard link (make a new name for a file, see man 2 link).

Parameters

original

Original file path.

link

New link location path.

bool exists(string filepath) nothrow

Checks if the given file exists.

void truncate(string filepath, size_t size)

Truncate a file to a specified length.

Parameters

filepath

File to be resized.

size

New length in bytes.

void safeMkdir(string path)

Create a directory only if it doesn't exist.

void applyFileAdvise(File f, FileAdvice advice)

Predeclare an access pattern for file data.

Parameters

f

File object.

advice

Access patern.

See Also:

man 2 posix_fadvise

void applyFileAdvise(int fd, FileAdvice advice)

Predeclare an access pattern for file data.

Parameters

fd

Open file descriptor.

advice

Access patern.

See Also:

man 2 posix_fadvise

The module provides many function related to files.

Public imports