hare

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

fs.ha (2244B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use fs;
      6 use path;
      7 use rt;
      8 use types::c;
      9 
     10 @init fn init_cwd() void = {
     11 	static let cwd_fs = os_filesystem { ... };
     12 	cwd = static_dirfdopen(rt::AT_FDCWD, &cwd_fs);
     13 };
     14 
     15 // Returns the current working directory. The return value is statically
     16 // allocated and must be duplicated (see [[strings::dup]]) before calling getcwd
     17 // again.
     18 export fn getcwd() str = c::tostr(rt::getcwd() as *const u8: *const c::char)!;
     19 
     20 // Change the current working directory.
     21 export fn chdir(target: (*fs::fs | str)) (void | fs::error) = {
     22 	const path: str = match (target) {
     23 	case let fs: *fs::fs =>
     24 		assert(fs.open == &fs_open);
     25 		let fs = fs: *os_filesystem;
     26 		match (rt::fchdir(fs.dirfd)) {
     27 		case let err: rt::errno =>
     28 			return errno_to_fs(err);
     29 		case void =>
     30 			return;
     31 		};
     32 	case let s: str =>
     33 		yield s;
     34 	};
     35 	match (rt::chdir(path)) {
     36 	case let err: rt::errno =>
     37 		return errno_to_fs(err);
     38 	case void => void;
     39 	};
     40 };
     41 
     42 // Changes the root directory of the process. Generally requires the caller to
     43 // have root or otherwise elevated permissions.
     44 //
     45 // This function is not appropriate for sandboxing.
     46 export fn chroot(target: str) (void | fs::error) = {
     47 	match (rt::chroot(target)) {
     48 	case let err: rt::errno =>
     49 		return errno_to_fs(err);
     50 	case void => void;
     51 	};
     52 };
     53 
     54 // Access modes for [[access]].
     55 export type amode = enum int {
     56 	F_OK = rt::F_OK,
     57 	R_OK = rt::R_OK,
     58 	W_OK = rt::W_OK,
     59 	X_OK = rt::X_OK,
     60 };
     61 
     62 // Returns true if the given mode of access is permissible. The use of this
     63 // function is discouraged as it can allow for a race condition to occur betwen
     64 // testing for the desired access mode and actually using the file should the
     65 // permissions of the file change between these operations. It is recommended
     66 // instead to attempt to use the file directly and to handle any errors that
     67 // should occur at that time.
     68 export fn access(path: str, mode: amode) (bool | fs::error) = {
     69 	match (rt::access(path, mode)) {
     70 	case let b: bool =>
     71 		return b;
     72 	case let err: rt::errno =>
     73 		return errno_to_fs(err);
     74 	};
     75 };
     76 
     77 // TODO: FreeBSD
     78 // export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
     79 // export fn mkblk(
     80 // export fn mkchr(
     81 // export fn mkfile(