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