Fix documentation of forward-line
[emacs.git] / lisp / tar-mode.el
blob6c7f7553f82587c9eba1e94d7c4b250cb329bad9
1 ;;; tar-mode.el --- simple editing of tar files from GNU Emacs
3 ;; Copyright (C) 1990-1991, 1993-2015 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 04 Apr 1990
8 ;; Keywords: unix
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 <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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,
41 ;; which you get with
43 ;; (autoload 'uncompress-while-visiting "uncompress")
44 ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
45 ;; auto-mode-alist))
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
71 ;; the list.
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.
76 ;; Rationale:
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.
90 ;;; Bugs:
92 ;; - Rename on ././@LongLink files
93 ;; - Revert confirmation displays the raw data temporarily.
95 ;;; Code:
97 (eval-when-compile (require 'cl-lib))
99 (defgroup tar nil
100 "Simple editing of tar files."
101 :prefix "tar-"
102 :group 'data)
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))
113 :group 'tar)
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."
123 :type 'boolean
124 :group 'tar)
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."
129 :type 'boolean
130 :group 'tar)
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
151 ;; the two.
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
168 ;; the summary.
169 (progn
170 (cl-assert (or (= (buffer-size tar-data-buffer) (buffer-size))
171 (eq tar-data-swapped
172 (> (buffer-size tar-data-buffer) (buffer-size)))))
173 tar-data-swapped)))
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
189 (:constructor nil)
190 (:type vector)
191 :named
192 (:constructor
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).
199 header-start)
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"))))
247 name linkname
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))
260 (- 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
266 (1- (match-end 0)))
267 "/" name)))
268 (if (default-value 'enable-multibyte-characters)
269 (setq name
270 (decode-coding-string name coding)
271 linkname
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))
289 coding)))
290 (cond
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))
299 descriptor)
301 (make-tar-header
302 (copy-marker pos nil)
303 name
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)
310 link-p
311 linkname
312 uname-valid-p
313 (when uname-valid-p
314 (decode-coding-string
315 (substring string tar-uname-offset uname-end) coding))
316 (when uname-valid-p
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)
321 ))))))
323 ;; Pseudo-field.
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)))
328 (+ data-start
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)
332 0))))
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)
339 (let ((n 0))
340 (while (< start end)
341 (setq n (if (< (aref string start) ?0) n
342 (+ (* n 8) (- (aref string start) ?0)))
343 start (1+ start)))
344 n)))
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)
350 (list 0 0)
351 (let ((lo 0)
352 (hi 0))
353 (while (< start end)
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)))
359 (list hi lo))))
361 (defun tar-parse-octal-integer-safe (string)
362 (if (zerop (length string)) (error "empty string"))
363 (mapc (lambda (c)
364 (if (or (< c ?0) (> c ?7))
365 (error "`%c' is not an octal digit" c)))
366 string)
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."
379 (make-tar-header
381 filename
382 #o644 0 0 (or size 0)
383 (or time (current-time))
384 nil ; checksum
385 nil nil
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)
392 (when val
393 (insert (tar--pad-to pos)
394 (if fmt
395 (format fmt (if mask (logand mask val) val))
396 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
401 checksum is correct.
403 If UPDATE-CHECKSUM is non-nil, update HEADER with the newly-computed
404 checksum before doing the check."
405 (with-temp-buffer
406 (set-buffer-multibyte nil)
407 (let ((encoded-name
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))
420 " ")
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))
430 (tar--put-at 512 "")
431 (let ((ck (tar-header-block-checksum (buffer-string))))
432 (goto-char (+ (point-min) tar-chk-offset))
433 (delete-char 8)
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)))
440 ;; .
441 (buffer-string)))
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))
449 (sum 0)
450 (i 0))
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))
455 i (1+ i)))
456 (setq i chk-field-end)
457 (while (< i 512)
458 (setq sum (+ sum (aref string i))
459 i (1+ i)))
460 (+ sum (* 32 8))))
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."
474 (string
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"
505 (if mod-p ?* ? )
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
520 (t ?\s)
522 (tar-grind-file-mode mode)
523 (if (= 0 (length uname)) uid uname)
524 (if (= 0 (length gname)) gid gname)
525 size
526 (if tar-mode-show-date (tar-clip-time-string time) "")
527 (propertize name
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)
532 ""))))
534 (defun tar-untar-buffer ()
535 "Extract all archive members in the tar-file into the current directory."
536 (interactive)
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.
539 (with-current-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)
545 name
546 (file-name-directory name)))
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))
555 (write-region start end name)))
556 (set-file-modes name (tar-header-mode descriptor))))))))
558 (defun tar-summarize-buffer ()
559 "Parse the contents of the tar file in the current buffer."
560 (cl-assert (tar-data-swapped-p))
561 (let* ((modified (buffer-modified-p))
562 (result '())
563 (pos (point-min))
564 (coding tar-file-name-coding-system)
565 (progress-reporter
566 (with-current-buffer tar-data-buffer
567 (make-progress-reporter "Parsing tar file..."
568 (point-min) (point-max))))
569 descriptor)
570 (with-current-buffer tar-data-buffer
571 (while (and (< pos (point-max))
572 (setq descriptor (tar-header-block-tokenize pos coding)))
573 (let ((size (tar-header-size descriptor)))
574 (if (< size 0)
575 (error "%s has size %s - corrupted"
576 (tar-header-name descriptor) size)))
578 ;; This is just too slow. Don't really need it anyway....
579 ;;(tar-header-block-check-checksum
580 ;; hblock (tar-header-block-checksum hblock)
581 ;; (tar-header-name descriptor))
583 (push descriptor result)
584 (setq pos (tar-header-data-end descriptor))
585 (progress-reporter-update progress-reporter pos)))
587 (set (make-local-variable 'tar-parse-info) (nreverse result))
588 ;; A tar file should end with a block or two of nulls,
589 ;; but let's not get a fatal error if it doesn't.
590 (if (null descriptor)
591 (progress-reporter-done progress-reporter)
592 (message "Warning: premature EOF parsing tar file"))
593 (goto-char (point-min))
594 (let ((buffer-file-truename nil) ; avoid changing dir mtime by lock_file
595 (inhibit-read-only t)
596 (total-summaries
597 (mapconcat 'tar-header-block-summarize tar-parse-info "\n")))
598 (insert total-summaries "\n")
599 (goto-char (point-min))
600 (restore-buffer-modified-p modified))))
602 (defvar tar-mode-map
603 (let ((map (make-keymap)))
604 (suppress-keymap map)
605 (define-key map " " 'tar-next-line)
606 (define-key map "C" 'tar-copy)
607 (define-key map "d" 'tar-flag-deleted)
608 (define-key map "\^D" 'tar-flag-deleted)
609 (define-key map "e" 'tar-extract)
610 (define-key map "f" 'tar-extract)
611 (define-key map "\C-m" 'tar-extract)
612 (define-key map [mouse-2] 'tar-mouse-extract)
613 (define-key map "g" 'revert-buffer)
614 (define-key map "n" 'tar-next-line)
615 (define-key map "\^N" 'tar-next-line)
616 (define-key map [down] 'tar-next-line)
617 (define-key map "o" 'tar-extract-other-window)
618 (define-key map "p" 'tar-previous-line)
619 (define-key map "\^P" 'tar-previous-line)
620 (define-key map [up] 'tar-previous-line)
621 (define-key map "I" 'tar-new-entry)
622 (define-key map "R" 'tar-rename-entry)
623 (define-key map "u" 'tar-unflag)
624 (define-key map "v" 'tar-view)
625 (define-key map "w" 'woman-tar-extract-file)
626 (define-key map "x" 'tar-expunge)
627 (define-key map "\177" 'tar-unflag-backwards)
628 (define-key map "E" 'tar-extract-other-window)
629 (define-key map "M" 'tar-chmod-entry)
630 (define-key map "G" 'tar-chgrp-entry)
631 (define-key map "O" 'tar-chown-entry)
632 ;; Let mouse-1 follow the link.
633 (define-key map [follow-link] 'mouse-face)
635 ;; Make menu bar items.
637 ;; Get rid of the Edit menu bar item to save space.
638 (define-key map [menu-bar edit] 'undefined)
640 (define-key map [menu-bar immediate]
641 (cons "Immediate" (make-sparse-keymap "Immediate")))
643 (define-key map [menu-bar immediate woman]
644 '("Read Man Page (WoMan)" . woman-tar-extract-file))
645 (define-key map [menu-bar immediate view]
646 '("View This File" . tar-view))
647 (define-key map [menu-bar immediate display]
648 '("Display in Other Window" . tar-display-other-window))
649 (define-key map [menu-bar immediate find-file-other-window]
650 '("Find in Other Window" . tar-extract-other-window))
651 (define-key map [menu-bar immediate find-file]
652 '("Find This File" . tar-extract))
654 (define-key map [menu-bar mark]
655 (cons "Mark" (make-sparse-keymap "Mark")))
657 (define-key map [menu-bar mark unmark-all]
658 '("Unmark All" . tar-clear-modification-flags))
659 (define-key map [menu-bar mark deletion]
660 '("Flag" . tar-flag-deleted))
661 (define-key map [menu-bar mark unmark]
662 '("Unflag" . tar-unflag))
664 (define-key map [menu-bar operate]
665 (cons "Operate" (make-sparse-keymap "Operate")))
667 (define-key map [menu-bar operate chown]
668 '("Change Owner..." . tar-chown-entry))
669 (define-key map [menu-bar operate chgrp]
670 '("Change Group..." . tar-chgrp-entry))
671 (define-key map [menu-bar operate chmod]
672 '("Change Mode..." . tar-chmod-entry))
673 (define-key map [menu-bar operate rename]
674 '("Rename to..." . tar-rename-entry))
675 (define-key map [menu-bar operate copy]
676 '("Copy to..." . tar-copy))
677 (define-key map [menu-bar operate expunge]
678 '("Expunge Marked Files" . tar-expunge))
680 map)
681 "Local keymap for Tar mode listings.")
684 ;; tar mode is suitable only for specially formatted data.
685 (put 'tar-mode 'mode-class 'special)
686 (put 'tar-subfile-mode 'mode-class 'special)
688 (defun tar-change-major-mode-hook ()
689 ;; Bring the actual Tar data back into the main buffer.
690 (when (tar-data-swapped-p) (tar-swap-data))
691 ;; Throw away the summary.
692 (when (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
694 (defun tar-mode-kill-buffer-hook ()
695 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
697 ;;;###autoload
698 (define-derived-mode tar-mode special-mode "Tar"
699 "Major mode for viewing a tar file as a dired-like listing of its contents.
700 You can move around using the usual cursor motion commands.
701 Letters no longer insert themselves.
702 Type `e' to pull a file out of the tar file and into its own buffer;
703 or click mouse-2 on the file's line in the Tar mode buffer.
704 Type `c' to copy an entry from the tar file into another file on disk.
706 If you edit a sub-file of this archive (as with the `e' command) and
707 save it with \\[save-buffer], the contents of that buffer will be
708 saved back into the tar-file buffer; in this way you can edit a file
709 inside of a tar archive without extracting it and re-archiving it.
711 See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
712 \\{tar-mode-map}"
713 (and buffer-file-name
714 (file-writable-p buffer-file-name)
715 (setq buffer-read-only nil)) ; undo what `special-mode' did
716 (make-local-variable 'tar-parse-info)
717 (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
718 (set (make-local-variable 'local-enable-local-variables) nil)
719 (set (make-local-variable 'next-line-add-newlines) nil)
720 (set (make-local-variable 'tar-file-name-coding-system)
721 (or file-name-coding-system
722 default-file-name-coding-system
723 locale-coding-system))
724 ;; Prevent loss of data when saving the file.
725 (set (make-local-variable 'file-precious-flag) t)
726 (buffer-disable-undo)
727 (widen)
728 ;; Now move the Tar data into an auxiliary buffer, so we can use the main
729 ;; buffer for the summary.
730 (cl-assert (not (tar-data-swapped-p)))
731 (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
732 ;; We started using write-contents-functions, but this hook is not
733 ;; used during auto-save, so we now use
734 ;; write-region-annotate-functions which hooks at a lower-level.
735 (add-hook 'write-region-annotate-functions 'tar-write-region-annotate nil t)
736 (add-hook 'kill-buffer-hook 'tar-mode-kill-buffer-hook nil t)
737 (add-hook 'change-major-mode-hook 'tar-change-major-mode-hook nil t)
738 ;; Tar data is made of bytes, not chars.
739 (set-buffer-multibyte nil) ;Hopefully a no-op.
740 (set (make-local-variable 'tar-data-buffer)
741 (generate-new-buffer (format " *tar-data %s*"
742 (file-name-nondirectory
743 (or buffer-file-name (buffer-name))))))
744 (condition-case err
745 (progn
746 (tar-swap-data)
747 (tar-summarize-buffer)
748 (tar-next-line 0))
749 (error
750 ;; If summarizing caused an error, then maybe the buffer doesn't contain
751 ;; tar data. Rather than show a mysterious empty buffer, let's
752 ;; revert to fundamental-mode.
753 (fundamental-mode)
754 (signal (car err) (cdr err)))))
756 (autoload 'woman-tar-extract-file "woman"
757 "In tar mode, run the WoMan man-page browser on this file." t)
759 (define-minor-mode tar-subfile-mode
760 "Minor mode for editing an element of a tar-file.
761 With a prefix argument ARG, enable the mode if ARG is positive,
762 and disable it otherwise. If called from Lisp, enable the mode
763 if ARG is omitted or nil. This mode arranges for \"saving\" this
764 buffer to write the data into the tar-file buffer that it came
765 from. The changes will actually appear on disk when you save the
766 tar-file's buffer."
767 ;; Don't do this, because it is redundant and wastes mode line space.
768 ;; :lighter " TarFile"
769 nil nil nil
770 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
771 (error "This buffer is not an element of a tar file"))
772 (cond (tar-subfile-mode
773 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
774 ;; turn off auto-save.
775 (auto-save-mode -1)
776 (setq buffer-auto-save-file-name nil))
778 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
781 ;; Revert the buffer and recompute the dired-like listing.
782 (defun tar-mode-revert (&optional no-auto-save no-confirm)
783 (unwind-protect
784 (let ((revert-buffer-function nil))
785 (if (tar-data-swapped-p) (tar-swap-data))
786 ;; FIXME: If we ask for confirmation, the user will be temporarily
787 ;; looking at the raw data.
788 (revert-buffer no-auto-save no-confirm 'preserve-modes)
789 ;; Recompute the summary.
790 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
791 (tar-mode))
792 (unless (tar-data-swapped-p) (tar-swap-data))))
795 (defun tar-next-line (arg)
796 "Move cursor vertically down ARG lines and to the start of the filename."
797 (interactive "p")
798 (forward-line arg)
799 (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
801 (defun tar-previous-line (arg)
802 "Move cursor vertically up ARG lines and to the start of the filename."
803 (interactive "p")
804 (tar-next-line (- arg)))
806 (defun tar-current-position ()
807 "Return the `tar-parse-info' index for the current line."
808 (count-lines (point-min) (line-beginning-position)))
810 (defun tar-current-descriptor (&optional noerror)
811 "Return the tar-descriptor of the current line, or signals an error."
812 ;; I wish lines had plists, like in ZMACS...
813 (or (nth (tar-current-position)
814 tar-parse-info)
815 (if noerror
817 (error "This line does not describe a tar-file entry"))))
819 (defun tar--check-descriptor (descriptor)
820 (let ((link-p (tar-header-link-type descriptor)))
821 (if link-p
822 (error "This is %s, not a real file"
823 (cond ((eq link-p 5) "a directory")
824 ((eq link-p 20) "a tar directory header")
825 ((eq link-p 28) "a next has longname")
826 ((eq link-p 29) "a multivolume-continuation")
827 ((eq link-p 35) "a sparse entry")
828 ((eq link-p 38) "a volume header")
829 ((eq link-p 55) "a pax global extended header")
830 ((eq link-p 72) "a pax extended header")
831 (t "a link"))))))
833 (defun tar-get-descriptor ()
834 (let* ((descriptor (tar-current-descriptor))
835 (size (tar-header-size descriptor)))
836 (tar--check-descriptor descriptor)
837 (if (zerop size) (message "This is a zero-length file"))
838 descriptor))
840 (defun tar-get-file-descriptor (file)
841 ;; Used by package.el.
842 (let ((desc ()))
843 (dolist (hdr tar-parse-info)
844 (when (equal file (tar-header-name hdr))
845 (setq desc hdr)))
846 (tar--check-descriptor desc)
847 desc))
849 (defun tar-mouse-extract (event)
850 "Extract a file whose tar directory line you click on."
851 (interactive "e")
852 (with-current-buffer (window-buffer (posn-window (event-end event)))
853 (save-excursion
854 (goto-char (posn-point (event-end event)))
855 ;; Just make sure this doesn't get an error.
856 (tar-get-descriptor)))
857 (select-window (posn-window (event-end event)))
858 (goto-char (posn-point (event-end event)))
859 (tar-extract))
861 (defun tar-file-name-handler (op &rest args)
862 "Helper function for `tar-extract'."
863 (or (eq op 'file-exists-p)
864 (let ((file-name-handler-alist nil))
865 (apply op args))))
867 (defun tar--extract (descriptor)
868 "Extract this entry of the tar file into its own buffer."
869 (let* ((name (tar-header-name descriptor))
870 (size (tar-header-size descriptor))
871 (start (tar-header-data-start descriptor))
872 (end (+ start size))
873 (tarname (buffer-name))
874 (bufname (concat (file-name-nondirectory name)
875 " ("
876 tarname
877 ")"))
878 (buffer (generate-new-buffer bufname)))
879 (with-current-buffer tar-data-buffer
880 (let (coding)
881 (narrow-to-region start end)
882 (goto-char start)
883 (setq coding (or coding-system-for-read
884 (and set-auto-coding-function
885 (funcall set-auto-coding-function
886 name (- end start)))
887 ;; The following binding causes
888 ;; find-buffer-file-type-coding-system
889 ;; (defined on dos-w32.el) to act as if
890 ;; the file being extracted existed, so
891 ;; that the file's contents' encoding and
892 ;; EOL format are auto-detected.
893 (let ((file-name-handler-alist
894 '(("" . tar-file-name-handler))))
895 (car (find-operation-coding-system
896 'insert-file-contents
897 (cons name (current-buffer)) t)))))
898 (if (or (not coding)
899 (eq (coding-system-type coding) 'undecided))
900 (setq coding (detect-coding-region start end t)))
901 (if (and (default-value 'enable-multibyte-characters)
902 (coding-system-get coding :for-unibyte))
903 (with-current-buffer buffer
904 (set-buffer-multibyte nil)))
905 (widen)
906 (with-current-buffer buffer
907 (setq buffer-undo-list t))
908 (decode-coding-region start end coding buffer)
909 (with-current-buffer buffer
910 (setq buffer-undo-list nil))))
911 buffer))
913 (defun tar-extract (&optional other-window-p)
914 "In Tar mode, extract this entry of the tar file into its own buffer."
915 (interactive)
916 (let* ((view-p (eq other-window-p 'view))
917 (descriptor (tar-get-descriptor))
918 (name (tar-header-name descriptor))
919 (tar-buffer (current-buffer))
920 (tarname (buffer-name))
921 (read-only-p (or buffer-read-only view-p))
922 (new-buffer-file-name (expand-file-name
923 ;; `:' is not allowed on Windows
924 (concat tarname "!"
925 (if (string-match "/" name)
926 name
927 ;; Make sure `name' contains a /
928 ;; so set-auto-mode doesn't try
929 ;; to look at `tarname' for hints.
930 (concat "./" name)))))
931 (buffer (get-file-buffer new-buffer-file-name))
932 (just-created nil))
933 (unless buffer
934 (setq buffer (tar--extract descriptor))
935 (setq just-created t)
936 (with-current-buffer buffer
937 (goto-char (point-min))
938 (setq buffer-file-name new-buffer-file-name)
939 (setq buffer-file-truename
940 (abbreviate-file-name buffer-file-name))
941 ;; Force buffer-file-coding-system to what
942 ;; decode-coding-region actually used.
943 (set-buffer-file-coding-system last-coding-system-used t)
944 ;; Set the default-directory to the dir of the
945 ;; superior buffer.
946 (setq default-directory
947 (with-current-buffer tar-buffer
948 default-directory))
949 (set-buffer-modified-p nil)
950 (normal-mode) ; pick a mode.
951 (set (make-local-variable 'tar-superior-buffer) tar-buffer)
952 (set (make-local-variable 'tar-superior-descriptor) descriptor)
953 (setq buffer-read-only read-only-p)
954 (tar-subfile-mode 1)))
955 (cond
956 (view-p
957 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
958 ((eq other-window-p 'display) (display-buffer buffer))
959 (other-window-p (switch-to-buffer-other-window buffer))
960 (t (switch-to-buffer buffer)))))
963 (defun tar-extract-other-window ()
964 "In Tar mode, find this entry of the tar file in another window."
965 (interactive)
966 (tar-extract t))
968 (defun tar-display-other-window ()
969 "In Tar mode, display this entry of the tar file in another window."
970 (interactive)
971 (tar-extract 'display))
973 (defun tar-view ()
974 "In Tar mode, view the tar file entry on this line."
975 (interactive)
976 (tar-extract 'view))
979 (defun tar-read-file-name (&optional prompt)
980 "Read a file name with this line's entry as the default."
981 (or prompt (setq prompt "Copy to: "))
982 (let* ((default-file (expand-file-name
983 (tar-header-name (tar-current-descriptor))))
984 (target (expand-file-name
985 (read-file-name prompt
986 (file-name-directory default-file)
987 default-file nil))))
988 (if (or (string= "" (file-name-nondirectory target))
989 (file-directory-p target))
990 (setq target (concat (if (string-match "/$" target)
991 (substring target 0 (1- (match-end 0)))
992 target)
994 (file-name-nondirectory default-file))))
995 target))
998 (defun tar-copy (&optional to-file)
999 "In Tar mode, extract this entry of the tar file into a file on disk.
1000 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
1001 the current tar-entry."
1002 (interactive (list (tar-read-file-name)))
1003 (let* ((descriptor (tar-get-descriptor))
1004 (name (tar-header-name descriptor))
1005 (size (tar-header-size descriptor))
1006 (start (tar-header-data-start descriptor))
1007 (end (+ start size))
1008 (inhibit-file-name-handlers inhibit-file-name-handlers)
1009 (inhibit-file-name-operation inhibit-file-name-operation))
1010 (with-current-buffer
1011 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
1012 ;; Inhibit compressing a subfile again if *both* name and
1013 ;; to-file are handled by jka-compr
1014 (if (and (eq (find-file-name-handler name 'write-region)
1015 'jka-compr-handler)
1016 (eq (find-file-name-handler to-file 'write-region)
1017 'jka-compr-handler))
1018 (setq inhibit-file-name-handlers
1019 (cons 'jka-compr-handler
1020 (and (eq inhibit-file-name-operation 'write-region)
1021 inhibit-file-name-handlers))
1022 inhibit-file-name-operation 'write-region))
1023 (let ((coding-system-for-write 'no-conversion))
1024 (write-region start end to-file nil nil nil t)))
1025 (message "Copied tar entry %s to %s" name to-file)))
1027 (defun tar-new-entry (filename &optional index)
1028 "Insert a new empty regular file before point."
1029 (interactive "*sFile name: ")
1030 (let* ((buffer (current-buffer))
1031 (index (or index (tar-current-position)))
1032 (d-list (and (not (zerop index))
1033 (nthcdr (+ -1 index) tar-parse-info)))
1034 (pos (if d-list
1035 (tar-header-data-end (car d-list))
1036 (point-min)))
1037 (new-descriptor
1038 (tar-new-regular-file-header filename)))
1039 ;; Update the data buffer; fill the missing descriptor fields.
1040 (with-current-buffer tar-data-buffer
1041 (goto-char pos)
1042 (insert (tar-header-serialize new-descriptor t))
1043 (setf (tar-header-data-start new-descriptor)
1044 (copy-marker (point) nil)))
1045 ;; Update tar-parse-info.
1046 (if d-list
1047 (setcdr d-list (cons new-descriptor (cdr d-list)))
1048 (setq tar-parse-info (cons new-descriptor tar-parse-info)))
1049 ;; Update the listing buffer.
1050 (save-excursion
1051 (goto-char (point-min))
1052 (forward-line index)
1053 (let ((inhibit-read-only t))
1054 (insert (tar-header-block-summarize new-descriptor) ?\n)))
1055 ;; .
1056 index))
1058 (defun tar-flag-deleted (p &optional unflag)
1059 "In Tar mode, mark this sub-file to be deleted from the tar file.
1060 With a prefix argument, mark that many files."
1061 (interactive "p")
1062 (beginning-of-line)
1063 (dotimes (i (abs p))
1064 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
1065 (progn
1066 (delete-char 1)
1067 (insert (if unflag " " "D"))))
1068 (forward-line (if (< p 0) -1 1)))
1069 (if (eobp) nil (forward-char 36)))
1071 (defun tar-unflag (p)
1072 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1073 With a prefix argument, un-mark that many files forward."
1074 (interactive "p")
1075 (tar-flag-deleted p t))
1077 (defun tar-unflag-backwards (p)
1078 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1079 With a prefix argument, un-mark that many files backward."
1080 (interactive "p")
1081 (tar-flag-deleted (- p) t))
1084 (defun tar-expunge-internal ()
1085 "Expunge the tar-entry specified by the current line."
1086 (let ((descriptor (tar-current-descriptor)))
1088 ;; delete the current line...
1089 (delete-region (line-beginning-position) (line-beginning-position 2))
1091 ;; delete the data pointer...
1092 (setq tar-parse-info (delq descriptor tar-parse-info))
1094 ;; delete the data from inside the file...
1095 (with-current-buffer tar-data-buffer
1096 (delete-region (or (tar-header-header-start descriptor)
1097 (- (tar-header-data-start descriptor) 512))
1098 (tar-header-data-end descriptor)))))
1101 (defun tar-expunge (&optional noconfirm)
1102 "In Tar mode, delete all the archived files flagged for deletion.
1103 This does not modify the disk image; you must save the tar file itself
1104 for this to be permanent."
1105 (interactive)
1106 (if (or noconfirm
1107 (y-or-n-p "Expunge files marked for deletion? "))
1108 (let ((n 0))
1109 (save-excursion
1110 (goto-char (point-min))
1111 (while (not (eobp))
1112 (if (looking-at "D")
1113 (progn (tar-expunge-internal)
1114 (setq n (1+ n)))
1115 (forward-line 1)))
1116 ;; after doing the deletions, add any padding that may be necessary.
1117 (tar-pad-to-blocksize))
1118 (if (zerop n)
1119 (message "Nothing to expunge.")
1120 (message "%s files expunged. Be sure to save this buffer." n)))))
1123 (defun tar-clear-modification-flags ()
1124 "Remove the stars at the beginning of each line."
1125 (interactive)
1126 (save-excursion
1127 (goto-char (point-min))
1128 (while (not (eobp))
1129 (if (not (eq (following-char) ?\s))
1130 (progn (delete-char 1) (insert " ")))
1131 (forward-line 1))))
1134 (defun tar-chown-entry (new-uid)
1135 "Change the user-id associated with this entry in the tar file.
1136 If this tar file was written by GNU tar, then you will be able to edit
1137 the user id as a string; otherwise, you must edit it as a number.
1138 You can force editing as a number by calling this with a prefix arg.
1139 This does not modify the disk image; you must save the tar file itself
1140 for this to be permanent."
1141 (interactive
1142 (list
1143 (let ((descriptor (tar-current-descriptor)))
1144 (if (or current-prefix-arg
1145 (not (tar-header-magic descriptor)))
1146 (read-number
1147 "New UID number: "
1148 (format "%s" (tar-header-uid descriptor)))
1149 (read-string "New UID string: " (tar-header-uname descriptor))))))
1150 (cond ((stringp new-uid)
1151 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
1152 (tar-alter-one-field tar-uname-offset
1153 (concat (encode-coding-string
1154 new-uid tar-file-name-coding-system)
1155 "\000")))
1157 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
1158 (tar-alter-one-field tar-uid-offset
1159 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1162 (defun tar-chgrp-entry (new-gid)
1163 "Change the group-id associated with this entry in the tar file.
1164 If this tar file was written by GNU tar, then you will be able to edit
1165 the group id as a string; otherwise, you must edit it as a number.
1166 You can force editing as a number by calling this with a prefix arg.
1167 This does not modify the disk image; you must save the tar file itself
1168 for this to be permanent."
1169 (interactive
1170 (list
1171 (let ((descriptor (tar-current-descriptor)))
1172 (if (or current-prefix-arg
1173 (not (tar-header-magic descriptor)))
1174 (read-number
1175 "New GID number: "
1176 (format "%s" (tar-header-gid descriptor)))
1177 (read-string "New GID string: " (tar-header-gname descriptor))))))
1178 (cond ((stringp new-gid)
1179 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1180 (tar-alter-one-field tar-gname-offset
1181 (concat (encode-coding-string
1182 new-gid tar-file-name-coding-system)
1183 "\000")))
1185 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1186 (tar-alter-one-field tar-gid-offset
1187 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1189 (defun tar-rename-entry (new-name)
1190 "Change the name associated with this entry in the tar file.
1191 This does not modify the disk image; you must save the tar file itself
1192 for this to be permanent."
1193 (interactive
1194 (list (read-string "New name: "
1195 (tar-header-name (tar-current-descriptor)))))
1196 (if (string= "" new-name) (error "zero length name"))
1197 (let ((encoded-new-name (encode-coding-string new-name
1198 tar-file-name-coding-system))
1199 (descriptor (tar-current-descriptor))
1200 (prefix nil))
1201 (when (tar-header-header-start descriptor)
1202 ;; FIXME: Make it work for ././@LongLink.
1203 (error "Rename with @LongLink format is not implemented"))
1205 (when (and (> (length encoded-new-name) 98)
1206 (string-match "/" encoded-new-name
1207 (- (length encoded-new-name) 99))
1208 (< (match-beginning 0) 155))
1209 (unless (equal (tar-header-magic descriptor) "ustar\0")
1210 (tar-alter-one-field tar-magic-offset (concat "ustar\0" "00")))
1211 (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
1212 (setq encoded-new-name (substring encoded-new-name (match-end 0))))
1214 (if (> (length encoded-new-name) 98) (error "name too long"))
1215 (setf (tar-header-name descriptor) new-name)
1216 (tar-alter-one-field 0
1217 (substring (concat encoded-new-name (make-string 99 0)) 0 99))
1218 (if prefix
1219 (tar-alter-one-field tar-prefix-offset
1220 (substring (concat prefix (make-string 155 0)) 0 155)))))
1223 (defun tar-chmod-entry (new-mode)
1224 "Change the protection bits associated with this entry in the tar file.
1225 This does not modify the disk image; you must save the tar file itself
1226 for this to be permanent."
1227 (interactive (list (tar-parse-octal-integer-safe
1228 (read-string "New protection (octal): "))))
1229 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1230 (tar-alter-one-field tar-mode-offset
1231 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1234 (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
1235 (unless descriptor (setq descriptor (tar-current-descriptor)))
1237 ;; update the header-line.
1238 (let ((col (current-column)))
1239 (delete-region (line-beginning-position)
1240 (prog2 (forward-line 1)
1241 (point)
1242 ;; Insert the new text after the old, before deleting,
1243 ;; to preserve markers such as the window start.
1244 (insert (tar-header-block-summarize descriptor) "\n")))
1245 (forward-line -1) (move-to-column col))
1247 (cl-assert (tar-data-swapped-p))
1248 (with-current-buffer tar-data-buffer
1249 (let* ((start (- (tar-header-data-start descriptor) 512)))
1251 ;; delete the old field and insert a new one.
1252 (goto-char (+ start data-position))
1253 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1254 (cl-assert (not (or enable-multibyte-characters
1255 (multibyte-string-p new-data-string))))
1256 (insert new-data-string)
1258 ;; compute a new checksum and insert it.
1259 (let ((chk (tar-header-block-checksum
1260 (buffer-substring start (+ start 512)))))
1261 (goto-char (+ start tar-chk-offset))
1262 (delete-region (point) (+ (point) 8))
1263 (insert (format "%6o\0 " chk))
1264 (setf (tar-header-checksum descriptor) chk)
1266 ;; ok, make sure we didn't botch it.
1267 (tar-header-block-check-checksum
1268 (buffer-substring start (+ start 512))
1269 chk (tar-header-name descriptor))
1270 ))))
1273 (defun tar-octal-time (timeval)
1274 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1275 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1276 (format "%05o%01o%05o"
1277 (lsh hibits -2)
1278 (logior (lsh (logand 3 hibits) 1)
1279 (if (> (logand lobits 32768) 0) 1 0))
1280 (logand 32767 lobits)
1283 (defun tar-subfile-save-buffer ()
1284 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1285 This doesn't write anything to disk; you must save the parent tar-file buffer
1286 to make your changes permanent."
1287 (interactive)
1288 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1289 (error "This buffer has no superior tar file buffer"))
1290 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1291 (error "This buffer doesn't have an index into its superior tar file!"))
1292 (let ((subfile (current-buffer))
1293 (coding buffer-file-coding-system)
1294 (descriptor tar-superior-descriptor)
1295 subfile-size)
1296 (with-current-buffer tar-superior-buffer
1297 (let* ((start (tar-header-data-start descriptor))
1298 (size (tar-header-size descriptor))
1299 (head (memq descriptor tar-parse-info)))
1300 (if (not head)
1301 (error "Can't find this tar file entry in its parent tar file!"))
1302 (with-current-buffer tar-data-buffer
1303 ;; delete the old data...
1304 (let* ((data-start start)
1305 (data-end (+ data-start (tar-roundup-512 size))))
1306 (narrow-to-region data-start data-end)
1307 (delete-region (point-min) (point-max))
1308 ;; insert the new data...
1309 (goto-char data-start)
1310 (let ((dest (current-buffer)))
1311 (with-current-buffer subfile
1312 (save-restriction
1313 (widen)
1314 (encode-coding-region (point-min) (point-max) coding dest))))
1315 (setq subfile-size (- (point-max) (point-min)))
1317 ;; pad the new data out to a multiple of 512...
1318 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1319 (goto-char (point-max))
1320 (insert (make-string (- subfile-size-pad subfile-size) 0))
1322 ;; update the data of this files...
1323 (setf (tar-header-size descriptor) subfile-size)
1325 ;; Update the size field in the header block.
1326 (widen))))
1328 ;; alter the descriptor-line and header
1330 (let ((position (- (length tar-parse-info) (length head))))
1331 (goto-char (point-min))
1332 (forward-line position)
1333 (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
1335 ;; Maybe update the datestamp.
1336 (when tar-update-datestamp
1337 (tar-alter-one-field tar-time-offset
1338 (concat (tar-octal-time (current-time)) " "))))
1339 ;; After doing the insertion, add any necessary final padding.
1340 (tar-pad-to-blocksize))
1341 (set-buffer-modified-p t) ; mark the tar file as modified
1342 (tar-next-line 0))
1343 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1344 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1345 (buffer-name tar-superior-buffer))
1346 ;; Prevent basic-save-buffer from changing our coding-system.
1347 (setq last-coding-system-used buffer-file-coding-system)
1348 ;; Prevent ordinary saving from happening.
1352 ;; When this function is called, it is sure that the buffer is unibyte.
1353 (defun tar-pad-to-blocksize ()
1354 "If we are being anal about tar file blocksizes, fix up the current buffer.
1355 Leaves the region wide."
1356 (if (null tar-anal-blocksize)
1358 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1359 (start (tar-header-data-start last-desc))
1360 (link-p (tar-header-link-type last-desc))
1361 (size (if link-p 0 (tar-header-size last-desc)))
1362 (data-end (+ start size))
1363 (bbytes (ash tar-anal-blocksize 9))
1364 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
1365 ;; If the padding after the last data is too long, delete some;
1366 ;; else insert some until we are padded out to the right number of blocks.
1368 (with-current-buffer tar-data-buffer
1369 (let ((goal-end (+ (point-min) pad-to)))
1370 (if (> (point-max) goal-end)
1371 (delete-region goal-end (point-max))
1372 (goto-char (point-max))
1373 (insert (make-string (- goal-end (point-max)) ?\0))))))))
1376 ;; Used in write-region-annotate-functions to write tar-files out correctly.
1377 (defun tar-write-region-annotate (start _end)
1378 ;; When called from write-file (and auto-save), `start' is nil.
1379 ;; When called from M-x write-region, we assume the user wants to save
1380 ;; (part of) the summary, not the tar data.
1381 (unless (or start (not (tar-data-swapped-p)))
1382 (tar-clear-modification-flags)
1383 (set-buffer tar-data-buffer)
1384 nil))
1386 (provide 'tar-mode)
1388 ;;; tar-mode.el ends here