2024-04-24 21:44:32 +02:00
|
|
|
(local ll (require :lualinux))
|
|
|
|
|
|
|
|
(local S_IFMT 0xf000)
|
|
|
|
(local S_IFSOCK 0xc000)
|
|
|
|
(local S_IFLNK 0xa000)
|
|
|
|
(local S_IFREG 0x8000)
|
|
|
|
(local S_IFBLK 0x6000)
|
|
|
|
(local S_IFDIR 0x4000)
|
|
|
|
(local S_IFCHR 0x2000)
|
|
|
|
(local S_IFIFO 0x1000)
|
|
|
|
|
|
|
|
(fn ifmt-bits [mode] (and mode (band mode 0xf000)))
|
2023-09-08 22:03:18 +02:00
|
|
|
|
2024-04-25 22:14:37 +02:00
|
|
|
(fn file-type [pathname]
|
|
|
|
(. {
|
|
|
|
S_IFDIR :directory
|
|
|
|
S_IFSOCK :socket
|
|
|
|
S_IFLNK :link
|
|
|
|
S_IFREG :file
|
|
|
|
S_IFBLK :block-device
|
|
|
|
S_IFCHR :character-device
|
|
|
|
S_IFIFO :fifo
|
|
|
|
}
|
|
|
|
(ifmt-bits (ll.lstat3 pathname))))
|
|
|
|
|
2023-09-08 22:17:42 +02:00
|
|
|
(fn directory? [pathname]
|
2024-04-25 22:14:37 +02:00
|
|
|
(= (file-type pathname) :directory))
|
2023-09-08 22:17:42 +02:00
|
|
|
|
|
|
|
(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))
|
2024-04-24 21:44:32 +02:00
|
|
|
(assert (ll.mkdir pathname)))))
|
2023-09-08 22:17:42 +02:00
|
|
|
|
2024-04-25 22:14:37 +02:00
|
|
|
(fn dir [name]
|
|
|
|
(let [dp (assert (ll.opendir name) name)]
|
|
|
|
(fn []
|
|
|
|
(match (ll.readdir dp)
|
|
|
|
(name type) (values name type)
|
|
|
|
(nil err) (do (if err (print err)) (ll.closedir dp) nil)))))
|
|
|
|
|
2023-09-08 22:03:18 +02:00
|
|
|
(fn rmtree [pathname]
|
2024-04-25 22:14:37 +02:00
|
|
|
(case (file-type pathname)
|
2023-09-08 22:03:18 +02:00
|
|
|
nil true
|
2024-04-25 22:14:37 +02:00
|
|
|
:directory
|
2023-09-08 22:03:18 +02:00
|
|
|
(do
|
2024-04-25 22:14:37 +02:00
|
|
|
(each [f (dir pathname)]
|
2023-09-08 22:03:18 +02:00
|
|
|
(when (not (or (= f ".") (= f "..")))
|
|
|
|
(rmtree ( .. pathname "/" f)))
|
2024-04-25 22:14:37 +02:00
|
|
|
(ll.rmdir pathname)))
|
|
|
|
:file
|
2023-09-08 22:03:18 +02:00
|
|
|
(os.remove pathname)
|
2024-04-25 22:14:37 +02:00
|
|
|
:link
|
2023-09-08 22:03:18 +02:00
|
|
|
(os.remove pathname)
|
|
|
|
unknown
|
2024-04-24 21:44:32 +02:00
|
|
|
(error (.. "can't remove " pathname " of mode \"" unknown "\""))))
|
2023-09-08 22:03:18 +02:00
|
|
|
|
|
|
|
|
2023-09-09 01:16:21 +02:00
|
|
|
{
|
|
|
|
: mktree
|
|
|
|
: rmtree
|
|
|
|
: directory?
|
2024-04-25 22:14:37 +02:00
|
|
|
: dir
|
|
|
|
: file-type
|
2024-04-24 21:44:32 +02:00
|
|
|
:symlink (fn [from to] (ll.symlink from to))
|
2023-09-09 01:16:21 +02:00
|
|
|
}
|