#! @file directories.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `directory`, a directory being read one name at a time, #! `list_directory`, which reads every name at once, what makes directories, and #! the working directory: finding it, changing it, and making a path absolute #! against it. import system; module std::io; #! A directory being read, one name at a time, which is closed where its scope #! ends. #! #! `for entry in listing` walks the names it holds, in no particular order, #! leaving out `.` and `..`. Each entry's name is only good until the next is #! read, so a name to keep is copied, or joined onto the directory's path. #! Reading that fails partway stops the walk early, and `error` says why. pub struct directory : owning, moveable { var _stream: system::dir_stream; var _error: maybe; } #! A name a directory holds, as `directory::next` gives it. pub struct directory::entry { pub var name: strview; #! Only good until the next entry is read, or the directory closes. pub var kind: file_kind; #! What the name is itself, so a link is a `link` wherever it leads. } #! Why reading the directory stopped before its end, where it did. #! @param this The directory to ask. #! @returns The failure, or `nothing` where none has happened. pub val directory::error: maybe { get => this._error; } #! Opens a directory to read the names it holds. #! @param at The path of the directory. #! @returns The open directory, or why it could not be opened. pub def directory::open(at: &path): result { var made: directory; val status := system::opendir(at.view(), &mut made._stream); if status < 0 { return system::error_of(status); } return move(made); } #! Reads the next name the directory holds. #! @param this The directory to read. #! @returns The next entry, or `nothing` once there are no more, or reading #! failed. pub def directory::next(&mut this): maybe { var found: system::dir_entry; val status := this._stream.next(&mut found); if status < 0 { this._error = system::error_of(status); return nothing{}; } if status == 0 { return nothing{}; } return directory::entry { name = found.name, kind = found.kind }; } #! Creates a directory by taking another's place in reading, which leaves the #! other closed. #! @param other The directory to move from. #! @returns The directory, which closes where its scope ends. pub def directory::move_of(other: &mut directory): directory { return directory { _stream = move(other._stream), _error = other._error }; } #! Stops reading the directory. Closing one that is not open does nothing. #! @param this The directory to close. pub def directory::close(&mut this): void { this._stream.close(); } #! Gives the path of everything a directory holds. #! @param at The path of the directory. #! @param alloc The optional allocator for the paths and the list of them. #! @returns The path of each name the directory holds, joined onto `at`, in no #! particular order, or why the directory could not be read. A name no #! path can hold, as one with `\` on Linux, fails with #! `io_error::invalid`; a `directory` gives every name as it is. pub def list_directory( at: &path, alloc: maybe<&mut allocator> = nothing{} ): result, io_error> { var listing := directory::open(at) else (e) { return e; }; var found := vector::::new(alloc); while listing.next() is some(each) { var parsed := path::parse(each.name, alloc); if parsed is not value(&name) { return io_error::invalid; } found.append(at.joined(name, alloc)); } if listing.error is some(why) { return why; } return move(found); } #! Makes a directory. #! @param at The path of the directory, whose parent must already exist. #! @param mode The permissions to give it, less what the system's mask takes #! away. #! @returns Why it could not be made, where it could not. pub def create_directory( at: &path, mode: permissions = permissions::default_directory ): result { return outcome(system::mkdir(at.view(), mode)); } #! Makes a directory, and every directory above it that is missing. #! @param at The path of the directory. #! @param mode The permissions to give each directory made, less the mask. #! @returns Why they could not be made, where they could not. A directory that #! already exists is not a failure, but anything else at the path is. pub def create_directories( at: &path, mode: permissions = permissions::default_directory ): result { val made := system::mkdir(at.view(), mode); if made == 0 { return; } val why := system::error_of(made); if why != io_error::not_found { return directory_at(at, why); } # A directory above it is missing too, so those are made first. var above := at.parent(); if above is not some(&parent) { return why; } create_directories(parent, mode) else (e) { return e; }; val again := system::mkdir(at.view(), mode); if again == 0 { return; } return directory_at(at, system::error_of(again)); } #! Gives the working directory, which relative paths are taken from. #! @param alloc The optional allocator for the path. #! @returns The absolute path of the working directory, or why it could not be #! found. pub def working_directory(alloc: maybe<&mut allocator> = nothing{}): result { var text: u8[system::path_limit]; val length := system::getcwd((&mut text) as *mut u8, system::path_limit); if length < 0 { return system::error_of(length); } var parsed := path::parse(strview::over((&text) as *u8, length as u64), alloc); if parsed is not value(&mut found) { return io_error::invalid; } return move(found); } #! Sets the working directory, which relative paths are taken from. #! @param at The path of the directory. #! @returns Why it could not be set, where it could not. pub def set_working_directory(at: &path): result { return outcome(system::chdir(at.view())); } #! Makes a path absolute, resolving it against the working directory. #! @param at The path to make absolute. #! @returns The path resolved as `path::resolve` does, from its text, with the #! working directory as the base where it is relative, or why the #! working directory could not be found. pub def absolute(at: &path): result { if at.is_absolute { return at.resolve(at); } val here := working_directory(at._text.alloc) else (e) { return e; }; return at.resolve(here); } # Whether a failure to make a directory leaves one there anyway, as when it was # already there. Succeeds where a directory is at the path, and gives the failure # otherwise. def directory_at(at: &path, why: io_error): result { if why != io_error::already_exists { return why; } val found := info(at) else { return why; }; if found.kind != file_kind::directory { return why; } return; }