liminix/pkgs/anoia/svc.fnl

56 lines
1.6 KiB
Text
Raw Normal View History

(local inotify (require :inotify))
(local { : file-exists? } (require :anoia))
2024-04-25 22:14:37 +02:00
(local { : file-type : dir &as fs } (require :anoia.fs))
(fn read-line [name]
(with-open [f (assert (io.open name :r) (.. "can't open file " name))]
(f:read "*l")))
(fn watch-fsevents [directory-name]
(let [handle (inotify.init)]
(handle:addwatch directory-name
inotify.IN_CREATE
inotify.IN_MOVE
inotify.IN_DELETE
inotify.IN_DELETE_SELF
inotify.IN_MOVED_FROM
inotify.IN_MOVED_TO
inotify.IN_CLOSE_WRITE)
handle))
(fn read-value [pathname]
2024-04-25 22:14:37 +02:00
(case (file-type pathname)
nil nil
2024-04-25 22:14:37 +02:00
:directory
(collect [f (fs.dir pathname)]
(when (not (or (= f ".") (= f "..")))
(values f (read-value ( .. pathname "/" f)))))
2024-04-25 22:14:37 +02:00
:file
(read-line pathname)
2024-04-25 22:14:37 +02:00
:link
(read-line pathname)
unknown
(error (.. "can't read " pathname " of kind \"" unknown.mode "\""))))
(fn events [self]
(coroutine.wrap
#(while true
(while (not (self:ready?)) (self:wait))
(coroutine.yield self)
(self:wait))))
(fn open [directory]
(let [watcher (watch-fsevents directory)
has-file? (fn [filename] (file-exists? (.. directory "/" filename)))]
{
:wait #(watcher:read)
:ready? (fn [self]
(and (has-file? "state") (not (has-file? ".lock"))))
:output (fn [_ filename]
(read-value (.. directory "/" filename)))
:close #(watcher:close)
: events
}))
{ : open }