anoia.fs.mktree replaces mkdir

This uses lfs to make the tree in-process instead of
shelling out to the mkdir command
This commit is contained in:
Daniel Barlow 2023-09-08 21:17:42 +01:00
parent 4e9227dff3
commit 9dd3cf23b4
3 changed files with 18 additions and 10 deletions

View file

@ -1,5 +1,17 @@
(local lfs (require :lfs)) (local lfs (require :lfs))
(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)))))
(fn rmtree [pathname] (fn rmtree [pathname]
(case (lfs.symlinkattributes pathname) (case (lfs.symlinkattributes pathname)
nil true nil true
@ -17,4 +29,4 @@
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\"")))) (error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
{ : rmtree } { : mktree : rmtree }

View file

@ -13,7 +13,4 @@
(fn system [s] (assert (os.execute s))) (fn system [s] (assert (os.execute s)))
(fn mkdir [directory] { : merge : split : file-exists? : system }
(os.execute (.. "mkdir -p " directory)))
{ : merge : split : file-exists? : system : mkdir }

View file

@ -1,9 +1,9 @@
(local { : split : merge : mkdir } (require :anoia)) (local { : split : merge } (require :anoia))
(local { : view } (require :fennel)) (local { : view } (require :fennel))
(local { : rmtree } (require :anoia.fs)) (local { : mktree : rmtree } (require :anoia.fs))
(local state-directory (assert (os.getenv "SERVICE_STATE"))) (local state-directory (assert (os.getenv "SERVICE_STATE")))
(mkdir state-directory) (mktree state-directory)
(fn write-value [name value] (fn write-value [name value]
(let [path (.. state-directory "/" name)] (let [path (.. state-directory "/" name)]
@ -29,11 +29,10 @@
keydir (.. prefix (-> address.address keydir (.. prefix (-> address.address
(: :gsub "::$" "") (: :gsub "::$" "")
(: :gsub ":" "-")))] (: :gsub ":" "-")))]
(mkdir (.. state-directory "/" keydir)) (mktree (.. state-directory "/" keydir))
(each [k v (pairs address)] (each [k v (pairs address)]
(write-value (.. keydir "/" k) v))))) (write-value (.. keydir "/" k) v)))))
;; we remove state before updating to ensure that consumers don't get ;; we remove state before updating to ensure that consumers don't get
;; a half-updated snapshot ;; a half-updated snapshot
(os.remove (.. state-directory "/state")) (os.remove (.. state-directory "/state"))