*** empty log message ***
[bpt/emacs.git] / lisp / tar-mode.el
1 ;;; tar-mode.el --- simple editing of tar files from GNU emacs
2
3 ;; Copyright (C) 1990,91,93,94,95,96,97,98,99,2000,2001
4 ;; 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 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
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.
40
41 ;; This code now understands the extra fields that GNU tar adds to tar files.
42
43 ;; This interacts correctly with "uncompress.el" in the Emacs library,
44 ;; which you get with
45 ;;
46 ;; (autoload 'uncompress-while-visiting "uncompress")
47 ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
48 ;; auto-mode-alist))
49 ;;
50 ;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
51
52 ;; *************** TO DO ***************
53 ;;
54 ;; o chmod should understand "a+x,og-w".
55 ;;
56 ;; o It's not possible to add a NEW file to a tar archive; not that
57 ;; important, but still...
58 ;;
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.
62 ;;
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.)
66 ;;
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.
71 ;;
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.
78 ;;
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.
81
82 ;; Rationale:
83
84 ;; Why does tar-mode edit the file itself instead of using tar?
85
86 ;; That means that you can edit tar files which you don't have room for
87 ;; on your local disk.
88
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.
95
96 ;;; Code:
97
98 (defgroup tar nil
99 "Simple editing of tar files."
100 :prefix "tar-"
101 :group 'data)
102
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)
113
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)
124
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)
130
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)
137
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)
143 \f
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)))))
157 \f
158 ;;; down to business.
159
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))
164
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))
179
180 (defmacro make-tar-desc (data-start tokens)
181 (list 'cons data-start tokens))
182
183 (defmacro tar-desc-data-start (x) (list 'car x))
184 (defmacro tar-desc-tokens (x) (list 'cdr x))
185
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-end-offset (+ tar-dmin-offset 8))
202
203 (defun tar-header-block-tokenize (string)
204 "Return a `tar-header' structure.
205 This is a list of name, mode, uid, gid, size,
206 write-date, checksum, link-type, and link-name."
207 (setq string (string-as-unibyte string))
208 (cond ((< (length string) 512) nil)
209 (;(some 'plusp string) ; <-- oops, massive cycle hog!
210 (or (not (= 0 (aref string 0))) ; This will do.
211 (not (= 0 (aref string 101))))
212 (let* ((name-end (1- tar-mode-offset))
213 (link-end (1- tar-magic-offset))
214 (uname-end (1- tar-gname-offset))
215 (gname-end (1- tar-dmaj-offset))
216 (link-p (aref string tar-linkp-offset))
217 (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
218 (uname-valid-p (or (string= "ustar " magic-str) (string= "GNUtar " magic-str)))
219 name linkname
220 (nulsexp "[^\000]*\000"))
221 (when (string-match nulsexp string tar-name-offset)
222 (setq name-end (min name-end (1- (match-end 0)))))
223 (when (string-match nulsexp string tar-link-offset)
224 (setq link-end (min link-end (1- (match-end 0)))))
225 (when (string-match nulsexp string tar-uname-offset)
226 (setq uname-end (min uname-end (1- (match-end 0)))))
227 (when (string-match nulsexp string tar-gname-offset)
228 (setq gname-end (min gname-end (1- (match-end 0)))))
229 (setq name (substring string tar-name-offset name-end)
230 link-p (if (or (= link-p 0) (= link-p ?0))
231 nil
232 (- link-p ?0)))
233 (setq linkname (substring string tar-link-offset link-end))
234 (if default-enable-multibyte-characters
235 (setq name
236 (decode-coding-string name tar-file-name-coding-system)
237 linkname
238 (decode-coding-string linkname
239 tar-file-name-coding-system)))
240 (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
241 (make-tar-header
242 name
243 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset)
244 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset)
245 (tar-parse-octal-integer string tar-gid-offset tar-size-offset)
246 (tar-parse-octal-integer string tar-size-offset tar-time-offset)
247 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset)
248 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset)
249 link-p
250 linkname
251 uname-valid-p
252 (and uname-valid-p (substring string tar-uname-offset uname-end))
253 (and uname-valid-p (substring string tar-gname-offset gname-end))
254 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset)
255 (tar-parse-octal-integer string tar-dmin-offset tar-end-offset)
256 )))
257 (t 'empty-tar-block)))
258
259
260 (defun tar-parse-octal-integer (string &optional start end)
261 (if (null start) (setq start 0))
262 (if (null end) (setq end (length string)))
263 (if (= (aref string start) 0)
264 0
265 (let ((n 0))
266 (while (< start end)
267 (setq n (if (< (aref string start) ?0) n
268 (+ (* n 8) (- (aref string start) ?0)))
269 start (1+ start)))
270 n)))
271
272 (defun tar-parse-octal-long-integer (string &optional start end)
273 (if (null start) (setq start 0))
274 (if (null end) (setq end (length string)))
275 (if (= (aref string start) 0)
276 (list 0 0)
277 (let ((lo 0)
278 (hi 0))
279 (while (< start end)
280 (if (>= (aref string start) ?0)
281 (setq lo (+ (* lo 8) (- (aref string start) ?0))
282 hi (+ (* hi 8) (ash lo -16))
283 lo (logand lo 65535)))
284 (setq start (1+ start)))
285 (list hi lo))))
286
287 (defun tar-parse-octal-integer-safe (string)
288 (let ((L (length string)))
289 (if (= L 0) (error "empty string"))
290 (dotimes (i L)
291 (if (or (< (aref string i) ?0)
292 (> (aref string i) ?7))
293 (error "`%c' is not an octal digit"))))
294 (tar-parse-octal-integer string))
295
296
297 (defun tar-header-block-checksum (string)
298 "Compute and return a tar-acceptable checksum for this block."
299 (setq string (string-as-unibyte string))
300 (let* ((chk-field-start tar-chk-offset)
301 (chk-field-end (+ chk-field-start 8))
302 (sum 0)
303 (i 0))
304 ;; Add up all of the characters except the ones in the checksum field.
305 ;; Add that field as if it were filled with spaces.
306 (while (< i chk-field-start)
307 (setq sum (+ sum (aref string i))
308 i (1+ i)))
309 (setq i chk-field-end)
310 (while (< i 512)
311 (setq sum (+ sum (aref string i))
312 i (1+ i)))
313 (+ sum (* 32 8))))
314
315 (defun tar-header-block-check-checksum (hblock desired-checksum file-name)
316 "Beep and print a warning if the checksum doesn't match."
317 (if (not (= desired-checksum (tar-header-block-checksum hblock)))
318 (progn (beep) (message "Invalid checksum for file %s!" file-name))))
319
320 (defun tar-clip-time-string (time)
321 (let ((str (current-time-string time)))
322 (concat " " (substring str 4 16) (substring str 19 24))))
323
324 (defun tar-grind-file-mode (mode)
325 "Construct a `-rw--r--r--' string indicating MODE.
326 MODE should be an integer which is a file mode value."
327 (string
328 (if (zerop (logand 256 mode)) ?- ?r)
329 (if (zerop (logand 128 mode)) ?- ?w)
330 (if (zerop (logand 1024 mode)) (if (zerop (logand 64 mode)) ?- ?x) ?s)
331 (if (zerop (logand 32 mode)) ?- ?r)
332 (if (zerop (logand 16 mode)) ?- ?w)
333 (if (zerop (logand 2048 mode)) (if (zerop (logand 8 mode)) ?- ?x) ?s)
334 (if (zerop (logand 4 mode)) ?- ?r)
335 (if (zerop (logand 2 mode)) ?- ?w)
336 (if (zerop (logand 1 mode)) ?- ?x)))
337
338 (defun tar-header-block-summarize (tar-hblock &optional mod-p)
339 "Return a line similar to the output of `tar -vtf'."
340 (let ((name (tar-header-name tar-hblock))
341 (mode (tar-header-mode tar-hblock))
342 (uid (tar-header-uid tar-hblock))
343 (gid (tar-header-gid tar-hblock))
344 (uname (tar-header-uname tar-hblock))
345 (gname (tar-header-gname tar-hblock))
346 (size (tar-header-size tar-hblock))
347 (time (tar-header-date tar-hblock))
348 (ck (tar-header-checksum tar-hblock))
349 (type (tar-header-link-type tar-hblock))
350 (link-name (tar-header-link-name tar-hblock)))
351 (format "%c%c%s%8s/%-8s%7s%s %s%s"
352 (if mod-p ?* ? )
353 (cond ((or (eq type nil) (eq type 0)) ?-)
354 ((eq type 1) ?h) ; link
355 ((eq type 2) ?l) ; symlink
356 ((eq type 3) ?c) ; char special
357 ((eq type 4) ?b) ; block special
358 ((eq type 5) ?d) ; directory
359 ((eq type 6) ?p) ; FIFO/pipe
360 ((eq type 20) ?*) ; directory listing
361 ((eq type 28) ?L) ; next has longname
362 ((eq type 29) ?M) ; multivolume continuation
363 ((eq type 35) ?S) ; sparse
364 ((eq type 38) ?V) ; volume header
365 (t ?\ )
366 )
367 (tar-grind-file-mode mode)
368 (if (= 0 (length uname)) uid uname)
369 (if (= 0 (length gname)) gid gname)
370 size
371 (if tar-mode-show-date (tar-clip-time-string time) "")
372 (propertize name
373 'mouse-face 'highlight
374 'help-echo "mouse-2: extract this file into a buffer")
375 (if (or (eq type 1) (eq type 2))
376 (concat (if (= type 1) " ==> " " --> ") link-name)
377 ""))))
378
379 (defun tar-untar-buffer ()
380 "Extract all archive members in the tar-file into the current directory."
381 (interactive)
382 (let ((multibyte enable-multibyte-characters))
383 (unwind-protect
384 (save-restriction
385 (widen)
386 (set-buffer-multibyte nil)
387 (dolist (descriptor tar-parse-info)
388 (let* ((tokens (tar-desc-tokens descriptor))
389 (name (tar-header-name tokens))
390 (dir (file-name-directory name))
391 (start (+ (tar-desc-data-start descriptor)
392 (- tar-header-offset (point-min))))
393 (end (+ start (tar-header-size tokens))))
394 (unless (file-directory-p name)
395 (message "Extracting %s" name)
396 (if (and dir (not (file-exists-p dir)))
397 (make-directory dir t))
398 (unless (file-directory-p name)
399 (write-region start end name))
400 (set-file-modes name (tar-header-mode tokens))))))
401 (set-buffer-multibyte multibyte))))
402
403 (defun tar-summarize-buffer ()
404 "Parse the contents of the tar file in the current buffer.
405 Place a dired-like listing on the front;
406 then narrow to it, so that only that listing
407 is visible (and the real data of the buffer is hidden)."
408 (message "Parsing tar file...")
409 (let* ((result '())
410 (pos (point-min))
411 (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
412 (bs100 (max 1 (/ bs 100)))
413 tokens)
414 (while (and (<= (+ pos 512) (point-max))
415 (not (eq 'empty-tar-block
416 (setq tokens
417 (tar-header-block-tokenize
418 (buffer-substring pos (+ pos 512)))))))
419 (setq pos (+ pos 512))
420 (message "Parsing tar file...%d%%"
421 ;(/ (* pos 100) bs) ; this gets round-off lossage
422 (/ pos bs100) ; this doesn't
423 )
424 (if (eq (tar-header-link-type tokens) 20)
425 ;; Foo. There's an extra empty block after these.
426 (setq pos (+ pos 512)))
427 (let ((size (tar-header-size tokens)))
428 (if (< size 0)
429 (error "%s has size %s - corrupted"
430 (tar-header-name tokens) size))
431 ;
432 ; This is just too slow. Don't really need it anyway....
433 ;(tar-header-block-check-checksum
434 ; hblock (tar-header-block-checksum hblock)
435 ; (tar-header-name tokens))
436
437 (setq result (cons (make-tar-desc pos tokens) result))
438
439 (and (null (tar-header-link-type tokens))
440 (> size 0)
441 (setq pos
442 (+ pos 512 (ash (ash (1- size) -9) 9)) ; this works
443 ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
444 ))))
445 (make-local-variable 'tar-parse-info)
446 (setq tar-parse-info (nreverse result))
447 ;; A tar file should end with a block or two of nulls,
448 ;; but let's not get a fatal error if it doesn't.
449 (if (eq tokens 'empty-tar-block)
450 (message "Parsing tar file...done")
451 (message "Warning: premature EOF parsing tar file")))
452 (save-excursion
453 (goto-char (point-min))
454 (let ((buffer-read-only nil)
455 (summaries nil))
456 ;; Collect summary lines and insert them all at once since tar files
457 ;; can be pretty big.
458 (dolist (tar-desc (reverse tar-parse-info))
459 (setq summaries
460 (cons (tar-header-block-summarize (tar-desc-tokens tar-desc))
461 (cons "\n"
462 summaries))))
463 (let ((total-summaries (apply 'concat summaries)))
464 (insert total-summaries))
465 (make-local-variable 'tar-header-offset)
466 (setq tar-header-offset (point))
467 (narrow-to-region (point-min) tar-header-offset)
468 (set-buffer-modified-p nil))))
469 \f
470 (defvar tar-mode-map nil "*Local keymap for Tar mode listings.")
471
472 (if tar-mode-map
473 nil
474 (setq tar-mode-map (make-keymap))
475 (suppress-keymap tar-mode-map)
476 (define-key tar-mode-map " " 'tar-next-line)
477 (define-key tar-mode-map "C" 'tar-copy)
478 (define-key tar-mode-map "d" 'tar-flag-deleted)
479 (define-key tar-mode-map "\^D" 'tar-flag-deleted)
480 (define-key tar-mode-map "e" 'tar-extract)
481 (define-key tar-mode-map "f" 'tar-extract)
482 (define-key tar-mode-map "\C-m" 'tar-extract)
483 (define-key tar-mode-map [mouse-2] 'tar-mouse-extract)
484 (define-key tar-mode-map "g" 'revert-buffer)
485 (define-key tar-mode-map "h" 'describe-mode)
486 (define-key tar-mode-map "n" 'tar-next-line)
487 (define-key tar-mode-map "\^N" 'tar-next-line)
488 (define-key tar-mode-map [down] 'tar-next-line)
489 (define-key tar-mode-map "o" 'tar-extract-other-window)
490 (define-key tar-mode-map "p" 'tar-previous-line)
491 (define-key tar-mode-map "q" 'quit-window)
492 (define-key tar-mode-map "\^P" 'tar-previous-line)
493 (define-key tar-mode-map [up] 'tar-previous-line)
494 (define-key tar-mode-map "R" 'tar-rename-entry)
495 (define-key tar-mode-map "u" 'tar-unflag)
496 (define-key tar-mode-map "v" 'tar-view)
497 (define-key tar-mode-map "x" 'tar-expunge)
498 (define-key tar-mode-map "\177" 'tar-unflag-backwards)
499 (define-key tar-mode-map "E" 'tar-extract-other-window)
500 (define-key tar-mode-map "M" 'tar-chmod-entry)
501 (define-key tar-mode-map "G" 'tar-chgrp-entry)
502 (define-key tar-mode-map "O" 'tar-chown-entry)
503 )
504 \f
505 ;; Make menu bar items.
506
507 ;; Get rid of the Edit menu bar item to save space.
508 (define-key tar-mode-map [menu-bar edit] 'undefined)
509
510 (define-key tar-mode-map [menu-bar immediate]
511 (cons "Immediate" (make-sparse-keymap "Immediate")))
512
513 (define-key tar-mode-map [menu-bar immediate view]
514 '("View This File" . tar-view))
515 (define-key tar-mode-map [menu-bar immediate display]
516 '("Display in Other Window" . tar-display-other-window))
517 (define-key tar-mode-map [menu-bar immediate find-file-other-window]
518 '("Find in Other Window" . tar-extract-other-window))
519 (define-key tar-mode-map [menu-bar immediate find-file]
520 '("Find This File" . tar-extract))
521
522 (define-key tar-mode-map [menu-bar mark]
523 (cons "Mark" (make-sparse-keymap "Mark")))
524
525 (define-key tar-mode-map [menu-bar mark unmark-all]
526 '("Unmark All" . tar-clear-modification-flags))
527 (define-key tar-mode-map [menu-bar mark deletion]
528 '("Flag" . tar-flag-deleted))
529 (define-key tar-mode-map [menu-bar mark unmark]
530 '("Unflag" . tar-unflag))
531
532 (define-key tar-mode-map [menu-bar operate]
533 (cons "Operate" (make-sparse-keymap "Operate")))
534
535 (define-key tar-mode-map [menu-bar operate chown]
536 '("Change Owner..." . tar-chown-entry))
537 (define-key tar-mode-map [menu-bar operate chgrp]
538 '("Change Group..." . tar-chgrp-entry))
539 (define-key tar-mode-map [menu-bar operate chmod]
540 '("Change Mode..." . tar-chmod-entry))
541 (define-key tar-mode-map [menu-bar operate rename]
542 '("Rename to..." . tar-rename-entry))
543 (define-key tar-mode-map [menu-bar operate copy]
544 '("Copy to..." . tar-copy))
545 (define-key tar-mode-map [menu-bar operate expunge]
546 '("Expunge Marked Files" . tar-expunge))
547 \f
548 ;; tar mode is suitable only for specially formatted data.
549 (put 'tar-mode 'mode-class 'special)
550 (put 'tar-subfile-mode 'mode-class 'special)
551
552 ;;;###autoload
553 (define-derived-mode tar-mode nil "Tar"
554 "Major mode for viewing a tar file as a dired-like listing of its contents.
555 You can move around using the usual cursor motion commands.
556 Letters no longer insert themselves.
557 Type `e' to pull a file out of the tar file and into its own buffer;
558 or click mouse-2 on the file's line in the Tar mode buffer.
559 Type `c' to copy an entry from the tar file into another file on disk.
560
561 If you edit a sub-file of this archive (as with the `e' command) and
562 save it with Control-x Control-s, the contents of that buffer will be
563 saved back into the tar-file buffer; in this way you can edit a file
564 inside of a tar archive without extracting it and re-archiving it.
565
566 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
567 \\{tar-mode-map}"
568 ;; this is not interactive because you shouldn't be turning this
569 ;; mode on and off. You can corrupt things that way.
570 ;; rms: with permanent locals, it should now be possible to make this work
571 ;; interactively in some reasonable fashion.
572 (make-local-variable 'tar-header-offset)
573 (make-local-variable 'tar-parse-info)
574 (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
575 (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
576 (set (make-local-variable 'local-enable-local-variables) nil)
577 (set (make-local-variable 'next-line-add-newlines) nil)
578 (set (make-local-variable 'tar-file-name-coding-system)
579 (or file-name-coding-system
580 default-file-name-coding-system
581 locale-coding-system))
582 ;; Prevent loss of data when saving the file.
583 (set (make-local-variable 'file-precious-flag) t)
584 (auto-save-mode 0)
585 (set (make-local-variable 'write-contents-hooks) '(tar-mode-write-file))
586 (widen)
587 (if (and (boundp 'tar-header-offset) tar-header-offset)
588 (narrow-to-region (point-min) tar-header-offset)
589 (tar-summarize-buffer)
590 (tar-next-line 0)))
591
592
593 (defun tar-subfile-mode (p)
594 "Minor mode for editing an element of a tar-file.
595 This mode arranges for \"saving\" this buffer to write the data
596 into the tar-file buffer that it came from. The changes will actually
597 appear on disk when you save the tar-file's buffer."
598 (interactive "P")
599 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
600 (error "This buffer is not an element of a tar file"))
601 ;; Don't do this, because it is redundant and wastes mode line space.
602 ;; (or (assq 'tar-subfile-mode minor-mode-alist)
603 ;; (setq minor-mode-alist (append minor-mode-alist
604 ;; (list '(tar-subfile-mode " TarFile")))))
605 (make-local-variable 'tar-subfile-mode)
606 (setq tar-subfile-mode
607 (if (null p)
608 (not tar-subfile-mode)
609 (> (prefix-numeric-value p) 0)))
610 (cond (tar-subfile-mode
611 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
612 ;; turn off auto-save.
613 (auto-save-mode -1)
614 (setq buffer-auto-save-file-name nil)
615 (run-hooks 'tar-subfile-mode-hook))
616 (t
617 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
618
619
620 ;; Revert the buffer and recompute the dired-like listing.
621 (defun tar-mode-revert (&optional no-auto-save no-confirm)
622 (let ((revert-buffer-function nil)
623 (old-offset tar-header-offset)
624 success)
625 (setq tar-header-offset nil)
626 (unwind-protect
627 (and (revert-buffer t no-confirm)
628 (progn (widen)
629 (setq success t)
630 (tar-mode)))
631 ;; If the revert was canceled,
632 ;; put back the old value of tar-header-offset.
633 (or success
634 (setq tar-header-offset old-offset)))))
635
636
637 (defun tar-next-line (arg)
638 "Move cursor vertically down ARG lines and to the start of the filename."
639 (interactive "p")
640 (forward-line arg)
641 (if (eobp) nil (forward-char (if tar-mode-show-date 54 36))))
642
643 (defun tar-previous-line (arg)
644 "Move cursor vertically up ARG lines and to the start of the filename."
645 (interactive "p")
646 (tar-next-line (- arg)))
647
648 (defun tar-current-descriptor (&optional noerror)
649 "Return the tar-descriptor of the current line, or signals an error."
650 ;; I wish lines had plists, like in ZMACS...
651 (or (nth (count-lines (point-min)
652 (save-excursion (beginning-of-line) (point)))
653 tar-parse-info)
654 (if noerror
655 nil
656 (error "This line does not describe a tar-file entry"))))
657
658 (defun tar-get-descriptor ()
659 (let* ((descriptor (tar-current-descriptor))
660 (tokens (tar-desc-tokens descriptor))
661 (size (tar-header-size tokens))
662 (link-p (tar-header-link-type tokens)))
663 (if link-p
664 (error "This is a %s, not a real file"
665 (cond ((eq link-p 5) "directory")
666 ((eq link-p 20) "tar directory header")
667 ((eq link-p 28) "next has longname")
668 ((eq link-p 29) "multivolume-continuation")
669 ((eq link-p 35) "sparse entry")
670 ((eq link-p 38) "volume header")
671 (t "link"))))
672 (if (zerop size) (error "This is a zero-length file"))
673 descriptor))
674
675 (defun tar-mouse-extract (event)
676 "Extract a file whose tar directory line you click on."
677 (interactive "e")
678 (save-excursion
679 (set-buffer (window-buffer (posn-window (event-end event))))
680 (save-excursion
681 (goto-char (posn-point (event-end event)))
682 ;; Just make sure this doesn't get an error.
683 (tar-get-descriptor)))
684 (select-window (posn-window (event-end event)))
685 (goto-char (posn-point (event-end event)))
686 (tar-extract))
687
688 (defun tar-extract (&optional other-window-p)
689 "In Tar mode, extract this entry of the tar file into its own buffer."
690 (interactive)
691 (let* ((view-p (eq other-window-p 'view))
692 (descriptor (tar-get-descriptor))
693 (tokens (tar-desc-tokens descriptor))
694 (name (tar-header-name tokens))
695 (size (tar-header-size tokens))
696 (start (+ (tar-desc-data-start descriptor)
697 (- tar-header-offset (point-min))))
698 (end (+ start size)))
699 (let* ((tar-buffer (current-buffer))
700 (tarname (buffer-name))
701 (bufname (concat (file-name-nondirectory name)
702 " ("
703 tarname
704 ")"))
705 (read-only-p (or buffer-read-only view-p))
706 (new-buffer-file-name (expand-file-name
707 ;; `:' is not allowed on Windows
708 (concat tarname "!" name)))
709 (buffer (get-file-buffer new-buffer-file-name))
710 (just-created nil)
711 (pos (point)))
712 (unless buffer
713 (setq buffer (generate-new-buffer bufname))
714 (setq bufname (buffer-name buffer))
715 (setq just-created t)
716 (unwind-protect
717 (let (coding)
718 (narrow-to-region start end)
719 (goto-char start)
720 (setq coding (or coding-system-for-read
721 (and set-auto-coding-function
722 (funcall set-auto-coding-function
723 name (- end start)))))
724 (if (or (not coding)
725 (eq (coding-system-type coding) 'undecided))
726 (setq coding (detect-coding-region start end t)))
727 (if (and default-enable-multibyte-characters
728 (coding-system-get coding :for-unibyte))
729 (save-excursion
730 (set-buffer buffer)
731 (set-buffer-multibyte nil)))
732 (widen)
733 (decode-coding-region start end coding buffer)
734 (save-excursion
735 (set-buffer buffer)
736 (goto-char (point-min))
737 (setq buffer-file-name new-buffer-file-name)
738 (setq buffer-file-truename
739 (abbreviate-file-name buffer-file-name))
740 (set-buffer-file-coding-system coding)
741 ;; Set the default-directory to the dir of the
742 ;; superior buffer.
743 (setq default-directory
744 (save-excursion
745 (set-buffer tar-buffer)
746 default-directory))
747 (normal-mode) ; pick a mode.
748 (rename-buffer bufname)
749 (make-local-variable 'tar-superior-buffer)
750 (make-local-variable 'tar-superior-descriptor)
751 (setq tar-superior-buffer tar-buffer)
752 (setq tar-superior-descriptor descriptor)
753 (setq buffer-read-only read-only-p)
754 (set-buffer-modified-p nil)
755 (tar-subfile-mode 1))
756 (set-buffer tar-buffer))
757 (narrow-to-region (point-min) tar-header-offset)
758 (goto-char pos)))
759 (if view-p
760 (view-buffer buffer (and just-created 'kill-buffer))
761 (if (eq other-window-p 'display)
762 (display-buffer buffer)
763 (if other-window-p
764 (switch-to-buffer-other-window buffer)
765 (switch-to-buffer buffer)))))))
766
767
768 (defun tar-extract-other-window ()
769 "*In Tar mode, find this entry of the tar file in another window."
770 (interactive)
771 (tar-extract t))
772
773 (defun tar-display-other-window ()
774 "*In Tar mode, display this entry of the tar file in another window."
775 (interactive)
776 (tar-extract 'display))
777
778 (defun tar-view ()
779 "*In Tar mode, view the tar file entry on this line."
780 (interactive)
781 (tar-extract 'view))
782
783
784 (defun tar-read-file-name (&optional prompt)
785 "Read a file name with this line's entry as the default."
786 (or prompt (setq prompt "Copy to: "))
787 (let* ((default-file (expand-file-name
788 (tar-header-name (tar-desc-tokens
789 (tar-current-descriptor)))))
790 (target (expand-file-name
791 (read-file-name prompt
792 (file-name-directory default-file)
793 default-file nil))))
794 (if (or (string= "" (file-name-nondirectory target))
795 (file-directory-p target))
796 (setq target (concat (if (string-match "/$" target)
797 (substring target 0 (1- (match-end 0)))
798 target)
799 "/"
800 (file-name-nondirectory default-file))))
801 target))
802
803
804 (defun tar-copy (&optional to-file)
805 "*In Tar mode, extract this entry of the tar file into a file on disk.
806 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
807 the current tar-entry."
808 (interactive (list (tar-read-file-name)))
809 (let* ((descriptor (tar-get-descriptor))
810 (tokens (tar-desc-tokens descriptor))
811 (name (tar-header-name tokens))
812 (size (tar-header-size tokens))
813 (start (+ (tar-desc-data-start descriptor)
814 (- tar-header-offset (point-min))))
815 (end (+ start size))
816 (inhibit-file-name-handlers inhibit-file-name-handlers)
817 (inhibit-file-name-operation inhibit-file-name-operation))
818 (save-restriction
819 (widen)
820 ;; Inhibit compressing a subfile again if *both* name and
821 ;; to-file are handled by jka-compr
822 (if (and (eq (find-file-name-handler name 'write-region) 'jka-compr-handler)
823 (eq (find-file-name-handler to-file 'write-region) 'jka-compr-handler))
824 (setq inhibit-file-name-handlers
825 (cons 'jka-compr-handler
826 (and (eq inhibit-file-name-operation 'write-region)
827 inhibit-file-name-handlers))
828 inhibit-file-name-operation 'write-region))
829 (let ((coding-system-for-write 'no-conversion))
830 (write-region start end to-file nil nil nil t)))
831 (message "Copied tar entry %s to %s" name to-file)))
832
833 (defun tar-flag-deleted (p &optional unflag)
834 "*In Tar mode, mark this sub-file to be deleted from the tar file.
835 With a prefix argument, mark that many files."
836 (interactive "p")
837 (beginning-of-line)
838 (dotimes (i (if (< p 0) (- p) p))
839 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
840 (progn
841 (delete-char 1)
842 (insert (if unflag " " "D"))))
843 (forward-line (if (< p 0) -1 1)))
844 (if (eobp) nil (forward-char 36)))
845
846 (defun tar-unflag (p)
847 "*In Tar mode, un-mark this sub-file if it is marked to be deleted.
848 With a prefix argument, un-mark that many files forward."
849 (interactive "p")
850 (tar-flag-deleted p t))
851
852 (defun tar-unflag-backwards (p)
853 "*In Tar mode, un-mark this sub-file if it is marked to be deleted.
854 With a prefix argument, un-mark that many files backward."
855 (interactive "p")
856 (tar-flag-deleted (- p) t))
857
858
859 (defun tar-expunge-internal ()
860 "Expunge the tar-entry specified by the current line."
861 (let* ((descriptor (tar-current-descriptor))
862 (tokens (tar-desc-tokens descriptor))
863 (line (tar-desc-data-start descriptor))
864 (name (tar-header-name tokens))
865 (size (tar-header-size tokens))
866 (link-p (tar-header-link-type tokens))
867 (start (tar-desc-data-start descriptor))
868 (following-descs (cdr (memq descriptor tar-parse-info))))
869 (if link-p (setq size 0)) ; size lies for hard-links.
870 ;;
871 ;; delete the current line...
872 (beginning-of-line)
873 (let ((line-start (point)))
874 (end-of-line) (forward-char)
875 (let ((line-len (- (point) line-start)))
876 (delete-region line-start (point))
877 ;;
878 ;; decrement the header-pointer to be in sync...
879 (setq tar-header-offset (- tar-header-offset line-len))))
880 ;;
881 ;; delete the data pointer...
882 (setq tar-parse-info (delq descriptor tar-parse-info))
883 ;;
884 ;; delete the data from inside the file...
885 (widen)
886 (let* ((data-start (+ start tar-header-offset -513))
887 (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
888 (delete-region data-start data-end)
889 ;;
890 ;; and finally, decrement the start-pointers of all following
891 ;; entries in the archive. This is a pig when deleting a bunch
892 ;; of files at once - we could optimize this to only do the
893 ;; iteration over the files that remain, or only iterate up to
894 ;; the next file to be deleted.
895 (let ((data-length (- data-end data-start)))
896 (dolist (desc following-descs)
897 (tar-setf (tar-desc-data-start desc)
898 (- (tar-desc-data-start desc) data-length))))
899 ))
900 (narrow-to-region (point-min) tar-header-offset))
901
902
903 (defun tar-expunge (&optional noconfirm)
904 "*In Tar mode, delete all the archived files flagged for deletion.
905 This does not modify the disk image; you must save the tar file itself
906 for this to be permanent."
907 (interactive)
908 (if (or noconfirm
909 (y-or-n-p "Expunge files marked for deletion? "))
910 (let ((n 0))
911 (save-excursion
912 (goto-char (point-min))
913 (while (not (eobp))
914 (if (looking-at "D")
915 (progn (tar-expunge-internal)
916 (setq n (1+ n)))
917 (forward-line 1)))
918 ;; after doing the deletions, add any padding that may be necessary.
919 (tar-pad-to-blocksize)
920 (narrow-to-region (point-min) tar-header-offset))
921 (if (zerop n)
922 (message "Nothing to expunge.")
923 (message "%s files expunged. Be sure to save this buffer." n)))))
924
925
926 (defun tar-clear-modification-flags ()
927 "Remove the stars at the beginning of each line."
928 (interactive)
929 (save-excursion
930 (goto-char (point-min))
931 (while (< (point) tar-header-offset)
932 (if (not (eq (following-char) ?\ ))
933 (progn (delete-char 1) (insert " ")))
934 (forward-line 1))))
935
936
937 (defun tar-chown-entry (new-uid)
938 "*Change the user-id associated with this entry in the tar file.
939 If this tar file was written by GNU tar, then you will be able to edit
940 the user id as a string; otherwise, you must edit it as a number.
941 You can force editing as a number by calling this with a prefix arg.
942 This does not modify the disk image; you must save the tar file itself
943 for this to be permanent."
944 (interactive (list
945 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
946 (if (or current-prefix-arg
947 (not (tar-header-magic tokens)))
948 (let (n)
949 (while (not (numberp (setq n (read-minibuffer
950 "New UID number: "
951 (format "%s" (tar-header-uid tokens)))))))
952 n)
953 (read-string "New UID string: " (tar-header-uname tokens))))))
954 (cond ((stringp new-uid)
955 (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
956 new-uid)
957 (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
958 (t
959 (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
960 new-uid)
961 (tar-alter-one-field tar-uid-offset
962 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
963
964
965 (defun tar-chgrp-entry (new-gid)
966 "*Change the group-id associated with this entry in the tar file.
967 If this tar file was written by GNU tar, then you will be able to edit
968 the group id as a string; otherwise, you must edit it as a number.
969 You can force editing as a number by calling this with a prefix arg.
970 This does not modify the disk image; you must save the tar file itself
971 for this to be permanent."
972 (interactive (list
973 (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
974 (if (or current-prefix-arg
975 (not (tar-header-magic tokens)))
976 (let (n)
977 (while (not (numberp (setq n (read-minibuffer
978 "New GID number: "
979 (format "%s" (tar-header-gid tokens)))))))
980 n)
981 (read-string "New GID string: " (tar-header-gname tokens))))))
982 (cond ((stringp new-gid)
983 (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
984 new-gid)
985 (tar-alter-one-field tar-gname-offset
986 (concat new-gid "\000")))
987 (t
988 (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
989 new-gid)
990 (tar-alter-one-field tar-gid-offset
991 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
992
993 (defun tar-rename-entry (new-name)
994 "*Change the name associated with this entry in the tar file.
995 This does not modify the disk image; you must save the tar file itself
996 for this to be permanent."
997 (interactive
998 (list (read-string "New name: "
999 (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
1000 (if (string= "" new-name) (error "zero length name"))
1001 (let ((encoded-new-name (encode-coding-string new-name
1002 tar-file-name-coding-system)))
1003 (if (> (length encoded-new-name) 98) (error "name too long"))
1004 (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
1005 new-name)
1006 (tar-alter-one-field 0
1007 (substring (concat encoded-new-name (make-string 99 0)) 0 99))))
1008
1009
1010 (defun tar-chmod-entry (new-mode)
1011 "*Change the protection bits associated with this entry in the tar file.
1012 This does not modify the disk image; you must save the tar file itself
1013 for this to be permanent."
1014 (interactive (list (tar-parse-octal-integer-safe
1015 (read-string "New protection (octal): "))))
1016 (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
1017 new-mode)
1018 (tar-alter-one-field tar-mode-offset
1019 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1020
1021
1022 (defun tar-alter-one-field (data-position new-data-string)
1023 (let* ((descriptor (tar-current-descriptor))
1024 (tokens (tar-desc-tokens descriptor)))
1025 (unwind-protect
1026 (save-excursion
1027 ;;
1028 ;; update the header-line.
1029 (beginning-of-line)
1030 (let ((p (point)))
1031 (forward-line 1)
1032 (delete-region p (point))
1033 (insert (tar-header-block-summarize tokens) "\n")
1034 (setq tar-header-offset (point-max)))
1035
1036 (widen)
1037 (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
1038 ;;
1039 ;; delete the old field and insert a new one.
1040 (goto-char (+ start data-position))
1041 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1042
1043 ;; As new-data-string is unibyte, just inserting it will
1044 ;; make eight-bit chars to the corresponding multibyte
1045 ;; chars. This avoid that conversion, i.e., eight-bit
1046 ;; chars are converted to multibyte form of eight-bit
1047 ;; chars.
1048 (insert (string-to-multibyte new-data-string))
1049 ;;
1050 ;; compute a new checksum and insert it.
1051 (let ((chk (tar-header-block-checksum
1052 (buffer-substring start (+ start 512)))))
1053 (goto-char (+ start tar-chk-offset))
1054 (delete-region (point) (+ (point) 8))
1055 (insert (format "%6o" chk))
1056 (insert 0)
1057 (insert ? )
1058 (tar-setf (tar-header-checksum tokens) chk)
1059 ;;
1060 ;; ok, make sure we didn't botch it.
1061 (tar-header-block-check-checksum
1062 (buffer-substring start (+ start 512))
1063 chk (tar-header-name tokens))
1064 )))
1065 (narrow-to-region (point-min) tar-header-offset)
1066 (tar-next-line 0))))
1067
1068
1069 (defun tar-octal-time (timeval)
1070 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1071 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1072 (format "%05o%01o%05o"
1073 (lsh hibits -2)
1074 (logior (lsh (logand 3 hibits) 1)
1075 (if (> (logand lobits 32768) 0) 1 0))
1076 (logand 32767 lobits)
1077 )))
1078
1079 (defun tar-subfile-save-buffer ()
1080 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1081 This doesn't write anything to disk; you must save the parent tar-file buffer
1082 to make your changes permanent."
1083 (interactive)
1084 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1085 (error "This buffer has no superior tar file buffer"))
1086 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1087 (error "This buffer doesn't have an index into its superior tar file!"))
1088 (save-excursion
1089 (let ((subfile (current-buffer))
1090 (coding buffer-file-coding-system)
1091 (descriptor tar-superior-descriptor)
1092 subfile-size)
1093 (set-buffer tar-superior-buffer)
1094 (let* ((tokens (tar-desc-tokens descriptor))
1095 (start (tar-desc-data-start descriptor))
1096 (name (tar-header-name tokens))
1097 (size (tar-header-size tokens))
1098 (size-pad (ash (ash (+ size 511) -9) 9))
1099 (head (memq descriptor tar-parse-info))
1100 (following-descs (cdr head)))
1101 (if (not head)
1102 (error "Can't find this tar file entry in its parent tar file!"))
1103 (unwind-protect
1104 (save-excursion
1105 ;; delete the old data...
1106 (let* ((data-start (+ start (- tar-header-offset (point-min))))
1107 (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
1108 (narrow-to-region data-start data-end)
1109 (delete-region (point-min) (point-max))
1110 ;; insert the new data...
1111 (goto-char data-start)
1112 (save-excursion
1113 (set-buffer subfile)
1114 (save-restriction
1115 (widen)
1116 (encode-coding-region 1 (point-max) coding tar-superior-buffer)))
1117 (setq subfile-size (- (point-max) (point-min)))
1118 ;;
1119 ;; pad the new data out to a multiple of 512...
1120 (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
1121 (goto-char (point-max))
1122 (insert (make-string (- subfile-size-pad subfile-size) 0))
1123 ;;
1124 ;; update the data pointer of this and all following files...
1125 (tar-setf (tar-header-size tokens) subfile-size)
1126 (let ((difference (- subfile-size-pad size-pad)))
1127 (dolist (desc following-descs)
1128 (tar-setf (tar-desc-data-start desc)
1129 (+ (tar-desc-data-start desc) difference))))
1130 ;;
1131 ;; Update the size field in the header block.
1132 (widen)
1133 (let ((header-start (- data-start 512)))
1134 (goto-char (+ header-start tar-size-offset))
1135 (delete-region (point) (+ (point) 12))
1136 (insert (format "%11o" subfile-size))
1137 (insert ? )
1138 ;;
1139 ;; Maybe update the datestamp.
1140 (if (not tar-update-datestamp)
1141 nil
1142 (goto-char (+ header-start tar-time-offset))
1143 (delete-region (point) (+ (point) 12))
1144 (insert (tar-octal-time (current-time)))
1145 (insert ? ))
1146 ;;
1147 ;; compute a new checksum and insert it.
1148 (let ((chk (tar-header-block-checksum
1149 (buffer-substring header-start data-start))))
1150 (goto-char (+ header-start tar-chk-offset))
1151 (delete-region (point) (+ (point) 8))
1152 (insert (format "%6o" chk))
1153 (insert 0)
1154 (insert ? )
1155 (tar-setf (tar-header-checksum tokens) chk)))
1156 ;;
1157 ;; alter the descriptor-line...
1158 ;;
1159 (let ((position (- (length tar-parse-info) (length head))))
1160 (goto-char (point-min))
1161 (next-line position)
1162 (beginning-of-line)
1163 (let ((p (point))
1164 after
1165 (m (set-marker (make-marker) tar-header-offset)))
1166 (forward-line 1)
1167 (setq after (point))
1168 ;; Insert the new text after the old, before deleting,
1169 ;; to preserve the window start.
1170 (let ((line (tar-header-block-summarize tokens t)))
1171 (insert-before-markers line "\n"))
1172 (delete-region p after)
1173 (setq tar-header-offset (marker-position m)))
1174 )))
1175 ;; after doing the insertion, add any final padding that may be necessary.
1176 (tar-pad-to-blocksize))
1177 (narrow-to-region (point-min) tar-header-offset)))
1178 (set-buffer-modified-p t) ; mark the tar file as modified
1179 (tar-next-line 0)
1180 (set-buffer subfile)
1181 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1182 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1183 (buffer-name tar-superior-buffer))
1184 ;; Prevent basic-save-buffer from changing our coding-system.
1185 (setq last-coding-system-used buffer-file-coding-system)
1186 ;; Prevent ordinary saving from happening.
1187 t)))
1188
1189
1190 ;; When this function is called, it is sure that the buffer is unibyte.
1191 (defun tar-pad-to-blocksize ()
1192 "If we are being anal about tar file blocksizes, fix up the current buffer.
1193 Leaves the region wide."
1194 (if (null tar-anal-blocksize)
1195 nil
1196 (widen)
1197 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1198 (start (tar-desc-data-start last-desc))
1199 (tokens (tar-desc-tokens last-desc))
1200 (link-p (tar-header-link-type tokens))
1201 (size (if link-p 0 (tar-header-size tokens)))
1202 (data-end (+ start size))
1203 (bbytes (ash tar-anal-blocksize 9))
1204 (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
1205 (inhibit-read-only t) ; ##
1206 )
1207 ;; If the padding after the last data is too long, delete some;
1208 ;; else insert some until we are padded out to the right number of blocks.
1209 ;;
1210 (goto-char (+ (or tar-header-offset 0) data-end))
1211 (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
1212 (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
1213 (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
1214 (1+ (buffer-size)))
1215 0)))
1216 )))
1217
1218
1219 ;; Used in write-file-hook to write tar-files out correctly.
1220 (defun tar-mode-write-file ()
1221 (unwind-protect
1222 (save-excursion
1223 (widen)
1224 ;; Doing this here confuses things - the region gets left too wide!
1225 ;; I suppose this is run in a context where changing the buffer is bad.
1226 ;; (tar-pad-to-blocksize)
1227 ;; tar-header-offset turns out to be null for files fetched with W3,
1228 ;; at least.
1229 (let ((coding-system-for-write 'no-conversion))
1230 (write-region (or tar-header-offset
1231 (point-min))
1232 (point-max)
1233 buffer-file-name nil t))
1234 (tar-clear-modification-flags)
1235 (set-buffer-modified-p nil))
1236 (narrow-to-region (point-min) tar-header-offset))
1237 ;; Return t because we've written the file.
1238 t)
1239 \f
1240 (provide 'tar-mode)
1241
1242 ;;; tar-mode.el ends here