Commit | Line | Data |
---|---|---|
861345b4 | 1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; $Id$ | |
4 | ;;; | |
5 | ;;; Safely modify collections of files | |
6 | ;;; | |
7 | ;;; (c) 2005 Straylight/Edgeware | |
8 | ;;; | |
9 | ||
10 | ;;;----- Licensing notice --------------------------------------------------- | |
11 | ;;; | |
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. | |
b2c12b4e | 16 | ;;; |
861345b4 | 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. | |
b2c12b4e | 21 | ;;; |
861345b4 | 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. | |
25 | ||
0b3651e5 | 26 | (defpackage #:safely |
623291d2 | 27 | (:use #:common-lisp #:mdw.base) |
861345b4 | 28 | (:export #:safely #:safely-close #:safely-delete-file |
29 | #:safely-open-output-stream #:safely-bail #:safely-commit | |
30 | #:safely-writing)) | |
0b3651e5 | 31 | (in-package #:safely) |
861345b4 | 32 | |
d7d81997 MW |
33 | #+(or cmu sbcl) |
34 | (eval-when (:compile-toplevel :execute) | |
35 | (import #+cmu '(ext:unix-namestring unix:unix-link) | |
36 | #+sbcl '(sb-int:unix-namestring))) | |
37 | ||
861345b4 | 38 | (defstruct (safely (:predicate safelyp)) |
39 | "Stores information about how to commit or undo safe writes." | |
40 | (streams nil) | |
41 | (trail nil)) | |
5e04ac39 | 42 | |
861345b4 | 43 | (defun safely-close (safe stream) |
44 | "Make sure that STREAM is closed when SAFE is finished." | |
45 | (push stream (safely-streams safe))) | |
5e04ac39 | 46 | |
861345b4 | 47 | (defun safely-delete-file (safe file) |
48 | "Delete FILE when SAFE is committed." | |
623291d2 MW |
49 | (push `(:delete ,file) (safely-trail safe))) |
50 | ||
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. | |
55 | ||
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. | |
62 | ||
63 | If FUNC is nil, it's treated the same as a function which always returns | |
64 | t. | |
65 | ||
66 | This is inspired by a similar facility in scsh." | |
67 | (let ((base (pathname base))) | |
68 | (dotimes (i 256 | |
69 | (error "Gave up trying to find a temporary ~A file for ~S." | |
70 | tag base)) | |
71 | (let* ((new (merge-pathnames | |
72 | (make-pathname | |
73 | :name (format nil "~A-~A-~X" | |
74 | (pathname-name base) | |
75 | tag | |
76 | (random most-positive-fixnum))) | |
77 | base)) | |
78 | (ret (and (not (probe-file new)) | |
79 | (if func | |
80 | (handler-case (funcall func new) | |
81 | (file-error (cond) | |
82 | (unless (pathname-match-p | |
83 | (file-error-pathname cond) | |
84 | new) | |
85 | (error cond)) | |
86 | nil)) | |
87 | t)))) | |
88 | (when ret | |
89 | (return (values new ret))))))) | |
5e04ac39 | 90 | |
861345b4 | 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. | |
0ff9df03 | 93 | Other OPEN-ARGS are passed to open." |
623291d2 MW |
94 | (multiple-value-bind |
95 | (name stream) | |
96 | (generate-fresh-file-name file "new" | |
97 | (lambda (name) | |
98 | (apply #'open name | |
99 | :direction :output | |
100 | :if-exists nil | |
101 | open-args))) | |
861345b4 | 102 | (safely-close safe stream) |
623291d2 | 103 | (push `(:shunt ,name ,file) |
861345b4 | 104 | (safely-trail safe)) |
105 | stream)) | |
5e04ac39 | 106 | |
443fcfc4 MW |
107 | (declaim (inline rename)) |
108 | (defun rename (old new) | |
109 | #-clisp (rename-file old new) | |
110 | #+clisp (posix:copy-file old new :method :rename)) | |
111 | ||
861345b4 | 112 | (defun delete-file-without-moaning (file) |
113 | "Delete the FILE, ignoring errors." | |
623291d2 MW |
114 | (handler-case (delete-file file) |
115 | (file-error () nil))) | |
5e04ac39 | 116 | |
861345b4 | 117 | (defun rename-file-without-moaning (old new) |
118 | "Rename OLD to NEW, ignoring errors, and without doing any stupid name | |
0ff9df03 | 119 | mangling." |
443fcfc4 | 120 | (handler-case (rename old new) |
623291d2 | 121 | (file-error () nil))) |
5e04ac39 | 122 | |
861345b4 | 123 | (defun safely-unwind (trail) |
124 | "Roll back the TRAIL of operations." | |
125 | (dolist (job trail) | |
126 | (ecase (car job) | |
623291d2 MW |
127 | (:shunt (destructuring-bind (new file) (cdr job) |
128 | (declare (ignore file)) | |
861345b4 | 129 | (delete-file-without-moaning new))) |
130 | (:delete) | |
623291d2 | 131 | (:rmtmp (destructuring-bind (file) (cdr job) |
861345b4 | 132 | (delete-file-without-moaning file))) |
623291d2 | 133 | (:revert (destructuring-bind (old new) (cdr job) |
861345b4 | 134 | (rename-file-without-moaning old new)))))) |
5e04ac39 | 135 | |
861345b4 | 136 | (defun safely-reset (safe) |
137 | "Reset SAFE to its initial state." | |
138 | (setf (safely-streams safe) nil) | |
139 | (setf (safely-trail safe) nil)) | |
5e04ac39 | 140 | |
861345b4 | 141 | (defun safely-bail (safe) |
142 | "Abort the operations in SAFE, unwinding all the things that have been | |
0ff9df03 | 143 | done. Streams are closed, new files are removed." |
861345b4 | 144 | (dolist (stream (safely-streams safe)) |
145 | (close stream :abort t)) | |
146 | (safely-unwind (safely-trail safe)) | |
147 | (safely-reset safe)) | |
5e04ac39 | 148 | |
d7d81997 MW |
149 | #+sbcl |
150 | (defun unix-link (from to) | |
151 | (sb-unix::int-syscall ("link" sb-alien:c-string sb-alien:c-string) | |
152 | from to)) | |
153 | ||
623291d2 MW |
154 | (defun safe-copy (file tag) |
155 | "Make a copy of the FILE. Return the new name." | |
156 | ||
d7d81997 | 157 | #+(or cmu sbcl) |
623291d2 MW |
158 | ;; Use link(2) where available. |
159 | (generate-fresh-file-name file tag | |
160 | (lambda (name) | |
d7d81997 MW |
161 | (let ((from (unix-namestring file t)) |
162 | (to (unix-namestring name nil))) | |
623291d2 | 163 | (and from to |
d7d81997 | 164 | (unix-link from to))))) |
623291d2 | 165 | |
443fcfc4 MW |
166 | #+clisp |
167 | (generate-fresh-file-name file tag | |
168 | (lambda (name) | |
169 | (>= (unix:link (namestring file) | |
170 | (namestring name)) | |
171 | 0))) | |
172 | ||
173 | ||
d7d81997 | 174 | #-(or cmu sbcl) |
623291d2 MW |
175 | ;; Otherwise just copy the file contents and hope for the best. |
176 | (with-open-file (input file :element-type :default) | |
177 | (multiple-value-bind | |
178 | (copy output) | |
179 | (generate-fresh-file-name file tag | |
180 | (lambda (name) | |
181 | (open name | |
182 | :direction :output | |
183 | :if-exists nil | |
184 | :element-type :default))) | |
185 | (unwind-protect | |
186 | (progn | |
187 | (let ((buffer (make-array 8192 | |
188 | :element-type (stream-element-type | |
189 | input)))) | |
190 | (loop | |
191 | (let ((read (read-sequence buffer input))) | |
192 | (when (plusp read) | |
193 | (write-sequence buffer output :end read)) | |
194 | (when (< read (length buffer)) | |
195 | (return copy)))))) | |
196 | (close output))))) | |
b2c12b4e | 197 | |
861345b4 | 198 | (defun safely-commit (safe) |
199 | "Commit SAFE. The files deleted by safely-delete-file are deleted; the | |
0ff9df03 MW |
200 | files created by safely-open-output-stream are renamed over the old |
201 | versions, if any. If a problem occurs during this stage, everything is | |
202 | rewound and no changes are made." | |
861345b4 | 203 | (let ((trail (safely-trail safe)) |
204 | (revert nil) | |
205 | (cleanup nil)) | |
206 | (unwind-protect | |
207 | (progn | |
208 | (dolist (stream (safely-streams safe)) | |
209 | (close stream)) | |
210 | (loop | |
211 | (unless trail | |
212 | (return)) | |
213 | (let ((job (pop trail))) | |
214 | (ecase (car job) | |
623291d2 | 215 | (:shunt (destructuring-bind (tag new file) job |
861345b4 | 216 | (declare (ignore tag)) |
861345b4 | 217 | (push `(:rmtmp ,new) revert) |
218 | (if (probe-file file) | |
623291d2 MW |
219 | (let ((old (safe-copy file "old"))) |
220 | (push `(:rmtmp ,old) cleanup) | |
861345b4 | 221 | (push `(:revert ,old ,file) revert)) |
222 | (push `(:rmtmp ,file) revert)) | |
443fcfc4 | 223 | (rename new file))) |
623291d2 | 224 | (:delete (destructuring-bind (tag file) job |
861345b4 | 225 | (declare (ignore tag)) |
623291d2 MW |
226 | (let ((old (safe-copy file "delete"))) |
227 | (push `(:revert ,old ,file) revert) | |
228 | (push `(:rmtmp ,old) cleanup) | |
229 | (delete-file file))))))) | |
861345b4 | 230 | (setf revert nil)) |
231 | (safely-unwind trail) | |
232 | (safely-unwind revert) | |
233 | (safely-unwind cleanup) | |
234 | (safely-reset safe)))) | |
5e04ac39 | 235 | |
861345b4 | 236 | (defmacro safely ((safe &key) &body body) |
237 | "Do stuff within the BODY safely. If BODY completes without errors, the | |
0ff9df03 | 238 | SAFE is committed; otherwise it's bailed." |
861345b4 | 239 | `(let ((,safe (make-safely))) |
240 | (unwind-protect | |
241 | (progn | |
242 | ,@body | |
243 | (safely-commit ,safe) | |
244 | (setf ,safe nil)) | |
245 | (when ,safe | |
246 | (safely-bail ,safe))))) | |
5e04ac39 | 247 | |
861345b4 | 248 | (defmacro safely-writing ((stream file &rest open-args) &body body) |
249 | "Simple macro for writing a single file safely. STREAM is opened onto a | |
0ff9df03 | 250 | temporary file, and if BODY completes, it is renamed to FILE." |
861345b4 | 251 | (with-gensyms safe |
252 | `(safely (,safe) | |
7413889a | 253 | (let ((,stream (safely-open-output-stream ,safe ,file ,@open-args))) |
861345b4 | 254 | ,@body)))) |
255 | ||
256 | ;;;----- That's all, folks -------------------------------------------------- |