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