Source   Edit


import std/inotify
when defined(linux):
  let inoty: FileHandle = inotify_init()           ## Create 1 Inotify.
  doAssert inoty >= 0                              ## Check for errors (FileHandle is alias to cint).
  let watchdoge: cint = inotify_add_watch(inoty, ".", IN_ALL_EVENTS) ## Add directory to watchdog.
  doAssert watchdoge >= 0                          ## Check for errors.
  doAssert inotify_rm_watch(inoty, watchdoge) >= 0 ## Remove directory from the watchdog


InotifyEvent {.pure, final, importc: "struct inotify_event",
               header: "<sys/inotify.h>".} = object
  wd* {.importc: "wd".}: FileHandle ## Watch descriptor.
  mask* {.importc: "mask".}: uint32 ## Watch mask.
  cookie* {.importc: "cookie".}: uint32 ## Cookie to synchronize two events.
  len* {.importc: "len".}: uint32 ## Length (including NULs) of name.
  name* {.importc: "name".}: char ## Name.
An Inotify event.   Source   Edit


IN_ACCESS = 0x00000001
File was accessed.   Source   Edit
  Source   Edit
IN_ATTRIB = 0x00000004
Metadata changed.   Source   Edit
Close.   Source   Edit
IN_CLOSE_NOWRITE = 0x00000010
Unwrittable file closed.   Source   Edit
IN_CLOSE_WRITE = 0x00000008
Writtable file was closed.   Source   Edit
IN_CREATE = 0x00000100
Subfile was created.   Source   Edit
IN_DELETE = 0x00000200
Subfile was deleted.   Source   Edit
IN_DELETE_SELF = 0x00000400
Self was deleted.   Source   Edit
IN_DONT_FOLLOW = 0x02000000
Do not follow a sym link.   Source   Edit
IN_IGNORED = 0x00008000
File was ignored.   Source   Edit
IN_ISDIR = 0x40000000
Event occurred against dir.   Source   Edit
IN_MASK_ADD = 0x20000000
Add to the mask of an already existing watch.   Source   Edit
IN_MODIFY = 0x00000002
File was modified.   Source   Edit
IN_MOVE = 192
Moves.   Source   Edit
IN_MOVE_SELF = 0x00000800
Self was moved.   Source   Edit
IN_MOVED_FROM = 0x00000040
File was moved from X.   Source   Edit
IN_MOVED_TO = 0x00000080
File was moved to Y.   Source   Edit
IN_ONESHOT = 0x0000000080000000'i64
Only send event once.   Source   Edit
IN_ONLYDIR = 0x01000000
Only watch the path if it is a directory.   Source   Edit
IN_OPEN = 0x00000020
File was opened.   Source   Edit
IN_Q_OVERFLOW = 0x00004000
Event queued overflowed.   Source   Edit
IN_UNMOUNT = 0x00002000
Backing fs was unmounted.   Source   Edit


proc inotify_add_watch(fd: cint; name: cstring; mask: uint32): cint {.cdecl,
    importc: "inotify_add_watch", header: "<sys/inotify.h>", ...raises: [],
    tags: [].}
Add watch of object NAME to inotify instance FD. Notify about events specified by MASK.   Source   Edit
proc inotify_init(): FileHandle {.cdecl, importc: "inotify_init",
                                  header: "<sys/inotify.h>", ...raises: [],
                                  tags: [].}
Create and initialize inotify instance.   Source   Edit
proc inotify_init1(flags: cint): FileHandle {.cdecl, importc: "inotify_init1",
    header: "<sys/inotify.h>", ...raises: [], tags: [].}
Create and initialize inotify instance.   Source   Edit
proc inotify_rm_watch(fd: cint; wd: cint): cint {.cdecl,
    importc: "inotify_rm_watch", header: "<sys/inotify.h>", ...raises: [], tags: [].}
Remove the watch specified by WD from the inotify instance FD.   Source   Edit


iterator inotify_events(evs: pointer; n: int): ptr InotifyEvent {....raises: [],
    tags: [].}
Abstract the packed buffer interface to yield event object pointers.
var evs = newSeq[byte](8192)        # Already did inotify_init+add_watch
while (let n = read(fd, evs[0].addr, 8192); n) > 0:     # read forever
  for e in inotify_events(evs[0].addr, n): echo e[].len # echo name lens
  Source   Edit