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:
parent
4e9227dff3
commit
9dd3cf23b4
3 changed files with 18 additions and 10 deletions
|
@ -1,5 +1,17 @@
|
|||
(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]
|
||||
(case (lfs.symlinkattributes pathname)
|
||||
nil true
|
||||
|
@ -17,4 +29,4 @@
|
|||
(error (.. "can't remove " pathname " of kind \"" unknown.mode "\""))))
|
||||
|
||||
|
||||
{ : rmtree }
|
||||
{ : mktree : rmtree }
|
||||
|
|
|
@ -13,7 +13,4 @@
|
|||
|
||||
(fn system [s] (assert (os.execute s)))
|
||||
|
||||
(fn mkdir [directory]
|
||||
(os.execute (.. "mkdir -p " directory)))
|
||||
|
||||
{ : merge : split : file-exists? : system : mkdir }
|
||||
{ : merge : split : file-exists? : system }
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
(local { : split : merge : mkdir } (require :anoia))
|
||||
(local { : split : merge } (require :anoia))
|
||||
(local { : view } (require :fennel))
|
||||
(local { : rmtree } (require :anoia.fs))
|
||||
(local { : mktree : rmtree } (require :anoia.fs))
|
||||
|
||||
(local state-directory (assert (os.getenv "SERVICE_STATE")))
|
||||
(mkdir state-directory)
|
||||
(mktree state-directory)
|
||||
|
||||
(fn write-value [name value]
|
||||
(let [path (.. state-directory "/" name)]
|
||||
|
@ -29,11 +29,10 @@
|
|||
keydir (.. prefix (-> address.address
|
||||
(: :gsub "::$" "")
|
||||
(: :gsub ":" "-")))]
|
||||
(mkdir (.. state-directory "/" keydir))
|
||||
(mktree (.. state-directory "/" keydir))
|
||||
(each [k v (pairs address)]
|
||||
(write-value (.. keydir "/" k) v)))))
|
||||
|
||||
|
||||
;; we remove state before updating to ensure that consumers don't get
|
||||
;; a half-updated snapshot
|
||||
(os.remove (.. state-directory "/state"))
|
||||
|
|
Loading…
Reference in a new issue