inotify

Readme.lua at tip

File Readme.lua from the latest check-in


--[[ inotify for lua

The reported flags will be:
- create
- modify
- attrib
- delete
(moved_to -> create + modify, moved_from -> delete)
]]

local inotify = require("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))
if arg[1] == "test_no_loop" then
  os.exit(0)
end
-- iteration returns string filepath, table flags and bool is_dir
-- pairs will work, but without timeout it is blocking
-- for filepath, flags, is_dir in pairs(inotify) do
for filepath, flags, is_dir 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 is_dir then
      print("del", filepath, inotify.del(filepath))
    end
    if flags.create and is_dir then
      print("add", filepath, inotify.add(filepath))
    end
  end
end