ezs/deamon/deamon.odin
2025-06-18 22:04:25 +02:00

44 lines
1.2 KiB
Odin

package deamon
import "core:log"
import "core:sys/posix"
import "core:sys/linux"
NAME_MAX :: posix.NAME_MAX
handle_inotify :: proc() {
fd, err := linux.inotify_init()
if err != nil {
log.fatalf("Could not initalize inotify: %v", err)
}
watch, watch_err := linux.inotify_add_watch(fd, "./cmd/", linux.IN_ALL_EVENTS | {.ONLYDIR})
if watch_err != nil {
log.fatalf("Could not add watch to directory 'cmd': %v", watch_err)
}
log.infof("Initalized with wd %v for %v", watch, fd)
for {
buffer: [size_of(linux.Inotify_Event)+NAME_MAX+1]u8
size, err := linux.read(fd, buffer[:])
if err == .EINVAL {
log.debug(err)
continue
} else if err != .NONE {
log.errorf("Could not read inotify fd: %v", err)
continue
}
i := 0
for size >= size_of(linux.Inotify_Event) {
event := (cast(^linux.Inotify_Event)&buffer[i])^
name := event.len > 0 ? cstring(raw_data(buffer[size_of(linux.Inotify_Event)+i:int(event.len)+i])) : ""
log.infof("%q: %v", name, event)
size -= int(size_of(linux.Inotify_Event)+event.len)
i += int(size_of(linux.Inotify_Event)+event.len)
}
}
}