cromo The Cromo Guide
Download PDF IT

std.fs

Files and folders: a file read or written whole, as text or as bytes, or open and read and written in pieces (File), or mapped in memory (Mapping); the folders and what they hold, links, and the parts of a path. A path is a string, absolute or from the current folder, with / between the names on every system, and \ too on Windows.

A function that fails throws an exception whose message has the action, the path and the reason: "cannot read notes.txt: no such file or folder".

cromo
import std.fs

fs.createFolders("data/logs")
fs.writeText("data/notes.txt", "first line\n")
fs.appendText("data/notes.txt", "second line\n")
for (string line in fs.readText("data/notes.txt").trimEnd().split("\n")) {
    println("> {}", line)
}
for (string name in fs.listFolder("data")) {
    println("{} {}", name, fs.isFolder(fs.joinPath("data", name)))
}
fs.removeAll("data")

The guide: Files, processes and time.

Made from the documentation in the sources of the library.

#Types

#Mode

cromo
enum Mode

How a file is opened.

  • Read: to read: the file must exist
  • Update: to read and write an existing file, which stays as it is
  • Create: to read and write a new file, or an existing one emptied
  • Append: to write at its end, created if it does not exist; it is not read

#File

cromo
struct File

A file open, read and written in pieces at a position, for the indices and the databases too large to be read whole. Its destructor closes it; it cannot be copied, only moved. A function that fails throws an exception with the path.

cromo
fs.File index = fs.File("games.idx", fs.Mode.Read)
list<u8> entry = index.readAt(47 * 1024, 47)   // 47 bytes from that position
cromo
File(string name, Mode how)

Opens the file at the path, as mode says. Throws if it cannot be opened.

#File.read

cromo
list<u8> read(i64 count)

Reads up to count bytes from the position, which moves after them: fewer at the end of the file, none after it.

#File.readAt

cromo
list<u8> readAt(i64 position, i64 count)

Reads up to count bytes from position, from 0, without moving the position: fewer at the end of the file.

#File.write

cromo
void write(list<u8> bytes)

Writes the bytes at the position, which moves after them; in a file opened to append, at its end.

#File.writeAt

cromo
void writeAt(i64 position, list<u8> bytes)

Writes the bytes at position, from 0, without moving the position; beyond the end the file grows, with zeros between.

#File.seek

cromo
void seek(i64 position)

Moves the position to position, from 0; beyond the end too, where a write makes the file grow.

#File.position

cromo
i64 position()

The position, from 0, where the next read or write starts.

#File.size

cromo
i64 size()

The size of the file in bytes, now.

#File.sync

cromo
void sync()

Sends what was written to the disk, before it returns: the data survive a fall of the system.

#Mapping

cromo
struct Mapping

A file mapped in memory, read only: its bytes and its numbers are read at a position, little-endian, with the check that they are inside it, which a panic stops as an index out of a list. Fast for large files, which the system reads as they are used. Its destructor unmaps it; it cannot be copied. A file that changes while it is mapped may show the change.

cromo
fs.Mapping names = fs.Mapping("names.bin")
u32 count = names.u32At(0)
list<u8> first = names.bytes(4, 32)
cromo
Mapping(string path)

Maps the file at the path. Throws if it cannot be read.

#Mapping.size

cromo
i64 size() const

The size of the file in bytes.

#Mapping.u8At

cromo
u8 u8At(i64 position) const

The u8, without sign, of the byte at position, from 0. A position out of the file is a panic.

#Mapping.i8At

cromo
i8 i8At(i64 position) const

The i8, with sign, of the byte at position, from 0. A position out of the file is a panic.

#Mapping.u16At

cromo
u16 u16At(i64 position) const

The u16 of the 2 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.i16At

cromo
i16 i16At(i64 position) const

The i16 of the 2 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.u32At

cromo
u32 u32At(i64 position) const

The u32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.i32At

cromo
i32 i32At(i64 position) const

The i32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.u64At

cromo
u64 u64At(i64 position) const

The u64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.i64At

cromo
i64 i64At(i64 position) const

The i64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.f32At

cromo
f32 f32At(i64 position) const

The f32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.f64At

cromo
f64 f64At(i64 position) const

The f64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.

#Mapping.bytes

cromo
list<u8> bytes(i64 position, i64 count) const

count bytes from position.

#Mapping.text

cromo
string text(i64 position, i64 count) const

count bytes from position as a text, which must be UTF-8.

#Functions

#readBytes

cromo
list<u8> readBytes(string path)

The bytes of a file. Throws if it cannot be read.

#readText

cromo
string readText(string path)

The text of a file, which must be UTF-8. Throws if the file cannot be read, or if it is not UTF-8.

#writeBytes

cromo
void writeBytes(string path, list<u8> bytes)

Writes the bytes to a file, which is created, or replaced if it exists. Throws if it cannot be written, as when its folder is missing.

#writeText

cromo
void writeText(string path, string text)

Writes the text to a file, which is created, or replaced if it exists. Throws if it cannot be written, as when its folder is missing.

#appendText

cromo
void appendText(string path, string text)

Adds the text at the end of the file, which is created if it does not exist.

#createFolders

cromo
void createFolders(string path)

Creates a folder with the ones above it that are missing, as mkdir -p. Nothing happens if the folder exists already; throws if the path, or one above it, is a file.

#removeAll

cromo
void removeAll(string path)

Removes a file, or a folder with all that it holds. A link is removed itself, never what it points to. Nothing happens if the path does not exist.

#joinPath

cromo
string joinPath(string first, string second)

The two paths with the separator of the system between them: joinPath("a", "b.txt") gives "a/b.txt", or "a\\b.txt" on Windows. No separator is added when first ends with one, and an empty first gives second.

#parent

cromo
string parent(string path)

The folder of a path: "a/b/c.txt" gives "a/b", "c.txt" gives "", "/c" gives "/", and on Windows "C:\\c" gives "C:\\".

#fileName

cromo
string fileName(string path)

The last name of a path: "a/b/c.txt" gives "c.txt", and "a/b/" gives "b".

#extension

cromo
string extension(string path)

The extension of the name, with its point: "c.tar.gz" gives ".gz", "notes" and ".profile" give "".

#exists

cromo
bool exists(string path)

Whether a file or a folder exists at the path. It never throws: a path that cannot be reached gives false.

#isFolder

cromo
bool isFolder(string path)

Whether the path is a folder, or a link to one; false if nothing is there.

#isFile

cromo
bool isFile(string path)

Whether the path is a file, or a link to one; false if nothing is there.

cromo
bool isLink(string path)

Whether the path is a link: a symbolic link, or on Windows a junction too. The link itself is checked, not what it points to; false if nothing is there.

#size

cromo
i64 size(string path)

The size of a file, in bytes. Throws if it does not exist.

#modified

cromo
i64 modified(string path)

The time of the last change of a file or a folder, in seconds from 1970-01-01 UTC, as time.unix() gives it. Throws if the path does not exist.

#rename

cromo
void rename(string from, string to)

Renames or moves a file or a folder; to is the new path, also in another folder, and a file already there is replaced. Throws if from does not exist. Only a file on Windows can move to another disk: otherwise both paths must be on the same one.

#copy

cromo
void copy(string from, string to)

Copies the file from to to, which is replaced if it exists. Throws if from does not exist or is a folder. On Linux the copy does not keep the permissions of the original: call makeExecutable for a program.

#isSeparator

cromo
bool isSeparator(u8 c)

Whether the byte separates the names of a path: /, and on Windows \\ too.

#separator

cromo
string separator()

The separator that joinPath puts between the names of a path: "/" on Linux, "\\" on Windows.

#remove

cromo
void remove(string path)

Removes a file, a link or an empty folder; a link is removed itself, not what it points to. Throws if the path does not exist, or if the folder is not empty: removeAll removes a folder with all that it holds.

#createFolder

cromo
void createFolder(string path)

Creates a folder. Throws if the path exists already, or if the folder above it is missing: createFolders creates those too.

#listFolder

cromo
list<string> listFolder(string path)

The names of the files, the folders and the links in a folder, without "." and "..", in no order: joinPath(path, name) gives the path of each. Throws if the folder does not exist.

cromo
void createLink(string target, string path)

Makes path a link to target. On Linux it is a symbolic link that holds target as it is written: a relative target is read from the folder of the link. On Windows it is a junction, which needs no administrator rights but can point only to a folder, and holds the absolute path of target. Throws if path exists already, and on Windows if target is not a folder.

cromo
string readLink(string path)

Where the link at the path points, as the link holds it: for a junction of Windows, an absolute path. Throws if the path does not exist or is not a link.

#makeExecutable

cromo
void makeExecutable(string path)

Makes a file executable: on Linux it adds the permission to run it for those who can read it, as chmod +x does; on Windows, which runs a file by its extension, nothing changes. Throws if the file does not exist.

#tempFolder

cromo
string tempFolder()

The folder of the temporary files of the system, without a separator at its end: that of the user on Windows, and on Linux the one of the variable TMPDIR, or "/tmp".

#absolute

cromo
string absolute(string path)

The absolute path of a path: one from the current folder is joined to it, and the names "." and ".." are taken out. The file may not exist, and the links stay as they are: see canonical.

#canonical

cromo
string canonical(string path)

The absolute path of a file or a folder that exists, with every link followed: two paths of the same file give the same text. Throws if the path does not exist.