3 ;;; Safely modify collections of files
5 ;;; (c) 2005 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
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.
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.
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.
25 (:use #:common-lisp #:mdw.base))
29 (eval-when (:compile-toplevel :execute)
30 (import #+cmu '(ext:unix-namestring unix:unix-link)
31 #+sbcl '(sb-ext:native-namestring)))
34 (declaim (inline unix-namestring))
35 (defun native-namestring (pathname &key as-file)
36 (declare (ignore as-file))
37 (unix-namestring pathname nil)))
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."
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)))
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)))
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.
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.
67 If FUNC is nil, it's treated the same as a function which always returns
70 This is inspired by a similar facility in scsh."
71 (let ((base (pathname base)))
73 (error "Gave up trying to find a temporary ~A file for ~S."
75 (let* ((new (merge-pathnames
77 :name (format nil "~A-~A-~X"
80 (random most-positive-fixnum)))
82 (ret (and (not (probe-file new))
84 (handler-case (funcall func new)
86 (unless (pathname-match-p
87 (file-error-pathname cond)
93 (return (values new ret)))))))
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."
101 (generate-fresh-file-name file "new"
107 (safely-close safe stream)
108 (push `(:shunt ,name ,file)
114 (ffi:def-call-out %rename (:name "rename")
116 (:library "libc.so.6")
117 (:arguments (from ffi:c-string)
119 (:return-type ffi:int)))
121 (declaim (inline rename))
122 (defun rename (old new)
124 (let ((target (make-pathname :directory '(:relative)
126 (rename-file old target))
129 (let ((rc (%rename (namestring old) (namestring new))))
131 (error "Failed to rename ~S to ~S: ~A" old new (posix:strerror)))))
133 (defun delete-file-without-moaning (file)
134 "Delete the FILE, ignoring errors."
135 (handler-case (delete-file file)
136 (file-error () nil)))
138 (defun rename-file-without-moaning (old new)
139 "Rename OLD to NEW, ignoring errors, and without doing any stupid name
141 (handler-case (rename old new)
142 (file-error () nil)))
144 (defun safely-unwind (trail)
145 "Roll back the TRAIL of operations."
148 (:shunt (destructuring-bind (new file) (cdr job)
149 (declare (ignore file))
150 (delete-file-without-moaning new)))
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))))))
157 (defun safely-reset (safe)
158 "Reset SAFE to its initial state."
159 (setf (safely-streams safe) nil)
160 (setf (safely-trail safe) nil))
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))
172 (defun unix-link (from to)
173 (sb-unix::int-syscall ("link" sb-alien:c-string sb-alien:c-string)
176 (defun safe-copy (file tag)
177 "Make a copy of the FILE. Return the new name."
180 ;; Use link(2) where available.
181 (generate-fresh-file-name file tag
183 (let ((from (native-namestring file
185 (to (native-namestring name
188 (unix-link from to)))))
191 (generate-fresh-file-name file tag
193 (posix:copy-file (namestring file)
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)
203 (generate-fresh-file-name file tag
208 :element-type :default)))
211 (let ((buffer (make-array 8192
212 :element-type (stream-element-type
215 (let ((read (read-sequence buffer input)))
217 (write-sequence buffer output :end read))
218 (when (< read (length buffer))
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))
233 (dolist (stream (safely-streams safe))
238 (let ((job (pop trail)))
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))
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)))))))
256 (safely-unwind trail)
257 (safely-unwind revert)
258 (safely-unwind cleanup)
259 (safely-reset safe))))
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)))
269 (safely-commit ,safe)
272 (safely-bail ,safe)))))
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."
280 (let ((,stream (safely-open-output-stream ,safe ,file ,@open-args)))
283 ;;;----- That's all, folks --------------------------------------------------