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".
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.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Mode
enum ModeHow a file is opened.
Read: to read: the file must existUpdate: to read and write an existing file, which stays as it isCreate: to read and write a new file, or an existing one emptiedAppend: to write at its end, created if it does not exist; it is not read
#File
struct FileA 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.
fs.File index = fs.File("games.idx", fs.Mode.Read)
list<u8> entry = index.readAt(47 * 1024, 47) // 47 bytes from that positionFile(string name, Mode how)Opens the file at the path, as mode says. Throws if it cannot be opened.
#File.read
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
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
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
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
void seek(i64 position)Moves the position to position, from 0; beyond the end too, where a write makes the file grow.
#File.position
i64 position()The position, from 0, where the next read or write starts.
#File.size
i64 size()The size of the file in bytes, now.
#File.sync
void sync()Sends what was written to the disk, before it returns: the data survive a fall of the system.
#Mapping
struct MappingA 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.
fs.Mapping names = fs.Mapping("names.bin")
u32 count = names.u32At(0)
list<u8> first = names.bytes(4, 32)Mapping(string path)Maps the file at the path. Throws if it cannot be read.
#Mapping.size
i64 size() constThe size of the file in bytes.
#Mapping.u8At
u8 u8At(i64 position) constThe u8, without sign, of the byte at position, from 0. A position out of the file is a panic.
#Mapping.i8At
i8 i8At(i64 position) constThe i8, with sign, of the byte at position, from 0. A position out of the file is a panic.
#Mapping.u16At
u16 u16At(i64 position) constThe u16 of the 2 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.i16At
i16 i16At(i64 position) constThe i16 of the 2 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.u32At
u32 u32At(i64 position) constThe u32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.i32At
i32 i32At(i64 position) constThe i32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.u64At
u64 u64At(i64 position) constThe u64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.i64At
i64 i64At(i64 position) constThe i64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.f32At
f32 f32At(i64 position) constThe f32 of the 4 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.f64At
f64 f64At(i64 position) constThe f64 of the 8 bytes, little-endian from position, from 0. Bytes out of the file are a panic.
#Mapping.bytes
list<u8> bytes(i64 position, i64 count) constcount bytes from position.
#Mapping.text
string text(i64 position, i64 count) constcount bytes from position as a text, which must be UTF-8.
#Funzioni
#readBytes
list<u8> readBytes(string path)The bytes of a file. Throws if it cannot be read.
#readText
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
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
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
void appendText(string path, string text)Adds the text at the end of the file, which is created if it does not exist.
#createFolders
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
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
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
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
string fileName(string path)The last name of a path: "a/b/c.txt" gives "c.txt", and "a/b/" gives "b".
#extension
string extension(string path)The extension of the name, with its point: "c.tar.gz" gives ".gz", "notes" and ".profile" give "".
#exists
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
bool isFolder(string path)Whether the path is a folder, or a link to one; false if nothing is there.
#isFile
bool isFile(string path)Whether the path is a file, or a link to one; false if nothing is there.
#isLink
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
i64 size(string path)The size of a file, in bytes. Throws if it does not exist.
#modified
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
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
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
bool isSeparator(u8 c)Whether the byte separates the names of a path: /, and on Windows \\ too.
#separator
string separator()The separator that joinPath puts between the names of a path: "/" on Linux, "\\" on Windows.
#remove
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
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
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.
#createLink
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.
#readLink
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
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
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
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
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.