commit 2a8ef053ee6842eea0dd20668ca6b5ebebfeaa05
parent a5e5f910d57663a4d9bd8806b663d2b8e268ff48
Author: Lorenz (xha) <me@xha.li>
Date: Sun, 10 Dec 2023 06:24:53 +0100
OpenBSD: implement os::mkfifo and os::mkfile
Signed-off-by: Lorenz (xha) <me@xha.li>
Diffstat:
2 files changed, 46 insertions(+), 5 deletions(-)
diff --git a/os/+openbsd/fs.ha b/os/+openbsd/fs.ha
@@ -3,6 +3,7 @@
use errors;
use fs;
+use io;
use path;
use rt;
use types::c;
@@ -74,8 +75,26 @@ export fn access(path: str, mode: amode) (bool | fs::error) = {
};
};
-// TODO: OpenBSD
-// export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
-// export fn mkblk(
-// export fn mkchr(
-// export fn mkfile(
+// Makes a FIFO node. This function is only available on Unix-like systems.
+export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
+ match (rt::mknodat(rt::AT_FDCWD, path,
+ mode: rt::mode_t | rt::S_IFIFO, 0)) {
+ case let err: rt::errno =>
+ return errno_to_fs(err);
+ case void => void;
+ };
+};
+
+// Makes a regular file. This function is only available on Unix-like systems.
+// This function should only be used if you have a special reason; most of the
+// time you should use [[create]] instead.
+export fn mkfile(path: str, mode: fs::mode) (void | fs::error) = {
+ let file = match(rt::openat(rt::AT_FDCWD, path, rt::O_RDONLY | rt::O_CREAT,
+ mode: rt::mode_t)) {
+ case let f: int =>
+ yield f: io::file;
+ case let err: rt::errno =>
+ return errno_to_fs(err);
+ };
+ io::close(file)?;
+};
diff --git a/rt/+openbsd/syscalls.ha b/rt/+openbsd/syscalls.ha
@@ -1229,6 +1229,28 @@ export fn mkdirat(dirfd: int, path: path, mode: mode_t) (void | errno) = {
// mkfifoat
// mknodat
+
+@symbol("mknodat") fn libc_mknodat(
+ fd: int,
+ path: *const u8,
+ mode: mode_t,
+ dev: dev_t
+) int;
+
+// The OpenBSD implementation of mknodat *only* supports FIFO and
+// device special files.
+export fn mknodat(
+ dirfd: int,
+ path: path,
+ mode: mode_t,
+ dev: dev_t,
+) (void | errno) = {
+ let res = libc_mknodat(dirfd, cpath(path)?, mode, dev);
+ if (res == -1) {
+ return *__errno(): errno;
+ };
+};
+
// openat
@symbol("openat") fn libc_openat(