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-ext:native-namestring)))
39 (declaim (inline unix-namestring))
40 (defun native-namestring (pathname &key as-file)
41 (declare (ignore as-file))
42 (unix-namestring pathname nil)))
44 (defstruct (safely (:predicate safelyp))
45 "Stores information about how to commit or undo safe writes."
49 (defun safely-close (safe stream)
50 "Make sure that STREAM is closed when SAFE is finished."
51 (push stream (safely-streams safe)))
53 (defun safely-delete-file (safe file)
54 "Delete FILE when SAFE is committed."
55 (push `(:delete ,file) (safely-trail safe)))
57 (defun generate-fresh-file-name (base tag &optional func)
58 "Return a fresh file name constructed from BASE (a filespec) and TAG (some
59 short descriptive string). The generated name has the same directory and
60 type as the BASE name, but a different name.
62 If FUNC is non-nil, then it is a function to call on the generated file
63 name: generate-fresh-file-name runs in a loop, calling FUNC with generated
64 file names until FUNC returns non-nil, at which point generate-fresh-
65 file-name returns two values: the generated name, and the result of FUNC.
66 generate-fresh-file-name catches errors of type file-error from FUNC, and
67 just tries again with a new name.
69 If FUNC is nil, it's treated the same as a function which always returns
72 This is inspired by a similar facility in scsh."
73 (let ((base (pathname base)))
75 (error "Gave up trying to find a temporary ~A file for ~S."
77 (let* ((new (merge-pathnames
79 :name (format nil "~A-~A-~X"
82 (random most-positive-fixnum)))
84 (ret (and (not (probe-file new))
86 (handler-case (funcall func new)
88 (unless (pathname-match-p
89 (file-error-pathname cond)
95 (return (values new ret)))))))
97 (defun safely-open-output-stream (safe file &rest open-args)
98 "Create an output stream which will be named FILE when SAFE is committed.
99 Other OPEN-ARGS are passed to open."
102 (generate-fresh-file-name file "new"
108 (safely-close safe stream)
109 (push `(:shunt ,name ,file)
113 (defun delete-file-without-moaning (file)
114 "Delete the FILE, ignoring errors."
115 (handler-case (delete-file file)
116 (file-error () nil)))
118 (defun rename-file-without-moaning (old new)
119 "Rename OLD to NEW, ignoring errors, and without doing any stupid name
121 (handler-case (rename-file old new)
122 (file-error () nil)))
124 (defun safely-unwind (trail)
125 "Roll back the TRAIL of operations."
128 (:shunt (destructuring-bind (new file) (cdr job)
129 (declare (ignore file))
130 (delete-file-without-moaning new)))
132 (:rmtmp (destructuring-bind (file) (cdr job)
133 (delete-file-without-moaning file)))
134 (:revert (destructuring-bind (old new) (cdr job)
135 (rename-file-without-moaning old new))))))
137 (defun safely-reset (safe)
138 "Reset SAFE to its initial state."
139 (setf (safely-streams safe) nil)
140 (setf (safely-trail safe) nil))
142 (defun safely-bail (safe)
143 "Abort the operations in SAFE, unwinding all the things that have been
144 done. Streams are closed, new files are removed."
145 (dolist (stream (safely-streams safe))
146 (close stream :abort t))
147 (safely-unwind (safely-trail safe))
151 (defun unix-link (from to)
152 (sb-unix::int-syscall ("link" sb-alien:c-string sb-alien:c-string)
155 (defun safe-copy (file tag)
156 "Make a copy of the FILE. Return the new name."
159 ;; Use link(2) where available.
160 (generate-fresh-file-name file tag
162 (let ((from (native-namestring file
164 (to (native-namestring name
167 (unix-link from to)))))
170 ;; Otherwise just copy the file contents and hope for the best.
171 (with-open-file (input file :element-type :default)
174 (generate-fresh-file-name file tag
179 :element-type :default)))
182 (let ((buffer (make-array 8192
183 :element-type (stream-element-type
186 (let ((read (read-sequence buffer input)))
188 (write-sequence buffer output :end read))
189 (when (< read (length buffer))
193 (defun safely-commit (safe)
194 "Commit SAFE. The files deleted by safely-delete-file are deleted; the
195 files created by safely-open-output-stream are renamed over the old
196 versions, if any. If a problem occurs during this stage, everything is
197 rewound and no changes are made."
198 (let ((trail (safely-trail safe))
203 (dolist (stream (safely-streams safe))
208 (let ((job (pop trail)))
210 (:shunt (destructuring-bind (tag new file) job
211 (declare (ignore tag))
212 (push `(:rmtmp ,new) revert)
213 (if (probe-file file)
214 (let ((old (safe-copy file "old")))
215 (push `(:rmtmp ,old) cleanup)
216 (push `(:revert ,old ,file) revert))
217 (push `(:rmtmp ,file) revert))
218 (rename-file new file)))
219 (:delete (destructuring-bind (tag file) job
220 (declare (ignore tag))
221 (let ((old (safe-copy file "delete")))
222 (push `(:revert ,old ,file) revert)
223 (push `(:rmtmp ,old) cleanup)
224 (delete-file file)))))))
226 (safely-unwind trail)
227 (safely-unwind revert)
228 (safely-unwind cleanup)
229 (safely-reset safe))))
231 (defmacro safely ((safe &key) &body body)
232 "Do stuff within the BODY safely. If BODY completes without errors, the
233 SAFE is committed; otherwise it's bailed."
234 `(let ((,safe (make-safely)))
238 (safely-commit ,safe)
241 (safely-bail ,safe)))))
243 (defmacro safely-writing ((stream file &rest open-args) &body body)
244 "Simple macro for writing a single file safely. STREAM is opened onto a
245 temporary file, and if BODY completes, it is renamed to FILE."
248 (let ((,stream (safely-open-output-stream ,safe ,file ,@open-args)))
251 ;;;----- That's all, folks --------------------------------------------------