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))
16 (defmethod print-object ((monitor file-monitor
) stream
)
17 (print-unreadable-object (monitor stream
:type nil
:identity nil
)
18 (format stream
"File monitor for ~S" (file-of monitor
))))
20 (defun monitor-oldp (monitor)
21 (declare (type file-monitor monitor
))
22 (let ((mtime (file-write-date (file-of monitor
))))
23 (values (< (timestamp-of monitor
) mtime
)
26 (defgeneric update-monitor
(monitor)
27 (:method
((monitor file-monitor
))
28 (bt:with-lock-held
((lock-of monitor
))
29 (multiple-value-bind (oldp mtime
) (monitor-oldp monitor
)
31 (funcall (update-fn-of monitor
) (file-of monitor
))
33 (values (timestamp-of monitor
) mtime
)
34 (setf (timestamp-of monitor
) mtime
)))))))