chiark / gitweb /
dep.lisp: Report out-of-date deps as being `stale'.
[lisp] / safely.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Safely modify collections of files
4 ;;;
5 ;;; (c) 2005 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software Foundation,
22 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 (defpackage #:safely
25   (:use #:common-lisp #:mdw.base))
26 (in-package #:safely)
27
28 #+(or cmu sbcl)
29 (eval-when (:compile-toplevel :execute)
30   (import #+cmu '(ext:unix-namestring unix:unix-link)
31           #+sbcl '(sb-ext:native-namestring)))
32 #+cmu
33 (progn
34   (declaim (inline unix-namestring))
35   (defun native-namestring (pathname &key as-file)
36     (declare (ignore as-file))
37     (unix-namestring pathname nil)))
38
39 (export '(safely safelyp make-safely))
40 (defstruct (safely (:predicate safelyp) (:constructor make-safely ()))
41   "Stores information about how to commit or undo safe writes."
42   (streams nil)
43   (trail nil))
44
45 (export 'safely-close)
46 (defun safely-close (safe stream)
47   "Make sure that STREAM is closed when SAFE is finished."
48   (push stream (safely-streams safe)))
49
50 (export 'safely-delete-file)
51 (defun safely-delete-file (safe file)
52   "Delete FILE when SAFE is committed."
53   (push `(:delete ,file) (safely-trail safe)))
54
55 (defun generate-fresh-file-name (base tag &optional func)
56   "Return a fresh file name constructed from BASE (a filespec) and TAG (some
57    short descriptive string).  The generated name has the same directory and
58    type as the BASE name, but a different name.
59
60    If FUNC is non-nil, then it is a function to call on the generated file
61    name: generate-fresh-file-name runs in a loop, calling FUNC with generated
62    file names until FUNC returns non-nil, at which point generate-fresh-
63    file-name returns two values: the generated name, and the result of FUNC.
64    generate-fresh-file-name catches errors of type file-error from FUNC, and
65    just tries again with a new name.
66
67    If FUNC is nil, it's treated the same as a function which always returns
68    t.
69
70    This is inspired by a similar facility in scsh."
71   (let ((base (pathname base)))
72     (dotimes (i 256
73               (error "Gave up trying to find a temporary ~A file for ~S."
74                      tag base))
75       (let* ((new (merge-pathnames
76                    (make-pathname
77                     :name (format nil "~A-~A-~X"
78                                   (pathname-name base)
79                                   tag
80                                   (random most-positive-fixnum)))
81                    base))
82              (ret (and (not (probe-file new))
83                        (if func
84                            (handler-case (funcall func new)
85                              (file-error (cond)
86                                (unless (pathname-match-p
87                                         (file-error-pathname cond)
88                                         new)
89                                  (error cond))
90                                nil))
91                            t))))
92         (when ret
93           (return (values new ret)))))))
94
95 (export 'safely-open-output-stream)
96 (defun safely-open-output-stream (safe file &rest open-args)
97   "Create an output stream which will be named FILE when SAFE is committed.
98    Other OPEN-ARGS are passed to open."
99   (multiple-value-bind
100       (name stream)
101       (generate-fresh-file-name file "new"
102                                 (lambda (name)
103                                   (apply #'open name
104                                          :direction :output
105                                          :if-exists nil
106                                          open-args)))
107     (safely-close safe stream)
108     (push `(:shunt ,name ,file)
109           (safely-trail safe))
110     stream))
111
112 #+clisp
113 (progn
114   (ffi:def-call-out %rename (:name "rename")
115     (:language :stdc)
116     (:library "libc.so.6")
117     (:arguments (from ffi:c-string)
118                 (to ffi:c-string))
119     (:return-type ffi:int)))
120
121 (declaim (inline rename))
122 (defun rename (old new)
123   #-clisp
124   (let ((target (make-pathname :directory '(:relative)
125                                :defaults new)))
126     (rename-file old target))
127
128   #+clisp
129   (let ((rc (%rename (namestring old) (namestring new))))
130     (when (= rc -1)
131       (error "Failed to rename ~S to ~S: ~A" old new (posix:strerror)))))
132
133 (defun delete-file-without-moaning (file)
134   "Delete the FILE, ignoring errors."
135   (handler-case (delete-file file)
136     (file-error () nil)))
137
138 (defun rename-file-without-moaning (old new)
139   "Rename OLD to NEW, ignoring errors, and without doing any stupid name
140    mangling."
141   (handler-case (rename old new)
142     (file-error () nil)))
143
144 (defun safely-unwind (trail)
145   "Roll back the TRAIL of operations."
146   (dolist (job trail)
147     (ecase (car job)
148       (:shunt (destructuring-bind (new file) (cdr job)
149                 (declare (ignore file))
150                 (delete-file-without-moaning new)))
151       (:delete)
152       (:rmtmp (destructuring-bind (file) (cdr job)
153                 (delete-file-without-moaning file)))
154       (:revert (destructuring-bind (old new) (cdr job)
155                  (rename-file-without-moaning old new))))))
156
157 (defun safely-reset (safe)
158   "Reset SAFE to its initial state."
159   (setf (safely-streams safe) nil)
160   (setf (safely-trail safe) nil))
161
162 (export 'safely-bail)
163 (defun safely-bail (safe)
164   "Abort the operations in SAFE, unwinding all the things that have been
165    done.  Streams are closed, new files are removed."
166   (dolist (stream (safely-streams safe))
167     (close stream :abort t))
168   (safely-unwind (safely-trail safe))
169   (safely-reset safe))
170
171 #+sbcl
172 (defun unix-link (from to)
173   (sb-unix::int-syscall ("link" sb-alien:c-string sb-alien:c-string)
174                         from to))
175
176 (defun safe-copy (file tag)
177   "Make a copy of the FILE.  Return the new name."
178
179   #+(or cmu sbcl)
180   ;; Use link(2) where available.
181   (generate-fresh-file-name file tag
182                             (lambda (name)
183                               (let ((from (native-namestring file
184                                                              :as-file t))
185                                     (to (native-namestring name
186                                                            :as-file t)))
187                                 (and from to
188                                      (unix-link from to)))))
189
190   #+clisp
191   (generate-fresh-file-name file tag
192                             (lambda (name)
193                               (posix:copy-file (namestring file)
194                                                (namestring name)
195                                                :method :hardlink
196                                                :if-exists nil)))
197
198   #-(or cmu sbcl clisp)
199   ;; Otherwise just copy the file contents and hope for the best.
200   (with-open-file (input file :element-type :default)
201     (multiple-value-bind
202         (copy output)
203         (generate-fresh-file-name file tag
204                                   (lambda (name)
205                                     (open name
206                                           :direction :output
207                                           :if-exists nil
208                                           :element-type :default)))
209       (unwind-protect
210            (progn
211              (let ((buffer (make-array 8192
212                                        :element-type (stream-element-type
213                                                       input))))
214                (loop
215                  (let ((read (read-sequence buffer input)))
216                    (when (plusp read)
217                      (write-sequence buffer output :end read))
218                    (when (< read (length buffer))
219                      (return copy))))))
220         (close output)))))
221
222 (export 'safely-commit)
223 (defun safely-commit (safe)
224   "Commit SAFE.  The files deleted by safely-delete-file are deleted; the
225    files created by safely-open-output-stream are renamed over the old
226    versions, if any.  If a problem occurs during this stage, everything is
227    rewound and no changes are made."
228   (let ((trail (safely-trail safe))
229         (revert nil)
230         (cleanup nil))
231     (unwind-protect
232         (progn
233           (dolist (stream (safely-streams safe))
234             (close stream))
235           (loop
236             (unless trail
237               (return))
238             (let ((job (pop trail)))
239               (ecase (car job)
240                 (:shunt (destructuring-bind (tag new file) job
241                           (declare (ignore tag))
242                           (push `(:rmtmp ,new) revert)
243                           (if (probe-file file)
244                               (let ((old (safe-copy file "old")))
245                                 (push `(:rmtmp ,old) cleanup)
246                                 (push `(:revert ,old ,file) revert))
247                               (push `(:rmtmp ,file) revert))
248                           (rename new file)))
249                 (:delete (destructuring-bind (tag file) job
250                            (declare (ignore tag))
251                            (let ((old (safe-copy file "delete")))
252                              (push `(:revert ,old ,file) revert)
253                              (push `(:rmtmp ,old) cleanup)
254                              (delete-file file)))))))
255           (setf revert nil))
256       (safely-unwind trail)
257       (safely-unwind revert)
258       (safely-unwind cleanup)
259       (safely-reset safe))))
260
261 ;; The symbol `safely' is already exported.
262 (defmacro safely ((safe &key) &body body)
263   "Do stuff within the BODY safely.  If BODY completes without errors, the
264    SAFE is committed; otherwise it's bailed."
265   `(let ((,safe (make-safely)))
266      (unwind-protect
267          (progn
268            ,@body
269            (safely-commit ,safe)
270            (setf ,safe nil))
271        (when ,safe
272          (safely-bail ,safe)))))
273
274 (export 'safely-writing)
275 (defmacro safely-writing ((stream file &rest open-args) &body body)
276   "Simple macro for writing a single file safely.  STREAM is opened onto a
277    temporary file, and if BODY completes, it is renamed to FILE."
278   (with-gensyms safe
279     `(safely (,safe)
280        (let ((,stream (safely-open-output-stream ,safe ,file ,@open-args)))
281          ,@body))))
282
283 ;;;----- That's all, folks --------------------------------------------------