From 0706f8cdbe45042a8948e772323fbd55dc9ba24f Mon Sep 17 00:00:00 2001 From: Olof-Joachim Frahm Date: Thu, 9 Jun 2011 14:24:30 +0200 Subject: [PATCH] Added handle lookup. Fixed README, docstrings and exports. --- README | 10 ++++++---- cl-inotify.asd | 2 +- inotify.lisp | 43 +++++++++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/README b/README index 6dee4f1..1fcc423 100644 --- a/README +++ b/README @@ -25,8 +25,8 @@ For example, to watch for modified or closed files in a directory, call (WATCH inotify "foo/" '(:modify :close)). The result of WATCH is a handle (currently a FIXNUM, but I wouldn't rely -on that) which can be fed to UNWATCH, but isn't useful for anything -else. +on that) which can be fed to UNWATCH and can be translated from events +with EVENT-PATHNAME/FLAGS. To finally get the events from the queue, use READ-EVENT (which blocks) or NEXT-EVENT (which doesn't block). EVENT-AVAILABLEP does what it @@ -34,8 +34,10 @@ should do, NEXT-EVENTS retrieves all currently available events as a list and DO-EVENTS (nonblocking) iterates over available events. The enhanced API registers all watched paths in a hashtable, so you can -use WATCHEDP to check if a pathname (exact match) is being watched and -LIST-WATCHED to return all watched paths as a list. +use PATHNAME-HANDLE/FLAGS to check if a pathname (exact match) is being +watched and LIST-WATCHED to return all watched paths as a list. +EVENT-PATHNAME/FLAGS may be used to get the pathname and flags for a +read event. UNWATCH has to be called with the path or the handle of the watched file or directory (a path'll looked up in the same table as with WATCHEDP). diff --git a/cl-inotify.asd b/cl-inotify.asd index e6815a5..9876e16 100644 --- a/cl-inotify.asd +++ b/cl-inotify.asd @@ -30,7 +30,7 @@ (asdf:operate 'asdf:load-op 'cffi-grovel)) (asdf:defsystem :cl-inotify - :depends-on (#:cffi #:binary-types) + :depends-on (#:cffi #:binary-types #:trivial-utf-8) :serial T :components ((:file "package") (cffi-grovel:grovel-file "grovel") diff --git a/inotify.lisp b/inotify.lisp index f0e3ecc..0d9ca13 100644 --- a/inotify.lisp +++ b/inotify.lisp @@ -159,9 +159,9 @@ called when the library is loaded." (len (binary-types:read-binary 'binary-types:u32 stream))) (when (plusp len) (with-slots (name) event - (setf name (binary-types:read-binary-string stream - :size len - :terminators '(0))))) + (let ((buffer (make-array len :element-type '(unsigned-byte 8)))) + (read-sequence buffer stream :end len) + (setf name (trivial-utf-8:utf-8-bytes-to-string buffer :end (position 0 buffer)))))) event)) (defun read-event-from-stream (stream) @@ -270,11 +270,19 @@ closed with CLOSE-INOTIFY." (setf watched (make-hash-table :test 'equal))) result)) -(defun watchedp (inotify pathname) - "Returns two values HANDLE and FLAGS if PATHNAME is being watched by INOTIFY, -else NIL. The match is exact." - (let ((it (gethash pathname (inotify-watched Inotify)))) - (when it (values (car it) (cdr it))))) +(defun pathname-handle/flags (inotify pathname) + "Returns a CONS cell with the values HANDLE and FLAGS if PATHNAME is +being watched by INOTIFY, else NIL. The match is exact." + (gethash pathname (inotify-watched inotify))) + +(defun event-pathname/flags (inotify event &optional (handle (slot-value event 'wd))) + "Returns two values PATHNAME and FLAGS for an EVENT which were used during +registration. If HANDLE is specified EVENT is ignored." + (block NIL + (maphash (lambda (pathname entry) + (when (eql (car entry) handle) + (return (values pathname (cdr entry))))) + (inotify-watched inotify)))) (defun sane-user-flags (inotify pathname flags &key (replace-p T)) (check-type flags watch-flag-list) @@ -293,22 +301,25 @@ else NIL. The match is exact." "Adds PATHNAME (either pathname or string) to be watched and records the watched paths. FLAGS (a list of keywords) determines how exactly (see inotify(7) for detailed information). Returns a handle which can be used -with UNWATCH. If REPLACE-P is set to T (default), the flags mask is -replaced rather than OR-ed to the current mask (if it exists). The -:MASK-ADD flag is therefore removed from the FLAGS argument." +with UNWATCH and EVENT-PATHNAME/FLAGS. If REPLACE-P is set to T (default), +the flags mask is replaced rather than OR-ed to the current mask (if it +exists). The :MASK-ADD flag is therefore removed from the FLAGS argument." (let* ((flags (sane-user-flags inotify pathname flags :replace-p replace-p)) (handle (watch-raw inotify pathname flags))) (with-slots (watched) inotify (setf (gethash pathname watched) (cons handle flags))) handle)) -(defun unwatch (inotify &key pathname handle) - "Disables watching the path associated with the supplied HANDLE or PATHNAME." - (unless (or pathname handle) - (error "either PATHNAME or HANDLE has to be specified")) +(defun unwatch (inotify &key pathname event handle) + "Disables watching the path associated with the supplied HANDLE (which +may be one from a given EVENT) or PATHNAME." + (unless (or pathname event handle) + (error "either PATHNAME, EVENT or HANDLE have to be specified")) + (when event + (setf handle (slot-value event 'wd))) (if handle (unwatch-raw inotify handle) - (let ((handle (watchedp inotify pathname))) + (let ((handle (car (pathname-handle/flags inotify pathname)))) (unless handle (error "PATHNAME ~S isn't being watched" pathname)) ;; remove even if unwatch-raw throws an error (which can happen if :oneshot is specified) -- 1.7.10.4