#!/usr/bin/lua5.4
--[[ inotify for lua
The reported flags will be:
- create
- modify
- attrib
- delete
- is_dir
(moved_to -> create + modify, moved_from -> delete)
]]
local inotify = require(arg[1] and "fanotify" or "inotify").init()
print("add ./", inotify.add("./"))
-- added dir with / in the end will be normalized
-- we can remove without /
-- call as self method will also work
print("del .", inotify:del("."))
-- without watches, will return immediately
print("has next:", inotify.next())
print("add .", inotify.add("."))
-- timeout 0 will return immediately
print("has next:", inotify.next(0))
-- iteration returns string filepath and table flags
-- pairs will work, but without timeout it is blocking
-- for filepath, flags in pairs(inotify) do
for filepath, flags in inotify.iter(5000) do
if filepath == "." then
-- exit with touch .
inotify:close()
os.exit(0)
end
if not(filepath) then
print("timeout - watching:", #inotify)
else
local flag_number = 1
for flag in pairs(flags) do
print(filepath, flag_number, flag)
flag_number = flag_number + 1
end
-- rename will handle subdirs wrong (old path)
-- on recursive dirs, every subdir needs to be readded
if flags.delete and flags.is_dir then
print("del", filepath, inotify.del(filepath))
end
if flags.create and flags.is_dir then
print("add", filepath, inotify.add(filepath))
end
end
end