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