1 ;;; tar-mode.el --- simple editing of tar files from GNU Emacs
3 ;; Copyright (C) 1990-1991, 1993-2018 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 04 Apr 1990
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
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.
38 ;; This code now understands the extra fields that GNU tar adds to tar files.
40 ;; This interacts correctly with "uncompress.el" in the Emacs library,
43 ;; (autoload 'uncompress-while-visiting "uncompress")
44 ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
47 ;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
49 ;; *************** TO DO ***************
51 ;; o chmod should understand "a+x,og-w".
53 ;; o The code is less efficient that it could be - in a lot of places, I
54 ;; pull a 512-character string out of the buffer and parse it, when I could
55 ;; be parsing it in place, not garbaging a string. Should redo that.
57 ;; o I'd like a command that searches for a string/regexp in every subfile
58 ;; of an archive, where <esc> would leave you in a subfile-edit buffer.
59 ;; (Like the Meta-R command of the Zmacs mail reader.)
61 ;; o Sometimes (but not always) reverting the tar-file buffer does not
62 ;; re-grind the listing, and you are staring at the binary tar data.
63 ;; Typing 'g' again immediately after that will always revert and re-grind
64 ;; it, though. I have no idea why this happens.
66 ;; o Tar-mode interacts poorly with crypt.el and zcat.el because the tar
67 ;; write-file-hook actually writes the file. Instead it should remove the
68 ;; header (and conspire to put it back afterwards) so that other write-file
69 ;; hooks which frob the buffer have a chance to do their dirty work. There
70 ;; might be a problem if the tar write-file-hook does not come *first* on
73 ;; o Block files, sparse files, continuation files, and the various header
74 ;; types aren't editable. Actually I don't know that they work at all.
78 ;; Why does tar-mode edit the file itself instead of using tar?
80 ;; That means that you can edit tar files which you don't have room for
81 ;; on your local disk.
83 ;; I don't know about recent features in gnu tar, but old versions of tar
84 ;; can't replace a file in the middle of a tar file with a new version.
85 ;; Tar-mode can. I don't think tar can do things like chmod the subfiles.
86 ;; An implementation which involved unpacking and repacking the file into
87 ;; some scratch directory would be very wasteful, and wouldn't be able to
88 ;; preserve the file owners.
92 ;; - Rename on ././@LongLink files
93 ;; - Revert confirmation displays the raw data temporarily.
97 (eval-when-compile (require 'cl-lib
))
100 "Simple editing of tar files."
104 (defcustom tar-anal-blocksize
20
105 "The blocksize of tar files written by Emacs, or nil, meaning don't care.
106 The blocksize of a tar file is not really the size of the blocks; rather, it is
107 the number of blocks written with one system call. When tarring to a tape,
108 this is the size of the *tape* blocks, but when writing to a file, it doesn't
109 matter much. The only noticeable difference is that if a tar file does not
110 have a blocksize of 20, tar will tell you that; all this really controls is
111 how many null padding bytes go on the end of the tar file."
112 :type
'(choice integer
(const nil
))
115 (defcustom tar-update-datestamp nil
116 "Non-nil means Tar mode should play fast and loose with sub-file datestamps.
117 If this is true, then editing and saving a tar file entry back into its
118 tar file will update its datestamp. If false, the datestamp is unchanged.
119 You may or may not want this - it is good in that you can tell when a file
120 in a tar archive has been changed, but it is bad for the same reason that
121 editing a file in the tar archive at all is bad - the changed version of
122 the file never exists on disk."
126 (defcustom tar-mode-show-date nil
127 "Non-nil means Tar mode should show the date/time of each subfile.
128 This information is useful, but it takes screen space away from file names."
132 (defvar tar-parse-info nil
)
133 (defvar tar-superior-buffer nil
134 "Buffer containing the tar archive from which a member was extracted.")
135 (defvar tar-superior-descriptor nil
136 "Tar descriptor for a member extracted from an archive.")
137 (defvar tar-file-name-coding-system nil
)
139 (put 'tar-superior-buffer
'permanent-local t
)
140 (put 'tar-superior-descriptor
'permanent-local t
)
142 ;; The Tar data is made up of bytes and better manipulated as bytes
143 ;; and can be very large, so insert/delete can be costly. The summary we
144 ;; want to display may contain non-ascii chars, of course, so we'd like it
145 ;; to be multibyte. We used to keep both in the same buffer and switch
146 ;; from/to uni/multibyte. But this had several downsides:
147 ;; - set-buffer-multibyte has an O(N^2) worst case that tends to be triggered
148 ;; here, so it gets atrociously slow on large Tar files.
149 ;; - need to widen/narrow the buffer to show/hide the raw data, and need to
150 ;; maintain a tar-header-offset that keeps track of the boundary between
152 ;; - can't use markers because they're not preserved by set-buffer-multibyte.
153 ;; So instead, we now keep the two pieces of data in separate buffers, and
154 ;; use the new buffer-swap-text primitive when we need to change which data
155 ;; is associated with "the" buffer.
156 (defvar tar-data-buffer nil
"Buffer that holds the actual raw tar bytes.")
157 (make-variable-buffer-local 'tar-data-buffer
)
159 (defvar tar-data-swapped nil
160 "If non-nil, `tar-data-buffer' indeed holds raw tar bytes.")
161 (make-variable-buffer-local 'tar-data-swapped
)
163 (defun tar-data-swapped-p ()
164 "Return non-nil if the tar-data is in `tar-data-buffer'."
165 (and (buffer-live-p tar-data-buffer
)
166 ;; Sanity check to try and make sure tar-data-swapped tracks the swap
167 ;; state correctly: the raw data is expected to be always larger than
170 (cl-assert (or (= (buffer-size tar-data-buffer
) (buffer-size))
172 (> (buffer-size tar-data-buffer
) (buffer-size)))))
175 (defun tar-swap-data ()
176 "Swap buffer contents between current buffer and `tar-data-buffer'.
177 Preserve the modified states of the buffers and set `buffer-swapped-with'."
178 (let ((data-buffer-modified-p (buffer-modified-p tar-data-buffer
))
179 (current-buffer-modified-p (buffer-modified-p)))
180 (buffer-swap-text tar-data-buffer
)
181 (setq tar-data-swapped
(not tar-data-swapped
))
182 (restore-buffer-modified-p data-buffer-modified-p
)
183 (with-current-buffer tar-data-buffer
184 (restore-buffer-modified-p current-buffer-modified-p
))))
186 ;;; down to business.
188 (cl-defstruct (tar-header
193 make-tar-header
(data-start name mode uid gid size date checksum
194 link-type link-name magic uname gname dmaj dmin
)))
195 data-start name mode uid gid size date checksum link-type link-name
196 magic uname gname dmaj dmin
197 ;; Start of the header can be nil (meaning it's 512 bytes before data-start)
198 ;; or a marker (in case the header uses LongLink thingies).
201 (defconst tar-name-offset
0)
202 (defconst tar-mode-offset
(+ tar-name-offset
100))
203 (defconst tar-uid-offset
(+ tar-mode-offset
8))
204 (defconst tar-gid-offset
(+ tar-uid-offset
8))
205 (defconst tar-size-offset
(+ tar-gid-offset
8))
206 (defconst tar-time-offset
(+ tar-size-offset
12))
207 (defconst tar-chk-offset
(+ tar-time-offset
12))
208 (defconst tar-linkp-offset
(+ tar-chk-offset
8))
209 (defconst tar-link-offset
(+ tar-linkp-offset
1))
210 ;;; GNU-tar specific slots.
211 (defconst tar-magic-offset
(+ tar-link-offset
100))
212 (defconst tar-uname-offset
(+ tar-magic-offset
8))
213 (defconst tar-gname-offset
(+ tar-uname-offset
32))
214 (defconst tar-dmaj-offset
(+ tar-gname-offset
32))
215 (defconst tar-dmin-offset
(+ tar-dmaj-offset
8))
216 (defconst tar-prefix-offset
(+ tar-dmin-offset
8))
217 (defconst tar-end-offset
(+ tar-prefix-offset
155))
219 (defun tar-roundup-512 (s)
220 "Round S up to the next multiple of 512."
221 (ash (ash (+ s
511) -
9) 9))
223 (defun tar-header-block-tokenize (pos coding
)
224 "Return a `tar-header' structure.
225 This is a list of name, mode, uid, gid, size,
226 write-date, checksum, link-type, and link-name."
227 (if (> (+ pos
512) (point-max)) (error "Malformed Tar header"))
228 (cl-assert (zerop (mod (- pos
(point-min)) 512)))
229 (cl-assert (not enable-multibyte-characters
))
230 (let ((string (buffer-substring pos
(setq pos
(+ pos
512)))))
231 (when ;(some 'plusp string) ; <-- oops, massive cycle hog!
232 (or (not (= 0 (aref string
0))) ; This will do.
233 (not (= 0 (aref string
101))))
234 (let* ((name-end tar-mode-offset
)
235 (link-end (1- tar-magic-offset
))
236 (uname-end (1- tar-gname-offset
))
237 (gname-end (1- tar-dmaj-offset
))
238 (link-p (aref string tar-linkp-offset
))
239 (magic-str (substring string tar-magic-offset
240 ;; The magic string is actually 6bytes
241 ;; of magic string plus 2bytes of version
242 ;; which we here ignore.
243 (- tar-uname-offset
2)))
244 ;; The magic string is "ustar\0" for POSIX format, and
245 ;; "ustar " for GNU Tar's format.
246 (uname-valid-p (car (member magic-str
'("ustar " "ustar\0"))))
248 (nulsexp "[^\000]*\000"))
249 (when (string-match nulsexp string tar-name-offset
)
250 (setq name-end
(min name-end
(1- (match-end 0)))))
251 (when (string-match nulsexp string tar-link-offset
)
252 (setq link-end
(min link-end
(1- (match-end 0)))))
253 (when (string-match nulsexp string tar-uname-offset
)
254 (setq uname-end
(min uname-end
(1- (match-end 0)))))
255 (when (string-match nulsexp string tar-gname-offset
)
256 (setq gname-end
(min gname-end
(1- (match-end 0)))))
257 (setq name
(substring string tar-name-offset name-end
)
258 link-p
(if (or (= link-p
0) (= link-p ?
0))
261 (setq linkname
(substring string tar-link-offset link-end
))
262 (when (and (equal uname-valid-p
"ustar\0")
263 (string-match nulsexp string tar-prefix-offset
)
264 (> (match-end 0) (1+ tar-prefix-offset
)))
265 (setq name
(concat (substring string tar-prefix-offset
268 (if (default-value 'enable-multibyte-characters
)
270 (decode-coding-string name coding
)
272 (decode-coding-string linkname coding
)))
273 (if (and (null link-p
) (string-match "/\\'" name
))
274 (setq link-p
5)) ; directory
276 (if (and (equal name
"././@LongLink")
277 ;; Supposedly @LongLink is only used for GNUTAR
278 ;; format (i.e. "ustar ") but some POSIX Tar files
279 ;; (with "ustar\0") have been seen using it as well.
280 (member magic-str
'("ustar " "ustar\0")))
281 ;; This is a GNU Tar long-file-name header.
282 (let* ((size (tar-parse-octal-integer
283 string tar-size-offset tar-time-offset
))
284 ;; -1 so as to strip the terminating 0 byte.
285 (name (decode-coding-string
286 (buffer-substring pos
(+ pos size -
1)) coding
))
287 (descriptor (tar-header-block-tokenize
288 (+ pos
(tar-roundup-512 size
))
291 ((eq link-p
(- ?L ?
0)) ;GNUTYPE_LONGNAME.
292 (setf (tar-header-name descriptor
) name
))
293 ((eq link-p
(- ?K ?
0)) ;GNUTYPE_LONGLINK.
294 (setf (tar-header-link-name descriptor
) name
))
296 (message "Unrecognized GNU Tar @LongLink format")))
297 (setf (tar-header-header-start descriptor
)
298 (copy-marker (- pos
512) t
))
302 (copy-marker pos nil
)
304 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset
)
305 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset
)
306 (tar-parse-octal-integer string tar-gid-offset tar-size-offset
)
307 (tar-parse-octal-integer string tar-size-offset tar-time-offset
)
308 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset
)
309 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset
)
314 (decode-coding-string
315 (substring string tar-uname-offset uname-end
) coding
))
317 (decode-coding-string
318 (substring string tar-gname-offset gname-end
) coding
))
319 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset
)
320 (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset
)
324 (defun tar-header-data-end (descriptor)
325 (let* ((data-start (tar-header-data-start descriptor
))
326 (link-type (tar-header-link-type descriptor
))
327 (size (tar-header-size descriptor
)))
329 ;; Ignore size for files of type 1-6
330 (if (and (not (memq link-type
'(1 2 3 4 5 6))) (> size
0))
331 (tar-roundup-512 size
)
334 (defun tar-parse-octal-integer (string &optional start end
)
335 (if (null start
) (setq start
0))
336 (if (null end
) (setq end
(length string
)))
337 (if (= (aref string start
) 0)
341 (setq n
(if (< (aref string start
) ?
0) n
342 (+ (* n
8) (- (aref string start
) ?
0)))
346 (defun tar-parse-octal-long-integer (string &optional start end
)
347 (if (null start
) (setq start
0))
348 (if (null end
) (setq end
(length string
)))
349 (if (= (aref string start
) 0)
354 (if (>= (aref string start
) ?
0)
355 (setq lo
(+ (* lo
8) (- (aref string start
) ?
0))
356 hi
(+ (* hi
8) (ash lo -
16))
357 lo
(logand lo
65535)))
358 (setq start
(1+ start
)))
361 (defun tar-parse-octal-integer-safe (string)
362 (if (zerop (length string
)) (error "empty string"))
364 (if (or (< c ?
0) (> c ?
7))
365 (error "`%c' is not an octal digit" c
)))
367 (tar-parse-octal-integer string
))
369 (defun tar-new-regular-file-header (filename &optional size time
)
370 "Return a Tar header for a regular file.
371 The header will lack a proper checksum; use `tar-header-block-checksum'
372 to compute one, or request `tar-header-serialize' to do that.
374 Other tar-mode facilities may also require the data-start header
375 field to be set to a valid value.
377 If SIZE is not given or nil, it defaults to 0.
378 If TIME is not given or nil, assume now."
382 #o644
0 0 (or size
0)
383 (or time
(current-time))
386 nil nil nil nil nil
))
388 (defun tar--pad-to (pos)
389 (make-string (+ pos
(- (point)) (point-min)) 0))
391 (defun tar--put-at (pos val
&optional fmt mask
)
393 (insert (tar--pad-to pos
)
395 (format fmt
(if mask
(logand mask val
) val
))
398 (defun tar-header-serialize (header &optional update-checksum
)
399 "Return the serialization of a Tar HEADER as a string.
400 This function calls `tar-header-block-check-checksum' to ensure the
403 If UPDATE-CHECKSUM is non-nil, update HEADER with the newly-computed
404 checksum before doing the check."
406 (set-buffer-multibyte nil
)
408 (encode-coding-string (tar-header-name header
)
409 tar-file-name-coding-system
)))
410 (unless (< (length encoded-name
) 99)
411 ;; FIXME: Implement it.
412 (error "Long file name support is not implemented"))
413 (insert encoded-name
))
414 (tar--put-at tar-mode-offset
(tar-header-mode header
) "%6o\0 " #o777777
)
415 (tar--put-at tar-uid-offset
(tar-header-uid header
) "%6o\0 " #o777777
)
416 (tar--put-at tar-gid-offset
(tar-header-gid header
) "%6o\0 " #o777777
)
417 (tar--put-at tar-size-offset
(tar-header-size header
) "%11o ")
418 (insert (tar--pad-to tar-time-offset
)
419 (tar-octal-time (tar-header-date header
))
421 ;; Omit tar-header-checksum (tar-chk-offset) for now.
422 (tar--put-at tar-linkp-offset
(tar-header-link-type header
))
423 (tar--put-at tar-link-offset
(tar-header-link-name header
))
424 (when (tar-header-magic header
)
425 (tar--put-at tar-magic-offset
(tar-header-magic header
))
426 (tar--put-at tar-uname-offset
(tar-header-uname header
))
427 (tar--put-at tar-gname-offset
(tar-header-gname header
))
428 (tar--put-at tar-dmaj-offset
(tar-header-dmaj header
) "%7o\0" #o7777777
)
429 (tar--put-at tar-dmin-offset
(tar-header-dmin header
) "%7o\0" #o7777777
))
431 (let ((ck (tar-header-block-checksum (buffer-string))))
432 (goto-char (+ (point-min) tar-chk-offset
))
434 (insert (format "%6o\0 " ck
))
435 (when update-checksum
436 (setf (tar-header-checksum header
) ck
))
437 (tar-header-block-check-checksum (buffer-string)
438 (tar-header-checksum header
)
439 (tar-header-name header
)))
444 (defun tar-header-block-checksum (string)
445 "Compute and return a tar-acceptable checksum for this block."
446 (cl-assert (not (multibyte-string-p string
)))
447 (let* ((chk-field-start tar-chk-offset
)
448 (chk-field-end (+ chk-field-start
8))
451 ;; Add up all of the characters except the ones in the checksum field.
452 ;; Add that field as if it were filled with spaces.
453 (while (< i chk-field-start
)
454 (setq sum
(+ sum
(aref string i
))
456 (setq i chk-field-end
)
458 (setq sum
(+ sum
(aref string i
))
462 (defun tar-header-block-check-checksum (hblock desired-checksum file-name
)
463 "Beep and print a warning if the checksum doesn't match."
464 (if (not (= desired-checksum
(tar-header-block-checksum hblock
)))
465 (progn (beep) (message "Invalid checksum for file %s!" file-name
))))
467 (defun tar-clip-time-string (time)
468 (let ((str (current-time-string time
)))
469 (concat " " (substring str
4 16) (format-time-string " %Y" time
))))
471 (defun tar-grind-file-mode (mode)
472 "Construct a `rw-r--r--' string indicating MODE.
473 MODE should be an integer which is a file mode value."
475 (if (zerop (logand 256 mode
)) ?- ?r
)
476 (if (zerop (logand 128 mode
)) ?- ?w
)
477 (if (zerop (logand 2048 mode
))
478 (if (zerop (logand 64 mode
)) ?- ?x
)
479 (if (zerop (logand 64 mode
)) ?S ?s
))
480 (if (zerop (logand 32 mode
)) ?- ?r
)
481 (if (zerop (logand 16 mode
)) ?- ?w
)
482 (if (zerop (logand 1024 mode
))
483 (if (zerop (logand 8 mode
)) ?- ?x
)
484 (if (zerop (logand 8 mode
)) ?S ?s
))
485 (if (zerop (logand 4 mode
)) ?- ?r
)
486 (if (zerop (logand 2 mode
)) ?- ?w
)
487 (if (zerop (logand 512 mode
))
488 (if (zerop (logand 1 mode
)) ?- ?x
)
489 (if (zerop (logand 1 mode
)) ?T ?t
))))
491 (defun tar-header-block-summarize (tar-hblock &optional mod-p
)
492 "Return a line similar to the output of `tar -vtf'."
493 (let ((name (tar-header-name tar-hblock
))
494 (mode (tar-header-mode tar-hblock
))
495 (uid (tar-header-uid tar-hblock
))
496 (gid (tar-header-gid tar-hblock
))
497 (uname (tar-header-uname tar-hblock
))
498 (gname (tar-header-gname tar-hblock
))
499 (size (tar-header-size tar-hblock
))
500 (time (tar-header-date tar-hblock
))
501 ;; (ck (tar-header-checksum tar-hblock))
502 (type (tar-header-link-type tar-hblock
))
503 (link-name (tar-header-link-name tar-hblock
)))
504 (format "%c%c%s %7s/%-7s %7s%s %s%s"
506 (cond ((or (eq type nil
) (eq type
0)) ?-
)
507 ((eq type
1) ?h
) ; link
508 ((eq type
2) ?l
) ; symlink
509 ((eq type
3) ?c
) ; char special
510 ((eq type
4) ?b
) ; block special
511 ((eq type
5) ?d
) ; directory
512 ((eq type
6) ?p
) ; FIFO/pipe
513 ((eq type
20) ?
*) ; directory listing
514 ((eq type
28) ?L
) ; next has longname
515 ((eq type
29) ?M
) ; multivolume continuation
516 ((eq type
35) ?S
) ; sparse
517 ((eq type
38) ?V
) ; volume header
518 ((eq type
55) ?H
) ; pax global extended header
519 ((eq type
72) ?X
) ; pax extended header
522 (tar-grind-file-mode mode
)
523 (if (= 0 (length uname
)) uid uname
)
524 (if (= 0 (length gname
)) gid gname
)
526 (if tar-mode-show-date
(tar-clip-time-string time
) "")
528 'mouse-face
'highlight
529 'help-echo
"mouse-2: extract this file into a buffer")
530 (if (or (eq type
1) (eq type
2))
531 (concat (if (= type
1) " ==> " " --> ") link-name
)
534 (defun tar-untar-buffer ()
535 "Extract all archive members in the tar-file into the current directory."
537 ;; FIXME: make it work even if we're not in tar-mode.
538 (let ((descriptors tar-parse-info
)) ;Read the var in its buffer.
540 (if (tar-data-swapped-p) tar-data-buffer
(current-buffer))
541 (set-buffer-multibyte nil
) ;Hopefully, a no-op.
542 (dolist (descriptor descriptors
)
543 (let* ((name (tar-header-name descriptor
))
544 (dir (if (eq (tar-header-link-type descriptor
) 5)
546 (file-name-directory name
)))
547 (link-desc (tar--describe-as-link descriptor
))
548 (start (tar-header-data-start descriptor
))
549 (end (+ start
(tar-header-size descriptor
))))
550 (unless (file-directory-p name
)
551 (message "Extracting %s" name
)
552 (if (and dir
(not (file-exists-p dir
)))
553 (make-directory dir t
))
554 (unless (file-directory-p name
)
555 (let ((coding-system-for-write 'no-conversion
))
557 (lwarn '(tar link
) :warning
558 "Extracted `%s', %s, as a normal file"
560 (write-region start end name
)))
561 (set-file-modes name
(tar-header-mode descriptor
))))))))
563 (defun tar-summarize-buffer ()
564 "Parse the contents of the tar file in the current buffer."
565 (cl-assert (tar-data-swapped-p))
566 (let* ((modified (buffer-modified-p))
569 (coding tar-file-name-coding-system
)
571 (with-current-buffer tar-data-buffer
572 (make-progress-reporter "Parsing tar file..."
573 (point-min) (point-max))))
575 (with-current-buffer tar-data-buffer
576 (while (and (< pos
(point-max))
577 (setq descriptor
(tar-header-block-tokenize pos coding
)))
578 (let ((size (tar-header-size descriptor
)))
580 (error "%s has size %s - corrupted"
581 (tar-header-name descriptor
) size
)))
583 ;; This is just too slow. Don't really need it anyway....
584 ;;(tar-header-block-check-checksum
585 ;; hblock (tar-header-block-checksum hblock)
586 ;; (tar-header-name descriptor))
588 (push descriptor result
)
589 (setq pos
(tar-header-data-end descriptor
))
590 (progress-reporter-update progress-reporter pos
)))
592 (set (make-local-variable 'tar-parse-info
) (nreverse result
))
593 ;; A tar file should end with a block or two of nulls,
594 ;; but let's not get a fatal error if it doesn't.
595 (if (null descriptor
)
596 (progress-reporter-done progress-reporter
)
597 (message "Warning: premature EOF parsing tar file"))
598 (goto-char (point-min))
599 (let ((buffer-file-truename nil
) ; avoid changing dir mtime by lock_file
600 (inhibit-read-only t
)
602 (mapconcat 'tar-header-block-summarize tar-parse-info
"\n")))
603 (insert total-summaries
"\n")
604 (goto-char (point-min))
605 (restore-buffer-modified-p modified
))))
608 (let ((map (make-keymap)))
609 (suppress-keymap map
)
610 (define-key map
" " 'tar-next-line
)
611 (define-key map
"C" 'tar-copy
)
612 (define-key map
"d" 'tar-flag-deleted
)
613 (define-key map
"\^D" 'tar-flag-deleted
)
614 (define-key map
"e" 'tar-extract
)
615 (define-key map
"f" 'tar-extract
)
616 (define-key map
"\C-m" 'tar-extract
)
617 (define-key map
[mouse-2
] 'tar-mouse-extract
)
618 (define-key map
"g" 'revert-buffer
)
619 (define-key map
"n" 'tar-next-line
)
620 (define-key map
"\^N" 'tar-next-line
)
621 (define-key map
[down] 'tar-next-line)
622 (define-key map "o" 'tar-extract-other-window)
623 (define-key map "p" 'tar-previous-line)
624 (define-key map "\^P" 'tar-previous-line)
625 (define-key map [up] 'tar-previous-line)
626 (define-key map "I" 'tar-new-entry)
627 (define-key map "R" 'tar-rename-entry)
628 (define-key map "u" 'tar-unflag)
629 (define-key map "v" 'tar-view)
630 (define-key map "w" 'woman-tar-extract-file)
631 (define-key map "x" 'tar-expunge)
632 (define-key map "\177" 'tar-unflag-backwards)
633 (define-key map "E" 'tar-extract-other-window)
634 (define-key map "M" 'tar-chmod-entry)
635 (define-key map "G" 'tar-chgrp-entry)
636 (define-key map "O" 'tar-chown-entry)
637 ;; Let mouse-1 follow the link.
638 (define-key map [follow-link] 'mouse-face)
640 ;; Make menu bar items.
642 ;; Get rid of the Edit menu bar item to save space.
643 (define-key map [menu-bar edit] 'undefined)
645 (define-key map [menu-bar immediate]
646 (cons "Immediate" (make-sparse-keymap "Immediate")))
648 (define-key map [menu-bar immediate woman]
649 '("Read Man Page (WoMan)" . woman-tar-extract-file))
650 (define-key map [menu-bar immediate view]
651 '("View This File" . tar-view))
652 (define-key map [menu-bar immediate display]
653 '("Display in Other Window" . tar-display-other-window))
654 (define-key map [menu-bar immediate find-file-other-window]
655 '("Find in Other Window" . tar-extract-other-window))
656 (define-key map [menu-bar immediate find-file]
657 '("Find This File" . tar-extract))
659 (define-key map [menu-bar mark]
660 (cons "Mark" (make-sparse-keymap "Mark")))
662 (define-key map [menu-bar mark unmark-all]
663 '("Unmark All" . tar-clear-modification-flags))
664 (define-key map [menu-bar mark deletion]
665 '("Flag" . tar-flag-deleted))
666 (define-key map [menu-bar mark unmark]
667 '("Unflag" . tar-unflag))
669 (define-key map [menu-bar operate]
670 (cons "Operate" (make-sparse-keymap "Operate")))
672 (define-key map [menu-bar operate chown]
673 '("Change Owner..." . tar-chown-entry))
674 (define-key map [menu-bar operate chgrp]
675 '("Change Group..." . tar-chgrp-entry))
676 (define-key map [menu-bar operate chmod]
677 '("Change Mode..." . tar-chmod-entry))
678 (define-key map [menu-bar operate rename]
679 '("Rename to..." . tar-rename-entry))
680 (define-key map [menu-bar operate copy]
681 '("Copy to..." . tar-copy))
682 (define-key map [menu-bar operate expunge]
683 '("Expunge Marked Files" . tar-expunge))
686 "Local keymap for Tar mode listings.")
689 ;; tar mode is suitable only for specially formatted data.
690 (put 'tar-mode 'mode-class 'special)
691 (put 'tar-subfile-mode 'mode-class 'special)
693 (defun tar-change-major-mode-hook ()
694 ;; Bring the actual Tar data back into the main buffer.
695 (when (tar-data-swapped-p) (tar-swap-data))
696 ;; Throw away the summary.
697 (when (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
699 (defun tar-mode-kill-buffer-hook ()
700 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
703 (define-derived-mode tar-mode special-mode "Tar"
704 "Major mode for viewing a tar file as a dired-like listing of its contents.
705 You can move around using the usual cursor motion commands.
706 Letters no longer insert themselves.
707 Type `e' to pull a file out of the tar file and into its own buffer;
708 or click mouse-2 on the file's line in the Tar mode buffer.
709 Type `c' to copy an entry from the tar file into another file on disk.
711 If you edit a sub-file of this archive (as with the `e' command) and
712 save it with \\[save-buffer], the contents of that buffer will be
713 saved back into the tar-file buffer; in this way you can edit a file
714 inside of a tar archive without extracting it and re-archiving it.
716 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
718 (and buffer-file-name
719 (file-writable-p buffer-file-name)
720 (setq buffer-read-only nil)) ; undo what `special-mode' did
721 (make-local-variable 'tar-parse-info)
722 (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
723 (set (make-local-variable 'local-enable-local-variables) nil)
724 (set (make-local-variable 'next-line-add-newlines) nil)
725 (set (make-local-variable 'tar-file-name-coding-system)
726 (or file-name-coding-system
727 default-file-name-coding-system
728 locale-coding-system))
729 ;; Prevent loss of data when saving the file.
730 (set (make-local-variable 'file-precious-flag) t)
731 (buffer-disable-undo)
733 ;; Now move the Tar data into an auxiliary buffer, so we can use the main
734 ;; buffer for the summary.
735 (cl-assert (not (tar-data-swapped-p)))
736 (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
737 ;; We started using write-contents-functions, but this hook is not
738 ;; used during auto-save, so we now use
739 ;; write-region-annotate-functions which hooks at a lower-level.
740 (add-hook 'write-region-annotate-functions 'tar-write-region-annotate nil t)
741 (add-hook 'kill-buffer-hook 'tar-mode-kill-buffer-hook nil t)
742 (add-hook 'change-major-mode-hook 'tar-change-major-mode-hook nil t)
743 ;; Tar data is made of bytes, not chars.
744 (set-buffer-multibyte nil) ;Hopefully a no-op.
745 (set (make-local-variable 'tar-data-buffer)
746 (generate-new-buffer (format " *tar-data %s*"
747 (file-name-nondirectory
748 (or buffer-file-name (buffer-name))))))
752 (tar-summarize-buffer)
755 ;; If summarizing caused an error, then maybe the buffer doesn't contain
756 ;; tar data. Rather than show a mysterious empty buffer, let's
757 ;; revert to fundamental-mode.
759 (signal (car err) (cdr err)))))
761 (autoload 'woman-tar-extract-file "woman"
762 "In tar mode, run the WoMan man-page browser on this file." t)
764 (define-minor-mode tar-subfile-mode
765 "Minor mode for editing an element of a tar-file.
766 With a prefix argument ARG, enable the mode if ARG is positive,
767 and disable it otherwise. If called from Lisp, enable the mode
768 if ARG is omitted or nil. This mode arranges for \"saving\" this
769 buffer to write the data into the tar-file buffer that it came
770 from. The changes will actually appear on disk when you save the
772 ;; Don't do this, because it is redundant and wastes mode line space.
773 ;; :lighter " TarFile"
775 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
776 (error "This buffer is not an element of a tar file"))
777 (cond (tar-subfile-mode
778 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
779 ;; turn off auto-save.
781 (setq buffer-auto-save-file-name nil))
783 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
786 ;; Revert the buffer and recompute the dired-like listing.
787 (defun tar-mode-revert (&optional no-auto-save no-confirm)
789 (let ((revert-buffer-function nil))
790 (if (tar-data-swapped-p) (tar-swap-data))
791 ;; FIXME: If we ask for confirmation, the user will be temporarily
792 ;; looking at the raw data.
793 (revert-buffer no-auto-save no-confirm 'preserve-modes)
794 ;; Recompute the summary.
795 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
797 (unless (tar-data-swapped-p) (tar-swap-data))))
800 (defun tar-next-line (arg)
801 "Move cursor vertically down ARG lines and to the start of the filename."
804 (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
806 (defun tar-previous-line (arg)
807 "Move cursor vertically up ARG lines and to the start of the filename."
809 (tar-next-line (- arg)))
811 (defun tar-current-position ()
812 "Return the `tar-parse-info' index for the current line."
813 (count-lines (point-min) (line-beginning-position)))
815 (defun tar-current-descriptor (&optional noerror)
816 "Return the tar-descriptor of the current line, or signals an error."
817 ;; I wish lines had plists, like in ZMACS...
818 (or (nth (tar-current-position)
822 (error "This line does not describe a tar-file entry"))))
824 (defun tar--describe-as-link (descriptor)
825 (let ((link-p (tar-header-link-type descriptor)))
827 (cond ((eq link-p 5) "a directory")
828 ((eq link-p 20) "a tar directory header")
829 ((eq link-p 28) "a next has longname")
830 ((eq link-p 29) "a multivolume-continuation")
831 ((eq link-p 35) "a sparse entry")
832 ((eq link-p 38) "a volume header")
833 ((eq link-p 55) "a pax global extended header")
834 ((eq link-p 72) "a pax extended header")
837 (defun tar--check-descriptor (descriptor)
838 (let ((link-desc (tar--describe-as-link descriptor)))
840 (error "This is %s, not a real file" link-desc))))
842 (defun tar-get-descriptor ()
843 (let* ((descriptor (tar-current-descriptor))
844 (size (tar-header-size descriptor)))
845 (tar--check-descriptor descriptor)
846 (if (zerop size) (message "This is a zero-length file"))
849 (defun tar-get-file-descriptor (file)
850 ;; Used by package.el.
852 (dolist (hdr tar-parse-info)
853 (when (equal file (tar-header-name hdr))
855 (tar--check-descriptor desc)
858 (defun tar-mouse-extract (event)
859 "Extract a file whose tar directory line you click on."
861 (with-current-buffer (window-buffer (posn-window (event-end event)))
863 (goto-char (posn-point (event-end event)))
864 ;; Just make sure this doesn't get an error.
865 (tar-get-descriptor)))
866 (select-window (posn-window (event-end event)))
867 (goto-char (posn-point (event-end event)))
870 (defun tar-file-name-handler (op &rest args)
871 "Helper function for `tar-extract'."
872 (or (eq op 'file-exists-p)
873 (let ((file-name-handler-alist nil))
876 (defun tar--extract (descriptor)
877 "Extract this entry of the tar file into its own buffer."
878 (let* ((name (tar-header-name descriptor))
879 (size (tar-header-size descriptor))
880 (start (tar-header-data-start descriptor))
882 (tarname (buffer-name))
883 (bufname (concat (file-name-nondirectory name)
887 (buffer (generate-new-buffer bufname)))
888 (with-current-buffer tar-data-buffer
890 (narrow-to-region start end)
892 (setq coding (or coding-system-for-read
893 (and set-auto-coding-function
894 (funcall set-auto-coding-function
896 ;; The following binding causes
897 ;; find-buffer-file-type-coding-system
898 ;; (defined on dos-w32.el) to act as if
899 ;; the file being extracted existed, so
900 ;; that the file's contents' encoding and
901 ;; EOL format are auto-detected.
902 (let ((file-name-handler-alist
903 '(("" . tar-file-name-handler))))
904 (car (find-operation-coding-system
905 'insert-file-contents
906 (cons name (current-buffer)) t)))))
908 (eq (coding-system-type coding) 'undecided))
909 (setq coding (detect-coding-region start end t)))
910 (if (and (default-value 'enable-multibyte-characters)
911 (coding-system-get coding :for-unibyte))
912 (with-current-buffer buffer
913 (set-buffer-multibyte nil)))
915 (with-current-buffer buffer
916 (setq buffer-undo-list t))
917 (decode-coding-region start end coding buffer)
918 (with-current-buffer buffer
919 (setq buffer-undo-list nil))))
922 (defun tar-extract (&optional other-window-p)
923 "In Tar mode, extract this entry of the tar file into its own buffer."
925 (let* ((view-p (eq other-window-p 'view))
926 (descriptor (tar-get-descriptor))
927 (name (tar-header-name descriptor))
928 (tar-buffer (current-buffer))
929 (tarname (buffer-name))
930 (read-only-p (or buffer-read-only view-p))
931 (new-buffer-file-name (expand-file-name
932 ;; `:' is not allowed on Windows
934 (if (string-match "/" name)
936 ;; Make sure `name' contains a /
937 ;; so set-auto-mode doesn't try
938 ;; to look at `tarname' for hints.
939 (concat "./" name)))))
940 (buffer (get-file-buffer new-buffer-file-name))
943 (setq buffer (tar--extract descriptor))
944 (setq just-created t)
945 (with-current-buffer buffer
946 (goto-char (point-min))
947 (setq buffer-file-name new-buffer-file-name)
948 (setq buffer-file-truename
949 (abbreviate-file-name buffer-file-name))
950 ;; Force buffer-file-coding-system to what
951 ;; decode-coding-region actually used.
952 (set-buffer-file-coding-system last-coding-system-used t)
953 ;; Set the default-directory to the dir of the
955 (setq default-directory
956 (with-current-buffer tar-buffer
958 (set-buffer-modified-p nil)
959 (normal-mode) ; pick a mode.
960 (set (make-local-variable 'tar-superior-buffer) tar-buffer)
961 (set (make-local-variable 'tar-superior-descriptor) descriptor)
962 (setq buffer-read-only read-only-p)
963 (tar-subfile-mode 1)))
966 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
967 ((eq other-window-p 'display) (display-buffer buffer))
968 (other-window-p (switch-to-buffer-other-window buffer))
969 (t (switch-to-buffer buffer)))))
972 (defun tar-extract-other-window ()
973 "In Tar mode, find this entry of the tar file in another window."
977 (defun tar-display-other-window ()
978 "In Tar mode, display this entry of the tar file in another window."
980 (tar-extract 'display))
983 "In Tar mode, view the tar file entry on this line."
988 (defun tar-read-file-name (&optional prompt)
989 "Read a file name with this line's entry as the default."
990 (or prompt (setq prompt "Copy to: "))
991 (let* ((default-file (expand-file-name
992 (tar-header-name (tar-current-descriptor))))
993 (target (expand-file-name
994 (read-file-name prompt
995 (file-name-directory default-file)
997 (if (or (string= "" (file-name-nondirectory target))
998 (file-directory-p target))
999 (setq target (concat (if (string-match "/$" target)
1000 (substring target 0 (1- (match-end 0)))
1003 (file-name-nondirectory default-file))))
1007 (defun tar-copy (&optional to-file)
1008 "In Tar mode, extract this entry of the tar file into a file on disk.
1009 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
1010 the current tar-entry."
1011 (interactive (list (tar-read-file-name)))
1012 (let* ((descriptor (tar-get-descriptor))
1013 (name (tar-header-name descriptor))
1014 (size (tar-header-size descriptor))
1015 (start (tar-header-data-start descriptor))
1016 (end (+ start size))
1017 (inhibit-file-name-handlers inhibit-file-name-handlers)
1018 (inhibit-file-name-operation inhibit-file-name-operation))
1019 (with-current-buffer
1020 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
1021 ;; Inhibit compressing a subfile again if *both* name and
1022 ;; to-file are handled by jka-compr
1023 (if (and (eq (find-file-name-handler name 'write-region)
1025 (eq (find-file-name-handler to-file 'write-region)
1026 'jka-compr-handler))
1027 (setq inhibit-file-name-handlers
1028 (cons 'jka-compr-handler
1029 (and (eq inhibit-file-name-operation 'write-region)
1030 inhibit-file-name-handlers))
1031 inhibit-file-name-operation 'write-region))
1032 (let ((coding-system-for-write 'no-conversion))
1033 (write-region start end to-file nil nil nil t)))
1034 (message "Copied tar entry %s to %s" name to-file)))
1036 (defun tar-new-entry (filename &optional index)
1037 "Insert a new empty regular file before point."
1038 (interactive "*sFile name: ")
1039 (let* ((buffer (current-buffer))
1040 (index (or index (tar-current-position)))
1041 (d-list (and (not (zerop index))
1042 (nthcdr (+ -1 index) tar-parse-info)))
1044 (tar-header-data-end (car d-list))
1047 (tar-new-regular-file-header filename)))
1048 ;; Update the data buffer; fill the missing descriptor fields.
1049 (with-current-buffer tar-data-buffer
1051 (insert (tar-header-serialize new-descriptor t))
1052 (setf (tar-header-data-start new-descriptor)
1053 (copy-marker (point) nil)))
1054 ;; Update tar-parse-info.
1056 (setcdr d-list (cons new-descriptor (cdr d-list)))
1057 (setq tar-parse-info (cons new-descriptor tar-parse-info)))
1058 ;; Update the listing buffer.
1060 (goto-char (point-min))
1061 (forward-line index)
1062 (let ((inhibit-read-only t))
1063 (insert (tar-header-block-summarize new-descriptor) ?\n)))
1067 (defun tar-flag-deleted (p &optional unflag)
1068 "In Tar mode, mark this sub-file to be deleted from the tar file.
1069 With a prefix argument, mark that many files."
1072 (dotimes (i (abs p))
1073 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
1076 (insert (if unflag " " "D"))))
1077 (forward-line (if (< p 0) -1 1)))
1078 (if (eobp) nil (forward-char 36)))
1080 (defun tar-unflag (p)
1081 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1082 With a prefix argument, un-mark that many files forward."
1084 (tar-flag-deleted p t))
1086 (defun tar-unflag-backwards (p)
1087 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1088 With a prefix argument, un-mark that many files backward."
1090 (tar-flag-deleted (- p) t))
1093 (defun tar-expunge-internal ()
1094 "Expunge the tar-entry specified by the current line."
1095 (let ((descriptor (tar-current-descriptor)))
1097 ;; delete the current line...
1098 (delete-region (line-beginning-position) (line-beginning-position 2))
1100 ;; delete the data pointer...
1101 (setq tar-parse-info (delq descriptor tar-parse-info))
1103 ;; delete the data from inside the file...
1104 (with-current-buffer tar-data-buffer
1105 (delete-region (or (tar-header-header-start descriptor)
1106 (- (tar-header-data-start descriptor) 512))
1107 (tar-header-data-end descriptor)))))
1110 (defun tar-expunge (&optional noconfirm)
1111 "In Tar mode, delete all the archived files flagged for deletion.
1112 This does not modify the disk image; you must save the tar file itself
1113 for this to be permanent."
1116 (y-or-n-p "Expunge files marked for deletion? "))
1119 (goto-char (point-min))
1121 (if (= (following-char) ?D)
1122 (progn (tar-expunge-internal)
1125 ;; after doing the deletions, add any padding that may be necessary.
1126 (tar-pad-to-blocksize))
1128 (message "Nothing to expunge.")
1129 (message "%s files expunged. Be sure to save this buffer." n)))))
1132 (defun tar-clear-modification-flags ()
1133 "Remove the stars at the beginning of each line."
1136 (goto-char (point-min))
1138 (if (not (eq (following-char) ?\s))
1139 (progn (delete-char 1) (insert " ")))
1143 (defun tar-chown-entry (new-uid)
1144 "Change the user-id associated with this entry in the tar file.
1145 If this tar file was written by GNU tar, then you will be able to edit
1146 the user id as a string; otherwise, you must edit it as a number.
1147 You can force editing as a number by calling this with a prefix arg.
1148 This does not modify the disk image; you must save the tar file itself
1149 for this to be permanent."
1152 (let ((descriptor (tar-current-descriptor)))
1153 (if (or current-prefix-arg
1154 (not (tar-header-magic descriptor)))
1157 (format "%s" (tar-header-uid descriptor)))
1158 (read-string "New UID string: " (tar-header-uname descriptor))))))
1159 (cond ((stringp new-uid)
1160 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
1161 (tar-alter-one-field tar-uname-offset
1162 (concat (encode-coding-string
1163 new-uid tar-file-name-coding-system)
1166 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
1167 (tar-alter-one-field tar-uid-offset
1168 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1171 (defun tar-chgrp-entry (new-gid)
1172 "Change the group-id associated with this entry in the tar file.
1173 If this tar file was written by GNU tar, then you will be able to edit
1174 the group id as a string; otherwise, you must edit it as a number.
1175 You can force editing as a number by calling this with a prefix arg.
1176 This does not modify the disk image; you must save the tar file itself
1177 for this to be permanent."
1180 (let ((descriptor (tar-current-descriptor)))
1181 (if (or current-prefix-arg
1182 (not (tar-header-magic descriptor)))
1185 (format "%s" (tar-header-gid descriptor)))
1186 (read-string "New GID string: " (tar-header-gname descriptor))))))
1187 (cond ((stringp new-gid)
1188 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1189 (tar-alter-one-field tar-gname-offset
1190 (concat (encode-coding-string
1191 new-gid tar-file-name-coding-system)
1194 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1195 (tar-alter-one-field tar-gid-offset
1196 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1198 (defun tar-rename-entry (new-name)
1199 "Change the name associated with this entry in the tar file.
1200 This does not modify the disk image; you must save the tar file itself
1201 for this to be permanent."
1203 (list (read-string "New name: "
1204 (tar-header-name (tar-current-descriptor)))))
1205 (if (string= "" new-name) (error "zero length name"))
1206 (let ((encoded-new-name (encode-coding-string new-name
1207 tar-file-name-coding-system))
1208 (descriptor (tar-current-descriptor))
1210 (when (tar-header-header-start descriptor)
1211 ;; FIXME: Make it work for ././@LongLink.
1212 (error "Rename with @LongLink format is not implemented"))
1214 (when (and (> (length encoded-new-name) 98)
1215 (string-match "/" encoded-new-name
1216 (- (length encoded-new-name) 99))
1217 (< (match-beginning 0) 155))
1218 (unless (equal (tar-header-magic descriptor) "ustar\0")
1219 (tar-alter-one-field tar-magic-offset (concat "ustar\0" "00")))
1220 (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
1221 (setq encoded-new-name (substring encoded-new-name (match-end 0))))
1223 (if (> (length encoded-new-name) 98) (error "name too long"))
1224 (setf (tar-header-name descriptor) new-name)
1225 (tar-alter-one-field 0
1226 (substring (concat encoded-new-name (make-string 99 0)) 0 99))
1228 (tar-alter-one-field tar-prefix-offset
1229 (substring (concat prefix (make-string 155 0)) 0 155)))))
1232 (defun tar-chmod-entry (new-mode)
1233 "Change the protection bits associated with this entry in the tar file.
1234 This does not modify the disk image; you must save the tar file itself
1235 for this to be permanent."
1236 (interactive (list (tar-parse-octal-integer-safe
1237 (read-string "New protection (octal): "))))
1238 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1239 (tar-alter-one-field tar-mode-offset
1240 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1243 (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
1244 (unless descriptor (setq descriptor (tar-current-descriptor)))
1246 ;; update the header-line.
1247 (let ((col (current-column)))
1248 (delete-region (line-beginning-position)
1249 (prog2 (forward-line 1)
1251 ;; Insert the new text after the old, before deleting,
1252 ;; to preserve markers such as the window start.
1253 (insert (tar-header-block-summarize descriptor) "\n")))
1254 (forward-line -1) (move-to-column col))
1256 (cl-assert (tar-data-swapped-p))
1257 (with-current-buffer tar-data-buffer
1258 (let* ((start (- (tar-header-data-start descriptor) 512)))
1260 ;; delete the old field and insert a new one.
1261 (goto-char (+ start data-position))
1262 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1263 (cl-assert (not (or enable-multibyte-characters
1264 (multibyte-string-p new-data-string))))
1265 (insert new-data-string)
1267 ;; compute a new checksum and insert it.
1268 (let ((chk (tar-header-block-checksum
1269 (buffer-substring start (+ start 512)))))
1270 (goto-char (+ start tar-chk-offset))
1271 (delete-region (point) (+ (point) 8))
1272 (insert (format "%6o\0 " chk))
1273 (setf (tar-header-checksum descriptor) chk)
1275 ;; ok, make sure we didn't botch it.
1276 (tar-header-block-check-checksum
1277 (buffer-substring start (+ start 512))
1278 chk (tar-header-name descriptor))
1282 (defun tar-octal-time (timeval)
1283 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1284 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1285 (format "%05o%01o%05o"
1287 (logior (lsh (logand 3 hibits) 1)
1288 (if (> (logand lobits 32768) 0) 1 0))
1289 (logand 32767 lobits)
1292 (defun tar-subfile-save-buffer ()
1293 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1294 This doesn't write anything to disk; you must save the parent tar-file buffer
1295 to make your changes permanent."
1297 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1298 (error "This buffer has no superior tar file buffer"))
1299 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1300 (error "This buffer doesn't have an index into its superior tar file!"))
1301 (let ((subfile (current-buffer))
1302 (coding buffer-file-coding-system)
1303 (descriptor tar-superior-descriptor)
1305 (with-current-buffer tar-superior-buffer
1306 (let* ((start (tar-header-data-start descriptor))
1307 (size (tar-header-size descriptor))
1308 (head (memq descriptor tar-parse-info)))
1310 (error "Can't find this tar file entry in its parent tar file!"))
1311 (with-current-buffer tar-data-buffer
1312 ;; delete the old data...
1313 (let* ((data-start start)
1314 (data-end (+ data-start (tar-roundup-512 size))))
1315 (narrow-to-region data-start data-end)
1316 (delete-region (point-min) (point-max))
1317 ;; insert the new data...
1318 (goto-char data-start)
1319 (let ((dest (current-buffer)))
1320 (with-current-buffer subfile
1323 (encode-coding-region (point-min) (point-max) coding dest))))
1324 (setq subfile-size (- (point-max) (point-min)))
1326 ;; pad the new data out to a multiple of 512...
1327 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1328 (goto-char (point-max))
1329 (insert (make-string (- subfile-size-pad subfile-size) 0))
1331 ;; update the data of this files...
1332 (setf (tar-header-size descriptor) subfile-size)
1334 ;; Update the size field in the header block.
1337 ;; alter the descriptor-line and header
1339 (let ((position (- (length tar-parse-info) (length head))))
1340 (goto-char (point-min))
1341 (forward-line position)
1342 (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
1344 ;; Maybe update the datestamp.
1345 (when tar-update-datestamp
1346 (tar-alter-one-field tar-time-offset
1347 (concat (tar-octal-time (current-time)) " "))))
1348 ;; After doing the insertion, add any necessary final padding.
1349 (tar-pad-to-blocksize))
1350 (set-buffer-modified-p t) ; mark the tar file as modified
1352 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1353 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1354 (buffer-name tar-superior-buffer))
1355 ;; Prevent basic-save-buffer from changing our coding-system.
1356 (setq last-coding-system-used buffer-file-coding-system)
1357 ;; Prevent ordinary saving from happening.
1361 ;; When this function is called, it is sure that the buffer is unibyte.
1362 (defun tar-pad-to-blocksize ()
1363 "If we are being anal about tar file blocksizes, fix up the current buffer.
1364 Leaves the region wide."
1365 (if (null tar-anal-blocksize)
1367 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1368 (start (tar-header-data-start last-desc))
1369 (link-p (tar-header-link-type last-desc))
1370 (size (if link-p 0 (tar-header-size last-desc)))
1371 (data-end (+ start size))
1372 (bbytes (ash tar-anal-blocksize 9))
1373 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
1374 ;; If the padding after the last data is too long, delete some;
1375 ;; else insert some until we are padded out to the right number of blocks.
1377 (with-current-buffer tar-data-buffer
1378 (let ((goal-end (+ (point-min) pad-to)))
1379 (if (> (point-max) goal-end)
1380 (delete-region goal-end (point-max))
1381 (goto-char (point-max))
1382 (insert (make-string (- goal-end (point-max)) ?\0))))))))
1385 ;; Used in write-region-annotate-functions to write tar-files out correctly.
1386 (defun tar-write-region-annotate (start _end)
1387 ;; When called from write-file (and auto-save), `start' is nil.
1388 ;; When called from M-x write-region, we assume the user wants to save
1389 ;; (part of) the summary, not the tar data.
1390 (unless (or start (not (tar-data-swapped-p)))
1391 (tar-clear-modification-flags)
1392 (set-buffer tar-data-buffer)
1397 ;;; tar-mode.el ends here