| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use errors; |
| 5 |
use path; |
| 6 |
use strings; |
| 7 |
|
| 8 |
// Converts a mode into a Unix-like mode string (e.g. "-rw-r--r--"). The string |
| 9 |
// is statically allocated, use [[strings::dup]] to duplicate it or it will be |
| 10 |
// overwritten on subsequent calls. |
| 11 |
export fn mode_str(m: mode) const str = { |
| 12 |
static let buf: [10]u8 = [0...]; |
| 13 |
buf = [ |
| 14 |
(if (m & mode::DIR == mode::DIR) 'd' |
| 15 |
else if (m & mode::FIFO == mode::FIFO) 'p' |
| 16 |
else if (m & mode::SOCK == mode::SOCK) 's' |
| 17 |
else if (m & mode::BLK == mode::BLK) 'b' |
| 18 |
else if (m & mode::LINK == mode::LINK) 'l' |
| 19 |
else if (m & mode::CHR == mode::CHR) 'c' |
| 20 |
else '-'): u8, |
| 21 |
(if (m & mode::USER_R == mode::USER_R) 'r' else '-'): u8, |
| 22 |
(if (m & mode::USER_W == mode::USER_W) 'w' else '-'): u8, |
| 23 |
(if (m & mode::SETUID == mode::SETUID) 's' |
| 24 |
else if (m & mode::USER_X == mode::USER_X) 'x' |
| 25 |
else '-'): u8, |
| 26 |
(if (m & mode::GROUP_R == mode::GROUP_R) 'r' else '-'): u8, |
| 27 |
(if (m & mode::GROUP_W == mode::GROUP_W) 'w' else '-'): u8, |
| 28 |
(if (m & mode::SETGID == mode::SETGID) 's' |
| 29 |
else if (m & mode::GROUP_X == mode::GROUP_X) 'x' |
| 30 |
else '-'): u8, |
| 31 |
(if (m & mode::OTHER_R == mode::OTHER_R) 'r' else '-'): u8, |
| 32 |
(if (m & mode::OTHER_W == mode::OTHER_W) 'w' else '-'): u8, |
| 33 |
(if (m & mode::STICKY == mode::STICKY) 't' |
| 34 |
else if (m & mode::OTHER_X == mode::OTHER_X) 'x' |
| 35 |
else '-'): u8, |
| 36 |
]; |
| 37 |
return strings::fromutf8(buf)!; |
| 38 |
}; |
| 39 |
|
| 40 |
@test fn mode_str() void = { |
| 41 |
assert(mode_str(0o777: mode) == "-rwxrwxrwx"); |
| 42 |
assert(mode_str(mode::DIR | 0o755: mode) == "drwxr-xr-x"); |
| 43 |
assert(mode_str(0o755: mode | mode::SETUID) == "-rwsr-xr-x"); |
| 44 |
assert(mode_str(0o644: mode) == "-rw-r--r--"); |
| 45 |
assert(mode_str(0: mode) == "----------"); |
| 46 |
}; |
| 47 |
|
| 48 |
// Returns the permission bits of a file mode. |
| 49 |
export fn mode_perm(m: mode) mode = (m: uint & 0o777u): mode; |
| 50 |
|
| 51 |
// Returns the type bits of a file mode. |
| 52 |
export fn mode_type(m: mode) mode = (m: uint & ~0o777u): mode; |
| 53 |
|
| 54 |
// bit mask for the file type bit field |
| 55 |
def IFMT: mode = 0o0170000u: mode; |
| 56 |
|
| 57 |
// Returns true if this item is a regular file. |
| 58 |
export fn isfile(mode: mode) bool = mode & IFMT == mode::REG; |
| 59 |
|
| 60 |
// Returns true if this item is a FIFO (named pipe). |
| 61 |
export fn isfifo(mode: mode) bool = mode & IFMT == mode::FIFO; |
| 62 |
|
| 63 |
// Returns true if this item is a directory. |
| 64 |
export fn isdir(mode: mode) bool = mode & IFMT == mode::DIR; |
| 65 |
|
| 66 |
// Returns true if this item is a character device. |
| 67 |
export fn ischdev(mode: mode) bool = mode & IFMT == mode::CHR; |
| 68 |
|
| 69 |
// Returns true if this item is a block device. |
| 70 |
export fn isblockdev(mode: mode) bool = mode & IFMT == mode::BLK; |
| 71 |
|
| 72 |
// Returns true if this item is a symbolic link. |
| 73 |
export fn islink(mode: mode) bool = mode & IFMT == mode::LINK; |
| 74 |
|
| 75 |
// Returns true if this item is a Unix socket. |
| 76 |
export fn issocket(mode: mode) bool = mode & IFMT == mode::SOCK; |
| 77 |
|
| 78 |
@test fn modes() void = { |
| 79 |
const foo = mode::LINK | 0o755: mode; |
| 80 |
assert(islink(foo)); |
| 81 |
assert(!isfile(foo)); |
| 82 |
}; |
| 83 |
|
| 84 |
// Reads all entries from a directory. The caller must free the return value |
| 85 |
// with [[dirents_free]]. |
| 86 |
export fn readdir(fs: *fs, path: str) ([]dirent | error | nomem) = { |
| 87 |
let i = iter(fs, path)?; |
| 88 |
defer finish(i); |
| 89 |
let ents: []dirent = []; |
| 90 |
let ok = false; |
| 91 |
defer if (!ok) dirents_free(ents); |
| 92 |
|
| 93 |
for (let d => next(i)?) { |
| 94 |
let dup = dirent_dup(&d)?; |
| 95 |
match (append(ents, dup)) { |
| 96 |
case void => void; |
| 97 |
case nomem => |
| 98 |
dirent_finish(&dup); |
| 99 |
return nomem; |
| 100 |
}; |
| 101 |
}; |
| 102 |
|
| 103 |
ok = true; |
| 104 |
return ents; |
| 105 |
}; |
| 106 |
|
| 107 |
// Frees a slice of [[dirent]]s. |
| 108 |
export fn dirents_free(dirents: []dirent) void = { |
| 109 |
for (let d &.. dirents) { |
| 110 |
dirent_finish(d); |
| 111 |
}; |
| 112 |
free(dirents); |
| 113 |
}; |
| 114 |
|
| 115 |
// Removes a directory, and anything in it. |
| 116 |
export fn rmdirall(fs: *fs, path: str) (void | error) = { |
| 117 |
match (path::init(path)) { |
| 118 |
case let buf: path::buffer => |
| 119 |
return rmdirall_path(fs, &buf); |
| 120 |
case let err: path::error => |
| 121 |
assert(err is path::too_long); |
| 122 |
return errors::noentry; |
| 123 |
}; |
| 124 |
}; |
| 125 |
|
| 126 |
fn rmdirall_path(fs: *fs, buf: *path::buffer) (void | error) = { |
| 127 |
let it = iter(fs, path::string(buf))?; |
| 128 |
defer finish(it); |
| 129 |
for (let ent => next(it)?) { |
| 130 |
path::push(buf, ent.name)!; |
| 131 |
|
| 132 |
switch (ent.ftype & mode::DIR) { |
| 133 |
case mode::DIR => |
| 134 |
rmdirall_path(fs, buf)?; |
| 135 |
case => |
| 136 |
remove(fs, path::string(buf))?; |
| 137 |
}; |
| 138 |
path::pop(buf); |
| 139 |
}; |
| 140 |
return rmdir(fs, path::string(buf)); |
| 141 |
}; |
| 142 |
|
| 143 |
// Canonicalizes a path in this filesystem by resolving all symlinks and |
| 144 |
// collapsing any "." or ".." path components. The return value is statically |
| 145 |
// allocated and will be overwritten on subsequent calls. |
| 146 |
export fn realpath(fs: *fs, path: str) (str | error) = { |
| 147 |
static let res = path::buffer { ... }; |
| 148 |
path::set(&res)!; |
| 149 |
static let pathbuf = path::buffer { ... }; |
| 150 |
path::set(&pathbuf, resolve(fs, path))!; |
| 151 |
const iter = path::iter(&pathbuf); |
| 152 |
|
| 153 |
for (let item => path::nextiter(&iter)) { |
| 154 |
const item = path::push(&res, item)!; |
| 155 |
|
| 156 |
const link = match (readlink(fs, item)) { |
| 157 |
case let link: str => |
| 158 |
yield link; |
| 159 |
case wrongtype => |
| 160 |
continue; |
| 161 |
case let err: error => |
| 162 |
return err; |
| 163 |
}; |
| 164 |
|
| 165 |
if (!path::abs(link)) { |
| 166 |
path::push(&res, "..", link)!; |
| 167 |
} else { |
| 168 |
path::set(&res, link)!; |
| 169 |
}; |
| 170 |
}; |
| 171 |
|
| 172 |
return path::string(&res); |
| 173 |
}; |