home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / tar-mode.el < prev    next >
Encoding:
Text File  |  1993-03-13  |  49.1 KB  |  1,287 lines

  1. ;;; -*- Mode: Emacs-Lisp -*-
  2.  
  3. ;;; File:        tar-mode.el
  4. ;;; Description:    simple editing of tar files from GNU emacs
  5. ;;; Author:        Jamie Zawinski <jwz@lucid.com>
  6. ;;; Created:        4 Apr 1990
  7. ;;; Version:        1.27, 6 Mar 93
  8.  
  9. ;;; Copyright (C) 1990-1993 Free Software Foundation, Inc.
  10. ;;;
  11. ;;; This file is part of GNU Emacs.
  12. ;;;
  13. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;;; it under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 2, or (at your option)
  16. ;;; any later version.
  17. ;;;
  18. ;;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  25. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;;; This package attempts to make dealing with Unix 'tar' archives easier.
  28. ;;; When this code is loaded, visiting a file whose name ends in '.tar' will
  29. ;;; cause the contents of that archive file to be displayed in a Dired-like
  30. ;;; listing.  It is then possible to use the customary Dired keybindings to
  31. ;;; extract sub-files from that archive, either by reading them into their own
  32. ;;; editor buffers, or by copying them directly to arbitrary files on disk.
  33. ;;; It is also possible to delete sub-files from within the tar file and write
  34. ;;; the modified archive back to disk, or to edit sub-files within the archive
  35. ;;; and re-insert the modified files into the archive.  See the documentation
  36. ;;; string of tar-mode for more info.
  37.  
  38. ;;; To autoload, add this to your .emacs file:
  39. ;;;
  40. ;;;  (setq auto-mode-alist (cons '("\\.tar$" . tar-mode) auto-mode-alist))
  41. ;;;  (autoload 'tar-mode "tar-mode")
  42. ;;;
  43. ;;; But beware: for certain tar files - those whose very first file has 
  44. ;;; a -*- property line - autoloading won't work.  See the function 
  45. ;;; "tar-normal-mode" to understand why.
  46.  
  47. ;;; This code now understands the extra fields that GNU tar adds to tar files.
  48.  
  49. ;;; This interacts correctly with "uncompress.el" in the Emacs library,
  50. ;;; and with sufficiently recent versions of "crypt.el" by Kyle Jones.
  51.  
  52. ;;;    ***************   TO DO   *************** 
  53. ;;;
  54. ;;; o  chmod should understand "a+x,og-w".
  55. ;;;
  56. ;;; o  It's not possible to add a NEW file to a tar archive; not that 
  57. ;;;    important, but still...
  58. ;;;
  59. ;;; o  The code is less efficient that it could be - in a lot of places, I
  60. ;;;    pull a 512-character string out of the buffer and parse it, when I could
  61. ;;;    be parsing it in place, not garbaging a string.  Should redo that.
  62. ;;;
  63. ;;; o  I'd like a command that searches for a string/regexp in every subfile
  64. ;;;    of an archive, where <esc> would leave you in a subfile-edit buffer.
  65. ;;;    (Like the Meta-R command of the Zmacs mail reader.)
  66. ;;;
  67. ;;; o  Sometimes (but not always) reverting the tar-file buffer does not 
  68. ;;;    re-grind the listing, and you are staring at the binary tar data.
  69. ;;;    Typing 'g' again immediately after that will always revert and re-grind
  70. ;;;    it, though.  I have no idea why this happens.
  71. ;;;
  72. ;;; o  If you edit a subfile, and selective-display gets set to t, then when
  73. ;;;    we save the subfile, we should map ^M -> ^J.
  74. ;;;
  75. ;;; o  Block files, sparse files, continuation files, and the various header
  76. ;;;    types aren't editable.  Actually I don't know that they work at all.
  77. ;;;    If you know that they work, or know that they don't, please let me know.
  78.  
  79. (defvar tar-anal-blocksize 20
  80.   "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
  81. The blocksize of a tar file is not really the size of the blocks; rather, it is
  82. the number of blocks written with one system call.  When tarring to a tape, 
  83. this is the size of the *tape* blocks, but when writing to a file, it doesn't
  84. matter much.  The only noticeable difference is that if a tar file does not
  85. have a blocksize of 20, the tar program will issue a warning; all this really
  86. controls is how many null padding bytes go on the end of the tar file.")
  87.  
  88. (defvar tar-update-datestamp (fboundp 'current-time-seconds)
  89.   "*Whether tar-mode should play fast and loose with sub-file datestamps;
  90. if this is true, then editing and saving a tar file entry back into its
  91. tar file will update its datestamp.  If false, the datestamp is unchanged.
  92. You may or may not want this - it is good in that you can tell when a file
  93. in a tar archive has been changed, but it is bad for the same reason that
  94. editing a file in the tar archive at all is bad - the changed version of 
  95. the file never exists on disk.
  96.  
  97. This does not work in Emacs 18, because there's no way to get the current 
  98. time as an integer - if this var is true, then editing a file sets its date
  99. to midnight, Jan 1 1970 GMT, which happens to be what 0 encodes.
  100.  
  101. I have written some C code to fix this deficiency which is included in 
  102. Lucid Emacs, and may be included in FSF's version 19.  Tar-mode will take
  103. advantage of this code if it is present.")
  104.  
  105. (defvar tar-view-kill-buffer t
  106.   "*Whether to kill the buffer when view-mode exits.  The standard view-mode
  107. requires this, but other versions (notably less.el) which don't use
  108. recursive edits do not.")
  109.  
  110.  
  111. ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
  112. ;;; but "cl.el" was messing some people up (also it's really big).
  113.  
  114. (defmacro tar-setf (form val)
  115.   "A mind-numbingly simple implementation of setf."
  116.   (let ((mform (macroexpand form (and (boundp 'byte-compile-macro-environment)
  117.                       byte-compile-macro-environment))))
  118.     (cond ((symbolp mform) (list 'setq mform val))
  119.       ((not (consp mform)) (error "can't setf %s" form))
  120.       ((eq (car mform) 'aref)
  121.        (list 'aset (nth 1 mform) (nth 2 mform) val))
  122.       ((eq (car mform) 'car)
  123.        (list 'setcar (nth 1 mform) val))
  124.       ((eq (car mform) 'cdr)
  125.        (list 'setcdr (nth 1 mform) val))
  126.       (t (error "don't know how to setf %s" form)))))
  127.  
  128. (defmacro tar-dolist (control &rest body)
  129.   "syntax: (dolist (var-name list-expr &optional return-value) &body body)"
  130.   (let ((var (car control))
  131.     (init (car (cdr control)))
  132.     (val (car (cdr (cdr control)))))
  133.     (list 'let (list (list '_dolist_iterator_ init))
  134.       (list 'while '_dolist_iterator_
  135.         (cons 'let
  136.           (cons (list (list var '(car _dolist_iterator_)))
  137.             (append body
  138.                 (list (list 'setq '_dolist_iterator_
  139.                     (list 'cdr '_dolist_iterator_)))))))
  140.       val)))
  141.  
  142. (defmacro tar-dotimes (control &rest body)
  143.   "syntax: (dotimes (var-name count-expr &optional return-value) &body body)"
  144.   (let ((var (car control))
  145.     (n (car (cdr control)))
  146.     (val (car (cdr (cdr control)))))
  147.     (list 'let (list (list '_dotimes_end_ n)
  148.              (list var 0))
  149.       (cons 'while
  150.         (cons (list '< var '_dotimes_end_)
  151.               (append body
  152.                   (list (list 'setq var (list '1+ var))))))
  153.       val)))
  154.  
  155.  
  156. ;;; down to business.
  157.  
  158. (defmacro make-tar-header (name mode uid git size date ck lt ln
  159.                magic uname gname devmaj devmin)
  160.   (list 'vector name mode uid git size date ck lt ln
  161.     magic uname gname devmaj devmin))
  162.  
  163. (defmacro tar-header-name (x) (list 'aref x 0))
  164. (defmacro tar-header-mode (x) (list 'aref x 1))
  165. (defmacro tar-header-uid  (x) (list 'aref x 2))
  166. (defmacro tar-header-gid  (x) (list 'aref x 3))
  167. (defmacro tar-header-size (x) (list 'aref x 4))
  168. (defmacro tar-header-date (x) (list 'aref x 5))
  169. (defmacro tar-header-checksum  (x) (list 'aref x 6))
  170. (defmacro tar-header-link-type (x) (list 'aref x 7))
  171. (defmacro tar-header-link-name (x) (list 'aref x 8))
  172. (defmacro tar-header-magic (x) (list 'aref x 9))
  173. (defmacro tar-header-uname (x) (list 'aref x 10))
  174. (defmacro tar-header-gname (x) (list 'aref x 11))
  175. (defmacro tar-header-dmaj (x) (list 'aref x 12))
  176. (defmacro tar-header-dmin (x) (list 'aref x 13))
  177.  
  178. (defmacro make-tar-desc (data-start tokens)
  179.   (list 'cons data-start tokens))
  180.  
  181. (defmacro tar-desc-data-start (x) (list 'car x))
  182. (defmacro tar-desc-tokens     (x) (list 'cdr x))
  183.  
  184. (defconst tar-name-offset 0)
  185. (defconst tar-mode-offset (+ tar-name-offset 100))
  186. (defconst tar-uid-offset  (+ tar-mode-offset 8))
  187. (defconst tar-gid-offset  (+ tar-uid-offset 8))
  188. (defconst tar-size-offset (+ tar-gid-offset 8))
  189. (defconst tar-time-offset (+ tar-size-offset 12))
  190. (defconst tar-chk-offset  (+ tar-time-offset 12))
  191. (defconst tar-linkp-offset (+ tar-chk-offset 8))
  192. (defconst tar-link-offset (+ tar-linkp-offset 1))
  193. ;;; GNU-tar specific slots.
  194. (defconst tar-magic-offset (+ tar-link-offset 100))
  195. (defconst tar-uname-offset (+ tar-magic-offset 8))
  196. (defconst tar-gname-offset (+ tar-uname-offset 32))
  197. (defconst tar-dmaj-offset (+ tar-gname-offset 32))
  198. (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
  199. (defconst tar-end-offset (+ tar-dmin-offset 8))
  200.  
  201. (defun tokenize-tar-header-block (string)
  202.   "Returns a 'tar-header' structure (a list of name, mode, uid, gid, size, 
  203. write-date, checksum, link-type, and link-name)."
  204.   (cond ((< (length string) 512) nil)
  205.     (;(some 'plusp string)         ; <-- oops, massive cycle hog!
  206.      (or (not (= 0 (aref string 0))) ; This will do.
  207.          (not (= 0 (aref string 101))))
  208.      (let* ((name-end (1- tar-mode-offset))
  209.         (link-end (1- tar-magic-offset))
  210.         (uname-end (1- tar-gname-offset))
  211.         (gname-end (1- tar-dmaj-offset))
  212.         (link-p (aref string tar-linkp-offset))
  213.         (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
  214.         (uname-valid-p (or (string= "ustar  " magic-str) (string= "GNUtar " magic-str)))
  215.         name
  216.         (nulsexp   "[^\000]*\000"))
  217.        (and (string-match nulsexp string tar-name-offset) (setq name-end (min name-end (1- (match-end 0)))))
  218.        (and (string-match nulsexp string tar-link-offset) (setq link-end (min link-end (1- (match-end 0)))))
  219.        (and (string-match nulsexp string tar-uname-offset) (setq uname-end (min uname-end (1- (match-end 0)))))
  220.        (and (string-match nulsexp string tar-gname-offset) (setq gname-end (min gname-end (1- (match-end 0)))))
  221.        (setq name (substring string tar-name-offset name-end)
  222.          link-p (if (or (= link-p 0) (= link-p ?0))
  223.                 nil
  224.               (- link-p ?0)))
  225.        (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
  226.        (make-tar-header
  227.          name
  228.          (tar-parse-octal-integer string tar-mode-offset (1- tar-uid-offset))
  229.          (tar-parse-octal-integer string tar-uid-offset (1- tar-gid-offset))
  230.          (tar-parse-octal-integer string tar-gid-offset (1- tar-size-offset))
  231.          (tar-parse-octal-integer string tar-size-offset (1- tar-time-offset))
  232.          (tar-parse-octal-integer-32 string tar-time-offset (1- tar-chk-offset))
  233.          (tar-parse-octal-integer string tar-chk-offset (1- tar-linkp-offset))
  234.          link-p
  235.          (substring string tar-link-offset link-end)
  236.          uname-valid-p
  237.          (and uname-valid-p (substring string tar-uname-offset uname-end))
  238.          (and uname-valid-p (substring string tar-gname-offset gname-end))
  239.          (tar-parse-octal-integer string tar-dmaj-offset (1- tar-dmin-offset))
  240.          (tar-parse-octal-integer string tar-dmin-offset (1- tar-end-offset))
  241.          )))
  242.     (t 'empty-tar-block)))
  243.  
  244.  
  245. (defun tar-parse-octal-integer (string &optional start end)
  246.   "deletes all your files, and then reboots."
  247.   (if (null start) (setq start 0))
  248.   (if (null end) (setq end (length string)))
  249.   (if (= (aref string start) 0)
  250.       0
  251.     (let ((n 0))
  252.       (while (< start end)
  253.     (setq n (if (< (aref string start) ?0) n
  254.           (+ (* n 8) (- (aref string start) 48)))
  255.           start (1+ start)))
  256.       n)))
  257.  
  258. (defun tar-parse-octal-integer-32 (string &optional start end)
  259.   ;; like tar-parse-octal-integer, but returns a cons of two 16-bit numbers,
  260.   ;; since elisp can't handle integers of that magnitude.
  261.   (or start (setq start 0))
  262.   (or end (setq end (length string)))
  263.   (let ((top (tar-parse-octal-integer string start (- end 6)))
  264.     (bot (tar-parse-octal-integer string (- end 6) end)))
  265.     (setq top (logior (ash top 2) (ash bot -16)))
  266.     (setq bot (logand bot 65535))
  267.     (cons top bot)))
  268.  
  269. (defun tar-parse-octal-integer-safe (string)
  270.   (let ((L (length string)))
  271.     (if (= L 0) (error "empty string"))
  272.     (tar-dotimes (i L)
  273.        (if (or (< (aref string i) ?0)
  274.            (> (aref string i) ?7))
  275.        (error "'%c' is not an octal digit."))))
  276.   (tar-parse-octal-integer string))
  277.  
  278.  
  279. (defun checksum-tar-header-block (string)
  280.   "Computes and returns a tar-acceptable checksum for this block."
  281.   (let* ((chk-field-start tar-chk-offset)
  282.      (chk-field-end (+ chk-field-start 8))
  283.      (sum 0)
  284.      (i 0))
  285.     ;; Add up all of the characters except the ones in the checksum field.
  286.     ;; Add that field as if it were filled with spaces.
  287.     (while (< i chk-field-start)
  288.       (setq sum (+ sum (aref string i))
  289.         i (1+ i)))
  290.     (setq i chk-field-end)
  291.     (while (< i 512)
  292.       (setq sum (+ sum (aref string i))
  293.         i (1+ i)))
  294.     (+ sum (* 32 8))))
  295.  
  296. (defun check-tar-header-block-checksum (hblock desired-checksum file-name)
  297.   "Beep and print a warning if the checksum doesn't match."
  298.   (if (not (= desired-checksum (checksum-tar-header-block hblock)))
  299.       (progn (beep) (message "Invalid checksum for file %s!" file-name))))
  300.  
  301. (defun recompute-tar-header-block-checksum (hblock)
  302.   "Modifies the given string to have a valid checksum field."
  303.   (let* ((chk (checksum-tar-header-block hblock))
  304.      (chk-string (format "%6o" chk))
  305.      (l (length chk-string)))
  306.     (aset hblock 154 0)
  307.     (aset hblock 155 32)
  308.     (tar-dotimes (i l) (aset hblock (- 153 i) (aref chk-string (- l i 1)))))
  309.   hblock)
  310.  
  311.  
  312. (defun tar-grind-file-mode (mode string start)
  313.   "Write a \"-rw--r--r-\" representing MODE into STRING beginning at START."
  314.   (aset string start       (if (zerop (logand 256 mode)) ?- ?r))
  315.   (aset string (+ start 1) (if (zerop (logand 128 mode)) ?- ?w))
  316.   (aset string (+ start 2) (if (zerop (logand  64 mode)) ?- ?x)) 
  317.   (aset string (+ start 3) (if (zerop (logand  32 mode)) ?- ?r))
  318.   (aset string (+ start 4) (if (zerop (logand  16 mode)) ?- ?w))
  319.   (aset string (+ start 5) (if (zerop (logand   8 mode)) ?- ?x))
  320.   (aset string (+ start 6) (if (zerop (logand   4 mode)) ?- ?r))
  321.   (aset string (+ start 7) (if (zerop (logand   2 mode)) ?- ?w))
  322.   (aset string (+ start 8) (if (zerop (logand   1 mode)) ?- ?x))
  323.   (if (zerop (logand 1024 mode)) nil (aset string (+ start 2) ?s))
  324.   (if (zerop (logand 2048 mode)) nil (aset string (+ start 5) ?s))
  325.   string)
  326.  
  327.  
  328. (defconst tar-can-print-dates (fboundp 'current-time-seconds)
  329.   "true if this emacs has been built with time-printing support")
  330.  
  331. (defun summarize-tar-header-block (tar-hblock &optional mod-p)
  332.   "Returns a line similar to the output of 'tar -vtf'."
  333.   (let ((name (tar-header-name tar-hblock))
  334.     (mode (tar-header-mode tar-hblock))
  335.     (uid (tar-header-uid tar-hblock))
  336.     (gid (tar-header-gid tar-hblock))
  337.     (uname (tar-header-uname tar-hblock))
  338.     (gname (tar-header-gname tar-hblock))
  339.     (size (tar-header-size tar-hblock))
  340.     (time (tar-header-date tar-hblock))
  341.     (ck (tar-header-checksum tar-hblock))
  342.     (link-p (tar-header-link-type tar-hblock))
  343.     (link-name (tar-header-link-name tar-hblock))
  344.     )
  345.     (let* ((left 11)
  346.        (namew 8)
  347.        (groupw 8)
  348.        (sizew 8)
  349.        (datew (if tar-can-print-dates 15 2))
  350.        (slash (1- (+ left namew)))
  351.        (lastdigit (+ slash groupw sizew))
  352.        (namestart (+ lastdigit datew))
  353.        (string (make-string (+ namestart (length name) (if link-p (+ 5 (length link-name)) 0)) 32))
  354.        (type (tar-header-link-type tar-hblock)))
  355.       (aset string 0 (if mod-p ?* ? ))
  356.       (aset string 1
  357.         (cond ((or (eq type nil) (eq type 0)) ?-)
  358.           ((eq type 1) ?l)  ; link
  359.           ((eq type 2) ?s)  ; symlink
  360.           ((eq type 3) ?c)  ; char special
  361.           ((eq type 4) ?b)  ; block special
  362.           ((eq type 5) ?d)  ; directory
  363.           ((eq type 6) ?p)  ; FIFO/pipe
  364.           ((eq type 20) ?*) ; directory listing
  365.           ((eq type 29) ?M) ; multivolume continuation
  366.           ((eq type 35) ?S) ; sparse
  367.           ((eq type 38) ?V) ; volume header
  368.           ))
  369.       (tar-grind-file-mode mode string 2)
  370.       (setq uid (if (= 0 (length uname)) (int-to-string uid) uname))
  371.       (setq gid (if (= 0 (length gname)) (int-to-string gid) gname))
  372.       (setq size (int-to-string size))
  373.       (tar-dotimes (i (min (1- namew) (length uid))) (aset string (- slash i) (aref uid (- (length uid) i 1))))
  374.       (aset string (1+ slash) ?/)
  375.       (tar-dotimes (i (min (1- groupw) (length gid))) (aset string (+ (+ slash 2) i) (aref gid i)))
  376.       (tar-dotimes (i (min sizew (length size))) (aset string (- lastdigit i) (aref size (- (length size) i 1))))
  377.  
  378.       (if tar-can-print-dates
  379.       (let* ((year (substring (current-time-string) -4))
  380.          ;; in v18, current-time-string doesn't take an argument
  381.          (file (current-time-string time))
  382.          (file-year (substring file -4))
  383.          (str (if (equal year file-year)
  384.               (substring file 4 16)
  385.             (concat (substring file 4 11) " " file-year))))
  386.         (tar-dotimes (i 12) (aset string (- namestart (- 13 i)) (aref str i)))))
  387.  
  388.       (tar-dotimes (i (length name)) (aset string (+ namestart i) (aref name i)))
  389.       (if (or (eq link-p 1) (eq link-p 2))
  390.       (progn
  391.         (tar-dotimes (i 3) (aset string (+ namestart 1 (length name) i) (aref (if (= link-p 1) "==>" "-->") i)))
  392.         (tar-dotimes (i (length link-name)) (aset string (+ namestart 5 (length name) i) (aref link-name i)))))
  393.       string)))
  394.  
  395.  
  396. ;; buffer-local variables in the tar file's buffer:
  397. ;;
  398. (defvar tar-parse-info)        ; the header structures
  399. (defvar tar-header-offset)    ; the end of the "pretty" data
  400.  
  401. (defun tar-summarize-buffer ()
  402.   "Parse the contents of the tar file in the current buffer, and place a
  403. dired-like listing on the front; then narrow to it, so that only that listing
  404. is visible (and the real data of the buffer is hidden)."
  405.   (message "parsing tar file...")
  406.   (let* ((result '())
  407.      (pos 1)
  408.      (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
  409.      (bs100 (max 1 (/ bs 100)))
  410.     (tokens nil))
  411.     (while (not (eq tokens 'empty-tar-block))
  412.       (if (> (+ pos 512) (point-max))
  413.       (error "truncated tar file"))
  414.       (let* ((hblock (buffer-substring pos (+ pos 512))))
  415.     (setq tokens (tokenize-tar-header-block hblock))
  416.     (setq pos (+ pos 512))
  417.     (message "parsing tar file...%s%%"
  418.          ;(/ (* pos 100) bs)   ; this gets round-off lossage
  419.          (/ pos bs100)         ; this doesn't
  420.          )
  421.     (if (eq tokens 'empty-tar-block)
  422.         nil
  423.       (if (null tokens) (error "premature EOF parsing tar file."))
  424.       (if (eq (tar-header-link-type tokens) 20)
  425.           ;; Foo.  There's an extra empty block after these.
  426.           (setq pos (+ pos 512)))
  427.       (let ((size (tar-header-size tokens)))
  428.         (if (< size 0)
  429.         (error "%s has size %s - corrupted."
  430.                (tar-header-name tokens) size))
  431.         ;
  432.         ; This is just too slow.  Don't really need it anyway....
  433.         ;(check-tar-header-block-checksum
  434.         ;  hblock (checksum-tar-header-block hblock)
  435.         ;  (tar-header-name tokens))
  436.         
  437.         (setq result (cons (make-tar-desc pos tokens) result))
  438.         
  439.         (if (and (null (tar-header-link-type tokens))
  440.              (> size 0))
  441.         (setq pos
  442.           (+ pos 512 (ash (ash (1- size) -9) 9))        ; this works
  443.           ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
  444.           ))
  445.         ))))
  446.     (make-local-variable 'tar-parse-info)
  447.     (setq tar-parse-info (nreverse result)))
  448.   (message "parsing tar file...formatting...")
  449.   (save-excursion
  450.     (goto-char (point-min))
  451.     (let ((buffer-read-only nil))
  452.       (tar-dolist (tar-desc tar-parse-info)
  453.     (insert
  454.       (summarize-tar-header-block (tar-desc-tokens tar-desc))
  455.       "\n"))
  456.       (make-local-variable 'tar-header-offset)
  457.       (setq tar-header-offset (point))
  458.       (narrow-to-region 1 tar-header-offset)
  459.       (set-buffer-modified-p nil)))
  460.   (message "parsing tar file...done."))
  461.  
  462.  
  463. (defvar tar-mode-map nil "*Local keymap for tar-mode listings.")
  464.  
  465. (if tar-mode-map
  466.     nil
  467.   (setq tar-mode-map (make-keymap))
  468.   (suppress-keymap tar-mode-map)
  469.   (define-key tar-mode-map " " 'tar-next-line)
  470.   (define-key tar-mode-map "c" 'tar-copy)
  471.   (define-key tar-mode-map "d" 'tar-flag-deleted)
  472.   (define-key tar-mode-map "\^D" 'tar-flag-deleted)
  473.   (define-key tar-mode-map "e" 'tar-extract)
  474.   (define-key tar-mode-map "f" 'tar-extract)
  475.   (define-key tar-mode-map "g" 'revert-buffer)
  476.   (define-key tar-mode-map "h" 'describe-mode)
  477.   (define-key tar-mode-map "n" 'tar-next-line)
  478.   (define-key tar-mode-map "\^N" 'tar-next-line)
  479.   (define-key tar-mode-map "o" 'tar-extract-other-window)
  480.   (define-key tar-mode-map "\^C" 'tar-copy)
  481.   (define-key tar-mode-map "p" 'tar-previous-line)
  482.   (define-key tar-mode-map "\^P" 'tar-previous-line)
  483.   (define-key tar-mode-map "r" 'tar-rename-entry)
  484.   (define-key tar-mode-map "u" 'tar-unflag)
  485.   (define-key tar-mode-map "v" 'tar-view)
  486.   (define-key tar-mode-map "x" 'tar-expunge)
  487.   (define-key tar-mode-map "\177" 'tar-unflag-backwards)
  488.   (define-key tar-mode-map "E" 'tar-extract-other-window)
  489.   (define-key tar-mode-map "M" 'tar-chmod-entry)
  490.   (define-key tar-mode-map "G" 'tar-chgrp-entry)
  491.   (define-key tar-mode-map "O" 'tar-chown-entry)
  492.   )
  493.  
  494. ;; tar mode is suitable only for specially formatted data.
  495. (put 'tar-mode 'mode-class 'special)
  496. (put 'tar-subfile-mode 'mode-class 'special)
  497.  
  498. (defun tar-mode ()
  499.   "Major mode for viewing a tar file as a dired-like listing of its contents.
  500. You can move around using the usual cursor motion commands. 
  501. Letters no longer insert themselves.
  502. Type 'e' to pull a file out of the tar file and into its own buffer.
  503. Type 'c' to copy an entry from the tar file into another file on disk.
  504.  
  505. If you edit a sub-file of this archive (as with the 'e' command) and 
  506. save it with Control-X Control-S, the contents of that buffer will be 
  507. saved back into the tar-file buffer; in this way you can edit a file 
  508. inside of a tar archive without extracting it and re-archiving it.
  509.  
  510. See also: variables tar-update-datestamp and tar-anal-blocksize.
  511. \\{tar-mode-map}"
  512.   ;; this is not interactive because you shouldn't be turning this
  513.   ;; mode on and off.  You can corrupt things that way.
  514.   (make-local-variable 'tar-header-offset)
  515.   (make-local-variable 'tar-parse-info)
  516.   (make-local-variable 'require-final-newline)
  517.   (setq require-final-newline nil) ; binary data, dude...
  518.   (make-local-variable 'revert-buffer-function)
  519.   (setq revert-buffer-function 'tar-mode-revert)
  520.   (setq major-mode 'tar-mode)
  521.   (setq mode-name "Tar")
  522.   (use-local-map tar-mode-map)
  523.   (auto-save-mode 0)
  524.   (widen)
  525.   (if (and (boundp 'tar-header-offset) tar-header-offset)
  526.       (narrow-to-region 1 tar-header-offset)
  527.       (tar-summarize-buffer))
  528.   (run-hooks 'tar-mode-hook)
  529.   )
  530.  
  531. ;; buffer-local variables in subfile mode.
  532. ;;
  533. (defvar tar-subfile-mode nil)        ; whether the minor-mode is on
  534. (defvar superior-tar-buffer)        ; parent buffer
  535. (defvar superior-tar-descriptor)    ; header object of this file
  536. (defvar tar-subfile-buffer-id)        ; pretty name-string
  537. (defvar subfile-orig-mlbid)        ; orig mode-line-buffer-identification
  538.  
  539. (defun tar-subfile-mode (p)
  540.   "Minor mode for editing an element of a tar-file.
  541. This mode redefines ^X^S to save the current buffer back into its 
  542. associated tar-file buffer.  You must save that buffer to actually
  543. save your changes to disk."
  544.   (interactive "P")
  545.   (or (and (boundp 'superior-tar-buffer) superior-tar-buffer)
  546.       (error "This buffer is not an element of a tar file."))
  547.   (or (assq 'tar-subfile-mode minor-mode-alist)
  548.       (setq minor-mode-alist (append minor-mode-alist
  549.                      (list '(tar-subfile-mode " TarFile")))))
  550.   (make-local-variable 'tar-subfile-mode)
  551.   (setq tar-subfile-mode
  552.     (if (null p)
  553.         (not tar-subfile-mode)
  554.         (> (prefix-numeric-value p) 0)))
  555.   (cond (tar-subfile-mode
  556.      ;; copy the local keymap so that we don't accidentally
  557.      ;; alter a keymap like 'lisp-mode-map' which is shared
  558.      ;; by all buffers in that mode.
  559.      (let ((m (current-local-map)))
  560.        (if m (use-local-map (copy-keymap m))))
  561.      (local-set-key "\^X\^S" 'tar-subfile-save-buffer)
  562.      ;; turn off auto-save.
  563.      (auto-save-mode nil)
  564.      (setq buffer-auto-save-file-name nil)
  565.      (run-hooks 'tar-subfile-mode-hook))
  566.     (t
  567.      ;; remove the local binding for C-x C-s.
  568.      (local-unset-key "\^X\^S")
  569.      (if subfile-orig-mlbid
  570.          (set (make-local-variable 'mode-line-buffer-identification)
  571.           subfile-orig-mlbid))
  572.      (setq superior-tar-buffer nil
  573.            superior-tar-descriptor nil
  574.            subfile-orig-mlbid nil)
  575.      ))
  576.   )
  577.  
  578. (defun tar-subfile-after-write-file-hook ()
  579.   ;; if the buffer has a filename, then it is no longer associated with
  580.   ;; the tar file.  Turn off subfile mode.
  581.   (if (and buffer-file-name tar-subfile-mode)
  582.       (tar-subfile-mode -1)))
  583.  
  584. (defun tar-mode-revert (&optional no-autosave no-confirm)
  585.   "Revert this buffer and turn on tar mode again, to re-compute the
  586. directory listing."
  587.   (setq tar-header-offset nil)
  588.   (let ((revert-buffer-function nil))
  589.     (revert-buffer t no-confirm)
  590.     (widen))
  591.   (tar-mode))
  592.  
  593.  
  594. (defun tar-next-line (p)
  595.   (interactive "p")
  596.   (forward-line p)
  597.   (if (eobp) nil (forward-char (if tar-can-print-dates 48 36))))
  598.  
  599. (defun tar-previous-line (p)
  600.   (interactive "p")
  601.   (tar-next-line (- p)))
  602.  
  603. (defun tar-current-descriptor (&optional noerror)
  604.   "Returns the tar-descriptor of the current line, or signals an error."
  605.   ;; I wish lines had plists, like in ZMACS...
  606.   (or (nth (count-lines (point-min)
  607.             (save-excursion (beginning-of-line) (point)))
  608.        tar-parse-info)
  609.       (if noerror
  610.       nil
  611.       (error "This line does not describe a tar-file entry."))))
  612.  
  613.  
  614. (defun tar-extract (&optional other-window-p)
  615.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  616.   (interactive)
  617.   (let* ((view-p (eq other-window-p 'view))
  618.      (descriptor (tar-current-descriptor))
  619.      (tokens (tar-desc-tokens descriptor))
  620.      (name (tar-header-name tokens))
  621.      (size (tar-header-size tokens))
  622.      (link-p (tar-header-link-type tokens))
  623.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  624.      (end (+ start size)))
  625.     (if link-p
  626.     (error "This is a %s, not a real file."
  627.            (cond ((eq link-p 5) "directory")
  628.              ((eq link-p 20) "tar directory header")
  629.              ((eq link-p 29) "multivolume-continuation")
  630.              ((eq link-p 35) "sparse entry")
  631.              ((eq link-p 38) "volume header")
  632.              (t "link"))))
  633.     (if (zerop size) (error "This is a zero-length file."))
  634.     (let* ((tar-buffer (current-buffer))
  635.        (bufname (file-name-nondirectory name))
  636.        (bufid (concat ;" (" name " in "
  637.                  " (in "
  638.               (file-name-nondirectory (buffer-file-name))
  639.               ")"))
  640.        (read-only-p (or buffer-read-only view-p))
  641.        (buffer nil)
  642.        (buffers (buffer-list))
  643.        (just-created nil))
  644.       ;; find a buffer visiting this subfile from this tar file.
  645.       (while (and buffers (not buffer))
  646.     (set-buffer (car buffers))
  647.     (if (and (null (buffer-file-name (car buffers)))
  648.          (boundp 'superior-tar-descriptor)
  649.          (eq superior-tar-descriptor descriptor))
  650.         (setq buffer (car buffers))
  651.         (setq buffers (cdr buffers))))
  652.       (set-buffer tar-buffer)
  653.       (if buffer
  654.       nil
  655.     (setq buffer (generate-new-buffer bufname))
  656.     (setq just-created t)
  657.     (unwind-protect
  658.         (progn
  659.           (widen)
  660.           (save-excursion
  661.         (set-buffer buffer)
  662.         (insert-buffer-substring tar-buffer start end)
  663.         (goto-char 0)
  664.         (set-visited-file-name name) ; give it a name to decide mode.
  665. ;;        (normal-mode)  ; pick a mode.
  666.         (after-find-file nil nil)  ; pick a mode; works with crypt.el
  667.         (set-visited-file-name nil)  ; nuke the name - not meaningful.
  668.         
  669.         (make-local-variable 'superior-tar-buffer)
  670.         (make-local-variable 'superior-tar-descriptor)
  671.         (make-local-variable 'mode-line-buffer-identification)
  672.         (make-local-variable 'tar-subfile-buffer-id)
  673.         (make-local-variable 'subfile-orig-mlbid)
  674.         (setq superior-tar-buffer tar-buffer)
  675.         (setq superior-tar-descriptor descriptor)
  676.         (setq tar-subfile-buffer-id bufid)
  677.         (setq subfile-orig-mlbid mode-line-buffer-identification)
  678.         (cond ((stringp mode-line-buffer-identification)
  679.                (setq mode-line-buffer-identification
  680.                  (list mode-line-buffer-identification))))
  681.         (let ((ms (car mode-line-buffer-identification))
  682.               n)
  683.           (cond ((and (stringp ms)
  684.                   (string-match "%\\([0-9]+\\)b\\'" ms))
  685.              (setq mode-line-buffer-identification
  686.                    (cons
  687.                 (concat (substring ms 0
  688.                            (1- (match-beginning 1)))
  689.                     (substring ms (1+ (match-end 1))))
  690.                 (cons
  691.                  (list (car (read-from-string
  692.                          (substring ms (match-beginning 1)
  693.                             (match-end 1))))
  694.                        (concat "%b" tar-subfile-buffer-id))
  695.                  (cdr mode-line-buffer-identification)))))
  696.             (t
  697.              (setq mode-line-buffer-identification
  698.                    (list "Emacs: "
  699.                      (list 17
  700.                        (concat "%b"
  701.                            tar-subfile-buffer-id)))))))
  702.         (tar-subfile-mode 1)
  703.         
  704.         (setq buffer-read-only read-only-p)
  705.         (set-buffer-modified-p nil))
  706.           (set-buffer tar-buffer))
  707.       (narrow-to-region 1 tar-header-offset)))
  708.       (if view-p
  709.        (if tar-view-kill-buffer
  710.           (progn
  711.         (view-buffer buffer)
  712.         (and just-created (kill-buffer buffer)))
  713.         (view-buffer))
  714.       (if other-window-p
  715.           (switch-to-buffer-other-window buffer)
  716.           (switch-to-buffer buffer))))))
  717.  
  718.  
  719. (defun tar-extract-other-window ()
  720.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  721.   (interactive)
  722.   (tar-extract t))
  723.  
  724. (defun tar-view ()
  725.   "*In tar-mode, view the tar file entry on this line."
  726.   (interactive)
  727.   (tar-extract 'view))
  728.  
  729.  
  730. (defun tar-read-file-name (&optional prompt)
  731.   "Calls read-file-name, with the default being the file of the current
  732. tar-file descriptor."
  733.   (or prompt (setq prompt "Copy to: "))
  734.   (let* ((default-file (expand-file-name
  735.             (tar-header-name (tar-desc-tokens
  736.                       (tar-current-descriptor)))))
  737.      (target (expand-file-name
  738.           (read-file-name prompt
  739.                   (file-name-directory default-file)
  740.                   default-file nil))))
  741.     (if (or (string= "" (file-name-nondirectory target))
  742.         (file-directory-p target))
  743.     (setq target (concat (if (string-match "/$" target)
  744.                  (substring target 0 (1- (match-end 0)))
  745.                  target)
  746.                  "/"
  747.                  (file-name-nondirectory default-file))))
  748.     target))
  749.  
  750.  
  751. (defun tar-copy (&optional to-file)
  752.   "*In tar-mode, extract this entry of the tar file into a file on disk.
  753. If TO-FILE is not supplied, it is prompted for, defaulting to the name of
  754. the current tar-entry."
  755.   (interactive (list (tar-read-file-name)))
  756.   (let* ((descriptor (tar-current-descriptor))
  757.      (tokens (tar-desc-tokens descriptor))
  758.      (name (tar-header-name tokens))
  759.      (size (tar-header-size tokens))
  760.      (link-p (tar-header-link-type tokens))
  761.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  762.      (end (+ start size)))
  763.     (if link-p (error "This is a link, not a real file."))
  764.     (if (zerop size) (error "This is a zero-length file."))
  765.     (let* ((tar-buffer (current-buffer))
  766.        buffer)
  767.       (unwind-protect
  768.       (progn
  769.         (setq buffer (generate-new-buffer "*tar-copy-tmp*"))
  770.         (widen)
  771.         (save-excursion
  772.           (set-buffer buffer)
  773.           (insert-buffer-substring tar-buffer start end)
  774.           (set-buffer-modified-p nil) ; in case we abort
  775.           (write-file to-file)
  776.           (message "Copied tar entry %s to %s" name to-file)
  777.           (set-buffer tar-buffer)))
  778.     (narrow-to-region 1 tar-header-offset)
  779.     (if buffer (kill-buffer buffer)))
  780.       )))
  781.  
  782.  
  783. (defun tar-flag-deleted (p &optional unflag)
  784.   "*In tar mode, mark this sub-file to be deleted from the tar file.
  785. With a prefix argument, mark that many files."
  786.   (interactive "p")
  787.   (beginning-of-line)
  788.   (tar-dotimes (i (if (< p 0) (- p) p))
  789.     (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
  790.     (progn
  791.       (delete-char 1)
  792.       (insert (if unflag " " "D"))))
  793.     (forward-line (if (< p 0) -1 1)))
  794.   (if (eobp) nil (forward-char 36)))
  795.  
  796. (defun tar-unflag (p)
  797.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  798. With a prefix argument, un-mark that many files forward."
  799.   (interactive "p")
  800.   (tar-flag-deleted p t))
  801.  
  802. (defun tar-unflag-backwards (p)
  803.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  804. With a prefix argument, un-mark that many files backward."
  805.   (interactive "p")
  806.   (tar-flag-deleted (- p) t))
  807.  
  808.  
  809. (defun tar-expunge-internal ()
  810.   "Expunge the tar-entry specified by the current line."
  811.   (let* ((descriptor (tar-current-descriptor))
  812.      (tokens (tar-desc-tokens descriptor))
  813.      (line (tar-desc-data-start descriptor))
  814.      (name (tar-header-name tokens))
  815.      (size (tar-header-size tokens))
  816.      (link-p (tar-header-link-type tokens))
  817.      (start (tar-desc-data-start descriptor))
  818.      (following-descs (cdr (memq descriptor tar-parse-info))))
  819.     (if link-p (setq size 0)) ; size lies for hard-links.
  820.     ;;
  821.     ;; delete the current line...
  822.     (beginning-of-line)
  823.     (let ((line-start (point)))
  824.       (end-of-line) (forward-char)
  825.       (let ((line-len (- (point) line-start)))
  826.     (delete-region line-start (point))
  827.     ;;
  828.     ;; decrement the header-pointer to be in synch...
  829.     (setq tar-header-offset (- tar-header-offset line-len))))
  830.     ;;
  831.     ;; delete the data pointer...
  832.     (setq tar-parse-info (delq descriptor tar-parse-info))
  833.     ;;
  834.     ;; delete the data from inside the file...
  835.     (widen)
  836.     (let* ((data-start (+ start tar-header-offset -513))
  837.        (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
  838.       (delete-region data-start data-end)
  839.       ;;
  840.       ;; and finally, decrement the start-pointers of all following
  841.       ;; entries in the archive.  This is a pig when deleting a bunch
  842.       ;; of files at once - we could optimize this to only do the
  843.       ;; iteration over the files that remain, or only iterate up to
  844.       ;; the next file to be deleted.
  845.       (let ((data-length (- data-end data-start)))
  846.     (tar-dolist (desc following-descs)
  847.       (tar-setf (tar-desc-data-start desc)
  848.             (- (tar-desc-data-start desc) data-length))))
  849.       ))
  850.   (narrow-to-region 1 tar-header-offset))
  851.  
  852.  
  853. (defun tar-expunge (&optional noconfirm)
  854.   "*In tar-mode, delete all the archived files flagged for deletion.
  855. This does not modify the disk image; you must save the tar file itself
  856. for this to be permanent."
  857.   (interactive)
  858.   (if (or noconfirm
  859.       (y-or-n-p "expunge files marked for deletion? "))
  860.       (let ((n 0))
  861.     (save-excursion
  862.       (goto-char 0)
  863.       (while (not (eobp))
  864.         (if (looking-at "D")
  865.         (progn (tar-expunge-internal)
  866.                (setq n (1+ n)))
  867.         (forward-line 1)))
  868.       ;; after doing the deletions, add any padding that may be necessary.
  869.       (tar-pad-to-blocksize)
  870.       (narrow-to-region 1 tar-header-offset)
  871.       )
  872.     (if (zerop n)
  873.         (message "nothing to expunge.")
  874.         (message "%s expunged.  Be sure to save this buffer." n)))))
  875.  
  876.  
  877. (defun tar-clear-modification-flags ()
  878.   "remove the stars at the beginning of each line."
  879.   (save-excursion
  880.     (goto-char 0)
  881.     (while (< (point) tar-header-offset)
  882.       (if (looking-at "*")
  883.       (progn (delete-char 1) (insert " ")))
  884.       (forward-line 1))))
  885.  
  886.  
  887. (defun tar-chown-entry (new-uid)
  888.   "*Change the user-id associated with this entry in the tar file.
  889. If this tar file was written by GNU tar, then you will be able to edit
  890. the user id as a string; otherwise, you must edit it as a number.
  891. You can force editing as a number by calling this with a prefix arg.
  892. This does not modify the disk image; you must save the tar file itself
  893. for this to be permanent."
  894.   (interactive (list
  895.          (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  896.            (if (or current-prefix-arg
  897.                (not (tar-header-magic tokens)))
  898.                (let (n)
  899.              (while (not (numberp (setq n (read-minibuffer
  900.                             "New UID number: "
  901.                             (format "%s" (tar-header-uid tokens)))))))
  902.              n)
  903.                (read-string "New UID string: " (tar-header-uname tokens))))))
  904.   (cond ((stringp new-uid)
  905.      (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
  906.            new-uid)
  907.      (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
  908.     (t
  909.      (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
  910.            new-uid)
  911.      (tar-alter-one-field tar-uid-offset
  912.        (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
  913.  
  914.  
  915. (defun tar-chgrp-entry (new-gid)
  916.   "*Change the group-id associated with this entry in the tar file.
  917. If this tar file was written by GNU tar, then you will be able to edit
  918. the group id as a string; otherwise, you must edit it as a number.
  919. You can force editing as a number by calling this with a prefix arg.
  920. This does not modify the disk image; you must save the tar file itself
  921. for this to be permanent."
  922.   (interactive (list
  923.          (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  924.            (if (or current-prefix-arg
  925.                (not (tar-header-magic tokens)))
  926.                (let (n)
  927.              (while (not (numberp (setq n (read-minibuffer
  928.                             "New GID number: "
  929.                             (format "%s" (tar-header-gid tokens)))))))
  930.              n)
  931.                (read-string "New GID string: " (tar-header-gname tokens))))))
  932.   (cond ((stringp new-gid)
  933.      (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
  934.            new-gid)
  935.      (tar-alter-one-field tar-gname-offset
  936.        (concat new-gid "\000")))
  937.     (t
  938.      (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
  939.            new-gid)
  940.      (tar-alter-one-field tar-gid-offset
  941.        (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
  942.  
  943. (defun tar-rename-entry (new-name)
  944.   "*Change the name associated with this entry in the tar file.
  945. This does not modify the disk image; you must save the tar file itself
  946. for this to be permanent."
  947.   (interactive
  948.     (list (read-string "New name: "
  949.         (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
  950.   (if (string= "" new-name) (error "zero length name."))
  951.   (if (> (length new-name) 98) (error "name too long."))
  952.   (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
  953.         new-name)
  954.   (tar-alter-one-field 0
  955.     (substring (concat new-name (make-string 99 0)) 0 99)))
  956.  
  957.  
  958. (defun tar-chmod-entry (new-mode)
  959.   "*Change the protection bits associated with this entry in the tar file.
  960. This does not modify the disk image; you must save the tar file itself
  961. for this to be permanent."
  962.   (interactive (list (tar-parse-octal-integer-safe
  963.                (read-string "New protection (octal): "))))
  964.   (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
  965.         new-mode)
  966.   (tar-alter-one-field tar-mode-offset
  967.     (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
  968.  
  969.  
  970. (defun tar-alter-one-field (data-position new-data-string)
  971.   (let* ((descriptor (tar-current-descriptor))
  972.      (tokens (tar-desc-tokens descriptor)))
  973.     (unwind-protect
  974.     (save-excursion
  975.       ;;
  976.       ;; update the header-line.
  977.       (beginning-of-line)
  978.       (let ((p (point)))
  979.         (forward-line 1)
  980.         (delete-region p (point))
  981.         (insert (summarize-tar-header-block tokens) "\n")
  982.         (setq tar-header-offset (point-max)))
  983.       
  984.       (widen)
  985.       (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
  986.         ;;
  987.         ;; delete the old field and insert a new one.
  988.         (goto-char (+ start data-position))
  989.         (delete-region (point) (+ (point) (length new-data-string))) ; <--
  990.         (insert new-data-string) ; <--
  991.         ;;
  992.         ;; compute a new checksum and insert it.
  993.         (let ((chk (checksum-tar-header-block
  994.             (buffer-substring start (+ start 512)))))
  995.           (goto-char (+ start tar-chk-offset))
  996.           (delete-region (point) (+ (point) 8))
  997.           (insert (format "%6o" chk))
  998.           (insert 0)
  999.           (insert ? )
  1000.           (tar-setf (tar-header-checksum tokens) chk)
  1001.           ;;
  1002.           ;; ok, make sure we didn't botch it.
  1003.           (check-tar-header-block-checksum
  1004.             (buffer-substring start (+ start 512))
  1005.             chk (tar-header-name tokens))
  1006.           )))
  1007.       (narrow-to-region 1 tar-header-offset))))
  1008.  
  1009.  
  1010. (defun tar-subfile-save-buffer ()
  1011.   "In tar subfile mode, write this buffer back into its parent tar-file buffer.
  1012. This doesn't write anything to disk - you must save the parent tar-file buffer
  1013. to make your changes permanent."
  1014.   (interactive)
  1015.   (cond (buffer-file-name
  1016.      ;; tar-subfile buffers should have nil as buffer-file-name.  If they
  1017.      ;; ever gain a buffer-file-name, that means they have been written to
  1018.      ;; a real disk file, as with ^X^W.  If this happens, behave just like
  1019.      ;; `save-buffer.'
  1020.      (call-interactively 'save-buffer))
  1021.     (t
  1022.      (tar-subfile-save-buffer-internal))))
  1023.  
  1024. (defun tar-subfile-save-buffer-internal ()
  1025.   (if (not (and (boundp 'superior-tar-buffer) superior-tar-buffer))
  1026.       (error "this buffer has no superior tar file buffer."))
  1027.   (or (buffer-name superior-tar-buffer)
  1028.       (error "the superior tar file's buffer has been killed."))
  1029.   (if (not (and (boundp 'superior-tar-descriptor) superior-tar-descriptor))
  1030.       (error "this buffer doesn't have an index into its superior tar file!"))
  1031.  
  1032.   ;; Notice when crypt.el has uncompressed while reading the file, and signal
  1033.   ;; an error if the user tries to save back into the parent file (because
  1034.   ;; it won't work - the .Z subfile it writes won't really be compressed.)
  1035.   ;;
  1036.   (if (and (boundp 'buffer-save-encrypted) buffer-save-encrypted)
  1037.       (error "Don't know how to encrypt back into a tar file."))
  1038.   (if (and (boundp 'buffer-save-compacted) buffer-save-compacted)
  1039.       (error "Don't know how to compact back into a tar file."))
  1040.   (if (and (boundp 'buffer-save-compressed) buffer-save-compressed)
  1041.       (error "Don't know how to compress back into a tar file."))
  1042.   (if (and (boundp 'buffer-save-gzipped) buffer-save-gzipped)
  1043.       (error "Don't know how to gzip back into a tar file."))
  1044.  
  1045.   (save-excursion
  1046.   (let ((subfile (current-buffer))
  1047.     (subfile-size (buffer-size))
  1048.     (descriptor superior-tar-descriptor))
  1049.     (set-buffer superior-tar-buffer)
  1050.     (let* ((tokens (tar-desc-tokens descriptor))
  1051.        (start (tar-desc-data-start descriptor))
  1052.        (name (tar-header-name tokens))
  1053.        (size (tar-header-size tokens))
  1054.        (size-pad (ash (ash (+ size 511) -9) 9))
  1055.        (head (memq descriptor tar-parse-info))
  1056.        (following-descs (cdr head)))
  1057.       (if (not head)
  1058.     (error "Can't find this tar file entry in its parent tar file!"))
  1059.       (unwind-protect
  1060.        (save-excursion
  1061.     (widen)
  1062.     ;; delete the old data...
  1063.     (let* ((data-start (+ start tar-header-offset -1))
  1064.            (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
  1065.       (delete-region data-start data-end)
  1066.       ;; insert the new data...
  1067.       (goto-char data-start)
  1068.       (insert-buffer subfile)
  1069.       ;;
  1070.       ;; pad the new data out to a multiple of 512...
  1071.       (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
  1072.         (goto-char (+ data-start subfile-size))
  1073.         (insert (make-string (- subfile-size-pad subfile-size) 0))
  1074.         ;;
  1075.         ;; update the data pointer of this and all following files...
  1076.         (tar-setf (tar-header-size tokens) subfile-size)
  1077.         (let ((difference (- subfile-size-pad size-pad)))
  1078.           (tar-dolist (desc following-descs)
  1079.         (tar-setf (tar-desc-data-start desc)
  1080.               (+ (tar-desc-data-start desc) difference))))
  1081.         ;;
  1082.         ;; Update the size field in the header block.
  1083.         (let ((header-start (- data-start 512)))
  1084.           (goto-char (+ header-start tar-size-offset))
  1085.           (delete-region (point) (+ (point) 12))
  1086.           (insert (format "%11o" subfile-size))
  1087.           (insert ? )
  1088.           ;;
  1089.           ;; Maybe update the datestamp.
  1090.           (if (not tar-update-datestamp)
  1091.           nil
  1092.         (goto-char (+ header-start tar-time-offset))
  1093.         (delete-region (point) (+ (point) 12))
  1094.         (if tar-can-print-dates
  1095.             (let* ((now (current-time-seconds)) ; not defined in v18
  1096.              (top (car now))
  1097.              (bot (cdr now)))
  1098.             (tar-setf (tar-header-date tokens) now)
  1099.             ;; hair to print two 16-bit numbers as one octal number.
  1100.             (setq bot (logior (ash (logand top 3) 16) bot))
  1101.             (setq top (ash top -2))
  1102.             (insert (format "%5o" top))
  1103.             (insert (format "%06o " bot)))
  1104.           ;; otherwise, set it to the epoch.
  1105.           (insert (format "%11o " 0))
  1106.           (tar-setf (tar-header-date tokens) (cons 0 0))
  1107.           ))
  1108.           ;;
  1109.           ;; compute a new checksum and insert it.
  1110.           (let ((chk (checksum-tar-header-block
  1111.               (buffer-substring header-start data-start))))
  1112.         (goto-char (+ header-start tar-chk-offset))
  1113.         (delete-region (point) (+ (point) 8))
  1114.         (insert (format "%6o" chk))
  1115.         (insert 0)
  1116.         (insert ? )
  1117.         (tar-setf (tar-header-checksum tokens) chk)))
  1118.         ;;
  1119.         ;; alter the descriptor-line...
  1120.         ;;
  1121.         (let ((position (- (length tar-parse-info) (length head))))
  1122.           (goto-char 1)
  1123.           (next-line position)
  1124.           (beginning-of-line)
  1125.           (let ((p (point))
  1126.             (m (set-marker (make-marker) tar-header-offset)))
  1127.         (forward-line 1)
  1128.         (delete-region p (point))
  1129.         (insert-before-markers (summarize-tar-header-block tokens t) "\n")
  1130.         (setq tar-header-offset (marker-position m)))
  1131.           )))
  1132.     ;; after doing the insertion, add any final padding that may be necessary.
  1133.     (tar-pad-to-blocksize))
  1134.        (narrow-to-region 1 tar-header-offset)))
  1135.     (set-buffer-modified-p t)   ; mark the tar file as modified
  1136.     (set-buffer subfile)
  1137.     (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
  1138.     (message "saved into tar-buffer \"%s\" - remember to save that buffer!"
  1139.          (buffer-name superior-tar-buffer))
  1140.     )))
  1141.  
  1142.  
  1143. (defun tar-pad-to-blocksize ()
  1144.   "If we are being anal about tar file blocksizes, fix up the current buffer.
  1145. Leaves the region wide."
  1146.   (if (null tar-anal-blocksize)
  1147.       nil
  1148.     (widen)
  1149.     (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
  1150.        (start (tar-desc-data-start last-desc))
  1151.        (tokens (tar-desc-tokens last-desc))
  1152.        (link-p (tar-header-link-type tokens))
  1153.        (size (if link-p 0 (tar-header-size tokens)))
  1154.        (data-end (+ start size))
  1155.        (bbytes (ash tar-anal-blocksize 9))
  1156.        (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
  1157.        (buffer-read-only nil) ; ##
  1158.        )
  1159.       ;; If the padding after the last data is too long, delete some;
  1160.       ;; else insert some until we are padded out to the right number of blocks.
  1161.       ;;
  1162.       (goto-char (+ (or tar-header-offset 0) data-end))
  1163.       (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
  1164.       (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
  1165.       (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
  1166.                   (1+ (buffer-size)))
  1167.                    0)))
  1168.       )))
  1169.  
  1170.  
  1171. (defun maybe-write-tar-file ()
  1172.   "Used as a write-file-hook to write tar-files out correctly."
  1173.   ;;
  1174.   ;; If the current buffer is in tar-mode and has its header-offset set,
  1175.   ;; remove the header from the file, call the remaining write-file hooks,
  1176.   ;; and then write out the buffer (if and only if one of the write-file
  1177.   ;; hooks didn't write it already).  Then put the header back on the
  1178.   ;; buffer.  Many thanks to Piet van Oostrum for this code, which causes
  1179.   ;; correct interaction with crypt.el (and probably anything like it.)
  1180.   ;;
  1181.   (and (eq major-mode 'tar-mode)
  1182.        (and (boundp 'tar-header-offset) tar-header-offset)
  1183.        (let ((hooks (cdr (memq 'maybe-write-tar-file write-file-hooks)))
  1184.          header-string
  1185.          done)
  1186.      (save-excursion
  1187.       (save-restriction
  1188.        (widen)
  1189.        (tar-clear-modification-flags)
  1190.        (setq header-string (buffer-substring 1 tar-header-offset))
  1191.        (delete-region 1 tar-header-offset)
  1192.        (unwind-protect
  1193.            (progn
  1194.          (while (and hooks
  1195.                  (not (setq done (funcall (car hooks)))))
  1196.            (setq hooks (cdr hooks)))
  1197.          (cond ((not done)
  1198.             (write-region 1 (1+ (buffer-size))
  1199.                      buffer-file-name nil t)
  1200.             (setq done t))))
  1201.          (goto-char 1)
  1202.          (insert header-string)
  1203.          (set-buffer-modified-p nil))))
  1204.      done)))
  1205.  
  1206.  
  1207. ;;; Patch it in.
  1208.  
  1209. (defvar tar-regexp "\\.tar$"
  1210.   "The regular expression used to identify tar file names.")
  1211.  
  1212. (setq auto-mode-alist
  1213.       (cons (cons tar-regexp 'tar-mode) auto-mode-alist))
  1214.  
  1215. ;; Note: the tar write-file-hook should go on the list *before* any other
  1216. ;; hooks which might write the file.  Since things like crypt-mode add things
  1217. ;; to the end of the write-file-hooks, this will normally be the case.
  1218.  
  1219. ;(or (boundp 'write-file-hooks) (setq write-file-hooks nil))
  1220. ;(or (listp write-file-hooks)
  1221. ;    (setq write-file-hooks (list write-file-hooks)))
  1222. ;(or (memq 'maybe-write-tar-file write-file-hooks)
  1223. ;    (setq write-file-hooks
  1224. ;      (cons 'maybe-write-tar-file write-file-hooks)))
  1225.  
  1226. (add-hook 'write-file-hooks 'maybe-write-tar-file)
  1227. (add-hook 'after-write-file-hooks 'tar-subfile-after-write-file-hook)
  1228.  
  1229.  
  1230. ;;; This is a hack.  For files ending in .tar, we want -*- lines to be
  1231. ;;; completely ignored - if there is one, it applies to the first file
  1232. ;;; in the archive, and not the archive itself!  Similarly for local
  1233. ;;; variables specifications in the last file of the archive.
  1234.  
  1235. (defun tar-normal-mode (&optional find-file)
  1236.   "Choose the major mode for this buffer automatically.
  1237. Also sets up any specified local variables of the file.
  1238. Uses the visited file name, the -*- line, and the local variables spec.
  1239.  
  1240. This function is called automatically from `find-file'.  In that case,
  1241. if `inhibit-local-variables' is non-`nil' we require confirmation before
  1242. processing a local variables spec.  If you run `normal-mode' explicitly,
  1243. confirmation is never required.
  1244.  
  1245. Note that this version of this function has been hacked to interact
  1246. correctly with tar files - when visiting a file which matches
  1247. 'tar-regexp', the -*- line and local-variables are not examined,
  1248. as they would apply to a file within the archive rather than the archive
  1249. itself."
  1250.   (interactive)
  1251.   (if (and buffer-file-name
  1252.        (string-match tar-regexp buffer-file-name))
  1253.       (tar-mode)
  1254.       (tar-real-normal-mode find-file)))
  1255.  
  1256. ;; We have to shadow this as well to get along with crypt.el.
  1257. ;; Shadowing this alone isn't enough, though; we need to shadow 
  1258. ;; tar-normal-mode in order to inhibit the local variables of the
  1259. ;; last file in the tar archive.
  1260. ;;
  1261. (defun tar-set-auto-mode ()
  1262.   "Select major mode appropriate for current buffer.
  1263. May base decision on visited file name (See variable  auto-mode-list)
  1264. or on buffer contents (-*- line or local variables spec), but does not look
  1265. for the \"mode:\" local variable.  For that, use  hack-local-variables.
  1266.  
  1267. Note that this version of this function has been hacked to interact
  1268. correctly with tar files - when visiting a file which matches
  1269. 'tar-regexp', the -*- line and local-variables are not examined,
  1270. as they would apply to a file within the archive rather than the archive
  1271. itself."
  1272.   (interactive)
  1273.   (if (and buffer-file-name
  1274.        (string-match tar-regexp buffer-file-name))
  1275.       (tar-mode)
  1276.       (tar-real-set-auto-mode)))
  1277.  
  1278. (if (not (fboundp 'tar-real-normal-mode))
  1279.     (fset 'tar-real-normal-mode (symbol-function 'normal-mode)))
  1280. (fset 'normal-mode 'tar-normal-mode)
  1281.  
  1282. (if (not (fboundp 'tar-real-set-auto-mode))
  1283.     (fset 'tar-real-set-auto-mode (symbol-function 'set-auto-mode)))
  1284. (fset 'set-auto-mode 'tar-set-auto-mode)
  1285.  
  1286. (provide 'tar-mode)
  1287.