Skip to content

Commit

Permalink
Add high-level interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
stassats committed May 4, 2010
1 parent 88923ec commit 8247cca
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
4 changes: 2 additions & 2 deletions grovel.lisp
Expand Up @@ -38,8 +38,8 @@
(constant (in-all-events "IN_ALL_EVENTS"))


(ctype :size-t "size_t")
(ctype :ssize-t "ssize_t")
(ctype size-t "size_t")
(ctype ssize-t "ssize_t")

(define "EVENT_SIZE" "(sizeof(struct inotify_event) + NAME_MAX + 1)")
(constant (event-size "EVENT_SIZE"))
92 changes: 87 additions & 5 deletions inotify.lisp
Expand Up @@ -8,10 +8,11 @@
(defcfun "inotify_init" :int)

(defcfun ("close" c-close) :int (fd :int))
(defcfun ("read" c-read) :ssize-t

(defcfun ("read" c-read) ssize-t
(fd :int)
(buffer :pointer)
(count :size-t))
(count size-t))

(defcfun "inotify_add_watch" :int
(fd :int)
Expand All @@ -20,11 +21,92 @@

(defcfun "inotify_rm_watch" :int
(fd :int)
(flags :uint32))
(watch-descritor :uint32))

(defcstruct (inotify-event :size 16)
(watch-descritor :int)
(defcstruct (inotify-event)
(watch :int)
(mask :uint32)
(cookie :uint32)
(name-length :uint32)
(name :char))

(defstruct (inotify (:constructor %make-inotify))
fd
buffer
watches)

(defun make-inotify ()
(%make-inotify
:fd (inotify-init)
:buffer (foreign-alloc :char :count event-size)))

(defun close-inotify (inotify)
(c-close (inotify-fd inotify))
(foreign-free (inotify-buffer inotify) )
(setf (inotify-buffer inotify) nil))

(defstruct watch
id
inotify
pathname
mask)

(defmethod print-object ((watch watch) stream)
(print-unreadable-object (watch stream :type t)
(format stream "pathname: ~a mask: ~a"
(watch-pathname watch)
(watch-mask watch))))

(defun add-watch (inotify pathname mask)
(let* ((pathname (namestring pathname))
(watch (make-watch :inotify inotify
:pathname pathname
:id (inotify-add-watch (inotify-fd inotify)
pathname
mask)
:mask mask)))
(push watch (inotify-watches inotify))
watch))

(defun find-watch (inotify id)
(find id (inotify-watches inotify) :key #'watch-id))

(defstruct event
watch
mask
cookie
name)

(defun event-full-name (event)
(if (event-name event)
(merge-pathnames (event-name event)
(make-pathname :directory (watch-pathname (event-watch event))))
(watch-pathname (event-watch event))))

(defun read-event (inotify)
(let ((buffer (inotify-buffer inotify)))
(c-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 make-inotify-with-watches (path-with-masks)
(let ((inotify (make-inotify)))
(loop for (path mask) in path-with-masks
do (add-watch inotify path mask))
inotify))

(defmacro with-inotify ((name paths-with-masks) &body body)
`(let ((,name (make-inotify-with-watches ,paths-with-masks)))
(unwind-protect (progn ,@body)
(close-inotify ,name))))
23 changes: 22 additions & 1 deletion packages.lisp
Expand Up @@ -2,4 +2,25 @@

(defpackage #:inotify
(:use #:cl #:cffi)
(:export ))
(:export
#:with-inotify

#:make-inotify
#:close-inotify

#:add-watch
#:watch
#:watch
#:watch-pathname
#:watch-inotify
#:watch-mask

#:inotify
#:inotify-watches

#:read-event
#:event
#:event-name
#:event-mask
#:event-watch
#:event-cookie))

0 comments on commit 8247cca

Please sign in to comment.