aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2008-05-27 20:08:21 +0000
committerStefan Monnier2008-05-27 20:08:21 +0000
commit61bb55d0e7aecec3684e37cd09f9334f4c312a5f (patch)
tree1d54870390cb274d2a6cb7a06b4c04fc4b71a800
parentf598e45ef9b9655205825316101940790e8c2b85 (diff)
downloademacs-61bb55d0e7aecec3684e37cd09f9334f4c312a5f.tar.gz
emacs-61bb55d0e7aecec3684e37cd09f9334f4c312a5f.zip
Use defstruct and markers.
(tar-setf): Remove. (tar-header): Use defstruct. Add `data-start' field. (make-tar-desc, tar-desc-tokens): Remove, folded into tar-header. (tar-desc-data-start): Remove (now called tar-header-data-start). (tar-roundup-512): New fun. (tar-header-block-tokenize): Receive a buffer position rather than a string. Handle @longLink here, be more careful about it. Create a marker for data-start. (tar-summarize-buffer): Don't handle @LongLink here any more. (tar-expunge-internal, tar-subfile-save-buffer): Don't update data-start on the following entries any more. (tar-chown-entry, tar-chgrp-entry): Use read-number.
-rw-r--r--lisp/ChangeLog14
-rw-r--r--lisp/tar-mode.el421
2 files changed, 194 insertions, 241 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 70709108b37..05308ac0cd4 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,19 @@
12008-05-27 Stefan Monnier <monnier@iro.umontreal.ca> 12008-05-27 Stefan Monnier <monnier@iro.umontreal.ca>
2 2
3 * tar-mode.el: Use defstruct and markers.
4 (tar-setf): Remove.
5 (tar-header): Use defstruct. Add `data-start' field.
6 (make-tar-desc, tar-desc-tokens): Remove, folded into tar-header.
7 (tar-desc-data-start): Remove (now called tar-header-data-start).
8 (tar-roundup-512): New fun.
9 (tar-header-block-tokenize): Receive a buffer position rather than
10 a string. Handle @longLink here, be more careful about it.
11 Create a marker for data-start.
12 (tar-summarize-buffer): Don't handle @LongLink here any more.
13 (tar-expunge-internal, tar-subfile-save-buffer): Don't update
14 data-start on the following entries any more.
15 (tar-chown-entry, tar-chgrp-entry): Use read-number.
16
3 * tar-mode.el: Use buffer-swap-text to separate summary and raw data. 17 * tar-mode.el: Use buffer-swap-text to separate summary and raw data.
4 (tar-header-offset): Remove. 18 (tar-header-offset): Remove.
5 (tar-parse-info, tar-header-offset, tar-file-name-coding-system): 19 (tar-parse-info, tar-header-offset, tar-file-name-coding-system):
diff --git a/lisp/tar-mode.el b/lisp/tar-mode.el
index a8d701808c5..3a1a0144fbd 100644
--- a/lisp/tar-mode.el
+++ b/lisp/tar-mode.el
@@ -171,47 +171,17 @@ This information is useful, but it takes screen space away from file names."
171 (> (buffer-size tar-data-buffer) (buffer-size)))) 171 (> (buffer-size tar-data-buffer) (buffer-size))))
172 172
173 173
174(defmacro tar-setf (form val)
175 "A mind-numbingly simple implementation of setf."
176 (let ((mform (macroexpand form (and (boundp 'byte-compile-macro-environment)
177 byte-compile-macro-environment))))
178 (cond ((symbolp mform) (list 'setq mform val))
179 ((not (consp mform)) (error "can't setf %s" form))
180 ((eq (car mform) 'aref)
181 (list 'aset (nth 1 mform) (nth 2 mform) val))
182 ((eq (car mform) 'car)
183 (list 'setcar (nth 1 mform) val))
184 ((eq (car mform) 'cdr)
185 (list 'setcdr (nth 1 mform) val))
186 (t (error "don't know how to setf %s" form)))))
187
188;;; down to business. 174;;; down to business.
189 175
190(defmacro make-tar-header (name mode uid git size date ck lt ln 176(defstruct (tar-header
191 magic uname gname devmaj devmin) 177 (:constructor nil)
192 (list 'vector name mode uid git size date ck lt ln 178 (:type vector)
193 magic uname gname devmaj devmin)) 179 :named
194 180 (:constructor
195(defmacro tar-header-name (x) (list 'aref x 0)) 181 make-tar-header (data-start name mode uid gid size date checksum
196(defmacro tar-header-mode (x) (list 'aref x 1)) 182 link-type link-name magic uname gname dmaj dmin)))
197(defmacro tar-header-uid (x) (list 'aref x 2)) 183 data-start name mode uid gid size date checksum link-type link-name
198(defmacro tar-header-gid (x) (list 'aref x 3)) 184 magic uname gname dmaj dmin)
199(defmacro tar-header-size (x) (list 'aref x 4))
200(defmacro tar-header-date (x) (list 'aref x 5))
201(defmacro tar-header-checksum (x) (list 'aref x 6))
202(defmacro tar-header-link-type (x) (list 'aref x 7))
203(defmacro tar-header-link-name (x) (list 'aref x 8))
204(defmacro tar-header-magic (x) (list 'aref x 9))
205(defmacro tar-header-uname (x) (list 'aref x 10))
206(defmacro tar-header-gname (x) (list 'aref x 11))
207(defmacro tar-header-dmaj (x) (list 'aref x 12))
208(defmacro tar-header-dmin (x) (list 'aref x 13))
209
210(defmacro make-tar-desc (data-start tokens)
211 (list 'cons data-start tokens))
212
213(defmacro tar-desc-data-start (x) (list 'car x))
214(defmacro tar-desc-tokens (x) (list 'cdr x))
215 185
216(defconst tar-name-offset 0) 186(defconst tar-name-offset 0)
217(defconst tar-mode-offset (+ tar-name-offset 100)) 187(defconst tar-mode-offset (+ tar-name-offset 100))
@@ -231,68 +201,95 @@ This information is useful, but it takes screen space away from file names."
231(defconst tar-prefix-offset (+ tar-dmin-offset 8)) 201(defconst tar-prefix-offset (+ tar-dmin-offset 8))
232(defconst tar-end-offset (+ tar-prefix-offset 155)) 202(defconst tar-end-offset (+ tar-prefix-offset 155))
233 203
234(defun tar-header-block-tokenize (string) 204(defun tar-roundup-512 (s)
205 "Round S up to the next multiple of 512."
206 (ash (ash (+ s 511) -9) 9))
207
208(defun tar-header-block-tokenize (pos)
235 "Return a `tar-header' structure. 209 "Return a `tar-header' structure.
236This is a list of name, mode, uid, gid, size, 210This is a list of name, mode, uid, gid, size,
237write-date, checksum, link-type, and link-name." 211write-date, checksum, link-type, and link-name."
238 (setq string (string-as-unibyte string)) 212 (assert (<= (+ pos 512) (point-max)))
239 (cond ((< (length string) 512) nil) 213 (assert (zerop (mod (- pos (point-min)) 512)))
240 (;(some 'plusp string) ; <-- oops, massive cycle hog! 214 (assert (not enable-multibyte-characters))
241 (or (not (= 0 (aref string 0))) ; This will do. 215 (let ((string (buffer-substring pos (setq pos (+ pos 512)))))
242 (not (= 0 (aref string 101)))) 216 (when ;(some 'plusp string) ; <-- oops, massive cycle hog!
243 (let* ((name-end tar-mode-offset) 217 (or (not (= 0 (aref string 0))) ; This will do.
244 (link-end (1- tar-magic-offset)) 218 (not (= 0 (aref string 101))))
245 (uname-end (1- tar-gname-offset)) 219 (let* ((name-end tar-mode-offset)
246 (gname-end (1- tar-dmaj-offset)) 220 (link-end (1- tar-magic-offset))
247 (link-p (aref string tar-linkp-offset)) 221 (uname-end (1- tar-gname-offset))
248 (magic-str (substring string tar-magic-offset (1- tar-uname-offset))) 222 (gname-end (1- tar-dmaj-offset))
249 (uname-valid-p (or (string= "ustar " magic-str) (string= "GNUtar " magic-str) 223 (link-p (aref string tar-linkp-offset))
250 (string= "ustar\0000" magic-str))) 224 (magic-str (substring string tar-magic-offset
251 name linkname 225 (1- tar-uname-offset)))
252 (nulsexp "[^\000]*\000")) 226 (uname-valid-p (member magic-str
253 (when (string-match nulsexp string tar-name-offset) 227 '("ustar " "GNUtar " "ustar\0\0")))
254 (setq name-end (min name-end (1- (match-end 0))))) 228 name linkname
255 (when (string-match nulsexp string tar-link-offset) 229 (nulsexp "[^\000]*\000"))
256 (setq link-end (min link-end (1- (match-end 0))))) 230 (when (string-match nulsexp string tar-name-offset)
257 (when (string-match nulsexp string tar-uname-offset) 231 (setq name-end (min name-end (1- (match-end 0)))))
258 (setq uname-end (min uname-end (1- (match-end 0))))) 232 (when (string-match nulsexp string tar-link-offset)
259 (when (string-match nulsexp string tar-gname-offset) 233 (setq link-end (min link-end (1- (match-end 0)))))
260 (setq gname-end (min gname-end (1- (match-end 0))))) 234 (when (string-match nulsexp string tar-uname-offset)
261 (setq name (substring string tar-name-offset name-end) 235 (setq uname-end (min uname-end (1- (match-end 0)))))
262 link-p (if (or (= link-p 0) (= link-p ?0)) 236 (when (string-match nulsexp string tar-gname-offset)
263 nil 237 (setq gname-end (min gname-end (1- (match-end 0)))))
264 (- link-p ?0))) 238 (setq name (substring string tar-name-offset name-end)
265 (setq linkname (substring string tar-link-offset link-end)) 239 link-p (if (or (= link-p 0) (= link-p ?0))
266 (when (and uname-valid-p 240 nil
267 (string-match nulsexp string tar-prefix-offset) 241 (- link-p ?0)))
268 (> (match-end 0) (1+ tar-prefix-offset))) 242 (setq linkname (substring string tar-link-offset link-end))
269 (setq name (concat (substring string tar-prefix-offset 243 (when (and uname-valid-p
270 (1- (match-end 0))) 244 (string-match nulsexp string tar-prefix-offset)
271 "/" name))) 245 (> (match-end 0) (1+ tar-prefix-offset)))
272 (if default-enable-multibyte-characters 246 (setq name (concat (substring string tar-prefix-offset
273 (setq name 247 (1- (match-end 0)))
274 (decode-coding-string name tar-file-name-coding-system) 248 "/" name)))
275 linkname 249 (if default-enable-multibyte-characters
276 (decode-coding-string linkname 250 (setq name
277 tar-file-name-coding-system))) 251 (decode-coding-string name tar-file-name-coding-system)
278 (if (and (null link-p) (string-match "/\\'" name)) (setq link-p 5)) ; directory 252 linkname
279 (make-tar-header 253 (decode-coding-string linkname
280 name 254 tar-file-name-coding-system)))
281 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset) 255 (if (and (null link-p) (string-match "/\\'" name))
282 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset) 256 (setq link-p 5)) ; directory
283 (tar-parse-octal-integer string tar-gid-offset tar-size-offset) 257
284 (tar-parse-octal-integer string tar-size-offset tar-time-offset) 258 (if (and (equal name "././@LongLink")
285 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset) 259 (equal magic-str "ustar ")) ;OLDGNU_MAGIC.
286 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset) 260 ;; This is a GNU Tar long-file-name header.
287 link-p 261 (let* ((size (tar-parse-octal-integer
288 linkname 262 string tar-size-offset tar-time-offset))
289 uname-valid-p 263 ;; -1 so as to strip the terminating 0 byte.
290 (and uname-valid-p (substring string tar-uname-offset uname-end)) 264 (name (buffer-substring pos (+ pos size -1)))
291 (and uname-valid-p (substring string tar-gname-offset gname-end)) 265 (descriptor (tar-header-block-tokenize
292 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset) 266 (+ pos (tar-roundup-512 size)))))
293 (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset) 267 (cond
294 ))) 268 ((eq link-p (- ?L ?0)) ;GNUTYPE_LONGNAME.
295 (t 'empty-tar-block))) 269 (setf (tar-header-name descriptor) name))
270 ((eq link-p (- ?K ?0)) ;GNUTYPE_LONGLINK.
271 (setf (tar-header-link-name descriptor) name))
272 (t
273 (message "Unrecognized GNU Tar @LongLink format")))
274 descriptor)
275
276 (make-tar-header
277 (copy-marker pos nil)
278 name
279 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset)
280 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset)
281 (tar-parse-octal-integer string tar-gid-offset tar-size-offset)
282 (tar-parse-octal-integer string tar-size-offset tar-time-offset)
283 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset)
284 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset)
285 link-p
286 linkname
287 uname-valid-p
288 (and uname-valid-p (substring string tar-uname-offset uname-end))
289 (and uname-valid-p (substring string tar-gname-offset gname-end))
290 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset)
291 (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset)
292 ))))))
296 293
297 294
298(defun tar-parse-octal-integer (string &optional start end) 295(defun tar-parse-octal-integer (string &optional start end)
@@ -333,6 +330,7 @@ write-date, checksum, link-type, and link-name."
333 330
334(defun tar-header-block-checksum (string) 331(defun tar-header-block-checksum (string)
335 "Compute and return a tar-acceptable checksum for this block." 332 "Compute and return a tar-acceptable checksum for this block."
333 (assert (not (multibyte-string-p string)))
336 (setq string (string-as-unibyte string)) 334 (setq string (string-as-unibyte string))
337 (let* ((chk-field-start tar-chk-offset) 335 (let* ((chk-field-start tar-chk-offset)
338 (chk-field-end (+ chk-field-start 8)) 336 (chk-field-end (+ chk-field-start 8))
@@ -423,26 +421,22 @@ MODE should be an integer which is a file mode value."
423 (if (tar-data-swapped-p) tar-data-buffer (current-buffer)) 421 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
424 (set-buffer-multibyte nil) ;Hopefully, a no-op. 422 (set-buffer-multibyte nil) ;Hopefully, a no-op.
425 (dolist (descriptor descriptors) 423 (dolist (descriptor descriptors)
426 (let* ((tokens (tar-desc-tokens descriptor)) 424 (let* ((name (tar-header-name descriptor))
427 (name (tar-header-name tokens)) 425 (dir (if (eq (tar-header-link-type descriptor) 5)
428 (dir (if (eq (tar-header-link-type tokens) 5)
429 name 426 name
430 (file-name-directory name))) 427 (file-name-directory name)))
431 (start (tar-desc-data-start descriptor)) 428 (start (tar-header-data-start descriptor))
432 (end (+ start (tar-header-size tokens)))) 429 (end (+ start (tar-header-size descriptor))))
433 (unless (file-directory-p name) 430 (unless (file-directory-p name)
434 (message "Extracting %s" name) 431 (message "Extracting %s" name)
435 (if (and dir (not (file-exists-p dir))) 432 (if (and dir (not (file-exists-p dir)))
436 (make-directory dir t)) 433 (make-directory dir t))
437 (unless (file-directory-p name) 434 (unless (file-directory-p name)
438 (write-region start end name)) 435 (write-region start end name))
439 (set-file-modes name (tar-header-mode tokens)))))))) 436 (set-file-modes name (tar-header-mode descriptor))))))))
440 437
441(defun tar-summarize-buffer () 438(defun tar-summarize-buffer ()
442 "Parse the contents of the tar file in the current buffer. 439 "Parse the contents of the tar file in the current buffer."
443Place a dired-like listing on the front;
444then narrow to it, so that only that listing
445is visible (and the real data of the buffer is hidden)."
446 (assert (tar-data-swapped-p)) 440 (assert (tar-data-swapped-p))
447 (let* ((modified (buffer-modified-p)) 441 (let* ((modified (buffer-modified-p))
448 (result '()) 442 (result '())
@@ -450,59 +444,42 @@ is visible (and the real data of the buffer is hidden)."
450 (progress-reporter 444 (progress-reporter
451 (make-progress-reporter "Parsing tar file..." 445 (make-progress-reporter "Parsing tar file..."
452 (point-min) (max 1 (- (buffer-size) 1024)))) 446 (point-min) (max 1 (- (buffer-size) 1024))))
453 tokens) 447 descriptor)
454 (with-current-buffer tar-data-buffer 448 (with-current-buffer tar-data-buffer
455 (while (and (<= (+ pos 512) (point-max)) 449 (while (and (<= (+ pos 512) (point-max))
456 (not (eq 'empty-tar-block 450 (setq descriptor (tar-header-block-tokenize pos)))
457 (setq tokens 451 (setq pos (marker-position (tar-header-data-start descriptor)))
458 (tar-header-block-tokenize
459 (buffer-substring pos (+ pos 512)))))))
460 (setq pos (+ pos 512))
461 (when (equal (tar-header-name tokens) "././@LongLink")
462 ;; This is a GNU Tar long-file-name header.
463 (let* ((size (tar-header-size tokens))
464 ;; -1 so as to strip the terminating 0 byte.
465 (name (buffer-substring pos (+ pos size -1))))
466 (setq pos (+ pos (ash (ash (+ 511 size) -9) 9)))
467 (setq tokens (tar-header-block-tokenize
468 (buffer-substring pos (+ pos 512))))
469 (tar-setf (tar-header-name tokens) name)
470 (setq pos (+ pos 512))))
471 (progress-reporter-update progress-reporter pos) 452 (progress-reporter-update progress-reporter pos)
472 (if (memq (tar-header-link-type tokens) '(20 55)) 453 (if (memq (tar-header-link-type descriptor) '(20 55))
473 ;; Foo. There's an extra empty block after these. 454 ;; Foo. There's an extra empty block after these.
474 (setq pos (+ pos 512))) 455 (setq pos (+ pos 512)))
475 (let ((size (tar-header-size tokens))) 456 (let ((size (tar-header-size descriptor)))
476 (if (< size 0) 457 (if (< size 0)
477 (error "%s has size %s - corrupted" 458 (error "%s has size %s - corrupted"
478 (tar-header-name tokens) size)) 459 (tar-header-name descriptor) size))
479 ;; 460 ;;
480 ;; This is just too slow. Don't really need it anyway.... 461 ;; This is just too slow. Don't really need it anyway....
481 ;;(tar-header-block-check-checksum 462 ;;(tar-header-block-check-checksum
482 ;; hblock (tar-header-block-checksum hblock) 463 ;; hblock (tar-header-block-checksum hblock)
483 ;; (tar-header-name tokens)) 464 ;; (tar-header-name descriptor))
484 465
485 (push (make-tar-desc pos tokens) result) 466 (push descriptor result)
486 467
487 (and (null (tar-header-link-type tokens)) 468 (and (null (tar-header-link-type descriptor))
488 (> size 0) 469 (> size 0)
489 ;; Round up to a multiple of 512. 470 ;; Round up to a multiple of 512.
490 (setq pos (+ pos (ash (ash (+ 511 size) -9) 9))))))) 471 (setq pos (+ pos (tar-roundup-512 size)))))))
491 (make-local-variable 'tar-parse-info) 472
492 (setq tar-parse-info (nreverse result)) 473 (set (make-local-variable 'tar-parse-info) (nreverse result))
493 ;; A tar file should end with a block or two of nulls, 474 ;; A tar file should end with a block or two of nulls,
494 ;; but let's not get a fatal error if it doesn't. 475 ;; but let's not get a fatal error if it doesn't.
495 (if (eq tokens 'empty-tar-block) 476 (if (null descriptor)
496 (progress-reporter-done progress-reporter) 477 (progress-reporter-done progress-reporter)
497 (message "Warning: premature EOF parsing tar file")) 478 (message "Warning: premature EOF parsing tar file"))
498 (goto-char (point-min)) 479 (goto-char (point-min))
499 (let ((inhibit-read-only t) 480 (let ((inhibit-read-only t)
500 (total-summaries 481 (total-summaries
501 (mapconcat 482 (mapconcat 'tar-header-block-summarize tar-parse-info "\n")))
502 (lambda (tar-desc)
503 (tar-header-block-summarize (tar-desc-tokens tar-desc)))
504 tar-parse-info
505 "\n")))
506 (insert total-summaries "\n")) 483 (insert total-summaries "\n"))
507 (goto-char (point-min)) 484 (goto-char (point-min))
508 (restore-buffer-modified-p modified))) 485 (restore-buffer-modified-p modified)))
@@ -716,9 +693,8 @@ appear on disk when you save the tar-file's buffer."
716 693
717(defun tar-get-descriptor () 694(defun tar-get-descriptor ()
718 (let* ((descriptor (tar-current-descriptor)) 695 (let* ((descriptor (tar-current-descriptor))
719 (tokens (tar-desc-tokens descriptor)) 696 (size (tar-header-size descriptor))
720 (size (tar-header-size tokens)) 697 (link-p (tar-header-link-type descriptor)))
721 (link-p (tar-header-link-type tokens)))
722 (if link-p 698 (if link-p
723 (error "This is %s, not a real file" 699 (error "This is %s, not a real file"
724 (cond ((eq link-p 5) "a directory") 700 (cond ((eq link-p 5) "a directory")
@@ -755,10 +731,9 @@ appear on disk when you save the tar-file's buffer."
755 (interactive) 731 (interactive)
756 (let* ((view-p (eq other-window-p 'view)) 732 (let* ((view-p (eq other-window-p 'view))
757 (descriptor (tar-get-descriptor)) 733 (descriptor (tar-get-descriptor))
758 (tokens (tar-desc-tokens descriptor)) 734 (name (tar-header-name descriptor))
759 (name (tar-header-name tokens)) 735 (size (tar-header-size descriptor))
760 (size (tar-header-size tokens)) 736 (start (tar-header-data-start descriptor))
761 (start (tar-desc-data-start descriptor))
762 (end (+ start size))) 737 (end (+ start size)))
763 (let* ((tar-buffer (current-buffer)) 738 (let* ((tar-buffer (current-buffer))
764 (tarname (buffer-name)) 739 (tarname (buffer-name))
@@ -862,8 +837,7 @@ appear on disk when you save the tar-file's buffer."
862 "Read a file name with this line's entry as the default." 837 "Read a file name with this line's entry as the default."
863 (or prompt (setq prompt "Copy to: ")) 838 (or prompt (setq prompt "Copy to: "))
864 (let* ((default-file (expand-file-name 839 (let* ((default-file (expand-file-name
865 (tar-header-name (tar-desc-tokens 840 (tar-header-name (tar-current-descriptor))))
866 (tar-current-descriptor)))))
867 (target (expand-file-name 841 (target (expand-file-name
868 (read-file-name prompt 842 (read-file-name prompt
869 (file-name-directory default-file) 843 (file-name-directory default-file)
@@ -884,10 +858,9 @@ If TO-FILE is not supplied, it is prompted for, defaulting to the name of
884the current tar-entry." 858the current tar-entry."
885 (interactive (list (tar-read-file-name))) 859 (interactive (list (tar-read-file-name)))
886 (let* ((descriptor (tar-get-descriptor)) 860 (let* ((descriptor (tar-get-descriptor))
887 (tokens (tar-desc-tokens descriptor)) 861 (name (tar-header-name descriptor))
888 (name (tar-header-name tokens)) 862 (size (tar-header-size descriptor))
889 (size (tar-header-size tokens)) 863 (start (tar-header-data-start descriptor))
890 (start (tar-desc-data-start descriptor))
891 (end (+ start size)) 864 (end (+ start size))
892 (inhibit-file-name-handlers inhibit-file-name-handlers) 865 (inhibit-file-name-handlers inhibit-file-name-handlers)
893 (inhibit-file-name-operation inhibit-file-name-operation)) 866 (inhibit-file-name-operation inhibit-file-name-operation))
@@ -935,12 +908,11 @@ With a prefix argument, un-mark that many files backward."
935(defun tar-expunge-internal () 908(defun tar-expunge-internal ()
936 "Expunge the tar-entry specified by the current line." 909 "Expunge the tar-entry specified by the current line."
937 (let* ((descriptor (tar-current-descriptor)) 910 (let* ((descriptor (tar-current-descriptor))
938 (tokens (tar-desc-tokens descriptor)) 911 ;; (line (tar-header-data-start descriptor))
939 ;; (line (tar-desc-data-start descriptor)) 912 (name (tar-header-name descriptor))
940 (name (tar-header-name tokens)) 913 (size (tar-header-size descriptor))
941 (size (tar-header-size tokens)) 914 (link-p (tar-header-link-type descriptor))
942 (link-p (tar-header-link-type tokens)) 915 (start (tar-header-data-start descriptor))
943 (start (tar-desc-data-start descriptor))
944 (following-descs (cdr (memq descriptor tar-parse-info)))) 916 (following-descs (cdr (memq descriptor tar-parse-info))))
945 (if link-p (setq size 0)) ; size lies for hard-links. 917 (if link-p (setq size 0)) ; size lies for hard-links.
946 ;; 918 ;;
@@ -951,21 +923,10 @@ With a prefix argument, un-mark that many files backward."
951 (setq tar-parse-info (delq descriptor tar-parse-info)) 923 (setq tar-parse-info (delq descriptor tar-parse-info))
952 ;; 924 ;;
953 ;; delete the data from inside the file... 925 ;; delete the data from inside the file...
954 (let* ((data-start (+ start -512)) 926 (let* ((data-start (- start 512))
955 (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9)))) 927 (data-end (+ start (tar-roundup-512 size))))
956 (with-current-buffer tar-data-buffer 928 (with-current-buffer tar-data-buffer
957 (delete-region data-start data-end)) 929 (delete-region data-start data-end)))))
958 ;;
959 ;; and finally, decrement the start-pointers of all following
960 ;; entries in the archive. This is a pig when deleting a bunch
961 ;; of files at once - we could optimize this to only do the
962 ;; iteration over the files that remain, or only iterate up to
963 ;; the next file to be deleted.
964 (let ((data-length (- data-end data-start)))
965 (dolist (desc following-descs)
966 (tar-setf (tar-desc-data-start desc)
967 (- (tar-desc-data-start desc) data-length))))
968 )))
969 930
970 931
971(defun tar-expunge (&optional noconfirm) 932(defun tar-expunge (&optional noconfirm)
@@ -1008,23 +969,20 @@ the user id as a string; otherwise, you must edit it as a number.
1008You can force editing as a number by calling this with a prefix arg. 969You can force editing as a number by calling this with a prefix arg.
1009This does not modify the disk image; you must save the tar file itself 970This does not modify the disk image; you must save the tar file itself
1010for this to be permanent." 971for this to be permanent."
1011 (interactive (list 972 (interactive
1012 (let ((tokens (tar-desc-tokens (tar-current-descriptor)))) 973 (list
1013 (if (or current-prefix-arg 974 (let ((descriptor (tar-current-descriptor)))
1014 (not (tar-header-magic tokens))) 975 (if (or current-prefix-arg
1015 (let (n) 976 (not (tar-header-magic descriptor)))
1016 (while (not (numberp (setq n (read-minibuffer 977 (read-number
1017 "New UID number: " 978 "New UID number: "
1018 (format "%s" (tar-header-uid tokens))))))) 979 (format "%s" (tar-header-uid descriptor)))
1019 n) 980 (read-string "New UID string: " (tar-header-uname descriptor))))))
1020 (read-string "New UID string: " (tar-header-uname tokens))))))
1021 (cond ((stringp new-uid) 981 (cond ((stringp new-uid)
1022 (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor))) 982 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
1023 new-uid)
1024 (tar-alter-one-field tar-uname-offset (concat new-uid "\000"))) 983 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
1025 (t 984 (t
1026 (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor))) 985 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
1027 new-uid)
1028 (tar-alter-one-field tar-uid-offset 986 (tar-alter-one-field tar-uid-offset
1029 (concat (substring (format "%6o" new-uid) 0 6) "\000 "))))) 987 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1030 988
@@ -1036,24 +994,21 @@ the group id as a string; otherwise, you must edit it as a number.
1036You can force editing as a number by calling this with a prefix arg. 994You can force editing as a number by calling this with a prefix arg.
1037This does not modify the disk image; you must save the tar file itself 995This does not modify the disk image; you must save the tar file itself
1038for this to be permanent." 996for this to be permanent."
1039 (interactive (list 997 (interactive
1040 (let ((tokens (tar-desc-tokens (tar-current-descriptor)))) 998 (list
1041 (if (or current-prefix-arg 999 (let ((descriptor (tar-current-descriptor)))
1042 (not (tar-header-magic tokens))) 1000 (if (or current-prefix-arg
1043 (let (n) 1001 (not (tar-header-magic descriptor)))
1044 (while (not (numberp (setq n (read-minibuffer 1002 (read-number
1045 "New GID number: " 1003 "New GID number: "
1046 (format "%s" (tar-header-gid tokens))))))) 1004 (format "%s" (tar-header-gid descriptor)))
1047 n) 1005 (read-string "New GID string: " (tar-header-gname descriptor))))))
1048 (read-string "New GID string: " (tar-header-gname tokens))))))
1049 (cond ((stringp new-gid) 1006 (cond ((stringp new-gid)
1050 (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor))) 1007 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1051 new-gid)
1052 (tar-alter-one-field tar-gname-offset 1008 (tar-alter-one-field tar-gname-offset
1053 (concat new-gid "\000"))) 1009 (concat new-gid "\000")))
1054 (t 1010 (t
1055 (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor))) 1011 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1056 new-gid)
1057 (tar-alter-one-field tar-gid-offset 1012 (tar-alter-one-field tar-gid-offset
1058 (concat (substring (format "%6o" new-gid) 0 6) "\000 "))))) 1013 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1059 1014
@@ -1063,13 +1018,12 @@ This does not modify the disk image; you must save the tar file itself
1063for this to be permanent." 1018for this to be permanent."
1064 (interactive 1019 (interactive
1065 (list (read-string "New name: " 1020 (list (read-string "New name: "
1066 (tar-header-name (tar-desc-tokens (tar-current-descriptor)))))) 1021 (tar-header-name (tar-current-descriptor)))))
1067 (if (string= "" new-name) (error "zero length name")) 1022 (if (string= "" new-name) (error "zero length name"))
1068 (let ((encoded-new-name (encode-coding-string new-name 1023 (let ((encoded-new-name (encode-coding-string new-name
1069 tar-file-name-coding-system))) 1024 tar-file-name-coding-system)))
1070 (if (> (length encoded-new-name) 98) (error "name too long")) 1025 (if (> (length encoded-new-name) 98) (error "name too long"))
1071 (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor))) 1026 (setf (tar-header-name (tar-current-descriptor)) new-name)
1072 new-name)
1073 (tar-alter-one-field 0 1027 (tar-alter-one-field 0
1074 (substring (concat encoded-new-name (make-string 99 0)) 0 99)))) 1028 (substring (concat encoded-new-name (make-string 99 0)) 0 99))))
1075 1029
@@ -1080,25 +1034,22 @@ This does not modify the disk image; you must save the tar file itself
1080for this to be permanent." 1034for this to be permanent."
1081 (interactive (list (tar-parse-octal-integer-safe 1035 (interactive (list (tar-parse-octal-integer-safe
1082 (read-string "New protection (octal): ")))) 1036 (read-string "New protection (octal): "))))
1083 (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor))) 1037 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1084 new-mode)
1085 (tar-alter-one-field tar-mode-offset 1038 (tar-alter-one-field tar-mode-offset
1086 (concat (substring (format "%6o" new-mode) 0 6) "\000 "))) 1039 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1087 1040
1088 1041
1089(defun tar-alter-one-field (data-position new-data-string) 1042(defun tar-alter-one-field (data-position new-data-string)
1090 (let* ((descriptor (tar-current-descriptor)) 1043 (let* ((descriptor (tar-current-descriptor)))
1091 (tokens (tar-desc-tokens descriptor)))
1092 ;; 1044 ;;
1093 ;; update the header-line. 1045 ;; update the header-line.
1094 (let ((col (current-column))) 1046 (let ((col (current-column)))
1095 (delete-region (line-beginning-position) (line-beginning-position 2)) 1047 (delete-region (line-beginning-position) (line-beginning-position 2))
1096 (insert (tar-header-block-summarize tokens) "\n") 1048 (insert (tar-header-block-summarize descriptor) "\n")
1097 (forward-line -1) (move-to-column col)) 1049 (forward-line -1) (move-to-column col))
1098 1050
1099 (with-current-buffer tar-data-buffer 1051 (with-current-buffer tar-data-buffer
1100 (let* ((start (+ (tar-desc-data-start descriptor) 1052 (let* ((start (- (tar-header-data-start descriptor) 512)))
1101 -512)))
1102 ;; 1053 ;;
1103 ;; delete the old field and insert a new one. 1054 ;; delete the old field and insert a new one.
1104 (goto-char (+ start data-position)) 1055 (goto-char (+ start data-position))
@@ -1116,12 +1067,12 @@ for this to be permanent."
1116 (insert (format "%6o" chk)) 1067 (insert (format "%6o" chk))
1117 (insert 0) 1068 (insert 0)
1118 (insert ? ) 1069 (insert ? )
1119 (tar-setf (tar-header-checksum tokens) chk) 1070 (setf (tar-header-checksum descriptor) chk)
1120 ;; 1071 ;;
1121 ;; ok, make sure we didn't botch it. 1072 ;; ok, make sure we didn't botch it.
1122 (tar-header-block-check-checksum 1073 (tar-header-block-check-checksum
1123 (buffer-substring start (+ start 512)) 1074 (buffer-substring start (+ start 512))
1124 chk (tar-header-name tokens)) 1075 chk (tar-header-name descriptor))
1125 ))))) 1076 )))))
1126 1077
1127 1078
@@ -1149,11 +1100,9 @@ to make your changes permanent."
1149 (descriptor tar-superior-descriptor) 1100 (descriptor tar-superior-descriptor)
1150 subfile-size) 1101 subfile-size)
1151 (with-current-buffer tar-superior-buffer 1102 (with-current-buffer tar-superior-buffer
1152 (let* ((tokens (tar-desc-tokens descriptor)) 1103 (let* ((start (tar-header-data-start descriptor))
1153 (start (tar-desc-data-start descriptor)) 1104 (name (tar-header-name descriptor))
1154 (name (tar-header-name tokens)) 1105 (size (tar-header-size descriptor))
1155 (size (tar-header-size tokens))
1156 (size-pad (ash (ash (+ size 511) -9) 9))
1157 (head (memq descriptor tar-parse-info)) 1106 (head (memq descriptor tar-parse-info))
1158 (following-descs (cdr head))) 1107 (following-descs (cdr head)))
1159 (if (not head) 1108 (if (not head)
@@ -1161,7 +1110,7 @@ to make your changes permanent."
1161 (with-current-buffer tar-data-buffer 1110 (with-current-buffer tar-data-buffer
1162 ;; delete the old data... 1111 ;; delete the old data...
1163 (let* ((data-start start) 1112 (let* ((data-start start)
1164 (data-end (+ data-start (ash (ash (+ size 511) -9) 9)))) 1113 (data-end (+ data-start (tar-roundup-512 size))))
1165 (narrow-to-region data-start data-end) 1114 (narrow-to-region data-start data-end)
1166 (delete-region (point-min) (point-max)) 1115 (delete-region (point-min) (point-max))
1167 ;; insert the new data... 1116 ;; insert the new data...
@@ -1174,24 +1123,19 @@ to make your changes permanent."
1174 (setq subfile-size (- (point-max) (point-min))) 1123 (setq subfile-size (- (point-max) (point-min)))
1175 ;; 1124 ;;
1176 ;; pad the new data out to a multiple of 512... 1125 ;; pad the new data out to a multiple of 512...
1177 (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9))) 1126 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1178 (goto-char (point-max)) 1127 (goto-char (point-max))
1179 (insert (make-string (- subfile-size-pad subfile-size) 0)) 1128 (insert (make-string (- subfile-size-pad subfile-size) 0))
1180 ;; 1129 ;;
1181 ;; update the data pointer of this and all following files... 1130 ;; update the data of this files...
1182 (tar-setf (tar-header-size tokens) subfile-size) 1131 (setf (tar-header-size descriptor) subfile-size)
1183 (let ((difference (- subfile-size-pad size-pad)))
1184 (dolist (desc following-descs)
1185 (tar-setf (tar-desc-data-start desc)
1186 (+ (tar-desc-data-start desc) difference))))
1187 ;; 1132 ;;
1188 ;; Update the size field in the header block. 1133 ;; Update the size field in the header block.
1189 (widen) 1134 (widen)
1190 (let ((header-start (- data-start 512))) 1135 (let ((header-start (- data-start 512)))
1191 (goto-char (+ header-start tar-size-offset)) 1136 (goto-char (+ header-start tar-size-offset))
1192 (delete-region (point) (+ (point) 12)) 1137 (delete-region (point) (+ (point) 12))
1193 (insert (format "%11o" subfile-size)) 1138 (insert (format "%11o " subfile-size))
1194 (insert ? )
1195 ;; 1139 ;;
1196 ;; Maybe update the datestamp. 1140 ;; Maybe update the datestamp.
1197 (if (not tar-update-datestamp) 1141 (if (not tar-update-datestamp)
@@ -1199,31 +1143,27 @@ to make your changes permanent."
1199 (goto-char (+ header-start tar-time-offset)) 1143 (goto-char (+ header-start tar-time-offset))
1200 (delete-region (point) (+ (point) 12)) 1144 (delete-region (point) (+ (point) 12))
1201 (insert (tar-octal-time (current-time))) 1145 (insert (tar-octal-time (current-time)))
1202 (insert ? )) 1146 (insert ?\s))
1203 ;; 1147 ;;
1204 ;; compute a new checksum and insert it. 1148 ;; compute a new checksum and insert it.
1205 (let ((chk (tar-header-block-checksum 1149 (let ((chk (tar-header-block-checksum
1206 (buffer-substring header-start data-start)))) 1150 (buffer-substring header-start data-start))))
1207 (goto-char (+ header-start tar-chk-offset)) 1151 (goto-char (+ header-start tar-chk-offset))
1208 (delete-region (point) (+ (point) 8)) 1152 (delete-region (point) (+ (point) 8))
1209 (insert (format "%6o" chk)) 1153 (insert (format "%6o\0 " chk))
1210 (insert 0) 1154 (setf (tar-header-checksum descriptor) chk))))))
1211 (insert ? )
1212 (tar-setf (tar-header-checksum tokens) chk))))))
1213 ;; 1155 ;;
1214 ;; alter the descriptor-line... 1156 ;; alter the descriptor-line...
1215 ;; 1157 ;;
1216 (let ((position (- (length tar-parse-info) (length head)))) 1158 (let ((position (- (length tar-parse-info) (length head))))
1217 (goto-char (point-min)) 1159 (goto-char (point-min))
1218 (forward-line position) 1160 (forward-line position)
1219 (beginning-of-line)
1220 (let ((p (point)) 1161 (let ((p (point))
1221 after) 1162 (after (line-beginning-position 2)))
1222 (forward-line 1) 1163 (goto-char after)
1223 (setq after (point))
1224 ;; Insert the new text after the old, before deleting, 1164 ;; Insert the new text after the old, before deleting,
1225 ;; to preserve the window start. 1165 ;; to preserve the window start.
1226 (let ((line (tar-header-block-summarize tokens t))) 1166 (let ((line (tar-header-block-summarize descriptor t)))
1227 (insert-before-markers line "\n")) 1167 (insert-before-markers line "\n"))
1228 (delete-region p after))) 1168 (delete-region p after)))
1229 ;; After doing the insertion, add any necessary final padding. 1169 ;; After doing the insertion, add any necessary final padding.
@@ -1246,10 +1186,9 @@ Leaves the region wide."
1246 (if (null tar-anal-blocksize) 1186 (if (null tar-anal-blocksize)
1247 nil 1187 nil
1248 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info)) 1188 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1249 (start (tar-desc-data-start last-desc)) 1189 (start (tar-header-data-start last-desc))
1250 (tokens (tar-desc-tokens last-desc)) 1190 (link-p (tar-header-link-type last-desc))
1251 (link-p (tar-header-link-type tokens)) 1191 (size (if link-p 0 (tar-header-size last-desc)))
1252 (size (if link-p 0 (tar-header-size tokens)))
1253 (data-end (+ start size)) 1192 (data-end (+ start size))
1254 (bbytes (ash tar-anal-blocksize 9)) 1193 (bbytes (ash tar-anal-blocksize 9))
1255 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes))))) 1194 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))