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