Mark ENOLINK and EMULTIHOP as optional
[iolib.git] / src / sockets / namedb / file-monitor.lisp
blob8ed4ac9c5db2c8949b50a3f3ffb7284e73b05025
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; file-monitor.lisp --- Monitor files on disk.
4 ;;;
6 (in-package :iolib/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 initialize-instance :after ((monitor file-monitor) &key file)
17 (unless (slot-boundp monitor 'lock)
18 (setf (lock-of monitor)
19 (bt:make-lock (format nil "Lock for monitor of ~S" file)))))
21 (defmethod print-object ((monitor file-monitor) stream)
22 (print-unreadable-object (monitor stream :type nil :identity nil)
23 (format stream "File monitor for ~S" (file-of monitor))))
25 (defun monitor-oldp (monitor)
26 (declare (type file-monitor monitor))
27 (let ((mtime (file-write-date (file-of monitor))))
28 (values (< (timestamp-of monitor) mtime)
29 mtime)))
31 (defgeneric update-monitor (monitor)
32 (:method ((monitor file-monitor))
33 (bt:with-lock-held ((lock-of monitor))
34 (multiple-value-bind (oldp mtime) (monitor-oldp monitor)
35 (when oldp
36 (funcall (update-fn-of monitor) (file-of monitor))
37 (multiple-value-prog1
38 (values (timestamp-of monitor) mtime)
39 (setf (timestamp-of monitor) mtime)))))))