summaryrefslogtreecommitdiff
path: root/lib/fileutil
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-06-24 13:38:25 -0700
committerOri Bernstein <ori@eigenstate.org>2018-06-24 13:38:25 -0700
commit0f959a0c6f79986cc32d8456b503957476bdae83 (patch)
tree01e0abf2b8732ba50091107121494ca5a029846a /lib/fileutil
parentfde9deac883c3f2ae6ffb7d05b4f5e404f5c2249 (diff)
downloadmc-0f959a0c6f79986cc32d8456b503957476bdae83.tar.gz
Add recursive file removal.
Also makes `std.remove` remove directories the same way it removes leaf files.
Diffstat (limited to 'lib/fileutil')
-rw-r--r--lib/fileutil/bld.sub1
-rw-r--r--lib/fileutil/del.myr43
2 files changed, 44 insertions, 0 deletions
diff --git a/lib/fileutil/bld.sub b/lib/fileutil/bld.sub
index e54f88d..00db725 100644
--- a/lib/fileutil/bld.sub
+++ b/lib/fileutil/bld.sub
@@ -2,6 +2,7 @@ lib fileutil =
walk.myr
homedir.myr
tmpdir.myr
+ del.myr
loopcheck+posixy.myr
loopcheck+plan9.myr
diff --git a/lib/fileutil/del.myr b/lib/fileutil/del.myr
new file mode 100644
index 0000000..c82bfc9
--- /dev/null
+++ b/lib/fileutil/del.myr
@@ -0,0 +1,43 @@
+use std
+
+use "loopcheck"
+
+pkg fileutil =
+ const rmrf : (dir : byte[:] -> bool)
+;;
+
+const rmrf = {dir
+ var loopck, ok
+
+ loopck = mkloopcheck(dir)
+ ok = rmrf_rec(dir, loopck)
+ freeloopcheck(loopck)
+ -> ok
+}
+
+const rmrf_rec = {path, loopck
+ var ok, p
+
+ ok = true
+ if std.fisdir(path)
+ match std.diropen(path)
+ | `std.Err e:
+ ok = false
+ | `std.Ok d:
+ for e : std.byentry(d)
+ if std.sleq(e, ".") || std.sleq(e, "..")
+ continue
+ ;;
+ p = std.pathcat(path, e)
+ if !looped(loopck, p)
+ ok = ok && rmrf(p)
+ ;;
+ std.slfree(p)
+ ;;
+ ok = ok && std.remove(path)
+ ;;
+ else
+ ok = ok && std.remove(path)
+ ;;
+ -> ok
+}