fs.ha (4771B)
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 Ember 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 // Creates a new (hard) link at 'new' for the file at 'old'. 74 export fn link(old: str, new: str) (void | fs::error) = fs::link(cwd, old, new); 75 76 // Creates a new symbolic link at 'path' which points to 'target'. 77 export fn symlink(target: str, path: str) (void | fs::error) = 78 fs::symlink(cwd, target, path); 79 80 // Opens a file. 81 // 82 // If no flags are provided, [[fs::flags::RDONLY]] is used when opening the 83 // file. 84 export fn open(path: str, flags: fs::flags...) (io::file | fs::error) = 85 fs::open_file(cwd, path, flags...); 86 87 // Creates a new file and opens it for writing. 88 // 89 // If no flags are provided, [[fs::flags::WRONLY]] is used when opening the 90 // file. 91 // 92 // Only the permission bits of the mode are used. If other bits are set, they 93 // are discarded. 94 export fn create( 95 path: str, 96 mode: fs::mode, 97 flags: fs::flags... 98 ) (io::file | fs::error) = fs::create_file(cwd, path, mode, flags...); 99 100 // Canonicalizes a path in this filesystem by resolving all symlinks and 101 // collapsing any "." or ".." path components. 102 // 103 // This function is a thin shim over [[fs::realpath]], and the return value is 104 // statically allocated by [[fs::realpath]]. Thus, calls to this function or to 105 // [[fs::realpath]] will overwrite the return value of either function. 106 export fn realpath(path: str) (str | fs::error) = fs::realpath(cwd, path); 107 108 // Opens a directory as a filesystem. 109 export fn diropen(path: str) (*fs::fs | fs::error) = { 110 const file = open(path, fs::flags::DIRECTORY | fs::flags::RDONLY)?; 111 return dirfdopen(file); 112 };