inotify.nim 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Get the platform-dependent flags.
  10. # Structure describing an inotify event.
  11. type
  12. InotifyEvent* {.pure, final, importc: "struct inotify_event",
  13. header: "<sys/inotify.h>".} = object ## An Inotify event.
  14. wd* {.importc: "wd".}: FileHandle ## Watch descriptor.
  15. mask* {.importc: "mask".}: uint32 ## Watch mask.
  16. cookie* {.importc: "cookie".}: uint32 ## Cookie to synchronize two events.
  17. len* {.importc: "len".}: uint32 ## Length (including NULs) of name.
  18. name* {.importc: "name".}: char ## Name.
  19. # Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH.
  20. const
  21. IN_ACCESS* = 0x00000001 ## File was accessed.
  22. IN_MODIFY* = 0x00000002 ## File was modified.
  23. IN_ATTRIB* = 0x00000004 ## Metadata changed.
  24. IN_CLOSE_WRITE* = 0x00000008 ## Writtable file was closed.
  25. IN_CLOSE_NOWRITE* = 0x00000010 ## Unwrittable file closed.
  26. IN_CLOSE* = (IN_CLOSE_WRITE or IN_CLOSE_NOWRITE) ## Close.
  27. IN_OPEN* = 0x00000020 ## File was opened.
  28. IN_MOVED_FROM* = 0x00000040 ## File was moved from X.
  29. IN_MOVED_TO* = 0x00000080 ## File was moved to Y.
  30. IN_MOVE* = (IN_MOVED_FROM or IN_MOVED_TO) ## Moves.
  31. IN_CREATE* = 0x00000100 ## Subfile was created.
  32. IN_DELETE* = 0x00000200 ## Subfile was deleted.
  33. IN_DELETE_SELF* = 0x00000400 ## Self was deleted.
  34. IN_MOVE_SELF* = 0x00000800 ## Self was moved.
  35. # Events sent by the kernel.
  36. const
  37. IN_UNMOUNT* = 0x00002000 ## Backing fs was unmounted.
  38. IN_Q_OVERFLOW* = 0x00004000 ## Event queued overflowed.
  39. IN_IGNORED* = 0x00008000 ## File was ignored.
  40. # Special flags.
  41. const
  42. IN_ONLYDIR* = 0x01000000 ## Only watch the path if it is a directory.
  43. IN_DONT_FOLLOW* = 0x02000000 ## Do not follow a sym link.
  44. IN_EXCL_UNLINK* = 0x04000000 ## Exclude events on unlinked objects.
  45. IN_MASK_ADD* = 0x20000000 ## Add to the mask of an already existing watch.
  46. IN_ISDIR* = 0x40000000 ## Event occurred against dir.
  47. IN_ONESHOT* = 0x80000000 ## Only send event once.
  48. # All events which a program can wait on.
  49. const
  50. IN_ALL_EVENTS* = (IN_ACCESS or IN_MODIFY or IN_ATTRIB or IN_CLOSE_WRITE or
  51. IN_CLOSE_NOWRITE or IN_OPEN or IN_MOVED_FROM or IN_MOVED_TO or
  52. IN_CREATE or IN_DELETE or IN_DELETE_SELF or IN_MOVE_SELF)
  53. proc inotify_init*(): FileHandle {.cdecl, importc: "inotify_init",
  54. header: "<sys/inotify.h>".}
  55. ## Create and initialize inotify instance.
  56. proc inotify_init1*(flags: cint): FileHandle {.cdecl, importc: "inotify_init1",
  57. header: "<sys/inotify.h>".}
  58. ## Create and initialize inotify instance.
  59. proc inotify_add_watch*(fd: cint; name: cstring; mask: uint32): cint {.cdecl,
  60. importc: "inotify_add_watch", header: "<sys/inotify.h>".}
  61. ## Add watch of object NAME to inotify instance FD. Notify about events specified by MASK.
  62. proc inotify_rm_watch*(fd: cint; wd: cint): cint {.cdecl,
  63. importc: "inotify_rm_watch", header: "<sys/inotify.h>".}
  64. ## Remove the watch specified by WD from the inotify instance FD.
  65. iterator inotify_events*(evs: pointer, n: int): ptr InotifyEvent =
  66. ## Abstract the packed buffer interface to yield event object pointers.
  67. ##
  68. ## .. code-block:: Nim
  69. ## var evs = newSeq[byte](8192) # Already did inotify_init+add_watch
  70. ## while (let n = read(fd, evs[0].addr, 8192); n) > 0: # read forever
  71. ## for e in inotify_events(evs[0].addr, n): echo e[].len # echo name lens
  72. var ev: ptr InotifyEvent = cast[ptr InotifyEvent](evs)
  73. var n = n
  74. while n > 0:
  75. yield ev
  76. let sz = InotifyEvent.sizeof + int(ev[].len)
  77. n -= sz
  78. ev = cast[ptr InotifyEvent](cast[uint](ev) + uint(sz))
  79. runnableExamples:
  80. when defined(linux):
  81. let inoty: FileHandle = inotify_init() ## Create 1 Inotify.
  82. doAssert inoty >= 0 ## Check for errors (FileHandle is alias to cint).
  83. let watchdoge: cint = inotify_add_watch(inoty, ".", IN_ALL_EVENTS) ## Add directory to watchdog.
  84. doAssert watchdoge >= 0 ## Check for errors.
  85. doAssert inotify_rm_watch(inoty, watchdoge) >= 0 ## Remove directory from the watchdog