hare

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

commit 8d7454a23348df1ac931e898b7d2efd88747621a
parent 22ff01a1c9e44e890147cb80e27ab922a614b44d
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sun,  5 Feb 2023 00:32:15 -0500

fs: re-use path buffer in rmdirall

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mfs/util.ha | 16++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/fs/util.ha b/fs/util.ha @@ -111,6 +111,13 @@ export fn dirents_free(d: []dirent) void = { // Removes a directory, and anything in it. export fn rmdirall(fs: *fs, path: str) (void | error) = { + let buf = path::init(); + path::set(&buf, path)!; + return rmdirall_path(fs, &buf); +}; + +fn rmdirall_path(fs: *fs, buf: *path::buffer) (void | error) = { + let path = path::string(buf); let it = iter(fs, path)?; for (true) { match (next(it)) { @@ -118,18 +125,15 @@ export fn rmdirall(fs: *fs, path: str) (void | error) = { if (ent.name == "." || ent.name == "..") { continue; }; - // XXX: This could probably be more efficient if we - // re-used the buffer further down the call stack. - let buf = alloc(path::init()); - defer free(buf); - const path = path::set(buf, path, ent.name)!; + path = path::add(buf, ent.name)!; switch (ent.ftype & mode::DIR) { case mode::DIR => - rmdirall(fs, path)?; + rmdirall_path(fs, buf)?; case => remove(fs, path)?; }; + path = path::add(buf, "..")!; case void => break; };