diff --git a/pkgs/anoia/fs.fnl b/pkgs/anoia/fs.fnl index ba5bfa45..f0213494 100644 --- a/pkgs/anoia/fs.fnl +++ b/pkgs/anoia/fs.fnl @@ -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 } diff --git a/pkgs/anoia/init.fnl b/pkgs/anoia/init.fnl index 795a8eba..06695ba7 100644 --- a/pkgs/anoia/init.fnl +++ b/pkgs/anoia/init.fnl @@ -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 } diff --git a/pkgs/odhcp-script/odhcp6-script.fnl b/pkgs/odhcp-script/odhcp6-script.fnl index 3e5983e8..2d4c61ee 100644 --- a/pkgs/odhcp-script/odhcp6-script.fnl +++ b/pkgs/odhcp-script/odhcp6-script.fnl @@ -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"))