hare

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

dirfdfs.ha (9759B)


      1 // License: MPL-2.0
      2 // (c) 2021-2022 Drew DeVault <sir@cmpwn.com>
      3 // (c) 2021 Eyal Sawady <ecs@d2evs.net>
      4 // (c) 2022 Jon Eskin <eskinjp@gmail.com>
      5 use bytes;
      6 use errors;
      7 use encoding::utf8;
      8 use fs;
      9 use io;
     10 use path;
     11 use rt;
     12 use strings;
     13 use time;
     14 
     15 type os_filesystem = struct {
     16 	fs: fs::fs,
     17 	dirfd: int,
     18 	getdents_bufsz: size,
     19 };
     20 
     21 // Opens a file descriptor as an [[fs::fs]]. This file descriptor must be a
     22 // directory file. The file will be closed when the fs is closed.
     23 export fn dirfdopen(fd: io::file) *fs::fs = {
     24 	let ofs = alloc(os_filesystem { ... });
     25 	let fs = static_dirfdopen(fd, ofs);
     26 	fs.close = &fs_close;
     27 	return fs;
     28 };
     29 
     30 fn static_dirfdopen(fd: io::file, filesystem: *os_filesystem) *fs::fs = {
     31 	*filesystem = os_filesystem {
     32 		fs = fs::fs {
     33 			open = &fs_open,
     34 			openfile = &fs_open_file,
     35 			create = &fs_create,
     36 			createfile = &fs_create_file,
     37 			remove = &fs_remove,
     38 			rename = &fs_rename,
     39 			iter = &fs_iter,
     40 			stat = &fs_stat,
     41 			readlink = &fs_readlink,
     42 			mkdir = &fs_mkdir,
     43 			rmdir = &fs_rmdir,
     44 			chmod = &fs_chmod,
     45 			chown = &fs_chown,
     46 			resolve = &fs_resolve,
     47 			...
     48 		},
     49 		dirfd = fd,
     50 		getdents_bufsz = 32768, // 32 KiB
     51 	};
     52 	return &filesystem.fs;
     53 };
     54 
     55 // Sets the buffer size to use with the getdents(2) system call, for use with
     56 // [[fs::iter]]. A larger buffer requires a larger runtime allocation, but can
     57 // scan large directories faster. The default buffer size is 32 KiB.
     58 //
     59 // This function is not portable.
     60 export fn dirfdfs_set_getdents_bufsz(fs: *fs::fs, sz: size) void = {
     61 	assert(fs.open == &fs_open);
     62 	let fs = fs: *os_filesystem;
     63 	fs.getdents_bufsz = sz;
     64 };
     65 
     66 // Returns an [[io::file]] for this filesystem. This function is not portable.
     67 export fn dirfile(fs: *fs::fs) io::file = {
     68 	assert(fs.open == &fs_open);
     69 	let fs = fs: *os_filesystem;
     70 	return fs.dirfd;
     71 };
     72 
     73 fn errno_to_fs(err: rt::errno) fs::error = switch (err) {
     74 case rt::ENOENT =>
     75 	yield errors::noentry;
     76 case rt::EEXIST =>
     77 	yield errors::exists;
     78 case rt::EACCES =>
     79 	yield errors::noaccess;
     80 case rt::EBUSY =>
     81 	yield errors::busy;
     82 case rt::ENOTDIR =>
     83 	yield fs::wrongtype;
     84 case rt::EOPNOTSUPP, rt::ENOSYS =>
     85 	yield errors::unsupported;
     86 case rt::EXDEV =>
     87 	yield fs::cannotrename;
     88 case =>
     89 	yield errors::errno(err);
     90 };
     91 
     92 fn _fs_open(
     93 	fs: *fs::fs,
     94 	path: str,
     95 	flags: int,
     96 	mode: uint,
     97 ) (io::file | fs::error) = {
     98 	let fs = fs: *os_filesystem;
     99 
    100 	let fd = match (rt::openat(fs.dirfd, path, flags, mode)) {
    101 	case let err: rt::errno =>
    102 		return errno_to_fs(err);
    103 	case let fd: int =>
    104 		yield fd;
    105 	};
    106 
    107 	return io::fdopen(fd);
    108 };
    109 
    110 fn fsflags_to_bsd(flags: fs::flags) int = {
    111 	let out = rt::O_NOCTTY | rt::O_CLOEXEC;
    112 	if (flags & fs::flags::WRONLY > 0) {
    113 		out |= rt::O_WRONLY;
    114 	};
    115 	if (flags & fs::flags::RDWR > 0) {
    116 		out |= rt::O_RDWR;
    117 	};
    118 	if (flags & fs::flags::CREATE > 0) {
    119 		out |= rt::O_CREAT;
    120 	};
    121 	if (flags & fs::flags::EXCL > 0) {
    122 		out |= rt::O_EXCL;
    123 	};
    124 	if (flags & fs::flags::CTTY > 0) {
    125 		out &= ~rt::O_NOCTTY;
    126 	};
    127 	if (flags & fs::flags::TRUNC > 0) {
    128 		out |= rt::O_TRUNC;
    129 	};
    130 	if (flags & fs::flags::APPEND > 0) {
    131 		out |= rt::O_APPEND;
    132 	};
    133 	if (flags & fs::flags::NONBLOCK > 0) {
    134 		out |= rt::O_NONBLOCK;
    135 	};
    136 	if (flags & fs::flags::DSYNC > 0) {
    137 		out |= rt::O_DSYNC;
    138 	};
    139 	if (flags & fs::flags::SYNC > 0) {
    140 		out |= rt::O_SYNC;
    141 	};
    142 	if (flags & fs::flags::RSYNC > 0) {
    143 		out |= rt::O_SYNC;
    144 	};
    145 	if (flags & fs::flags::DIRECTORY > 0) {
    146 		out |= rt::O_DIRECTORY;
    147 	};
    148 	if (flags & fs::flags::NOFOLLOW > 0) {
    149 		out |= rt::O_NOFOLLOW;
    150 	};
    151 	if (flags & fs::flags::NOCLOEXEC > 0) {
    152 		out &= ~rt::O_CLOEXEC;
    153 	};
    154 	if (flags & fs::flags::PATH > 0) {
    155 		abort("fs::flags::PATH is not supported on FreeBSD");
    156 	};
    157 	if (flags & fs::flags::NOATIME > 0) {
    158 		abort("fs::flags::NOATIME is not supported on FreeBSD");
    159 	};
    160 	if (flags & fs::flags::TMPFILE > 0) {
    161 		abort("fs::flags::TMPFILE is not supported on FreeBSD");
    162 	};
    163 	return out;
    164 };
    165 
    166 fn fs_open_file(
    167 	fs: *fs::fs,
    168 	path: str,
    169 	flags: fs::flags...
    170 ) (io::file | fs::error) = {
    171 	let oflags = fs::flags::RDONLY;
    172 	for (let i = 0z; i < len(flags); i += 1z) {
    173 		oflags |= flags[i];
    174 	};
    175 	oflags ^= fs::flags::CTTY | fs::flags::NOCLOEXEC; // invert NOCTTY/CLOEXEC
    176 	return _fs_open(fs, path, fsflags_to_bsd(oflags), 0);
    177 };
    178 
    179 fn fs_open(
    180 	fs: *fs::fs,
    181 	path: str,
    182 	flags: fs::flags...
    183 ) (io::handle | fs::error) = fs_open_file(fs, path, flags...)?;
    184 
    185 fn fs_create_file(
    186 	fs: *fs::fs,
    187 	path: str,
    188 	mode: fs::mode,
    189 	flags: fs::flags...
    190 ) (io::file | fs::error) = {
    191 	let oflags: fs::flags = 0;
    192 	if (len(flags) == 0z) {
    193 		oflags |= fs::flags::WRONLY;
    194 	};
    195 	for (let i = 0z; i < len(flags); i += 1z) {
    196 		oflags |= flags[i];
    197 	};
    198 	oflags ^= fs::flags::CTTY | fs::flags::NOCLOEXEC; // invert NOCTTY/CLOEXEC
    199 	oflags |= fs::flags::CREATE;
    200 	return _fs_open(fs, path, fsflags_to_bsd(oflags), mode)?;
    201 };
    202 
    203 fn fs_create(
    204 	fs: *fs::fs,
    205 	path: str,
    206 	mode: fs::mode,
    207 	flags: fs::flags...
    208 ) (io::handle | fs::error) = {
    209 	return fs_create_file(fs, path, mode, flags...)?;
    210 };
    211 
    212 fn fs_remove(fs: *fs::fs, path: str) (void | fs::error) = {
    213 	let fs = fs: *os_filesystem;
    214 	match (rt::unlinkat(fs.dirfd, path, 0)) {
    215 	case let err: rt::errno =>
    216 		return errno_to_fs(err);
    217 	case void => void;
    218 	};
    219 };
    220 
    221 fn fs_rename(fs: *fs::fs, oldpath: str, newpath: str) (void | fs::error) = {
    222 	let fs = fs: *os_filesystem;
    223 	match (rt::renameat(fs.dirfd, oldpath, fs.dirfd, newpath)) {
    224 	case let err: rt::errno =>
    225 		return errno_to_fs(err);
    226 	case void => void;
    227 	};
    228 };
    229 
    230 fn fs_stat(fs: *fs::fs, path: str) (fs::filestat | fs::error) = {
    231 	let fs = fs: *os_filesystem;
    232 	let st = rt::st { ... };
    233 	match (rt::fstatat(fs.dirfd, path, &st, rt::AT_SYMLINK_NOFOLLOW)) {
    234 	case let err: rt::errno =>
    235 		return errno_to_fs(err);
    236 	case void => void;
    237 	};
    238 	return fs::filestat {
    239 		mask = fs::stat_mask::UID
    240 			| fs::stat_mask::GID
    241 			| fs::stat_mask::SIZE
    242 			| fs::stat_mask::INODE
    243 			| fs::stat_mask::ATIME
    244 			| fs::stat_mask::MTIME
    245 			| fs::stat_mask::CTIME,
    246 		mode = st.mode: fs::mode,
    247 		uid = st.uid,
    248 		gid = st.gid,
    249 		sz = st.sz: size,
    250 		inode = st.ino,
    251 		atime = time::instant {
    252 			sec = st.atime.tv_sec,
    253 			nsec = st.atime.tv_nsec,
    254 		},
    255 		mtime = time::instant {
    256 			sec = st.mtime.tv_sec,
    257 			nsec = st.mtime.tv_nsec,
    258 		},
    259 		ctime = time::instant {
    260 			sec = st.ctime.tv_sec,
    261 			nsec = st.ctime.tv_nsec,
    262 		},
    263 	};
    264 };
    265 
    266 fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = {
    267 	let fs = fs: *os_filesystem;
    268 	static let buf: [rt::PATH_MAX]u8 = [0...];
    269 	let z = match (rt::readlinkat(fs.dirfd, path, buf[..])) {
    270 	case let err: rt::errno =>
    271 		return errno_to_fs(err);
    272 	case let z: size =>
    273 		yield z;
    274 	};
    275 	return strings::fromutf8(buf[..z]);
    276 };
    277 
    278 fn fs_rmdir(fs: *fs::fs, path: str) (void | fs::error) = {
    279 	let fs = fs: *os_filesystem;
    280 	match (rt::unlinkat(fs.dirfd, path, rt::AT_REMOVEDIR)) {
    281 	case let err: rt::errno =>
    282 		return errno_to_fs(err);
    283 	case void => void;
    284 	};
    285 };
    286 
    287 fn fs_mkdir(fs: *fs::fs, path: str, mode: fs::mode) (void | fs::error) = {
    288 	let fs = fs: *os_filesystem;
    289 	match (rt::mkdirat(fs.dirfd, path, mode: uint)) {
    290 	case let err: rt::errno =>
    291 		switch (err) {
    292 		case rt::EISDIR =>
    293 			return errors::exists;
    294 		case =>
    295 			return errno_to_fs(err);
    296 		};
    297 	case void => void;
    298 	};
    299 };
    300 
    301 fn fs_chmod(fs: *fs::fs, path: str, mode: fs::mode) (void | fs::error) = {
    302 	let fs = fs: *os_filesystem;
    303 	match (rt::fchmodat(fs.dirfd, path, mode: uint, 0)) {
    304 	case let err: rt::errno =>
    305 		return errno_to_fs(err);
    306 	case void => void;
    307 	};
    308 };
    309 
    310 fn fs_chown(fs: *fs::fs, path: str, uid: uint, gid: uint) (void | fs::error) = {
    311 	let fs = fs: *os_filesystem;
    312 	match (rt::fchownat(fs.dirfd, path, uid, gid, 0)) {
    313 	case let err: rt::errno =>
    314 		return errno_to_fs(err);
    315 	case void => void;
    316 	};
    317 };
    318 
    319 fn fs_resolve(fs: *fs::fs, path: str) str = {
    320 	if (path::abs(path)) {
    321 		return path;
    322 	};
    323 	// XXX: This approach might not be right if this fs is based on a subdir
    324 	static let buf = path::buffer { ... };
    325 	path::set(&buf, getcwd(), path)!;
    326 	return path::string(&buf);
    327 };
    328 
    329 fn fs_close(fs: *fs::fs) void = {
    330 	let fs = fs: *os_filesystem;
    331 	rt::close(fs.dirfd)!;
    332 };
    333 
    334 // Based on musl's readdir
    335 type os_iterator = struct {
    336 	iter: fs::iterator,
    337 	fd: int,
    338 	buf_pos: size,
    339 	buf_end: size,
    340 	buf: []u8,
    341 };
    342 
    343 fn fs_iter(fs: *fs::fs, path: str) (*fs::iterator | fs::error) = {
    344 	let fs = fs: *os_filesystem;
    345 	let flags = rt::O_RDONLY | rt::O_CLOEXEC | rt::O_DIRECTORY;
    346 	let fd: int = match (rt::openat(fs.dirfd, path, flags, 0)) {
    347 	case let err: rt::errno =>
    348 		return errno_to_fs(err);
    349 	case let fd: int =>
    350 		yield fd;
    351 	};
    352 
    353 	let buf = match (rt::malloc(fs.getdents_bufsz)) {
    354 	case let v: *void =>
    355 		yield v: *[*]u8;
    356 	case null =>
    357 		return errors::nomem;
    358 	};
    359 	let iter = alloc(os_iterator {
    360 		iter = fs::iterator {
    361 			next = &iter_next,
    362 			finish = &iter_finish,
    363 		},
    364 		fd = fd,
    365 		buf = buf[..fs.getdents_bufsz],
    366 		...
    367 	});
    368 	return &iter.iter;
    369 };
    370 
    371 fn iter_next(iter: *fs::iterator) (fs::dirent | void) = {
    372 	let iter = iter: *os_iterator;
    373 	if (iter.buf_pos >= iter.buf_end) {
    374 		let n = rt::getdents(iter.fd,
    375 			iter.buf: *[*]u8, len(iter.buf)) as size;
    376 		if (n == 0) {
    377 			return;
    378 		};
    379 		iter.buf_end = n;
    380 		iter.buf_pos = 0;
    381 	};
    382 	let de = &iter.buf[iter.buf_pos]: *rt::freebsd11_dirent;
    383 	iter.buf_pos += de.d_reclen;
    384 	let name = strings::fromc(&de.d_name: *const char);
    385 
    386 	let ftype: fs::mode = switch (de.d_type) {
    387 	case rt::DT_UNKNOWN =>
    388 		yield fs::mode::UNKNOWN;
    389 	case rt::DT_FIFO =>
    390 		yield fs::mode::FIFO;
    391 	case rt::DT_CHR =>
    392 		yield fs::mode::CHR;
    393 	case rt::DT_DIR =>
    394 		yield fs::mode::DIR;
    395 	case rt::DT_BLK =>
    396 		yield fs::mode::BLK;
    397 	case rt::DT_REG =>
    398 		yield fs::mode::REG;
    399 	case rt::DT_LNK =>
    400 		yield fs::mode::LINK;
    401 	case rt::DT_SOCK =>
    402 		yield fs::mode::SOCK;
    403 	case =>
    404 		yield fs::mode::UNKNOWN;
    405 	};
    406 	return fs::dirent {
    407 		name = name,
    408 		ftype = ftype,
    409 	};
    410 };
    411 
    412 fn iter_finish(iter: *fs::iterator) void = {
    413 	let iter = iter: *os_iterator;
    414 	rt::close(iter.fd)!;
    415 	free(iter.buf);
    416 	free(iter);
    417 };