*** empty log message ***
[emacs.git] / lisp / tar-mode.el
blob534204e6124b497c60a0bf493d1f44b59c33422b
1 ;;; tar-mode.el --- simple editing of tar files from GNU emacs
3 ;;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Created: 04 Apr 1990
7 ;; Version: 1.21
8 ;; Keywords: unix
10 ;;; This file is part of GNU Emacs.
11 ;;;
12 ;;; GNU Emacs 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, or (at your option)
15 ;;; any later version.
16 ;;;
17 ;;; GNU Emacs 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.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;;; This package attempts to make dealing with Unix 'tar' archives easier.
29 ;;; When this code is loaded, visiting a file whose name ends in '.tar' will
30 ;;; cause the contents of that archive file to be displayed in a Dired-like
31 ;;; listing. It is then possible to use the customary Dired keybindings to
32 ;;; extract sub-files from that archive, either by reading them into their own
33 ;;; editor buffers, or by copying them directly to arbitrary files on disk.
34 ;;; It is also possible to delete sub-files from within the tar file and write
35 ;;; the modified archive back to disk, or to edit sub-files within the archive
36 ;;; and re-insert the modified files into the archive. See the documentation
37 ;;; string of tar-mode for more info.
39 ;;; To autoload, add this to your .emacs file:
40 ;;;
41 ;;; (setq auto-mode-alist (cons '("\\.tar$" . tar-mode) auto-mode-alist))
42 ;;; (autoload 'tar-mode "tar-mode")
43 ;;;
44 ;;; But beware: for certain tar files - those whose very first file has
45 ;;; a -*- property line - autoloading won't work. See the function
46 ;;; "tar-normal-mode" to understand why.
48 ;;; This code now understands the extra fields that GNU tar adds to tar files.
50 ;;; This interacts correctly with "uncompress.el" in the Emacs library,
51 ;;; which you get with
52 ;;;
53 ;;; (autoload 'uncompress-while-visiting "uncompress")
54 ;;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
55 ;;; auto-mode-alist))
56 ;;;
57 ;;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
59 ;;; *************** TO DO ***************
60 ;;;
61 ;;; o chmod should understand "a+x,og-w".
62 ;;;
63 ;;; o It's not possible to add a NEW file to a tar archive; not that
64 ;;; important, but still...
65 ;;;
66 ;;; o In the directory listing, we don't show creation times because I don't
67 ;;; know how to print an arbitrary date, and I don't really want to have to
68 ;;; implement decode-universal-time.
69 ;;;
70 ;;; o The code is less efficient that it could be - in a lot of places, I
71 ;;; pull a 512-character string out of the buffer and parse it, when I could
72 ;;; be parsing it in place, not garbaging a string. Should redo that.
73 ;;;
74 ;;; o I'd like a command that searches for a string/regexp in every subfile
75 ;;; of an archive, where <esc> would leave you in a subfile-edit buffer.
76 ;;; (Like the Meta-R command of the Zmacs mail reader.)
77 ;;;
78 ;;; o Sometimes (but not always) reverting the tar-file buffer does not
79 ;;; re-grind the listing, and you are staring at the binary tar data.
80 ;;; Typing 'g' again immediately after that will always revert and re-grind
81 ;;; it, though. I have no idea why this happens.
82 ;;;
83 ;;; o Tar-mode interacts poorly with crypt.el and zcat.el because the tar
84 ;;; write-file-hook actually writes the file. Instead it should remove the
85 ;;; header (and conspire to put it back afterwards) so that other write-file
86 ;;; hooks which frob the buffer have a chance to do their dirty work. There
87 ;;; might be a problem if the tar write-file-hook does not come *first* on
88 ;;; the list.
89 ;;;
90 ;;; o Block files, sparse files, continuation files, and the various header
91 ;;; types aren't editable. Actually I don't know that they work at all.
93 ;;; Code:
95 (defvar tar-anal-blocksize 20
96 "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
97 The blocksize of a tar file is not really the size of the blocks; rather, it is
98 the number of blocks written with one system call. When tarring to a tape,
99 this is the size of the *tape* blocks, but when writing to a file, it doesn't
100 matter much. The only noticeable difference is that if a tar file does not
101 have a blocksize of 20, tar will tell you that; all this really controls is
102 how many null padding bytes go on the end of the tar file.")
104 (defvar tar-update-datestamp nil
105 "*Whether tar-mode should play fast and loose with sub-file datestamps;
106 if this is true, then editing and saving a tar file entry back into its
107 tar file will update its datestamp. If false, the datestamp is unchanged.
108 You may or may not want this - it is good in that you can tell when a file
109 in a tar archive has been changed, but it is bad for the same reason that
110 editing a file in the tar archive at all is bad - the changed version of
111 the file never exists on disk.")
115 ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
116 ;;; but "cl.el" was messing some people up (also it's really big).
118 (defmacro tar-setf (form val)
119 "A mind-numbingly simple implementation of setf."
120 (let ((mform (macroexpand form (and (boundp 'byte-compile-macro-environment)
121 byte-compile-macro-environment))))
122 (cond ((symbolp mform) (list 'setq mform val))
123 ((not (consp mform)) (error "can't setf %s" form))
124 ((eq (car mform) 'aref)
125 (list 'aset (nth 1 mform) (nth 2 mform) val))
126 ((eq (car mform) 'car)
127 (list 'setcar (nth 1 mform) val))
128 ((eq (car mform) 'cdr)
129 (list 'setcdr (nth 1 mform) val))
130 (t (error "don't know how to setf %s" form)))))
132 (defmacro tar-dolist (control &rest body)
133 "syntax: (dolist (var-name list-expr &optional return-value) &body body)"
134 (let ((var (car control))
135 (init (car (cdr control)))
136 (val (car (cdr (cdr control)))))
137 (list 'let (list (list '_dolist_iterator_ init))
138 (list 'while '_dolist_iterator_
139 (cons 'let
140 (cons (list (list var '(car _dolist_iterator_)))
141 (append body
142 (list (list 'setq '_dolist_iterator_
143 (list 'cdr '_dolist_iterator_)))))))
144 val)))
146 (defmacro tar-dotimes (control &rest body)
147 "syntax: (dolist (var-name count-expr &optional return-value) &body body)"
148 (let ((var (car control))
149 (n (car (cdr control)))
150 (val (car (cdr (cdr control)))))
151 (list 'let (list (list '_dotimes_end_ n)
152 (list var 0))
153 (cons 'while
154 (cons (list '< var '_dotimes_end_)
155 (append body
156 (list (list 'setq var (list '1+ var))))))
157 val)))
160 ;;; down to business.
162 (defmacro make-tar-header (name mode uid git size date ck lt ln
163 magic uname gname devmaj devmin)
164 (list 'vector name mode uid git size date ck lt ln
165 magic uname gname devmaj devmin))
167 (defmacro tar-header-name (x) (list 'aref x 0))
168 (defmacro tar-header-mode (x) (list 'aref x 1))
169 (defmacro tar-header-uid (x) (list 'aref x 2))
170 (defmacro tar-header-gid (x) (list 'aref x 3))
171 (defmacro tar-header-size (x) (list 'aref x 4))
172 (defmacro tar-header-date (x) (list 'aref x 5))
173 (defmacro tar-header-checksum (x) (list 'aref x 6))
174 (defmacro tar-header-link-type (x) (list 'aref x 7))
175 (defmacro tar-header-link-name (x) (list 'aref x 8))
176 (defmacro tar-header-magic (x) (list 'aref x 9))
177 (defmacro tar-header-uname (x) (list 'aref x 10))
178 (defmacro tar-header-gname (x) (list 'aref x 11))
179 (defmacro tar-header-dmaj (x) (list 'aref x 12))
180 (defmacro tar-header-dmin (x) (list 'aref x 13))
182 (defmacro make-tar-desc (data-start tokens)
183 (list 'cons data-start tokens))
185 (defmacro tar-desc-data-start (x) (list 'car x))
186 (defmacro tar-desc-tokens (x) (list 'cdr x))
188 (defconst tar-name-offset 0)
189 (defconst tar-mode-offset (+ tar-name-offset 100))
190 (defconst tar-uid-offset (+ tar-mode-offset 8))
191 (defconst tar-gid-offset (+ tar-uid-offset 8))
192 (defconst tar-size-offset (+ tar-gid-offset 8))
193 (defconst tar-time-offset (+ tar-size-offset 12))
194 (defconst tar-chk-offset (+ tar-time-offset 12))
195 (defconst tar-linkp-offset (+ tar-chk-offset 8))
196 (defconst tar-link-offset (+ tar-linkp-offset 1))
197 ;;; GNU-tar specific slots.
198 (defconst tar-magic-offset (+ tar-link-offset 100))
199 (defconst tar-uname-offset (+ tar-magic-offset 8))
200 (defconst tar-gname-offset (+ tar-uname-offset 32))
201 (defconst tar-dmaj-offset (+ tar-gname-offset 32))
202 (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
203 (defconst tar-end-offset (+ tar-dmin-offset 8))
205 (defun tokenize-tar-header-block (string)
206 "Returns a 'tar-header' structure (a list of name, mode, uid, gid, size,
207 write-date, checksum, link-type, and link-name)."
208 (cond ((< (length string) 512) nil)
209 (;(some 'plusp string) ; <-- oops, massive cycle hog!
210 (or (not (= 0 (aref string 0))) ; This will do.
211 (not (= 0 (aref string 101))))
212 (let* ((name-end (1- tar-mode-offset))
213 (link-end (1- tar-magic-offset))
214 (uname-end (1- tar-gname-offset))
215 (gname-end (1- tar-dmaj-offset))
216 (link-p (aref string tar-linkp-offset))
217 (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
218 (uname-valid-p (or (string= "ustar " magic-str) (string= "GNUtar " magic-str)))
219 name
220 (nulsexp "[^\000]*\000"))
221 (and (string-match nulsexp string tar-name-offset) (setq name-end (min name-end (1- (match-end 0)))))
222 (and (string-match nulsexp string tar-link-offset) (setq link-end (min link-end (1- (match-end 0)))))
223 (and (string-match nulsexp string tar-uname-offset) (setq uname-end (min uname-end (1- (match-end 0)))))
224 (and (string-match nulsexp string tar-gname-offset) (setq gname-end (min gname-end (1- (match-end 0)))))
225 (setq name (substring string tar-name-offset name-end)
226 link-p (if (or (= link-p 0) (= link-p ?0))
228 (- link-p ?0)))
229 (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
230 (make-tar-header
231 name
232 (tar-parse-octal-integer string tar-mode-offset (1- tar-uid-offset))
233 (tar-parse-octal-integer string tar-uid-offset (1- tar-gid-offset))
234 (tar-parse-octal-integer string tar-gid-offset (1- tar-size-offset))
235 (tar-parse-octal-integer string tar-size-offset (1- tar-time-offset))
236 (tar-parse-octal-integer string tar-time-offset (1- tar-chk-offset))
237 (tar-parse-octal-integer string tar-chk-offset (1- tar-linkp-offset))
238 link-p
239 (substring string tar-link-offset link-end)
240 uname-valid-p
241 (and uname-valid-p (substring string tar-uname-offset uname-end))
242 (and uname-valid-p (substring string tar-gname-offset gname-end))
243 (tar-parse-octal-integer string tar-dmaj-offset (1- tar-dmin-offset))
244 (tar-parse-octal-integer string tar-dmin-offset (1- tar-end-offset))
246 (t 'empty-tar-block)))
249 (defun tar-parse-octal-integer (string &optional start end)
250 "deletes all your files, and then reboots."
251 (if (null start) (setq start 0))
252 (if (null end) (setq end (length string)))
253 (if (= (aref string start) 0)
255 (let ((n 0))
256 (while (< start end)
257 (setq n (if (< (aref string start) ?0) n
258 (+ (* n 8) (- (aref string start) 48)))
259 start (1+ start)))
260 n)))
262 (defun tar-parse-octal-integer-safe (string)
263 (let ((L (length string)))
264 (if (= L 0) (error "empty string"))
265 (tar-dotimes (i L)
266 (if (or (< (aref string i) ?0)
267 (> (aref string i) ?7))
268 (error "'%c' is not an octal digit."))))
269 (tar-parse-octal-integer string))
272 (defun checksum-tar-header-block (string)
273 "Computes and returns a tar-acceptable checksum for this block."
274 (let* ((chk-field-start tar-chk-offset)
275 (chk-field-end (+ chk-field-start 8))
276 (sum 0)
277 (i 0))
278 ;; Add up all of the characters except the ones in the checksum field.
279 ;; Add that field as if it were filled with spaces.
280 (while (< i chk-field-start)
281 (setq sum (+ sum (aref string i))
282 i (1+ i)))
283 (setq i chk-field-end)
284 (while (< i 512)
285 (setq sum (+ sum (aref string i))
286 i (1+ i)))
287 (+ sum (* 32 8))))
289 (defun check-tar-header-block-checksum (hblock desired-checksum file-name)
290 "Beep and print a warning if the checksum doesn't match."
291 (if (not (= desired-checksum (checksum-tar-header-block hblock)))
292 (progn (beep) (message "Invalid checksum for file %s!" file-name))))
294 (defun recompute-tar-header-block-checksum (hblock)
295 "Modifies the given string to have a valid checksum field."
296 (let* ((chk (checksum-tar-header-block hblock))
297 (chk-string (format "%6o" chk))
298 (l (length chk-string)))
299 (aset hblock 154 0)
300 (aset hblock 155 32)
301 (tar-dotimes (i l) (aset hblock (- 153 i) (aref chk-string (- l i 1)))))
302 hblock)
305 (defun tar-grind-file-mode (mode string start)
306 "Write a \"-rw--r--r-\" representing MODE into STRING beginning at START."
307 (aset string start (if (zerop (logand 256 mode)) ?- ?r))
308 (aset string (+ start 1) (if (zerop (logand 128 mode)) ?- ?w))
309 (aset string (+ start 2) (if (zerop (logand 64 mode)) ?- ?x))
310 (aset string (+ start 3) (if (zerop (logand 32 mode)) ?- ?r))
311 (aset string (+ start 4) (if (zerop (logand 16 mode)) ?- ?w))
312 (aset string (+ start 5) (if (zerop (logand 8 mode)) ?- ?x))
313 (aset string (+ start 6) (if (zerop (logand 4 mode)) ?- ?r))
314 (aset string (+ start 7) (if (zerop (logand 2 mode)) ?- ?w))
315 (aset string (+ start 8) (if (zerop (logand 1 mode)) ?- ?x))
316 (if (zerop (logand 1024 mode)) nil (aset string (+ start 2) ?s))
317 (if (zerop (logand 2048 mode)) nil (aset string (+ start 5) ?s))
318 string)
320 (defun summarize-tar-header-block (tar-hblock &optional mod-p)
321 "Returns a line similar to the output of 'tar -vtf'."
322 (let ((name (tar-header-name tar-hblock))
323 (mode (tar-header-mode tar-hblock))
324 (uid (tar-header-uid tar-hblock))
325 (gid (tar-header-gid tar-hblock))
326 (uname (tar-header-uname tar-hblock))
327 (gname (tar-header-gname tar-hblock))
328 (size (tar-header-size tar-hblock))
329 (time (tar-header-date tar-hblock))
330 (ck (tar-header-checksum tar-hblock))
331 (link-p (tar-header-link-type tar-hblock))
332 (link-name (tar-header-link-name tar-hblock))
334 (let* ((left 11)
335 (namew 8)
336 (groupw 8)
337 (sizew 8)
338 (datew 2)
339 (slash (1- (+ left namew)))
340 (lastdigit (+ slash groupw sizew))
341 (namestart (+ lastdigit datew))
342 (string (make-string (+ namestart (length name) (if link-p (+ 5 (length link-name)) 0)) 32))
343 (type (tar-header-link-type tar-hblock)))
344 (aset string 0 (if mod-p ?* ? ))
345 (aset string 1
346 (cond ((or (eq type nil) (eq type 0)) ?-)
347 ((eq type 1) ?l) ; link
348 ((eq type 2) ?s) ; symlink
349 ((eq type 3) ?c) ; char special
350 ((eq type 4) ?b) ; block special
351 ((eq type 5) ?d) ; directory
352 ((eq type 6) ?p) ; FIFO/pipe
353 ((eq type 20) ?*) ; directory listing
354 ((eq type 29) ?M) ; multivolume continuation
355 ((eq type 35) ?S) ; sparse
356 ((eq type 38) ?V) ; volume header
358 (tar-grind-file-mode mode string 2)
359 (setq uid (if (= 0 (length uname)) (int-to-string uid) uname))
360 (setq gid (if (= 0 (length gname)) (int-to-string gid) gname))
361 (setq size (int-to-string size))
362 (tar-dotimes (i (min (1- namew) (length uid))) (aset string (- slash i) (aref uid (- (length uid) i 1))))
363 (aset string (1+ slash) ?/)
364 (tar-dotimes (i (min (1- groupw) (length gid))) (aset string (+ (+ slash 2) i) (aref gid i)))
365 (tar-dotimes (i (min sizew (length size))) (aset string (- lastdigit i) (aref size (- (length size) i 1))))
366 ;; ## bloody hell, how do I print an arbitrary date??
367 (tar-dotimes (i (length name)) (aset string (+ namestart i) (aref name i)))
368 (if (or (eq link-p 1) (eq link-p 2))
369 (progn
370 (tar-dotimes (i 3) (aset string (+ namestart 1 (length name) i) (aref (if (= link-p 1) "==>" "-->") i)))
371 (tar-dotimes (i (length link-name)) (aset string (+ namestart 5 (length name) i) (aref link-name i)))))
372 string)))
375 (defun tar-summarize-buffer ()
376 "Parse the contents of the tar file in the current buffer, and place a
377 dired-like listing on the front; then narrow to it, so that only that listing
378 is visible (and the real data of the buffer is hidden)."
379 (message "parsing tar file...")
380 (let* ((result '())
381 (pos 1)
382 (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
383 (bs100 (max 1 (/ bs 100)))
384 (tokens nil))
385 (while (not (eq tokens 'empty-tar-block))
386 (let* ((hblock (buffer-substring pos (+ pos 512))))
387 (setq tokens (tokenize-tar-header-block hblock))
388 (setq pos (+ pos 512))
389 (message "parsing tar file...%s%%"
390 ;(/ (* pos 100) bs) ; this gets round-off lossage
391 (/ pos bs100) ; this doesn't
393 (if (eq tokens 'empty-tar-block)
395 (if (null tokens) (error "premature EOF parsing tar file."))
396 (if (eq (tar-header-link-type tokens) 20)
397 ;; Foo. There's an extra empty block after these.
398 (setq pos (+ pos 512)))
399 (let ((size (tar-header-size tokens)))
400 (if (< size 0)
401 (error "%s has size %s - corrupted."
402 (tar-header-name tokens) size))
404 ; This is just too slow. Don't really need it anyway....
405 ;(check-tar-header-block-checksum
406 ; hblock (checksum-tar-header-block hblock)
407 ; (tar-header-name tokens))
409 (setq result (cons (make-tar-desc pos tokens) result))
411 (if (and (null (tar-header-link-type tokens))
412 (> size 0))
413 (setq pos
414 (+ pos 512 (ash (ash (1- size) -9) 9)) ; this works
415 ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
417 ))))
418 (make-local-variable 'tar-parse-info)
419 (setq tar-parse-info (nreverse result)))
420 (save-excursion
421 (goto-char (point-min))
422 (let ((buffer-read-only nil))
423 (tar-dolist (tar-desc tar-parse-info)
424 (insert-string
425 (summarize-tar-header-block (tar-desc-tokens tar-desc)))
426 (insert-string "\n"))
427 (make-local-variable 'tar-header-offset)
428 (setq tar-header-offset (point))
429 (narrow-to-region 1 tar-header-offset)
430 (set-buffer-modified-p nil)))
431 (message "parsing tar file...done."))
434 (defvar tar-mode-map nil "*Local keymap for tar-mode listings.")
436 (if tar-mode-map
438 (setq tar-mode-map (make-keymap))
439 (suppress-keymap tar-mode-map)
440 (define-key tar-mode-map " " 'tar-next-line)
441 (define-key tar-mode-map "c" 'tar-copy)
442 (define-key tar-mode-map "d" 'tar-flag-deleted)
443 (define-key tar-mode-map "\^D" 'tar-flag-deleted)
444 (define-key tar-mode-map "e" 'tar-extract)
445 (define-key tar-mode-map "f" 'tar-extract)
446 (define-key tar-mode-map "g" 'revert-buffer)
447 (define-key tar-mode-map "h" 'describe-mode)
448 (define-key tar-mode-map "n" 'tar-next-line)
449 (define-key tar-mode-map "\^N" 'tar-next-line)
450 (define-key tar-mode-map "o" 'tar-extract-other-window)
451 (define-key tar-mode-map "\^C" 'tar-copy)
452 (define-key tar-mode-map "p" 'tar-previous-line)
453 (define-key tar-mode-map "\^P" 'tar-previous-line)
454 (define-key tar-mode-map "r" 'tar-rename-entry)
455 (define-key tar-mode-map "u" 'tar-unflag)
456 (define-key tar-mode-map "v" 'tar-view)
457 (define-key tar-mode-map "x" 'tar-expunge)
458 (define-key tar-mode-map "\177" 'tar-unflag-backwards)
459 (define-key tar-mode-map "E" 'tar-extract-other-window)
460 (define-key tar-mode-map "M" 'tar-chmod-entry)
461 (define-key tar-mode-map "G" 'tar-chgrp-entry)
462 (define-key tar-mode-map "O" 'tar-chown-entry)
465 ;; tar mode is suitable only for specially formatted data.
466 (put 'tar-mode 'mode-class 'special)
467 (put 'tar-subfile-mode 'mode-class 'special)
469 (defun tar-mode ()
470 "Major mode for viewing a tar file as a dired-like listing of its contents.
471 You can move around using the usual cursor motion commands.
472 Letters no longer insert themselves.
473 Type 'e' to pull a file out of the tar file and into its own buffer.
474 Type 'c' to copy an entry from the tar file into another file on disk.
476 If you edit a sub-file of this archive (as with the 'e' command) and
477 save it with Control-X Control-S, the contents of that buffer will be
478 saved back into the tar-file buffer; in this way you can edit a file
479 inside of a tar archive without extracting it and re-archiving it.
481 See also: variables tar-update-datestamp and tar-anal-blocksize.
482 \\{tar-mode-map}"
483 ;; this is not interactive because you shouldn't be turning this
484 ;; mode on and off. You can corrupt things that way.
485 (make-local-variable 'tar-header-offset)
486 (make-local-variable 'tar-parse-info)
487 (make-local-variable 'require-final-newline)
488 (setq require-final-newline nil) ; binary data, dude...
489 (make-local-variable 'revert-buffer-function)
490 (setq revert-buffer-function 'tar-mode-revert)
491 (setq major-mode 'tar-mode)
492 (setq mode-name "Tar")
493 (use-local-map tar-mode-map)
494 (auto-save-mode 0)
495 (widen)
496 (if (and (boundp 'tar-header-offset) tar-header-offset)
497 (narrow-to-region 1 tar-header-offset)
498 (tar-summarize-buffer))
499 (run-hooks 'tar-mode-hook)
503 (defun tar-subfile-mode (p)
504 "Minor mode for editing an element of a tar-file.
505 This mode redefines ^X^S to save the current buffer back into its
506 associated tar-file buffer. You must save that buffer to actually
507 save your changes to disk."
508 (interactive "P")
509 (or (and (boundp 'superior-tar-buffer) superior-tar-buffer)
510 (error "This buffer is not an element of a tar file."))
511 (or (assq 'tar-subfile-mode minor-mode-alist)
512 (setq minor-mode-alist (append minor-mode-alist
513 (list '(tar-subfile-mode
514 " TarFile")))))
515 (make-local-variable 'tar-subfile-mode)
516 (setq tar-subfile-mode
517 (if (null p)
518 (not tar-subfile-mode)
519 (> (prefix-numeric-value p) 0)))
520 (cond (tar-subfile-mode
521 ;; copy the local keymap so that we don't accidentally
522 ;; alter a keymap like 'lisp-mode-map' which is shared
523 ;; by all buffers in that mode.
524 (let ((m (current-local-map)))
525 (if m (use-local-map (copy-keymap m))))
526 (local-set-key "\^X\^S" 'tar-subfile-save-buffer)
527 ;; turn off auto-save.
528 (auto-save-mode nil)
529 (setq buffer-auto-save-file-name nil)
530 (run-hooks 'tar-subfile-mode-hook))
531 (t (local-set-key "\^X\^S" 'save-buffer)))
535 (defun tar-mode-revert (&optional no-autosave no-confirm)
536 "Revert this buffer and turn on tar mode again, to re-compute the
537 directory listing."
538 (setq tar-header-offset nil)
539 (let ((revert-buffer-function nil))
540 (revert-buffer t no-confirm)
541 (widen))
542 (tar-mode))
545 (defun tar-next-line (p)
546 (interactive "p")
547 (forward-line p)
548 (if (eobp) nil (forward-char 36)))
550 (defun tar-previous-line (p)
551 (interactive "p")
552 (tar-next-line (- p)))
554 (defun tar-current-descriptor (&optional noerror)
555 "Returns the tar-descriptor of the current line, or signals an error."
556 ;; I wish lines had plists, like in ZMACS...
557 (or (nth (count-lines (point-min)
558 (save-excursion (beginning-of-line) (point)))
559 tar-parse-info)
560 (if noerror
562 (error "This line does not describe a tar-file entry."))))
565 (defun tar-extract (&optional other-window-p)
566 "*In tar-mode, extract this entry of the tar file into its own buffer."
567 (interactive)
568 (let* ((view-p (eq other-window-p 'view))
569 (descriptor (tar-current-descriptor))
570 (tokens (tar-desc-tokens descriptor))
571 (name (tar-header-name tokens))
572 (size (tar-header-size tokens))
573 (link-p (tar-header-link-type tokens))
574 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
575 (end (+ start size)))
576 (if link-p
577 (error "This is a %s, not a real file."
578 (cond ((eq link-p 5) "directory")
579 ((eq link-p 20) "tar directory header")
580 ((eq link-p 29) "multivolume-continuation")
581 ((eq link-p 35) "sparse entry")
582 ((eq link-p 38) "volume header")
583 (t "link"))))
584 (if (zerop size) (error "This is a zero-length file."))
585 (let* ((tar-buffer (current-buffer))
586 (bufname (concat (file-name-nondirectory name)
587 " (" name " in "
588 (file-name-nondirectory (buffer-file-name))
589 ")"))
590 (read-only-p (or buffer-read-only view-p))
591 (buffer (get-buffer bufname))
592 (just-created nil))
593 (if buffer
595 (setq buffer (get-buffer-create bufname))
596 (setq just-created t)
597 (unwind-protect
598 (progn
599 (widen)
600 (save-excursion
601 (set-buffer buffer)
602 (insert-buffer-substring tar-buffer start end)
603 (goto-char 0)
604 (set-visited-file-name name) ; give it a name to decide mode.
605 (normal-mode) ; pick a mode.
606 (set-visited-file-name nil) ; nuke the name - not meaningful.
607 (rename-buffer bufname)
609 (make-local-variable 'superior-tar-buffer)
610 (make-local-variable 'superior-tar-descriptor)
611 (setq superior-tar-buffer tar-buffer)
612 (setq superior-tar-descriptor descriptor)
613 (tar-subfile-mode 1)
615 (setq buffer-read-only read-only-p)
616 (set-buffer-modified-p nil))
617 (set-buffer tar-buffer))
618 (narrow-to-region 1 tar-header-offset)))
619 (if view-p
620 (progn
621 (view-buffer buffer)
622 (and just-created (kill-buffer buffer)))
623 (if other-window-p
624 (switch-to-buffer-other-window buffer)
625 (switch-to-buffer buffer))))))
628 (defun tar-extract-other-window ()
629 "*In tar-mode, extract this entry of the tar file into its own buffer."
630 (interactive)
631 (tar-extract t))
633 (defun tar-view ()
634 "*In tar-mode, view the tar file entry on this line."
635 (interactive)
636 (tar-extract 'view))
639 (defun tar-read-file-name (&optional prompt)
640 "Calls read-file-name, with the default being the file of the current
641 tar-file descriptor."
642 (or prompt (setq prompt "Copy to: "))
643 (let* ((default-file (expand-file-name
644 (tar-header-name (tar-desc-tokens
645 (tar-current-descriptor)))))
646 (target (expand-file-name
647 (read-file-name prompt
648 (file-name-directory default-file)
649 default-file nil))))
650 (if (or (string= "" (file-name-nondirectory target))
651 (file-directory-p target))
652 (setq target (concat (if (string-match "/$" target)
653 (substring target 0 (1- (match-end 0)))
654 target)
656 (file-name-nondirectory default-file))))
657 target))
660 (defun tar-copy (&optional to-file)
661 "*In tar-mode, extract this entry of the tar file into a file on disk.
662 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
663 the current tar-entry."
664 (interactive (list (tar-read-file-name)))
665 (let* ((descriptor (tar-current-descriptor))
666 (tokens (tar-desc-tokens descriptor))
667 (name (tar-header-name tokens))
668 (size (tar-header-size tokens))
669 (link-p (tar-header-link-type tokens))
670 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
671 (end (+ start size)))
672 (if link-p (error "This is a link, not a real file."))
673 (if (zerop size) (error "This is a zero-length file."))
674 (let* ((tar-buffer (current-buffer))
675 buffer)
676 (unwind-protect
677 (progn
678 (setq buffer (generate-new-buffer "*tar-copy-tmp*"))
679 (widen)
680 (save-excursion
681 (set-buffer buffer)
682 (insert-buffer-substring tar-buffer start end)
683 (set-buffer-modified-p nil) ; in case we abort
684 (write-file to-file)
685 (message "Copied tar entry %s to %s" name to-file)
686 (set-buffer tar-buffer)))
687 (narrow-to-region 1 tar-header-offset)
688 (if buffer (kill-buffer buffer)))
692 (defun tar-flag-deleted (p &optional unflag)
693 "*In tar mode, mark this sub-file to be deleted from the tar file.
694 With a prefix argument, mark that many files."
695 (interactive "p")
696 (beginning-of-line)
697 (tar-dotimes (i (if (< p 0) (- p) p))
698 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
699 (progn
700 (delete-char 1)
701 (insert (if unflag " " "D"))))
702 (forward-line (if (< p 0) -1 1)))
703 (if (eobp) nil (forward-char 36)))
705 (defun tar-unflag (p)
706 "*In tar mode, un-mark this sub-file if it is marked to be deleted.
707 With a prefix argument, un-mark that many files forward."
708 (interactive "p")
709 (tar-flag-deleted p t))
711 (defun tar-unflag-backwards (p)
712 "*In tar mode, un-mark this sub-file if it is marked to be deleted.
713 With a prefix argument, un-mark that many files backward."
714 (interactive "p")
715 (tar-flag-deleted (- p) t))
718 (defun tar-expunge-internal ()
719 "Expunge the tar-entry specified by the current line."
720 (let* ((descriptor (tar-current-descriptor))
721 (tokens (tar-desc-tokens descriptor))
722 (line (tar-desc-data-start descriptor))
723 (name (tar-header-name tokens))
724 (size (tar-header-size tokens))
725 (link-p (tar-header-link-type tokens))
726 (start (tar-desc-data-start descriptor))
727 (following-descs (cdr (memq descriptor tar-parse-info))))
728 (if link-p (setq size 0)) ; size lies for hard-links.
730 ;; delete the current line...
731 (beginning-of-line)
732 (let ((line-start (point)))
733 (end-of-line) (forward-char)
734 (let ((line-len (- (point) line-start)))
735 (delete-region line-start (point))
737 ;; decrement the header-pointer to be in synch...
738 (setq tar-header-offset (- tar-header-offset line-len))))
740 ;; delete the data pointer...
741 (setq tar-parse-info (delq descriptor tar-parse-info))
743 ;; delete the data from inside the file...
744 (widen)
745 (let* ((data-start (+ start tar-header-offset -513))
746 (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
747 (delete-region data-start data-end)
749 ;; and finally, decrement the start-pointers of all following
750 ;; entries in the archive. This is a pig when deleting a bunch
751 ;; of files at once - we could optimize this to only do the
752 ;; iteration over the files that remain, or only iterate up to
753 ;; the next file to be deleted.
754 (let ((data-length (- data-end data-start)))
755 (tar-dolist (desc following-descs)
756 (tar-setf (tar-desc-data-start desc)
757 (- (tar-desc-data-start desc) data-length))))
759 (narrow-to-region 1 tar-header-offset))
762 (defun tar-expunge (&optional noconfirm)
763 "*In tar-mode, delete all the archived files flagged for deletion.
764 This does not modify the disk image; you must save the tar file itself
765 for this to be permanent."
766 (interactive)
767 (if (or noconfirm
768 (y-or-n-p "expunge files marked for deletion? "))
769 (let ((n 0))
770 (save-excursion
771 (goto-char 0)
772 (while (not (eobp))
773 (if (looking-at "D")
774 (progn (tar-expunge-internal)
775 (setq n (1+ n)))
776 (forward-line 1)))
777 ;; after doing the deletions, add any padding that may be necessary.
778 (tar-pad-to-blocksize)
779 (narrow-to-region 1 tar-header-offset)
781 (if (zerop n)
782 (message "nothing to expunge.")
783 (message "%s expunged. Be sure to save this buffer." n)))))
786 (defun tar-clear-modification-flags ()
787 "remove the stars at the beginning of each line."
788 (save-excursion
789 (goto-char 0)
790 (while (< (point) tar-header-offset)
791 (if (looking-at "*")
792 (progn (delete-char 1) (insert " ")))
793 (forward-line 1))))
796 (defun tar-chown-entry (new-uid)
797 "*Change the user-id associated with this entry in the tar file.
798 If this tar file was written by GNU tar, then you will be able to edit
799 the user id as a string; otherwise, you must edit it as a number.
800 You can force editing as a number by calling this with a prefix arg.
801 This does not modify the disk image; you must save the tar file itself
802 for this to be permanent."
803 (interactive (list
804 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
805 (if (or current-prefix-arg
806 (not (tar-header-magic tokens)))
807 (let (n)
808 (while (not (numberp (setq n (read-minibuffer
809 "New UID number: "
810 (format "%s" (tar-header-uid tokens)))))))
812 (read-string "New UID string: " (tar-header-uname tokens))))))
813 (cond ((stringp new-uid)
814 (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
815 new-uid)
816 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
818 (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
819 new-uid)
820 (tar-alter-one-field tar-uid-offset
821 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
824 (defun tar-chgrp-entry (new-gid)
825 "*Change the group-id associated with this entry in the tar file.
826 If this tar file was written by GNU tar, then you will be able to edit
827 the group id as a string; otherwise, you must edit it as a number.
828 You can force editing as a number by calling this with a prefix arg.
829 This does not modify the disk image; you must save the tar file itself
830 for this to be permanent."
831 (interactive (list
832 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
833 (if (or current-prefix-arg
834 (not (tar-header-magic tokens)))
835 (let (n)
836 (while (not (numberp (setq n (read-minibuffer
837 "New GID number: "
838 (format "%s" (tar-header-gid tokens)))))))
840 (read-string "New GID string: " (tar-header-gname tokens))))))
841 (cond ((stringp new-gid)
842 (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
843 new-gid)
844 (tar-alter-one-field tar-gname-offset
845 (concat new-gid "\000")))
847 (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
848 new-gid)
849 (tar-alter-one-field tar-gid-offset
850 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
852 (defun tar-rename-entry (new-name)
853 "*Change the name associated with this entry in the tar file.
854 This does not modify the disk image; you must save the tar file itself
855 for this to be permanent."
856 (interactive
857 (list (read-string "New name: "
858 (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
859 (if (string= "" new-name) (error "zero length name."))
860 (if (> (length new-name) 98) (error "name too long."))
861 (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
862 new-name)
863 (tar-alter-one-field 0
864 (substring (concat new-name (make-string 99 0)) 0 99)))
867 (defun tar-chmod-entry (new-mode)
868 "*Change the protection bits associated with this entry in the tar file.
869 This does not modify the disk image; you must save the tar file itself
870 for this to be permanent."
871 (interactive (list (tar-parse-octal-integer-safe
872 (read-string "New protection (octal): "))))
873 (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
874 new-mode)
875 (tar-alter-one-field tar-mode-offset
876 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
879 (defun tar-alter-one-field (data-position new-data-string)
880 (let* ((descriptor (tar-current-descriptor))
881 (tokens (tar-desc-tokens descriptor)))
882 (unwind-protect
883 (save-excursion
885 ;; update the header-line.
886 (beginning-of-line)
887 (let ((p (point)))
888 (forward-line 1)
889 (delete-region p (point))
890 (insert (summarize-tar-header-block tokens) "\n")
891 (setq tar-header-offset (point-max)))
893 (widen)
894 (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
896 ;; delete the old field and insert a new one.
897 (goto-char (+ start data-position))
898 (delete-region (point) (+ (point) (length new-data-string))) ; <--
899 (insert new-data-string) ; <--
901 ;; compute a new checksum and insert it.
902 (let ((chk (checksum-tar-header-block
903 (buffer-substring start (+ start 512)))))
904 (goto-char (+ start tar-chk-offset))
905 (delete-region (point) (+ (point) 8))
906 (insert (format "%6o" chk))
907 (insert 0)
908 (insert ? )
909 (tar-setf (tar-header-checksum tokens) chk)
911 ;; ok, make sure we didn't botch it.
912 (check-tar-header-block-checksum
913 (buffer-substring start (+ start 512))
914 chk (tar-header-name tokens))
916 (narrow-to-region 1 tar-header-offset))))
919 (defun tar-subfile-save-buffer ()
920 "In tar subfile mode, write this buffer back into its parent tar-file buffer.
921 This doesn't write anything to disk - you must save the parent tar-file buffer
922 to make your changes permanent."
923 (interactive)
924 (if (not (and (boundp 'superior-tar-buffer) superior-tar-buffer))
925 (error "this buffer has no superior tar file buffer."))
926 (if (not (and (boundp 'superior-tar-descriptor) superior-tar-descriptor))
927 (error "this buffer doesn't have an index into its superior tar file!"))
928 (save-excursion
929 (let ((subfile (current-buffer))
930 (subfile-size (buffer-size))
931 (descriptor superior-tar-descriptor))
932 (set-buffer superior-tar-buffer)
933 (let* ((tokens (tar-desc-tokens descriptor))
934 (start (tar-desc-data-start descriptor))
935 (name (tar-header-name tokens))
936 (size (tar-header-size tokens))
937 (size-pad (ash (ash (+ size 511) -9) 9))
938 (head (memq descriptor tar-parse-info))
939 (following-descs (cdr head)))
940 (if (not head)
941 (error "Can't find this tar file entry in its parent tar file!"))
942 (unwind-protect
943 (save-excursion
944 (widen)
945 ;; delete the old data...
946 (let* ((data-start (+ start tar-header-offset -1))
947 (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
948 (delete-region data-start data-end)
949 ;; insert the new data...
950 (goto-char data-start)
951 (insert-buffer subfile)
953 ;; pad the new data out to a multiple of 512...
954 (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
955 (goto-char (+ data-start subfile-size))
956 (insert (make-string (- subfile-size-pad subfile-size) 0))
958 ;; update the data pointer of this and all following files...
959 (tar-setf (tar-header-size tokens) subfile-size)
960 (let ((difference (- subfile-size-pad size-pad)))
961 (tar-dolist (desc following-descs)
962 (tar-setf (tar-desc-data-start desc)
963 (+ (tar-desc-data-start desc) difference))))
965 ;; Update the size field in the header block.
966 (let ((header-start (- data-start 512)))
967 (goto-char (+ header-start tar-size-offset))
968 (delete-region (point) (+ (point) 12))
969 (insert (format "%11o" subfile-size))
970 (insert ? )
972 ;; Maybe update the datestamp.
973 (if (not tar-update-datestamp)
975 (goto-char (+ header-start tar-time-offset))
976 (delete-region (point) (+ (point) 12))
977 (insert (format "%11o" (current-time)))
978 (insert ? ))
980 ;; compute a new checksum and insert it.
981 (let ((chk (checksum-tar-header-block
982 (buffer-substring header-start data-start))))
983 (goto-char (+ header-start tar-chk-offset))
984 (delete-region (point) (+ (point) 8))
985 (insert (format "%6o" chk))
986 (insert 0)
987 (insert ? )
988 (tar-setf (tar-header-checksum tokens) chk)))
990 ;; alter the descriptor-line...
992 (let ((position (- (length tar-parse-info) (length head))))
993 (goto-char 1)
994 (next-line position)
995 (beginning-of-line)
996 (let ((p (point))
997 (m (set-marker (make-marker) tar-header-offset)))
998 (forward-line 1)
999 (delete-region p (point))
1000 (insert-before-markers (summarize-tar-header-block tokens t) "\n")
1001 (setq tar-header-offset (marker-position m)))
1003 ;; after doing the insertion, add any final padding that may be necessary.
1004 (tar-pad-to-blocksize))
1005 (narrow-to-region 1 tar-header-offset)))
1006 (set-buffer-modified-p t) ; mark the tar file as modified
1007 (set-buffer subfile)
1008 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1009 (message "saved into tar-buffer \"%s\" - remember to save that buffer!"
1010 (buffer-name superior-tar-buffer))
1014 (defun tar-pad-to-blocksize ()
1015 "If we are being anal about tar file blocksizes, fix up the current buffer.
1016 Leaves the region wide."
1017 (if (null tar-anal-blocksize)
1019 (widen)
1020 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1021 (start (tar-desc-data-start last-desc))
1022 (tokens (tar-desc-tokens last-desc))
1023 (link-p (tar-header-link-type tokens))
1024 (size (if link-p 0 (tar-header-size tokens)))
1025 (data-end (+ start size))
1026 (bbytes (ash tar-anal-blocksize 9))
1027 (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
1028 (buffer-read-only nil) ; ##
1030 ;; If the padding after the last data is too long, delete some;
1031 ;; else insert some until we are padded out to the right number of blocks.
1033 (goto-char (+ (or tar-header-offset 0) data-end))
1034 (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
1035 (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
1036 (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
1037 (1+ (buffer-size)))
1038 0)))
1042 (defun maybe-write-tar-file ()
1043 "Used as a write-file-hook to write tar-files out correctly."
1045 ;; If the current buffer is in tar-mode and has its header-offset set,
1046 ;; only write out the part of the file after the header-offset.
1048 (if (and (eq major-mode 'tar-mode)
1049 (and (boundp 'tar-header-offset) tar-header-offset))
1050 (unwind-protect
1051 (save-excursion
1052 (tar-clear-modification-flags)
1053 (widen)
1054 ;; Doing this here confuses things - the region gets left too wide!
1055 ;; I suppose this is run in a context where changing the buffer is bad.
1056 ;; (tar-pad-to-blocksize)
1057 (write-region tar-header-offset (1+ (buffer-size)) buffer-file-name nil t)
1058 ;; return T because we've written the file.
1060 (narrow-to-region 1 tar-header-offset)
1062 ;; return NIL because we haven't.
1063 nil))
1066 ;;; Patch it in.
1068 (defvar tar-regexp "\\.tar$"
1069 "The regular expression used to identify tar file names.")
1071 (setq auto-mode-alist
1072 (cons (cons tar-regexp 'tar-mode) auto-mode-alist))
1074 (or (boundp 'write-file-hooks) (setq write-file-hooks nil))
1075 (or (listp write-file-hooks)
1076 (setq write-file-hooks (list write-file-hooks)))
1077 (or (memq 'maybe-write-tar-file write-file-hooks)
1078 (setq write-file-hooks
1079 (cons 'maybe-write-tar-file write-file-hooks)))
1082 ;;; This is a hack. For files ending in .tar, we want -*- lines to be
1083 ;;; completely ignored - if there is one, it applies to the first file
1084 ;;; in the archive, and not the archive itself!
1086 (defun tar-normal-mode (&optional find-file)
1087 "Choose the major mode for this buffer automatically.
1088 Also sets up any specified local variables of the file.
1089 Uses the visited file name, the -*- line, and the local variables spec.
1091 This function is called automatically from `find-file'. In that case,
1092 if `inhibit-local-variables' is non-`nil' we require confirmation before
1093 processing a local variables spec. If you run `normal-mode' explicitly,
1094 confirmation is never required.
1096 Note that this version of this function has been hacked to interact
1097 correctly with tar files - when visiting a file which matches
1098 'tar-regexp', the -*- line and local-variables are not examined,
1099 as they would apply to a file within the archive rather than the archive
1100 itself."
1101 (interactive)
1102 (if (and buffer-file-name
1103 (string-match tar-regexp buffer-file-name))
1104 (tar-mode)
1105 (tar-real-normal-mode find-file)))
1108 (if (not (fboundp 'tar-real-normal-mode))
1109 (fset 'tar-real-normal-mode (symbol-function 'normal-mode)))
1110 (fset 'normal-mode 'tar-normal-mode)
1112 (provide 'tar-mode)
1114 ;;; tar-mode.el ends here