1 ;;; tar-mode.el --- simple editing of tar files from GNU emacs
3 ;; Copyright (C) 1990, 1991, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;; Created: 04 Apr 1990
10 ;; This file is part of GNU Emacs.
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)
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.
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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
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.
40 ;; This code now understands the extra fields that GNU tar adds to tar files.
42 ;; This interacts correctly with "uncompress.el" in the Emacs library,
45 ;; (autoload 'uncompress-while-visiting "uncompress")
46 ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
49 ;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
51 ;; *************** TO DO ***************
53 ;; o chmod should understand "a+x,og-w".
55 ;; o It's not possible to add a NEW file to a tar archive; not that
56 ;; important, but still...
58 ;; o The code is less efficient that it could be - in a lot of places, I
59 ;; pull a 512-character string out of the buffer and parse it, when I could
60 ;; be parsing it in place, not garbaging a string. Should redo that.
62 ;; o I'd like a command that searches for a string/regexp in every subfile
63 ;; of an archive, where <esc> would leave you in a subfile-edit buffer.
64 ;; (Like the Meta-R command of the Zmacs mail reader.)
66 ;; o Sometimes (but not always) reverting the tar-file buffer does not
67 ;; re-grind the listing, and you are staring at the binary tar data.
68 ;; Typing 'g' again immediately after that will always revert and re-grind
69 ;; it, though. I have no idea why this happens.
71 ;; o Tar-mode interacts poorly with crypt.el and zcat.el because the tar
72 ;; write-file-hook actually writes the file. Instead it should remove the
73 ;; header (and conspire to put it back afterwards) so that other write-file
74 ;; hooks which frob the buffer have a chance to do their dirty work. There
75 ;; might be a problem if the tar write-file-hook does not come *first* on
78 ;; o Block files, sparse files, continuation files, and the various header
79 ;; types aren't editable. Actually I don't know that they work at all.
83 ;; Why does tar-mode edit the file itself instead of using tar?
85 ;; That means that you can edit tar files which you don't have room for
86 ;; on your local disk.
88 ;; I don't know about recent features in gnu tar, but old versions of tar
89 ;; can't replace a file in the middle of a tar file with a new version.
90 ;; Tar-mode can. I don't think tar can do things like chmod the subfiles.
91 ;; An implementation which involved unpacking and repacking the file into
92 ;; some scratch directory would be very wasteful, and wouldn't be able to
93 ;; preserve the file owners.
98 "Simple editing of tar files."
102 (defcustom tar-anal-blocksize
20
103 "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
104 The blocksize of a tar file is not really the size of the blocks; rather, it is
105 the number of blocks written with one system call. When tarring to a tape,
106 this is the size of the *tape* blocks, but when writing to a file, it doesn't
107 matter much. The only noticeable difference is that if a tar file does not
108 have a blocksize of 20, tar will tell you that; all this really controls is
109 how many null padding bytes go on the end of the tar file."
110 :type
'(choice integer
(const nil
))
113 (defcustom tar-update-datestamp nil
114 "*Non-nil means Tar mode should play fast and loose with sub-file datestamps.
115 If this is true, then editing and saving a tar file entry back into its
116 tar file will update its datestamp. If false, the datestamp is unchanged.
117 You may or may not want this - it is good in that you can tell when a file
118 in a tar archive has been changed, but it is bad for the same reason that
119 editing a file in the tar archive at all is bad - the changed version of
120 the file never exists on disk."
124 (defcustom tar-mode-show-date nil
125 "*Non-nil means Tar mode should show the date/time of each subfile.
126 This information is useful, but it takes screen space away from file names."
130 (defvar tar-parse-info nil
)
131 ;; Be sure that this variable holds byte position, not char position.
132 (defvar tar-header-offset nil
)
133 (defvar tar-superior-buffer nil
)
134 (defvar tar-superior-descriptor nil
)
135 (defvar tar-subfile-mode nil
)
137 (put 'tar-parse-info
'permanent-local t
)
138 (put 'tar-header-offset
'permanent-local t
)
139 (put 'tar-superior-buffer
'permanent-local t
)
140 (put 'tar-superior-descriptor
'permanent-local t
)
142 ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
143 ;;; but "cl.el" was messing some people up (also it's really big).
145 (defmacro tar-setf
(form val
)
146 "A mind-numbingly simple implementation of setf."
147 (let ((mform (macroexpand form
(and (boundp 'byte-compile-macro-environment
)
148 byte-compile-macro-environment
))))
149 (cond ((symbolp mform
) (list 'setq mform val
))
150 ((not (consp mform
)) (error "can't setf %s" form
))
151 ((eq (car mform
) 'aref
)
152 (list 'aset
(nth 1 mform
) (nth 2 mform
) val
))
153 ((eq (car mform
) 'car
)
154 (list 'setcar
(nth 1 mform
) val
))
155 ((eq (car mform
) 'cdr
)
156 (list 'setcdr
(nth 1 mform
) val
))
157 (t (error "don't know how to setf %s" form
)))))
159 (defmacro tar-dolist
(control &rest body
)
160 "syntax: (dolist (var-name list-expr &optional return-value) &body body)"
161 (let ((var (car control
))
162 (init (car (cdr control
)))
163 (val (car (cdr (cdr control
)))))
164 (list 'let
(list (list '_dolist_iterator_ init
))
165 (list 'while
'_dolist_iterator_
167 (cons (list (list var
'(car _dolist_iterator_
)))
169 (list (list 'setq
'_dolist_iterator_
170 (list 'cdr
'_dolist_iterator_
)))))))
173 (defmacro tar-dotimes
(control &rest body
)
174 "syntax: (dolist (var-name count-expr &optional return-value) &body body)"
175 (let ((var (car control
))
176 (n (car (cdr control
)))
177 (val (car (cdr (cdr control
)))))
178 (list 'let
(list (list '_dotimes_end_ n
)
181 (cons (list '< var
'_dotimes_end_
)
183 (list (list 'setq var
(list '1+ var
))))))
187 ;;; down to business.
189 (defmacro make-tar-header
(name mode uid git size date ck lt ln
190 magic uname gname devmaj devmin
)
191 (list 'vector name mode uid git size date ck lt ln
192 magic uname gname devmaj devmin
))
194 (defmacro tar-header-name
(x) (list 'aref x
0))
195 (defmacro tar-header-mode
(x) (list 'aref x
1))
196 (defmacro tar-header-uid
(x) (list 'aref x
2))
197 (defmacro tar-header-gid
(x) (list 'aref x
3))
198 (defmacro tar-header-size
(x) (list 'aref x
4))
199 (defmacro tar-header-date
(x) (list 'aref x
5))
200 (defmacro tar-header-checksum
(x) (list 'aref x
6))
201 (defmacro tar-header-link-type
(x) (list 'aref x
7))
202 (defmacro tar-header-link-name
(x) (list 'aref x
8))
203 (defmacro tar-header-magic
(x) (list 'aref x
9))
204 (defmacro tar-header-uname
(x) (list 'aref x
10))
205 (defmacro tar-header-gname
(x) (list 'aref x
11))
206 (defmacro tar-header-dmaj
(x) (list 'aref x
12))
207 (defmacro tar-header-dmin
(x) (list 'aref x
13))
209 (defmacro make-tar-desc
(data-start tokens
)
210 (list 'cons data-start tokens
))
212 (defmacro tar-desc-data-start
(x) (list 'car x
))
213 (defmacro tar-desc-tokens
(x) (list 'cdr x
))
215 (defconst tar-name-offset
0)
216 (defconst tar-mode-offset
(+ tar-name-offset
100))
217 (defconst tar-uid-offset
(+ tar-mode-offset
8))
218 (defconst tar-gid-offset
(+ tar-uid-offset
8))
219 (defconst tar-size-offset
(+ tar-gid-offset
8))
220 (defconst tar-time-offset
(+ tar-size-offset
12))
221 (defconst tar-chk-offset
(+ tar-time-offset
12))
222 (defconst tar-linkp-offset
(+ tar-chk-offset
8))
223 (defconst tar-link-offset
(+ tar-linkp-offset
1))
224 ;;; GNU-tar specific slots.
225 (defconst tar-magic-offset
(+ tar-link-offset
100))
226 (defconst tar-uname-offset
(+ tar-magic-offset
8))
227 (defconst tar-gname-offset
(+ tar-uname-offset
32))
228 (defconst tar-dmaj-offset
(+ tar-gname-offset
32))
229 (defconst tar-dmin-offset
(+ tar-dmaj-offset
8))
230 (defconst tar-end-offset
(+ tar-dmin-offset
8))
232 (defun tar-header-block-tokenize (string)
233 "Return a `tar-header' structure.
234 This is a list of name, mode, uid, gid, size,
235 write-date, checksum, link-type, and link-name."
236 (cond ((< (length string
) 512) nil
)
237 (;(some 'plusp string) ; <-- oops, massive cycle hog!
238 (or (not (= 0 (aref string
0))) ; This will do.
239 (not (= 0 (aref string
101))))
240 (let* ((name-end (1- tar-mode-offset
))
241 (link-end (1- tar-magic-offset
))
242 (uname-end (1- tar-gname-offset
))
243 (gname-end (1- tar-dmaj-offset
))
244 (link-p (aref string tar-linkp-offset
))
245 (magic-str (substring string tar-magic-offset
(1- tar-uname-offset
)))
246 (uname-valid-p (or (string= "ustar " magic-str
) (string= "GNUtar " magic-str
)))
248 (nulsexp "[^\000]*\000"))
249 (when (string-match nulsexp string tar-name-offset
)
250 (setq name-end
(min name-end
(1- (match-end 0)))))
251 (when (string-match nulsexp string tar-link-offset
)
252 (setq link-end
(min link-end
(1- (match-end 0)))))
253 (when (string-match nulsexp string tar-uname-offset
)
254 (setq uname-end
(min uname-end
(1- (match-end 0)))))
255 (when (string-match nulsexp string tar-gname-offset
)
256 (setq gname-end
(min gname-end
(1- (match-end 0)))))
257 (setq name
(substring string tar-name-offset name-end
)
258 link-p
(if (or (= link-p
0) (= link-p ?
0))
261 (setq linkname
(substring string tar-link-offset link-end
))
262 (if default-enable-multibyte-characters
264 (decode-coding-string name
(or file-name-coding-system
267 (decode-coding-string linkname
(or file-name-coding-system
269 (if (and (null link-p
) (string-match "/$" name
)) (setq link-p
5)) ; directory
272 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset
)
273 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset
)
274 (tar-parse-octal-integer string tar-gid-offset tar-size-offset
)
275 (tar-parse-octal-integer string tar-size-offset tar-time-offset
)
276 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset
)
277 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset
)
281 (and uname-valid-p
(substring string tar-uname-offset uname-end
))
282 (and uname-valid-p
(substring string tar-gname-offset gname-end
))
283 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset
)
284 (tar-parse-octal-integer string tar-dmin-offset tar-end-offset
)
286 (t 'empty-tar-block
)))
289 (defun tar-parse-octal-integer (string &optional start end
)
290 (if (null start
) (setq start
0))
291 (if (null end
) (setq end
(length string
)))
292 (if (= (aref string start
) 0)
296 (setq n
(if (< (aref string start
) ?
0) n
297 (+ (* n
8) (- (aref string start
) ?
0)))
301 (defun tar-parse-octal-long-integer (string &optional start end
)
302 (if (null start
) (setq start
0))
303 (if (null end
) (setq end
(length string
)))
304 (if (= (aref string start
) 0)
309 (if (>= (aref string start
) ?
0)
310 (setq lo
(+ (* lo
8) (- (aref string start
) ?
0))
311 hi
(+ (* hi
8) (ash lo -
16))
312 lo
(logand lo
65535)))
313 (setq start
(1+ start
)))
316 (defun tar-parse-octal-integer-safe (string)
317 (let ((L (length string
)))
318 (if (= L
0) (error "empty string"))
320 (if (or (< (aref string i
) ?
0)
321 (> (aref string i
) ?
7))
322 (error "`%c' is not an octal digit"))))
323 (tar-parse-octal-integer string
))
326 (defun tar-header-block-checksum (string)
327 "Compute and return a tar-acceptable checksum for this block."
328 (let* ((chk-field-start tar-chk-offset
)
329 (chk-field-end (+ chk-field-start
8))
332 ;; Add up all of the characters except the ones in the checksum field.
333 ;; Add that field as if it were filled with spaces.
334 (while (< i chk-field-start
)
335 (setq sum
(+ sum
(aref string i
))
337 (setq i chk-field-end
)
339 (setq sum
(+ sum
(aref string i
))
343 (defun tar-header-block-check-checksum (hblock desired-checksum file-name
)
344 "Beep and print a warning if the checksum doesn't match."
345 (if (not (= desired-checksum
(tar-header-block-checksum hblock
)))
346 (progn (beep) (message "Invalid checksum for file %s!" file-name
))))
348 (defun tar-header-block-recompute-checksum (hblock)
349 "Modifies the given string to have a valid checksum field."
350 (let* ((chk (tar-header-block-checksum hblock
))
351 (chk-string (format "%6o" chk
))
352 (l (length chk-string
)))
355 (tar-dotimes (i l
) (aset hblock
(- 153 i
) (aref chk-string
(- l i
1)))))
358 (defun tar-clip-time-string (time)
359 (let ((str (current-time-string time
)))
360 (concat (substring str
4 16) (substring str
19 24))))
362 (defun tar-grind-file-mode (mode string start
)
363 "Store `-rw--r--r--' indicating MODE into STRING beginning at START.
364 MODE should be an integer which is a file mode value."
365 (aset string start
(if (zerop (logand 256 mode
)) ?- ?r
))
366 (aset string
(+ start
1) (if (zerop (logand 128 mode
)) ?- ?w
))
367 (aset string
(+ start
2) (if (zerop (logand 64 mode
)) ?- ?x
))
368 (aset string
(+ start
3) (if (zerop (logand 32 mode
)) ?- ?r
))
369 (aset string
(+ start
4) (if (zerop (logand 16 mode
)) ?- ?w
))
370 (aset string
(+ start
5) (if (zerop (logand 8 mode
)) ?- ?x
))
371 (aset string
(+ start
6) (if (zerop (logand 4 mode
)) ?- ?r
))
372 (aset string
(+ start
7) (if (zerop (logand 2 mode
)) ?- ?w
))
373 (aset string
(+ start
8) (if (zerop (logand 1 mode
)) ?- ?x
))
374 (if (zerop (logand 1024 mode
)) nil
(aset string
(+ start
2) ?s
))
375 (if (zerop (logand 2048 mode
)) nil
(aset string
(+ start
5) ?s
))
378 (defun tar-header-block-summarize (tar-hblock &optional mod-p
)
379 "Returns a line similar to the output of `tar -vtf'."
380 (let ((name (tar-header-name tar-hblock
))
381 (mode (tar-header-mode tar-hblock
))
382 (uid (tar-header-uid tar-hblock
))
383 (gid (tar-header-gid tar-hblock
))
384 (uname (tar-header-uname tar-hblock
))
385 (gname (tar-header-gname tar-hblock
))
386 (size (tar-header-size tar-hblock
))
387 (time (tar-header-date tar-hblock
))
388 (ck (tar-header-checksum tar-hblock
))
389 (link-p (tar-header-link-type tar-hblock
))
390 (link-name (tar-header-link-name tar-hblock
))
396 (datew (if tar-mode-show-date
18 0))
397 (slash (1- (+ left namew
)))
398 (lastdigit (+ slash groupw sizew
))
399 (datestart (+ lastdigit
2))
400 (namestart (+ datestart datew
))
401 (multibyte (or (multibyte-string-p name
)
402 (multibyte-string-p link-name
)))
403 ;; If multibyte, we can't use optimized method of aset,
404 ;; instead we must use concat.
405 (string (make-string (if multibyte
409 (if link-p
(+ 5 (length link-name
)) 0)))
411 (type (tar-header-link-type tar-hblock
)))
412 (aset string
0 (if mod-p ?
* ?
))
414 (cond ((or (eq type nil
) (eq type
0)) ?-
)
415 ((eq type
1) ?l
) ; link
416 ((eq type
2) ?s
) ; symlink
417 ((eq type
3) ?c
) ; char special
418 ((eq type
4) ?b
) ; block special
419 ((eq type
5) ?d
) ; directory
420 ((eq type
6) ?p
) ; FIFO/pipe
421 ((eq type
20) ?
*) ; directory listing
422 ((eq type
29) ?M
) ; multivolume continuation
423 ((eq type
35) ?S
) ; sparse
424 ((eq type
38) ?V
) ; volume header
426 (tar-grind-file-mode mode string
2)
427 (setq uid
(if (= 0 (length uname
)) (int-to-string uid
) uname
))
428 (setq gid
(if (= 0 (length gname
)) (int-to-string gid
) gname
))
429 (setq size
(int-to-string size
))
430 (setq time
(tar-clip-time-string time
))
431 (tar-dotimes (i (min (1- namew
) (length uid
))) (aset string
(- slash i
) (aref uid
(- (length uid
) i
1))))
432 (aset string
(1+ slash
) ?
/)
433 (tar-dotimes (i (min (1- groupw
) (length gid
))) (aset string
(+ (+ slash
2) i
) (aref gid i
)))
434 (tar-dotimes (i (min sizew
(length size
))) (aset string
(- lastdigit i
) (aref size
(- (length size
) i
1))))
435 (if tar-mode-show-date
436 (tar-dotimes (i (length time
)) (aset string
(+ datestart i
) (aref time i
))))
438 (setq string
(concat string name
))
439 (tar-dotimes (i (length name
)) (aset string
(+ namestart i
) (aref name i
))))
440 (if (or (eq link-p
1) (eq link-p
2))
442 (setq string
(concat string
443 (if (= link-p
1) " ==> " " --> ")
445 (tar-dotimes (i 3) (aset string
(+ namestart
1 (length name
) i
) (aref (if (= link-p
1) "==>" "-->") i
)))
446 (tar-dotimes (i (length link-name
)) (aset string
(+ namestart
5 (length name
) i
) (aref link-name i
)))))
447 (put-text-property namestart
(length string
)
448 'mouse-face
'highlight string
)
452 (defun tar-summarize-buffer ()
453 "Parse the contents of the tar file in the current buffer.
454 Place a dired-like listing on the front;
455 then narrow to it, so that only that listing
456 is visible (and the real data of the buffer is hidden)."
457 (set-buffer-multibyte nil
)
458 (message "Parsing tar file...")
461 (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
462 (bs100 (max 1 (/ bs
100)))
464 (while (and (<= (+ pos
512) (point-max))
465 (not (eq 'empty-tar-block
467 (tar-header-block-tokenize
468 (buffer-substring pos
(+ pos
512)))))))
469 (setq pos
(+ pos
512))
470 (message "Parsing tar file...%d%%"
471 ;(/ (* pos 100) bs) ; this gets round-off lossage
472 (/ pos bs100
) ; this doesn't
474 (if (eq (tar-header-link-type tokens
) 20)
475 ;; Foo. There's an extra empty block after these.
476 (setq pos
(+ pos
512)))
477 (let ((size (tar-header-size tokens
)))
479 (error "%s has size %s - corrupted"
480 (tar-header-name tokens
) size
))
482 ; This is just too slow. Don't really need it anyway....
483 ;(tar-header-block-check-checksum
484 ; hblock (tar-header-block-checksum hblock)
485 ; (tar-header-name tokens))
487 (setq result
(cons (make-tar-desc pos tokens
) result
))
489 (and (null (tar-header-link-type tokens
))
492 (+ pos
512 (ash (ash (1- size
) -
9) 9)) ; this works
493 ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
495 (make-local-variable 'tar-parse-info
)
496 (setq tar-parse-info
(nreverse result
))
497 ;; A tar file should end with a block or two of nulls,
498 ;; but let's not get a fatal error if it doesn't.
499 (if (eq tokens
'empty-tar-block
)
500 (message "Parsing tar file...done")
501 (message "Warning: premature EOF parsing tar file")))
503 (goto-char (point-min))
504 (let ((buffer-read-only nil
)
506 ;; Collect summary lines and insert them all at once since tar files
507 ;; can be pretty big.
508 (tar-dolist (tar-desc (reverse tar-parse-info
))
510 (cons (tar-header-block-summarize (tar-desc-tokens tar-desc
))
513 (let ((total-summaries (apply 'concat summaries
)))
514 (if (multibyte-string-p total-summaries
)
515 (set-buffer-multibyte t
))
516 (insert total-summaries
))
517 (make-local-variable 'tar-header-offset
)
518 (setq tar-header-offset
(point))
519 (narrow-to-region 1 tar-header-offset
)
520 (if enable-multibyte-characters
521 (setq tar-header-offset
(position-bytes tar-header-offset
)))
522 (set-buffer-modified-p nil
))))
524 (defvar tar-mode-map nil
"*Local keymap for Tar mode listings.")
528 (setq tar-mode-map
(make-keymap))
529 (suppress-keymap tar-mode-map
)
530 (define-key tar-mode-map
" " 'tar-next-line
)
531 (define-key tar-mode-map
"C" 'tar-copy
)
532 (define-key tar-mode-map
"d" 'tar-flag-deleted
)
533 (define-key tar-mode-map
"\^D" 'tar-flag-deleted
)
534 (define-key tar-mode-map
"e" 'tar-extract
)
535 (define-key tar-mode-map
"f" 'tar-extract
)
536 (define-key tar-mode-map
"\C-m" 'tar-extract
)
537 (define-key tar-mode-map
[mouse-2
] 'tar-mouse-extract
)
538 (define-key tar-mode-map
"g" 'revert-buffer
)
539 (define-key tar-mode-map
"h" 'describe-mode
)
540 (define-key tar-mode-map
"n" 'tar-next-line
)
541 (define-key tar-mode-map
"\^N" 'tar-next-line
)
542 (define-key tar-mode-map
[down] 'tar-next-line)
543 (define-key tar-mode-map "o" 'tar-extract-other-window)
544 (define-key tar-mode-map "p" 'tar-previous-line)
545 (define-key tar-mode-map "q" 'tar-quit)
546 (define-key tar-mode-map "\^P" 'tar-previous-line)
547 (define-key tar-mode-map [up] 'tar-previous-line)
548 (define-key tar-mode-map "R" 'tar-rename-entry)
549 (define-key tar-mode-map "u" 'tar-unflag)
550 (define-key tar-mode-map "v" 'tar-view)
551 (define-key tar-mode-map "x" 'tar-expunge)
552 (define-key tar-mode-map "\177" 'tar-unflag-backwards)
553 (define-key tar-mode-map "E" 'tar-extract-other-window)
554 (define-key tar-mode-map "M" 'tar-chmod-entry)
555 (define-key tar-mode-map "G" 'tar-chgrp-entry)
556 (define-key tar-mode-map "O" 'tar-chown-entry)
559 ;; Make menu bar items.
561 ;; Get rid of the Edit menu bar item to save space.
562 (define-key tar-mode-map [menu-bar edit] 'undefined)
564 (define-key tar-mode-map [menu-bar immediate]
565 (cons "Immediate" (make-sparse-keymap "Immediate")))
567 (define-key tar-mode-map [menu-bar immediate view]
568 '("View This File" . tar-view))
569 (define-key tar-mode-map [menu-bar immediate display]
570 '("Display in Other Window" . tar-display-other-window))
571 (define-key tar-mode-map [menu-bar immediate find-file-other-window]
572 '("Find in Other Window" . tar-extract-other-window))
573 (define-key tar-mode-map [menu-bar immediate find-file]
574 '("Find This File" . tar-extract))
576 (define-key tar-mode-map [menu-bar mark]
577 (cons "Mark" (make-sparse-keymap "Mark")))
579 (define-key tar-mode-map [menu-bar mark unmark-all]
580 '("Unmark All" . tar-clear-modification-flags))
581 (define-key tar-mode-map [menu-bar mark deletion]
582 '("Flag" . tar-flag-deleted))
583 (define-key tar-mode-map [menu-bar mark unmark]
584 '("Unflag" . tar-unflag))
586 (define-key tar-mode-map [menu-bar operate]
587 (cons "Operate" (make-sparse-keymap "Operate")))
589 (define-key tar-mode-map [menu-bar operate chown]
590 '("Change Owner..." . tar-chown-entry))
591 (define-key tar-mode-map [menu-bar operate chgrp]
592 '("Change Group..." . tar-chgrp-entry))
593 (define-key tar-mode-map [menu-bar operate chmod]
594 '("Change Mode..." . tar-chmod-entry))
595 (define-key tar-mode-map [menu-bar operate rename]
596 '("Rename to..." . tar-rename-entry))
597 (define-key tar-mode-map [menu-bar operate copy]
598 '("Copy to..." . tar-copy))
599 (define-key tar-mode-map [menu-bar operate expunge]
600 '("Expunge Marked Files" . tar-expunge))
602 ;; tar mode is suitable only for specially formatted data.
603 (put 'tar-mode 'mode-class 'special)
604 (put 'tar-subfile-mode 'mode-class 'special)
608 "Major mode for viewing a tar file as a dired-like listing of its contents.
609 You can move around using the usual cursor motion commands.
610 Letters no longer insert themselves.
611 Type `e' to pull a file out of the tar file and into its own buffer;
612 or click mouse-2 on the file's line in the Tar mode buffer.
613 Type `c' to copy an entry from the tar file into another file on disk.
615 If you edit a sub-file of this archive (as with the `e' command) and
616 save it with Control-x Control-s, the contents of that buffer will be
617 saved back into the tar-file buffer; in this way you can edit a file
618 inside of a tar archive without extracting it and re-archiving it.
620 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
622 ;; this is not interactive because you shouldn't be turning this
623 ;; mode on and off. You can corrupt things that way.
624 ;; rms: with permanent locals, it should now be possible to make this work
625 ;; interactively in some reasonable fashion.
626 (kill-all-local-variables)
627 (make-local-variable 'tar-header-offset)
628 (make-local-variable 'tar-parse-info)
629 (make-local-variable 'require-final-newline)
630 (setq require-final-newline nil) ; binary data, dude...
631 (make-local-variable 'revert-buffer-function)
632 (setq revert-buffer-function 'tar-mode-revert)
633 (make-local-variable 'local-enable-local-variables)
634 (setq local-enable-local-variables nil)
635 (make-local-variable 'next-line-add-newlines)
636 (setq next-line-add-newlines nil)
637 ;; Prevent loss of data when saving the file.
638 (make-local-variable 'file-precious-flag)
639 (setq file-precious-flag t)
640 (setq major-mode 'tar-mode)
641 (setq mode-name "Tar")
642 (use-local-map tar-mode-map)
644 (make-local-variable 'write-contents-hooks)
645 (setq write-contents-hooks '(tar-mode-write-file))
647 (if (and (boundp 'tar-header-offset) tar-header-offset)
648 (narrow-to-region 1 (byte-to-position tar-header-offset))
649 (tar-summarize-buffer)
651 (run-hooks 'tar-mode-hook)
655 (defun tar-subfile-mode (p)
656 "Minor mode for editing an element of a tar-file.
657 This mode arranges for \"saving\" this buffer to write the data
658 into the tar-file buffer that it came from. The changes will actually
659 appear on disk when you save the tar-file's buffer."
661 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
662 (error "This buffer is not an element of a tar file"))
663 ;;; Don't do this, because it is redundant and wastes mode line space.
664 ;;; (or (assq 'tar-subfile-mode minor-mode-alist)
665 ;;; (setq minor-mode-alist (append minor-mode-alist
666 ;;; (list '(tar-subfile-mode " TarFile")))))
667 (make-local-variable 'tar-subfile-mode)
668 (setq tar-subfile-mode
670 (not tar-subfile-mode)
671 (> (prefix-numeric-value p) 0)))
672 (cond (tar-subfile-mode
673 (make-local-variable 'local-write-file-hooks)
674 (setq local-write-file-hooks '(tar-subfile-save-buffer))
675 ;; turn off auto-save.
677 (setq buffer-auto-save-file-name nil)
678 (run-hooks 'tar-subfile-mode-hook))
680 (kill-local-variable 'local-write-file-hooks))))
683 ;; Revert the buffer and recompute the dired-like listing.
684 (defun tar-mode-revert (&optional no-auto-save no-confirm)
685 (let ((revert-buffer-function nil)
686 (old-offset tar-header-offset)
688 (setq tar-header-offset nil)
690 (and (revert-buffer t no-confirm)
694 ;; If the revert was canceled,
695 ;; put back the old value of tar-header-offset.
697 (setq tar-header-offset old-offset)))))
700 (defun tar-next-line (p)
703 (if (eobp) nil (forward-char (if tar-mode-show-date 54 36))))
705 (defun tar-previous-line (p)
707 (tar-next-line (- p)))
709 (defun tar-current-descriptor (&optional noerror)
710 "Return the tar-descriptor of the current line, or signals an error."
711 ;; I wish lines had plists, like in ZMACS...
712 (or (nth (count-lines (point-min)
713 (save-excursion (beginning-of-line) (point)))
717 (error "This line does not describe a tar-file entry"))))
719 (defun tar-get-descriptor ()
720 (let* ((descriptor (tar-current-descriptor))
721 (tokens (tar-desc-tokens descriptor))
722 (size (tar-header-size tokens))
723 (link-p (tar-header-link-type tokens)))
725 (error "This is a %s, not a real file"
726 (cond ((eq link-p 5) "directory")
727 ((eq link-p 20) "tar directory header")
728 ((eq link-p 29) "multivolume-continuation")
729 ((eq link-p 35) "sparse entry")
730 ((eq link-p 38) "volume header")
732 (if (zerop size) (error "This is a zero-length file"))
735 (defun tar-mouse-extract (event)
736 "Extract a file whose tar directory line you click on."
739 (set-buffer (window-buffer (posn-window (event-end event))))
741 (goto-char (posn-point (event-end event)))
742 ;; Just make sure this doesn't get an error.
743 (tar-get-descriptor)))
744 (select-window (posn-window (event-end event)))
745 (goto-char (posn-point (event-end event)))
748 (defun tar-extract (&optional other-window-p)
749 "In Tar mode, extract this entry of the tar file into its own buffer."
751 (let* ((view-p (eq other-window-p 'view))
752 (descriptor (tar-get-descriptor))
753 (tokens (tar-desc-tokens descriptor))
754 (name (tar-header-name tokens))
755 (size (tar-header-size tokens))
756 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
757 (end (+ start size)))
758 (let* ((tar-buffer (current-buffer))
759 (tar-buffer-multibyte enable-multibyte-characters)
760 (tarname (if (buffer-file-name)
761 (file-name-nondirectory (buffer-file-name))
763 (bufname (concat (file-name-nondirectory name)
767 (read-only-p (or buffer-read-only view-p))
768 (buffer (get-buffer bufname))
772 (setq buffer (get-buffer-create bufname))
773 (setq just-created t)
777 (set-buffer-multibyte nil)
780 (if enable-multibyte-characters
782 ;; We must avoid unibyte->multibyte conversion.
783 (set-buffer-multibyte nil)
784 (insert-buffer-substring tar-buffer start end)
785 (set-buffer-multibyte t))
786 (insert-buffer-substring tar-buffer start end))
788 (setq buffer-file-name
789 ;; `:' is not allowed on Windows
790 (expand-file-name (concat tarname "!" name)))
791 (setq buffer-file-truename
792 (abbreviate-file-name buffer-file-name))
793 ;; We need to mimic the parts of insert-file-contents
794 ;; which determine the coding-system and decode the text.
796 (and set-auto-coding-function
798 (funcall set-auto-coding-function
800 (multibyte enable-multibyte-characters)
801 (detected (detect-coding-region
802 1 (min 16384 (point-max)) t)))
804 (or (numberp (coding-system-eol-type coding))
805 (setq coding (coding-system-change-eol-conversion
807 (coding-system-eol-type detected))))
809 (or (find-new-buffer-file-coding-system detected)
811 (find-operation-coding-system
812 'insert-file-contents buffer-file-name)))
813 (if (consp file-coding)
814 (setq file-coding (car file-coding))
816 (if (or (eq coding 'no-conversion)
817 (eq (coding-system-type coding) 5))
818 (setq multibyte (set-buffer-multibyte nil)))
821 (coding-system-change-text-conversion
823 (decode-coding-region 1 (point-max) coding)
824 (set-buffer-file-coding-system coding))
825 ;; Set the default-directory to the dir of the
827 (setq default-directory
829 (set-buffer tar-buffer)
831 (normal-mode) ; pick a mode.
832 (rename-buffer bufname)
833 (make-local-variable 'tar-superior-buffer)
834 (make-local-variable 'tar-superior-descriptor)
835 (setq tar-superior-buffer tar-buffer)
836 (setq tar-superior-descriptor descriptor)
837 (setq buffer-read-only read-only-p)
838 (set-buffer-modified-p nil)
839 (tar-subfile-mode 1))
840 (set-buffer tar-buffer))
841 (narrow-to-region 1 tar-header-offset)
842 (set-buffer-multibyte tar-buffer-multibyte)))
844 (view-buffer buffer (and just-created 'kill-buffer))
845 (if (eq other-window-p 'display)
846 (display-buffer buffer)
848 (switch-to-buffer-other-window buffer)
849 (switch-to-buffer buffer)))))))
852 (defun tar-extract-other-window ()
853 "*In Tar mode, find this entry of the tar file in another window."
857 (defun tar-display-other-window ()
858 "*In Tar mode, display this entry of the tar file in another window."
860 (tar-extract 'display))
863 "*In Tar mode, view the tar file entry on this line."
868 (defun tar-read-file-name (&optional prompt)
869 "Read a file name with this line's entry as the default."
870 (or prompt (setq prompt "Copy to: "))
871 (let* ((default-file (expand-file-name
872 (tar-header-name (tar-desc-tokens
873 (tar-current-descriptor)))))
874 (target (expand-file-name
875 (read-file-name prompt
876 (file-name-directory default-file)
878 (if (or (string= "" (file-name-nondirectory target))
879 (file-directory-p target))
880 (setq target (concat (if (string-match "/$" target)
881 (substring target 0 (1- (match-end 0)))
884 (file-name-nondirectory default-file))))
888 (defun tar-copy (&optional to-file)
889 "*In Tar mode, extract this entry of the tar file into a file on disk.
890 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
891 the current tar-entry."
892 (interactive (list (tar-read-file-name)))
893 (let* ((descriptor (tar-get-descriptor))
894 (tokens (tar-desc-tokens descriptor))
895 (name (tar-header-name tokens))
896 (size (tar-header-size tokens))
897 (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
899 (multibyte enable-multibyte-characters)
900 (inhibit-file-name-handlers inhibit-file-name-handlers)
901 (inhibit-file-name-operation inhibit-file-name-operation))
904 ;; Inhibit compressing a subfile again if *both* name and
905 ;; to-file are handled by jka-compr
906 (if (and (eq (find-file-name-handler name 'write-region) 'jka-compr-handler)
907 (eq (find-file-name-handler to-file 'write-region) 'jka-compr-handler))
908 (setq inhibit-file-name-handlers
909 (cons 'jka-compr-handler
910 (and (eq inhibit-file-name-operation 'write-region)
911 inhibit-file-name-handlers))
912 inhibit-file-name-operation 'write-region))
914 (let ((coding-system-for-write 'no-conversion))
915 (set-buffer-multibyte nil)
916 (write-region start end to-file))
917 (set-buffer-multibyte multibyte)))
918 (message "Copied tar entry %s to %s" name to-file)))
920 (defun tar-flag-deleted (p &optional unflag)
921 "*In Tar mode, mark this sub-file to be deleted from the tar file.
922 With a prefix argument, mark that many files."
925 (tar-dotimes (i (if (< p 0) (- p) p))
926 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
929 (insert (if unflag " " "D"))))
930 (forward-line (if (< p 0) -1 1)))
931 (if (eobp) nil (forward-char 36)))
933 (defun tar-unflag (p)
934 "*In Tar mode, un-mark this sub-file if it is marked to be deleted.
935 With a prefix argument, un-mark that many files forward."
937 (tar-flag-deleted p t))
939 (defun tar-unflag-backwards (p)
940 "*In Tar mode, un-mark this sub-file if it is marked to be deleted.
941 With a prefix argument, un-mark that many files backward."
943 (tar-flag-deleted (- p) t))
946 ;; When this function is called, it is sure that the buffer is unibyte.
947 (defun tar-expunge-internal ()
948 "Expunge the tar-entry specified by the current line."
949 (let* ((descriptor (tar-current-descriptor))
950 (tokens (tar-desc-tokens descriptor))
951 (line (tar-desc-data-start descriptor))
952 (name (tar-header-name tokens))
953 (size (tar-header-size tokens))
954 (link-p (tar-header-link-type tokens))
955 (start (tar-desc-data-start descriptor))
956 (following-descs (cdr (memq descriptor tar-parse-info))))
957 (if link-p (setq size 0)) ; size lies for hard-links.
959 ;; delete the current line...
961 (let ((line-start (point)))
962 (end-of-line) (forward-char)
963 (let ((line-len (- (point) line-start)))
964 (delete-region line-start (point))
966 ;; decrement the header-pointer to be in sync...
967 (setq tar-header-offset (- tar-header-offset line-len))))
969 ;; delete the data pointer...
970 (setq tar-parse-info (delq descriptor tar-parse-info))
972 ;; delete the data from inside the file...
974 (let* ((data-start (+ start tar-header-offset -513))
975 (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
976 (delete-region data-start data-end)
978 ;; and finally, decrement the start-pointers of all following
979 ;; entries in the archive. This is a pig when deleting a bunch
980 ;; of files at once - we could optimize this to only do the
981 ;; iteration over the files that remain, or only iterate up to
982 ;; the next file to be deleted.
983 (let ((data-length (- data-end data-start)))
984 (tar-dolist (desc following-descs)
985 (tar-setf (tar-desc-data-start desc)
986 (- (tar-desc-data-start desc) data-length))))
988 (narrow-to-region 1 tar-header-offset))
991 (defun tar-expunge (&optional noconfirm)
992 "*In Tar mode, delete all the archived files flagged for deletion.
993 This does not modify the disk image; you must save the tar file itself
994 for this to be permanent."
997 (y-or-n-p "Expunge files marked for deletion? "))
999 (multibyte enable-multibyte-characters))
1000 (set-buffer-multibyte nil)
1004 (if (looking-at "D")
1005 (progn (tar-expunge-internal)
1008 ;; after doing the deletions, add any padding that may be necessary.
1009 (tar-pad-to-blocksize)
1010 (narrow-to-region 1 tar-header-offset))
1011 (set-buffer-multibyte multibyte)
1013 (message "Nothing to expunge.")
1014 (message "%s files expunged. Be sure to save this buffer." n)))))
1017 (defun tar-clear-modification-flags ()
1018 "Remove the stars at the beginning of each line."
1022 (while (< (position-bytes (point)) tar-header-offset)
1023 (if (not (eq (following-char) ?\ ))
1024 (progn (delete-char 1) (insert " ")))
1028 (defun tar-chown-entry (new-uid)
1029 "*Change the user-id associated with this entry in the tar file.
1030 If this tar file was written by GNU tar, then you will be able to edit
1031 the user id as a string; otherwise, you must edit it as a number.
1032 You can force editing as a number by calling this with a prefix arg.
1033 This does not modify the disk image; you must save the tar file itself
1034 for this to be permanent."
1036 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
1037 (if (or current-prefix-arg
1038 (not (tar-header-magic tokens)))
1040 (while (not (numberp (setq n (read-minibuffer
1042 (format "%s" (tar-header-uid tokens)))))))
1044 (read-string "New UID string: " (tar-header-uname tokens))))))
1045 (cond ((stringp new-uid)
1046 (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
1048 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
1050 (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
1052 (tar-alter-one-field tar-uid-offset
1053 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1056 (defun tar-chgrp-entry (new-gid)
1057 "*Change the group-id associated with this entry in the tar file.
1058 If this tar file was written by GNU tar, then you will be able to edit
1059 the group id as a string; otherwise, you must edit it as a number.
1060 You can force editing as a number by calling this with a prefix arg.
1061 This does not modify the disk image; you must save the tar file itself
1062 for this to be permanent."
1064 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
1065 (if (or current-prefix-arg
1066 (not (tar-header-magic tokens)))
1068 (while (not (numberp (setq n (read-minibuffer
1070 (format "%s" (tar-header-gid tokens)))))))
1072 (read-string "New GID string: " (tar-header-gname tokens))))))
1073 (cond ((stringp new-gid)
1074 (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
1076 (tar-alter-one-field tar-gname-offset
1077 (concat new-gid "\000")))
1079 (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
1081 (tar-alter-one-field tar-gid-offset
1082 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1084 (defun tar-rename-entry (new-name)
1085 "*Change the name associated with this entry in the tar file.
1086 This does not modify the disk image; you must save the tar file itself
1087 for this to be permanent."
1089 (list (read-string "New name: "
1090 (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
1091 (if (string= "" new-name) (error "zero length name"))
1092 (if (> (length new-name) 98) (error "name too long"))
1093 (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
1095 (tar-alter-one-field 0
1096 (substring (concat new-name (make-string 99 0)) 0 99)))
1099 (defun tar-chmod-entry (new-mode)
1100 "*Change the protection bits associated with this entry in the tar file.
1101 This does not modify the disk image; you must save the tar file itself
1102 for this to be permanent."
1103 (interactive (list (tar-parse-octal-integer-safe
1104 (read-string "New protection (octal): "))))
1105 (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
1107 (tar-alter-one-field tar-mode-offset
1108 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1111 (defun tar-alter-one-field (data-position new-data-string)
1112 (let* ((descriptor (tar-current-descriptor))
1113 (tokens (tar-desc-tokens descriptor))
1114 (multibyte enable-multibyte-characters))
1118 ;; update the header-line.
1122 (delete-region p (point))
1123 (insert (tar-header-block-summarize tokens) "\n")
1124 (setq tar-header-offset (position-bytes (point-max))))
1127 (set-buffer-multibyte nil)
1128 (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
1130 ;; delete the old field and insert a new one.
1131 (goto-char (+ start data-position))
1132 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1133 (insert new-data-string) ; <--
1135 ;; compute a new checksum and insert it.
1136 (let ((chk (tar-header-block-checksum
1137 (buffer-substring start (+ start 512)))))
1138 (goto-char (+ start tar-chk-offset))
1139 (delete-region (point) (+ (point) 8))
1140 (insert (format "%6o" chk))
1143 (tar-setf (tar-header-checksum tokens) chk)
1145 ;; ok, make sure we didn't botch it.
1146 (tar-header-block-check-checksum
1147 (buffer-substring start (+ start 512))
1148 chk (tar-header-name tokens))
1150 (narrow-to-region 1 tar-header-offset)
1151 (set-buffer-multibyte multibyte)
1152 (tar-next-line 0))))
1155 (defun tar-octal-time (timeval)
1156 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1157 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1158 (insert (format "%05o%01o%05o"
1160 (logior (lsh (logand 3 hibits) 1) (> (logand lobits 32768) 0))
1161 (logand 32767 lobits)
1164 (defun tar-subfile-save-buffer ()
1165 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1166 This doesn't write anything to disk; you must save the parent tar-file buffer
1167 to make your changes permanent."
1169 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1170 (error "This buffer has no superior tar file buffer"))
1171 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1172 (error "This buffer doesn't have an index into its superior tar file!"))
1174 (let ((subfile (current-buffer))
1175 (subfile-multibyte enable-multibyte-characters)
1176 (coding buffer-file-coding-system)
1177 (descriptor tar-superior-descriptor)
1179 ;; We must make the current buffer unibyte temporarily to avoid
1180 ;; multibyte->unibyte conversion in `insert-buffer'.
1181 (set-buffer-multibyte nil)
1182 (setq subfile-size (buffer-size))
1183 (set-buffer tar-superior-buffer)
1184 (let* ((tokens (tar-desc-tokens descriptor))
1185 (start (tar-desc-data-start descriptor))
1186 (name (tar-header-name tokens))
1187 (size (tar-header-size tokens))
1188 (size-pad (ash (ash (+ size 511) -9) 9))
1189 (head (memq descriptor tar-parse-info))
1190 (following-descs (cdr head))
1191 (tar-buffer-multibyte enable-multibyte-characters))
1193 (error "Can't find this tar file entry in its parent tar file!"))
1197 (set-buffer-multibyte nil)
1198 ;; delete the old data...
1199 (let* ((data-start (+ start tar-header-offset -1))
1200 (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
1201 (delete-region data-start data-end)
1202 ;; insert the new data...
1203 (goto-char data-start)
1204 (insert-buffer subfile)
1206 (encode-coding-region
1207 data-start (+ data-start subfile-size) coding))
1209 ;; pad the new data out to a multiple of 512...
1210 (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
1211 (goto-char (+ data-start subfile-size))
1212 (insert (make-string (- subfile-size-pad subfile-size) 0))
1214 ;; update the data pointer of this and all following files...
1215 (tar-setf (tar-header-size tokens) subfile-size)
1216 (let ((difference (- subfile-size-pad size-pad)))
1217 (tar-dolist (desc following-descs)
1218 (tar-setf (tar-desc-data-start desc)
1219 (+ (tar-desc-data-start desc) difference))))
1221 ;; Update the size field in the header block.
1222 (let ((header-start (- data-start 512)))
1223 (goto-char (+ header-start tar-size-offset))
1224 (delete-region (point) (+ (point) 12))
1225 (insert (format "%11o" subfile-size))
1228 ;; Maybe update the datestamp.
1229 (if (not tar-update-datestamp)
1231 (goto-char (+ header-start tar-time-offset))
1232 (delete-region (point) (+ (point) 12))
1233 (insert (tar-octal-time (current-time)))
1236 ;; compute a new checksum and insert it.
1237 (let ((chk (tar-header-block-checksum
1238 (buffer-substring header-start data-start))))
1239 (goto-char (+ header-start tar-chk-offset))
1240 (delete-region (point) (+ (point) 8))
1241 (insert (format "%6o" chk))
1244 (tar-setf (tar-header-checksum tokens) chk)))
1246 ;; alter the descriptor-line...
1248 (let ((position (- (length tar-parse-info) (length head))))
1250 (next-line position)
1254 (m (set-marker (make-marker) tar-header-offset)))
1256 (setq after (point))
1257 ;; Insert the new text after the old, before deleting,
1258 ;; to preserve the window start.
1259 (let ((line (tar-header-block-summarize tokens t)))
1260 (if (multibyte-string-p line)
1261 (insert-before-markers (string-as-unibyte line) "\n")
1262 (insert-before-markers line "\n")))
1263 (delete-region p after)
1264 (setq tar-header-offset (marker-position m)))
1266 ;; after doing the insertion, add any final padding that may be necessary.
1267 (tar-pad-to-blocksize))
1268 (narrow-to-region 1 tar-header-offset)
1269 (set-buffer-multibyte tar-buffer-multibyte)))
1270 (set-buffer-modified-p t) ; mark the tar file as modified
1272 (set-buffer subfile)
1273 ;; Restore the buffer multibyteness.
1274 (set-buffer-multibyte subfile-multibyte)
1275 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1276 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1277 (buffer-name tar-superior-buffer))
1278 ;; Prevent basic-save-buffer from changing our coding-system.
1279 (setq last-coding-system-used buffer-file-coding-system)
1280 ;; Prevent ordinary saving from happening.
1284 ;; When this function is called, it is sure that the buffer is unibyte.
1285 (defun tar-pad-to-blocksize ()
1286 "If we are being anal about tar file blocksizes, fix up the current buffer.
1287 Leaves the region wide."
1288 (if (null tar-anal-blocksize)
1291 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1292 (start (tar-desc-data-start last-desc))
1293 (tokens (tar-desc-tokens last-desc))
1294 (link-p (tar-header-link-type tokens))
1295 (size (if link-p 0 (tar-header-size tokens)))
1296 (data-end (+ start size))
1297 (bbytes (ash tar-anal-blocksize 9))
1298 (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
1299 (inhibit-read-only t) ; ##
1301 ;; If the padding after the last data is too long, delete some;
1302 ;; else insert some until we are padded out to the right number of blocks.
1304 (goto-char (+ (or tar-header-offset 0) data-end))
1305 (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
1306 (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
1307 (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
1313 ;; Used in write-file-hook to write tar-files out correctly.
1314 (defun tar-mode-write-file ()
1318 ;; Doing this here confuses things - the region gets left too wide!
1319 ;; I suppose this is run in a context where changing the buffer is bad.
1320 ;; (tar-pad-to-blocksize)
1321 ;; tar-header-offset turns out to be null for files fetched with W3,
1323 (let ((coding-system-for-write 'no-conversion))
1324 (write-region (or (byte-to-position tar-header-offset)
1327 buffer-file-name nil t))
1328 (tar-clear-modification-flags)
1329 (set-buffer-modified-p nil))
1330 (narrow-to-region 1 (byte-to-position tar-header-offset)))
1331 ;; return T because we've written the file.
1335 "Kill the current tar buffer."
1342 ;;; tar-mode.el ends here