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
269 (decode-coding-string name coding
)
271 (decode-coding-string linkname coding
))
272 (if (and (null link-p
) (string-match "/\\'" name
))
273 (setq link-p
5)) ; directory
275 (if (and (equal name
"././@LongLink")
276 ;; Supposedly @LongLink is only used for GNUTAR
277 ;; format (i.e. "ustar ") but some POSIX Tar files
278 ;; (with "ustar\0") have been seen using it as well.
279 (member magic-str
'("ustar " "ustar\0")))
280 ;; This is a GNU Tar long-file-name header.
281 (let* ((size (tar-parse-octal-integer
282 string tar-size-offset tar-time-offset
))
283 ;; -1 so as to strip the terminating 0 byte.
284 (name (decode-coding-string
285 (buffer-substring pos
(+ pos size -
1)) coding
))
286 (descriptor (tar-header-block-tokenize
287 (+ pos
(tar-roundup-512 size
))
290 ((eq link-p
(- ?L ?
0)) ;GNUTYPE_LONGNAME.
291 (setf (tar-header-name descriptor
) name
))
292 ((eq link-p
(- ?K ?
0)) ;GNUTYPE_LONGLINK.
293 (setf (tar-header-link-name descriptor
) name
))
295 (message "Unrecognized GNU Tar @LongLink format")))
296 (setf (tar-header-header-start descriptor
)
297 (copy-marker (- pos
512) t
))
301 (copy-marker pos nil
)
303 (tar-parse-octal-integer string tar-mode-offset tar-uid-offset
)
304 (tar-parse-octal-integer string tar-uid-offset tar-gid-offset
)
305 (tar-parse-octal-integer string tar-gid-offset tar-size-offset
)
306 (tar-parse-octal-integer string tar-size-offset tar-time-offset
)
307 (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset
)
308 (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset
)
313 (decode-coding-string
314 (substring string tar-uname-offset uname-end
) coding
))
316 (decode-coding-string
317 (substring string tar-gname-offset gname-end
) coding
))
318 (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset
)
319 (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset
)
323 (defun tar-header-data-end (descriptor)
324 (let* ((data-start (tar-header-data-start descriptor
))
325 (link-type (tar-header-link-type descriptor
))
326 (size (tar-header-size descriptor
)))
328 ;; Ignore size for files of type 1-6
329 (if (and (not (memq link-type
'(1 2 3 4 5 6))) (> size
0))
330 (tar-roundup-512 size
)
333 (defun tar-parse-octal-integer (string &optional start end
)
334 (if (null start
) (setq start
0))
335 (if (null end
) (setq end
(length string
)))
336 (if (= (aref string start
) 0)
340 (setq n
(if (< (aref string start
) ?
0) n
341 (+ (* n
8) (- (aref string start
) ?
0)))
345 (defun tar-parse-octal-long-integer (string &optional start end
)
346 (if (null start
) (setq start
0))
347 (if (null end
) (setq end
(length string
)))
348 (if (= (aref string start
) 0)
353 (if (>= (aref string start
) ?
0)
354 (setq lo
(+ (* lo
8) (- (aref string start
) ?
0))
355 hi
(+ (* hi
8) (ash lo -
16))
356 lo
(logand lo
65535)))
357 (setq start
(1+ start
)))
360 (defun tar-parse-octal-integer-safe (string)
361 (if (zerop (length string
)) (error "empty string"))
363 (if (or (< c ?
0) (> c ?
7))
364 (error "`%c' is not an octal digit" c
)))
366 (tar-parse-octal-integer string
))
368 (defun tar-new-regular-file-header (filename &optional size time
)
369 "Return a Tar header for a regular file.
370 The header will lack a proper checksum; use `tar-header-block-checksum'
371 to compute one, or request `tar-header-serialize' to do that.
373 Other tar-mode facilities may also require the data-start header
374 field to be set to a valid value.
376 If SIZE is not given or nil, it defaults to 0.
377 If TIME is not given or nil, assume now."
381 #o644
0 0 (or size
0)
382 (or time
(current-time))
385 nil nil nil nil nil
))
387 (defun tar--pad-to (pos)
388 (make-string (+ pos
(- (point)) (point-min)) 0))
390 (defun tar--put-at (pos val
&optional fmt mask
)
392 (insert (tar--pad-to pos
)
394 (format fmt
(if mask
(logand mask val
) val
))
397 (defun tar-header-serialize (header &optional update-checksum
)
398 "Return the serialization of a Tar HEADER as a string.
399 This function calls `tar-header-block-check-checksum' to ensure the
402 If UPDATE-CHECKSUM is non-nil, update HEADER with the newly-computed
403 checksum before doing the check."
405 (set-buffer-multibyte nil
)
407 (encode-coding-string (tar-header-name header
)
408 tar-file-name-coding-system
)))
409 (unless (< (length encoded-name
) 99)
410 ;; FIXME: Implement it.
411 (error "Long file name support is not implemented"))
412 (insert encoded-name
))
413 (tar--put-at tar-mode-offset
(tar-header-mode header
) "%6o\0 " #o777777
)
414 (tar--put-at tar-uid-offset
(tar-header-uid header
) "%6o\0 " #o777777
)
415 (tar--put-at tar-gid-offset
(tar-header-gid header
) "%6o\0 " #o777777
)
416 (tar--put-at tar-size-offset
(tar-header-size header
) "%11o ")
417 (insert (tar--pad-to tar-time-offset
)
418 (tar-octal-time (tar-header-date header
))
420 ;; Omit tar-header-checksum (tar-chk-offset) for now.
421 (tar--put-at tar-linkp-offset
(tar-header-link-type header
))
422 (tar--put-at tar-link-offset
(tar-header-link-name header
))
423 (when (tar-header-magic header
)
424 (tar--put-at tar-magic-offset
(tar-header-magic header
))
425 (tar--put-at tar-uname-offset
(tar-header-uname header
))
426 (tar--put-at tar-gname-offset
(tar-header-gname header
))
427 (tar--put-at tar-dmaj-offset
(tar-header-dmaj header
) "%7o\0" #o7777777
)
428 (tar--put-at tar-dmin-offset
(tar-header-dmin header
) "%7o\0" #o7777777
))
430 (let ((ck (tar-header-block-checksum (buffer-string))))
431 (goto-char (+ (point-min) tar-chk-offset
))
433 (insert (format "%6o\0 " ck
))
434 (when update-checksum
435 (setf (tar-header-checksum header
) ck
))
436 (tar-header-block-check-checksum (buffer-string)
437 (tar-header-checksum header
)
438 (tar-header-name header
)))
443 (defun tar-header-block-checksum (string)
444 "Compute and return a tar-acceptable checksum for this block."
445 (cl-assert (not (multibyte-string-p string
)))
446 (let* ((chk-field-start tar-chk-offset
)
447 (chk-field-end (+ chk-field-start
8))
450 ;; Add up all of the characters except the ones in the checksum field.
451 ;; Add that field as if it were filled with spaces.
452 (while (< i chk-field-start
)
453 (setq sum
(+ sum
(aref string i
))
455 (setq i chk-field-end
)
457 (setq sum
(+ sum
(aref string i
))
461 (defun tar-header-block-check-checksum (hblock desired-checksum file-name
)
462 "Beep and print a warning if the checksum doesn't match."
463 (if (not (= desired-checksum
(tar-header-block-checksum hblock
)))
464 (progn (beep) (message "Invalid checksum for file %s!" file-name
))))
466 (defun tar-clip-time-string (time)
467 (let ((str (current-time-string time
)))
468 (concat " " (substring str
4 16) (format-time-string " %Y" time
))))
470 (defun tar-grind-file-mode (mode)
471 "Construct a `rw-r--r--' string indicating MODE.
472 MODE should be an integer which is a file mode value."
474 (if (zerop (logand 256 mode
)) ?- ?r
)
475 (if (zerop (logand 128 mode
)) ?- ?w
)
476 (if (zerop (logand 2048 mode
))
477 (if (zerop (logand 64 mode
)) ?- ?x
)
478 (if (zerop (logand 64 mode
)) ?S ?s
))
479 (if (zerop (logand 32 mode
)) ?- ?r
)
480 (if (zerop (logand 16 mode
)) ?- ?w
)
481 (if (zerop (logand 1024 mode
))
482 (if (zerop (logand 8 mode
)) ?- ?x
)
483 (if (zerop (logand 8 mode
)) ?S ?s
))
484 (if (zerop (logand 4 mode
)) ?- ?r
)
485 (if (zerop (logand 2 mode
)) ?- ?w
)
486 (if (zerop (logand 512 mode
))
487 (if (zerop (logand 1 mode
)) ?- ?x
)
488 (if (zerop (logand 1 mode
)) ?T ?t
))))
490 (defun tar-header-block-summarize (tar-hblock &optional mod-p
)
491 "Return a line similar to the output of `tar -vtf'."
492 (let ((name (tar-header-name tar-hblock
))
493 (mode (tar-header-mode tar-hblock
))
494 (uid (tar-header-uid tar-hblock
))
495 (gid (tar-header-gid tar-hblock
))
496 (uname (tar-header-uname tar-hblock
))
497 (gname (tar-header-gname tar-hblock
))
498 (size (tar-header-size tar-hblock
))
499 (time (tar-header-date tar-hblock
))
500 ;; (ck (tar-header-checksum tar-hblock))
501 (type (tar-header-link-type tar-hblock
))
502 (link-name (tar-header-link-name tar-hblock
)))
503 (format "%c%c%s %7s/%-7s %7s%s %s%s"
505 (cond ((or (eq type nil
) (eq type
0)) ?-
)
506 ((eq type
1) ?h
) ; link
507 ((eq type
2) ?l
) ; symlink
508 ((eq type
3) ?c
) ; char special
509 ((eq type
4) ?b
) ; block special
510 ((eq type
5) ?d
) ; directory
511 ((eq type
6) ?p
) ; FIFO/pipe
512 ((eq type
20) ?
*) ; directory listing
513 ((eq type
28) ?L
) ; next has longname
514 ((eq type
29) ?M
) ; multivolume continuation
515 ((eq type
35) ?S
) ; sparse
516 ((eq type
38) ?V
) ; volume header
517 ((eq type
55) ?H
) ; pax global extended header
518 ((eq type
72) ?X
) ; pax extended header
521 (tar-grind-file-mode mode
)
522 (if (= 0 (length uname
)) uid uname
)
523 (if (= 0 (length gname
)) gid gname
)
525 (if tar-mode-show-date
(tar-clip-time-string time
) "")
527 'mouse-face
'highlight
528 'help-echo
"mouse-2: extract this file into a buffer")
529 (if (or (eq type
1) (eq type
2))
530 (concat (if (= type
1) " ==> " " --> ") link-name
)
533 (defun tar-untar-buffer ()
534 "Extract all archive members in the tar-file into the current directory."
536 ;; FIXME: make it work even if we're not in tar-mode.
537 (let ((descriptors tar-parse-info
)) ;Read the var in its buffer.
539 (if (tar-data-swapped-p) tar-data-buffer
(current-buffer))
540 (set-buffer-multibyte nil
) ;Hopefully, a no-op.
541 (dolist (descriptor descriptors
)
542 (let* ((name (tar-header-name descriptor
))
543 (dir (if (eq (tar-header-link-type descriptor
) 5)
545 (file-name-directory name
)))
546 (link-desc (tar--describe-as-link descriptor
))
547 (start (tar-header-data-start descriptor
))
548 (end (+ start
(tar-header-size descriptor
))))
549 (unless (file-directory-p name
)
550 (message "Extracting %s" name
)
551 (if (and dir
(not (file-exists-p dir
)))
552 (make-directory dir t
))
553 (unless (file-directory-p name
)
554 (let ((coding-system-for-write 'no-conversion
))
556 (lwarn '(tar link
) :warning
557 "Extracted `%s', %s, as a normal file"
559 (write-region start end name
)))
560 (set-file-modes name
(tar-header-mode descriptor
))))))))
562 (defun tar-summarize-buffer ()
563 "Parse the contents of the tar file in the current buffer."
564 (cl-assert (tar-data-swapped-p))
565 (let* ((modified (buffer-modified-p))
568 (coding tar-file-name-coding-system
)
570 (with-current-buffer tar-data-buffer
571 (make-progress-reporter "Parsing tar file..."
572 (point-min) (point-max))))
574 (with-current-buffer tar-data-buffer
575 (while (and (< pos
(point-max))
576 (setq descriptor
(tar-header-block-tokenize pos coding
)))
577 (let ((size (tar-header-size descriptor
)))
579 (error "%s has size %s - corrupted"
580 (tar-header-name descriptor
) size
)))
582 ;; This is just too slow. Don't really need it anyway....
583 ;;(tar-header-block-check-checksum
584 ;; hblock (tar-header-block-checksum hblock)
585 ;; (tar-header-name descriptor))
587 (push descriptor result
)
588 (setq pos
(tar-header-data-end descriptor
))
589 (progress-reporter-update progress-reporter pos
)))
591 (set (make-local-variable 'tar-parse-info
) (nreverse result
))
592 ;; A tar file should end with a block or two of nulls,
593 ;; but let's not get a fatal error if it doesn't.
594 (if (null descriptor
)
595 (progress-reporter-done progress-reporter
)
596 (message "Warning: premature EOF parsing tar file"))
597 (goto-char (point-min))
598 (let ((create-lockfiles nil
) ; avoid changing dir mtime by lock_file
599 (inhibit-read-only t
)
601 (mapconcat 'tar-header-block-summarize tar-parse-info
"\n")))
602 (insert total-summaries
"\n")
603 (goto-char (point-min))
604 (restore-buffer-modified-p modified
))))
607 (let ((map (make-keymap)))
608 (suppress-keymap map
)
609 (define-key map
" " 'tar-next-line
)
610 (define-key map
"C" 'tar-copy
)
611 (define-key map
"d" 'tar-flag-deleted
)
612 (define-key map
"\^D" 'tar-flag-deleted
)
613 (define-key map
"e" 'tar-extract
)
614 (define-key map
"f" 'tar-extract
)
615 (define-key map
"\C-m" 'tar-extract
)
616 (define-key map
[mouse-2
] 'tar-mouse-extract
)
617 (define-key map
"g" 'revert-buffer
)
618 (define-key map
"n" 'tar-next-line
)
619 (define-key map
"\^N" 'tar-next-line
)
620 (define-key map
[down] 'tar-next-line)
621 (define-key map "o" 'tar-extract-other-window)
622 (define-key map "p" 'tar-previous-line)
623 (define-key map "\^P" 'tar-previous-line)
624 (define-key map [up] 'tar-previous-line)
625 (define-key map "I" 'tar-new-entry)
626 (define-key map "R" 'tar-rename-entry)
627 (define-key map "u" 'tar-unflag)
628 (define-key map "v" 'tar-view)
629 (define-key map "w" 'woman-tar-extract-file)
630 (define-key map "x" 'tar-expunge)
631 (define-key map "\177" 'tar-unflag-backwards)
632 (define-key map "E" 'tar-extract-other-window)
633 (define-key map "M" 'tar-chmod-entry)
634 (define-key map "G" 'tar-chgrp-entry)
635 (define-key map "O" 'tar-chown-entry)
636 ;; Let mouse-1 follow the link.
637 (define-key map [follow-link] 'mouse-face)
639 ;; Make menu bar items.
641 ;; Get rid of the Edit menu bar item to save space.
642 (define-key map [menu-bar edit] 'undefined)
644 (define-key map [menu-bar immediate]
645 (cons "Immediate" (make-sparse-keymap "Immediate")))
647 (define-key map [menu-bar immediate woman]
648 '("Read Man Page (WoMan)" . woman-tar-extract-file))
649 (define-key map [menu-bar immediate view]
650 '("View This File" . tar-view))
651 (define-key map [menu-bar immediate display]
652 '("Display in Other Window" . tar-display-other-window))
653 (define-key map [menu-bar immediate find-file-other-window]
654 '("Find in Other Window" . tar-extract-other-window))
655 (define-key map [menu-bar immediate find-file]
656 '("Find This File" . tar-extract))
658 (define-key map [menu-bar mark]
659 (cons "Mark" (make-sparse-keymap "Mark")))
661 (define-key map [menu-bar mark unmark-all]
662 '("Unmark All" . tar-clear-modification-flags))
663 (define-key map [menu-bar mark deletion]
664 '("Flag" . tar-flag-deleted))
665 (define-key map [menu-bar mark unmark]
666 '("Unflag" . tar-unflag))
668 (define-key map [menu-bar operate]
669 (cons "Operate" (make-sparse-keymap "Operate")))
671 (define-key map [menu-bar operate chown]
672 '("Change Owner..." . tar-chown-entry))
673 (define-key map [menu-bar operate chgrp]
674 '("Change Group..." . tar-chgrp-entry))
675 (define-key map [menu-bar operate chmod]
676 '("Change Mode..." . tar-chmod-entry))
677 (define-key map [menu-bar operate rename]
678 '("Rename to..." . tar-rename-entry))
679 (define-key map [menu-bar operate copy]
680 '("Copy to..." . tar-copy))
681 (define-key map [menu-bar operate expunge]
682 '("Expunge Marked Files" . tar-expunge))
685 "Local keymap for Tar mode listings.")
688 ;; tar mode is suitable only for specially formatted data.
689 (put 'tar-mode 'mode-class 'special)
690 (put 'tar-subfile-mode 'mode-class 'special)
692 (defun tar-change-major-mode-hook ()
693 ;; Bring the actual Tar data back into the main buffer.
694 (when (tar-data-swapped-p) (tar-swap-data))
695 ;; Throw away the summary.
696 (when (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
698 (defun tar-mode-kill-buffer-hook ()
699 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
702 (define-derived-mode tar-mode special-mode "Tar"
703 "Major mode for viewing a tar file as a dired-like listing of its contents.
704 You can move around using the usual cursor motion commands.
705 Letters no longer insert themselves.
706 Type `e' to pull a file out of the tar file and into its own buffer;
707 or click mouse-2 on the file's line in the Tar mode buffer.
708 Type `c' to copy an entry from the tar file into another file on disk.
710 If you edit a sub-file of this archive (as with the `e' command) and
711 save it with \\[save-buffer], the contents of that buffer will be
712 saved back into the tar-file buffer; in this way you can edit a file
713 inside of a tar archive without extracting it and re-archiving it.
715 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
717 (and buffer-file-name
718 (file-writable-p buffer-file-name)
719 (setq buffer-read-only nil)) ; undo what `special-mode' did
720 (make-local-variable 'tar-parse-info)
721 (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
722 (set (make-local-variable 'local-enable-local-variables) nil)
723 (set (make-local-variable 'next-line-add-newlines) nil)
724 (set (make-local-variable 'tar-file-name-coding-system)
725 (or file-name-coding-system
726 default-file-name-coding-system
727 locale-coding-system))
728 ;; Prevent loss of data when saving the file.
729 (set (make-local-variable 'file-precious-flag) t)
730 (buffer-disable-undo)
732 ;; Now move the Tar data into an auxiliary buffer, so we can use the main
733 ;; buffer for the summary.
734 (cl-assert (not (tar-data-swapped-p)))
735 (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
736 ;; We started using write-contents-functions, but this hook is not
737 ;; used during auto-save, so we now use
738 ;; write-region-annotate-functions which hooks at a lower-level.
739 (add-hook 'write-region-annotate-functions 'tar-write-region-annotate nil t)
740 (add-hook 'kill-buffer-hook 'tar-mode-kill-buffer-hook nil t)
741 (add-hook 'change-major-mode-hook 'tar-change-major-mode-hook nil t)
742 ;; Tar data is made of bytes, not chars.
743 (set-buffer-multibyte nil) ;Hopefully a no-op.
744 (set (make-local-variable 'tar-data-buffer)
745 (generate-new-buffer (format " *tar-data %s*"
746 (file-name-nondirectory
747 (or buffer-file-name (buffer-name))))))
751 (tar-summarize-buffer)
754 ;; If summarizing caused an error, then maybe the buffer doesn't contain
755 ;; tar data. Rather than show a mysterious empty buffer, let's
756 ;; revert to fundamental-mode.
758 (signal (car err) (cdr err)))))
760 (autoload 'woman-tar-extract-file "woman"
761 "In tar mode, run the WoMan man-page browser on this file." t)
763 (define-minor-mode tar-subfile-mode
764 "Minor mode for editing an element of a tar-file.
766 This mode arranges for \"saving\" this buffer to write the data
767 into the tar-file buffer that it came from. The changes will
768 actually appear on disk when you save the tar-file's buffer."
769 ;; Don't do this, because it is redundant and wastes mode line space.
770 ;; :lighter " TarFile"
772 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
773 (error "This buffer is not an element of a tar file"))
774 (cond (tar-subfile-mode
775 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
776 ;; turn off auto-save.
778 (setq buffer-auto-save-file-name nil))
780 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
783 ;; Revert the buffer and recompute the dired-like listing.
784 (defun tar-mode-revert (&optional no-auto-save no-confirm)
786 (let ((revert-buffer-function nil))
787 (if (tar-data-swapped-p) (tar-swap-data))
788 ;; FIXME: If we ask for confirmation, the user will be temporarily
789 ;; looking at the raw data.
790 (revert-buffer no-auto-save no-confirm 'preserve-modes)
791 ;; Recompute the summary.
792 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
794 (unless (tar-data-swapped-p) (tar-swap-data))))
797 (defun tar-next-line (arg)
798 "Move cursor vertically down ARG lines and to the start of the filename."
801 (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
803 (defun tar-previous-line (arg)
804 "Move cursor vertically up ARG lines and to the start of the filename."
806 (tar-next-line (- arg)))
808 (defun tar-current-position ()
809 "Return the `tar-parse-info' index for the current line."
810 (count-lines (point-min) (line-beginning-position)))
812 (defun tar-current-descriptor (&optional noerror)
813 "Return the tar-descriptor of the current line, or signals an error."
814 ;; I wish lines had plists, like in ZMACS...
815 (or (nth (tar-current-position)
819 (error "This line does not describe a tar-file entry"))))
821 (defun tar--describe-as-link (descriptor)
822 (let ((link-p (tar-header-link-type descriptor)))
824 (cond ((eq link-p 5) "a directory")
825 ((eq link-p 20) "a tar directory header")
826 ((eq link-p 28) "a next has longname")
827 ((eq link-p 29) "a multivolume-continuation")
828 ((eq link-p 35) "a sparse entry")
829 ((eq link-p 38) "a volume header")
830 ((eq link-p 55) "a pax global extended header")
831 ((eq link-p 72) "a pax extended header")
834 (defun tar--check-descriptor (descriptor)
835 (let ((link-desc (tar--describe-as-link descriptor)))
837 (error "This is %s, not a real file" link-desc))))
839 (defun tar-get-descriptor ()
840 (let* ((descriptor (tar-current-descriptor))
841 (size (tar-header-size descriptor)))
842 (tar--check-descriptor descriptor)
843 (if (zerop size) (message "This is a zero-length file"))
846 (defun tar-get-file-descriptor (file)
847 ;; Used by package.el.
849 (dolist (hdr tar-parse-info)
850 (when (equal file (tar-header-name hdr))
852 (tar--check-descriptor desc)
855 (defun tar-mouse-extract (event)
856 "Extract a file whose tar directory line you click on."
858 (with-current-buffer (window-buffer (posn-window (event-end event)))
860 (goto-char (posn-point (event-end event)))
861 ;; Just make sure this doesn't get an error.
862 (tar-get-descriptor)))
863 (select-window (posn-window (event-end event)))
864 (goto-char (posn-point (event-end event)))
867 (defun tar-file-name-handler (op &rest args)
868 "Helper function for `tar-extract'."
869 (or (eq op 'file-exists-p)
870 (let ((file-name-handler-alist nil))
873 (defun tar--extract (descriptor)
874 "Extract this entry of the tar file into its own buffer."
875 (let* ((name (tar-header-name descriptor))
876 (size (tar-header-size descriptor))
877 (start (tar-header-data-start descriptor))
879 (tarname (buffer-name))
880 (bufname (concat (file-name-nondirectory name)
884 (buffer (generate-new-buffer bufname)))
885 (with-current-buffer tar-data-buffer
887 (narrow-to-region start end)
889 (setq coding (or coding-system-for-read
890 (and set-auto-coding-function
891 (funcall set-auto-coding-function
893 ;; The following binding causes
894 ;; find-buffer-file-type-coding-system
895 ;; (defined on dos-w32.el) to act as if
896 ;; the file being extracted existed, so
897 ;; that the file's contents' encoding and
898 ;; EOL format are auto-detected.
899 (let ((file-name-handler-alist
900 '(("" . tar-file-name-handler))))
901 (car (find-operation-coding-system
902 'insert-file-contents
903 (cons name (current-buffer)) t)))))
905 (eq (coding-system-type coding) 'undecided))
906 (setq coding (detect-coding-region start end t)))
907 (if (coding-system-get coding :for-unibyte)
908 (with-current-buffer buffer
909 (set-buffer-multibyte nil)))
911 (with-current-buffer buffer
912 (setq buffer-undo-list t))
913 (decode-coding-region start end coding buffer)
914 (with-current-buffer buffer
915 (setq buffer-undo-list nil))))
918 (defun tar-extract (&optional other-window-p)
919 "In Tar mode, extract this entry of the tar file into its own buffer."
921 (let* ((view-p (eq other-window-p 'view))
922 (descriptor (tar-get-descriptor))
923 (name (tar-header-name descriptor))
924 (tar-buffer (current-buffer))
925 (tarname (buffer-name))
926 (read-only-p (or buffer-read-only view-p))
927 (new-buffer-file-name (expand-file-name
928 ;; `:' is not allowed on Windows
930 (if (string-match "/" name)
932 ;; Make sure `name' contains a /
933 ;; so set-auto-mode doesn't try
934 ;; to look at `tarname' for hints.
935 (concat "./" name)))))
936 (buffer (get-file-buffer new-buffer-file-name))
939 (setq buffer (tar--extract descriptor))
940 (setq just-created t)
941 (with-current-buffer buffer
942 (goto-char (point-min))
943 (setq buffer-file-name new-buffer-file-name)
944 (setq buffer-file-truename
945 (abbreviate-file-name buffer-file-name))
946 ;; Force buffer-file-coding-system to what
947 ;; decode-coding-region actually used.
948 (set-buffer-file-coding-system last-coding-system-used t)
949 ;; Set the default-directory to the dir of the
951 (setq default-directory
952 (with-current-buffer tar-buffer
954 (set-buffer-modified-p nil)
955 (normal-mode) ; pick a mode.
956 (set (make-local-variable 'tar-superior-buffer) tar-buffer)
957 (set (make-local-variable 'tar-superior-descriptor) descriptor)
958 (setq buffer-read-only read-only-p)
959 (tar-subfile-mode 1)))
962 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
963 ((eq other-window-p 'display) (display-buffer buffer))
964 (other-window-p (switch-to-buffer-other-window buffer))
965 (t (switch-to-buffer buffer)))))
968 (defun tar-extract-other-window ()
969 "In Tar mode, find this entry of the tar file in another window."
973 (defun tar-display-other-window ()
974 "In Tar mode, display this entry of the tar file in another window."
976 (tar-extract 'display))
979 "In Tar mode, view the tar file entry on this line."
984 (defun tar-read-file-name (&optional prompt)
985 "Read a file name with this line's entry as the default."
986 (or prompt (setq prompt "Copy to: "))
987 (let* ((default-file (expand-file-name
988 (tar-header-name (tar-current-descriptor))))
989 (target (expand-file-name
990 (read-file-name prompt
991 (file-name-directory default-file)
993 (if (or (string= "" (file-name-nondirectory target))
994 (file-directory-p target))
995 (setq target (concat (if (string-match "/$" target)
996 (substring target 0 (1- (match-end 0)))
999 (file-name-nondirectory default-file))))
1003 (defun tar-copy (&optional to-file)
1004 "In Tar mode, extract this entry of the tar file into a file on disk.
1005 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
1006 the current tar-entry."
1007 (interactive (list (tar-read-file-name)))
1008 (let* ((descriptor (tar-get-descriptor))
1009 (name (tar-header-name descriptor))
1010 (size (tar-header-size descriptor))
1011 (start (tar-header-data-start descriptor))
1012 (end (+ start size))
1013 (inhibit-file-name-handlers inhibit-file-name-handlers)
1014 (inhibit-file-name-operation inhibit-file-name-operation))
1015 (with-current-buffer
1016 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
1017 ;; Inhibit compressing a subfile again if *both* name and
1018 ;; to-file are handled by jka-compr
1019 (if (and (eq (find-file-name-handler name 'write-region)
1021 (eq (find-file-name-handler to-file 'write-region)
1022 'jka-compr-handler))
1023 (setq inhibit-file-name-handlers
1024 (cons 'jka-compr-handler
1025 (and (eq inhibit-file-name-operation 'write-region)
1026 inhibit-file-name-handlers))
1027 inhibit-file-name-operation 'write-region))
1028 (let ((coding-system-for-write 'no-conversion))
1029 (write-region start end to-file nil nil nil t)))
1030 (message "Copied tar entry %s to %s" name to-file)))
1032 (defun tar-new-entry (filename &optional index)
1033 "Insert a new empty regular file before point."
1034 (interactive "*sFile name: ")
1035 (let* ((buffer (current-buffer))
1036 (index (or index (tar-current-position)))
1037 (d-list (and (not (zerop index))
1038 (nthcdr (+ -1 index) tar-parse-info)))
1040 (tar-header-data-end (car d-list))
1043 (tar-new-regular-file-header filename)))
1044 ;; Update the data buffer; fill the missing descriptor fields.
1045 (with-current-buffer tar-data-buffer
1047 (insert (tar-header-serialize new-descriptor t))
1048 (setf (tar-header-data-start new-descriptor)
1049 (copy-marker (point) nil)))
1050 ;; Update tar-parse-info.
1052 (setcdr d-list (cons new-descriptor (cdr d-list)))
1053 (setq tar-parse-info (cons new-descriptor tar-parse-info)))
1054 ;; Update the listing buffer.
1056 (goto-char (point-min))
1057 (forward-line index)
1058 (let ((inhibit-read-only t))
1059 (insert (tar-header-block-summarize new-descriptor) ?\n)))
1063 (defun tar-flag-deleted (p &optional unflag)
1064 "In Tar mode, mark this sub-file to be deleted from the tar file.
1065 With a prefix argument, mark that many files."
1068 (dotimes (i (abs p))
1069 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
1072 (insert (if unflag " " "D"))))
1073 (forward-line (if (< p 0) -1 1)))
1074 (if (eobp) nil (forward-char 36)))
1076 (defun tar-unflag (p)
1077 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1078 With a prefix argument, un-mark that many files forward."
1080 (tar-flag-deleted p t))
1082 (defun tar-unflag-backwards (p)
1083 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1084 With a prefix argument, un-mark that many files backward."
1086 (tar-flag-deleted (- p) t))
1089 (defun tar-expunge-internal ()
1090 "Expunge the tar-entry specified by the current line."
1091 (let ((descriptor (tar-current-descriptor)))
1093 ;; delete the current line...
1094 (delete-region (line-beginning-position) (line-beginning-position 2))
1096 ;; delete the data pointer...
1097 (setq tar-parse-info (delq descriptor tar-parse-info))
1099 ;; delete the data from inside the file...
1100 (with-current-buffer tar-data-buffer
1101 (delete-region (or (tar-header-header-start descriptor)
1102 (- (tar-header-data-start descriptor) 512))
1103 (tar-header-data-end descriptor)))))
1106 (defun tar-expunge (&optional noconfirm)
1107 "In Tar mode, delete all the archived files flagged for deletion.
1108 This does not modify the disk image; you must save the tar file itself
1109 for this to be permanent."
1112 (y-or-n-p "Expunge files marked for deletion? "))
1115 (goto-char (point-min))
1117 (if (= (following-char) ?D)
1118 (progn (tar-expunge-internal)
1121 ;; after doing the deletions, add any padding that may be necessary.
1122 (tar-pad-to-blocksize))
1124 (message "Nothing to expunge.")
1125 (message "%s files expunged. Be sure to save this buffer." n)))))
1128 (defun tar-clear-modification-flags ()
1129 "Remove the stars at the beginning of each line."
1132 (goto-char (point-min))
1134 (if (not (eq (following-char) ?\s))
1135 (progn (delete-char 1) (insert " ")))
1139 (defun tar-chown-entry (new-uid)
1140 "Change the user-id associated with this entry in the tar file.
1141 If this tar file was written by GNU tar, then you will be able to edit
1142 the user id as a string; otherwise, you must edit it as a number.
1143 You can force editing as a number by calling this with a prefix arg.
1144 This does not modify the disk image; you must save the tar file itself
1145 for this to be permanent."
1148 (let ((descriptor (tar-current-descriptor)))
1149 (if (or current-prefix-arg
1150 (not (tar-header-magic descriptor)))
1153 (format "%s" (tar-header-uid descriptor)))
1154 (read-string "New UID string: " (tar-header-uname descriptor))))))
1155 (cond ((stringp new-uid)
1156 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
1157 (tar-alter-one-field tar-uname-offset
1158 (concat (encode-coding-string
1159 new-uid tar-file-name-coding-system)
1162 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
1163 (tar-alter-one-field tar-uid-offset
1164 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1167 (defun tar-chgrp-entry (new-gid)
1168 "Change the group-id associated with this entry in the tar file.
1169 If this tar file was written by GNU tar, then you will be able to edit
1170 the group id as a string; otherwise, you must edit it as a number.
1171 You can force editing as a number by calling this with a prefix arg.
1172 This does not modify the disk image; you must save the tar file itself
1173 for this to be permanent."
1176 (let ((descriptor (tar-current-descriptor)))
1177 (if (or current-prefix-arg
1178 (not (tar-header-magic descriptor)))
1181 (format "%s" (tar-header-gid descriptor)))
1182 (read-string "New GID string: " (tar-header-gname descriptor))))))
1183 (cond ((stringp new-gid)
1184 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1185 (tar-alter-one-field tar-gname-offset
1186 (concat (encode-coding-string
1187 new-gid tar-file-name-coding-system)
1190 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1191 (tar-alter-one-field tar-gid-offset
1192 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1194 (defun tar-rename-entry (new-name)
1195 "Change the name associated with this entry in the tar file.
1196 This does not modify the disk image; you must save the tar file itself
1197 for this to be permanent."
1199 (list (read-string "New name: "
1200 (tar-header-name (tar-current-descriptor)))))
1201 (if (string= "" new-name) (error "zero length name"))
1202 (let ((encoded-new-name (encode-coding-string new-name
1203 tar-file-name-coding-system))
1204 (descriptor (tar-current-descriptor))
1206 (when (tar-header-header-start descriptor)
1207 ;; FIXME: Make it work for ././@LongLink.
1208 (error "Rename with @LongLink format is not implemented"))
1210 (when (and (> (length encoded-new-name) 98)
1211 (string-match "/" encoded-new-name
1212 (- (length encoded-new-name) 99))
1213 (< (match-beginning 0) 155))
1214 (unless (equal (tar-header-magic descriptor) "ustar\0")
1215 (tar-alter-one-field tar-magic-offset (concat "ustar\0" "00")))
1216 (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
1217 (setq encoded-new-name (substring encoded-new-name (match-end 0))))
1219 (if (> (length encoded-new-name) 98) (error "name too long"))
1220 (setf (tar-header-name descriptor) new-name)
1221 (tar-alter-one-field 0
1222 (substring (concat encoded-new-name (make-string 99 0)) 0 99))
1224 (tar-alter-one-field tar-prefix-offset
1225 (substring (concat prefix (make-string 155 0)) 0 155)))))
1228 (defun tar-chmod-entry (new-mode)
1229 "Change the protection bits associated with this entry in the tar file.
1230 This does not modify the disk image; you must save the tar file itself
1231 for this to be permanent."
1232 (interactive (list (tar-parse-octal-integer-safe
1233 (read-string "New protection (octal): "))))
1234 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1235 (tar-alter-one-field tar-mode-offset
1236 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1239 (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
1240 (unless descriptor (setq descriptor (tar-current-descriptor)))
1242 ;; update the header-line.
1243 (let ((col (current-column)))
1244 (delete-region (line-beginning-position)
1245 (prog2 (forward-line 1)
1247 ;; Insert the new text after the old, before deleting,
1248 ;; to preserve markers such as the window start.
1249 (insert (tar-header-block-summarize descriptor) "\n")))
1250 (forward-line -1) (move-to-column col))
1252 (cl-assert (tar-data-swapped-p))
1253 (with-current-buffer tar-data-buffer
1254 (let* ((start (- (tar-header-data-start descriptor) 512)))
1256 ;; delete the old field and insert a new one.
1257 (goto-char (+ start data-position))
1258 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1259 (cl-assert (not (or enable-multibyte-characters
1260 (multibyte-string-p new-data-string))))
1261 (insert new-data-string)
1263 ;; compute a new checksum and insert it.
1264 (let ((chk (tar-header-block-checksum
1265 (buffer-substring start (+ start 512)))))
1266 (goto-char (+ start tar-chk-offset))
1267 (delete-region (point) (+ (point) 8))
1268 (insert (format "%6o\0 " chk))
1269 (setf (tar-header-checksum descriptor) chk)
1271 ;; ok, make sure we didn't botch it.
1272 (tar-header-block-check-checksum
1273 (buffer-substring start (+ start 512))
1274 chk (tar-header-name descriptor))
1278 (defun tar-octal-time (timeval)
1279 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1280 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1281 (format "%05o%01o%05o"
1283 (logior (ash (logand 3 hibits) 1)
1284 (if (> (logand lobits 32768) 0) 1 0))
1285 (logand 32767 lobits)
1288 (defun tar-subfile-save-buffer ()
1289 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1290 This doesn't write anything to disk; you must save the parent tar-file buffer
1291 to make your changes permanent."
1293 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1294 (error "This buffer has no superior tar file buffer"))
1295 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1296 (error "This buffer doesn't have an index into its superior tar file!"))
1297 (let ((subfile (current-buffer))
1298 (coding buffer-file-coding-system)
1299 (descriptor tar-superior-descriptor)
1301 (with-current-buffer tar-superior-buffer
1302 (let* ((start (tar-header-data-start descriptor))
1303 (size (tar-header-size descriptor))
1304 (head (memq descriptor tar-parse-info)))
1306 (error "Can't find this tar file entry in its parent tar file!"))
1307 (with-current-buffer tar-data-buffer
1308 ;; delete the old data...
1309 (let* ((data-start start)
1310 (data-end (+ data-start (tar-roundup-512 size))))
1311 (narrow-to-region data-start data-end)
1312 (delete-region (point-min) (point-max))
1313 ;; insert the new data...
1314 (goto-char data-start)
1315 (let ((dest (current-buffer)))
1316 (with-current-buffer subfile
1319 (encode-coding-region (point-min) (point-max) coding dest))))
1320 (setq subfile-size (- (point-max) (point-min)))
1322 ;; pad the new data out to a multiple of 512...
1323 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1324 (goto-char (point-max))
1325 (insert (make-string (- subfile-size-pad subfile-size) 0))
1327 ;; update the data of this files...
1328 (setf (tar-header-size descriptor) subfile-size)
1330 ;; Update the size field in the header block.
1333 ;; alter the descriptor-line and header
1335 (let ((position (- (length tar-parse-info) (length head))))
1336 (goto-char (point-min))
1337 (forward-line position)
1338 (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
1340 ;; Maybe update the datestamp.
1341 (when tar-update-datestamp
1342 (tar-alter-one-field tar-time-offset
1343 (concat (tar-octal-time (current-time)) " "))))
1344 ;; After doing the insertion, add any necessary final padding.
1345 (tar-pad-to-blocksize))
1346 (set-buffer-modified-p t) ; mark the tar file as modified
1348 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1349 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1350 (buffer-name tar-superior-buffer))
1351 ;; Prevent basic-save-buffer from changing our coding-system.
1352 (setq last-coding-system-used buffer-file-coding-system)
1353 ;; Prevent ordinary saving from happening.
1357 ;; When this function is called, it is sure that the buffer is unibyte.
1358 (defun tar-pad-to-blocksize ()
1359 "If we are being anal about tar file blocksizes, fix up the current buffer.
1360 Leaves the region wide."
1361 (if (null tar-anal-blocksize)
1363 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1364 (start (tar-header-data-start last-desc))
1365 (link-p (tar-header-link-type last-desc))
1366 (size (if link-p 0 (tar-header-size last-desc)))
1367 (data-end (+ start size))
1368 (bbytes (ash tar-anal-blocksize 9))
1369 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
1370 ;; If the padding after the last data is too long, delete some;
1371 ;; else insert some until we are padded out to the right number of blocks.
1373 (with-current-buffer tar-data-buffer
1374 (let ((goal-end (+ (point-min) pad-to)))
1375 (if (> (point-max) goal-end)
1376 (delete-region goal-end (point-max))
1377 (goto-char (point-max))
1378 (insert (make-string (- goal-end (point-max)) ?\0))))))))
1381 ;; Used in write-region-annotate-functions to write tar-files out correctly.
1382 (defun tar-write-region-annotate (start _end)
1383 ;; When called from write-file (and auto-save), `start' is nil.
1384 ;; When called from M-x write-region, we assume the user wants to save
1385 ;; (part of) the summary, not the tar data.
1386 (unless (or start (not (tar-data-swapped-p)))
1387 (tar-clear-modification-flags)
1388 (set-buffer tar-data-buffer)
1393 ;;; tar-mode.el ends here