summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/fileutil/bld.sub1
-rw-r--r--lib/fileutil/del.myr43
-rw-r--r--lib/std/syswrap+posixy.myr8
-rw-r--r--lib/sys/sys+netbsd-x64.myr2
-rw-r--r--lib/sys/sys+osx-x64.myr2
5 files changed, 55 insertions, 1 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
+}
diff --git a/lib/std/syswrap+posixy.myr b/lib/std/syswrap+posixy.myr
index d0c63c8..23b8e04 100644
--- a/lib/std/syswrap+posixy.myr
+++ b/lib/std/syswrap+posixy.myr
@@ -92,7 +92,13 @@ const dup2 = {ofd, nfd; -> check((sys.dup2((ofd : sys.fd), (nfd : sys.fd)) : fd)
/* 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
diff --git a/lib/sys/sys+netbsd-x64.myr b/lib/sys/sys+netbsd-x64.myr
index 02f9a76..87c590a 100644
--- a/lib/sys/sys+netbsd-x64.myr
+++ b/lib/sys/sys+netbsd-x64.myr
@@ -853,6 +853,7 @@ pkg sys =
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 openmode = {path, opts, mode; -> (syscall(Sysopen, cstring(path), a(opts),
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)}
diff --git a/lib/sys/sys+osx-x64.myr b/lib/sys/sys+osx-x64.myr
index bd1f427..e260889 100644
--- a/lib/sys/sys+osx-x64.myr
+++ b/lib/sys/sys+osx-x64.myr
@@ -773,6 +773,7 @@ pkg sys =
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 close = {fd; -> syscall(Sysclose, a(fd))}
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)}