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
34 (eval-when (:compile-toplevel :execute)
35 (import #+cmu '(ext:unix-namestring unix:unix-link)
36 #+sbcl '(sb-int:unix-namestring)))
38 (defstruct (safely (:predicate safelyp))
39 "Stores information about how to commit or undo safe writes."
43 (defun safely-close (safe stream)
44 "Make sure that STREAM is closed when SAFE is finished."
45 (push stream (safely-streams safe)))
47 (defun safely-delete-file (safe file)
48 "Delete FILE when SAFE is committed."
49 (push `(:delete ,file) (safely-trail safe)))
51 (defun generate-fresh-file-name (base tag &optional func)
52 "Return a fresh file name constructed from BASE (a filespec) and TAG (some
53 short descriptive string). The generated name has the same directory and
54 type as the BASE name, but a different name.
56 If FUNC is non-nil, then it is a function to call on the generated file
57 name: generate-fresh-file-name runs in a loop, calling FUNC with generated
58 file names until FUNC returns non-nil, at which point generate-fresh-
59 file-name returns two values: the generated name, and the result of FUNC.
60 generate-fresh-file-name catches errors of type file-error from FUNC, and
61 just tries again with a new name.
63 If FUNC is nil, it's treated the same as a function which always returns
66 This is inspired by a similar facility in scsh."
67 (let ((base (pathname base)))
69 (error "Gave up trying to find a temporary ~A file for ~S."
71 (let* ((new (merge-pathnames
73 :name (format nil "~A-~A-~X"
76 (random most-positive-fixnum)))
78 (ret (and (not (probe-file new))
80 (handler-case (funcall func new)
82 (unless (pathname-match-p
83 (file-error-pathname cond)
89 (return (values new ret)))))))
91 (defun safely-open-output-stream (safe file &rest open-args)
92 "Create an output stream which will be named FILE when SAFE is committed.
93 Other OPEN-ARGS are passed to open."
96 (generate-fresh-file-name file "new"
102 (safely-close safe stream)
103 (push `(:shunt ,name ,file)
107 (defun delete-file-without-moaning (file)
108 "Delete the FILE, ignoring errors."
109 (handler-case (delete-file file)
110 (file-error () nil)))
112 (defun rename-file-without-moaning (old new)
113 "Rename OLD to NEW, ignoring errors, and without doing any stupid name
115 (handler-case (rename-file old new)
116 (file-error () nil)))
118 (defun safely-unwind (trail)
119 "Roll back the TRAIL of operations."
122 (:shunt (destructuring-bind (new file) (cdr job)
123 (declare (ignore file))
124 (delete-file-without-moaning new)))
126 (:rmtmp (destructuring-bind (file) (cdr job)
127 (delete-file-without-moaning file)))
128 (:revert (destructuring-bind (old new) (cdr job)
129 (rename-file-without-moaning old new))))))
131 (defun safely-reset (safe)
132 "Reset SAFE to its initial state."
133 (setf (safely-streams safe) nil)
134 (setf (safely-trail safe) nil))
136 (defun safely-bail (safe)
137 "Abort the operations in SAFE, unwinding all the things that have been
138 done. Streams are closed, new files are removed."
139 (dolist (stream (safely-streams safe))
140 (close stream :abort t))
141 (safely-unwind (safely-trail safe))
145 (defun unix-link (from to)
146 (sb-unix::int-syscall ("link" sb-alien:c-string sb-alien:c-string)
149 (defun safe-copy (file tag)
150 "Make a copy of the FILE. Return the new name."
153 ;; Use link(2) where available.
154 (generate-fresh-file-name file tag
156 (let ((from (unix-namestring file t))
157 (to (unix-namestring name nil)))
159 (unix-link from to)))))
162 ;; Otherwise just copy the file contents and hope for the best.
163 (with-open-file (input file :element-type :default)
166 (generate-fresh-file-name file tag
171 :element-type :default)))
174 (let ((buffer (make-array 8192
175 :element-type (stream-element-type
178 (let ((read (read-sequence buffer input)))
180 (write-sequence buffer output :end read))
181 (when (< read (length buffer))
185 (defun safely-commit (safe)
186 "Commit SAFE. The files deleted by safely-delete-file are deleted; the
187 files created by safely-open-output-stream are renamed over the old
188 versions, if any. If a problem occurs during this stage, everything is
189 rewound and no changes are made."
190 (let ((trail (safely-trail safe))
195 (dolist (stream (safely-streams safe))
200 (let ((job (pop trail)))
202 (:shunt (destructuring-bind (tag new file) job
203 (declare (ignore tag))
204 (push `(:rmtmp ,new) revert)
205 (if (probe-file file)
206 (let ((old (safe-copy file "old")))
207 (push `(:rmtmp ,old) cleanup)
208 (push `(:revert ,old ,file) revert))
209 (push `(:rmtmp ,file) revert))
210 (rename-file new file)))
211 (:delete (destructuring-bind (tag file) job
212 (declare (ignore tag))
213 (let ((old (safe-copy file "delete")))
214 (push `(:revert ,old ,file) revert)
215 (push `(:rmtmp ,old) cleanup)
216 (delete-file file)))))))
218 (safely-unwind trail)
219 (safely-unwind revert)
220 (safely-unwind cleanup)
221 (safely-reset safe))))
223 (defmacro safely ((safe &key) &body body)
224 "Do stuff within the BODY safely. If BODY completes without errors, the
225 SAFE is committed; otherwise it's bailed."
226 `(let ((,safe (make-safely)))
230 (safely-commit ,safe)
233 (safely-bail ,safe)))))
235 (defmacro safely-writing ((stream file &rest open-args) &body body)
236 "Simple macro for writing a single file safely. STREAM is opened onto a
237 temporary file, and if BODY completes, it is renamed to FILE."
240 (let ((,stream (apply #'safely-open-output-stream
241 ,safe ,file ,open-args)))
244 ;;;----- That's all, folks --------------------------------------------------