5 ;;; Safely modify collections of files
7 ;;; (c) 2005 Straylight/Edgeware
10 ;;;----- Licensing notice ---------------------------------------------------
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 (:use #:common-lisp #:mdw.base)
28 (:export #:safely #:safely-close #:safely-delete-file
29 #:safely-open-output-stream #:safely-bail #:safely-commit
33 (defstruct (safely (:predicate safelyp))
34 "Stores information about how to commit or undo safe writes."
38 (defun safely-close (safe stream)
39 "Make sure that STREAM is closed when SAFE is finished."
40 (push stream (safely-streams safe)))
42 (defun safely-delete-file (safe file)
43 "Delete FILE when SAFE is committed."
44 (push `(:delete ,file) (safely-trail safe)))
46 (defun generate-fresh-file-name (base tag &optional func)
47 "Return a fresh file name constructed from BASE (a filespec) and TAG (some
48 short descriptive string). The generated name has the same directory and
49 type as the BASE name, but a different name.
51 If FUNC is non-nil, then it is a function to call on the generated file
52 name: generate-fresh-file-name runs in a loop, calling FUNC with generated
53 file names until FUNC returns non-nil, at which point generate-fresh-
54 file-name returns two values: the generated name, and the result of FUNC.
55 generate-fresh-file-name catches errors of type file-error from FUNC, and
56 just tries again with a new name.
58 If FUNC is nil, it's treated the same as a function which always returns
61 This is inspired by a similar facility in scsh."
62 (let ((base (pathname base)))
64 (error "Gave up trying to find a temporary ~A file for ~S."
66 (let* ((new (merge-pathnames
68 :name (format nil "~A-~A-~X"
71 (random most-positive-fixnum)))
73 (ret (and (not (probe-file new))
75 (handler-case (funcall func new)
77 (unless (pathname-match-p
78 (file-error-pathname cond)
84 (return (values new ret)))))))
86 (defun safely-open-output-stream (safe file &rest open-args)
87 "Create an output stream which will be named FILE when SAFE is committed.
88 Other OPEN-ARGS are passed to open."
91 (generate-fresh-file-name file "new"
97 (safely-close safe stream)
98 (push `(:shunt ,name ,file)
102 (defun delete-file-without-moaning (file)
103 "Delete the FILE, ignoring errors."
104 (handler-case (delete-file file)
105 (file-error () nil)))
107 (defun rename-file-without-moaning (old new)
108 "Rename OLD to NEW, ignoring errors, and without doing any stupid name
110 (handler-case (rename-file old new)
111 (file-error () nil)))
113 (defun safely-unwind (trail)
114 "Roll back the TRAIL of operations."
117 (:shunt (destructuring-bind (new file) (cdr job)
118 (declare (ignore file))
119 (delete-file-without-moaning new)))
121 (:rmtmp (destructuring-bind (file) (cdr job)
122 (delete-file-without-moaning file)))
123 (:revert (destructuring-bind (old new) (cdr job)
124 (rename-file-without-moaning old new))))))
126 (defun safely-reset (safe)
127 "Reset SAFE to its initial state."
128 (setf (safely-streams safe) nil)
129 (setf (safely-trail safe) nil))
131 (defun safely-bail (safe)
132 "Abort the operations in SAFE, unwinding all the things that have been
133 done. Streams are closed, new files are removed."
134 (dolist (stream (safely-streams safe))
135 (close stream :abort t))
136 (safely-unwind (safely-trail safe))
139 (defun safe-copy (file tag)
140 "Make a copy of the FILE. Return the new name."
143 ;; Use link(2) where available.
144 (generate-fresh-file-name file tag
146 (let ((from (ext:unix-namestring file t))
147 (to (ext:unix-namestring name nil)))
149 (unix:unix-link from to)))))
152 ;; Otherwise just copy the file contents and hope for the best.
153 (with-open-file (input file :element-type :default)
156 (generate-fresh-file-name file tag
161 :element-type :default)))
164 (let ((buffer (make-array 8192
165 :element-type (stream-element-type
168 (let ((read (read-sequence buffer input)))
170 (write-sequence buffer output :end read))
171 (when (< read (length buffer))
175 (defun safely-commit (safe)
176 "Commit SAFE. The files deleted by safely-delete-file are deleted; the
177 files created by safely-open-output-stream are renamed over the old
178 versions, if any. If a problem occurs during this stage, everything is
179 rewound and no changes are made."
180 (let ((trail (safely-trail safe))
185 (dolist (stream (safely-streams safe))
190 (let ((job (pop trail)))
192 (:shunt (destructuring-bind (tag new file) job
193 (declare (ignore tag))
194 (push `(:rmtmp ,new) revert)
195 (if (probe-file file)
196 (let ((old (safe-copy file "old")))
197 (push `(:rmtmp ,old) cleanup)
198 (push `(:revert ,old ,file) revert))
199 (push `(:rmtmp ,file) revert))
200 (rename-file new file)))
201 (:delete (destructuring-bind (tag file) job
202 (declare (ignore tag))
203 (let ((old (safe-copy file "delete")))
204 (push `(:revert ,old ,file) revert)
205 (push `(:rmtmp ,old) cleanup)
206 (delete-file file)))))))
208 (safely-unwind trail)
209 (safely-unwind revert)
210 (safely-unwind cleanup)
211 (safely-reset safe))))
213 (defmacro safely ((safe &key) &body body)
214 "Do stuff within the BODY safely. If BODY completes without errors, the
215 SAFE is committed; otherwise it's bailed."
216 `(let ((,safe (make-safely)))
220 (safely-commit ,safe)
223 (safely-bail ,safe)))))
225 (defmacro safely-writing ((stream file &rest open-args) &body body)
226 "Simple macro for writing a single file safely. STREAM is opened onto a
227 temporary file, and if BODY completes, it is renamed to FILE."
230 (let ((,stream (apply #'safely-open-output-stream
231 ,safe ,file ,open-args)))
234 ;;;----- That's all, folks --------------------------------------------------