inotify.nim 4.6 KB

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