Debian Codegraph @

os/+freebsd/fs.ha

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