hare

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

commit fbdc4e4319b0a753f3ad4ca60dc22152e5bec412
parent 578a4e3d62125cc987329080ca8a99a4633b884e
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri,  4 Mar 2022 13:26:32 +0100

fs: drop subdir/mksubdir

This is not really that useful and I would like to slim down this
interface a bit. Similar functionality will be possible directly via the
os module with dirfds.

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mfs/fs.ha | 24------------------------
Mfs/types.ha | 8--------
Mos/+freebsd/dirfdfs.ha | 15---------------
Mos/+linux/dirfdfs.ha | 19-------------------
Mos/fs.ha | 13++++++-------
5 files changed, 6 insertions(+), 73 deletions(-)

diff --git a/fs/fs.ha b/fs/fs.ha @@ -176,18 +176,6 @@ export fn readlink(fs: *fs, path: str) (str | error) = { }; }; -// Opens a new filesystem for a subdirectory. The subdirectory must be closed -// separately from the parent filesystem, and its lifetime can outlive that of -// its parent. -export fn subdir(fs: *fs, path: str) (*fs | error) = { - match (fs.subdir) { - case null => - return errors::unsupported; - case let f: *subdirfunc => - return f(fs, path); - }; -}; - // Creates a directory. export fn mkdir(fs: *fs, path: str) (void | error) = { match (fs.mkdir) { @@ -231,18 +219,6 @@ export fn rmdir(fs: *fs, path: str) (void | error) = { }; }; -// 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: str) (*fs | error) = { - match (fs.mksubdir) { - case null => - mkdir(fs, path)?; - return subdir(fs, path); - case let f: *mksubdirfunc => - return f(fs, path); - }; -}; - // Changes mode flags on a file or directory. export fn chmod(fs: *fs, path: str, mode: mode) (void | error) = { match (fs.chmod) { diff --git a/fs/types.ha b/fs/types.ha @@ -192,10 +192,8 @@ export type removefunc = fn(fs: *fs, path: str) (void | error); export type renamefunc = fn(fs: *fs, oldpath: str, newpath: str) (void | error); export type iterfunc = fn(fs: *fs, path: str) (*iterator | error); export type statfunc = fn(fs: *fs, path: str) (filestat | error); -export type subdirfunc = fn(fs: *fs, path: str) (*fs | error); export type mkdirfunc = fn(fs: *fs, path: str) (void | error); export type rmdirfunc = fn(fs: *fs, path: str) (void | error); -export type mksubdirfunc = fn(fs: *fs, path: str) (*fs | error); export type chmodfunc = fn(fs: *fs, path: str, mode: mode) (void | error); export type chownfunc = fn(fs: *fs, path: str, uid: uint, gid: uint) (void | error); export type resolvefunc = fn(fs: *fs, path: str) str; @@ -274,18 +272,12 @@ export type fs = struct { // the return value. readlink: nullable *readlinkfunc, - // Opens a new filesystem for a subdirectory. - subdir: nullable *subdirfunc, - // 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, - // Changes mode flags on a file or directory. chmod: nullable *chmodfunc, diff --git a/os/+freebsd/dirfdfs.ha b/os/+freebsd/dirfdfs.ha @@ -43,7 +43,6 @@ fn static_dirfdopen(fd: io::file, filesystem: *os_filesystem) *fs::fs = { iter = &fs_iter, stat = &fs_stat, readlink = &fs_readlink, - subdir = &fs_subdir, mkdir = &fs_mkdir, rmdir = &fs_rmdir, chmod = &fs_chmod, @@ -281,20 +280,6 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { return strings::fromutf8(buf[..z]); }; -fn fs_subdir(fs: *fs::fs, path: str) (*fs::fs | fs::error) = { - let fs = fs: *os_filesystem; - let flags = rt::O_RDONLY | rt::O_CLOEXEC | rt::O_DIRECTORY; - - let fd: int = match (rt::openat(fs.dirfd, path, flags, 0)) { - case let err: rt::errno => - return errno_to_fs(err); - case let fd: int => - yield fd; - }; - - return dirfdopen(fd); -}; - fn fs_rmdir(fs: *fs::fs, path: str) (void | fs::error) = { let fs = fs: *os_filesystem; match (rt::unlinkat(fs.dirfd, path, rt::AT_REMOVEDIR)) { diff --git a/os/+linux/dirfdfs.ha b/os/+linux/dirfdfs.ha @@ -78,7 +78,6 @@ fn static_dirfdopen(fd: io::file, filesystem: *os_filesystem) *fs::fs = { iter = &fs_iter, stat = &fs_stat, readlink = &fs_readlink, - subdir = &fs_subdir, mkdir = &fs_mkdir, rmdir = &fs_rmdir, chmod = &fs_chmod, @@ -315,24 +314,6 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { return strings::fromutf8(buf[..z]); }; -fn fs_subdir(fs: *fs::fs, path: str) (*fs::fs | fs::error) = { - let fs = fs: *os_filesystem; - let oh = rt::open_how { - flags = (rt::O_RDONLY | rt::O_CLOEXEC | rt::O_DIRECTORY): u64, - ... - }; - - let fd: int = match (rt::openat2(fs.dirfd, path, - &oh, size(rt::open_how))) { - case let err: rt::errno => - return errno_to_fs(err); - case let fd: int => - yield fd; - }; - - return dirfdopen(fd); -}; - fn fs_rmdir(fs: *fs::fs, path: str) (void | fs::error) = { let fs = fs: *os_filesystem; match (rt::unlinkat(fs.dirfd, path, rt::AT_REMOVEDIR)) { diff --git a/os/fs.ha b/os/fs.ha @@ -33,9 +33,6 @@ export fn readdir(path: str) ([]fs::dirent | fs::error) = fs::readdir(cwd, path) // Returns file information for a given path. export fn stat(path: str) (fs::filestat | fs::error) = fs::stat(cwd, path); -// Opens a directory as a filesystem. -export fn diropen(path: str) (*fs::fs | fs::error) = fs::subdir(cwd, path); - // Creates a directory. export fn mkdir(path: str) (void | fs::error) = fs::mkdir(cwd, path); @@ -49,10 +46,6 @@ export fn rmdir(path: str) (void | fs::error) = fs::rmdir(cwd, path); // Removes a directory, and anything in it. export fn rmdirall(path: str) (void | fs::error) = fs::rmdirall(cwd, path); -// 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(path: str) (*fs::fs | fs::error) = fs::mksubdir(cwd, path); - // Changes mode flags on a file or directory. Type bits are discared. export fn chmod(path: str, mode: fs::mode) (void | fs::error) = fs::chmod(cwd, path, mode); @@ -99,3 +92,9 @@ export fn create( // statically allocated by [[fs::realpath]]. Thus, calls to this function or to // [[fs::realpath]] will overwrite the return value of either function. export fn realpath(path: str) (str | fs::error) = fs::realpath(cwd, path); + +// Opens a directory as a filesystem. +export fn diropen(path: str) (*fs::fs | fs::error) = { + const file = open(path, fs::flags::DIRECTORY | fs::flags::RDONLY)?; + return dirfdopen(file); +};