]> code.delx.au - gnu-emacs/blob - lisp/tar-mode.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / tar-mode.el
1 ;;; tar-mode.el --- simple editing of tar files from GNU emacs
2
3 ;; Copyright (C) 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;; Maintainer: FSF
8 ;; Created: 04 Apr 1990
9 ;; Keywords: unix
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
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; This package attempts to make dealing with Unix 'tar' archives easier.
29 ;; When this code is loaded, visiting a file whose name ends in '.tar' will
30 ;; cause the contents of that archive file to be displayed in a Dired-like
31 ;; listing. It is then possible to use the customary Dired keybindings to
32 ;; extract sub-files from that archive, either by reading them into their own
33 ;; editor buffers, or by copying them directly to arbitrary files on disk.
34 ;; It is also possible to delete sub-files from within the tar file and write
35 ;; the modified archive back to disk, or to edit sub-files within the archive
36 ;; and re-insert the modified files into the archive. See the documentation
37 ;; string of tar-mode for more info.
38
39 ;; This code now understands the extra fields that GNU tar adds to tar files.
40
41 ;; This interacts correctly with "uncompress.el" in the Emacs library,
42 ;; which you get with
43 ;;
44 ;; (autoload 'uncompress-while-visiting "uncompress")
45 ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
46 ;; auto-mode-alist))
47 ;;
48 ;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
49
50 ;; *************** TO DO ***************
51 ;;
52 ;; o chmod should understand "a+x,og-w".
53 ;;
54 ;; o It's not possible to add a NEW file to a tar archive; not that
55 ;; important, but still...
56 ;;
57 ;; o The code is less efficient that it could be - in a lot of places, I
58 ;; pull a 512-character string out of the buffer and parse it, when I could
59 ;; be parsing it in place, not garbaging a string. Should redo that.
60 ;;
61 ;; o I'd like a command that searches for a string/regexp in every subfile
62 ;; of an archive, where <esc> would leave you in a subfile-edit buffer.
63 ;; (Like the Meta-R command of the Zmacs mail reader.)
64 ;;
65 ;; o Sometimes (but not always) reverting the tar-file buffer does not
66 ;; re-grind the listing, and you are staring at the binary tar data.
67 ;; Typing 'g' again immediately after that will always revert and re-grind
68 ;; it, though. I have no idea why this happens.
69 ;;
70 ;; o Tar-mode interacts poorly with crypt.el and zcat.el because the tar
71 ;; write-file-hook actually writes the file. Instead it should remove the
72 ;; header (and conspire to put it back afterwards) so that other write-file
73 ;; hooks which frob the buffer have a chance to do their dirty work. There
74 ;; might be a problem if the tar write-file-hook does not come *first* on
75 ;; the list.
76 ;;
77 ;; o Block files, sparse files, continuation files, and the various header
78 ;; types aren't editable. Actually I don't know that they work at all.
79
80 ;; Rationale:
81
82 ;; Why does tar-mode edit the file itself instead of using tar?
83
84 ;; That means that you can edit tar files which you don't have room for
85 ;; on your local disk.
86
87 ;; I don't know about recent features in gnu tar, but old versions of tar
88 ;; can't replace a file in the middle of a tar file with a new version.
89 ;; Tar-mode can. I don't think tar can do things like chmod the subfiles.
90 ;; An implementation which involved unpacking and repacking the file into
91 ;; some scratch directory would be very wasteful, and wouldn't be able to
92 ;; preserve the file owners.
93
94 ;;; Bugs:
95
96 ;; - Rename on ././@LongLink files
97 ;; - Revert confirmation displays the raw data temporarily.
98
99 ;;; Code:
100
101 (eval-when-compile (require 'cl))
102
103 (defgroup tar nil
104 "Simple editing of tar files."
105 :prefix "tar-"
106 :group 'data)
107
108 (defcustom tar-anal-blocksize 20
109 "The blocksize of tar files written by Emacs, or nil, meaning don't care.
110 The blocksize of a tar file is not really the size of the blocks; rather, it is
111 the number of blocks written with one system call. When tarring to a tape,
112 this is the size of the *tape* blocks, but when writing to a file, it doesn't
113 matter much. The only noticeable difference is that if a tar file does not
114 have a blocksize of 20, tar will tell you that; all this really controls is
115 how many null padding bytes go on the end of the tar file."
116 :type '(choice integer (const nil))
117 :group 'tar)
118
119 (defcustom tar-update-datestamp nil
120 "Non-nil means Tar mode should play fast and loose with sub-file datestamps.
121 If this is true, then editing and saving a tar file entry back into its
122 tar file will update its datestamp. If false, the datestamp is unchanged.
123 You may or may not want this - it is good in that you can tell when a file
124 in a tar archive has been changed, but it is bad for the same reason that
125 editing a file in the tar archive at all is bad - the changed version of
126 the file never exists on disk."
127 :type 'boolean
128 :group 'tar)
129
130 (defcustom tar-mode-show-date nil
131 "Non-nil means Tar mode should show the date/time of each subfile.
132 This information is useful, but it takes screen space away from file names."
133 :type 'boolean
134 :group 'tar)
135
136 (defvar tar-parse-info nil)
137 (defvar tar-superior-buffer nil)
138 (defvar tar-superior-descriptor nil)
139 (defvar tar-subfile-mode nil)
140 (defvar tar-file-name-coding-system nil)
141
142 (put 'tar-superior-buffer 'permanent-local t)
143 (put 'tar-superior-descriptor 'permanent-local t)
144
145 ;; The Tar data is made up of bytes and better manipulated as bytes
146 ;; and can be very large, so insert/delete can be costly. The summary we
147 ;; want to display may contain non-ascci chars, of course, so we'd like it
148 ;; to be multibyte. We used to keep both in the same buffer and switch
149 ;; from/to uni/multibyte. But this had several downsides:
150 ;; - set-buffer-multibyte has an O(N^2) worst case that tends to be triggered
151 ;; here, so it gets atrociously slow on large Tar files.
152 ;; - need to widen/narrow the buffer to show/hide the raw data, and need to
153 ;; maintain a tar-header-offset that keeps track of the boundary between
154 ;; the two.
155 ;; - can't use markers because they're not preserved by set-buffer-multibyte.
156 ;; So instead, we now keep the two pieces of data in separate buffers, and
157 ;; use the new buffer-swap-text primitive when we need to change which data
158 ;; is associated with "the" buffer.
159 (defvar tar-data-buffer nil "Buffer that holds the actual raw tar bytes.")
160 (make-variable-buffer-local 'tar-data-buffer)
161
162 (defun tar-data-swapped-p ()
163 "Return non-nil if the tar-data is in `tar-data-buffer'."
164 ;; We need to be careful to keep track of which buffer holds the tar-data,
165 ;; since we swap them back and forth. Since the user may make the summary
166 ;; buffer unibyte, we can't rely on the multibyteness of the buffers.
167 ;; We could try and recognize the tar-format signature, but instead
168 ;; I decided to go for something simpler.
169 (and (buffer-live-p tar-data-buffer)
170 (> (buffer-size tar-data-buffer) (buffer-size))))
171
172 \f
173 ;;; down to business.
174
175 (defstruct (tar-header
176 (:constructor nil)
177 (:type vector)
178 :named
179 (:constructor
180 make-tar-header (data-start name mode uid gid size date checksum
181 link-type link-name magic uname gname dmaj dmin)))
182 data-start name mode uid gid size date checksum link-type link-name
183 magic uname gname dmaj dmin
184 ;; Start of the header can be nil (meaning it's 512 bytes before data-start)
185 ;; or a marker (in case the header uses LongLink thingies).
186 header-start)
187
188 (defconst tar-name-offset 0)
189 (defconst tar-mode-offset (+ tar-name-offset 100))
190 (defconst tar-uid-offset (+ tar-mode-offset 8))
191 (defconst tar-gid-offset (+ tar-uid-offset 8))
192 (defconst tar-size-offset (+ tar-gid-offset 8))
193 (defconst tar-time-offset (+ tar-size-offset 12))
194 (defconst tar-chk-offset (+ tar-time-offset 12))
195 (defconst tar-linkp-offset (+ tar-chk-offset 8))
196 (defconst tar-link-offset (+ tar-linkp-offset 1))
197 ;;; GNU-tar specific slots.
198 (defconst tar-magic-offset (+ tar-link-offset 100))
199 (defconst tar-uname-offset (+ tar-magic-offset 8))
200 (defconst tar-gname-offset (+ tar-uname-offset 32))
201 (defconst tar-dmaj-offset (+ tar-gname-offset 32))
202 (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
203 (defconst tar-prefix-offset (+ tar-dmin-offset 8))
204 (defconst tar-end-offset (+ tar-prefix-offset 155))
205
206 (defun tar-roundup-512 (s)
207 "Round S up to the next multiple of 512."
208 (ash (ash (+ s 511) -9) 9))
209
210 (defun tar-header-block-tokenize (pos)
211 "Return a `tar-header' structure.
212 This is a list of name, mode, uid, gid, size,
213 write-date, checksum, link-type, and link-name."
214 (assert (<= (+ pos 512) (point-max)))
215 (assert (zerop (mod (- pos (point-min)) 512)))
216 (assert (not enable-multibyte-characters))
217 (let ((string (buffer-substring pos (setq pos (+ pos 512)))))
218 (when ;(some 'plusp string) ; <-- oops, massive cycle hog!
219 (or (not (= 0 (aref string 0))) ; This will do.
220 (not (= 0 (aref string 101))))
221 (let* ((name-end tar-mode-offset)
222 (link-end (1- tar-magic-offset))
223 (uname-end (1- tar-gname-offset))
224 (gname-end (1- tar-dmaj-offset))
225 (link-p (aref string tar-linkp-offset))
226 (magic-str (substring string tar-magic-offset
227 (1- tar-uname-offset)))
228 (uname-valid-p (car (member magic-str '("ustar " "ustar\0\0"))))
229 name linkname
230 (nulsexp "[^\000]*\000"))
231 (when (string-match nulsexp string tar-name-offset)
232 (setq name-end (min name-end (1- (match-end 0)))))
233 (when (string-match nulsexp string tar-link-offset)
234 (setq link-end (min link-end (1- (match-end 0)))))
235 (when (string-match nulsexp string tar-uname-offset)
236 (setq uname-end (min uname-end (1- (match-end 0)))))
237 (when (string-match nulsexp string tar-gname-offset)
238 (setq gname-end (min gname-end (1- (match-end 0)))))
239 (setq name (substring string tar-name-offset name-end)
240 link-p (if (or (= link-p 0) (= link-p ?0))
241 nil
242 (- link-p ?0)))
243 (setq linkname (substring string tar-link-offset link-end))
244 (when (and (equal uname-valid-p "ustar\0\0")
245 (string-match nulsexp string tar-prefix-offset)
246 (> (match-end 0) (1+ tar-prefix-offset)))
247 (setq name (concat (substring string tar-prefix-offset
248 (1- (match-end 0)))
249 "/" name)))
250 (if default-enable-multibyte-characters
251 (setq name
252 (decode-coding-string name tar-file-name-coding-system)
253 linkname
254 (decode-coding-string linkname
255 tar-file-name-coding-system)))
256 (if (and (null link-p) (string-match "/\\'" name))
257 (setq link-p 5)) ; directory
258
259 (if (and (equal name "././@LongLink")
260 (equal magic-str "ustar ")) ;OLDGNU_MAGIC.
261 ;; This is a GNU Tar long-file-name header.
262 (let* ((size (tar-parse-octal-integer
263 string tar-size-offset tar-time-offset))
264 ;; -1 so as to strip the terminating 0 byte.
265 (name (buffer-substring pos (+ pos size -1)))
266 (descriptor (tar-header-block-tokenize
267 (+ pos (tar-roundup-512 size)))))
268 (cond
269 ((eq link-p (- ?L ?0)) ;GNUTYPE_LONGNAME.
270 (setf (tar-header-name descriptor) name))
271 ((eq link-p (- ?K ?0)) ;GNUTYPE_LONGLINK.
272 (setf (tar-header-link-name descriptor) name))
273 (t
274 (message "Unrecognized GNU Tar @LongLink format")))
275 (setf (tar-header-header-start descriptor)
276 (copy-marker (- pos 512) t))
277 descriptor)
278
279 (make-tar-header
280 (copy-marker pos nil)
281 name
282 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset)
283 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset)
284 (tar-parse-octal-integer string tar-gid-offset tar-size-offset)
285 (tar-parse-octal-integer string tar-size-offset tar-time-offset)
286 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset)
287 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset)
288 link-p
289 linkname
290 uname-valid-p
291 (and uname-valid-p (substring string tar-uname-offset uname-end))
292 (and uname-valid-p (substring string tar-gname-offset gname-end))
293 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset)
294 (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset)
295 ))))))
296
297 ;; Pseudo-field.
298 (defun tar-header-data-end (descriptor)
299 (let* ((data-start (tar-header-data-start descriptor))
300 (link-type (tar-header-link-type descriptor))
301 (size (tar-header-size descriptor))
302 (fudge (cond
303 ;; Foo. There's an extra empty block after these.
304 ((memq link-type '(20 55)) 512)
305 (t 0))))
306 (+ data-start fudge
307 (if (and (null link-type) (> size 0))
308 (tar-roundup-512 size)
309 0))))
310
311 (defun tar-parse-octal-integer (string &optional start end)
312 (if (null start) (setq start 0))
313 (if (null end) (setq end (length string)))
314 (if (= (aref string start) 0)
315 0
316 (let ((n 0))
317 (while (< start end)
318 (setq n (if (< (aref string start) ?0) n
319 (+ (* n 8) (- (aref string start) ?0)))
320 start (1+ start)))
321 n)))
322
323 (defun tar-parse-octal-long-integer (string &optional start end)
324 (if (null start) (setq start 0))
325 (if (null end) (setq end (length string)))
326 (if (= (aref string start) 0)
327 (list 0 0)
328 (let ((lo 0)
329 (hi 0))
330 (while (< start end)
331 (if (>= (aref string start) ?0)
332 (setq lo (+ (* lo 8) (- (aref string start) ?0))
333 hi (+ (* hi 8) (ash lo -16))
334 lo (logand lo 65535)))
335 (setq start (1+ start)))
336 (list hi lo))))
337
338 (defun tar-parse-octal-integer-safe (string)
339 (if (zerop (length string)) (error "empty string"))
340 (mapc (lambda (c)
341 (if (or (< c ?0) (> c ?7))
342 (error "`%c' is not an octal digit" c)))
343 string)
344 (tar-parse-octal-integer string))
345
346
347 (defun tar-header-block-checksum (string)
348 "Compute and return a tar-acceptable checksum for this block."
349 (assert (not (multibyte-string-p string)))
350 (let* ((chk-field-start tar-chk-offset)
351 (chk-field-end (+ chk-field-start 8))
352 (sum 0)
353 (i 0))
354 ;; Add up all of the characters except the ones in the checksum field.
355 ;; Add that field as if it were filled with spaces.
356 (while (< i chk-field-start)
357 (setq sum (+ sum (aref string i))
358 i (1+ i)))
359 (setq i chk-field-end)
360 (while (< i 512)
361 (setq sum (+ sum (aref string i))
362 i (1+ i)))
363 (+ sum (* 32 8))))
364
365 (defun tar-header-block-check-checksum (hblock desired-checksum file-name)
366 "Beep and print a warning if the checksum doesn't match."
367 (if (not (= desired-checksum (tar-header-block-checksum hblock)))
368 (progn (beep) (message "Invalid checksum for file %s!" file-name))))
369
370 (defun tar-clip-time-string (time)
371 (let ((str (current-time-string time)))
372 (concat " " (substring str 4 16) (substring str 19 24))))
373
374 (defun tar-grind-file-mode (mode)
375 "Construct a `-rw--r--r--' string indicating MODE.
376 MODE should be an integer which is a file mode value."
377 (string
378 (if (zerop (logand 256 mode)) ?- ?r)
379 (if (zerop (logand 128 mode)) ?- ?w)
380 (if (zerop (logand 1024 mode)) (if (zerop (logand 64 mode)) ?- ?x) ?s)
381 (if (zerop (logand 32 mode)) ?- ?r)
382 (if (zerop (logand 16 mode)) ?- ?w)
383 (if (zerop (logand 2048 mode)) (if (zerop (logand 8 mode)) ?- ?x) ?s)
384 (if (zerop (logand 4 mode)) ?- ?r)
385 (if (zerop (logand 2 mode)) ?- ?w)
386 (if (zerop (logand 1 mode)) ?- ?x)))
387
388 (defun tar-header-block-summarize (tar-hblock &optional mod-p)
389 "Return a line similar to the output of `tar -vtf'."
390 (let ((name (tar-header-name tar-hblock))
391 (mode (tar-header-mode tar-hblock))
392 (uid (tar-header-uid tar-hblock))
393 (gid (tar-header-gid tar-hblock))
394 (uname (tar-header-uname tar-hblock))
395 (gname (tar-header-gname tar-hblock))
396 (size (tar-header-size tar-hblock))
397 (time (tar-header-date tar-hblock))
398 ;; (ck (tar-header-checksum tar-hblock))
399 (type (tar-header-link-type tar-hblock))
400 (link-name (tar-header-link-name tar-hblock)))
401 (format "%c%c%s %7s/%-7s %7s%s %s%s"
402 (if mod-p ?* ? )
403 (cond ((or (eq type nil) (eq type 0)) ?-)
404 ((eq type 1) ?h) ; link
405 ((eq type 2) ?l) ; symlink
406 ((eq type 3) ?c) ; char special
407 ((eq type 4) ?b) ; block special
408 ((eq type 5) ?d) ; directory
409 ((eq type 6) ?p) ; FIFO/pipe
410 ((eq type 20) ?*) ; directory listing
411 ((eq type 28) ?L) ; next has longname
412 ((eq type 29) ?M) ; multivolume continuation
413 ((eq type 35) ?S) ; sparse
414 ((eq type 38) ?V) ; volume header
415 ((eq type 55) ?H) ; extended pax header
416 (t ?\s)
417 )
418 (tar-grind-file-mode mode)
419 (if (= 0 (length uname)) uid uname)
420 (if (= 0 (length gname)) gid gname)
421 size
422 (if tar-mode-show-date (tar-clip-time-string time) "")
423 (propertize name
424 'mouse-face 'highlight
425 'help-echo "mouse-2: extract this file into a buffer")
426 (if (or (eq type 1) (eq type 2))
427 (concat (if (= type 1) " ==> " " --> ") link-name)
428 ""))))
429
430 (defun tar-untar-buffer ()
431 "Extract all archive members in the tar-file into the current directory."
432 (interactive)
433 ;; FIXME: make it work even if we're not in tar-mode.
434 (let ((descriptors tar-parse-info)) ;Read the var in its buffer.
435 (with-current-buffer
436 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
437 (set-buffer-multibyte nil) ;Hopefully, a no-op.
438 (dolist (descriptor descriptors)
439 (let* ((name (tar-header-name descriptor))
440 (dir (if (eq (tar-header-link-type descriptor) 5)
441 name
442 (file-name-directory name)))
443 (start (tar-header-data-start descriptor))
444 (end (+ start (tar-header-size descriptor))))
445 (unless (file-directory-p name)
446 (message "Extracting %s" name)
447 (if (and dir (not (file-exists-p dir)))
448 (make-directory dir t))
449 (unless (file-directory-p name)
450 (write-region start end name))
451 (set-file-modes name (tar-header-mode descriptor))))))))
452
453 (defun tar-summarize-buffer ()
454 "Parse the contents of the tar file in the current buffer."
455 (assert (tar-data-swapped-p))
456 (let* ((modified (buffer-modified-p))
457 (result '())
458 (pos (point-min))
459 (progress-reporter
460 (with-current-buffer tar-data-buffer
461 (make-progress-reporter "Parsing tar file..."
462 (point-min) (point-max))))
463 descriptor)
464 (with-current-buffer tar-data-buffer
465 (while (and (<= (+ pos 512) (point-max))
466 (setq descriptor (tar-header-block-tokenize pos)))
467 (let ((size (tar-header-size descriptor)))
468 (if (< size 0)
469 (error "%s has size %s - corrupted"
470 (tar-header-name descriptor) size)))
471 ;;
472 ;; This is just too slow. Don't really need it anyway....
473 ;;(tar-header-block-check-checksum
474 ;; hblock (tar-header-block-checksum hblock)
475 ;; (tar-header-name descriptor))
476
477 (push descriptor result)
478 (setq pos (tar-header-data-end descriptor))
479 (progress-reporter-update progress-reporter pos)))
480
481 (set (make-local-variable 'tar-parse-info) (nreverse result))
482 ;; A tar file should end with a block or two of nulls,
483 ;; but let's not get a fatal error if it doesn't.
484 (if (null descriptor)
485 (progress-reporter-done progress-reporter)
486 (message "Warning: premature EOF parsing tar file"))
487 (goto-char (point-min))
488 (let ((inhibit-read-only t)
489 (total-summaries
490 (mapconcat 'tar-header-block-summarize tar-parse-info "\n")))
491 (insert total-summaries "\n"))
492 (goto-char (point-min))
493 (restore-buffer-modified-p modified)))
494 \f
495 (defvar tar-mode-map
496 (let ((map (make-keymap)))
497 (suppress-keymap map)
498 (define-key map " " 'tar-next-line)
499 (define-key map "C" 'tar-copy)
500 (define-key map "d" 'tar-flag-deleted)
501 (define-key map "\^D" 'tar-flag-deleted)
502 (define-key map "e" 'tar-extract)
503 (define-key map "f" 'tar-extract)
504 (define-key map "\C-m" 'tar-extract)
505 (define-key map [mouse-2] 'tar-mouse-extract)
506 (define-key map "g" 'revert-buffer)
507 (define-key map "h" 'describe-mode)
508 (define-key map "n" 'tar-next-line)
509 (define-key map "\^N" 'tar-next-line)
510 (define-key map [down] 'tar-next-line)
511 (define-key map "o" 'tar-extract-other-window)
512 (define-key map "p" 'tar-previous-line)
513 (define-key map "q" 'quit-window)
514 (define-key map "\^P" 'tar-previous-line)
515 (define-key map [up] 'tar-previous-line)
516 (define-key map "R" 'tar-rename-entry)
517 (define-key map "u" 'tar-unflag)
518 (define-key map "v" 'tar-view)
519 (define-key map "x" 'tar-expunge)
520 (define-key map "\177" 'tar-unflag-backwards)
521 (define-key map "E" 'tar-extract-other-window)
522 (define-key map "M" 'tar-chmod-entry)
523 (define-key map "G" 'tar-chgrp-entry)
524 (define-key map "O" 'tar-chown-entry)
525 ;; Let mouse-1 follow the link.
526 (define-key map [follow-link] 'mouse-face)
527
528 ;; Make menu bar items.
529
530 ;; Get rid of the Edit menu bar item to save space.
531 (define-key map [menu-bar edit] 'undefined)
532
533 (define-key map [menu-bar immediate]
534 (cons "Immediate" (make-sparse-keymap "Immediate")))
535
536 (define-key map [menu-bar immediate view]
537 '("View This File" . tar-view))
538 (define-key map [menu-bar immediate display]
539 '("Display in Other Window" . tar-display-other-window))
540 (define-key map [menu-bar immediate find-file-other-window]
541 '("Find in Other Window" . tar-extract-other-window))
542 (define-key map [menu-bar immediate find-file]
543 '("Find This File" . tar-extract))
544
545 (define-key map [menu-bar mark]
546 (cons "Mark" (make-sparse-keymap "Mark")))
547
548 (define-key map [menu-bar mark unmark-all]
549 '("Unmark All" . tar-clear-modification-flags))
550 (define-key map [menu-bar mark deletion]
551 '("Flag" . tar-flag-deleted))
552 (define-key map [menu-bar mark unmark]
553 '("Unflag" . tar-unflag))
554
555 (define-key map [menu-bar operate]
556 (cons "Operate" (make-sparse-keymap "Operate")))
557
558 (define-key map [menu-bar operate chown]
559 '("Change Owner..." . tar-chown-entry))
560 (define-key map [menu-bar operate chgrp]
561 '("Change Group..." . tar-chgrp-entry))
562 (define-key map [menu-bar operate chmod]
563 '("Change Mode..." . tar-chmod-entry))
564 (define-key map [menu-bar operate rename]
565 '("Rename to..." . tar-rename-entry))
566 (define-key map [menu-bar operate copy]
567 '("Copy to..." . tar-copy))
568 (define-key map [menu-bar operate expunge]
569 '("Expunge Marked Files" . tar-expunge))
570 \f
571 map)
572 "Local keymap for Tar mode listings.")
573
574 \f
575 ;; tar mode is suitable only for specially formatted data.
576 (put 'tar-mode 'mode-class 'special)
577 (put 'tar-subfile-mode 'mode-class 'special)
578
579 (defun tar-change-major-mode-hook ()
580 ;; Bring the actual Tar data back into the main buffer.
581 (when (tar-data-swapped-p) (buffer-swap-text tar-data-buffer))
582 ;; Throw away the summary.
583 (when (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
584
585 (defun tar-mode-kill-buffer-hook ()
586 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
587
588 ;;;###autoload
589 (define-derived-mode tar-mode nil "Tar"
590 "Major mode for viewing a tar file as a dired-like listing of its contents.
591 You can move around using the usual cursor motion commands.
592 Letters no longer insert themselves.
593 Type `e' to pull a file out of the tar file and into its own buffer;
594 or click mouse-2 on the file's line in the Tar mode buffer.
595 Type `c' to copy an entry from the tar file into another file on disk.
596
597 If you edit a sub-file of this archive (as with the `e' command) and
598 save it with \\[save-buffer], the contents of that buffer will be
599 saved back into the tar-file buffer; in this way you can edit a file
600 inside of a tar archive without extracting it and re-archiving it.
601
602 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
603 \\{tar-mode-map}"
604 ;; this is not interactive because you shouldn't be turning this
605 ;; mode on and off. You can corrupt things that way.
606 ;; rms: with permanent locals, it should now be possible to make this work
607 ;; interactively in some reasonable fashion.
608 (make-local-variable 'tar-parse-info)
609 (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
610 (set (make-local-variable 'local-enable-local-variables) nil)
611 (set (make-local-variable 'next-line-add-newlines) nil)
612 (set (make-local-variable 'tar-file-name-coding-system)
613 (or file-name-coding-system
614 default-file-name-coding-system
615 locale-coding-system))
616 ;; Prevent loss of data when saving the file.
617 (set (make-local-variable 'file-precious-flag) t)
618 (buffer-disable-undo)
619 (widen)
620 ;; Now move the Tar data into an auxiliary buffer, so we can use the main
621 ;; buffer for the summary.
622 (assert (not (tar-data-swapped-p)))
623 (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
624 (add-hook 'write-region-annotate-functions 'tar-write-region-annotate nil t)
625 (add-hook 'kill-buffer-hook 'tar-mode-kill-buffer-hook nil t)
626 (add-hook 'change-major-mode-hook 'tar-change-major-mode-hook nil t)
627 ;; Tar data is made of bytes, not chars.
628 (set-buffer-multibyte nil) ;Hopefully a no-op.
629 (set (make-local-variable 'tar-data-buffer)
630 (generate-new-buffer (format " *tar-data %s*"
631 (file-name-nondirectory
632 (or buffer-file-name (buffer-name))))))
633 (buffer-swap-text tar-data-buffer)
634 (tar-summarize-buffer)
635 (tar-next-line 0))
636
637
638 (defun tar-subfile-mode (p)
639 "Minor mode for editing an element of a tar-file.
640 This mode arranges for \"saving\" this buffer to write the data
641 into the tar-file buffer that it came from. The changes will actually
642 appear on disk when you save the tar-file's buffer."
643 (interactive "P")
644 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
645 (error "This buffer is not an element of a tar file"))
646 ;; Don't do this, because it is redundant and wastes mode line space.
647 ;; (or (assq 'tar-subfile-mode minor-mode-alist)
648 ;; (setq minor-mode-alist (append minor-mode-alist
649 ;; (list '(tar-subfile-mode " TarFile")))))
650 (make-local-variable 'tar-subfile-mode)
651 (setq tar-subfile-mode
652 (if (null p)
653 (not tar-subfile-mode)
654 (> (prefix-numeric-value p) 0)))
655 (cond (tar-subfile-mode
656 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
657 ;; turn off auto-save.
658 (auto-save-mode -1)
659 (setq buffer-auto-save-file-name nil)
660 (run-hooks 'tar-subfile-mode-hook))
661 (t
662 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
663
664
665 ;; Revert the buffer and recompute the dired-like listing.
666 (defun tar-mode-revert (&optional no-auto-save no-confirm)
667 (unwind-protect
668 (let ((revert-buffer-function nil))
669 (if (tar-data-swapped-p) (buffer-swap-text tar-data-buffer))
670 ;; FIXME: If we ask for confirmation, the user will be temporarily
671 ;; looking at the raw data.
672 (revert-buffer no-auto-save no-confirm 'preserve-modes)
673 ;; The new raw data may be smaller than the old summary, so let's
674 ;; make sure tar-data-swapped-p doesn't get confused.
675 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
676 ;; Recompute the summary.
677 (tar-mode))
678 (unless (tar-data-swapped-p) (buffer-swap-text tar-data-buffer))))
679
680
681 (defun tar-next-line (arg)
682 "Move cursor vertically down ARG lines and to the start of the filename."
683 (interactive "p")
684 (forward-line arg)
685 (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
686
687 (defun tar-previous-line (arg)
688 "Move cursor vertically up ARG lines and to the start of the filename."
689 (interactive "p")
690 (tar-next-line (- arg)))
691
692 (defun tar-current-descriptor (&optional noerror)
693 "Return the tar-descriptor of the current line, or signals an error."
694 ;; I wish lines had plists, like in ZMACS...
695 (or (nth (count-lines (point-min) (line-beginning-position))
696 tar-parse-info)
697 (if noerror
698 nil
699 (error "This line does not describe a tar-file entry"))))
700
701 (defun tar-get-descriptor ()
702 (let* ((descriptor (tar-current-descriptor))
703 (size (tar-header-size descriptor))
704 (link-p (tar-header-link-type descriptor)))
705 (if link-p
706 (error "This is %s, not a real file"
707 (cond ((eq link-p 5) "a directory")
708 ((eq link-p 20) "a tar directory header")
709 ((eq link-p 28) "a next has longname")
710 ((eq link-p 29) "a multivolume-continuation")
711 ((eq link-p 35) "a sparse entry")
712 ((eq link-p 38) "a volume header")
713 ((eq link-p 55) "an extended pax header")
714 (t "a link"))))
715 (if (zerop size) (message "This is a zero-length file"))
716 descriptor))
717
718 (defun tar-mouse-extract (event)
719 "Extract a file whose tar directory line you click on."
720 (interactive "e")
721 (with-current-buffer (window-buffer (posn-window (event-end event)))
722 (save-excursion
723 (goto-char (posn-point (event-end event)))
724 ;; Just make sure this doesn't get an error.
725 (tar-get-descriptor)))
726 (select-window (posn-window (event-end event)))
727 (goto-char (posn-point (event-end event)))
728 (tar-extract))
729
730 (defun tar-file-name-handler (op &rest args)
731 "Helper function for `tar-extract'."
732 (or (eq op 'file-exists-p)
733 (let ((file-name-handler-alist nil))
734 (apply op args))))
735
736 (defun tar-extract (&optional other-window-p)
737 "In Tar mode, extract this entry of the tar file into its own buffer."
738 (interactive)
739 (let* ((view-p (eq other-window-p 'view))
740 (descriptor (tar-get-descriptor))
741 (name (tar-header-name descriptor))
742 (size (tar-header-size descriptor))
743 (start (tar-header-data-start descriptor))
744 (end (+ start size)))
745 (let* ((tar-buffer (current-buffer))
746 (tarname (buffer-name))
747 (bufname (concat (file-name-nondirectory name)
748 " ("
749 tarname
750 ")"))
751 (read-only-p (or buffer-read-only view-p))
752 (new-buffer-file-name (expand-file-name
753 ;; `:' is not allowed on Windows
754 (concat tarname "!" name)))
755 (buffer (get-file-buffer new-buffer-file-name))
756 (just-created nil)
757 undo-list)
758 (unless buffer
759 (setq buffer (generate-new-buffer bufname))
760 (with-current-buffer buffer
761 (setq undo-list buffer-undo-list
762 buffer-undo-list t))
763 (setq bufname (buffer-name buffer))
764 (setq just-created t)
765 (with-current-buffer tar-data-buffer
766 (let (coding)
767 (narrow-to-region start end)
768 (goto-char start)
769 (setq coding (or coding-system-for-read
770 (and set-auto-coding-function
771 (funcall set-auto-coding-function
772 name (- end start)))
773 ;; The following binding causes
774 ;; find-buffer-file-type-coding-system
775 ;; (defined on dos-w32.el) to act as if
776 ;; the file being extracted existed, so
777 ;; that the file's contents' encoding and
778 ;; EOL format are auto-detected.
779 (let ((file-name-handler-alist
780 '(("" . tar-file-name-handler))))
781 (car (find-operation-coding-system
782 'insert-file-contents
783 (cons name (current-buffer)) t)))))
784 (if (or (not coding)
785 (eq (coding-system-type coding) 'undecided))
786 (setq coding (detect-coding-region start end t)))
787 (if (and default-enable-multibyte-characters
788 (coding-system-get coding :for-unibyte))
789 (with-current-buffer buffer
790 (set-buffer-multibyte nil)))
791 (widen)
792 (decode-coding-region start end coding buffer)))
793 (with-current-buffer buffer
794 (goto-char (point-min))
795 (setq buffer-file-name new-buffer-file-name)
796 (setq buffer-file-truename
797 (abbreviate-file-name buffer-file-name))
798 ;; Force buffer-file-coding-system to what
799 ;; decode-coding-region actually used.
800 (set-buffer-file-coding-system last-coding-system-used t)
801 ;; Set the default-directory to the dir of the
802 ;; superior buffer.
803 (setq default-directory
804 (with-current-buffer tar-buffer
805 default-directory))
806 (normal-mode) ; pick a mode.
807 (rename-buffer bufname)
808 (make-local-variable 'tar-superior-buffer)
809 (make-local-variable 'tar-superior-descriptor)
810 (setq tar-superior-buffer tar-buffer)
811 (setq tar-superior-descriptor descriptor)
812 (setq buffer-read-only read-only-p)
813 (set-buffer-modified-p nil)
814 (setq buffer-undo-list undo-list)
815 (tar-subfile-mode 1)))
816 (if view-p
817 (view-buffer
818 buffer (and just-created 'kill-buffer-if-not-modified))
819 (if (eq other-window-p 'display)
820 (display-buffer buffer)
821 (if other-window-p
822 (switch-to-buffer-other-window buffer)
823 (switch-to-buffer buffer)))))))
824
825
826 (defun tar-extract-other-window ()
827 "In Tar mode, find this entry of the tar file in another window."
828 (interactive)
829 (tar-extract t))
830
831 (defun tar-display-other-window ()
832 "In Tar mode, display this entry of the tar file in another window."
833 (interactive)
834 (tar-extract 'display))
835
836 (defun tar-view ()
837 "In Tar mode, view the tar file entry on this line."
838 (interactive)
839 (tar-extract 'view))
840
841
842 (defun tar-read-file-name (&optional prompt)
843 "Read a file name with this line's entry as the default."
844 (or prompt (setq prompt "Copy to: "))
845 (let* ((default-file (expand-file-name
846 (tar-header-name (tar-current-descriptor))))
847 (target (expand-file-name
848 (read-file-name prompt
849 (file-name-directory default-file)
850 default-file nil))))
851 (if (or (string= "" (file-name-nondirectory target))
852 (file-directory-p target))
853 (setq target (concat (if (string-match "/$" target)
854 (substring target 0 (1- (match-end 0)))
855 target)
856 "/"
857 (file-name-nondirectory default-file))))
858 target))
859
860
861 (defun tar-copy (&optional to-file)
862 "In Tar mode, extract this entry of the tar file into a file on disk.
863 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
864 the current tar-entry."
865 (interactive (list (tar-read-file-name)))
866 (let* ((descriptor (tar-get-descriptor))
867 (name (tar-header-name descriptor))
868 (size (tar-header-size descriptor))
869 (start (tar-header-data-start descriptor))
870 (end (+ start size))
871 (inhibit-file-name-handlers inhibit-file-name-handlers)
872 (inhibit-file-name-operation inhibit-file-name-operation))
873 (save-restriction
874 (widen)
875 ;; Inhibit compressing a subfile again if *both* name and
876 ;; to-file are handled by jka-compr
877 (if (and (eq (find-file-name-handler name 'write-region) 'jka-compr-handler)
878 (eq (find-file-name-handler to-file 'write-region) 'jka-compr-handler))
879 (setq inhibit-file-name-handlers
880 (cons 'jka-compr-handler
881 (and (eq inhibit-file-name-operation 'write-region)
882 inhibit-file-name-handlers))
883 inhibit-file-name-operation 'write-region))
884 (let ((coding-system-for-write 'no-conversion))
885 (write-region start end to-file nil nil nil t)))
886 (message "Copied tar entry %s to %s" name to-file)))
887
888 (defun tar-flag-deleted (p &optional unflag)
889 "In Tar mode, mark this sub-file to be deleted from the tar file.
890 With a prefix argument, mark that many files."
891 (interactive "p")
892 (beginning-of-line)
893 (dotimes (i (abs p))
894 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
895 (progn
896 (delete-char 1)
897 (insert (if unflag " " "D"))))
898 (forward-line (if (< p 0) -1 1)))
899 (if (eobp) nil (forward-char 36)))
900
901 (defun tar-unflag (p)
902 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
903 With a prefix argument, un-mark that many files forward."
904 (interactive "p")
905 (tar-flag-deleted p t))
906
907 (defun tar-unflag-backwards (p)
908 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
909 With a prefix argument, un-mark that many files backward."
910 (interactive "p")
911 (tar-flag-deleted (- p) t))
912
913
914 (defun tar-expunge-internal ()
915 "Expunge the tar-entry specified by the current line."
916 (let ((descriptor (tar-current-descriptor)))
917 ;;
918 ;; delete the current line...
919 (delete-region (line-beginning-position) (line-beginning-position 2))
920 ;;
921 ;; delete the data pointer...
922 (setq tar-parse-info (delq descriptor tar-parse-info))
923 ;;
924 ;; delete the data from inside the file...
925 (with-current-buffer tar-data-buffer
926 (delete-region (or (tar-header-header-start descriptor)
927 (- (tar-header-data-start descriptor) 512))
928 (tar-header-data-end descriptor)))))
929
930
931 (defun tar-expunge (&optional noconfirm)
932 "In Tar mode, delete all the archived files flagged for deletion.
933 This does not modify the disk image; you must save the tar file itself
934 for this to be permanent."
935 (interactive)
936 (if (or noconfirm
937 (y-or-n-p "Expunge files marked for deletion? "))
938 (let ((n 0))
939 (save-excursion
940 (goto-char (point-min))
941 (while (not (eobp))
942 (if (looking-at "D")
943 (progn (tar-expunge-internal)
944 (setq n (1+ n)))
945 (forward-line 1)))
946 ;; after doing the deletions, add any padding that may be necessary.
947 (tar-pad-to-blocksize))
948 (if (zerop n)
949 (message "Nothing to expunge.")
950 (message "%s files expunged. Be sure to save this buffer." n)))))
951
952
953 (defun tar-clear-modification-flags ()
954 "Remove the stars at the beginning of each line."
955 (interactive)
956 (save-excursion
957 (goto-char (point-min))
958 (while (not (eobp))
959 (if (not (eq (following-char) ?\s))
960 (progn (delete-char 1) (insert " ")))
961 (forward-line 1))))
962
963
964 (defun tar-chown-entry (new-uid)
965 "Change the user-id associated with this entry in the tar file.
966 If this tar file was written by GNU tar, then you will be able to edit
967 the user id as a string; otherwise, you must edit it as a number.
968 You can force editing as a number by calling this with a prefix arg.
969 This does not modify the disk image; you must save the tar file itself
970 for this to be permanent."
971 (interactive
972 (list
973 (let ((descriptor (tar-current-descriptor)))
974 (if (or current-prefix-arg
975 (not (tar-header-magic descriptor)))
976 (read-number
977 "New UID number: "
978 (format "%s" (tar-header-uid descriptor)))
979 (read-string "New UID string: " (tar-header-uname descriptor))))))
980 (cond ((stringp new-uid)
981 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
982 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
983 (t
984 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
985 (tar-alter-one-field tar-uid-offset
986 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
987
988
989 (defun tar-chgrp-entry (new-gid)
990 "Change the group-id associated with this entry in the tar file.
991 If this tar file was written by GNU tar, then you will be able to edit
992 the group id as a string; otherwise, you must edit it as a number.
993 You can force editing as a number by calling this with a prefix arg.
994 This does not modify the disk image; you must save the tar file itself
995 for this to be permanent."
996 (interactive
997 (list
998 (let ((descriptor (tar-current-descriptor)))
999 (if (or current-prefix-arg
1000 (not (tar-header-magic descriptor)))
1001 (read-number
1002 "New GID number: "
1003 (format "%s" (tar-header-gid descriptor)))
1004 (read-string "New GID string: " (tar-header-gname descriptor))))))
1005 (cond ((stringp new-gid)
1006 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1007 (tar-alter-one-field tar-gname-offset
1008 (concat new-gid "\000")))
1009 (t
1010 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1011 (tar-alter-one-field tar-gid-offset
1012 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1013
1014 (defun tar-rename-entry (new-name)
1015 "Change the name associated with this entry in the tar file.
1016 This does not modify the disk image; you must save the tar file itself
1017 for this to be permanent."
1018 (interactive
1019 (list (read-string "New name: "
1020 (tar-header-name (tar-current-descriptor)))))
1021 (if (string= "" new-name) (error "zero length name"))
1022 (let ((encoded-new-name (encode-coding-string new-name
1023 tar-file-name-coding-system))
1024 (descriptor (tar-current-descriptor))
1025 (prefix nil))
1026 (when (tar-header-header-start descriptor)
1027 ;; FIXME: Make it work for ././@LongLink.
1028 (error "Rename with @LongLink format is not implemented"))
1029
1030 (when (and (> (length encoded-new-name) 98)
1031 (string-match "/" encoded-new-name
1032 (- (length encoded-new-name) 99))
1033 (< (match-beginning 0) 155))
1034 (unless (equal (tar-header-magic descriptor) "ustar\0\0")
1035 (tar-alter-one-field tar-magic-offset "ustar\0\0"))
1036 (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
1037 (setq encoded-new-name (substring encoded-new-name (match-end 0))))
1038
1039 (if (> (length encoded-new-name) 98) (error "name too long"))
1040 (setf (tar-header-name descriptor) new-name)
1041 (tar-alter-one-field 0
1042 (substring (concat encoded-new-name (make-string 99 0)) 0 99))
1043 (if prefix
1044 (tar-alter-one-field tar-prefix-offset
1045 (substring (concat prefix (make-string 155 0)) 0 155)))))
1046
1047
1048 (defun tar-chmod-entry (new-mode)
1049 "Change the protection bits associated with this entry in the tar file.
1050 This does not modify the disk image; you must save the tar file itself
1051 for this to be permanent."
1052 (interactive (list (tar-parse-octal-integer-safe
1053 (read-string "New protection (octal): "))))
1054 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1055 (tar-alter-one-field tar-mode-offset
1056 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1057
1058
1059 (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
1060 (unless descriptor (setq descriptor (tar-current-descriptor)))
1061 ;;
1062 ;; update the header-line.
1063 (let ((col (current-column)))
1064 (delete-region (line-beginning-position)
1065 (prog2 (forward-line 1)
1066 (point)
1067 ;; Insert the new text after the old, before deleting,
1068 ;; to preserve markers such as the window start.
1069 (insert (tar-header-block-summarize descriptor) "\n")))
1070 (forward-line -1) (move-to-column col))
1071
1072 (assert (tar-data-swapped-p))
1073 (with-current-buffer tar-data-buffer
1074 (let* ((start (- (tar-header-data-start descriptor) 512)))
1075 ;;
1076 ;; delete the old field and insert a new one.
1077 (goto-char (+ start data-position))
1078 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1079 (assert (not (or enable-multibyte-characters
1080 (multibyte-string-p new-data-string))))
1081 (insert new-data-string)
1082 ;;
1083 ;; compute a new checksum and insert it.
1084 (let ((chk (tar-header-block-checksum
1085 (buffer-substring start (+ start 512)))))
1086 (goto-char (+ start tar-chk-offset))
1087 (delete-region (point) (+ (point) 8))
1088 (insert (format "%6o\0 " chk))
1089 (setf (tar-header-checksum descriptor) chk)
1090 ;;
1091 ;; ok, make sure we didn't botch it.
1092 (tar-header-block-check-checksum
1093 (buffer-substring start (+ start 512))
1094 chk (tar-header-name descriptor))
1095 ))))
1096
1097
1098 (defun tar-octal-time (timeval)
1099 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1100 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1101 (format "%05o%01o%05o"
1102 (lsh hibits -2)
1103 (logior (lsh (logand 3 hibits) 1)
1104 (if (> (logand lobits 32768) 0) 1 0))
1105 (logand 32767 lobits)
1106 )))
1107
1108 (defun tar-subfile-save-buffer ()
1109 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1110 This doesn't write anything to disk; you must save the parent tar-file buffer
1111 to make your changes permanent."
1112 (interactive)
1113 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1114 (error "This buffer has no superior tar file buffer"))
1115 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1116 (error "This buffer doesn't have an index into its superior tar file!"))
1117 (let ((subfile (current-buffer))
1118 (coding buffer-file-coding-system)
1119 (descriptor tar-superior-descriptor)
1120 subfile-size)
1121 (with-current-buffer tar-superior-buffer
1122 (let* ((start (tar-header-data-start descriptor))
1123 (name (tar-header-name descriptor))
1124 (size (tar-header-size descriptor))
1125 (head (memq descriptor tar-parse-info)))
1126 (if (not head)
1127 (error "Can't find this tar file entry in its parent tar file!"))
1128 (with-current-buffer tar-data-buffer
1129 ;; delete the old data...
1130 (let* ((data-start start)
1131 (data-end (+ data-start (tar-roundup-512 size))))
1132 (narrow-to-region data-start data-end)
1133 (delete-region (point-min) (point-max))
1134 ;; insert the new data...
1135 (goto-char data-start)
1136 (let ((dest (current-buffer)))
1137 (with-current-buffer subfile
1138 (save-restriction
1139 (widen)
1140 (encode-coding-region (point-min) (point-max) coding dest))))
1141 (setq subfile-size (- (point-max) (point-min)))
1142 ;;
1143 ;; pad the new data out to a multiple of 512...
1144 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1145 (goto-char (point-max))
1146 (insert (make-string (- subfile-size-pad subfile-size) 0))
1147 ;;
1148 ;; update the data of this files...
1149 (setf (tar-header-size descriptor) subfile-size)
1150 ;;
1151 ;; Update the size field in the header block.
1152 (widen))))
1153 ;;
1154 ;; alter the descriptor-line and header
1155 ;;
1156 (let ((position (- (length tar-parse-info) (length head))))
1157 (goto-char (point-min))
1158 (forward-line position)
1159 (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
1160 ;;
1161 ;; Maybe update the datestamp.
1162 (when tar-update-datestamp
1163 (tar-alter-one-field tar-time-offset
1164 (concat (tar-octal-time (current-time)) " "))))
1165 ;; After doing the insertion, add any necessary final padding.
1166 (tar-pad-to-blocksize))
1167 (set-buffer-modified-p t) ; mark the tar file as modified
1168 (tar-next-line 0))
1169 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1170 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1171 (buffer-name tar-superior-buffer))
1172 ;; Prevent basic-save-buffer from changing our coding-system.
1173 (setq last-coding-system-used buffer-file-coding-system)
1174 ;; Prevent ordinary saving from happening.
1175 t))
1176
1177
1178 ;; When this function is called, it is sure that the buffer is unibyte.
1179 (defun tar-pad-to-blocksize ()
1180 "If we are being anal about tar file blocksizes, fix up the current buffer.
1181 Leaves the region wide."
1182 (if (null tar-anal-blocksize)
1183 nil
1184 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1185 (start (tar-header-data-start last-desc))
1186 (link-p (tar-header-link-type last-desc))
1187 (size (if link-p 0 (tar-header-size last-desc)))
1188 (data-end (+ start size))
1189 (bbytes (ash tar-anal-blocksize 9))
1190 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
1191 ;; If the padding after the last data is too long, delete some;
1192 ;; else insert some until we are padded out to the right number of blocks.
1193 ;;
1194 (with-current-buffer tar-data-buffer
1195 (let ((goal-end (+ (point-min) pad-to)))
1196 (if (> (point-max) goal-end)
1197 (delete-region goal-end (point-max))
1198 (goto-char (point-max))
1199 (insert (make-string (- goal-end (point-max)) ?\0))))))))
1200
1201
1202 ;; Used in write-region-annotate-functions to write tar-files out correctly.
1203 (defun tar-write-region-annotate (start end)
1204 ;; When called from write-file (and auto-save), `start' is nil.
1205 ;; When called from M-x write-region, we assume the user wants to save
1206 ;; (part of) the summary, not the tar data.
1207 (unless (or start (not (tar-data-swapped-p)))
1208 (tar-clear-modification-flags)
1209 (set-buffer tar-data-buffer)
1210 nil))
1211 \f
1212 (provide 'tar-mode)
1213
1214 ;; arch-tag: 8a585a4a-340e-42c2-89e7-d3b1013a4b78
1215 ;;; tar-mode.el ends here