std1.0.0-beta-7 SafeC docs

Module dir

#include <std/dir.h> source

Directories and file system entries (hosted)

Listing, creating, copying and removing directories, and querying what a path names. Backends: dir_posix.sc (Linux, macOS, BSD) and dir_win32.sc (Windows); the tree operations (create_all, walk, copy, remove_all) are shared, in dir.sc. Paths are '/'-separated (Windows accepts '/' too); see std/path.h for joining and normalizing them.

?&Dir d = Dir::open("docs");
match (d) {
    case some(dir): {
        struct DirEntry e = DirEntry::new();
        while (dir.next(&e)) { println(e.name.as_ptr()); }
        e.free();
        dir.close();
    }
    case null: { eprintln("cannot open docs/"); }
}

struct Vec<struct String> files = Vec::new();
dir_walk("docs", &files);            // every file below docs/, sorted
dir_copy_tree("public", "dist");     // cp -R public/. dist/

All int-returning operations return 1 on success and 0 on failure.

Structs

DirEntry
DirAn open directory listing.
FileStatWhat file_stat reports.

Functions

file_statFills 'out' for 'path' (symbolic links are not followed). 0, with out.kind = DIR_NONE, if nothing is there.
path_kindThe kind of 'path' (DIR_NONE when nothing is there).
path_exists1 if something exists at 'path'.
dir_createCreates one directory (its parent must exist). 1 also when it already exists as a directory.
dir_create_allCreates 'path' and every missing parent (mkdir -p).
dir_removeRemoves an empty directory.
dir_remove_allRemoves 'path' and everything below it (rm -rf); links are removed, not followed. 1 also when nothing was there.
file_removeDeletes a file (or a link).
file_copyCopies the file 'from' to 'to' (created or truncated).
dir_walkAppends the path of every file below 'root', relative to it and '/'-separated ("guide/intro.md"), to 'out', sorted. Directories are descended into (links to them are not); hidden entries are included.
dir_copy_treeCopies everything inside 'from' into 'to' (cp -R from/. to/), creating 'to' and subdirectories as needed; existing files are overwritten.

Macros

DIR_NONEWhat a path or directory entry names.
DIR_FILEa regular file
DIR_DIRa directory
DIR_LINKa symbolic link (not followed) / Windows reparse point
DIR_OTHERdevice, pipe, socket, ...