Use LASTCAR.
[iolib.git] / sockets / namedb / file-monitor.lisp
blob162c85a4037138404d8b53ac7a858d75bdf4efc1
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; file-monitor.lisp --- Monitor files on disk.
4 ;;;
5 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :net.sockets)
26 (defclass file-monitor ()
27 ((file :initform (error "Must supply a file name")
28 :initarg :file :accessor file-of)
29 (timestamp :initarg :timestamp :accessor timestamp-of)
30 (update-fn :initarg :update-fn :accessor update-fn-of)
31 (lock :initarg :lock :accessor lock-of))
32 (:default-initargs :timestamp 0))
34 (defmethod print-object ((monitor file-monitor) stream)
35 (print-unreadable-object (monitor stream :type nil :identity nil)
36 (format stream "File monitor for ~S" (file-of monitor))))
38 (defun monitor-oldp (monitor)
39 (declare (type file-monitor monitor))
40 (let ((mtime (file-write-date (file-of monitor))))
41 (values (< (timestamp-of monitor) mtime)
42 mtime)))
44 (defgeneric update-monitor (monitor)
45 (:method ((monitor file-monitor))
46 (bt:with-lock-held ((lock-of monitor))
47 (multiple-value-bind (oldp mtime) (monitor-oldp monitor)
48 (when oldp
49 (funcall (update-fn-of monitor) (file-of monitor))
50 (multiple-value-prog1
51 (values (timestamp-of monitor) mtime)
52 (setf (timestamp-of monitor) mtime)))))))