commit eb390c6626e3a35c749523e3d449b27a225b95fd
parent 8210f69cec1da3d1ec76614b3def658ebf0fe74b
Author: Sebastian <sebastian@sebsite.pw>
Date: Sat, 9 Dec 2023 21:17:37 -0500
os+linux: add mkfile
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
5 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/cmd/hare/build/queue.ha b/cmd/hare/build/queue.ha
@@ -139,6 +139,7 @@ fn run_task(ctx: *context, jobs: *[]job, t: *task) (bool | error) = {
path::set(&buf, out)?;
let tmp = path::push_ext(&buf, "tmp")?;
+ // TODO: use os::mkfile once that's supported on freebsd and openbsd
io::close(os::create(tmp, 0o644)?)?;
let args = get_args(ctx, tmp, flags, t);
diff --git a/os/+freebsd/fs.ha b/os/+freebsd/fs.ha
@@ -78,3 +78,4 @@ export fn access(path: str, mode: amode) (bool | fs::error) = {
// export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
// export fn mkblk(
// export fn mkchr(
+// export fn mkfile(
diff --git a/os/+linux/fs.ha b/os/+linux/fs.ha
@@ -94,6 +94,18 @@ export fn mkchr(
};
};
+// 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) = {
+ match (rt::mknodat(rt::AT_FDCWD, path,
+ mode: rt::mode_t | rt::S_IFREG, 0)) {
+ case let err: rt::errno =>
+ return errors::errno(err);
+ case void => void;
+ };
+};
+
// Access modes for [[access]].
export type amode = enum int {
F_OK = rt::F_OK,
diff --git a/os/+openbsd/fs.ha b/os/+openbsd/fs.ha
@@ -73,3 +73,9 @@ export fn access(path: str, mode: amode) (bool | fs::error) = {
return errno_to_fs(err);
};
};
+
+// TODO: OpenBSD
+// export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
+// export fn mkblk(
+// export fn mkchr(
+// export fn mkfile(
diff --git a/os/os.ha b/os/os.ha
@@ -97,6 +97,8 @@ export fn open(path: str, flags: fs::flag...) (io::file | fs::error) =
//
// Only the permission bits of the mode are used. If other bits are set, they
// are discarded.
+//
+// To create a file without opening it, see [[mkfile]].
export fn create(
path: str,
mode: fs::mode,