std/inotify

Source   Edit  

Example:

import std/inotify
when defined(linux):
  let inotifyFd = inotify_init()  # create and get new inotify FileHandle
  doAssert inotifyFd >= 0         # check for errors

  let wd = inotifyFd.inotify_add_watch("/tmp", IN_CREATE or IN_DELETE)  # Add new watch
  doAssert wd >= 0                 # check for errors

  discard inotifyFd.inotify_rm_watch(wd) # remove watch

Types

InotifyEvent {.pure, final, importc: "struct inotify_event",
               header: "<sys/inotify.h>", completeStruct.} = 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".}: UncheckedArray[char] ## Name.
An Inotify event. Source   Edit  

Consts

IN_ACCESS = 0x00000001
File was accessed. Source   Edit  
IN_ALL_EVENTS = 4095
Source   Edit  
IN_ATTRIB = 0x00000004
Metadata changed. Source   Edit  
IN_CLOSE = 24
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  

Procs

proc inotify_add_watch(fd: cint; name: cstring; mask: uint32): cint {.cdecl,
    importc: "inotify_add_watch", header: "<sys/inotify.h>", ...raises: [],
    tags: [], forbids: [].}
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: [], forbids: [].}
Create and initialize inotify instance. Source   Edit  
proc inotify_init1(flags: cint): FileHandle {.cdecl, importc: "inotify_init1",
    header: "<sys/inotify.h>", ...raises: [], tags: [], forbids: [].}
Like inotify_init , but has a flags argument that provides access to some extra functionality. Source   Edit  
proc inotify_rm_watch(fd: cint; wd: cint): cint {.cdecl,
    importc: "inotify_rm_watch", header: "<sys/inotify.h>", ...raises: [],
    tags: [], forbids: [].}
Remove the watch specified by WD from the inotify instance FD. Source   Edit  

Iterators

iterator inotify_events(evs: pointer; n: int): ptr InotifyEvent {....raises: [],
    tags: [], forbids: [].}
Abstract the packed buffer interface to yield event object pointers.

Example: cmd: -r:off

when defined(linux):
   import std/posix  # needed for FileHandle read procedure
   const MaxWatches = 8192

   let inotifyFd = inotify_init()  # create new inotify instance and get it's FileHandle
   let wd = inotifyFd.inotify_add_watch("/tmp", IN_CREATE or IN_DELETE)  # Add new watch

   var events: array[MaxWatches, byte]  # event buffer
   while (let n = read(inotifyFd, addr events, MaxWatches); n) > 0:  # blocks until any events have been read
     for e in inotify_events(addr events, n):
       echo (e[].wd, e[].mask, cast[cstring](addr e[].name))    # echo watch id, mask, and name value of each event
Source   Edit