commit 578a4e3d62125cc987329080ca8a99a4633b884e
parent d3ac290ee408868d18b6eea429911d62305c8c83
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 4 Mar 2022 12:24:01 +0100
os: add os::access
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/os/+freebsd/fs.ha b/os/+freebsd/fs.ha
@@ -51,6 +51,21 @@ export fn chroot(target: str) (void | fs::error) = {
};
};
+// Returns true if the given mode of access is permissible. The use of this
+// function is discouraged as it can allow for a race condition to occur betwen
+// testing for the desired access mode and actually using the file should the
+// permissions of the file change between these operations. It is recommended
+// instead to attempt to use the file directly and to handle any errors that
+// should occur at that time.
+export fn access(path: str, mode: amode) (bool | fs::error) = {
+ match (rt::access(path, mode)) {
+ case let b: bool =>
+ return b;
+ case let err: rt::errno =>
+ return errno_to_fs(err);
+ };
+};
+
// TODO: FreeBSD
// export fn mkfifo(path: str, mode: fs::mode) (void | fs::error) = {
// export fn mkblk(
diff --git a/os/+linux/fs.ha b/os/+linux/fs.ha
@@ -91,3 +91,26 @@ export fn mkchr(
case void => void;
};
};
+
+// Access modes for [[access]].
+export type amode = enum int {
+ F_OK = rt::F_OK,
+ R_OK = rt::R_OK,
+ W_OK = rt::W_OK,
+ X_OK = rt::X_OK,
+};
+
+// Returns true if the given mode of access is permissible. The use of this
+// function is discouraged as it can allow for a race condition to occur betwen
+// testing for the desired access mode and actually using the file should the
+// permissions of the file change between these operations. It is recommended
+// instead to attempt to use the file directly and to handle any errors that
+// should occur at that time.
+export fn access(path: str, mode: amode) (bool | fs::error) = {
+ match (rt::access(path, mode)) {
+ case let b: bool =>
+ return b;
+ case let err: rt::errno =>
+ return errno_to_fs(err);
+ };
+};