Skip to content
CryoCryo home

directory

import std::fs::directory; · source

Creating, removing, and listing directories. Every operation is a static on Directory, and every failure names the path it happened at.

import std::fmt;
import std::fs::directory::{ Directory };

Directory::create_all("build/cache")?;
for (entry in Directory::entries("src")?) {
    if (entry.is_file()) { fmt::println(f"{entry.path().as_str()}"); }
}
Directory::remove_all("build")?;

Types

Directory

type struct Directory {}
MethodNotes
create(path)One directory, mode DEFAULT_DIRECTORY_MODE (0755). Its parent must exist; AlreadyExists if anything is there.
create_all(path)mkdir -p. A directory already there is success, including one another process creates at the same moment.
remove(path)An empty directory; one with entries fails with DirectoryNotEmpty.
remove_all(path)rm -r. A symlink inside is removed itself, not followed. Stops at the first failure.
copy_all(from, to)The tree under from copied to to, made with any missing parents. A link to a directory is left out, so the copy cannot reach outside from or loop.
entries(path)A listing of the directory, without . and ...

Files are removed with File::remove, and any entry is moved with Path.rename_to or made absolute with Path.canonicalize.

Static functions

static create(path: Path) -> Result<(), IoError>;

Create the directory path. Its parent must exist; see create_all. Fails with AlreadyExists if anything is there.

static create(path: Path) -> Result<(), IoError>;

Create the directory path, whose parent must exist; as the copy above.

static create_all(path: Path) -> Result<(), IoError>;

Create the directory path and every missing directory above it. A directory already there is success, including one another process creates at the same moment; anything else in the way is not.

static remove(path: Path) -> Result<(), IoError>;

Remove the empty directory path. A directory with entries fails with DirectoryNotEmpty; see remove_all.

static remove(path: Path) -> Result<(), IoError>;

Remove the empty directory path; as the copy above.

static copy_all(from: Path, to: Path) -> Result<(), IoError>;

Copy the directory from and everything in it to to, which is made with any missing parents. A file, or a link to one, is copied as a file; a link to a directory is not followed and is left out, so the copy cannot reach outside from or loop. Stops at the first failure, which names the entry it could not copy.

static remove_all(path: Path) -> Result<(), IoError>;

Remove the directory path and everything in it. A symlink inside is removed itself; what it points at is left alone. Stops at the first failure, which names the entry it could not remove.

static entries(path: Path) -> Result<DirectoryEntries, IoError>;

The entries of the directory path, without . and ...

static entries(path: Path) -> Result<DirectoryEntries, IoError>;

The entries of the directory path, without . and ..; as the copy above.

DirectoryEntries

type struct DirectoryEntries {}

entries(path) returns a DirectoryEntries, an iterator of DirectoryEntry values that holds the directory open until it is dropped. The order is the filesystem's own. Linux lists with opendir / readdir; Windows with FindFirstFileW / FindNextFileW, so names outside the ANSI code page come back intact (a name that is not valid UTF-16 has no Str form and is skipped).

DirectoryEntry

type struct DirectoryEntry {
    private entry_path:     PathBuf;
    private name_length:    i64;
    private kind:           FileType;
    private directory_link: boolean;
}

One entry of a listing: its own name(), its full path() (the listed directory joined with the name), and its file_type(). The type comes from the listing itself, so a symlink is reported as Symlink, not as what it points at; a filesystem that leaves the type unknown in the listing is asked with lstat. metadata() follows a symlink.

Methods

name(&this) -> Str;

The entry's own name, with no directory part.

path(&this) -> Path;

The listed directory joined with the entry's name.

file_type(&this) -> FileType;

The kind of entry, from the listing itself; a symlink is reported as Symlink, not as what it points at.

is_file(&this) -> boolean;

True when the entry is a regular file, as the listing reports it.

is_directory(&this) -> boolean;

True when the entry is a directory, as the listing reports it.

metadata(&this) -> Result<Metadata, IoError>;

What is at the entry's path, following a symlink.

Trait implementations

DirectoryEntries

implement trait Iterator<DirectoryEntry> for struct DirectoryEntries
implement trait Drop for struct DirectoryEntries

DirectoryEntry

implement trait Drop for struct DirectoryEntry

Constants

const DEFAULT_DIRECTORY_MODE: u32 = libc::S_IRWXU | libc::S_IRGRP | libc::S_IXGRP | libc::S_IROTH | libc::S_IXOTH;