convert anoia.fs to use lualinux

This commit is contained in:
Daniel Barlow 2024-04-25 21:14:37 +01:00
parent dbd1264352
commit cdb23b147c
9 changed files with 44 additions and 21 deletions

View file

@ -3,8 +3,9 @@
, linotify , linotify
, anoia , anoia
, lua , lua
, lualinux
}: }:
writeFennel "acquire-delegated-prefix" { writeFennel "acquire-delegated-prefix" {
packages = [ linotify anoia lua.pkgs.luafilesystem ]; packages = [ linotify anoia lualinux ];
mainFunction = "run"; mainFunction = "run";
} ./acquire-delegated-prefix.fnl } ./acquire-delegated-prefix.fnl

View file

@ -2,9 +2,10 @@
writeFennel writeFennel
, linotify , linotify
, anoia , anoia
, lualinux
, lua , lua
}: }:
writeFennel "acquire-wan-address" { writeFennel "acquire-wan-address" {
packages = [ linotify anoia lua.pkgs.luafilesystem ]; packages = [ linotify anoia lualinux ];
mainFunction = "run"; mainFunction = "run";
} ./acquire-wan-address.fnl } ./acquire-wan-address.fnl

View file

@ -2,7 +2,9 @@
default: fs.lua init.lua nl.lua svc.lua default: fs.lua init.lua nl.lua svc.lua
test: test:
ln -s . anoia
fennel test.fnl fennel test.fnl
fennel test-svc.fnl
%.lua: %.fnl %.lua: %.fnl
fennel --compile $< > $@ fennel --compile $< > $@

View file

@ -1,6 +1,7 @@
{ {
fennel fennel
, stdenv , stdenv
, linotify
, lua , lua
, lualinux , lualinux
}: }:
@ -10,7 +11,7 @@ in stdenv.mkDerivation {
version = "0.1"; version = "0.1";
src = ./.; src = ./.;
nativeBuildInputs = [ fennel ]; nativeBuildInputs = [ fennel ];
buildInputs = with lua.pkgs; [ luafilesystem lualinux ]; buildInputs = with lua.pkgs; [ linotify lualinux ];
outputs = [ "out" "dev" ]; outputs = [ "out" "dev" ];
doCheck = true; doCheck = true;

View file

@ -11,9 +11,20 @@
(fn ifmt-bits [mode] (and mode (band mode 0xf000))) (fn ifmt-bits [mode] (and mode (band mode 0xf000)))
(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))))
(fn directory? [pathname] (fn directory? [pathname]
(let [(mode size mtime) (ll.lstat3 pathname)] (= (file-type pathname) :directory))
(= (ifmt-bits mode) S_IFDIR)))
(fn mktree [pathname] (fn mktree [pathname]
(if (or (= pathname "") (= pathname "/")) (if (or (= pathname "") (= pathname "/"))
@ -24,18 +35,25 @@
(or (directory? parent) (mktree parent)) (or (directory? parent) (mktree parent))
(assert (ll.mkdir pathname))))) (assert (ll.mkdir pathname)))))
(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)))))
(fn rmtree [pathname] (fn rmtree [pathname]
(case (ifmt-bits (ll.lstat3 pathname)) (case (file-type pathname)
nil true nil true
S_IFDIR :directory
(do (do
(each [f (lfs.dir pathname)] (each [f (dir pathname)]
(when (not (or (= f ".") (= f ".."))) (when (not (or (= f ".") (= f "..")))
(rmtree ( .. pathname "/" f))) (rmtree ( .. pathname "/" f)))
(lfs.rmdir pathname))) (ll.rmdir pathname)))
S_IFREG :file
(os.remove pathname) (os.remove pathname)
S_IFLNK :link
(os.remove pathname) (os.remove pathname)
unknown unknown
(error (.. "can't remove " pathname " of mode \"" unknown "\"")))) (error (.. "can't remove " pathname " of mode \"" unknown "\""))))
@ -45,5 +63,7 @@
: mktree : mktree
: rmtree : rmtree
: directory? : directory?
: dir
: file-type
:symlink (fn [from to] (ll.symlink from to)) :symlink (fn [from to] (ll.symlink from to))
} }

View file

@ -1,7 +1,6 @@
(local inotify (require :inotify)) (local inotify (require :inotify))
(local { : file-exists? } (require :anoia)) (local { : file-exists? } (require :anoia))
(local { : directory? } (require :anoia.fs)) (local { : file-type : dir &as fs } (require :anoia.fs))
(local lfs (require :lfs))
(fn read-line [name] (fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))] (with-open [f (assert (io.open name :r) (.. "can't open file " name))]
@ -20,15 +19,15 @@
handle)) handle))
(fn read-value [pathname] (fn read-value [pathname]
(case (lfs.symlinkattributes pathname) (case (file-type pathname)
nil nil nil nil
{:mode "directory"} :directory
(collect [f (lfs.dir pathname)] (collect [f (fs.dir pathname)]
(when (not (or (= f ".") (= f ".."))) (when (not (or (= f ".") (= f "..")))
(values f (read-value ( .. pathname "/" f))))) (values f (read-value ( .. pathname "/" f)))))
{:mode "file"} :file
(read-line pathname) (read-line pathname)
{:mode "link"} :link
(read-line pathname) (read-line pathname)
unknown unknown
(error (.. "can't read " pathname " of kind \"" unknown.mode "\"")))) (error (.. "can't read " pathname " of kind \"" unknown.mode "\""))))

View file

@ -1,4 +1,4 @@
(local svc (require :anoia.svc)) (local svc (require :svc))
(local { : view } (require :fennel)) (local { : view } (require :fennel))
(local ex (svc.open "./example-output")) (local ex (svc.open "./example-output"))

View file

@ -15,7 +15,7 @@ stdenv.mkDerivation {
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p $out/bin
cp -p ${writeFennel "devout" { cp -p ${writeFennel "devout" {
packages = [fennel anoia nellie lua.pkgs.luafilesystem lualinux]; packages = [fennel anoia nellie lualinux];
mainFunction = "run"; mainFunction = "run";
} ./devout.fnl} $out/bin/devout } ./devout.fnl} $out/bin/devout
''; '';

View file

@ -19,7 +19,6 @@ let packages = [
lualinux lualinux
netlink-lua netlink-lua
lua.pkgs.readline lua.pkgs.readline
lua.pkgs.luafilesystem
]; ];
join = ps: builtins.concatStringsSep ";" ps; join = ps: builtins.concatStringsSep ";" ps;
luapath = join (builtins.map (f: luapath = join (builtins.map (f: