commit 5aefaad5fca9fd8078d03e45c6338883f7623a4f
parent cee7bd005cdcdc0da162b8b9cde1b5d34d83217e
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 9 Mar 2021 15:32:53 -0500
fs: add remove, rmdir, rmdirall
Diffstat:
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/fs/fs.ha b/fs/fs.ha
@@ -40,6 +40,14 @@ export fn create(
};
};
+// Removes a file.
+export fn remove(fs: *fs, path: path::path) (void | error) = {
+ return match (fs.remove) {
+ null => io::unsupported,
+ f: *removefunc => f(fs, path),
+ };
+};
+
// Returns an iterator for a path, which yields the contents of a directory.
// Pass empty string to yield from the root. The order in which entries are
// returned is undefined.
@@ -90,6 +98,20 @@ export fn mkdirs(fs: *fs, path: path::path) (void | error) = {
return mkdir(fs, path);
};
+// Removes a directory. The target directory must be empty; see [rmdirall] to
+// remove its contents as well.
+export fn rmdir(fs: *fs, path: path::path) (void | error) = {
+ return match (fs.rmdir) {
+ null => io::unsupported,
+ f: *rmdirfunc => f(fs, path),
+ };
+};
+
+// Removes a directory, and anything in it.
+export fn rmdirall(fs: *fs, path: path::path) (void | error) = {
+ abort(); // TODO
+};
+
// Creates a directory and returns a subdir for it. Some filesystems support
// doing this operation atomically, but if not, a fallback is used.
export fn mksubdir(fs: *fs, path: path::path) (*fs | error) = {
diff --git a/fs/types.ha b/fs/types.ha
@@ -146,7 +146,7 @@ export type flags = enum int {
WRONLY = 1,
RDWR = 2,
CREATE = 0o100,
- EXCLUSIVE = 0o200,
+ EXCL = 0o200,
NOCTTY = 0o400,
TRUNC = 0o1000,
APPEND = 0o2000,
@@ -157,13 +157,16 @@ export type flags = enum int {
DIRECTORY = 0o200000,
NOFOLLOW = 0o400000,
CLOEXEC = 0o2000000,
+ TMPFILE = 0o20200000,
};
export type closefunc = fn(fs: *fs) void;
+export type removefunc = fn(fs: *fs, path: path::path) (void | error);
export type iterfunc = fn(fs: *fs, path: path::path) (*iterator | error);
export type statfunc = fn(fs: *fs, path: path::path) (filestat | error);
export type subdirfunc = fn(fs: *fs, path: path::path) (*fs | error);
export type mkdirfunc = fn(fs: *fs, path: path::path) (void | error);
+export type rmdirfunc = fn(fs: *fs, path: path::path) (void | error);
export type mksubdirfunc = fn(fs: *fs, path: path::path) (*fs | error);
export type resolvefunc = fn(fs: *fs, path: path::path) path::path;
@@ -193,6 +196,9 @@ export type fs = struct {
// Creates a new file.
create: nullable *createfunc,
+ // Removes a file.
+ remove: nullable *removefunc,
+
// Returns an iterator for a path, which yields the contents of a
// directory. Pass empty string to yield from the root.
//
@@ -210,6 +216,9 @@ export type fs = struct {
// Creates a directory.
mkdir: nullable *mkdirfunc,
+ // Removes a directory. The target directory must be empty.
+ rmdir: nullable *rmdirfunc,
+
// Creates a directory and returns a subdir for it.
mksubdir: nullable *mksubdirfunc,