/[meta-cvs]/meta-cvs/F-FFF16CA4956A36F19290AC9E1EBAFFD8
ViewVC logotype

Contents of /meta-cvs/F-FFF16CA4956A36F19290AC9E1EBAFFD8

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.49.2.7 - (show annotations)
Sun Jan 12 20:49:45 2003 UTC (11 years, 3 months ago) by kaz
Branch: mcvs-1-0-branch
Changes since 1.49.2.6: +2 -2 lines
Hard link sync optimization.

* code/mapping.lisp (mapping-synchronize): New optional parameter,
specifies map to use instead of reading *map-local*.

* code/mcvs-generic.lisp (mcvs-generic): Pass extra parameter to
mapping-synchronize to only sync the selected subset of files.
1 ;;; This source file is part of the Meta-CVS program,
2 ;;; which is distributed under the GNU license.
3 ;;; Copyright 2002 Kaz Kylheku
4
5 (require "dirwalk")
6 (require "system")
7 (require "options")
8 (require "sync")
9 (require "chatter")
10 (require "restart")
11 (require "seqfuncs")
12 (provide "mapping")
13
14 (eval-when (:compile-toplevel :load-toplevel :execute)
15 (defconstant *mcvs-dir* "MCVS")
16 (defconstant *mcvs-map-name* "MAP")
17 (defconstant *mcvs-map-local-name* "MAP-LOCAL")
18 (defconstant *mcvs-displaced-name* "DISPLACED"))
19
20 (eval-when (:compile-toplevel :load-toplevel :execute)
21 (defconstant *mcvs-map* #.(path-cat *mcvs-dir* *mcvs-map-name*))
22 (defconstant *mcvs-map-local* #.(path-cat *mcvs-dir* *mcvs-map-local-name*))
23 (defconstant *mcvs-displaced* #.(path-cat *mcvs-dir* *mcvs-displaced-name*)))
24
25 (defvar *displaced-path-prefix* nil)
26 (defvar *displaced-path-length* nil)
27
28 (defun mcvs-locate ()
29 (let ((current-dir (split-fields (getcwd) *path-sep*)))
30 (dotimes (i (length current-dir) nil)
31 (let* ((path-components (butlast current-dir i))
32 (path-string (reduce #'path-cat path-components)))
33 (when (ignore-errors (stat (path-cat path-string *mcvs-dir*)))
34 (chdir path-string)
35 (return (if (zerop i)
36 "."
37 (reduce #'path-cat (last current-dir i)))))))))
38
39 (defun real-path-exists (path)
40 (or (null *displaced-path-prefix*)
41 (path-prefix-equal *displaced-path-prefix* path)))
42
43 (defun abstract-to-real-path (path)
44 (if *displaced-path-length*
45 (if (or (= (length path) (1- *displaced-path-length*))
46 (= (length path) *displaced-path-length*))
47 *this-dir*
48 (substring path *displaced-path-length*))
49 path))
50
51 (defun real-to-abstract-path (path)
52 (if *displaced-path-prefix*
53 (concatenate 'string *displaced-path-prefix* path)
54 path))
55
56 (declaim (inline real-path-exists abstract-to-real-path
57 real-to-abstract-path))
58
59 (defmacro in-sandbox-root-dir (&body forms)
60 (let ((downpath-sym (gensym "DOWNPATH-")))
61 `(current-dir-restore
62 (let ((,downpath-sym (mcvs-locate)))
63 (when (not ,downpath-sym)
64 (error "could not locate ~a directory." *mcvs-dir*))
65 (let* ((*displaced-path-prefix* (displaced-path-read))
66 (*displaced-path-length* (if *displaced-path-prefix*
67 (length *displaced-path-prefix*))))
68 (flet ((sandbox-translate-path (in-path)
69 (multiple-value-bind (out-path out-of-bounds)
70 (canonicalize-path
71 (if (path-absolute-p in-path)
72 (path-cat *this-dir* in-path)
73 (path-cat ,downpath-sym in-path)))
74 (if out-of-bounds
75 (error "path ~a is not within sandbox." out-path)
76 out-path))))
77 (symbol-macrolet ((sandbox-down-path ,downpath-sym))
78 ,@forms)))))))
79
80 (defstruct mapping-entry
81 (kind :file)
82 (id "")
83 (path "")
84 (target "")
85 (executable nil)
86 (raw-plist nil))
87
88 (defun equal-mapping-entries (left right)
89 (and (eq (mapping-entry-kind left) (mapping-entry-kind right))
90 (string= (mapping-entry-id left) (mapping-entry-id right))
91 (path-equal (mapping-entry-path left) (mapping-entry-path right))
92 (equal (mapping-entry-target left) (mapping-entry-target right))))
93
94 (defun equal-filemaps (left right)
95 (let ((same t))
96 (mapc #'(lambda (le re)
97 (setf same (and same (equal-mapping-entries le re))))
98 left right)
99 same))
100
101 (defun mapping-entry-parse-plist (entry)
102 (with-slots (executable raw-plist) entry
103 (destructuring-bind (&key exec &allow-other-keys)
104 raw-plist
105 (setf executable exec)))
106 (values))
107
108 (defun mapping-generate-id (&key no-dir (suffix "") (prefix "F-"))
109 (format nil "~a~a~32,'0X~a"
110 (if no-dir "" (concatenate 'string *mcvs-dir* *path-sep*))
111 prefix
112 (guid-gen)
113 (if (or (null suffix) (string= "" suffix))
114 ""
115 (concatenate 'string "." suffix))))
116
117 (defun mapping-extract-kind (filemap kind)
118 (remove-if-not #'(lambda (entry-kind)
119 (eq entry-kind kind))
120 filemap
121 :key #'mapping-entry-kind))
122
123 (defun mapping-extract-paths (filemap)
124 (mapcar #'mapping-entry-path filemap))
125 (declaim (inline mapping-extract-paths))
126
127 (defun mapping-lookup (filemap path)
128 (find path filemap :test #'path-equal :key #'mapping-entry-path))
129
130 (defun mapping-prefix-lookup (filemap prefix)
131 (if (path-equal *this-dir* prefix)
132 (first filemap)
133 (find prefix filemap :test #'path-prefix-equal :key #'mapping-entry-path)))
134
135 (defun mapping-prefix-matches (filemap path)
136 (if (path-equal *this-dir* path)
137 filemap
138 (remove-if-not #'(lambda (entry)
139 (path-prefix-equal path (mapping-entry-path entry)))
140 filemap)))
141
142 (defun mapping-same-id-p (entry-one entry-two)
143 (string= (mapping-entry-id entry-one) (mapping-entry-id entry-two)))
144
145 (defun mapping-same-path-p (entry-one entry-two)
146 (path-equal (mapping-entry-path entry-one) (mapping-entry-path entry-two)))
147
148 (defun mapping-rename-files (filemap file-list old-prefix new-prefix)
149 "Returns a new filemap, in which the pathames in the list file-list are edited
150 by replacing the old-prefix with the new-prefix. If any path thus created
151 matches an existing map entry, that map entry is removed. The sorting order
152 of the map is not preserved."
153 (flet ((combine (prefix path)
154 (if (string= path "")
155 prefix
156 (canonicalize-path (path-cat prefix path)))))
157 (let* ((op-len (length old-prefix))
158 (delete-map (mapcan #'(lambda (entry)
159 (with-slots (path) entry
160 (if (and (member path file-list
161 :test #'path-equal)
162 (path-prefix-equal old-prefix
163 path))
164 (list entry)))) filemap))
165 (replace-map (mapcan #'(lambda (entry)
166 (with-slots (path) entry
167 (let ((new-entry (copy-mapping-entry
168 entry)))
169 (setf (mapping-entry-path new-entry)
170 (combine new-prefix
171 (subseq path op-len)))
172 (list new-entry))))
173 delete-map)))
174 (append
175 (set-difference
176 (set-difference filemap delete-map :test #'mapping-same-path-p)
177 replace-map :test #'mapping-same-path-p)
178 replace-map))))
179
180 (defun malformed-map ()
181 (error "malformed map (merge conflicts?): correct and run mcvs update."))
182
183 (defun mapping-dupe-check (filemap)
184 "Signals an error condition if the filemap contains duplicate paths or
185 duplicate objects. Otherwise returns the filemap, sorted by path."
186 (let ((dupes)
187 (id-hash (make-hash-table :test #'equal))
188 (path-hash (make-hash-table :test #'equal)))
189 (dolist (entry filemap)
190 (if (gethash (mapping-entry-id entry) id-hash)
191 (push entry dupes)
192 (setf (gethash (mapping-entry-id entry) id-hash) entry))
193 (if (gethash (mapping-entry-path entry) path-hash)
194 (push entry dupes)
195 (setf (gethash (mapping-entry-path entry) path-hash) entry)))
196 (when dupes
197 (dolist (dupe dupes)
198 (chatter-terse "duplicate ~a -> ~a~%"
199 (mapping-entry-id dupe) (mapping-entry-path dupe)))
200 (error "duplicates in map: correct and run mcvs update.")))
201 filemap)
202
203 (defun mapping-convert-old-style-in (raw-filemap)
204 "Converts old-style Meta-CVS file mapping to a list of mapping-entry
205 structures."
206 (mapcar #'(lambda (item)
207 (when (or (not (consp item))
208 (not (and (stringp (first item))
209 (stringp (second item)))))
210 (malformed-map))
211 (make-mapping-entry :kind :file
212 :id (first item)
213 :path (second item)))
214 raw-filemap))
215
216 (defun mapping-convert-in (raw-filemap)
217 "Converts a Meta-CVS filemap as read from a file into its internal
218 representation---a list of mapping-entry structures."
219 (mapcar #'(lambda (item)
220 (when (or (not (consp item))
221 (not (and (keywordp (first item))
222 (stringp (second item)))))
223 (malformed-map))
224 (case (first item)
225 ((:file)
226 (let ((entry (make-mapping-entry :kind :file
227 :id (second item)
228 :path (third item)
229 :raw-plist (fourth item))))
230 (when (fourth item)
231 (mapping-entry-parse-plist entry))
232 entry))
233 ((:symlink)
234 (when (not (third item))
235 (error "bad map: symlink ~a has no target."
236 (second item)))
237 (make-mapping-entry :kind :symlink
238 :id (second item)
239 :path (third item)
240 :target (fourth item)
241 :raw-plist (fifth item)))
242 (otherwise (error "bad type keyword ~s in map."
243 (first item)))))
244 raw-filemap))
245
246 (defun mapping-convert-out (filemap)
247 "Converts the internal representation of a Meta-CVS mapping to
248 the external form that is written out to files."
249 (mapcar #'(lambda (entry)
250 (with-slots (kind id path target executable raw-plist) entry
251 (if executable
252 (setf (getf raw-plist :exec) t)
253 (remf raw-plist :exec))
254 (case kind
255 ((:file) (list* kind id path
256 (if raw-plist (list raw-plist))))
257 ((:symlink) (list* kind id path target
258 (if raw-plist (list raw-plist))))
259 (otherwise (error "unknown mapping entry type ~s." kind)))))
260 filemap))
261
262 (defun mapping-read-raw-map (stream)
263 (let ((*read-eval* nil))
264 (let ((map (read stream nil :error)))
265 (if (or (eq map :error)
266 (and (not (consp map)) (not (null map))))
267 (malformed-map)
268 map))))
269
270 (defun mapping-read (source &key sanity-check)
271 "Reads a Meta-CVS from a file, optionally performing a check
272 for duplicate entries"
273 (let (filemap)
274 ;;
275 ;; Read the raw data, ensure that the file contains
276 ;; a Lisp object and that it's a list, or at least a cons.
277 ;;
278 (if (streamp source)
279 (setf filemap (mapping-read-raw-map source))
280 (with-open-file (stream source :direction :input)
281 (setf filemap (mapping-read-raw-map stream))))
282 ;;
283 ;; Distinguish between the old-style Meta-CVS map and
284 ;; the new one. The old one is a list of lists of strings.
285 ;; The new one is a list of lists having a keyword in
286 ;; the first position.
287 ;;
288 (setf filemap (if (or (null filemap) (keywordp (first (first filemap))))
289 (mapping-convert-in filemap)
290 (mapping-convert-old-style-in filemap)))
291
292 (if sanity-check
293 (mapping-dupe-check filemap)
294 filemap)))
295
296 (defun mapping-write (filemap filename &key sort-map)
297 (when *dry-run-option*
298 (chatter-debug "not writing to ~a because of -n global option.~%" filename)
299 (return-from mapping-write))
300 (when sort-map
301 (setf filemap (sort (copy-list filemap)
302 #'string< :key #'mapping-entry-id)))
303 (let ((raw-filemap (mapping-convert-out filemap)))
304 (handler-case
305 (with-open-file (file filename :direction :output)
306 (let ((*print-right-margin* 1))
307 (prin1 raw-filemap file)
308 (terpri file)))
309 (error (cond) (error "unable to write mapping file: ~a" cond)))))
310
311 (defun mapping-synchronize (&optional filemap)
312 "Synchronizes the contents of files in the sandbox, and their corresponding
313 CVS files in the Meta-CVS directory. This must be done before any CVS operation
314 such as commit or update, so that the Meta-CVS files have the correct contents
315 reflecting local changes. It must also be done after any CVS update operation,
316 to ensure that the newly incorporated changes are propagated to the sandbox"
317 (let ((filemap (or filemap (mapping-read *mcvs-map-local*))))
318 (dolist (entry filemap)
319 (can-restart-here ("Continue synchronizing files.")
320 (with-slots (kind id path target executable) entry
321 (when (real-path-exists path)
322 (case kind
323 ((:file)
324 (let ((left id) (right (abstract-to-real-path path)))
325 (case (synchronize-files left right executable)
326 ((:left)
327 (chatter-info "sync ~a -> ~a~%" left right))
328 ((:right)
329 (chatter-info "sync ~a <- ~a~%" left right))
330 ((:same))
331 ((:dir)
332 (error "cannot sync, either ~a or ~a is a directory."
333 left right))
334 ((nil)
335 (error "cannot sync, neither ~a nor ~a exists."
336 left right)))))
337 ((:symlink)
338 (let* ((symlink (abstract-to-real-path path))
339 (linkdata (no-existence-error (readlink symlink))))
340 (when (or (not linkdata)
341 (not (string= linkdata target)))
342 (chatter-info "linking: ~a -> ~a~%"
343 symlink target)
344 (honor-dry-run (target symlink)
345 (no-existence-error (unlink symlink))
346 (ensure-directories-exist symlink)
347 (symlink target symlink))))))))))))
348
349 (defun mapping-difference (old-mapping new-mapping)
350 "Compute the difference between two mappings. Returns three values:
351 - a mapping containing only elements added by new-mapping;
352 - a mapping containing only elements removed by new-mapping; and
353 - a list of moved items, which contains pairs of elements from both, whose
354 object name matches, but path differs."
355 (multiple-value-bind (moved-pairs added-items removed-items)
356 (intersection-difference
357 new-mapping old-mapping
358 :key #'mapping-entry-id :test #'equal
359 :combine #'(lambda (new old)
360 (unless (string= (mapping-entry-path new)
361 (mapping-entry-path old))
362 (list old new)))
363 :squash-nil t)
364 (values added-items removed-items moved-pairs)))
365
366 (defun mapping-update (&key no-delete-removed)
367 #.(format nil
368 "Reads the Meta-CVS mapping files ~a and ~a, the local
369 mapping and repository mapping, respectively. It computes the difference
370 between them and then reorganizes the file structure of the sandbox as
371 necessary to make the mapping up to date. Then the local mapping file is
372 overwritten so it is identical to the repository one. This is necessary to
373 bring the local structure up to date after incorporating mapping changes
374 whether they came from the CVS repository, or from local operations."
375 *mcvs-map-local* *mcvs-map*)
376 (let ((old-filemap (mapping-read *mcvs-map-local*))
377 (new-filemap (mapping-read *mcvs-map* :sanity-check t))
378 rollback-remove-items rollback-restore-items)
379 (restart-case
380 (multiple-value-bind (added-items removed-items moved-pairs)
381 (mapping-difference old-filemap new-filemap)
382 ;; First remove what has to be removed. This way when we
383 ;; do sanity checks, we won't complain about clobbering things
384 ;; that are slated to disappear.
385 (dolist (item removed-items)
386 (when (real-path-exists (mapping-entry-path item))
387 (let ((real (abstract-to-real-path (mapping-entry-path item))))
388 (chatter-terse "removing ~a~%" real)
389 (unless no-delete-removed
390 (restart-case
391 (honor-dry-run (real)
392 (ensure-directories-gone real))
393 (ignore () :report "Ignore file removal error."))
394 (push item rollback-restore-items)))))
395
396 (dolist (pair moved-pairs)
397 (let ((old-item (first pair)))
398 (with-slots (path) old-item
399 (when (real-path-exists path)
400 (honor-dry-run (path)
401 (ensure-directories-gone (abstract-to-real-path path)))
402 (push old-item rollback-restore-items)))))
403
404 ;; Now check sanity of adds and moves, to verify they don't
405 ;; clobber any local files.
406 (let (clobber-add-items clobber-move-pairs)
407 (dolist (item added-items)
408 (with-slots (kind id path target) item
409 (when (real-path-exists path)
410 (let* ((real-path (abstract-to-real-path path))
411 (file-info (exists real-path)))
412 (when (and file-info
413 (case kind
414 ((:file)
415 (not (same-file-p file-info (stat id))))
416 ((:symlink)
417 (not (string= (readlink real-path)
418 target)))
419 (otherwise t))
420 (not (mapping-lookup old-filemap path)))
421 (push item clobber-add-items))))))
422
423 (dolist (item moved-pairs)
424 (destructuring-bind (old-item new-item) item
425 (declare (ignore old-item))
426 (with-slots (path) new-item
427 (when (real-path-exists path)
428 (let ((file-info (exists (abstract-to-real-path path))))
429 (when (and file-info
430 (not (mapping-lookup old-filemap path)))
431 (push item clobber-move-pairs)))))))
432
433 (when (or clobber-add-items clobber-move-pairs)
434 (block nil
435 (restart-bind
436 ((print-clobbers
437 #'(lambda ()
438 (dolist (item clobber-add-items)
439 (format t "add: ~a~%"
440 (abstract-to-real-path (mapping-entry-path item))))
441 (dolist (pair clobber-move-pairs)
442 (format t "move ~a -> ~a~%"
443 (abstract-to-real-path (mapping-entry-path
444 (first pair)))
445 (abstract-to-real-path (mapping-entry-path
446 (second pair))))))
447 :report-function
448 #'(lambda (stream)
449 (write-string "Print list of adds or moves which want to overwrite."
450 stream)))
451 (do-clobber
452 #'(lambda ()
453 (return))
454 :report-function
455 #'(lambda (stream)
456 (write-string "Go ahead and overwrite the target files."
457 stream))))
458 (error "some moves or adds want to overwrite local files or directories.")))))
459
460 ;; Sanity check passed, complete moves and adds.
461 (dolist (item moved-pairs)
462 (destructuring-bind (old-item new-item) item
463 (with-slots ((old-path path) (old-id id)) old-item
464 (with-slots ((new-path path) (new-id id) kind target executable)
465 new-item
466 (let ((real-old-exists (real-path-exists old-path))
467 (real-new-exists (real-path-exists new-path)))
468 (let ((real-old (and real-old-exists
469 (abstract-to-real-path old-path)))
470 (real-new (and real-new-exists
471 (abstract-to-real-path new-path))))
472 (cond
473 ((and real-old-exists real-new-exists)
474 (chatter-terse "moving ~a -> ~a~%" real-old real-new))
475 (real-new-exists
476 (chatter-terse "moving (out-of-sandbox) -> ~a~%" real-new))
477 (real-old-exists
478 (chatter-terse "moving ~a -> (out-of-sandbox)~%" real-old)))
479
480 (when real-new-exists
481 (no-existence-error (honor-dry-run (real-new)
482 (unlink real-new)))
483 (case kind
484 ((:file)
485 (synchronize-files new-id real-new executable))
486 ((:symlink)
487 (honor-dry-run (target real-new)
488 (ensure-directories-exist real-new)
489 (symlink target real-new))))
490 (push new-item rollback-remove-items))))))))
491
492 (dolist (item added-items)
493 (with-slots (kind id path target executable) item
494 (when (real-path-exists path)
495 (let ((real (abstract-to-real-path path)))
496 (can-restart-here ("Continue updating file structure.")
497 (no-existence-error (honor-dry-run (real)
498 (unlink real)))
499 (case kind
500 ((:file)
501 (chatter-terse "adding ~a~%" real)
502 (synchronize-files id real executable))
503 ((:symlink)
504 (chatter-terse "linking ~a -> ~a~%" real target)
505 (honor-dry-run (real)
506 (ensure-directories-exist real)
507 (symlink target real))))
508 (push item rollback-remove-items)))))))
509 (continue ()
510 :report "Restore all restructuring done so far."
511 (chatter-debug "Restoring.~%")
512 (dolist (item rollback-remove-items)
513 (let ((real (abstract-to-real-path (mapping-entry-path item))))
514 (chatter-terse "removing ~a~%" real)
515 (honor-dry-run (real)
516 (ensure-directories-gone real))))
517 (dolist (item rollback-restore-items)
518 (with-slots (kind path id target executable) item
519 (let ((real (abstract-to-real-path path)))
520 (chatter-terse "restoring ~a~%" real)
521 (case kind
522 ((:file)
523 (synchronize-files id real executable))
524 ((:symlink)
525 (honor-dry-run (real)
526 (ensure-directories-exist real)
527 (symlink target real)))))))
528 (return-from mapping-update nil)))
529
530 (mapping-write new-filemap *mcvs-map-local*))
531 t)
532
533 (defun mapping-removed-files (filemap)
534 (let ((to-be-removed ())
535 (f-hash (make-hash-table :test #'equal)))
536 (dolist (entry filemap)
537 (setf (gethash (mapping-entry-id entry) f-hash) entry))
538 (for-each-file-info (fi *mcvs-dir*)
539 (when (and (directory-p fi)
540 (path-equal (basename (file-name fi)) "CVS"))
541 (skip))
542 (let ((base (basename (file-name fi))))
543 (multiple-value-bind (suffix name) (suffix base)
544 (declare (ignore suffix))
545 (when (and (= (length name) 34)
546 (string= (subseq name 0 2) "F-")
547 (not (gethash (file-name fi) f-hash)))
548 (push (file-name fi) to-be-removed)))))
549 to-be-removed))
550
551 (defun displaced-path-read ()
552 (let ((*read-eval* nil))
553 (ignore-errors (with-open-file (file *mcvs-displaced* :direction :input)
554 (read file)))))
555
556 (defun displaced-path-write (path)
557 (with-open-file (file *mcvs-displaced* :direction :output)
558 (prin1 path file)
559 (terpri file)))

  ViewVC Help
Powered by ViewVC 1.1.5