ref: 0f959a0c6f79986cc32d8456b503957476bdae83
parent: fde9deac883c3f2ae6ffb7d05b4f5e404f5c2249
author: Ori Bernstein <[email protected]>
date: Sun Jun 24 09:38:25 EDT 2018
Add recursive file removal. Also makes `std.remove` remove directories the same way it removes leaf files.
--- a/lib/fileutil/bld.sub
+++ b/lib/fileutil/bld.sub
@@ -2,6 +2,7 @@
walk.myr
homedir.myr
tmpdir.myr
+ del.myr
loopcheck+posixy.myr
loopcheck+plan9.myr
--- /dev/null
+++ b/lib/fileutil/del.myr
@@ -1,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
+}
--- a/lib/std/syswrap+posixy.myr
+++ b/lib/std/syswrap+posixy.myr
@@ -92,7 +92,13 @@
/* path manipulation */
const mkdir = {path, mode; -> (sys.mkdir(path, mode) : errno)}
const chdir = {path; -> sys.chdir(path) == 0}
-const remove = {path; -> sys.unlink(path) == 0}
+const remove = {path;
+ if std.fisdir(path)
+ -> sys.rmdir(sys.cstring(path)) == 0
+ else
+ -> sys.unlink(path) == 0
+ ;;
+}
/* useful/portable bits of uname */
const getsysinfo = {si
--- a/lib/sys/sys+netbsd-x64.myr
+++ b/lib/sys/sys+netbsd-x64.myr
@@ -853,6 +853,7 @@
const close : (fd:fd -> int64)
const creat : (path:byte[:], mode:int64 -> fd)
const unlink : (path:byte[:] -> int)
+ const rmdir : (path:byte[:] -> int)
const read : (fd:fd, buf:byte[:] -> size)
const pread : (fd:fd, buf:byte[:], off : off -> size)
const readv : (fd:fd, iov:iovec[:] -> size)
@@ -985,6 +986,7 @@
const close = {fd; -> syscall(Sysclose, a(fd))}
const creat = {path, mode; -> (openmode(path, Ocreat | Otrunc | Owronly, mode) : fd)}
const unlink = {path; -> (syscall(Sysunlink, cstring(path)) : int)}
+const rmdir = {path; -> (syscall(Sysunlink, a(path)) : int)}
const read = {fd, buf; -> (syscall(Sysread, a(fd), (buf : byte#), a(buf.len)) : size)}
const pread = {fd, buf, off; -> (syscall(Syspread, a(fd), (buf : byte#), a(buf.len), a(off)) : size)}
const readv = {fd, vec; -> (syscall(Sysreadv, a(fd), (vec : iovec#), a(vec.len)) : size)}
--- a/lib/sys/sys+osx-x64.myr
+++ b/lib/sys/sys+osx-x64.myr
@@ -773,6 +773,7 @@
const rename : (from : byte[:], to : byte[:] -> int64)
const creat : (path:byte[:], mode:int64 -> fd)
const unlink : (path:byte[:] -> int)
+ const rmdir : (path:byte# -> int)
const read : (fd:fd, buf:byte[:] -> size)
const pread : (fd:fd, buf:byte[:], off : off -> size)
const write : (fd:fd, buf:byte[:] -> size)
@@ -945,6 +946,7 @@
const rename = {from, to; -> syscall(Sysrename, cstring(from), cstring(to))}
const creat = {path, mode; -> (openmode(path, Ocreat | Otrunc | Owronly, mode) : fd)}
const unlink = {path; -> (syscall(Sysunlink, cstring(path)) : int)}
+const rmdir = {path; -> (syscall(Sysunlink, a(path)) : int)}
const read = {fd, buf; -> (syscall(Sysread, a(fd), (buf : byte#), a(buf.len)) : size)}
const pread = {fd, buf, off; -> (syscall(Syspread, a(fd), (buf : byte#), a(buf.len), a(off)) : size)}
const write = {fd, buf; -> (syscall(Syswrite, a(fd), (buf : byte#), a(buf.len)) : size)}