Add default initarg to :LOCK of FILE-MONITOR class.
[iolib.git] / net.sockets / namedb / file-monitor.lisp
blobcd69fd3ef6283a4000e4c19a79282ca6c214d3a3
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; file-monitor.lisp --- Monitor files on disk.
4 ;;;
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)
25 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)
31 (when oldp
32 (funcall (update-fn-of monitor) (file-of monitor))
33 (multiple-value-prog1
34 (values (timestamp-of monitor) mtime)
35 (setf (timestamp-of monitor) mtime)))))))