Fixed license header, added ASDF fields.
[cl-inotify.git] / README.md
1 CL-INOTIFY - Interface to the Linux inotify API.
2
3 Copyright (C) 2011 Olof-Joachim Frahm
4 Released under a Simplified BSD license.
5
6 Working, but unfinished.
7 Implementations currently running on: SBCL.
8
9 Uses CFFI, binary-types (from [my Github][1] or see [CLiki][2]) and
10 trivial-utf-8.  Doesn't use iolib, because we don't need most of the
11 functionality, although it might gain us some implementation
12 independence (patches which can be conditionally compiled are welcome).
13
14 A similar package is at [stassats Github][3].
15
16
17 # HOWTO
18
19 After loading the library use `MAKE-INOTIFY` to create a new event
20 queue.  The `NONBLOCKING` argument currently determines if we use the
21 standard `CL:LISTEN` function or `SB-UNIX:UNIX-READ` to check for
22 available events.
23
24 The result of `MAKE-INOTIFY` is used with `WATCH` and `UNWATCH`, the first
25 being used to watch a file or directory, the second to stop watching
26 it.  The `FLAGS` parameter of `WATCH` is described in the notify(7)
27 manpage; you can use a combination of the flags (as keywords) to create
28 a suitable bitmask.  The types `INOTIFY-ADD/READ-FLAG`,
29 `INOTIFY-READ-FLAG` and `INOTIFY-ADD-FLAG` are also defined and can be
30 examined.
31
32 For example, to watch for modified or closed files in a directory, call
33 `(WATCH inotify "foo/" '(:modify :close))`.
34
35 The result of `WATCH` is a handle (currently a `FIXNUM`, but I wouldn't
36 rely on that) which can be fed to `UNWATCH` and can be translated from
37 events with `EVENT-PATHNAME/FLAGS`.
38
39 To finally get the events from the queue, use `READ-EVENT` (which
40 blocks) or `NEXT-EVENT` (which doesn't block).  `EVENT-AVAILABLEP` does
41 what it should do, `NEXT-EVENTS` retrieves all currently available
42 events as a list and `DO-EVENTS` (nonblocking) iterates over available
43 events.
44
45 The enhanced API registers all watched paths in a hashtable, so you can
46 use `PATHNAME-HANDLE/FLAGS` to check if a pathname (exact match) is
47 being watched and `LIST-WATCHED` to return all watched paths as a list.
48 `EVENT-PATHNAME/FLAGS` may be used to get the pathname and flags for a
49 read event.
50
51 `UNWATCH` has to be called with the path or the handle of the watched
52 file or directory (a path will be looked up in the same table as with
53 `PATHNAME-HANDLE/FLAGS`). 
54
55
56 The raw API, which doesn't register watched paths, consists of
57 `READ-RAW-EVENT-FROM-STREAM`, `READ-EVENT-FROM-STREAM`, `WATCH-RAW` and
58 `UNWATCH-RAW`.  They are just a thin wrapper around the C functions, but
59 they're exported in case someone doesn't like the upper layers.
60
61
62 In case you want to use `epoll` or `select` on the event queue you can
63 access the file descriptor yourself and then use the normal functions
64 afterwards.  Currently no such functionality is integrated here.
65
66
67 # EXAMPLE
68
69     > (use-package '#:cl-inotify)
70     > (defvar *tmp*)
71     > (setf *tmp* (make-notify))
72     > (watch *tmp* "/var/tmp/" :all-events)
73     > (next-events *tmp*)
74     > (close-inotify *tmp*)
75
76
77 # TODO
78
79 - more functionality to examine read events
80 - extend to other APIs?
81 - make things more implementation independent
82 - (maybe) don't use the libc for this, direct syscall
83 - (maybe) add iolib replacement for io functions
84 - easier interface for (e)poll/select maybe using iolib (done, using
85   CL:LISTEN and/or SB-UNIX:UNIX-READ)
86
87
88 LINKS
89
90 [1]: https://github.com/Ferada/binary-types
91 [2]: http://www.cliki.net/Binary-types
92 [3]: https://github.com/stassats/inotify