tar-mode.el 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. ;;; tar-mode.el --- simple editing of tar files from GNU Emacs
  2. ;; Copyright (C) 1990-1991, 1993-2015 Free Software Foundation, Inc.
  3. ;; Author: Jamie Zawinski <jwz@lucid.com>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Created: 04 Apr 1990
  6. ;; Keywords: unix
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package attempts to make dealing with Unix 'tar' archives easier.
  20. ;; When this code is loaded, visiting a file whose name ends in '.tar' will
  21. ;; cause the contents of that archive file to be displayed in a Dired-like
  22. ;; listing. It is then possible to use the customary Dired keybindings to
  23. ;; extract sub-files from that archive, either by reading them into their own
  24. ;; editor buffers, or by copying them directly to arbitrary files on disk.
  25. ;; It is also possible to delete sub-files from within the tar file and write
  26. ;; the modified archive back to disk, or to edit sub-files within the archive
  27. ;; and re-insert the modified files into the archive. See the documentation
  28. ;; string of tar-mode for more info.
  29. ;; This code now understands the extra fields that GNU tar adds to tar files.
  30. ;; This interacts correctly with "uncompress.el" in the Emacs library,
  31. ;; which you get with
  32. ;;
  33. ;; (autoload 'uncompress-while-visiting "uncompress")
  34. ;; (setq auto-mode-alist (cons '("\\.Z$" . uncompress-while-visiting)
  35. ;; auto-mode-alist))
  36. ;;
  37. ;; Do not attempt to use tar-mode.el with crypt.el, you will lose.
  38. ;; *************** TO DO ***************
  39. ;;
  40. ;; o chmod should understand "a+x,og-w".
  41. ;;
  42. ;; o The code is less efficient that it could be - in a lot of places, I
  43. ;; pull a 512-character string out of the buffer and parse it, when I could
  44. ;; be parsing it in place, not garbaging a string. Should redo that.
  45. ;;
  46. ;; o I'd like a command that searches for a string/regexp in every subfile
  47. ;; of an archive, where <esc> would leave you in a subfile-edit buffer.
  48. ;; (Like the Meta-R command of the Zmacs mail reader.)
  49. ;;
  50. ;; o Sometimes (but not always) reverting the tar-file buffer does not
  51. ;; re-grind the listing, and you are staring at the binary tar data.
  52. ;; Typing 'g' again immediately after that will always revert and re-grind
  53. ;; it, though. I have no idea why this happens.
  54. ;;
  55. ;; o Tar-mode interacts poorly with crypt.el and zcat.el because the tar
  56. ;; write-file-hook actually writes the file. Instead it should remove the
  57. ;; header (and conspire to put it back afterwards) so that other write-file
  58. ;; hooks which frob the buffer have a chance to do their dirty work. There
  59. ;; might be a problem if the tar write-file-hook does not come *first* on
  60. ;; the list.
  61. ;;
  62. ;; o Block files, sparse files, continuation files, and the various header
  63. ;; types aren't editable. Actually I don't know that they work at all.
  64. ;; Rationale:
  65. ;; Why does tar-mode edit the file itself instead of using tar?
  66. ;; That means that you can edit tar files which you don't have room for
  67. ;; on your local disk.
  68. ;; I don't know about recent features in gnu tar, but old versions of tar
  69. ;; can't replace a file in the middle of a tar file with a new version.
  70. ;; Tar-mode can. I don't think tar can do things like chmod the subfiles.
  71. ;; An implementation which involved unpacking and repacking the file into
  72. ;; some scratch directory would be very wasteful, and wouldn't be able to
  73. ;; preserve the file owners.
  74. ;;; Bugs:
  75. ;; - Rename on ././@LongLink files
  76. ;; - Revert confirmation displays the raw data temporarily.
  77. ;;; Code:
  78. (eval-when-compile (require 'cl-lib))
  79. (defgroup tar nil
  80. "Simple editing of tar files."
  81. :prefix "tar-"
  82. :group 'data)
  83. (defcustom tar-anal-blocksize 20
  84. "The blocksize of tar files written by Emacs, or nil, meaning don't care.
  85. The blocksize of a tar file is not really the size of the blocks; rather, it is
  86. the number of blocks written with one system call. When tarring to a tape,
  87. this is the size of the *tape* blocks, but when writing to a file, it doesn't
  88. matter much. The only noticeable difference is that if a tar file does not
  89. have a blocksize of 20, tar will tell you that; all this really controls is
  90. how many null padding bytes go on the end of the tar file."
  91. :type '(choice integer (const nil))
  92. :group 'tar)
  93. (defcustom tar-update-datestamp nil
  94. "Non-nil means 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. :type 'boolean
  102. :group 'tar)
  103. (defcustom tar-mode-show-date nil
  104. "Non-nil means Tar mode should show the date/time of each subfile.
  105. This information is useful, but it takes screen space away from file names."
  106. :type 'boolean
  107. :group 'tar)
  108. (defvar tar-parse-info nil)
  109. (defvar tar-superior-buffer nil
  110. "Buffer containing the tar archive from which a member was extracted.")
  111. (defvar tar-superior-descriptor nil
  112. "Tar descriptor for a member extracted from an archive.")
  113. (defvar tar-file-name-coding-system nil)
  114. (put 'tar-superior-buffer 'permanent-local t)
  115. (put 'tar-superior-descriptor 'permanent-local t)
  116. ;; The Tar data is made up of bytes and better manipulated as bytes
  117. ;; and can be very large, so insert/delete can be costly. The summary we
  118. ;; want to display may contain non-ascii chars, of course, so we'd like it
  119. ;; to be multibyte. We used to keep both in the same buffer and switch
  120. ;; from/to uni/multibyte. But this had several downsides:
  121. ;; - set-buffer-multibyte has an O(N^2) worst case that tends to be triggered
  122. ;; here, so it gets atrociously slow on large Tar files.
  123. ;; - need to widen/narrow the buffer to show/hide the raw data, and need to
  124. ;; maintain a tar-header-offset that keeps track of the boundary between
  125. ;; the two.
  126. ;; - can't use markers because they're not preserved by set-buffer-multibyte.
  127. ;; So instead, we now keep the two pieces of data in separate buffers, and
  128. ;; use the new buffer-swap-text primitive when we need to change which data
  129. ;; is associated with "the" buffer.
  130. (defvar tar-data-buffer nil "Buffer that holds the actual raw tar bytes.")
  131. (make-variable-buffer-local 'tar-data-buffer)
  132. (defvar tar-data-swapped nil
  133. "If non-nil, `tar-data-buffer' indeed holds raw tar bytes.")
  134. (make-variable-buffer-local 'tar-data-swapped)
  135. (defun tar-data-swapped-p ()
  136. "Return non-nil if the tar-data is in `tar-data-buffer'."
  137. (and (buffer-live-p tar-data-buffer)
  138. ;; Sanity check to try and make sure tar-data-swapped tracks the swap
  139. ;; state correctly: the raw data is expected to be always larger than
  140. ;; the summary.
  141. (progn
  142. (cl-assert (or (= (buffer-size tar-data-buffer) (buffer-size))
  143. (eq tar-data-swapped
  144. (> (buffer-size tar-data-buffer) (buffer-size)))))
  145. tar-data-swapped)))
  146. (defun tar-swap-data ()
  147. "Swap buffer contents between current buffer and `tar-data-buffer'.
  148. Preserve the modified states of the buffers and set `buffer-swapped-with'."
  149. (let ((data-buffer-modified-p (buffer-modified-p tar-data-buffer))
  150. (current-buffer-modified-p (buffer-modified-p)))
  151. (buffer-swap-text tar-data-buffer)
  152. (setq tar-data-swapped (not tar-data-swapped))
  153. (restore-buffer-modified-p data-buffer-modified-p)
  154. (with-current-buffer tar-data-buffer
  155. (restore-buffer-modified-p current-buffer-modified-p))))
  156. ;;; down to business.
  157. (cl-defstruct (tar-header
  158. (:constructor nil)
  159. (:type vector)
  160. :named
  161. (:constructor
  162. make-tar-header (data-start name mode uid gid size date checksum
  163. link-type link-name magic uname gname dmaj dmin)))
  164. data-start name mode uid gid size date checksum link-type link-name
  165. magic uname gname dmaj dmin
  166. ;; Start of the header can be nil (meaning it's 512 bytes before data-start)
  167. ;; or a marker (in case the header uses LongLink thingies).
  168. header-start)
  169. (defconst tar-name-offset 0)
  170. (defconst tar-mode-offset (+ tar-name-offset 100))
  171. (defconst tar-uid-offset (+ tar-mode-offset 8))
  172. (defconst tar-gid-offset (+ tar-uid-offset 8))
  173. (defconst tar-size-offset (+ tar-gid-offset 8))
  174. (defconst tar-time-offset (+ tar-size-offset 12))
  175. (defconst tar-chk-offset (+ tar-time-offset 12))
  176. (defconst tar-linkp-offset (+ tar-chk-offset 8))
  177. (defconst tar-link-offset (+ tar-linkp-offset 1))
  178. ;;; GNU-tar specific slots.
  179. (defconst tar-magic-offset (+ tar-link-offset 100))
  180. (defconst tar-uname-offset (+ tar-magic-offset 8))
  181. (defconst tar-gname-offset (+ tar-uname-offset 32))
  182. (defconst tar-dmaj-offset (+ tar-gname-offset 32))
  183. (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
  184. (defconst tar-prefix-offset (+ tar-dmin-offset 8))
  185. (defconst tar-end-offset (+ tar-prefix-offset 155))
  186. (defun tar-roundup-512 (s)
  187. "Round S up to the next multiple of 512."
  188. (ash (ash (+ s 511) -9) 9))
  189. (defun tar-header-block-tokenize (pos coding)
  190. "Return a `tar-header' structure.
  191. This is a list of name, mode, uid, gid, size,
  192. write-date, checksum, link-type, and link-name."
  193. (if (> (+ pos 512) (point-max)) (error "Malformed Tar header"))
  194. (cl-assert (zerop (mod (- pos (point-min)) 512)))
  195. (cl-assert (not enable-multibyte-characters))
  196. (let ((string (buffer-substring pos (setq pos (+ pos 512)))))
  197. (when ;(some 'plusp string) ; <-- oops, massive cycle hog!
  198. (or (not (= 0 (aref string 0))) ; This will do.
  199. (not (= 0 (aref string 101))))
  200. (let* ((name-end tar-mode-offset)
  201. (link-end (1- tar-magic-offset))
  202. (uname-end (1- tar-gname-offset))
  203. (gname-end (1- tar-dmaj-offset))
  204. (link-p (aref string tar-linkp-offset))
  205. (magic-str (substring string tar-magic-offset
  206. ;; The magic string is actually 6bytes
  207. ;; of magic string plus 2bytes of version
  208. ;; which we here ignore.
  209. (- tar-uname-offset 2)))
  210. ;; The magic string is "ustar\0" for POSIX format, and
  211. ;; "ustar " for GNU Tar's format.
  212. (uname-valid-p (car (member magic-str '("ustar " "ustar\0"))))
  213. name linkname
  214. (nulsexp "[^\000]*\000"))
  215. (when (string-match nulsexp string tar-name-offset)
  216. (setq name-end (min name-end (1- (match-end 0)))))
  217. (when (string-match nulsexp string tar-link-offset)
  218. (setq link-end (min link-end (1- (match-end 0)))))
  219. (when (string-match nulsexp string tar-uname-offset)
  220. (setq uname-end (min uname-end (1- (match-end 0)))))
  221. (when (string-match nulsexp string tar-gname-offset)
  222. (setq gname-end (min gname-end (1- (match-end 0)))))
  223. (setq name (substring string tar-name-offset name-end)
  224. link-p (if (or (= link-p 0) (= link-p ?0))
  225. nil
  226. (- link-p ?0)))
  227. (setq linkname (substring string tar-link-offset link-end))
  228. (when (and (equal uname-valid-p "ustar\0")
  229. (string-match nulsexp string tar-prefix-offset)
  230. (> (match-end 0) (1+ tar-prefix-offset)))
  231. (setq name (concat (substring string tar-prefix-offset
  232. (1- (match-end 0)))
  233. "/" name)))
  234. (if (default-value 'enable-multibyte-characters)
  235. (setq name
  236. (decode-coding-string name coding)
  237. linkname
  238. (decode-coding-string linkname coding)))
  239. (if (and (null link-p) (string-match "/\\'" name))
  240. (setq link-p 5)) ; directory
  241. (if (and (equal name "././@LongLink")
  242. ;; Supposedly @LongLink is only used for GNUTAR
  243. ;; format (i.e. "ustar ") but some POSIX Tar files
  244. ;; (with "ustar\0") have been seen using it as well.
  245. (member magic-str '("ustar " "ustar\0")))
  246. ;; This is a GNU Tar long-file-name header.
  247. (let* ((size (tar-parse-octal-integer
  248. string tar-size-offset tar-time-offset))
  249. ;; -1 so as to strip the terminating 0 byte.
  250. (name (decode-coding-string
  251. (buffer-substring pos (+ pos size -1)) coding))
  252. (descriptor (tar-header-block-tokenize
  253. (+ pos (tar-roundup-512 size))
  254. coding)))
  255. (cond
  256. ((eq link-p (- ?L ?0)) ;GNUTYPE_LONGNAME.
  257. (setf (tar-header-name descriptor) name))
  258. ((eq link-p (- ?K ?0)) ;GNUTYPE_LONGLINK.
  259. (setf (tar-header-link-name descriptor) name))
  260. (t
  261. (message "Unrecognized GNU Tar @LongLink format")))
  262. (setf (tar-header-header-start descriptor)
  263. (copy-marker (- pos 512) t))
  264. descriptor)
  265. (make-tar-header
  266. (copy-marker pos nil)
  267. name
  268. (tar-parse-octal-integer string tar-mode-offset tar-uid-offset)
  269. (tar-parse-octal-integer string tar-uid-offset tar-gid-offset)
  270. (tar-parse-octal-integer string tar-gid-offset tar-size-offset)
  271. (tar-parse-octal-integer string tar-size-offset tar-time-offset)
  272. (tar-parse-octal-long-integer string tar-time-offset tar-chk-offset)
  273. (tar-parse-octal-integer string tar-chk-offset tar-linkp-offset)
  274. link-p
  275. linkname
  276. uname-valid-p
  277. (when uname-valid-p
  278. (decode-coding-string
  279. (substring string tar-uname-offset uname-end) coding))
  280. (when uname-valid-p
  281. (decode-coding-string
  282. (substring string tar-gname-offset gname-end) coding))
  283. (tar-parse-octal-integer string tar-dmaj-offset tar-dmin-offset)
  284. (tar-parse-octal-integer string tar-dmin-offset tar-prefix-offset)
  285. ))))))
  286. ;; Pseudo-field.
  287. (defun tar-header-data-end (descriptor)
  288. (let* ((data-start (tar-header-data-start descriptor))
  289. (link-type (tar-header-link-type descriptor))
  290. (size (tar-header-size descriptor)))
  291. (+ data-start
  292. ;; Ignore size for files of type 1-6
  293. (if (and (not (memq link-type '(1 2 3 4 5 6))) (> size 0))
  294. (tar-roundup-512 size)
  295. 0))))
  296. (defun tar-parse-octal-integer (string &optional start end)
  297. (if (null start) (setq start 0))
  298. (if (null end) (setq end (length string)))
  299. (if (= (aref string start) 0)
  300. 0
  301. (let ((n 0))
  302. (while (< start end)
  303. (setq n (if (< (aref string start) ?0) n
  304. (+ (* n 8) (- (aref string start) ?0)))
  305. start (1+ start)))
  306. n)))
  307. (defun tar-parse-octal-long-integer (string &optional start end)
  308. (if (null start) (setq start 0))
  309. (if (null end) (setq end (length string)))
  310. (if (= (aref string start) 0)
  311. (list 0 0)
  312. (let ((lo 0)
  313. (hi 0))
  314. (while (< start end)
  315. (if (>= (aref string start) ?0)
  316. (setq lo (+ (* lo 8) (- (aref string start) ?0))
  317. hi (+ (* hi 8) (ash lo -16))
  318. lo (logand lo 65535)))
  319. (setq start (1+ start)))
  320. (list hi lo))))
  321. (defun tar-parse-octal-integer-safe (string)
  322. (if (zerop (length string)) (error "empty string"))
  323. (mapc (lambda (c)
  324. (if (or (< c ?0) (> c ?7))
  325. (error "`%c' is not an octal digit" c)))
  326. string)
  327. (tar-parse-octal-integer string))
  328. (defun tar-new-regular-file-header (filename &optional size time)
  329. "Return a Tar header for a regular file.
  330. The header will lack a proper checksum; use `tar-header-block-checksum'
  331. to compute one, or request `tar-header-serialize' to do that.
  332. Other tar-mode facilities may also require the data-start header
  333. field to be set to a valid value.
  334. If SIZE is not given or nil, it defaults to 0.
  335. If TIME is not given or nil, assume now."
  336. (make-tar-header
  337. nil
  338. filename
  339. #o644 0 0 (or size 0)
  340. (or time (current-time))
  341. nil ; checksum
  342. nil nil
  343. nil nil nil nil nil))
  344. (defun tar--pad-to (pos)
  345. (make-string (+ pos (- (point)) (point-min)) 0))
  346. (defun tar--put-at (pos val &optional fmt mask)
  347. (when val
  348. (insert (tar--pad-to pos)
  349. (if fmt
  350. (format fmt (if mask (logand mask val) val))
  351. val))))
  352. (defun tar-header-serialize (header &optional update-checksum)
  353. "Return the serialization of a Tar HEADER as a string.
  354. This function calls `tar-header-block-check-checksum' to ensure the
  355. checksum is correct.
  356. If UPDATE-CHECKSUM is non-nil, update HEADER with the newly-computed
  357. checksum before doing the check."
  358. (with-temp-buffer
  359. (set-buffer-multibyte nil)
  360. (let ((encoded-name
  361. (encode-coding-string (tar-header-name header)
  362. tar-file-name-coding-system)))
  363. (unless (< (length encoded-name) 99)
  364. ;; FIXME: Implement it.
  365. (error "Long file name support is not implemented"))
  366. (insert encoded-name))
  367. (tar--put-at tar-mode-offset (tar-header-mode header) "%6o\0 " #o777777)
  368. (tar--put-at tar-uid-offset (tar-header-uid header) "%6o\0 " #o777777)
  369. (tar--put-at tar-gid-offset (tar-header-gid header) "%6o\0 " #o777777)
  370. (tar--put-at tar-size-offset (tar-header-size header) "%11o ")
  371. (insert (tar--pad-to tar-time-offset)
  372. (tar-octal-time (tar-header-date header))
  373. " ")
  374. ;; Omit tar-header-checksum (tar-chk-offset) for now.
  375. (tar--put-at tar-linkp-offset (tar-header-link-type header))
  376. (tar--put-at tar-link-offset (tar-header-link-name header))
  377. (when (tar-header-magic header)
  378. (tar--put-at tar-magic-offset (tar-header-magic header))
  379. (tar--put-at tar-uname-offset (tar-header-uname header))
  380. (tar--put-at tar-gname-offset (tar-header-gname header))
  381. (tar--put-at tar-dmaj-offset (tar-header-dmaj header) "%7o\0" #o7777777)
  382. (tar--put-at tar-dmin-offset (tar-header-dmin header) "%7o\0" #o7777777))
  383. (tar--put-at 512 "")
  384. (let ((ck (tar-header-block-checksum (buffer-string))))
  385. (goto-char (+ (point-min) tar-chk-offset))
  386. (delete-char 8)
  387. (insert (format "%6o\0 " ck))
  388. (when update-checksum
  389. (setf (tar-header-checksum header) ck))
  390. (tar-header-block-check-checksum (buffer-string)
  391. (tar-header-checksum header)
  392. (tar-header-name header)))
  393. ;; .
  394. (buffer-string)))
  395. (defun tar-header-block-checksum (string)
  396. "Compute and return a tar-acceptable checksum for this block."
  397. (cl-assert (not (multibyte-string-p string)))
  398. (let* ((chk-field-start tar-chk-offset)
  399. (chk-field-end (+ chk-field-start 8))
  400. (sum 0)
  401. (i 0))
  402. ;; Add up all of the characters except the ones in the checksum field.
  403. ;; Add that field as if it were filled with spaces.
  404. (while (< i chk-field-start)
  405. (setq sum (+ sum (aref string i))
  406. i (1+ i)))
  407. (setq i chk-field-end)
  408. (while (< i 512)
  409. (setq sum (+ sum (aref string i))
  410. i (1+ i)))
  411. (+ sum (* 32 8))))
  412. (defun tar-header-block-check-checksum (hblock desired-checksum file-name)
  413. "Beep and print a warning if the checksum doesn't match."
  414. (if (not (= desired-checksum (tar-header-block-checksum hblock)))
  415. (progn (beep) (message "Invalid checksum for file %s!" file-name))))
  416. (defun tar-clip-time-string (time)
  417. (let ((str (current-time-string time)))
  418. (concat " " (substring str 4 16) (format-time-string " %Y" time))))
  419. (defun tar-grind-file-mode (mode)
  420. "Construct a `-rw--r--r--' string indicating MODE.
  421. MODE should be an integer which is a file mode value."
  422. (string
  423. (if (zerop (logand 256 mode)) ?- ?r)
  424. (if (zerop (logand 128 mode)) ?- ?w)
  425. (if (zerop (logand 2048 mode))
  426. (if (zerop (logand 64 mode)) ?- ?x)
  427. (if (zerop (logand 64 mode)) ?S ?s))
  428. (if (zerop (logand 32 mode)) ?- ?r)
  429. (if (zerop (logand 16 mode)) ?- ?w)
  430. (if (zerop (logand 1024 mode))
  431. (if (zerop (logand 8 mode)) ?- ?x)
  432. (if (zerop (logand 8 mode)) ?S ?s))
  433. (if (zerop (logand 4 mode)) ?- ?r)
  434. (if (zerop (logand 2 mode)) ?- ?w)
  435. (if (zerop (logand 512 mode))
  436. (if (zerop (logand 1 mode)) ?- ?x)
  437. (if (zerop (logand 1 mode)) ?T ?t))))
  438. (defun tar-header-block-summarize (tar-hblock &optional mod-p)
  439. "Return a line similar to the output of `tar -vtf'."
  440. (let ((name (tar-header-name tar-hblock))
  441. (mode (tar-header-mode tar-hblock))
  442. (uid (tar-header-uid tar-hblock))
  443. (gid (tar-header-gid tar-hblock))
  444. (uname (tar-header-uname tar-hblock))
  445. (gname (tar-header-gname tar-hblock))
  446. (size (tar-header-size tar-hblock))
  447. (time (tar-header-date tar-hblock))
  448. ;; (ck (tar-header-checksum tar-hblock))
  449. (type (tar-header-link-type tar-hblock))
  450. (link-name (tar-header-link-name tar-hblock)))
  451. (format "%c%c%s %7s/%-7s %7s%s %s%s"
  452. (if mod-p ?* ? )
  453. (cond ((or (eq type nil) (eq type 0)) ?-)
  454. ((eq type 1) ?h) ; link
  455. ((eq type 2) ?l) ; symlink
  456. ((eq type 3) ?c) ; char special
  457. ((eq type 4) ?b) ; block special
  458. ((eq type 5) ?d) ; directory
  459. ((eq type 6) ?p) ; FIFO/pipe
  460. ((eq type 20) ?*) ; directory listing
  461. ((eq type 28) ?L) ; next has longname
  462. ((eq type 29) ?M) ; multivolume continuation
  463. ((eq type 35) ?S) ; sparse
  464. ((eq type 38) ?V) ; volume header
  465. ((eq type 55) ?H) ; pax global extended header
  466. ((eq type 72) ?X) ; pax extended header
  467. (t ?\s)
  468. )
  469. (tar-grind-file-mode mode)
  470. (if (= 0 (length uname)) uid uname)
  471. (if (= 0 (length gname)) gid gname)
  472. size
  473. (if tar-mode-show-date (tar-clip-time-string time) "")
  474. (propertize name
  475. 'mouse-face 'highlight
  476. 'help-echo "mouse-2: extract this file into a buffer")
  477. (if (or (eq type 1) (eq type 2))
  478. (concat (if (= type 1) " ==> " " --> ") link-name)
  479. ""))))
  480. (defun tar-untar-buffer ()
  481. "Extract all archive members in the tar-file into the current directory."
  482. (interactive)
  483. ;; FIXME: make it work even if we're not in tar-mode.
  484. (let ((descriptors tar-parse-info)) ;Read the var in its buffer.
  485. (with-current-buffer
  486. (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
  487. (set-buffer-multibyte nil) ;Hopefully, a no-op.
  488. (dolist (descriptor descriptors)
  489. (let* ((name (tar-header-name descriptor))
  490. (dir (if (eq (tar-header-link-type descriptor) 5)
  491. name
  492. (file-name-directory name)))
  493. (start (tar-header-data-start descriptor))
  494. (end (+ start (tar-header-size descriptor))))
  495. (unless (file-directory-p name)
  496. (message "Extracting %s" name)
  497. (if (and dir (not (file-exists-p dir)))
  498. (make-directory dir t))
  499. (unless (file-directory-p name)
  500. (let ((coding-system-for-write 'no-conversion))
  501. (write-region start end name)))
  502. (set-file-modes name (tar-header-mode descriptor))))))))
  503. (defun tar-summarize-buffer ()
  504. "Parse the contents of the tar file in the current buffer."
  505. (cl-assert (tar-data-swapped-p))
  506. (let* ((modified (buffer-modified-p))
  507. (result '())
  508. (pos (point-min))
  509. (coding tar-file-name-coding-system)
  510. (progress-reporter
  511. (with-current-buffer tar-data-buffer
  512. (make-progress-reporter "Parsing tar file..."
  513. (point-min) (point-max))))
  514. descriptor)
  515. (with-current-buffer tar-data-buffer
  516. (while (and (< pos (point-max))
  517. (setq descriptor (tar-header-block-tokenize pos coding)))
  518. (let ((size (tar-header-size descriptor)))
  519. (if (< size 0)
  520. (error "%s has size %s - corrupted"
  521. (tar-header-name descriptor) size)))
  522. ;;
  523. ;; This is just too slow. Don't really need it anyway....
  524. ;;(tar-header-block-check-checksum
  525. ;; hblock (tar-header-block-checksum hblock)
  526. ;; (tar-header-name descriptor))
  527. (push descriptor result)
  528. (setq pos (tar-header-data-end descriptor))
  529. (progress-reporter-update progress-reporter pos)))
  530. (set (make-local-variable 'tar-parse-info) (nreverse result))
  531. ;; A tar file should end with a block or two of nulls,
  532. ;; but let's not get a fatal error if it doesn't.
  533. (if (null descriptor)
  534. (progress-reporter-done progress-reporter)
  535. (message "Warning: premature EOF parsing tar file"))
  536. (goto-char (point-min))
  537. (let ((buffer-file-truename nil) ; avoid changing dir mtime by lock_file
  538. (inhibit-read-only t)
  539. (total-summaries
  540. (mapconcat 'tar-header-block-summarize tar-parse-info "\n")))
  541. (insert total-summaries "\n")
  542. (goto-char (point-min))
  543. (restore-buffer-modified-p modified))))
  544. (defvar tar-mode-map
  545. (let ((map (make-keymap)))
  546. (suppress-keymap map)
  547. (define-key map " " 'tar-next-line)
  548. (define-key map "C" 'tar-copy)
  549. (define-key map "d" 'tar-flag-deleted)
  550. (define-key map "\^D" 'tar-flag-deleted)
  551. (define-key map "e" 'tar-extract)
  552. (define-key map "f" 'tar-extract)
  553. (define-key map "\C-m" 'tar-extract)
  554. (define-key map [mouse-2] 'tar-mouse-extract)
  555. (define-key map "g" 'revert-buffer)
  556. (define-key map "n" 'tar-next-line)
  557. (define-key map "\^N" 'tar-next-line)
  558. (define-key map [down] 'tar-next-line)
  559. (define-key map "o" 'tar-extract-other-window)
  560. (define-key map "p" 'tar-previous-line)
  561. (define-key map "\^P" 'tar-previous-line)
  562. (define-key map [up] 'tar-previous-line)
  563. (define-key map "I" 'tar-new-entry)
  564. (define-key map "R" 'tar-rename-entry)
  565. (define-key map "u" 'tar-unflag)
  566. (define-key map "v" 'tar-view)
  567. (define-key map "w" 'woman-tar-extract-file)
  568. (define-key map "x" 'tar-expunge)
  569. (define-key map "\177" 'tar-unflag-backwards)
  570. (define-key map "E" 'tar-extract-other-window)
  571. (define-key map "M" 'tar-chmod-entry)
  572. (define-key map "G" 'tar-chgrp-entry)
  573. (define-key map "O" 'tar-chown-entry)
  574. ;; Let mouse-1 follow the link.
  575. (define-key map [follow-link] 'mouse-face)
  576. ;; Make menu bar items.
  577. ;; Get rid of the Edit menu bar item to save space.
  578. (define-key map [menu-bar edit] 'undefined)
  579. (define-key map [menu-bar immediate]
  580. (cons "Immediate" (make-sparse-keymap "Immediate")))
  581. (define-key map [menu-bar immediate woman]
  582. '("Read Man Page (WoMan)" . woman-tar-extract-file))
  583. (define-key map [menu-bar immediate view]
  584. '("View This File" . tar-view))
  585. (define-key map [menu-bar immediate display]
  586. '("Display in Other Window" . tar-display-other-window))
  587. (define-key map [menu-bar immediate find-file-other-window]
  588. '("Find in Other Window" . tar-extract-other-window))
  589. (define-key map [menu-bar immediate find-file]
  590. '("Find This File" . tar-extract))
  591. (define-key map [menu-bar mark]
  592. (cons "Mark" (make-sparse-keymap "Mark")))
  593. (define-key map [menu-bar mark unmark-all]
  594. '("Unmark All" . tar-clear-modification-flags))
  595. (define-key map [menu-bar mark deletion]
  596. '("Flag" . tar-flag-deleted))
  597. (define-key map [menu-bar mark unmark]
  598. '("Unflag" . tar-unflag))
  599. (define-key map [menu-bar operate]
  600. (cons "Operate" (make-sparse-keymap "Operate")))
  601. (define-key map [menu-bar operate chown]
  602. '("Change Owner..." . tar-chown-entry))
  603. (define-key map [menu-bar operate chgrp]
  604. '("Change Group..." . tar-chgrp-entry))
  605. (define-key map [menu-bar operate chmod]
  606. '("Change Mode..." . tar-chmod-entry))
  607. (define-key map [menu-bar operate rename]
  608. '("Rename to..." . tar-rename-entry))
  609. (define-key map [menu-bar operate copy]
  610. '("Copy to..." . tar-copy))
  611. (define-key map [menu-bar operate expunge]
  612. '("Expunge Marked Files" . tar-expunge))
  613. map)
  614. "Local keymap for Tar mode listings.")
  615. ;; tar mode is suitable only for specially formatted data.
  616. (put 'tar-mode 'mode-class 'special)
  617. (put 'tar-subfile-mode 'mode-class 'special)
  618. (defun tar-change-major-mode-hook ()
  619. ;; Bring the actual Tar data back into the main buffer.
  620. (when (tar-data-swapped-p) (tar-swap-data))
  621. ;; Throw away the summary.
  622. (when (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
  623. (defun tar-mode-kill-buffer-hook ()
  624. (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer)))
  625. ;;;###autoload
  626. (define-derived-mode tar-mode special-mode "Tar"
  627. "Major mode for viewing a tar file as a dired-like listing of its contents.
  628. You can move around using the usual cursor motion commands.
  629. Letters no longer insert themselves.
  630. Type `e' to pull a file out of the tar file and into its own buffer;
  631. or click mouse-2 on the file's line in the Tar mode buffer.
  632. Type `c' to copy an entry from the tar file into another file on disk.
  633. If you edit a sub-file of this archive (as with the `e' command) and
  634. save it with \\[save-buffer], the contents of that buffer will be
  635. saved back into the tar-file buffer; in this way you can edit a file
  636. inside of a tar archive without extracting it and re-archiving it.
  637. See also: variables `tar-update-datestamp' and `tar-anal-blocksize'.
  638. \\{tar-mode-map}"
  639. (and buffer-file-name
  640. (file-writable-p buffer-file-name)
  641. (setq buffer-read-only nil)) ; undo what `special-mode' did
  642. (make-local-variable 'tar-parse-info)
  643. (set (make-local-variable 'require-final-newline) nil) ; binary data, dude...
  644. (set (make-local-variable 'local-enable-local-variables) nil)
  645. (set (make-local-variable 'next-line-add-newlines) nil)
  646. (set (make-local-variable 'tar-file-name-coding-system)
  647. (or file-name-coding-system
  648. default-file-name-coding-system
  649. locale-coding-system))
  650. ;; Prevent loss of data when saving the file.
  651. (set (make-local-variable 'file-precious-flag) t)
  652. (buffer-disable-undo)
  653. (widen)
  654. ;; Now move the Tar data into an auxiliary buffer, so we can use the main
  655. ;; buffer for the summary.
  656. (cl-assert (not (tar-data-swapped-p)))
  657. (set (make-local-variable 'revert-buffer-function) 'tar-mode-revert)
  658. ;; We started using write-contents-functions, but this hook is not
  659. ;; used during auto-save, so we now use
  660. ;; write-region-annotate-functions which hooks at a lower-level.
  661. (add-hook 'write-region-annotate-functions 'tar-write-region-annotate nil t)
  662. (add-hook 'kill-buffer-hook 'tar-mode-kill-buffer-hook nil t)
  663. (add-hook 'change-major-mode-hook 'tar-change-major-mode-hook nil t)
  664. ;; Tar data is made of bytes, not chars.
  665. (set-buffer-multibyte nil) ;Hopefully a no-op.
  666. (set (make-local-variable 'tar-data-buffer)
  667. (generate-new-buffer (format " *tar-data %s*"
  668. (file-name-nondirectory
  669. (or buffer-file-name (buffer-name))))))
  670. (condition-case err
  671. (progn
  672. (tar-swap-data)
  673. (tar-summarize-buffer)
  674. (tar-next-line 0))
  675. (error
  676. ;; If summarizing caused an error, then maybe the buffer doesn't contain
  677. ;; tar data. Rather than show a mysterious empty buffer, let's
  678. ;; revert to fundamental-mode.
  679. (fundamental-mode)
  680. (signal (car err) (cdr err)))))
  681. (autoload 'woman-tar-extract-file "woman"
  682. "In tar mode, run the WoMan man-page browser on this file." t)
  683. (define-minor-mode tar-subfile-mode
  684. "Minor mode for editing an element of a tar-file.
  685. With a prefix argument ARG, enable the mode if ARG is positive,
  686. and disable it otherwise. If called from Lisp, enable the mode
  687. if ARG is omitted or nil. This mode arranges for \"saving\" this
  688. buffer to write the data into the tar-file buffer that it came
  689. from. The changes will actually appear on disk when you save the
  690. tar-file's buffer."
  691. ;; Don't do this, because it is redundant and wastes mode line space.
  692. ;; :lighter " TarFile"
  693. nil nil nil
  694. (or (and (boundp 'tar-superior-buffer) tar-superior-buffer)
  695. (error "This buffer is not an element of a tar file"))
  696. (cond (tar-subfile-mode
  697. (add-hook 'write-file-functions 'tar-subfile-save-buffer nil t)
  698. ;; turn off auto-save.
  699. (auto-save-mode -1)
  700. (setq buffer-auto-save-file-name nil))
  701. (t
  702. (remove-hook 'write-file-functions 'tar-subfile-save-buffer t))))
  703. ;; Revert the buffer and recompute the dired-like listing.
  704. (defun tar-mode-revert (&optional no-auto-save no-confirm)
  705. (unwind-protect
  706. (let ((revert-buffer-function nil))
  707. (if (tar-data-swapped-p) (tar-swap-data))
  708. ;; FIXME: If we ask for confirmation, the user will be temporarily
  709. ;; looking at the raw data.
  710. (revert-buffer no-auto-save no-confirm 'preserve-modes)
  711. ;; Recompute the summary.
  712. (if (buffer-live-p tar-data-buffer) (kill-buffer tar-data-buffer))
  713. (tar-mode))
  714. (unless (tar-data-swapped-p) (tar-swap-data))))
  715. (defun tar-next-line (arg)
  716. "Move cursor vertically down ARG lines and to the start of the filename."
  717. (interactive "p")
  718. (forward-line arg)
  719. (goto-char (or (next-single-property-change (point) 'mouse-face) (point))))
  720. (defun tar-previous-line (arg)
  721. "Move cursor vertically up ARG lines and to the start of the filename."
  722. (interactive "p")
  723. (tar-next-line (- arg)))
  724. (defun tar-current-position ()
  725. "Return the `tar-parse-info' index for the current line."
  726. (count-lines (point-min) (line-beginning-position)))
  727. (defun tar-current-descriptor (&optional noerror)
  728. "Return the tar-descriptor of the current line, or signals an error."
  729. ;; I wish lines had plists, like in ZMACS...
  730. (or (nth (tar-current-position)
  731. tar-parse-info)
  732. (if noerror
  733. nil
  734. (error "This line does not describe a tar-file entry"))))
  735. (defun tar--check-descriptor (descriptor)
  736. (let ((link-p (tar-header-link-type descriptor)))
  737. (if link-p
  738. (error "This is %s, not a real file"
  739. (cond ((eq link-p 5) "a directory")
  740. ((eq link-p 20) "a tar directory header")
  741. ((eq link-p 28) "a next has longname")
  742. ((eq link-p 29) "a multivolume-continuation")
  743. ((eq link-p 35) "a sparse entry")
  744. ((eq link-p 38) "a volume header")
  745. ((eq link-p 55) "a pax global extended header")
  746. ((eq link-p 72) "a pax extended header")
  747. (t "a link"))))))
  748. (defun tar-get-descriptor ()
  749. (let* ((descriptor (tar-current-descriptor))
  750. (size (tar-header-size descriptor)))
  751. (tar--check-descriptor descriptor)
  752. (if (zerop size) (message "This is a zero-length file"))
  753. descriptor))
  754. (defun tar-get-file-descriptor (file)
  755. ;; Used by package.el.
  756. (let ((desc ()))
  757. (dolist (hdr tar-parse-info)
  758. (when (equal file (tar-header-name hdr))
  759. (setq desc hdr)))
  760. (tar--check-descriptor desc)
  761. desc))
  762. (defun tar-mouse-extract (event)
  763. "Extract a file whose tar directory line you click on."
  764. (interactive "e")
  765. (with-current-buffer (window-buffer (posn-window (event-end event)))
  766. (save-excursion
  767. (goto-char (posn-point (event-end event)))
  768. ;; Just make sure this doesn't get an error.
  769. (tar-get-descriptor)))
  770. (select-window (posn-window (event-end event)))
  771. (goto-char (posn-point (event-end event)))
  772. (tar-extract))
  773. (defun tar-file-name-handler (op &rest args)
  774. "Helper function for `tar-extract'."
  775. (or (eq op 'file-exists-p)
  776. (let ((file-name-handler-alist nil))
  777. (apply op args))))
  778. (defun tar--extract (descriptor)
  779. "Extract this entry of the tar file into its own buffer."
  780. (let* ((name (tar-header-name descriptor))
  781. (size (tar-header-size descriptor))
  782. (start (tar-header-data-start descriptor))
  783. (end (+ start size))
  784. (tarname (buffer-name))
  785. (bufname (concat (file-name-nondirectory name)
  786. " ("
  787. tarname
  788. ")"))
  789. (buffer (generate-new-buffer bufname)))
  790. (with-current-buffer tar-data-buffer
  791. (let (coding)
  792. (narrow-to-region start end)
  793. (goto-char start)
  794. (setq coding (or coding-system-for-read
  795. (and set-auto-coding-function
  796. (funcall set-auto-coding-function
  797. name (- end start)))
  798. ;; The following binding causes
  799. ;; find-buffer-file-type-coding-system
  800. ;; (defined on dos-w32.el) to act as if
  801. ;; the file being extracted existed, so
  802. ;; that the file's contents' encoding and
  803. ;; EOL format are auto-detected.
  804. (let ((file-name-handler-alist
  805. '(("" . tar-file-name-handler))))
  806. (car (find-operation-coding-system
  807. 'insert-file-contents
  808. (cons name (current-buffer)) t)))))
  809. (if (or (not coding)
  810. (eq (coding-system-type coding) 'undecided))
  811. (setq coding (detect-coding-region start end t)))
  812. (if (and (default-value 'enable-multibyte-characters)
  813. (coding-system-get coding :for-unibyte))
  814. (with-current-buffer buffer
  815. (set-buffer-multibyte nil)))
  816. (widen)
  817. (with-current-buffer buffer
  818. (setq buffer-undo-list t))
  819. (decode-coding-region start end coding buffer)
  820. (with-current-buffer buffer
  821. (setq buffer-undo-list nil))))
  822. buffer))
  823. (defun tar-extract (&optional other-window-p)
  824. "In Tar mode, extract this entry of the tar file into its own buffer."
  825. (interactive)
  826. (let* ((view-p (eq other-window-p 'view))
  827. (descriptor (tar-get-descriptor))
  828. (name (tar-header-name descriptor))
  829. (tar-buffer (current-buffer))
  830. (tarname (buffer-name))
  831. (read-only-p (or buffer-read-only view-p))
  832. (new-buffer-file-name (expand-file-name
  833. ;; `:' is not allowed on Windows
  834. (concat tarname "!"
  835. (if (string-match "/" name)
  836. name
  837. ;; Make sure `name' contains a /
  838. ;; so set-auto-mode doesn't try
  839. ;; to look at `tarname' for hints.
  840. (concat "./" name)))))
  841. (buffer (get-file-buffer new-buffer-file-name))
  842. (just-created nil))
  843. (unless buffer
  844. (setq buffer (tar--extract descriptor))
  845. (setq just-created t)
  846. (with-current-buffer buffer
  847. (goto-char (point-min))
  848. (setq buffer-file-name new-buffer-file-name)
  849. (setq buffer-file-truename
  850. (abbreviate-file-name buffer-file-name))
  851. ;; Force buffer-file-coding-system to what
  852. ;; decode-coding-region actually used.
  853. (set-buffer-file-coding-system last-coding-system-used t)
  854. ;; Set the default-directory to the dir of the
  855. ;; superior buffer.
  856. (setq default-directory
  857. (with-current-buffer tar-buffer
  858. default-directory))
  859. (set-buffer-modified-p nil)
  860. (normal-mode) ; pick a mode.
  861. (set (make-local-variable 'tar-superior-buffer) tar-buffer)
  862. (set (make-local-variable 'tar-superior-descriptor) descriptor)
  863. (setq buffer-read-only read-only-p)
  864. (tar-subfile-mode 1)))
  865. (cond
  866. (view-p
  867. (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
  868. ((eq other-window-p 'display) (display-buffer buffer))
  869. (other-window-p (switch-to-buffer-other-window buffer))
  870. (t (switch-to-buffer buffer)))))
  871. (defun tar-extract-other-window ()
  872. "In Tar mode, find this entry of the tar file in another window."
  873. (interactive)
  874. (tar-extract t))
  875. (defun tar-display-other-window ()
  876. "In Tar mode, display this entry of the tar file in another window."
  877. (interactive)
  878. (tar-extract 'display))
  879. (defun tar-view ()
  880. "In Tar mode, view the tar file entry on this line."
  881. (interactive)
  882. (tar-extract 'view))
  883. (defun tar-read-file-name (&optional prompt)
  884. "Read a file name with this line's entry as the default."
  885. (or prompt (setq prompt "Copy to: "))
  886. (let* ((default-file (expand-file-name
  887. (tar-header-name (tar-current-descriptor))))
  888. (target (expand-file-name
  889. (read-file-name prompt
  890. (file-name-directory default-file)
  891. default-file nil))))
  892. (if (or (string= "" (file-name-nondirectory target))
  893. (file-directory-p target))
  894. (setq target (concat (if (string-match "/$" target)
  895. (substring target 0 (1- (match-end 0)))
  896. target)
  897. "/"
  898. (file-name-nondirectory default-file))))
  899. target))
  900. (defun tar-copy (&optional to-file)
  901. "In Tar mode, extract this entry of the tar file into a file on disk.
  902. If TO-FILE is not supplied, it is prompted for, defaulting to the name of
  903. the current tar-entry."
  904. (interactive (list (tar-read-file-name)))
  905. (let* ((descriptor (tar-get-descriptor))
  906. (name (tar-header-name descriptor))
  907. (size (tar-header-size descriptor))
  908. (start (tar-header-data-start descriptor))
  909. (end (+ start size))
  910. (inhibit-file-name-handlers inhibit-file-name-handlers)
  911. (inhibit-file-name-operation inhibit-file-name-operation))
  912. (with-current-buffer
  913. (if (tar-data-swapped-p) tar-data-buffer (current-buffer))
  914. ;; Inhibit compressing a subfile again if *both* name and
  915. ;; to-file are handled by jka-compr
  916. (if (and (eq (find-file-name-handler name 'write-region)
  917. 'jka-compr-handler)
  918. (eq (find-file-name-handler to-file 'write-region)
  919. 'jka-compr-handler))
  920. (setq inhibit-file-name-handlers
  921. (cons 'jka-compr-handler
  922. (and (eq inhibit-file-name-operation 'write-region)
  923. inhibit-file-name-handlers))
  924. inhibit-file-name-operation 'write-region))
  925. (let ((coding-system-for-write 'no-conversion))
  926. (write-region start end to-file nil nil nil t)))
  927. (message "Copied tar entry %s to %s" name to-file)))
  928. (defun tar-new-entry (filename &optional index)
  929. "Insert a new empty regular file before point."
  930. (interactive "*sFile name: ")
  931. (let* ((buffer (current-buffer))
  932. (index (or index (tar-current-position)))
  933. (d-list (and (not (zerop index))
  934. (nthcdr (+ -1 index) tar-parse-info)))
  935. (pos (if d-list
  936. (tar-header-data-end (car d-list))
  937. (point-min)))
  938. (new-descriptor
  939. (tar-new-regular-file-header filename)))
  940. ;; Update the data buffer; fill the missing descriptor fields.
  941. (with-current-buffer tar-data-buffer
  942. (goto-char pos)
  943. (insert (tar-header-serialize new-descriptor t))
  944. (setf (tar-header-data-start new-descriptor)
  945. (copy-marker (point) nil)))
  946. ;; Update tar-parse-info.
  947. (if d-list
  948. (setcdr d-list (cons new-descriptor (cdr d-list)))
  949. (setq tar-parse-info (cons new-descriptor tar-parse-info)))
  950. ;; Update the listing buffer.
  951. (save-excursion
  952. (goto-char (point-min))
  953. (forward-line index)
  954. (let ((inhibit-read-only t))
  955. (insert (tar-header-block-summarize new-descriptor) ?\n)))
  956. ;; .
  957. index))
  958. (defun tar-flag-deleted (p &optional unflag)
  959. "In Tar mode, mark this sub-file to be deleted from the tar file.
  960. With a prefix argument, mark that many files."
  961. (interactive "p")
  962. (beginning-of-line)
  963. (dotimes (i (abs p))
  964. (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
  965. (progn
  966. (delete-char 1)
  967. (insert (if unflag " " "D"))))
  968. (forward-line (if (< p 0) -1 1)))
  969. (if (eobp) nil (forward-char 36)))
  970. (defun tar-unflag (p)
  971. "In Tar mode, un-mark this sub-file if it is marked to be deleted.
  972. With a prefix argument, un-mark that many files forward."
  973. (interactive "p")
  974. (tar-flag-deleted p t))
  975. (defun tar-unflag-backwards (p)
  976. "In Tar mode, un-mark this sub-file if it is marked to be deleted.
  977. With a prefix argument, un-mark that many files backward."
  978. (interactive "p")
  979. (tar-flag-deleted (- p) t))
  980. (defun tar-expunge-internal ()
  981. "Expunge the tar-entry specified by the current line."
  982. (let ((descriptor (tar-current-descriptor)))
  983. ;;
  984. ;; delete the current line...
  985. (delete-region (line-beginning-position) (line-beginning-position 2))
  986. ;;
  987. ;; delete the data pointer...
  988. (setq tar-parse-info (delq descriptor tar-parse-info))
  989. ;;
  990. ;; delete the data from inside the file...
  991. (with-current-buffer tar-data-buffer
  992. (delete-region (or (tar-header-header-start descriptor)
  993. (- (tar-header-data-start descriptor) 512))
  994. (tar-header-data-end descriptor)))))
  995. (defun tar-expunge (&optional noconfirm)
  996. "In Tar mode, delete all the archived files flagged for deletion.
  997. This does not modify the disk image; you must save the tar file itself
  998. for this to be permanent."
  999. (interactive)
  1000. (if (or noconfirm
  1001. (y-or-n-p "Expunge files marked for deletion? "))
  1002. (let ((n 0))
  1003. (save-excursion
  1004. (goto-char (point-min))
  1005. (while (not (eobp))
  1006. (if (looking-at "D")
  1007. (progn (tar-expunge-internal)
  1008. (setq n (1+ n)))
  1009. (forward-line 1)))
  1010. ;; after doing the deletions, add any padding that may be necessary.
  1011. (tar-pad-to-blocksize))
  1012. (if (zerop n)
  1013. (message "Nothing to expunge.")
  1014. (message "%s files expunged. Be sure to save this buffer." n)))))
  1015. (defun tar-clear-modification-flags ()
  1016. "Remove the stars at the beginning of each line."
  1017. (interactive)
  1018. (save-excursion
  1019. (goto-char (point-min))
  1020. (while (not (eobp))
  1021. (if (not (eq (following-char) ?\s))
  1022. (progn (delete-char 1) (insert " ")))
  1023. (forward-line 1))))
  1024. (defun tar-chown-entry (new-uid)
  1025. "Change the user-id associated with this entry in the tar file.
  1026. If this tar file was written by GNU tar, then you will be able to edit
  1027. the user id as a string; otherwise, you must edit it as a number.
  1028. You can force editing as a number by calling this with a prefix arg.
  1029. This does not modify the disk image; you must save the tar file itself
  1030. for this to be permanent."
  1031. (interactive
  1032. (list
  1033. (let ((descriptor (tar-current-descriptor)))
  1034. (if (or current-prefix-arg
  1035. (not (tar-header-magic descriptor)))
  1036. (read-number
  1037. "New UID number: "
  1038. (format "%s" (tar-header-uid descriptor)))
  1039. (read-string "New UID string: " (tar-header-uname descriptor))))))
  1040. (cond ((stringp new-uid)
  1041. (setf (tar-header-uname (tar-current-descriptor)) new-uid)
  1042. (tar-alter-one-field tar-uname-offset
  1043. (concat (encode-coding-string
  1044. new-uid tar-file-name-coding-system)
  1045. "\000")))
  1046. (t
  1047. (setf (tar-header-uid (tar-current-descriptor)) new-uid)
  1048. (tar-alter-one-field tar-uid-offset
  1049. (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
  1050. (defun tar-chgrp-entry (new-gid)
  1051. "Change the group-id associated with this entry in the tar file.
  1052. If this tar file was written by GNU tar, then you will be able to edit
  1053. the group id as a string; otherwise, you must edit it as a number.
  1054. You can force editing as a number by calling this with a prefix arg.
  1055. This does not modify the disk image; you must save the tar file itself
  1056. for this to be permanent."
  1057. (interactive
  1058. (list
  1059. (let ((descriptor (tar-current-descriptor)))
  1060. (if (or current-prefix-arg
  1061. (not (tar-header-magic descriptor)))
  1062. (read-number
  1063. "New GID number: "
  1064. (format "%s" (tar-header-gid descriptor)))
  1065. (read-string "New GID string: " (tar-header-gname descriptor))))))
  1066. (cond ((stringp new-gid)
  1067. (setf (tar-header-gname (tar-current-descriptor)) new-gid)
  1068. (tar-alter-one-field tar-gname-offset
  1069. (concat (encode-coding-string
  1070. new-gid tar-file-name-coding-system)
  1071. "\000")))
  1072. (t
  1073. (setf (tar-header-gid (tar-current-descriptor)) new-gid)
  1074. (tar-alter-one-field tar-gid-offset
  1075. (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
  1076. (defun tar-rename-entry (new-name)
  1077. "Change the name associated with this entry in the tar file.
  1078. This does not modify the disk image; you must save the tar file itself
  1079. for this to be permanent."
  1080. (interactive
  1081. (list (read-string "New name: "
  1082. (tar-header-name (tar-current-descriptor)))))
  1083. (if (string= "" new-name) (error "zero length name"))
  1084. (let ((encoded-new-name (encode-coding-string new-name
  1085. tar-file-name-coding-system))
  1086. (descriptor (tar-current-descriptor))
  1087. (prefix nil))
  1088. (when (tar-header-header-start descriptor)
  1089. ;; FIXME: Make it work for ././@LongLink.
  1090. (error "Rename with @LongLink format is not implemented"))
  1091. (when (and (> (length encoded-new-name) 98)
  1092. (string-match "/" encoded-new-name
  1093. (- (length encoded-new-name) 99))
  1094. (< (match-beginning 0) 155))
  1095. (unless (equal (tar-header-magic descriptor) "ustar\0")
  1096. (tar-alter-one-field tar-magic-offset (concat "ustar\0" "00")))
  1097. (setq prefix (substring encoded-new-name 0 (match-beginning 0)))
  1098. (setq encoded-new-name (substring encoded-new-name (match-end 0))))
  1099. (if (> (length encoded-new-name) 98) (error "name too long"))
  1100. (setf (tar-header-name descriptor) new-name)
  1101. (tar-alter-one-field 0
  1102. (substring (concat encoded-new-name (make-string 99 0)) 0 99))
  1103. (if prefix
  1104. (tar-alter-one-field tar-prefix-offset
  1105. (substring (concat prefix (make-string 155 0)) 0 155)))))
  1106. (defun tar-chmod-entry (new-mode)
  1107. "Change the protection bits associated with this entry in the tar file.
  1108. This does not modify the disk image; you must save the tar file itself
  1109. for this to be permanent."
  1110. (interactive (list (tar-parse-octal-integer-safe
  1111. (read-string "New protection (octal): "))))
  1112. (setf (tar-header-mode (tar-current-descriptor)) new-mode)
  1113. (tar-alter-one-field tar-mode-offset
  1114. (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
  1115. (defun tar-alter-one-field (data-position new-data-string &optional descriptor)
  1116. (unless descriptor (setq descriptor (tar-current-descriptor)))
  1117. ;;
  1118. ;; update the header-line.
  1119. (let ((col (current-column)))
  1120. (delete-region (line-beginning-position)
  1121. (prog2 (forward-line 1)
  1122. (point)
  1123. ;; Insert the new text after the old, before deleting,
  1124. ;; to preserve markers such as the window start.
  1125. (insert (tar-header-block-summarize descriptor) "\n")))
  1126. (forward-line -1) (move-to-column col))
  1127. (cl-assert (tar-data-swapped-p))
  1128. (with-current-buffer tar-data-buffer
  1129. (let* ((start (- (tar-header-data-start descriptor) 512)))
  1130. ;;
  1131. ;; delete the old field and insert a new one.
  1132. (goto-char (+ start data-position))
  1133. (delete-region (point) (+ (point) (length new-data-string))) ; <--
  1134. (cl-assert (not (or enable-multibyte-characters
  1135. (multibyte-string-p new-data-string))))
  1136. (insert new-data-string)
  1137. ;;
  1138. ;; compute a new checksum and insert it.
  1139. (let ((chk (tar-header-block-checksum
  1140. (buffer-substring start (+ start 512)))))
  1141. (goto-char (+ start tar-chk-offset))
  1142. (delete-region (point) (+ (point) 8))
  1143. (insert (format "%6o\0 " chk))
  1144. (setf (tar-header-checksum descriptor) chk)
  1145. ;;
  1146. ;; ok, make sure we didn't botch it.
  1147. (tar-header-block-check-checksum
  1148. (buffer-substring start (+ start 512))
  1149. chk (tar-header-name descriptor))
  1150. ))))
  1151. (defun tar-octal-time (timeval)
  1152. ;; Format a timestamp as 11 octal digits. Ghod, I hope this works...
  1153. (let ((hibits (car timeval)) (lobits (car (cdr timeval))))
  1154. (format "%05o%01o%05o"
  1155. (lsh hibits -2)
  1156. (logior (lsh (logand 3 hibits) 1)
  1157. (if (> (logand lobits 32768) 0) 1 0))
  1158. (logand 32767 lobits)
  1159. )))
  1160. (defun tar-subfile-save-buffer ()
  1161. "In tar subfile mode, save this buffer into its parent tar-file buffer.
  1162. This doesn't write anything to disk; you must save the parent tar-file buffer
  1163. to make your changes permanent."
  1164. (interactive)
  1165. (if (not (and (boundp 'tar-superior-buffer) tar-superior-buffer))
  1166. (error "This buffer has no superior tar file buffer"))
  1167. (if (not (and (boundp 'tar-superior-descriptor) tar-superior-descriptor))
  1168. (error "This buffer doesn't have an index into its superior tar file!"))
  1169. (let ((subfile (current-buffer))
  1170. (coding buffer-file-coding-system)
  1171. (descriptor tar-superior-descriptor)
  1172. subfile-size)
  1173. (with-current-buffer tar-superior-buffer
  1174. (let* ((start (tar-header-data-start descriptor))
  1175. (size (tar-header-size descriptor))
  1176. (head (memq descriptor tar-parse-info)))
  1177. (if (not head)
  1178. (error "Can't find this tar file entry in its parent tar file!"))
  1179. (with-current-buffer tar-data-buffer
  1180. ;; delete the old data...
  1181. (let* ((data-start start)
  1182. (data-end (+ data-start (tar-roundup-512 size))))
  1183. (narrow-to-region data-start data-end)
  1184. (delete-region (point-min) (point-max))
  1185. ;; insert the new data...
  1186. (goto-char data-start)
  1187. (let ((dest (current-buffer)))
  1188. (with-current-buffer subfile
  1189. (save-restriction
  1190. (widen)
  1191. (encode-coding-region (point-min) (point-max) coding dest))))
  1192. (setq subfile-size (- (point-max) (point-min)))
  1193. ;;
  1194. ;; pad the new data out to a multiple of 512...
  1195. (let ((subfile-size-pad (tar-roundup-512 subfile-size)))
  1196. (goto-char (point-max))
  1197. (insert (make-string (- subfile-size-pad subfile-size) 0))
  1198. ;;
  1199. ;; update the data of this files...
  1200. (setf (tar-header-size descriptor) subfile-size)
  1201. ;;
  1202. ;; Update the size field in the header block.
  1203. (widen))))
  1204. ;;
  1205. ;; alter the descriptor-line and header
  1206. ;;
  1207. (let ((position (- (length tar-parse-info) (length head))))
  1208. (goto-char (point-min))
  1209. (forward-line position)
  1210. (tar-alter-one-field tar-size-offset (format "%11o " subfile-size))
  1211. ;;
  1212. ;; Maybe update the datestamp.
  1213. (when tar-update-datestamp
  1214. (tar-alter-one-field tar-time-offset
  1215. (concat (tar-octal-time (current-time)) " "))))
  1216. ;; After doing the insertion, add any necessary final padding.
  1217. (tar-pad-to-blocksize))
  1218. (set-buffer-modified-p t) ; mark the tar file as modified
  1219. (tar-next-line 0))
  1220. (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
  1221. (message "Saved into tar-buffer `%s'. Be sure to save that buffer!"
  1222. (buffer-name tar-superior-buffer))
  1223. ;; Prevent basic-save-buffer from changing our coding-system.
  1224. (setq last-coding-system-used buffer-file-coding-system)
  1225. ;; Prevent ordinary saving from happening.
  1226. t))
  1227. ;; When this function is called, it is sure that the buffer is unibyte.
  1228. (defun tar-pad-to-blocksize ()
  1229. "If we are being anal about tar file blocksizes, fix up the current buffer.
  1230. Leaves the region wide."
  1231. (if (null tar-anal-blocksize)
  1232. nil
  1233. (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
  1234. (start (tar-header-data-start last-desc))
  1235. (link-p (tar-header-link-type last-desc))
  1236. (size (if link-p 0 (tar-header-size last-desc)))
  1237. (data-end (+ start size))
  1238. (bbytes (ash tar-anal-blocksize 9))
  1239. (pad-to (+ bbytes (* bbytes (/ (- data-end (point-min)) bbytes)))))
  1240. ;; If the padding after the last data is too long, delete some;
  1241. ;; else insert some until we are padded out to the right number of blocks.
  1242. ;;
  1243. (with-current-buffer tar-data-buffer
  1244. (let ((goal-end (+ (point-min) pad-to)))
  1245. (if (> (point-max) goal-end)
  1246. (delete-region goal-end (point-max))
  1247. (goto-char (point-max))
  1248. (insert (make-string (- goal-end (point-max)) ?\0))))))))
  1249. ;; Used in write-region-annotate-functions to write tar-files out correctly.
  1250. (defun tar-write-region-annotate (start _end)
  1251. ;; When called from write-file (and auto-save), `start' is nil.
  1252. ;; When called from M-x write-region, we assume the user wants to save
  1253. ;; (part of) the summary, not the tar data.
  1254. (unless (or start (not (tar-data-swapped-p)))
  1255. (tar-clear-modification-flags)
  1256. (set-buffer tar-data-buffer)
  1257. nil))
  1258. (provide 'tar-mode)
  1259. ;;; tar-mode.el ends here