int openFileDescriptor(string path, int flags, int mode = oct!666)

Opens the file specified by path.

Parameters

path

Path to the file.

flags

Bit mask of flags for the 'open' system call. Flags are located in 'core.sys.posix.fcntl'.

mode

Specifies the file mode bits to be applied when a new file is created.

Returns

the file descriptor.

void closeFileDescriptor(int fd)

Closes the specified file descriptor.

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.

ubyte[] readFromFileDescriptor(int fd, ssize_t size = -1)

Reads the contents by the specified file descriptor.

Parameters

fd

File descriptor.

size

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

Returns

the read bytes.

void writeToFileDescriptor(T)(int fd, const ref T data)

Writes data to the specified file descriptor. Well suited for writing strings, arrays of numeric types, and scalar values. For complex data structures, use a cusom serialization approach.

Parameters

fd

File descriptor open for writing.

data

Data for writing.

void writeToFileDescriptor(int fd, const ubyte[] data)

Writes byte array to the specified file descriptor.

Parameters

fd

File descriptor open for writing.

data

Data for writing.

void write(T)(string filepath, const ref T data)

Writes data to the specified file.

Parameters

path

Path to the file.

data

Data for writing.

void append(T)(string filepath, const ref T data)

Appends data to the file end.

Parameters

filepath

Path to the file.

data

Data for writing.

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 permissions, 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

void rename(string oldPath, string newPath)

This function renames the specified file.

string buildPath(string[] args...) @safe pure nothrow

The function for creating a path from components. Its behavior towards components with leading slashes differs from the behavior of std.path.buildPath (see unit-tests).

string dirName(string path) @trusted nothrow pure

Returns the parent directory of the specified path.

inout(Char)[] dirName(Char)(inout(Char)[] path) @trusted nothrow pure 
if (std.traits.isSomeChar!Char || is(Char == byte) || is(Char == ubyte))

Returns the parent directory of the specified path.

string baseName(string path) @trusted nothrow pure

Gets the file name from the specified path, without any leading directory.

inout(Char)[] baseName(Char)(inout(Char)[] path) @trusted nothrow pure 
if (std.traits.isSomeChar!Char || is(Char == byte) || is(Char == ubyte))

Gets the file name from the specified path, without any leading directory.

The module provides many function related to files.

Public imports

Enums

FileType

Enumeration of UNIX file types.

FileAdvice

Access patterns for file data.

Variables

STATX_TYPE
STATX_DEFAULT

Mask for stat field by default.

Functions

openFileDescriptor isBrokenSymlink getStat
closeFileDescriptor isPipe getFileTypeFromFileMode
read isSocket getFileTypeFromFileMask
readFromFileDescriptor isSymlink makeUnixFileModeLine
writeToFileDescriptor isRegularFile hardlink
write isBlockDevice exists
append isDir truncate
copy isSymlinkToDir safeMkdir
isEmptyDir isCharDevice applyFileAdvise
getFiles getAbsolutePath rename
getRegularFiles copyRecurse buildPath
getDirectories copyMetaData dirName
getSymlinks statx baseName
readLinkRecurse getStatX

Structs

DirReader

Implementation of file list reader. Can be used in foreach as input range, the generator returns FileEntry.

FileEntry

Special type for files as entries in directories. Analogue of the 'dirent' structure type.

statx_t

A low-level structure to filling with the 'statx' system call.

FileStat

A structure for storing and retrieving file information on Linux.

Classes

DesktopEntry

Special set of functions for working with ini-like .desktop files.

xattr

Function set for working with file extended attributes.