Skip to content
Browse files

Print masks using their names, not integers.

  • Loading branch information...
1 parent 543734d commit ff464d48b3e1944197f922eccaae57b0708e9a98 @stassats committed Oct 13, 2013
Showing with 53 additions and 11 deletions.
  1. +24 −7 README
  2. +27 −3 inotify.lisp
  3. +2 −1 packages.lisp
View
31 README
@@ -7,26 +7,43 @@ blocks
=>
(#S(EVENT
- :WATCH #<WATCH pathname: #P"/home/stas/" mask: 256>
- :MASK 256
+ :WATCH #<WATCH pathname: #P"/home/stas/" mask: (IN-CREATE)>
+ :MASK (IN-CREATE)
:COOKIE 0
:NAME "file"))
+
+The masks are only printed using names, the actual values are integers.
+
+An easy way to check for multiple events:
+(member (logior in-ignored in-delete-self in-move-self)
+ events
+ :key #'event-mask
+ :test #'logtest)
With a timeout:
-(inotify:with-inotify (inotify `(("/tmp/test" ,inotify:in-modify)))
- (loop for events = (inotify:read-events inotify :time-out 1)
+(with-inotify (inotify `(("/tmp/test" ,(logior in-modify
+ in-close))))
+ (loop for events = (read-events inotify :time-out 1)
thereis events
do (write-line "timed out")))
timed out
timed out
-timed out
$ echo 10 >> /tmp/test
=>
(#S(INOTIFY:EVENT
- :WATCH #<INOTIFY:WATCH pathname: #P"/tmp/test" mask: 2>
- :MASK 2
+ :WATCH #<INOTIFY:WATCH
+ pathname: #P"/tmp/test" mask: (IN-CLOSE-NOWRITE IN-CLOSE-WRITE
+ IN-MODIFY)>
+ :MASK (INOTIFY:IN-MODIFY)
+ :COOKIE 0
+ :NAME NIL)
+ #S(INOTIFY:EVENT
+ :WATCH #<INOTIFY:WATCH
+ pathname: #P"/tmp/test" mask: (IN-CLOSE-NOWRITE IN-CLOSE-WRITE
+ IN-MODIFY)>
+ :MASK (INOTIFY:IN-CLOSE-WRITE)
:COOKIE 0
:NAME NIL))
View
30 inotify.lisp
@@ -47,6 +47,21 @@
(unwind-protect (isys:close (inotify-fd inotify))
(setf (inotify-fd inotify) nil))))
+(defvar *masks*
+ (loop for name in
+ '(in-access in-attrib in-close-nowrite in-close-write in-create
+ in-delete in-delete-self in-dont-follow in-ignored in-isdir
+ in-mask-add in-modify in-move-self in-moved-from in-moved-to
+ in-oneshot in-onlydir in-open in-q-overflow in-unmount)
+ collect (cons (symbol-value name) name)))
+
+(defun mask-to-names (mask)
+ (if (= mask in-all-events)
+ '(in-all-events)
+ (loop for (value . name) in *masks*
+ when (logtest value mask)
+ collect name)))
+
(defstruct watch
id
inotify
@@ -57,7 +72,7 @@
(print-unreadable-object (watch stream :type t)
(format stream "pathname: ~s mask: ~a"
(watch-pathname watch)
- (watch-mask watch))))
+ (mask-to-names (watch-mask watch)))))
(defun add-watch (inotify pathname mask)
(let* ((pathname (namestring pathname))
@@ -105,6 +120,12 @@
cookie
name)
+(defmethod print-object ((event event) stream)
+ (let ((copy (copy-event event)))
+ (setf (event-mask copy)
+ (mask-to-names (event-mask copy)))
+ (call-next-method copy stream)))
+
(defun parse-event-name (event)
(let* ((name (event-name event))
(dot (position #\. name :from-end t)))
@@ -150,8 +171,11 @@
(defun make-inotify-with-watches (paths-with-masks)
(let ((inotify (make-inotify)))
- (loop for (path mask) in paths-with-masks
- do (add-watch inotify path mask))
+ (loop for path-and-mask in paths-with-masks
+ ;; LOOP destructuring doesn't check the number of destructured arguments
+ do
+ (destructuring-bind (path mask) path-and-mask
+ (add-watch inotify path mask)))
inotify))
(defmacro with-inotify ((name paths-with-masks) &body body)
View
3 packages.lisp
@@ -52,4 +52,5 @@
#:in-isdir
#:in-oneshot
#:in-all-events
- #:find-watch))
+ #:find-watch
+ #:mask-to-names))

0 comments on commit ff464d4

Please sign in to comment.
Something went wrong with that request. Please try again.