From e9370358aedb76d43dcd617eb20005f67357b673 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 21 Apr 2024 11:19:06 +0100 Subject: [PATCH] implement "remove" events --- pkgs/devout/devout.fnl | 7 ++++++- pkgs/devout/test.fnl | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkgs/devout/devout.fnl b/pkgs/devout/devout.fnl index b3fec14..b1481fb 100644 --- a/pkgs/devout/devout.fnl +++ b/pkgs/devout/devout.fnl @@ -26,7 +26,12 @@ :find (fn [_ terms] (find-in-database db terms)) :add (fn [_ event-string] (let [e (parse-uevent event-string)] - (tset db e.path e))) + (match e.action + :add (tset db e.path e) + :change (tset db e.path e) + ; ;bind ? + :remove (tset db e.path nil) + ))) :at-path (fn [_ path] (. db path)) })) diff --git a/pkgs/devout/test.fnl b/pkgs/devout/test.fnl index e128fb3..796ce30 100644 --- a/pkgs/devout/test.fnl +++ b/pkgs/devout/test.fnl @@ -27,6 +27,31 @@ DEVTYPE=disk DISKSEQ=2 SEQNUM=1527") +(local sdb1-insert + "add@/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1\0ACTION=add +DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1 +SUBSYSTEM=block +DEVNAME=/dev/sdb1 +DEVTYPE=partition +DISKSEQ=33 +PARTN=1 +SEQNUM=82381 +MAJOR=8 +MINOR=17") + +(local sdb1-remove + "remove@/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1\0ACTION=remove +DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1 +SUBSYSTEM=block +DEVNAME=/dev/sdb1 +DEVTYPE=partition +DISKSEQ=33 +PARTN=1 +SEQNUM=82386 +MAJOR=8 +MINOR=17") + + (example "when I add a device, I can find it" (let [db (database)] @@ -50,6 +75,21 @@ SEQNUM=1527") (expect= m.devname "sda") (expect= m.major "8")))) +(example + "when I add and then remove a device, I cannot retrieve it by path" + (let [db (database)] + (db:add sdb1-insert) + (db:add sdb1-remove) + (expect= (db:at-path "/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host1/target1:0:0/1:0:0:0/block/sdb/sdb1") nil))) + +(example + "when I add and then remove a device, I cannot find it" + (let [db (database)] + (db:add sdb1-insert) + (db:add sda-uevent) + (db:add sdb1-remove) + (expect= (db:find {:devname "/dev/sdb1"}) []))) + (example "when I search on multiple terms it uses all of them" (let [db (database)]