home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / emacs / sources / 964 < prev    next >
Encoding:
Text File  |  1993-01-25  |  50.2 KB  |  1,316 lines

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