#! @file files.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what `system` and `std::io` above it both speak of files in: what #! kind of file something is, who may do what with it, how one is opened, where #! a seek is measured from, and what is known of one. They are the system's, so #! `system` names them without reaching up into the layer above it. module system; #! What kind of file a path names. pub enum file_kind: u8 { other, #! A device, a pipe, a socket, or anything else. regular, #! A file that holds bytes. directory, #! A directory, which holds other files. link, #! A link to another path, which is only seen where links are not followed. } #! Who may do what with a file: read it, write it, or run it, each for its #! owner, its group, and everyone else, as nine bits. #! #! These are the bits POSIX systems keep for every file, written in octal as #! `0o644`, and they are given and taken as those systems keep them. Those #! systems may also set three more, the set-user, set-group, and sticky bits, #! which are kept as they are. #! #! Windows keeps less: a file is read only or it is not, and whether it runs is #! decided by its name. There, permissions read as readable by everyone, #! writable by everyone unless the file is read only, and runnable by everyone #! for a directory or a program. Setting them looks only at whether the owner #! may write, which clears or sets the file's read-only attribute. So #! `is_read_only` and `with_read_only` mean the same on every system. pub type permissions = u32; pub val permissions::owner_read := 0o400; #! The owner may read it. pub val permissions::owner_write := 0o200; #! The owner may write it. pub val permissions::owner_execute := 0o100; #! The owner may run it, or search it where it is a directory. pub val permissions::group_read := 0o040; #! Its group may read it. pub val permissions::group_write := 0o020; #! Its group may write it. pub val permissions::group_execute := 0o010; #! Its group may run it, or search it. pub val permissions::others_read := 0o004; #! Everyone else may read it. pub val permissions::others_write := 0o002; #! Everyone else may write it. pub val permissions::others_execute := 0o001; #! Everyone else may run it, or search it. #! What a new file is given, less what the system's mask of the program takes #! away, as `0o644` from the usual mask of `0o022`. pub val permissions::default_file := 0o666; #! What a new directory is given, less what the system's mask takes away. pub val permissions::default_directory := 0o777; #! Whether no one may write the file. #! @param this The permissions to read. #! @returns Whether none of the three write bits is set. pub val permissions::is_read_only: bool { get => (this & 0o222) == 0; } #! Makes permissions that are read only, or that let the owner write. #! @param this The permissions to start from. #! @param read_only Whether no one may write the file. #! @returns The permissions without any write bit when read only, and otherwise #! with the owner's write bit added, leaving who else may write as it #! was. pub def permissions::with_read_only(&this, read_only: bool): permissions { if read_only { return this & 0o7555; } return this | 0o200; } #! How a file is opened, combined with `|`. A file opened without `write` or #! `append` is opened for reading. pub type open_flags = u32; pub val open_flags::read := 1; #! The file is read. pub val open_flags::write := 2; #! The file is written. pub val open_flags::create := 4; #! The file is made where there is none, as `write` needs. pub val open_flags::truncate := 8; #! What the file held is thrown away. pub val open_flags::append := 16; #! Every write goes at the end; this implies `write`. pub val open_flags::exclusive := 32; #! With `create`, opening fails where the file exists. #! Where an offset to seek to is measured from. pub type whence = i32; pub val whence::start := 0; #! The start of the file. pub val whence::current := 1; #! Where the next read or write happens. pub val whence::end := 2; #! The end of the file. #! What is known of a file. pub struct file_info { pub var kind: file_kind; #! A file, a directory, a link, or another kind. pub var size: u64; #! In bytes. A directory's size means what its system says. pub var permissions: permissions; #! Who may read, write, and run it. pub var modified: i64; #! When its bytes last changed, in nanoseconds since 1970. pub var accessed: i64; #! When it was last read, in nanoseconds since 1970. }