82 lines
3.0 KiB
Plaintext
82 lines
3.0 KiB
Plaintext
#! @file files.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains what is done to a file by its path, without holding it open: reading
|
|
#! or writing the whole of it, describing it, whether anything is there,
|
|
#! removing and renaming it, and changing who may use it.
|
|
|
|
import system;
|
|
|
|
module std::io;
|
|
|
|
#! Reads the whole of a file.
|
|
#! @param at The path of the file to read.
|
|
#! @param alloc The optional allocator for the text to be held in.
|
|
#! @returns The contents of the file, or why it could not be read.
|
|
pub def read_file(at: &path, alloc: maybe<&mut allocator> = nothing{}): result<string, io_error> {
|
|
var opened := file::open(at) else (e) { return e; };
|
|
return opened.read_all(alloc);
|
|
}
|
|
|
|
#! Replaces the contents of a file, making it where there is none.
|
|
#! @param at The path of the file to write.
|
|
#! @param data The bytes to write.
|
|
#! @returns Why they could not all be written, where they could not.
|
|
pub def write_file(at: &path, data: span<u8>): result<void, io_error> {
|
|
var opened := file::create(at) else (e) { return e; };
|
|
return opened.write(data);
|
|
}
|
|
|
|
#! Describes the file at a path.
|
|
#! @param at The path of the file to describe.
|
|
#! @param follow Whether a link the path ends in is followed to what it leads
|
|
#! to, rather than described itself.
|
|
#! @returns What is known of the file, or why it could not be described.
|
|
pub def info(at: &path, follow: bool = true): result<file_info, io_error> {
|
|
var out: file_info;
|
|
val status := follow
|
|
? system::stat(at.view(), &mut out)
|
|
: system::lstat(at.view(), &mut out);
|
|
if status < 0 {
|
|
return system::error_of(status);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
#! Tells whether anything is at a path, following a link the path ends in.
|
|
#! @param at The path to look at.
|
|
#! @returns Whether the file it names could be described.
|
|
pub def exists(at: &path): bool {
|
|
return info(at) is value;
|
|
}
|
|
|
|
#! Removes the file, link, or empty directory at a path. A link is removed
|
|
#! itself, not what it leads to.
|
|
#! @param at The path of what to remove.
|
|
#! @returns Why it could not be removed, where it could not.
|
|
pub def remove(at: &path): result<void, io_error> {
|
|
val found := info(at, false) else (e) { return e; };
|
|
if found.kind == file_kind::directory {
|
|
return outcome(system::rmdir(at.view()));
|
|
}
|
|
return outcome(system::unlink(at.view()));
|
|
}
|
|
|
|
#! Moves a file or directory to another path, replacing a file already there.
|
|
#! @param from The path of what to move.
|
|
#! @param to Where to move it, on the same device.
|
|
#! @returns Why it could not be moved, where it could not.
|
|
pub def rename(from: &path, to: &path): result<void, io_error> {
|
|
return outcome(system::rename(from.view(), to.view()));
|
|
}
|
|
|
|
#! Sets who may do what with the file at a path.
|
|
#! @param at The path of the file.
|
|
#! @param mode The permissions to give it.
|
|
#! @returns Why they could not be set, where they could not.
|
|
pub def set_permissions(at: &path, mode: permissions): result<void, io_error> {
|
|
return outcome(system::chmod(at.view(), mode));
|
|
}
|