Skip to content
Browse files

API incompatible change:

* Remove read-event function.
* Replace it with read-events, which reads several events at once.

Based on a patch by Francisco Vides Fernández.
  • Loading branch information...
1 parent 66d248d commit 97fe5b7b610895bc857714cf334ec6ed24593ec5 @stassats committed Dec 6, 2010
Showing with 45 additions and 27 deletions.
  1. +6 −6 README
  2. +4 −1 grovel.lisp
  3. +34 −19 inotify.lisp
  4. +1 −1 packages.lisp
View
12 README
@@ -1,16 +1,16 @@
Example:
(with-inotify (inot `((,(user-homedir-pathname) ,in-create)))
- (read-event inot))
+ (read-events inot))
blocks
$ touch ~/file
=>
- #S(EVENT
- :WATCH #<WATCH pathname: "/home/stas/" mask: 256>
- :MASK 256
- :COOKIE 0
- :NAME "file")
+(#S(EVENT
+ :WATCH #<WATCH pathname: #P"/home/stas/" mask: 256>
+ :MASK 256
+ :COOKIE 0
+ :NAME "file"))
View
5 grovel.lisp
@@ -37,5 +37,8 @@
(constant (in-oneshot "IN_ONESHOT") :documentation "Only send event once.")
(constant (in-all-events "IN_ALL_EVENTS"))
-(define "EVENT_SIZE" "(sizeof(struct inotify_event) + NAME_MAX + 1)")
+(define "EVENT_SIZE" "(sizeof(struct inotify_event))")
(constant (+event-size+ "EVENT_SIZE"))
+
+(define "EVENT_MAX_SIZE" "(EVENT_SIZE + NAME_MAX + 1)")
+(constant (+event-max-size+ "EVENT_MAX_SIZE"))
View
53 inotify.lisp
@@ -28,13 +28,16 @@
(defstruct (inotify (:constructor %make-inotify))
fd
+ buffer-size
buffer
watches)
-(defun make-inotify ()
- (%make-inotify
- :fd (inotify-init)
- :buffer (foreign-alloc :char :count +event-size+)))
+(defun make-inotify (&optional (buffer-count 100))
+ (let ((buffer-size (* +event-max-size+ buffer-count)))
+ (%make-inotify
+ :fd (inotify-init)
+ :buffer-size buffer-size
+ :buffer (foreign-alloc :char :count buffer-size))))
(defun close-inotify (inotify)
(isys:close (inotify-fd inotify))
@@ -79,21 +82,33 @@
(watch-pathname (event-watch event)))
(watch-pathname (event-watch event))))
-(defun read-event (inotify)
- (let ((buffer (inotify-buffer inotify)))
- (isys:repeat-upon-eintr
- (isys:read (inotify-fd inotify) buffer +event-size+))
- (with-foreign-slots ((watch mask cookie name-length)
- buffer inotify-event)
- (let ((event (make-event :watch (find-watch inotify watch)
- :mask mask
- :cookie cookie)))
- (unless (zerop name-length)
- (setf (event-name event)
- (foreign-string-to-lisp
- (foreign-slot-pointer buffer 'inotify-event 'name)
- :max-chars name-length)))
- event))))
+(defun read-event (inotify buffer)
+ (with-foreign-slots ((watch mask cookie name-length)
+ buffer inotify-event)
+ (let ((event (make-event :watch (find-watch inotify watch)
+ :mask mask
+ :cookie cookie)))
+ (unless (zerop name-length)
+ (setf (event-name event)
+ (foreign-string-to-lisp
+ (foreign-slot-pointer buffer 'inotify-event 'name)
+ :max-chars name-length)))
+ (values event
+ (+ +event-size+ name-length)))))
+
+(defun read-events (inotify)
+ (let* ((buffer (inotify-buffer inotify))
+ (bytes-read
+ (isys:repeat-upon-eintr
+ (isys:read (inotify-fd inotify)
+ buffer
+ (inotify-buffer-size inotify)))))
+ (loop with event and event-length
+ for offset = 0 then (+ offset event-length)
+ while (< offset bytes-read)
+ do (setf (values event event-length)
+ (read-event inotify buffer))
+ collect event)))
(defun make-inotify-with-watches (path-with-masks)
(let ((inotify (make-inotify)))
View
2 packages.lisp
@@ -18,7 +18,7 @@
#:inotify
#:inotify-watches
- #:read-event
+ #:read-events
#:event
#:event-name
#:event-mask

0 comments on commit 97fe5b7

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