hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

fs.ha (4463B)


      1 // License: MPL-2.0
      2 // (c) 2021 Alexey Yerin <yyp@disroot.org>
      3 // (c) 2021-2022 Drew DeVault <sir@cmpwn.com>
      4 // (c) 2021 Eyal Sawady <ecs@d2evs.net>
      5 // (c) 2022 Jon Eskin <eskinjp@gmail.com>
      6 use fs;
      7 use io;
      8 use path;
      9 
     10 // Provides an implementation of [[fs::fs]] for the current working directory.
     11 export let cwd: *fs::fs = null: *fs::fs;
     12 
     13 // Removes a file.
     14 export fn remove(path: str) (void | fs::error) = fs::remove(cwd, path);
     15 
     16 // Renames a file. This generally only works if the source and destination path
     17 // are both on the same filesystem. See [[move]] for an implementation which
     18 // falls back on a "copy & remove" procedure in this situation.
     19 export fn rename(oldpath: str, newpath: str) (void | fs::error) =
     20 	fs::rename(cwd, oldpath, newpath);
     21 
     22 // Moves a file. This will use [[rename]] if possible, and will fall back to
     23 // copy and remove if necessary.
     24 export fn move(oldpath: str, newpath: str) (void | fs::error) =
     25 	fs::move(cwd, oldpath, newpath);
     26 
     27 // Creates an [[fs::iterator]] for a given directory to read its contents. The
     28 // user should call [[fs::next]] to enumerate entries, and [[fs::finish]] when
     29 // done using the object.
     30 export fn iter(path: str) (*fs::iterator | fs::error) = fs::iter(cwd, path);
     31 
     32 // Frees state associated with a directory iterator.
     33 export fn finish(iter: *fs::iterator) void = fs::finish(iter);
     34 
     35 // Reads all entries from a directory. The caller must free the return value
     36 // with [[fs::dirents_free]].
     37 export fn readdir(path: str) ([]fs::dirent | fs::error) = fs::readdir(cwd, path);
     38 
     39 // Returns file information for a given path. If the target is a symlink,
     40 // information is returned about the link, not its target.
     41 export fn stat(path: str) (fs::filestat | fs::error) = fs::stat(cwd, path);
     42 
     43 // Creates a directory.
     44 export fn mkdir(path: str, mode: fs::mode) (void | fs::error) = fs::mkdir(cwd, path, mode);
     45 
     46 // Creates a directory, and all non-extant directories in its path.
     47 export fn mkdirs(path: str, mode: fs::mode) (void | fs::error) = fs::mkdirs(cwd, path, mode);
     48 
     49 // Removes a directory. The target directory must be empty; see [[rmdirall]] to
     50 // remove its contents as well.
     51 export fn rmdir(path: str) (void | fs::error) = fs::rmdir(cwd, path);
     52 
     53 // Removes a directory, and anything in it.
     54 export fn rmdirall(path: str) (void | fs::error) = fs::rmdirall(cwd, path);
     55 
     56 // Changes mode flags on a file or directory. Type bits are discared.
     57 export fn chmod(path: str, mode: fs::mode) (void | fs::error) = fs::chmod(cwd, path, mode);
     58 
     59 // Changes ownership of a file.
     60 export fn chown(path: str, uid: uint, gid: uint) (void | fs::error) = fs::chown(cwd, path, uid, gid);
     61 
     62 // Resolves a path to its absolute, normalized value. Relative paths will be
     63 // rooted (if supported by the host filesystem), and "." and ".." components
     64 // will be reduced. This function does not follow symlinks; see [[realpath]] if
     65 // you need this behavior. The return value is statically allocated; use
     66 // [[strings::dup]] to extend its lifetime.
     67 export fn resolve(path: str) str = fs::resolve(cwd, path);
     68 
     69 // Returns the path referred to by a symbolic link. The return value is
     70 // statically allocated and will be overwritten on subsequent calls.
     71 export fn readlink(path: str) (str | fs::error) = fs::readlink(cwd, path);
     72 
     73 // Opens a file.
     74 //
     75 // If no flags are provided, [[fs::flags::RDONLY]] is used when opening the
     76 // file.
     77 export fn open(path: str, flags: fs::flags...) (io::file | fs::error) =
     78 	fs::open_file(cwd, path, flags...);
     79 
     80 // Creates a new file and opens it for writing.
     81 //
     82 // If no flags are provided, [[fs::flags::WRONLY]] is used when opening the
     83 // file.
     84 //
     85 // Only the permission bits of the mode are used. If other bits are set, they
     86 // are discarded.
     87 export fn create(
     88 	path: str,
     89 	mode: fs::mode,
     90 	flags: fs::flags...
     91 ) (io::file | fs::error) = fs::create_file(cwd, path, mode, flags...);
     92 
     93 // Canonicalizes a path in this filesystem by resolving all symlinks and
     94 // collapsing any "." or ".." path components.
     95 //
     96 // This function is a thin shim over [[fs::realpath]], and the return value is
     97 // statically allocated by [[fs::realpath]]. Thus, calls to this function or to
     98 // [[fs::realpath]] will overwrite the return value of either function.
     99 export fn realpath(path: str) (str | fs::error) = fs::realpath(cwd, path);
    100 
    101 // Opens a directory as a filesystem.
    102 export fn diropen(path: str) (*fs::fs | fs::error) = {
    103 	const file = open(path, fs::flags::DIRECTORY | fs::flags::RDONLY)?;
    104 	return dirfdopen(file);
    105 };