hare

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

commit 4892c133c2c25bd653d753aa293f6b0d764346df
parent 73c1f66606a123d995d5bcab177b5b37f8d85186
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun,  9 Jan 2022 16:34:49 +0100

fs::readlink: use static return value

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

Diffstat:
Mfs/fs.ha | 4++--
Mhare/module/scan.ha | 11++++-------
Mhare/module/walk.ha | 7+++----
Mos/+freebsd/dirfdfs.ha | 2+-
Mos/+linux/dirfdfs.ha | 2+-
Mos/fs.ha | 4++--
6 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/fs/fs.ha b/fs/fs.ha @@ -154,8 +154,8 @@ export fn exists(fs: *fs, path: str) bool = { }; }; -// Returns the path referred to by a symbolic link. The caller must free the -// return value. +// Returns the path referred to by a symbolic link. The return value is +// statically allocated and will be overwritten on subsequent calls. export fn readlink(fs: *fs, path: str) (str | error) = { match (fs.readlink) { case null => diff --git a/hare/module/scan.ha b/hare/module/scan.ha @@ -141,6 +141,7 @@ fn scan_directory( free(dirs); }; + let pathbuf = path::init(); for (true) { const ent = match (fs::next(iter)) { case void => @@ -151,14 +152,10 @@ fn scan_directory( switch (ent.ftype) { case fs::mode::LINK => - let linkpath = path::join(path, ent.name); - defer free(linkpath); - let linkpath = fs::readlink(ctx.fs, linkpath)?; - defer free(linkpath); + let linkpath = path::set(&pathbuf, path, ent.name)!; + linkpath = fs::readlink(ctx.fs, linkpath)?; if (!path::abs(linkpath)) { - let newpath = path::join(path, linkpath); - free(linkpath); - linkpath = newpath; + linkpath = path::set(&pathbuf, path, linkpath)!; }; const st = fs::stat(ctx.fs, linkpath)?; diff --git a/hare/module/walk.ha b/hare/module/walk.ha @@ -34,6 +34,8 @@ fn _walk( case let iter: *fs::iterator => yield iter; }; + + // TODO: Refactor me to use path::buffer for (true) { const ent = match (fs::next(iter)) { case void => @@ -60,11 +62,8 @@ fn _walk( let linkpath = path::join(path, ent.name); defer free(linkpath); let linkpath = fs::readlink(ctx.fs, linkpath)?; - defer free(linkpath); if (!path::abs(linkpath)) { - let newpath = path::join(path, linkpath); - free(linkpath); - linkpath = newpath; + linkpath = path::join(path, linkpath); }; const st = fs::stat(ctx.fs, linkpath)?; diff --git a/os/+freebsd/dirfdfs.ha b/os/+freebsd/dirfdfs.ha @@ -275,7 +275,7 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { case let z: size => yield z; }; - return strings::dup(strings::fromutf8(buf[..z])); + return strings::fromutf8(buf[..z]); }; fn fs_subdir(fs: *fs::fs, path: str) (*fs::fs | fs::error) = { diff --git a/os/+linux/dirfdfs.ha b/os/+linux/dirfdfs.ha @@ -305,7 +305,7 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { case let z: size => yield z; }; - return strings::dup(strings::fromutf8(buf[..z])); + return strings::fromutf8(buf[..z]); }; fn fs_subdir(fs: *fs::fs, path: str) (*fs::fs | fs::error) = { diff --git a/os/fs.ha b/os/fs.ha @@ -60,8 +60,8 @@ export fn chown(path: str, uid: uint, gid: uint) (void | fs::error) = fs::chown( // the return value. export fn resolve(path: str) str = fs::resolve(cwd, path); -// Returns the path referred to by a symbolic link. The caller must free the -// return value. +// Returns the path referred to by a symbolic link. The return value is +// statically allocated and will be overwritten on subsequent calls. export fn readlink(path: str) (str | fs::error) = fs::readlink(cwd, path); // Opens a file.