2023-09-08 22:03:18 +02:00
|
|
|
(local lfs (require :lfs))
|
|
|
|
|
2023-09-08 22:17:42 +02:00
|
|
|
(fn directory? [pathname]
|
|
|
|
(= (lfs.symlinkattributes pathname :mode) "directory"))
|
|
|
|
|
|
|
|
(fn mktree [pathname]
|
|
|
|
(if (or (= pathname "") (= pathname "/"))
|
|
|
|
(error (.. "can't mkdir " pathname)))
|
|
|
|
|
|
|
|
(or (directory? pathname)
|
|
|
|
(let [parent (string.gsub pathname "/[^/]+/?$" "")]
|
|
|
|
(or (directory? parent) (mktree parent))
|
|
|
|
(assert (lfs.mkdir pathname)))))
|
|
|
|
|
2023-09-08 22:03:18 +02:00
|
|
|
(fn rmtree [pathname]
|
|
|
|
(case (lfs.symlinkattributes pathname)
|
|
|
|
nil true
|
|
|
|
{:mode "directory"}
|
|
|
|
(do
|
|
|
|
(each [f (lfs.dir pathname)]
|
|
|
|
(when (not (or (= f ".") (= f "..")))
|
|
|
|
(rmtree ( .. pathname "/" f)))
|
|
|
|
(lfs.rmdir pathname)))
|
|
|
|
{:mode "file"}
|
|
|
|
(os.remove pathname)
|
|
|
|
{:mode "link"}
|
|
|
|
(os.remove pathname)
|
|
|
|
unknown
|
|
|
|
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
|
|
|
|
|
|
|
|
|
2023-09-09 01:16:21 +02:00
|
|
|
{
|
|
|
|
: mktree
|
|
|
|
: rmtree
|
|
|
|
: directory?
|
|
|
|
}
|