* lisp/calendar/diary-lib.el (diary-remind): Fix bug#30455.
[emacs.git] / lisp / tar-mode.el
blobf7b14fab516629c5cf4633145b99e295da189360
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
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 <https://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 (setq name
269 (decode-coding-string name coding)
270 linkname
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))
288 coding)))
289 (cond
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))
298 descriptor)
300 (make-tar-header
301 (copy-marker pos nil)
302 name
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)
309 link-p
310 linkname
311 uname-valid-p
312 (when uname-valid-p
313 (decode-coding-string
314 (substring string tar-uname-offset uname-end) coding))
315 (when uname-valid-p
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)
320 ))))))
322 ;; Pseudo-field.
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)))
327 (+ data-start
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)
331 0))))
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)
338 (let ((n 0))
339 (while (< start end)
340 (setq n (if (< (aref string start) ?0) n
341 (+ (* n 8) (- (aref string start) ?0)))
342 start (1+ start)))
343 n)))
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)
349 (list 0 0)
350 (let ((lo 0)
351 (hi 0))
352 (while (< start end)
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)))
358 (list hi lo))))
360 (defun tar-parse-octal-integer-safe (string)
361 (if (zerop (length string)) (error "empty string"))
362 (mapc (lambda (c)
363 (if (or (< c ?0) (> c ?7))
364 (error "`%c' is not an octal digit" c)))
365 string)
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."
378 (make-tar-header
380 filename
381 #o644 0 0 (or size 0)
382 (or time (current-time))
383 nil ; checksum
384 nil nil
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)
391 (when val
392 (insert (tar--pad-to pos)
393 (if fmt
394 (format fmt (if mask (logand mask val) val))
395 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
400 checksum is correct.
402 If UPDATE-CHECKSUM is non-nil, update HEADER with the newly-computed
403 checksum before doing the check."
404 (with-temp-buffer
405 (set-buffer-multibyte nil)
406 (let ((encoded-name
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))
419 " ")
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))
429 (tar--put-at 512 "")
430 (let ((ck (tar-header-block-checksum (buffer-string))))
431 (goto-char (+ (point-min) tar-chk-offset))
432 (delete-char 8)
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)))
439 ;; .
440 (buffer-string)))
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))
448 (sum 0)
449 (i 0))
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))
454 i (1+ i)))
455 (setq i chk-field-end)
456 (while (< i 512)
457 (setq sum (+ sum (aref string i))
458 i (1+ i)))
459 (+ sum (* 32 8))))
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."
473 (string
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"
504 (if mod-p ?* ? )
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
519 (t ?\s)
521 (tar-grind-file-mode mode)
522 (if (= 0 (length uname)) uid uname)
523 (if (= 0 (length gname)) gid gname)
524 size
525 (if tar-mode-show-date (tar-clip-time-string time) "")
526 (propertize name
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)
531 ""))))
533 (defun tar-untar-buffer ()
534 "Extract all archive members in the tar-file into the current directory."
535 (interactive)
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.
538 (with-current-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)
544 name
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))
555 (when link-desc
556 (lwarn '(tar link) :warning
557 "Extracted `%s', %s, as a normal file"
558 name link-desc))
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))
566 (result '())
567 (pos (point-min))
568 (coding tar-file-name-coding-system)
569 (progress-reporter
570 (with-current-buffer tar-data-buffer
571 (make-progress-reporter "Parsing tar file..."
572 (point-min) (point-max))))
573 descriptor)
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)))
578 (if (< size 0)
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)
600 (total-summaries
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))))
606 (defvar tar-mode-map
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))
684 map)
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)))
701 ;;;###autoload
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'.
716 \\{tar-mode-map}"
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)
731 (widen)
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))))))
748 (condition-case err
749 (progn
750 (tar-swap-data)
751 (tar-summarize-buffer)
752 (tar-next-line 0))
753 (error
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.
757 (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.
765 With a prefix argument ARG, enable the mode if ARG is positive,
766 and disable it otherwise. If called from Lisp, enable the mode
767 if ARG is omitted or nil. This mode arranges for \"saving\" this
768 buffer to write the data into the tar-file buffer that it came
769 from. The changes will actually appear on disk when you save the
770 tar-file's buffer."
771 ;; Don't do this, because it is redundant and wastes mode line space.
772 ;; :lighter " TarFile"
773 nil nil nil
774 (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
775 (error "This buffer is not an element of a tar file"))
776 (cond (tar-subfile-mode
777 (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
778 ;; turn off auto-save.
779 (auto-save-mode -1)
780 (setq buffer-auto-save-file-name nil))
782 (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
785 ;; Revert the buffer and recompute the dired-like listing.
786 (defun tar-mode-revert (&optional no-auto-save no-confirm)
787 (unwind-protect
788 (let ((revert-buffer-function nil))
789 (if (tar-data-swapped-p) (tar-swap-data))
790 ;; FIXME: If we ask for confirmation, the user will be temporarily
791 ;; looking at the raw data.
792 (revert-buffer no-auto-save no-confirm 'preserve-modes)
793 ;; Recompute the summary.
794 (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
795 (tar-mode))
796 (unless (tar-data-swapped-p) (tar-swap-data))))
799 (defun tar-next-line (arg)
800 "Move cursor vertically down ARG lines and to the start of the filename."
801 (interactive "p")
802 (forward-line arg)
803 (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
805 (defun tar-previous-line (arg)
806 "Move cursor vertically up ARG lines and to the start of the filename."
807 (interactive "p")
808 (tar-next-line (- arg)))
810 (defun tar-current-position ()
811 "Return the `tar-parse-info' index for the current line."
812 (count-lines (point-min) (line-beginning-position)))
814 (defun tar-current-descriptor (&optional noerror)
815 "Return the tar-descriptor of the current line, or signals an error."
816 ;; I wish lines had plists, like in ZMACS...
817 (or (nth (tar-current-position)
818 tar-parse-info)
819 (if noerror
821 (error "This line does not describe a tar-file entry"))))
823 (defun tar--describe-as-link (descriptor)
824 (let ((link-p (tar-header-link-type descriptor)))
825 (if link-p
826 (cond ((eq link-p 5) "a directory")
827 ((eq link-p 20) "a tar directory header")
828 ((eq link-p 28) "a next has longname")
829 ((eq link-p 29) "a multivolume-continuation")
830 ((eq link-p 35) "a sparse entry")
831 ((eq link-p 38) "a volume header")
832 ((eq link-p 55) "a pax global extended header")
833 ((eq link-p 72) "a pax extended header")
834 (t "a link")))))
836 (defun tar--check-descriptor (descriptor)
837 (let ((link-desc (tar--describe-as-link descriptor)))
838 (when link-desc
839 (error "This is %s, not a real file" link-desc))))
841 (defun tar-get-descriptor ()
842 (let* ((descriptor (tar-current-descriptor))
843 (size (tar-header-size descriptor)))
844 (tar--check-descriptor descriptor)
845 (if (zerop size) (message "This is a zero-length file"))
846 descriptor))
848 (defun tar-get-file-descriptor (file)
849 ;; Used by package.el.
850 (let ((desc ()))
851 (dolist (hdr tar-parse-info)
852 (when (equal file (tar-header-name hdr))
853 (setq desc hdr)))
854 (tar--check-descriptor desc)
855 desc))
857 (defun tar-mouse-extract (event)
858 "Extract a file whose tar directory line you click on."
859 (interactive "e")
860 (with-current-buffer (window-buffer (posn-window (event-end event)))
861 (save-excursion
862 (goto-char (posn-point (event-end event)))
863 ;; Just make sure this doesn't get an error.
864 (tar-get-descriptor)))
865 (select-window (posn-window (event-end event)))
866 (goto-char (posn-point (event-end event)))
867 (tar-extract))
869 (defun tar-file-name-handler (op &rest args)
870 "Helper function for `tar-extract'."
871 (or (eq op 'file-exists-p)
872 (let ((file-name-handler-alist nil))
873 (apply op args))))
875 (defun tar--extract (descriptor)
876 "Extract this entry of the tar file into its own buffer."
877 (let* ((name (tar-header-name descriptor))
878 (size (tar-header-size descriptor))
879 (start (tar-header-data-start descriptor))
880 (end (+ start size))
881 (tarname (buffer-name))
882 (bufname (concat (file-name-nondirectory name)
883 " ("
884 tarname
885 ")"))
886 (buffer (generate-new-buffer bufname)))
887 (with-current-buffer tar-data-buffer
888 (let (coding)
889 (narrow-to-region start end)
890 (goto-char start)
891 (setq coding (or coding-system-for-read
892 (and set-auto-coding-function
893 (funcall set-auto-coding-function
894 name (- end start)))
895 ;; The following binding causes
896 ;; find-buffer-file-type-coding-system
897 ;; (defined on dos-w32.el) to act as if
898 ;; the file being extracted existed, so
899 ;; that the file's contents' encoding and
900 ;; EOL format are auto-detected.
901 (let ((file-name-handler-alist
902 '(("" . tar-file-name-handler))))
903 (car (find-operation-coding-system
904 'insert-file-contents
905 (cons name (current-buffer)) t)))))
906 (if (or (not coding)
907 (eq (coding-system-type coding) 'undecided))
908 (setq coding (detect-coding-region start end t)))
909 (if (coding-system-get coding :for-unibyte)
910 (with-current-buffer buffer
911 (set-buffer-multibyte nil)))
912 (widen)
913 (with-current-buffer buffer
914 (setq buffer-undo-list t))
915 (decode-coding-region start end coding buffer)
916 (with-current-buffer buffer
917 (setq buffer-undo-list nil))))
918 buffer))
920 (defun tar-extract (&optional other-window-p)
921 "In Tar mode, extract this entry of the tar file into its own buffer."
922 (interactive)
923 (let* ((view-p (eq other-window-p 'view))
924 (descriptor (tar-get-descriptor))
925 (name (tar-header-name descriptor))
926 (tar-buffer (current-buffer))
927 (tarname (buffer-name))
928 (read-only-p (or buffer-read-only view-p))
929 (new-buffer-file-name (expand-file-name
930 ;; `:' is not allowed on Windows
931 (concat tarname "!"
932 (if (string-match "/" name)
933 name
934 ;; Make sure `name' contains a /
935 ;; so set-auto-mode doesn't try
936 ;; to look at `tarname' for hints.
937 (concat "./" name)))))
938 (buffer (get-file-buffer new-buffer-file-name))
939 (just-created nil))
940 (unless buffer
941 (setq buffer (tar--extract descriptor))
942 (setq just-created t)
943 (with-current-buffer buffer
944 (goto-char (point-min))
945 (setq buffer-file-name new-buffer-file-name)
946 (setq buffer-file-truename
947 (abbreviate-file-name buffer-file-name))
948 ;; Force buffer-file-coding-system to what
949 ;; decode-coding-region actually used.
950 (set-buffer-file-coding-system last-coding-system-used t)
951 ;; Set the default-directory to the dir of the
952 ;; superior buffer.
953 (setq default-directory
954 (with-current-buffer tar-buffer
955 default-directory))
956 (set-buffer-modified-p nil)
957 (normal-mode) ; pick a mode.
958 (set (make-local-variable 'tar-superior-buffer) tar-buffer)
959 (set (make-local-variable 'tar-superior-descriptor) descriptor)
960 (setq buffer-read-only read-only-p)
961 (tar-subfile-mode 1)))
962 (cond
963 (view-p
964 (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
965 ((eq other-window-p 'display) (display-buffer buffer))
966 (other-window-p (switch-to-buffer-other-window buffer))
967 (t (switch-to-buffer buffer)))))
970 (defun tar-extract-other-window ()
971 "In Tar mode, find this entry of the tar file in another window."
972 (interactive)
973 (tar-extract t))
975 (defun tar-display-other-window ()
976 "In Tar mode, display this entry of the tar file in another window."
977 (interactive)
978 (tar-extract 'display))
980 (defun tar-view ()
981 "In Tar mode, view the tar file entry on this line."
982 (interactive)
983 (tar-extract 'view))
986 (defun tar-read-file-name (&optional prompt)
987 "Read a file name with this line's entry as the default."
988 (or prompt (setq prompt "Copy to: "))
989 (let* ((default-file (expand-file-name
990 (tar-header-name (tar-current-descriptor))))
991 (target (expand-file-name
992 (read-file-name prompt
993 (file-name-directory default-file)
994 default-file nil))))
995 (if (or (string= "" (file-name-nondirectory target))
996 (file-directory-p target))
997 (setq target (concat (if (string-match "/$" target)
998 (substring target 0 (1- (match-end 0)))
999 target)
1001 (file-name-nondirectory default-file))))
1002 target))
1005 (defun tar-copy (&optional to-file)
1006 "In Tar mode, extract this entry of the tar file into a file on disk.
1007 If TO-FILE is not supplied, it is prompted for, defaulting to the name of
1008 the current tar-entry."
1009 (interactive (list (tar-read-file-name)))
1010 (let* ((descriptor (tar-get-descriptor))
1011 (name (tar-header-name descriptor))
1012 (size (tar-header-size descriptor))
1013 (start (tar-header-data-start descriptor))
1014 (end (+ start size))
1015 (inhibit-file-name-handlers inhibit-file-name-handlers)
1016 (inhibit-file-name-operation inhibit-file-name-operation))
1017 (with-current-buffer
1018 (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
1019 ;; Inhibit compressing a subfile again if *both* name and
1020 ;; to-file are handled by jka-compr
1021 (if (and (eq (find-file-name-handler name 'write-region)
1022 'jka-compr-handler)
1023 (eq (find-file-name-handler to-file 'write-region)
1024 'jka-compr-handler))
1025 (setq inhibit-file-name-handlers
1026 (cons 'jka-compr-handler
1027 (and (eq inhibit-file-name-operation 'write-region)
1028 inhibit-file-name-handlers))
1029 inhibit-file-name-operation 'write-region))
1030 (let ((coding-system-for-write 'no-conversion))
1031 (write-region start end to-file nil nil nil t)))
1032 (message "Copied tar entry %s to %s" name to-file)))
1034 (defun tar-new-entry (filename &optional index)
1035 "Insert a new empty regular file before point."
1036 (interactive "*sFile name: ")
1037 (let* ((buffer (current-buffer))
1038 (index (or index (tar-current-position)))
1039 (d-list (and (not (zerop index))
1040 (nthcdr (+ -1 index) tar-parse-info)))
1041 (pos (if d-list
1042 (tar-header-data-end (car d-list))
1043 (point-min)))
1044 (new-descriptor
1045 (tar-new-regular-file-header filename)))
1046 ;; Update the data buffer; fill the missing descriptor fields.
1047 (with-current-buffer tar-data-buffer
1048 (goto-char pos)
1049 (insert (tar-header-serialize new-descriptor t))
1050 (setf (tar-header-data-start new-descriptor)
1051 (copy-marker (point) nil)))
1052 ;; Update tar-parse-info.
1053 (if d-list
1054 (setcdr d-list (cons new-descriptor (cdr d-list)))
1055 (setq tar-parse-info (cons new-descriptor tar-parse-info)))
1056 ;; Update the listing buffer.
1057 (save-excursion
1058 (goto-char (point-min))
1059 (forward-line index)
1060 (let ((inhibit-read-only t))
1061 (insert (tar-header-block-summarize new-descriptor) ?\n)))
1062 ;; .
1063 index))
1065 (defun tar-flag-deleted (p &optional unflag)
1066 "In Tar mode, mark this sub-file to be deleted from the tar file.
1067 With a prefix argument, mark that many files."
1068 (interactive "p")
1069 (beginning-of-line)
1070 (dotimes (i (abs p))
1071 (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
1072 (progn
1073 (delete-char 1)
1074 (insert (if unflag " " "D"))))
1075 (forward-line (if (< p 0) -1 1)))
1076 (if (eobp) nil (forward-char 36)))
1078 (defun tar-unflag (p)
1079 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1080 With a prefix argument, un-mark that many files forward."
1081 (interactive "p")
1082 (tar-flag-deleted p t))
1084 (defun tar-unflag-backwards (p)
1085 "In Tar mode, un-mark this sub-file if it is marked to be deleted.
1086 With a prefix argument, un-mark that many files backward."
1087 (interactive "p")
1088 (tar-flag-deleted (- p) t))
1091 (defun tar-expunge-internal ()
1092 "Expunge the tar-entry specified by the current line."
1093 (let ((descriptor (tar-current-descriptor)))
1095 ;; delete the current line...
1096 (delete-region (line-beginning-position) (line-beginning-position 2))
1098 ;; delete the data pointer...
1099 (setq tar-parse-info (delq descriptor tar-parse-info))
1101 ;; delete the data from inside the file...
1102 (with-current-buffer tar-data-buffer
1103 (delete-region (or (tar-header-header-start descriptor)
1104 (- (tar-header-data-start descriptor) 512))
1105 (tar-header-data-end descriptor)))))
1108 (defun tar-expunge (&optional noconfirm)
1109 "In Tar mode, delete all the archived files flagged for deletion.
1110 This does not modify the disk image; you must save the tar file itself
1111 for this to be permanent."
1112 (interactive)
1113 (if (or noconfirm
1114 (y-or-n-p "Expunge files marked for deletion? "))
1115 (let ((n 0))
1116 (save-excursion
1117 (goto-char (point-min))
1118 (while (not (eobp))
1119 (if (= (following-char) ?D)
1120 (progn (tar-expunge-internal)
1121 (setq n (1+ n)))
1122 (forward-line 1)))
1123 ;; after doing the deletions, add any padding that may be necessary.
1124 (tar-pad-to-blocksize))
1125 (if (zerop n)
1126 (message "Nothing to expunge.")
1127 (message "%s files expunged. Be sure to save this buffer." n)))))
1130 (defun tar-clear-modification-flags ()
1131 "Remove the stars at the beginning of each line."
1132 (interactive)
1133 (save-excursion
1134 (goto-char (point-min))
1135 (while (not (eobp))
1136 (if (not (eq (following-char) ?\s))
1137 (progn (delete-char 1) (insert " ")))
1138 (forward-line 1))))
1141 (defun tar-chown-entry (new-uid)
1142 "Change the user-id associated with this entry in the tar file.
1143 If this tar file was written by GNU tar, then you will be able to edit
1144 the user id as a string; otherwise, you must edit it as a number.
1145 You can force editing as a number by calling this with a prefix arg.
1146 This does not modify the disk image; you must save the tar file itself
1147 for this to be permanent."
1148 (interactive
1149 (list
1150 (let ((descriptor (tar-current-descriptor)))
1151 (if (or current-prefix-arg
1152 (not (tar-header-magic descriptor)))
1153 (read-number
1154 "New UID number: "
1155 (format "%s" (tar-header-uid descriptor)))
1156 (read-string "New UID string: " (tar-header-uname descriptor))))))
1157 (cond ((stringp new-uid)
1158 (setf (tar-header-uname (tar-current-descriptor)) new-uid)
1159 (tar-alter-one-field tar-uname-offset
1160 (concat (encode-coding-string
1161 new-uid tar-file-name-coding-system)
1162 "\000")))
1164 (setf (tar-header-uid (tar-current-descriptor)) new-uid)
1165 (tar-alter-one-field tar-uid-offset
1166 (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
1169 (defun tar-chgrp-entry (new-gid)
1170 "Change the group-id associated with this entry in the tar file.
1171 If this tar file was written by GNU tar, then you will be able to edit
1172 the group id as a string; otherwise, you must edit it as a number.
1173 You can force editing as a number by calling this with a prefix arg.
1174 This does not modify the disk image; you must save the tar file itself
1175 for this to be permanent."
1176 (interactive
1177 (list
1178 (let ((descriptor (tar-current-descriptor)))
1179 (if (or current-prefix-arg
1180 (not (tar-header-magic descriptor)))
1181 (read-number
1182 "New GID number: "
1183 (format "%s" (tar-header-gid descriptor)))
1184 (read-string "New GID string: " (tar-header-gname descriptor))))))
1185 (cond ((stringp new-gid)
1186 (setf (tar-header-gname (tar-current-descriptor)) new-gid)
1187 (tar-alter-one-field tar-gname-offset
1188 (concat (encode-coding-string
1189 new-gid tar-file-name-coding-system)
1190 "\000")))
1192 (setf (tar-header-gid (tar-current-descriptor)) new-gid)
1193 (tar-alter-one-field tar-gid-offset
1194 (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
1196 (defun tar-rename-entry (new-name)
1197 "Change the name associated with this entry in the tar file.
1198 This does not modify the disk image; you must save the tar file itself
1199 for this to be permanent."
1200 (interactive
1201 (list (read-string "New name: "
1202 (tar-header-name (tar-current-descriptor)))))
1203 (if (string= "" new-name) (error "zero length name"))
1204 (let ((encoded-new-name (encode-coding-string new-name
1205 tar-file-name-coding-system))
1206 (descriptor (tar-current-descriptor))
1207 (prefix nil))
1208 (when (tar-header-header-start descriptor)
1209 ;; FIXME: Make it work for ././@LongLink.
1210 (error "Rename with @LongLink format is not implemented"))
1212 (when (and (> (length encoded-new-name) 98)
1213 (string-match "/" encoded-new-name
1214 (- (length encoded-new-name) 99))
1215 (< (match-beginning 0) 155))
1216 (unless (equal (tar-header-magic descriptor) "ustar\0")
1217 (tar-alter-one-field tar-magic-offset (concat "ustar\0" "00")))
1218 (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
1219 (setq encoded-new-name (substring encoded-new-name (match-end 0))))
1221 (if (> (length encoded-new-name) 98) (error "name too long"))
1222 (setf (tar-header-name descriptor) new-name)
1223 (tar-alter-one-field 0
1224 (substring (concat encoded-new-name (make-string 99 0)) 0 99))
1225 (if prefix
1226 (tar-alter-one-field tar-prefix-offset
1227 (substring (concat prefix (make-string 155 0)) 0 155)))))
1230 (defun tar-chmod-entry (new-mode)
1231 "Change the protection bits associated with this entry in the tar file.
1232 This does not modify the disk image; you must save the tar file itself
1233 for this to be permanent."
1234 (interactive (list (tar-parse-octal-integer-safe
1235 (read-string "New protection (octal): "))))
1236 (setf (tar-header-mode (tar-current-descriptor)) new-mode)
1237 (tar-alter-one-field tar-mode-offset
1238 (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
1241 (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
1242 (unless descriptor (setq descriptor (tar-current-descriptor)))
1244 ;; update the header-line.
1245 (let ((col (current-column)))
1246 (delete-region (line-beginning-position)
1247 (prog2 (forward-line 1)
1248 (point)
1249 ;; Insert the new text after the old, before deleting,
1250 ;; to preserve markers such as the window start.
1251 (insert (tar-header-block-summarize descriptor) "\n")))
1252 (forward-line -1) (move-to-column col))
1254 (cl-assert (tar-data-swapped-p))
1255 (with-current-buffer tar-data-buffer
1256 (let* ((start (- (tar-header-data-start descriptor) 512)))
1258 ;; delete the old field and insert a new one.
1259 (goto-char (+ start data-position))
1260 (delete-region (point) (+ (point) (length new-data-string))) ; <--
1261 (cl-assert (not (or enable-multibyte-characters
1262 (multibyte-string-p new-data-string))))
1263 (insert new-data-string)
1265 ;; compute a new checksum and insert it.
1266 (let ((chk (tar-header-block-checksum
1267 (buffer-substring start (+ start 512)))))
1268 (goto-char (+ start tar-chk-offset))
1269 (delete-region (point) (+ (point) 8))
1270 (insert (format "%6o\0 " chk))
1271 (setf (tar-header-checksum descriptor) chk)
1273 ;; ok, make sure we didn't botch it.
1274 (tar-header-block-check-checksum
1275 (buffer-substring start (+ start 512))
1276 chk (tar-header-name descriptor))
1277 ))))
1280 (defun tar-octal-time (timeval)
1281 ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
1282 (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
1283 (format "%05o%01o%05o"
1284 (lsh hibits -2)
1285 (logior (lsh (logand 3 hibits) 1)
1286 (if (> (logand lobits 32768) 0) 1 0))
1287 (logand 32767 lobits)
1290 (defun tar-subfile-save-buffer ()
1291 "In tar subfile mode, save this buffer into its parent tar-file buffer.
1292 This doesn't write anything to disk; you must save the parent tar-file buffer
1293 to make your changes permanent."
1294 (interactive)
1295 (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
1296 (error "This buffer has no superior tar file buffer"))
1297 (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
1298 (error "This buffer doesn't have an index into its superior tar file!"))
1299 (let ((subfile (current-buffer))
1300 (coding buffer-file-coding-system)
1301 (descriptor tar-superior-descriptor)
1302 subfile-size)
1303 (with-current-buffer tar-superior-buffer
1304 (let* ((start (tar-header-data-start descriptor))
1305 (size (tar-header-size descriptor))
1306 (head (memq descriptor tar-parse-info)))
1307 (if (not head)
1308 (error "Can't find this tar file entry in its parent tar file!"))
1309 (with-current-buffer tar-data-buffer
1310 ;; delete the old data...
1311 (let* ((data-start start)
1312 (data-end (+ data-start (tar-roundup-512 size))))
1313 (narrow-to-region data-start data-end)
1314 (delete-region (point-min) (point-max))
1315 ;; insert the new data...
1316 (goto-char data-start)
1317 (let ((dest (current-buffer)))
1318 (with-current-buffer subfile
1319 (save-restriction
1320 (widen)
1321 (encode-coding-region (point-min) (point-max) coding dest))))
1322 (setq subfile-size (- (point-max) (point-min)))
1324 ;; pad the new data out to a multiple of 512...
1325 (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
1326 (goto-char (point-max))
1327 (insert (make-string (- subfile-size-pad subfile-size) 0))
1329 ;; update the data of this files...
1330 (setf (tar-header-size descriptor) subfile-size)
1332 ;; Update the size field in the header block.
1333 (widen))))
1335 ;; alter the descriptor-line and header
1337 (let ((position (- (length tar-parse-info) (length head))))
1338 (goto-char (point-min))
1339 (forward-line position)
1340 (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
1342 ;; Maybe update the datestamp.
1343 (when tar-update-datestamp
1344 (tar-alter-one-field tar-time-offset
1345 (concat (tar-octal-time (current-time)) " "))))
1346 ;; After doing the insertion, add any necessary final padding.
1347 (tar-pad-to-blocksize))
1348 (set-buffer-modified-p t) ; mark the tar file as modified
1349 (tar-next-line 0))
1350 (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
1351 (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
1352 (buffer-name tar-superior-buffer))
1353 ;; Prevent basic-save-buffer from changing our coding-system.
1354 (setq last-coding-system-used buffer-file-coding-system)
1355 ;; Prevent ordinary saving from happening.
1359 ;; When this function is called, it is sure that the buffer is unibyte.
1360 (defun tar-pad-to-blocksize ()
1361 "If we are being anal about tar file blocksizes, fix up the current buffer.
1362 Leaves the region wide."
1363 (if (null tar-anal-blocksize)
1365 (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
1366 (start (tar-header-data-start last-desc))
1367 (link-p (tar-header-link-type last-desc))
1368 (size (if link-p 0 (tar-header-size last-desc)))
1369 (data-end (+ start size))
1370 (bbytes (ash tar-anal-blocksize 9))
1371 (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
1372 ;; If the padding after the last data is too long, delete some;
1373 ;; else insert some until we are padded out to the right number of blocks.
1375 (with-current-buffer tar-data-buffer
1376 (let ((goal-end (+ (point-min) pad-to)))
1377 (if (> (point-max) goal-end)
1378 (delete-region goal-end (point-max))
1379 (goto-char (point-max))
1380 (insert (make-string (- goal-end (point-max)) ?\0))))))))
1383 ;; Used in write-region-annotate-functions to write tar-files out correctly.
1384 (defun tar-write-region-annotate (start _end)
1385 ;; When called from write-file (and auto-save), `start' is nil.
1386 ;; When called from M-x write-region, we assume the user wants to save
1387 ;; (part of) the summary, not the tar data.
1388 (unless (or start (not (tar-data-swapped-p)))
1389 (tar-clear-modification-flags)
1390 (set-buffer tar-data-buffer)
1391 nil))
1393 (provide 'tar-mode)
1395 ;;; tar-mode.el ends here