fio
Values
pub fn append(
path: String,
content: String,
) -> Result(Nil, error.FioError)
Append UTF-8 text to a file (creates if missing).
pub fn append_bits(
path: String,
content: BitArray,
) -> Result(Nil, error.FioError)
Append raw bytes (BitArray) to a file.
pub fn atomic(
path: String,
callback: fn(String) -> Result(a, error.FioError),
) -> Result(a, error.FioError)
Executes a callback providing a temporary file path to write to.
If the callback succeeds, the temporary file is atomically renamed to path.
The temporary file is placed in the same directory as path using the
same .__fio_tmp_* prefix as write_atomic.
pub fn checksum(
path: String,
algorithm: types.HashAlgorithm,
) -> Result(String, error.FioError)
Compute a file checksum using the specified algorithm. Returns a hex-encoded string.
pub fn copy(
src: String,
dest: String,
) -> Result(Nil, error.FioError)
Copy a file from source to destination.
pub fn copy_directory(
src: String,
dest: String,
) -> Result(Nil, error.FioError)
Recursively copy a directory and its contents.
pub fn copy_if_newer(
src: String,
dest: String,
) -> Result(Bool, error.FioError)
Copy src to dest only when src is newer than dest.
If dest does not exist the copy always happens.
Returns Ok(True) when a copy was performed, Ok(False) when skipped.
pub fn create_directory(
path: String,
) -> Result(Nil, error.FioError)
Create a directory. Parent directory must exist.
pub fn create_directory_all(
path: String,
) -> Result(Nil, error.FioError)
Create a directory and all parent directories.
pub fn create_hard_link(
target target: String,
link link: String,
) -> Result(Nil, error.FioError)
Create a hard link to an existing file.
pub fn create_symlink(
target target: String,
link link: String,
) -> Result(Nil, error.FioError)
Create a symbolic link.
pub fn current_directory() -> Result(String, error.FioError)
Get the current working directory.
pub fn delete(path: String) -> Result(Nil, error.FioError)
Delete a file (not a directory).
pub fn delete_all(path: String) -> Result(Nil, error.FioError)
Delete a path recursively; idempotent (succeeds if missing).
pub fn delete_directory(
path: String,
) -> Result(Nil, error.FioError)
Delete an empty directory.
pub fn directory_name(path_str: String) -> String
Get the directory portion of a path.
pub fn ensure_file(path: String) -> Result(Nil, error.FioError)
Create a file if it does not already exist.
If the file already exists this is a no-op and returns Ok(Nil).
pub fn exists(path: String) -> Bool
Check if a path exists (file, directory, or symlink).
pub fn expand(path_str: String) -> Result(String, Nil)
Expand/normalize a path, resolving . and .. segments.
Returns Error(Nil) if .. would go above the root.
pub fn explain(err: error.FioError) -> String
Explains a FioError in a CLI-friendly format.
pub fn extension(path_str: String) -> Result(String, Nil)
Get the file extension (without dot).
pub fn file_info(
path: String,
) -> Result(types.FileInfo, error.FioError)
Get file metadata (follows symlinks).
pub fn is_directory(path: String) -> Result(Bool, error.FioError)
Check if a path is a directory (follows symlinks).
pub fn is_file(path: String) -> Result(Bool, error.FioError)
Check if a path is a regular file (follows symlinks).
pub fn is_symlink(path: String) -> Result(Bool, error.FioError)
Check if a path is a symbolic link (does not follow symlinks).
pub fn link_info(
path: String,
) -> Result(types.FileInfo, error.FioError)
Get file metadata without following symlinks.
pub fn list(path: String) -> Result(List(String), error.FioError)
List the contents of a directory (names only).
pub fn list_recursive(
path: String,
) -> Result(List(String), error.FioError)
Recursively list files and directories (paths relative to path).
pub fn read(path: String) -> Result(String, error.FioError)
Read a UTF-8 text file. Returns NotUtf8(path) on invalid UTF-8.
pub fn read_bits(
path: String,
) -> Result(BitArray, error.FioError)
Read a file as raw bytes (BitArray).
pub fn read_fold(
path: String,
chunk_size: Int,
initial: acc,
f: fn(acc, BitArray) -> acc,
) -> Result(acc, error.FioError)
Read a file in chunks, folding each chunk into an accumulator.
Opens the file, reads it in chunk_size-byte pieces, and calls f on each
chunk until EOF. The file handle is always closed before returning.
// Count bytes without loading the whole file into memory
fio.read_fold("big.bin", 65_536, 0, fn(acc, chunk) {
acc + bit_array.byte_size(chunk)
})
pub fn read_lines(
path: String,
) -> Result(List(String), error.FioError)
Reads a file and splits it into lines.
Both \n (Unix) and \r\n (Windows) line endings are normalised.
pub fn read_link(path: String) -> Result(String, error.FioError)
Read the target path of a symbolic link.
pub fn rename(
src: String,
dest: String,
) -> Result(Nil, error.FioError)
Rename or move a file or directory.
pub fn safe_relative(path_str: String) -> Result(String, Nil)
Validate that a path is a safe relative path (does not escape root).
On Windows it also normalizes backslashes into /.
pub fn set_permissions(
path: String,
permissions: types.FilePermissions,
) -> Result(Nil, error.FioError)
Set file permissions using the FilePermissions type.
pub fn set_permissions_octal(
path: String,
mode: Int,
) -> Result(Nil, error.FioError)
Set file permissions with an octal integer.
pub fn stream(
path: String,
) -> Result(List(String), error.FioError)
Reads a file in chunks and returns all chunks as a list of String.
Returns Error(NotUtf8) if any chunk is not valid UTF-8.
pub fn stream_bytes(
path: String,
) -> Result(List(BitArray), error.FioError)
Reads a file in chunks and returns all chunks as a list of BitArray.
Uses a 64 KiB chunk size. Returns Error if the file cannot be opened.
pub fn strip_extension(path_str: String) -> String
Remove the extension from a path.
pub fn touch(path: String) -> Result(Nil, error.FioError)
Touch a file: create or update modification time.
pub fn verify_checksum(
path: String,
expected: String,
algorithm: types.HashAlgorithm,
) -> Result(Bool, error.FioError)
Verify that a file’s checksum matches the expected hex-encoded hash.
pub fn with_extension(path_str: String, ext: String) -> String
Change the extension of a path.
pub fn with_opened(
path: String,
mode: handle.OpenMode,
callback: fn(handle.FileHandle) -> Result(a, error.FioError),
) -> Result(a, error.FioError)
Evaluates a callback with an opened file handle and guarantees the handle is closed at the end, even in case of errors.
pub fn with_temp_directory(
callback: fn(String) -> Result(a, error.FioError),
) -> Result(a, error.FioError)
Run a callback with a path to a temporary directory that is automatically deleted (recursively) when the callback returns.
pub fn with_temp_file(
callback: fn(String) -> Result(a, error.FioError),
) -> Result(a, error.FioError)
Run a callback with a path to a temporary file that is automatically deleted when the callback returns (even if it returns an Error).
pub fn with_writer(
path: String,
callback: fn(handle.FileHandle) -> Result(a, error.FioError),
) -> Result(a, error.FioError)
Evaluates a callback with a file handle opened for writing and guarantees the handle is closed at the end.
pub fn write(
path: String,
content: String,
) -> Result(Nil, error.FioError)
Write UTF-8 text to a file (creates/overwrites).
pub fn write_atomic(
path: String,
content: String,
) -> Result(Nil, error.FioError)
Write UTF-8 text atomically: writes to a sibling temp file, then renames
it into place with a single rename(2) syscall.
Readers never observe partial content. Returns AtomicFailed on error.
pub fn write_bits(
path: String,
content: BitArray,
) -> Result(Nil, error.FioError)
Write raw bytes (BitArray) to a file (creates/overwrites).
pub fn write_bits_atomic(
path: String,
content: BitArray,
) -> Result(Nil, error.FioError)
Write raw bytes (BitArray) atomically.
Same atomic guarantee as write_atomic.
pub fn write_if_changed(
path: String,
content: String,
) -> Result(Bool, error.FioError)
Writes content to a file. If the file already has identical content, it
skips rewriting and returns False. If it overwrote or created, returns True.
pub fn write_lines(
path: String,
lines: List(String),
) -> Result(Nil, error.FioError)
Joins lines with newlines and writes to a file.
pub fn write_new(
path: String,
content: String,
) -> Result(Nil, error.FioError)
Writes content to a file only if it doesn’t exist yet. Returns an Eexist
error if it does.