1 ;;; -*- mode: lisp; syntax: common-lisp; coding: utf-8; package: cl-inotify; -*-
3 ;; Copyright (c) 2011-12, Olof-Joachim Frahm
4 ;; All rights reserved.
6 ;; Redistribution and use in source and binary forms, with or without
7 ;; modification, are permitted provided that the following conditions
10 ;; 1. Redistributions of source code must retain the above copyright
11 ;; notice, this list of conditions and the following disclaimer.
13 ;; 2. Redistributions in binary form must reproduce the above copyright
14 ;; notice, this list of conditions and the following disclaimer in the
15 ;; documentation and/or other materials provided with the distribution.
17 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package #:cl-inotify)
31 (defbitfield (inotify-flag :uint32)
35 (:close-write #.in-close-write)
36 (:close-nowrite #.in-close-nowrite)
39 (:moved-from #.in-moved-from)
40 (:moved-to #.in-moved-to)
44 (:delete-self #.in-delete-self)
45 (:move-self #.in-move-self)
46 (:unmount #.in-unmount)
47 (:q-overflow #.in-q-overflow)
48 (:ignored #.in-ignored)
49 (:onlydir #.in-onlydir)
50 (:dont-follow #.in-dont-follow)
51 (:mask-add #.in-mask-add)
53 (:oneshot #.in-oneshot)
54 (:all-events #.in-all-events))
56 (deftype inotify-add/read-flag ()
57 "Shared valid flags for the WATCH-RAW and READ-EVENT functions."
60 :close-write :close-nowrite :close
61 :create :delete :delete-self
63 :move-self :moved-from :moved-to :move
66 (deftype inotify-read-flag ()
67 "Valid flags which are returned from READ-EVENT."
68 '(or inotify-add/read-flag
69 (member :ignored :isdir :q-overflow :unmount)))
71 (deftype inotify-add-flag ()
72 "Valid flags for the WATCH-RAW function."
73 '(or inotify-add/read-flag
74 (member :dont-follow :mask-add :oneshot :onlydir)))
76 (defun valid-watch-flag-p (x)
77 (and (typep x 'inotify-add-flag)
78 (not (eq :mask-add x))
79 (not (eq :oneshot x))))
81 (defun valid-watch-flag-list-p (list)
82 (every #'valid-watch-flag-p list))
84 (deftype watch-flag-list ()
85 "Valid flags argument for the WATCH function, a list of keywords from
86 INOTIFY-ADD-FLAG. Basically only :MASK-ADD and :ONESHOT are removed.
87 The :MASK-ADD behaviour is replicated with the REPLACE-P argument; the
88 :ONESHOT behaviour doesn't play well with the WATCH function design (and
89 thus should be used only with WATCH-RAW)."
90 '(or (satisfies valid-watch-flag-p)
91 (and list (satisfies valid-watch-flag-list-p))))
93 (defcfun ("inotify_init" c-inotify-init) :int
94 "Initialises a new inotify event queue.")
96 (defcfun ("inotify_add_watch" c-inotify-add-watch) :int
97 "Watches a path on a event queue."
102 (defcfun ("inotify_rm_watch" c-inotify-rm-watch) :int
103 "Removes a watched path from a event queue."
107 (binary-types:define-signed int #.(cffi:foreign-type-size :int))
109 (binary-types:define-binary-struct inotify-event ()
110 "An inotify native event structure.
111 WD is the watch/file descriptor,
112 MASK is the (parsed) combination of events,
113 COOKIE is a unique integer which connects related events,
114 NAME optionally identifies a file relative to a watched directory."
115 (wd 0 :binary-type int)
116 (mask 0 :binary-type binary-types:u32)
117 (cookie 0 :binary-type binary-types:u32)
120 (defstruct (inotify-instance
121 (:constructor make-inotify-instance ())
122 (:conc-name inotify-))
123 "Contains the stream and file descriptor for a inotify instance."
128 ;;;; initialisation and stuff
130 (eval-when (:compile-toplevel :load-toplevel :execute)
131 (defun read-new-value (&optional (stream *query-io*))
132 "READs a value from the STREAM and returns it (wrapped in a list)."
133 (format stream "~&Enter a new value (unevaluated): ")
134 (force-output stream)
135 (list (read stream))))
137 (eval-when (:compile-toplevel :load-toplevel :execute)
138 (defun init-endian ()
139 "Initialises the endianess for the BINARY-TYPES library. Is automatically
140 called when the library is loaded."
141 (setf binary-types:*endian*
142 (restart-case #+little-endian :little-endian
143 #+big-endian :big-endian
144 #-(or little-endian big-endian) (error "unknown endianess")
146 :report "Enter a correct value (either :LITTLE-ENDIAN or :BIG-ENDIAN)."
147 :interactive read-new-value
148 ;; TODO: better way to test for correct value/retry values?
150 ((:little-endian :big-endian) value)
151 (T (error "wrong value supplied (not :LITTLE-ENDIAN or :BIG-ENDIAN)"))))))))
153 ;; initialise the endianess
154 (eval-when (:compile-toplevel :load-toplevel :execute)
157 ;;;; basic wrapping of the API
159 (defun read-raw-event-from-stream (stream)
160 "Reads a raw event from the inotify stream."
161 (let* ((event (binary-types:read-binary 'inotify-event stream))
162 (len (binary-types:read-binary 'binary-types:u32 stream)))
164 (with-slots (name) event
165 (let ((buffer (make-array len :element-type '(unsigned-byte 8))))
166 (read-sequence buffer stream :end len)
167 (setf name (trivial-utf-8:utf-8-bytes-to-string buffer :end (position 0 buffer))))))
170 (defun read-event-from-stream (stream)
171 "Reads a event from the inotify stream and converts bitmasks on reading."
172 (let ((event (read-raw-event-from-stream stream)))
173 (with-slots (mask) event
174 (setf mask (foreign-bitfield-symbols 'inotify-flag mask)))
177 (defun set-nonblocking (fd nonblocking)
178 "Enables or disables NONBLOCKING mode on a file descriptor FD."
179 (let ((flags (sb-posix:fcntl fd sb-posix:f-getfl)))
180 ;; an error is raised if this fails, so we don't have to do it ourselves
181 (sb-posix:fcntl fd sb-posix:f-setfl
182 (funcall (if nonblocking #'logior #'logxor)
183 flags sb-posix:o-nonblock)))
186 (defun init-unregistered-inotify (inotify &optional (nonblocking T))
187 "Creates a new inotify event queue. If NONBLOCKING is set (default),
188 the file descriptor is set to non-blocking I/O."
189 (let ((result (c-inotify-init)))
190 (when (minusp result)
191 (perror "inotify_init failed"))
192 (with-slots (fd stream (non-block nonblocking)) inotify
194 ;; file descriptor is collected with auto-close
198 (set-nonblocking fd T)
199 (setf non-block nonblocking))
201 (sb-sys:make-fd-stream
204 :element-type '(unsigned-byte 8)
205 :name (format NIL "inotify event queue ~A" fd)
207 :buffering (if nonblocking :none :full))))
208 ;; if stream is constructed, gc'ing it will cleanup the file descriptor
210 (sb-posix:close fd)))))
213 (defun make-unregistered-inotify (&optional (nonblocking T))
214 "Creates a new unregistered INOTIFY instance."
215 (init-unregistered-inotify (make-inotify-instance) nonblocking))
217 (defun close-inotify (inotify)
218 "Closes the inotify event queue."
219 (close (inotify-stream inotify))
222 (defun perror (prefix-string)
223 #+sbcl (sb-int:simple-perror prefix-string)
224 #-(or sbcl) (error prefix-string))
226 (defun ensure-list (arg)
227 (if (listp arg) arg `(,arg)))
229 (defun translate-keyword-flags (flags)
232 (foreign-bitfield-value 'inotify-flag (ensure-list flags)))
235 (defun watch-raw (inotify pathname flags)
236 "Adds PATHNAME (either of type PATHNAME or STRING) to be watched. FLAGS
237 determines how exactly (see inotify(7) for detailed information) and can
238 be of type LIST, KEYWORD or a raw numerical value (which isn't checked
239 for validity though). Returns a handle which can be used with UNWATCH-RAW."
240 (let* ((path (etypecase pathname
242 (pathname (namestring pathname))))
243 (result (c-inotify-add-watch (inotify-fd inotify)
244 path (translate-keyword-flags flags))))
245 (when (minusp result)
246 (perror "inotify_add_watch failed"))
249 (defun unwatch-raw (inotify handle)
250 "Stops watching the path associated with a HANDLE established by WATCH-RAW."
251 (let ((result (c-inotify-rm-watch (inotify-fd inotify) handle)))
252 (when (minusp result)
253 (perror "inotify_rm_watch failed")))
256 ;;;; support functions, making life easier
258 (defstruct (registered-inotify-instance
259 (:include inotify-instance)
260 (:constructor make-registered-inotify-instance ())
261 (:conc-name inotify-))
262 "Additionally to the information in INOTIFY-INSTANCE, records watched
263 paths in a dictionary."
266 (defun make-inotify (&optional (nonblocking T))
267 "Creates a new registered INOTIFY instance. In NONBLOCKING mode, the file
268 descriptor is set to non-blocking mode. The resulting object has to be
269 closed with CLOSE-INOTIFY."
270 (let ((result (make-registered-inotify-instance)))
271 (init-unregistered-inotify result nonblocking)
272 (with-slots (watched) result
273 (setf watched (make-hash-table :test 'equal)))
276 (defun pathname-handle/flags (inotify pathname)
277 "Returns a CONS cell with the values HANDLE and FLAGS if PATHNAME is
278 being watched by INOTIFY, else NIL. The match is exact."
279 (gethash pathname (inotify-watched inotify)))
281 (defun event-pathname/flags (inotify event &optional (handle (slot-value event 'wd)))
282 "Returns two values PATHNAME and FLAGS for an EVENT which were used during
283 registration. If HANDLE is specified EVENT is ignored."
285 (maphash (lambda (pathname entry)
286 (when (eql (car entry) handle)
287 (return (values pathname (cdr entry)))))
288 (inotify-watched inotify))))
290 (defun sane-user-flags (inotify pathname flags &key (replace-p T))
291 (check-type flags watch-flag-list)
292 ;; now, :mask-add can't be member of flags
294 (let* ((flags (ensure-list flags))
295 (rep-flags (if replace-p
296 (cons :mask-add flags)
298 (let ((it (gethash pathname (slot-value inotify 'watched))))
300 (union (cdr it) rep-flags :test #'eq)
303 (defun watch (inotify pathname flags &key (replace-p T))
304 "Adds PATHNAME (either pathname or string) to be watched and records the
305 watched paths. FLAGS (a list of keywords) determines how exactly (see
306 inotify(7) for detailed information). Returns a handle which can be used
307 with UNWATCH and EVENT-PATHNAME/FLAGS. If REPLACE-P is set to T (default),
308 the flags mask is replaced rather than OR-ed to the current mask (if it
309 exists). The :MASK-ADD flag is therefore removed from the FLAGS argument."
310 (let* ((flags (sane-user-flags inotify pathname flags :replace-p replace-p))
311 (handle (watch-raw inotify pathname flags)))
312 (with-slots (watched) inotify
313 (setf (gethash pathname watched) (cons handle flags)))
316 (defun unwatch (inotify &key pathname event handle)
317 "Disables watching the path associated with the supplied HANDLE (which
318 may be one from a given EVENT) or PATHNAME."
319 (unless (or pathname event handle)
320 (error "either PATHNAME, EVENT or HANDLE have to be specified"))
322 (setf handle (slot-value event 'wd)))
324 (unwatch-raw inotify handle)
325 (let ((handle (car (pathname-handle/flags inotify pathname))))
327 (error "PATHNAME ~S isn't being watched" pathname))
328 ;; remove even if unwatch-raw throws an error (which can happen if :oneshot is specified)
329 (remhash pathname (inotify-watched inotify))
330 (unwatch-raw inotify handle)))
333 (defun list-watched (inotify)
334 "Returns a LIST of all watched pathnames in no particular order."
336 for pathname being each hash-key in (inotify-watched inotify)
339 (defun unix-eagain-p (fd-stream)
340 "Returns T on a FD-STREAM if trying to read from the stream raised a
342 (multiple-value-bind (result error)
343 (sb-unix:unix-read (sb-sys:fd-stream-fd fd-stream) NIL 0)
344 (declare (ignore result))
345 (= error sb-unix:eagain)))
347 (defun event-available-p (inotify)
348 "Returns T if an event is available on the queue."
349 (if (inotify-nonblocking inotify)
350 (not (unix-eagain-p (inotify-stream inotify)))
351 (listen (inotify-stream inotify))))
353 (defun read-event (inotify)
354 "Reads an event from the queue. Blocks if no event is available."
355 (read-event-from-stream (inotify-stream inotify)))
357 (defun next-event (inotify)
358 "Reads an event from the queue. Returns NIL if none is available."
359 (when (event-available-p inotify)
360 (read-event inotify)))
362 (defmacro do-events ((var inotify &key blocking-p) &body body)
363 "Loops BODY with VAR bound to the next events retrieved from INOTIFY.
364 The macro uses NEXT-EVENT, so reading an event won't block and the loop
365 terminates if no events are available."
366 (check-type var symbol)
367 (let ((inotify-sym (gensym)))
369 with ,var and ,inotify-sym = ,inotify
371 `(while (event-available-p ,inotify-sym)))
373 (setf ,var (read-event ,inotify-sym))
376 (defun next-events (inotify)
377 "Reads all available events from the queue. Returns a LIST of events."
379 while (event-available-p inotify)
380 collect (read-event inotify)))
382 ;;; this has the longer name, because this way you actually have to read
383 ;;; about the differences, at least i hope so
384 (defmacro with-unregistered-inotify ((inotify &optional (nonblocking T) &rest rest) &body body)
385 "Like WITH-INOTIFY, but uses MAKE-UNREGISTERED-INOTIFY and WATCH-RAW
386 instead. Useful if you need to monitor just a fixed set of paths."
387 `(let* ((,inotify (make-unregistered-inotify ,nonblocking)))
390 ,.(mapcar (lambda (specifier)
391 `(watch-raw ,inotify ,@specifier))
394 (close-inotify ,inotify))))
396 (defmacro with-inotify ((inotify &optional (nonblocking T) &rest rest) &body body)
397 "Executes BODY with a newly created queue bound to INOTIFY if true.
398 See MAKE-INOTIFY for more information about possible arguments.
400 The REST is a list of argument forms for the WATCH function, i.e. one or
401 more forms (PATHNAME FLAGS &KEY (REPLACE-P T)).
403 Since the QUEUE is closed on unwinding, this macro doesn't bother with
404 UNWATCH calls on all WATCHed paths."
405 `(let* ((,inotify (make-inotify ,nonblocking)))
408 ,.(mapcar (lambda (specifier)
409 `(watch ,inotify ,@specifier))
412 (close-inotify ,inotify))))