1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; file-monitor.lisp --- Monitor files on disk.
6 (in-package :net.sockets
)
8 (defclass file-monitor
()
9 ((file :initform
(error "Must supply a file name")
10 :initarg
:file
:accessor file-of
)
11 (timestamp :initarg
:timestamp
:accessor timestamp-of
)
12 (update-fn :initarg
:update-fn
:accessor update-fn-of
)
13 (lock :initarg
:lock
:accessor lock-of
))
14 (:default-initargs
:timestamp
0
15 :lock
(bt:make-lock
)))
17 (defmethod print-object ((monitor file-monitor
) stream
)
18 (print-unreadable-object (monitor stream
:type nil
:identity nil
)
19 (format stream
"File monitor for ~S" (file-of monitor
))))
21 (defun monitor-oldp (monitor)
22 (declare (type file-monitor monitor
))
23 (let ((mtime (file-write-date (file-of monitor
))))
24 (values (< (timestamp-of monitor
) mtime
)
27 (defgeneric update-monitor
(monitor)
28 (:method
((monitor file-monitor
))
29 (bt:with-lock-held
((lock-of monitor
))
30 (multiple-value-bind (oldp mtime
) (monitor-oldp monitor
)
32 (funcall (update-fn-of monitor
) (file-of monitor
))
34 (values (timestamp-of monitor
) mtime
)
35 (setf (timestamp-of monitor
) mtime
)))))))