inotify.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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>",
  16. completeStruct.} = object ## An Inotify event.
  17. wd* {.importc: "wd".}: FileHandle ## Watch descriptor.
  18. mask* {.importc: "mask".}: uint32 ## Watch mask.
  19. cookie* {.importc: "cookie".}: uint32 ## Cookie to synchronize two events.
  20. len* {.importc: "len".}: uint32 ## Length (including NULs) of name.
  21. name* {.importc: "name".}: UncheckedArray[char] ## Name.
  22. # Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH.
  23. const
  24. IN_ACCESS* = 0x00000001 ## File was accessed.
  25. IN_MODIFY* = 0x00000002 ## File was modified.
  26. IN_ATTRIB* = 0x00000004 ## Metadata changed.
  27. IN_CLOSE_WRITE* = 0x00000008 ## Writtable file was closed.
  28. IN_CLOSE_NOWRITE* = 0x00000010 ## Unwrittable file closed.
  29. IN_CLOSE* = (IN_CLOSE_WRITE or IN_CLOSE_NOWRITE) ## Close.
  30. IN_OPEN* = 0x00000020 ## File was opened.
  31. IN_MOVED_FROM* = 0x00000040 ## File was moved from X.
  32. IN_MOVED_TO* = 0x00000080 ## File was moved to Y.
  33. IN_MOVE* = (IN_MOVED_FROM or IN_MOVED_TO) ## Moves.
  34. IN_CREATE* = 0x00000100 ## Subfile was created.
  35. IN_DELETE* = 0x00000200 ## Subfile was deleted.
  36. IN_DELETE_SELF* = 0x00000400 ## Self was deleted.
  37. IN_MOVE_SELF* = 0x00000800 ## Self was moved.
  38. # Events sent by the kernel.
  39. const
  40. IN_UNMOUNT* = 0x00002000 ## Backing fs was unmounted.
  41. IN_Q_OVERFLOW* = 0x00004000 ## Event queued overflowed.
  42. IN_IGNORED* = 0x00008000 ## File was ignored.
  43. # Special flags.
  44. const
  45. IN_ONLYDIR* = 0x01000000 ## Only watch the path if it is a directory.
  46. IN_DONT_FOLLOW* = 0x02000000 ## Do not follow a sym link.
  47. IN_EXCL_UNLINK* = 0x04000000 ## Exclude events on unlinked objects.
  48. IN_MASK_ADD* = 0x20000000 ## Add to the mask of an already existing watch.
  49. IN_ISDIR* = 0x40000000 ## Event occurred against dir.
  50. IN_ONESHOT* = 0x80000000 ## Only send event once.
  51. # All events which a program can wait on.
  52. const
  53. IN_ALL_EVENTS* = (IN_ACCESS or IN_MODIFY or IN_ATTRIB or IN_CLOSE_WRITE or
  54. IN_CLOSE_NOWRITE or IN_OPEN or IN_MOVED_FROM or IN_MOVED_TO or
  55. IN_CREATE or IN_DELETE or IN_DELETE_SELF or IN_MOVE_SELF)
  56. proc inotify_init*(): FileHandle {.cdecl, importc: "inotify_init",
  57. header: "<sys/inotify.h>".}
  58. ## Create and initialize inotify instance.
  59. proc inotify_init1*(flags: cint): FileHandle {.cdecl, importc: "inotify_init1",
  60. header: "<sys/inotify.h>".}
  61. ## Like `inotify_init<#inotify_init>`_ ,
  62. ## but has a flags argument that provides access to some extra functionality.
  63. proc inotify_add_watch*(fd: cint; name: cstring; mask: uint32): cint {.cdecl,
  64. importc: "inotify_add_watch", header: "<sys/inotify.h>".}
  65. ## Add watch of object NAME to inotify instance FD. Notify about events specified by MASK.
  66. proc inotify_rm_watch*(fd: cint; wd: cint): cint {.cdecl,
  67. importc: "inotify_rm_watch", header: "<sys/inotify.h>".}
  68. ## Remove the watch specified by WD from the inotify instance FD.
  69. iterator inotify_events*(evs: pointer, n: int): ptr InotifyEvent =
  70. ## Abstract the packed buffer interface to yield event object pointers.
  71. runnableExamples("-r:off"):
  72. when defined(linux):
  73. import std/posix # needed for FileHandle read procedure
  74. const MaxWatches = 8192
  75. let inotifyFd = inotify_init() # create new inotify instance and get it's FileHandle
  76. let wd = inotifyFd.inotify_add_watch("/tmp", IN_CREATE or IN_DELETE) # Add new watch
  77. var events: array[MaxWatches, byte] # event buffer
  78. while (let n = read(inotifyFd, addr events, MaxWatches); n) > 0: # blocks until any events have been read
  79. for e in inotify_events(addr events, n):
  80. echo (e[].wd, e[].mask, cast[cstring](addr e[].name)) # echo watch id, mask, and name value of each event
  81. var ev: ptr InotifyEvent = cast[ptr InotifyEvent](evs)
  82. var n = n
  83. while n > 0:
  84. yield ev
  85. let sz = InotifyEvent.sizeof + int(ev[].len)
  86. n -= sz
  87. ev = cast[ptr InotifyEvent](cast[uint](ev) + uint(sz))
  88. runnableExamples:
  89. when defined(linux):
  90. let inotifyFd = inotify_init() # create and get new inotify FileHandle
  91. doAssert inotifyFd >= 0 # check for errors
  92. let wd = inotifyFd.inotify_add_watch("/tmp", IN_CREATE or IN_DELETE) # Add new watch
  93. doAssert wd >= 0 # check for errors
  94. discard inotifyFd.inotify_rm_watch(wd) # remove watch