PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/tarfile.py

https://bitbucket.org/evelyn559/pypy
Python | 2594 lines | 2488 code | 28 blank | 78 comment | 23 complexity | 4cbb66665d9944279608be91a1e23f41 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-1 -*-
  3. #-------------------------------------------------------------------
  4. # tarfile.py
  5. #-------------------------------------------------------------------
  6. # Copyright (C) 2002 Lars Gustäbel <lars@gustaebel.de>
  7. # All rights reserved.
  8. #
  9. # Permission is hereby granted, free of charge, to any person
  10. # obtaining a copy of this software and associated documentation
  11. # files (the "Software"), to deal in the Software without
  12. # restriction, including without limitation the rights to use,
  13. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. # copies of the Software, and to permit persons to whom the
  15. # Software is furnished to do so, subject to the following
  16. # conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be
  19. # included in all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  23. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  25. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  26. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  28. # OTHER DEALINGS IN THE SOFTWARE.
  29. #
  30. """Read from and write to tar format archives.
  31. """
  32. __version__ = "$Revision$"
  33. # $Source$
  34. version = "0.9.0"
  35. __author__ = "Lars Gustäbel (lars@gustaebel.de)"
  36. __date__ = "$Date$"
  37. __cvsid__ = "$Id$"
  38. __credits__ = "Gustavo Niemeyer, Niels Gustäbel, Richard Townsend."
  39. #---------
  40. # Imports
  41. #---------
  42. import sys
  43. import os
  44. import shutil
  45. import stat
  46. import errno
  47. import time
  48. import struct
  49. import copy
  50. import re
  51. import operator
  52. try:
  53. import grp, pwd
  54. except ImportError:
  55. grp = pwd = None
  56. # from tarfile import *
  57. __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
  58. #---------------------------------------------------------
  59. # tar constants
  60. #---------------------------------------------------------
  61. NUL = "\0" # the null character
  62. BLOCKSIZE = 512 # length of processing blocks
  63. RECORDSIZE = BLOCKSIZE * 20 # length of records
  64. GNU_MAGIC = "ustar \0" # magic gnu tar string
  65. POSIX_MAGIC = "ustar\x0000" # magic posix tar string
  66. LENGTH_NAME = 100 # maximum length of a filename
  67. LENGTH_LINK = 100 # maximum length of a linkname
  68. LENGTH_PREFIX = 155 # maximum length of the prefix field
  69. REGTYPE = "0" # regular file
  70. AREGTYPE = "\0" # regular file
  71. LNKTYPE = "1" # link (inside tarfile)
  72. SYMTYPE = "2" # symbolic link
  73. CHRTYPE = "3" # character special device
  74. BLKTYPE = "4" # block special device
  75. DIRTYPE = "5" # directory
  76. FIFOTYPE = "6" # fifo special device
  77. CONTTYPE = "7" # contiguous file
  78. GNUTYPE_LONGNAME = "L" # GNU tar longname
  79. GNUTYPE_LONGLINK = "K" # GNU tar longlink
  80. GNUTYPE_SPARSE = "S" # GNU tar sparse file
  81. XHDTYPE = "x" # POSIX.1-2001 extended header
  82. XGLTYPE = "g" # POSIX.1-2001 global header
  83. SOLARIS_XHDTYPE = "X" # Solaris extended header
  84. USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
  85. GNU_FORMAT = 1 # GNU tar format
  86. PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
  87. DEFAULT_FORMAT = GNU_FORMAT
  88. #---------------------------------------------------------
  89. # tarfile constants
  90. #---------------------------------------------------------
  91. # File types that tarfile supports:
  92. SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
  93. SYMTYPE, DIRTYPE, FIFOTYPE,
  94. CONTTYPE, CHRTYPE, BLKTYPE,
  95. GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
  96. GNUTYPE_SPARSE)
  97. # File types that will be treated as a regular file.
  98. REGULAR_TYPES = (REGTYPE, AREGTYPE,
  99. CONTTYPE, GNUTYPE_SPARSE)
  100. # File types that are part of the GNU tar format.
  101. GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
  102. GNUTYPE_SPARSE)
  103. # Fields from a pax header that override a TarInfo attribute.
  104. PAX_FIELDS = ("path", "linkpath", "size", "mtime",
  105. "uid", "gid", "uname", "gname")
  106. # Fields in a pax header that are numbers, all other fields
  107. # are treated as strings.
  108. PAX_NUMBER_FIELDS = {
  109. "atime": float,
  110. "ctime": float,
  111. "mtime": float,
  112. "uid": int,
  113. "gid": int,
  114. "size": int
  115. }
  116. #---------------------------------------------------------
  117. # Bits used in the mode field, values in octal.
  118. #---------------------------------------------------------
  119. S_IFLNK = 0120000 # symbolic link
  120. S_IFREG = 0100000 # regular file
  121. S_IFBLK = 0060000 # block device
  122. S_IFDIR = 0040000 # directory
  123. S_IFCHR = 0020000 # character device
  124. S_IFIFO = 0010000 # fifo
  125. TSUID = 04000 # set UID on execution
  126. TSGID = 02000 # set GID on execution
  127. TSVTX = 01000 # reserved
  128. TUREAD = 0400 # read by owner
  129. TUWRITE = 0200 # write by owner
  130. TUEXEC = 0100 # execute/search by owner
  131. TGREAD = 0040 # read by group
  132. TGWRITE = 0020 # write by group
  133. TGEXEC = 0010 # execute/search by group
  134. TOREAD = 0004 # read by other
  135. TOWRITE = 0002 # write by other
  136. TOEXEC = 0001 # execute/search by other
  137. #---------------------------------------------------------
  138. # initialization
  139. #---------------------------------------------------------
  140. ENCODING = sys.getfilesystemencoding()
  141. if ENCODING is None:
  142. ENCODING = sys.getdefaultencoding()
  143. #---------------------------------------------------------
  144. # Some useful functions
  145. #---------------------------------------------------------
  146. def stn(s, length):
  147. """Convert a python string to a null-terminated string buffer.
  148. """
  149. return s[:length] + (length - len(s)) * NUL
  150. def nts(s):
  151. """Convert a null-terminated string field to a python string.
  152. """
  153. # Use the string up to the first null char.
  154. p = s.find("\0")
  155. if p == -1:
  156. return s
  157. return s[:p]
  158. def nti(s):
  159. """Convert a number field to a python number.
  160. """
  161. # There are two possible encodings for a number field, see
  162. # itn() below.
  163. if s[0] != chr(0200):
  164. try:
  165. n = int(nts(s) or "0", 8)
  166. except ValueError:
  167. raise InvalidHeaderError("invalid header")
  168. else:
  169. n = 0L
  170. for i in xrange(len(s) - 1):
  171. n <<= 8
  172. n += ord(s[i + 1])
  173. return n
  174. def itn(n, digits=8, format=DEFAULT_FORMAT):
  175. """Convert a python number to a number field.
  176. """
  177. # POSIX 1003.1-1988 requires numbers to be encoded as a string of
  178. # octal digits followed by a null-byte, this allows values up to
  179. # (8**(digits-1))-1. GNU tar allows storing numbers greater than
  180. # that if necessary. A leading 0200 byte indicates this particular
  181. # encoding, the following digits-1 bytes are a big-endian
  182. # representation. This allows values up to (256**(digits-1))-1.
  183. if 0 <= n < 8 ** (digits - 1):
  184. s = "%0*o" % (digits - 1, n) + NUL
  185. else:
  186. if format != GNU_FORMAT or n >= 256 ** (digits - 1):
  187. raise ValueError("overflow in number field")
  188. if n < 0:
  189. # XXX We mimic GNU tar's behaviour with negative numbers,
  190. # this could raise OverflowError.
  191. n = struct.unpack("L", struct.pack("l", n))[0]
  192. s = ""
  193. for i in xrange(digits - 1):
  194. s = chr(n & 0377) + s
  195. n >>= 8
  196. s = chr(0200) + s
  197. return s
  198. def uts(s, encoding, errors):
  199. """Convert a unicode object to a string.
  200. """
  201. if errors == "utf-8":
  202. # An extra error handler similar to the -o invalid=UTF-8 option
  203. # in POSIX.1-2001. Replace untranslatable characters with their
  204. # UTF-8 representation.
  205. try:
  206. return s.encode(encoding, "strict")
  207. except UnicodeEncodeError:
  208. x = []
  209. for c in s:
  210. try:
  211. x.append(c.encode(encoding, "strict"))
  212. except UnicodeEncodeError:
  213. x.append(c.encode("utf8"))
  214. return "".join(x)
  215. else:
  216. return s.encode(encoding, errors)
  217. def calc_chksums(buf):
  218. """Calculate the checksum for a member's header by summing up all
  219. characters except for the chksum field which is treated as if
  220. it was filled with spaces. According to the GNU tar sources,
  221. some tars (Sun and NeXT) calculate chksum with signed char,
  222. which will be different if there are chars in the buffer with
  223. the high bit set. So we calculate two checksums, unsigned and
  224. signed.
  225. """
  226. unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512]))
  227. signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512]))
  228. return unsigned_chksum, signed_chksum
  229. def copyfileobj(src, dst, length=None):
  230. """Copy length bytes from fileobj src to fileobj dst.
  231. If length is None, copy the entire content.
  232. """
  233. if length == 0:
  234. return
  235. if length is None:
  236. shutil.copyfileobj(src, dst)
  237. return
  238. BUFSIZE = 16 * 1024
  239. blocks, remainder = divmod(length, BUFSIZE)
  240. for b in xrange(blocks):
  241. buf = src.read(BUFSIZE)
  242. if len(buf) < BUFSIZE:
  243. raise IOError("end of file reached")
  244. dst.write(buf)
  245. if remainder != 0:
  246. buf = src.read(remainder)
  247. if len(buf) < remainder:
  248. raise IOError("end of file reached")
  249. dst.write(buf)
  250. return
  251. filemode_table = (
  252. ((S_IFLNK, "l"),
  253. (S_IFREG, "-"),
  254. (S_IFBLK, "b"),
  255. (S_IFDIR, "d"),
  256. (S_IFCHR, "c"),
  257. (S_IFIFO, "p")),
  258. ((TUREAD, "r"),),
  259. ((TUWRITE, "w"),),
  260. ((TUEXEC|TSUID, "s"),
  261. (TSUID, "S"),
  262. (TUEXEC, "x")),
  263. ((TGREAD, "r"),),
  264. ((TGWRITE, "w"),),
  265. ((TGEXEC|TSGID, "s"),
  266. (TSGID, "S"),
  267. (TGEXEC, "x")),
  268. ((TOREAD, "r"),),
  269. ((TOWRITE, "w"),),
  270. ((TOEXEC|TSVTX, "t"),
  271. (TSVTX, "T"),
  272. (TOEXEC, "x"))
  273. )
  274. def filemode(mode):
  275. """Convert a file's mode to a string of the form
  276. -rwxrwxrwx.
  277. Used by TarFile.list()
  278. """
  279. perm = []
  280. for table in filemode_table:
  281. for bit, char in table:
  282. if mode & bit == bit:
  283. perm.append(char)
  284. break
  285. else:
  286. perm.append("-")
  287. return "".join(perm)
  288. class TarError(Exception):
  289. """Base exception."""
  290. pass
  291. class ExtractError(TarError):
  292. """General exception for extract errors."""
  293. pass
  294. class ReadError(TarError):
  295. """Exception for unreadble tar archives."""
  296. pass
  297. class CompressionError(TarError):
  298. """Exception for unavailable compression methods."""
  299. pass
  300. class StreamError(TarError):
  301. """Exception for unsupported operations on stream-like TarFiles."""
  302. pass
  303. class HeaderError(TarError):
  304. """Base exception for header errors."""
  305. pass
  306. class EmptyHeaderError(HeaderError):
  307. """Exception for empty headers."""
  308. pass
  309. class TruncatedHeaderError(HeaderError):
  310. """Exception for truncated headers."""
  311. pass
  312. class EOFHeaderError(HeaderError):
  313. """Exception for end of file headers."""
  314. pass
  315. class InvalidHeaderError(HeaderError):
  316. """Exception for invalid headers."""
  317. pass
  318. class SubsequentHeaderError(HeaderError):
  319. """Exception for missing and invalid extended headers."""
  320. pass
  321. #---------------------------
  322. # internal stream interface
  323. #---------------------------
  324. class _LowLevelFile:
  325. """Low-level file object. Supports reading and writing.
  326. It is used instead of a regular file object for streaming
  327. access.
  328. """
  329. def __init__(self, name, mode):
  330. mode = {
  331. "r": os.O_RDONLY,
  332. "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
  333. }[mode]
  334. if hasattr(os, "O_BINARY"):
  335. mode |= os.O_BINARY
  336. self.fd = os.open(name, mode, 0666)
  337. def close(self):
  338. os.close(self.fd)
  339. def read(self, size):
  340. return os.read(self.fd, size)
  341. def write(self, s):
  342. os.write(self.fd, s)
  343. class _Stream:
  344. """Class that serves as an adapter between TarFile and
  345. a stream-like object. The stream-like object only
  346. needs to have a read() or write() method and is accessed
  347. blockwise. Use of gzip or bzip2 compression is possible.
  348. A stream-like object could be for example: sys.stdin,
  349. sys.stdout, a socket, a tape device etc.
  350. _Stream is intended to be used only internally.
  351. """
  352. def __init__(self, name, mode, comptype, fileobj, bufsize):
  353. """Construct a _Stream object.
  354. """
  355. self._extfileobj = True
  356. if fileobj is None:
  357. fileobj = _LowLevelFile(name, mode)
  358. self._extfileobj = False
  359. if comptype == '*':
  360. # Enable transparent compression detection for the
  361. # stream interface
  362. fileobj = _StreamProxy(fileobj)
  363. comptype = fileobj.getcomptype()
  364. self.name = name or ""
  365. self.mode = mode
  366. self.comptype = comptype
  367. self.fileobj = fileobj
  368. self.bufsize = bufsize
  369. self.buf = ""
  370. self.pos = 0L
  371. self.closed = False
  372. if comptype == "gz":
  373. try:
  374. import zlib
  375. except ImportError:
  376. raise CompressionError("zlib module is not available")
  377. self.zlib = zlib
  378. self.crc = zlib.crc32("") & 0xffffffffL
  379. if mode == "r":
  380. self._init_read_gz()
  381. else:
  382. self._init_write_gz()
  383. if comptype == "bz2":
  384. try:
  385. import bz2
  386. except ImportError:
  387. raise CompressionError("bz2 module is not available")
  388. if mode == "r":
  389. self.dbuf = ""
  390. self.cmp = bz2.BZ2Decompressor()
  391. else:
  392. self.cmp = bz2.BZ2Compressor()
  393. def __del__(self):
  394. if hasattr(self, "closed") and not self.closed:
  395. self.close()
  396. def _init_write_gz(self):
  397. """Initialize for writing with gzip compression.
  398. """
  399. self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
  400. -self.zlib.MAX_WBITS,
  401. self.zlib.DEF_MEM_LEVEL,
  402. 0)
  403. timestamp = struct.pack("<L", long(time.time()))
  404. self.__write("\037\213\010\010%s\002\377" % timestamp)
  405. if self.name.endswith(".gz"):
  406. self.name = self.name[:-3]
  407. self.__write(self.name + NUL)
  408. def write(self, s):
  409. """Write string s to the stream.
  410. """
  411. if self.comptype == "gz":
  412. self.crc = self.zlib.crc32(s, self.crc) & 0xffffffffL
  413. self.pos += len(s)
  414. if self.comptype != "tar":
  415. s = self.cmp.compress(s)
  416. self.__write(s)
  417. def __write(self, s):
  418. """Write string s to the stream if a whole new block
  419. is ready to be written.
  420. """
  421. self.buf += s
  422. while len(self.buf) > self.bufsize:
  423. self.fileobj.write(self.buf[:self.bufsize])
  424. self.buf = self.buf[self.bufsize:]
  425. def close(self):
  426. """Close the _Stream object. No operation should be
  427. done on it afterwards.
  428. """
  429. if self.closed:
  430. return
  431. if self.mode == "w" and self.comptype != "tar":
  432. self.buf += self.cmp.flush()
  433. if self.mode == "w" and self.buf:
  434. self.fileobj.write(self.buf)
  435. self.buf = ""
  436. if self.comptype == "gz":
  437. # The native zlib crc is an unsigned 32-bit integer, but
  438. # the Python wrapper implicitly casts that to a signed C
  439. # long. So, on a 32-bit box self.crc may "look negative",
  440. # while the same crc on a 64-bit box may "look positive".
  441. # To avoid irksome warnings from the `struct` module, force
  442. # it to look positive on all boxes.
  443. self.fileobj.write(struct.pack("<L", self.crc & 0xffffffffL))
  444. self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFFL))
  445. if not self._extfileobj:
  446. self.fileobj.close()
  447. self.closed = True
  448. def _init_read_gz(self):
  449. """Initialize for reading a gzip compressed fileobj.
  450. """
  451. self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
  452. self.dbuf = ""
  453. # taken from gzip.GzipFile with some alterations
  454. if self.__read(2) != "\037\213":
  455. raise ReadError("not a gzip file")
  456. if self.__read(1) != "\010":
  457. raise CompressionError("unsupported compression method")
  458. flag = ord(self.__read(1))
  459. self.__read(6)
  460. if flag & 4:
  461. xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
  462. self.read(xlen)
  463. if flag & 8:
  464. while True:
  465. s = self.__read(1)
  466. if not s or s == NUL:
  467. break
  468. if flag & 16:
  469. while True:
  470. s = self.__read(1)
  471. if not s or s == NUL:
  472. break
  473. if flag & 2:
  474. self.__read(2)
  475. def tell(self):
  476. """Return the stream's file pointer position.
  477. """
  478. return self.pos
  479. def seek(self, pos=0):
  480. """Set the stream's file pointer to pos. Negative seeking
  481. is forbidden.
  482. """
  483. if pos - self.pos >= 0:
  484. blocks, remainder = divmod(pos - self.pos, self.bufsize)
  485. for i in xrange(blocks):
  486. self.read(self.bufsize)
  487. self.read(remainder)
  488. else:
  489. raise StreamError("seeking backwards is not allowed")
  490. return self.pos
  491. def read(self, size=None):
  492. """Return the next size number of bytes from the stream.
  493. If size is not defined, return all bytes of the stream
  494. up to EOF.
  495. """
  496. if size is None:
  497. t = []
  498. while True:
  499. buf = self._read(self.bufsize)
  500. if not buf:
  501. break
  502. t.append(buf)
  503. buf = "".join(t)
  504. else:
  505. buf = self._read(size)
  506. self.pos += len(buf)
  507. return buf
  508. def _read(self, size):
  509. """Return size bytes from the stream.
  510. """
  511. if self.comptype == "tar":
  512. return self.__read(size)
  513. c = len(self.dbuf)
  514. t = [self.dbuf]
  515. while c < size:
  516. buf = self.__read(self.bufsize)
  517. if not buf:
  518. break
  519. try:
  520. buf = self.cmp.decompress(buf)
  521. except IOError:
  522. raise ReadError("invalid compressed data")
  523. t.append(buf)
  524. c += len(buf)
  525. t = "".join(t)
  526. self.dbuf = t[size:]
  527. return t[:size]
  528. def __read(self, size):
  529. """Return size bytes from stream. If internal buffer is empty,
  530. read another block from the stream.
  531. """
  532. c = len(self.buf)
  533. t = [self.buf]
  534. while c < size:
  535. buf = self.fileobj.read(self.bufsize)
  536. if not buf:
  537. break
  538. t.append(buf)
  539. c += len(buf)
  540. t = "".join(t)
  541. self.buf = t[size:]
  542. return t[:size]
  543. # class _Stream
  544. class _StreamProxy(object):
  545. """Small proxy class that enables transparent compression
  546. detection for the Stream interface (mode 'r|*').
  547. """
  548. def __init__(self, fileobj):
  549. self.fileobj = fileobj
  550. self.buf = self.fileobj.read(BLOCKSIZE)
  551. def read(self, size):
  552. self.read = self.fileobj.read
  553. return self.buf
  554. def getcomptype(self):
  555. if self.buf.startswith("\037\213\010"):
  556. return "gz"
  557. if self.buf.startswith("BZh91"):
  558. return "bz2"
  559. return "tar"
  560. def close(self):
  561. self.fileobj.close()
  562. # class StreamProxy
  563. class _BZ2Proxy(object):
  564. """Small proxy class that enables external file object
  565. support for "r:bz2" and "w:bz2" modes. This is actually
  566. a workaround for a limitation in bz2 module's BZ2File
  567. class which (unlike gzip.GzipFile) has no support for
  568. a file object argument.
  569. """
  570. blocksize = 16 * 1024
  571. def __init__(self, fileobj, mode):
  572. self.fileobj = fileobj
  573. self.mode = mode
  574. self.name = getattr(self.fileobj, "name", None)
  575. self.init()
  576. def init(self):
  577. import bz2
  578. self.pos = 0
  579. if self.mode == "r":
  580. self.bz2obj = bz2.BZ2Decompressor()
  581. self.fileobj.seek(0)
  582. self.buf = ""
  583. else:
  584. self.bz2obj = bz2.BZ2Compressor()
  585. def read(self, size):
  586. b = [self.buf]
  587. x = len(self.buf)
  588. while x < size:
  589. raw = self.fileobj.read(self.blocksize)
  590. if not raw:
  591. break
  592. data = self.bz2obj.decompress(raw)
  593. b.append(data)
  594. x += len(data)
  595. self.buf = "".join(b)
  596. buf = self.buf[:size]
  597. self.buf = self.buf[size:]
  598. self.pos += len(buf)
  599. return buf
  600. def seek(self, pos):
  601. if pos < self.pos:
  602. self.init()
  603. self.read(pos - self.pos)
  604. def tell(self):
  605. return self.pos
  606. def write(self, data):
  607. self.pos += len(data)
  608. raw = self.bz2obj.compress(data)
  609. self.fileobj.write(raw)
  610. def close(self):
  611. if self.mode == "w":
  612. raw = self.bz2obj.flush()
  613. self.fileobj.write(raw)
  614. # class _BZ2Proxy
  615. #------------------------
  616. # Extraction file object
  617. #------------------------
  618. class _FileInFile(object):
  619. """A thin wrapper around an existing file object that
  620. provides a part of its data as an individual file
  621. object.
  622. """
  623. def __init__(self, fileobj, offset, size, sparse=None):
  624. self.fileobj = fileobj
  625. self.offset = offset
  626. self.size = size
  627. self.sparse = sparse
  628. self.position = 0
  629. def tell(self):
  630. """Return the current file position.
  631. """
  632. return self.position
  633. def seek(self, position):
  634. """Seek to a position in the file.
  635. """
  636. self.position = position
  637. def read(self, size=None):
  638. """Read data from the file.
  639. """
  640. if size is None:
  641. size = self.size - self.position
  642. else:
  643. size = min(size, self.size - self.position)
  644. if self.sparse is None:
  645. return self.readnormal(size)
  646. else:
  647. return self.readsparse(size)
  648. def readnormal(self, size):
  649. """Read operation for regular files.
  650. """
  651. self.fileobj.seek(self.offset + self.position)
  652. self.position += size
  653. return self.fileobj.read(size)
  654. def readsparse(self, size):
  655. """Read operation for sparse files.
  656. """
  657. data = []
  658. while size > 0:
  659. buf = self.readsparsesection(size)
  660. if not buf:
  661. break
  662. size -= len(buf)
  663. data.append(buf)
  664. return "".join(data)
  665. def readsparsesection(self, size):
  666. """Read a single section of a sparse file.
  667. """
  668. section = self.sparse.find(self.position)
  669. if section is None:
  670. return ""
  671. size = min(size, section.offset + section.size - self.position)
  672. if isinstance(section, _data):
  673. realpos = section.realpos + self.position - section.offset
  674. self.fileobj.seek(self.offset + realpos)
  675. self.position += size
  676. return self.fileobj.read(size)
  677. else:
  678. self.position += size
  679. return NUL * size
  680. #class _FileInFile
  681. class ExFileObject(object):
  682. """File-like object for reading an archive member.
  683. Is returned by TarFile.extractfile().
  684. """
  685. blocksize = 1024
  686. def __init__(self, tarfile, tarinfo):
  687. self.fileobj = _FileInFile(tarfile.fileobj,
  688. tarinfo.offset_data,
  689. tarinfo.size,
  690. getattr(tarinfo, "sparse", None))
  691. self.name = tarinfo.name
  692. self.mode = "r"
  693. self.closed = False
  694. self.size = tarinfo.size
  695. self.position = 0
  696. self.buffer = ""
  697. def read(self, size=None):
  698. """Read at most size bytes from the file. If size is not
  699. present or None, read all data until EOF is reached.
  700. """
  701. if self.closed:
  702. raise ValueError("I/O operation on closed file")
  703. buf = ""
  704. if self.buffer:
  705. if size is None:
  706. buf = self.buffer
  707. self.buffer = ""
  708. else:
  709. buf = self.buffer[:size]
  710. self.buffer = self.buffer[size:]
  711. if size is None:
  712. buf += self.fileobj.read()
  713. else:
  714. buf += self.fileobj.read(size - len(buf))
  715. self.position += len(buf)
  716. return buf
  717. def readline(self, size=-1):
  718. """Read one entire line from the file. If size is present
  719. and non-negative, return a string with at most that
  720. size, which may be an incomplete line.
  721. """
  722. if self.closed:
  723. raise ValueError("I/O operation on closed file")
  724. if "\n" in self.buffer:
  725. pos = self.buffer.find("\n") + 1
  726. else:
  727. buffers = [self.buffer]
  728. while True:
  729. buf = self.fileobj.read(self.blocksize)
  730. buffers.append(buf)
  731. if not buf or "\n" in buf:
  732. self.buffer = "".join(buffers)
  733. pos = self.buffer.find("\n") + 1
  734. if pos == 0:
  735. # no newline found.
  736. pos = len(self.buffer)
  737. break
  738. if size != -1:
  739. pos = min(size, pos)
  740. buf = self.buffer[:pos]
  741. self.buffer = self.buffer[pos:]
  742. self.position += len(buf)
  743. return buf
  744. def readlines(self):
  745. """Return a list with all remaining lines.
  746. """
  747. result = []
  748. while True:
  749. line = self.readline()
  750. if not line: break
  751. result.append(line)
  752. return result
  753. def tell(self):
  754. """Return the current file position.
  755. """
  756. if self.closed:
  757. raise ValueError("I/O operation on closed file")
  758. return self.position
  759. def seek(self, pos, whence=os.SEEK_SET):
  760. """Seek to a position in the file.
  761. """
  762. if self.closed:
  763. raise ValueError("I/O operation on closed file")
  764. if whence == os.SEEK_SET:
  765. self.position = min(max(pos, 0), self.size)
  766. elif whence == os.SEEK_CUR:
  767. if pos < 0:
  768. self.position = max(self.position + pos, 0)
  769. else:
  770. self.position = min(self.position + pos, self.size)
  771. elif whence == os.SEEK_END:
  772. self.position = max(min(self.size + pos, self.size), 0)
  773. else:
  774. raise ValueError("Invalid argument")
  775. self.buffer = ""
  776. self.fileobj.seek(self.position)
  777. def close(self):
  778. """Close the file object.
  779. """
  780. self.closed = True
  781. def __iter__(self):
  782. """Get an iterator over the file's lines.
  783. """
  784. while True:
  785. line = self.readline()
  786. if not line:
  787. break
  788. yield line
  789. #class ExFileObject
  790. #------------------
  791. # Exported Classes
  792. #------------------
  793. class TarInfo(object):
  794. """Informational class which holds the details about an
  795. archive member given by a tar header block.
  796. TarInfo objects are returned by TarFile.getmember(),
  797. TarFile.getmembers() and TarFile.gettarinfo() and are
  798. usually created internally.
  799. """
  800. def __init__(self, name=""):
  801. """Construct a TarInfo object. name is the optional name
  802. of the member.
  803. """
  804. self.name = name # member name
  805. self.mode = 0644 # file permissions
  806. self.uid = 0 # user id
  807. self.gid = 0 # group id
  808. self.size = 0 # file size
  809. self.mtime = 0 # modification time
  810. self.chksum = 0 # header checksum
  811. self.type = REGTYPE # member type
  812. self.linkname = "" # link name
  813. self.uname = "" # user name
  814. self.gname = "" # group name
  815. self.devmajor = 0 # device major number
  816. self.devminor = 0 # device minor number
  817. self.offset = 0 # the tar header starts here
  818. self.offset_data = 0 # the file's data starts here
  819. self.pax_headers = {} # pax header information
  820. # In pax headers the "name" and "linkname" field are called
  821. # "path" and "linkpath".
  822. def _getpath(self):
  823. return self.name
  824. def _setpath(self, name):
  825. self.name = name
  826. path = property(_getpath, _setpath)
  827. def _getlinkpath(self):
  828. return self.linkname
  829. def _setlinkpath(self, linkname):
  830. self.linkname = linkname
  831. linkpath = property(_getlinkpath, _setlinkpath)
  832. def __repr__(self):
  833. return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self))
  834. def get_info(self, encoding, errors):
  835. """Return the TarInfo's attributes as a dictionary.
  836. """
  837. info = {
  838. "name": self.name,
  839. "mode": self.mode & 07777,
  840. "uid": self.uid,
  841. "gid": self.gid,
  842. "size": self.size,
  843. "mtime": self.mtime,
  844. "chksum": self.chksum,
  845. "type": self.type,
  846. "linkname": self.linkname,
  847. "uname": self.uname,
  848. "gname": self.gname,
  849. "devmajor": self.devmajor,
  850. "devminor": self.devminor
  851. }
  852. if info["type"] == DIRTYPE and not info["name"].endswith("/"):
  853. info["name"] += "/"
  854. for key in ("name", "linkname", "uname", "gname"):
  855. if type(info[key]) is unicode:
  856. info[key] = info[key].encode(encoding, errors)
  857. return info
  858. def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="strict"):
  859. """Return a tar header as a string of 512 byte blocks.
  860. """
  861. info = self.get_info(encoding, errors)
  862. if format == USTAR_FORMAT:
  863. return self.create_ustar_header(info)
  864. elif format == GNU_FORMAT:
  865. return self.create_gnu_header(info)
  866. elif format == PAX_FORMAT:
  867. return self.create_pax_header(info, encoding, errors)
  868. else:
  869. raise ValueError("invalid format")
  870. def create_ustar_header(self, info):
  871. """Return the object as a ustar header block.
  872. """
  873. info["magic"] = POSIX_MAGIC
  874. if len(info["linkname"]) > LENGTH_LINK:
  875. raise ValueError("linkname is too long")
  876. if len(info["name"]) > LENGTH_NAME:
  877. info["prefix"], info["name"] = self._posix_split_name(info["name"])
  878. return self._create_header(info, USTAR_FORMAT)
  879. def create_gnu_header(self, info):
  880. """Return the object as a GNU header block sequence.
  881. """
  882. info["magic"] = GNU_MAGIC
  883. buf = ""
  884. if len(info["linkname"]) > LENGTH_LINK:
  885. buf += self._create_gnu_long_header(info["linkname"], GNUTYPE_LONGLINK)
  886. if len(info["name"]) > LENGTH_NAME:
  887. buf += self._create_gnu_long_header(info["name"], GNUTYPE_LONGNAME)
  888. return buf + self._create_header(info, GNU_FORMAT)
  889. def create_pax_header(self, info, encoding, errors):
  890. """Return the object as a ustar header block. If it cannot be
  891. represented this way, prepend a pax extended header sequence
  892. with supplement information.
  893. """
  894. info["magic"] = POSIX_MAGIC
  895. pax_headers = self.pax_headers.copy()
  896. # Test string fields for values that exceed the field length or cannot
  897. # be represented in ASCII encoding.
  898. for name, hname, length in (
  899. ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK),
  900. ("uname", "uname", 32), ("gname", "gname", 32)):
  901. if hname in pax_headers:
  902. # The pax header has priority.
  903. continue
  904. val = info[name].decode(encoding, errors)
  905. # Try to encode the string as ASCII.
  906. try:
  907. val.encode("ascii")
  908. except UnicodeEncodeError:
  909. pax_headers[hname] = val
  910. continue
  911. if len(info[name]) > length:
  912. pax_headers[hname] = val
  913. # Test number fields for values that exceed the field limit or values
  914. # that like to be stored as float.
  915. for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)):
  916. if name in pax_headers:
  917. # The pax header has priority. Avoid overflow.
  918. info[name] = 0
  919. continue
  920. val = info[name]
  921. if not 0 <= val < 8 ** (digits - 1) or isinstance(val, float):
  922. pax_headers[name] = unicode(val)
  923. info[name] = 0
  924. # Create a pax extended header if necessary.
  925. if pax_headers:
  926. buf = self._create_pax_generic_header(pax_headers)
  927. else:
  928. buf = ""
  929. return buf + self._create_header(info, USTAR_FORMAT)
  930. @classmethod
  931. def create_pax_global_header(cls, pax_headers):
  932. """Return the object as a pax global header block sequence.
  933. """
  934. return cls._create_pax_generic_header(pax_headers, type=XGLTYPE)
  935. def _posix_split_name(self, name):
  936. """Split a name longer than 100 chars into a prefix
  937. and a name part.
  938. """
  939. prefix = name[:LENGTH_PREFIX + 1]
  940. while prefix and prefix[-1] != "/":
  941. prefix = prefix[:-1]
  942. name = name[len(prefix):]
  943. prefix = prefix[:-1]
  944. if not prefix or len(name) > LENGTH_NAME:
  945. raise ValueError("name is too long")
  946. return prefix, name
  947. @staticmethod
  948. def _create_header(info, format):
  949. """Return a header block. info is a dictionary with file
  950. information, format must be one of the *_FORMAT constants.
  951. """
  952. parts = [
  953. stn(info.get("name", ""), 100),
  954. itn(info.get("mode", 0) & 07777, 8, format),
  955. itn(info.get("uid", 0), 8, format),
  956. itn(info.get("gid", 0), 8, format),
  957. itn(info.get("size", 0), 12, format),
  958. itn(info.get("mtime", 0), 12, format),
  959. " ", # checksum field
  960. info.get("type", REGTYPE),
  961. stn(info.get("linkname", ""), 100),
  962. stn(info.get("magic", POSIX_MAGIC), 8),
  963. stn(info.get("uname", ""), 32),
  964. stn(info.get("gname", ""), 32),
  965. itn(info.get("devmajor", 0), 8, format),
  966. itn(info.get("devminor", 0), 8, format),
  967. stn(info.get("prefix", ""), 155)
  968. ]
  969. buf = struct.pack("%ds" % BLOCKSIZE, "".join(parts))
  970. chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
  971. buf = buf[:-364] + "%06o\0" % chksum + buf[-357:]
  972. return buf
  973. @staticmethod
  974. def _create_payload(payload):
  975. """Return the string payload filled with zero bytes
  976. up to the next 512 byte border.
  977. """
  978. blocks, remainder = divmod(len(payload), BLOCKSIZE)
  979. if remainder > 0:
  980. payload += (BLOCKSIZE - remainder) * NUL
  981. return payload
  982. @classmethod
  983. def _create_gnu_long_header(cls, name, type):
  984. """Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
  985. for name.
  986. """
  987. name += NUL
  988. info = {}
  989. info["name"] = "././@LongLink"
  990. info["type"] = type
  991. info["size"] = len(name)
  992. info["magic"] = GNU_MAGIC
  993. # create extended header + name blocks.
  994. return cls._create_header(info, USTAR_FORMAT) + \
  995. cls._create_payload(name)
  996. @classmethod
  997. def _create_pax_generic_header(cls, pax_headers, type=XHDTYPE):
  998. """Return a POSIX.1-2001 extended or global header sequence
  999. that contains a list of keyword, value pairs. The values
  1000. must be unicode objects.
  1001. """
  1002. records = []
  1003. for keyword, value in pax_headers.iteritems():
  1004. keyword = keyword.encode("utf8")
  1005. value = value.encode("utf8")
  1006. l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n'
  1007. n = p = 0
  1008. while True:
  1009. n = l + len(str(p))
  1010. if n == p:
  1011. break
  1012. p = n
  1013. records.append("%d %s=%s\n" % (p, keyword, value))
  1014. records = "".join(records)
  1015. # We use a hardcoded "././@PaxHeader" name like star does
  1016. # instead of the one that POSIX recommends.
  1017. info = {}
  1018. info["name"] = "././@PaxHeader"
  1019. info["type"] = type
  1020. info["size"] = len(records)
  1021. info["magic"] = POSIX_MAGIC
  1022. # Create pax header + record blocks.
  1023. return cls._create_header(info, USTAR_FORMAT) + \
  1024. cls._create_payload(records)
  1025. @classmethod
  1026. def frombuf(cls, buf):
  1027. """Construct a TarInfo object from a 512 byte string buffer.
  1028. """
  1029. if len(buf) == 0:
  1030. raise EmptyHeaderError("empty header")
  1031. if len(buf) != BLOCKSIZE:
  1032. raise TruncatedHeaderError("truncated header")
  1033. if buf.count(NUL) == BLOCKSIZE:
  1034. raise EOFHeaderError("end of file header")
  1035. chksum = nti(buf[148:156])
  1036. if chksum not in calc_chksums(buf):
  1037. raise InvalidHeaderError("bad checksum")
  1038. obj = cls()
  1039. obj.buf = buf
  1040. obj.name = nts(buf[0:100])
  1041. obj.mode = nti(buf[100:108])
  1042. obj.uid = nti(buf[108:116])
  1043. obj.gid = nti(buf[116:124])
  1044. obj.size = nti(buf[124:136])
  1045. obj.mtime = nti(buf[136:148])
  1046. obj.chksum = chksum
  1047. obj.type = buf[156:157]
  1048. obj.linkname = nts(buf[157:257])
  1049. obj.uname = nts(buf[265:297])
  1050. obj.gname = nts(buf[297:329])
  1051. obj.devmajor = nti(buf[329:337])
  1052. obj.devminor = nti(buf[337:345])
  1053. prefix = nts(buf[345:500])
  1054. # Old V7 tar format represents a directory as a regular
  1055. # file with a trailing slash.
  1056. if obj.type == AREGTYPE and obj.name.endswith("/"):
  1057. obj.type = DIRTYPE
  1058. # Remove redundant slashes from directories.
  1059. if obj.isdir():
  1060. obj.name = obj.name.rstrip("/")
  1061. # Reconstruct a ustar longname.
  1062. if prefix and obj.type not in GNU_TYPES:
  1063. obj.name = prefix + "/" + obj.name
  1064. return obj
  1065. @classmethod
  1066. def fromtarfile(cls, tarfile):
  1067. """Return the next TarInfo object from TarFile object
  1068. tarfile.
  1069. """
  1070. buf = tarfile.fileobj.read(BLOCKSIZE)
  1071. obj = cls.frombuf(buf)
  1072. obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
  1073. return obj._proc_member(tarfile)
  1074. #--------------------------------------------------------------------------
  1075. # The following are methods that are called depending on the type of a
  1076. # member. The entry point is _proc_member() which can be overridden in a
  1077. # subclass to add custom _proc_*() methods. A _proc_*() method MUST
  1078. # implement the following
  1079. # operations:
  1080. # 1. Set self.offset_data to the position where the data blocks begin,
  1081. # if there is data that follows.
  1082. # 2. Set tarfile.offset to the position where the next member's header will
  1083. # begin.
  1084. # 3. Return self or another valid TarInfo object.
  1085. def _proc_member(self, tarfile):
  1086. """Choose the right processing method depending on
  1087. the type and call it.
  1088. """
  1089. if self.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
  1090. return self._proc_gnulong(tarfile)
  1091. elif self.type == GNUTYPE_SPARSE:
  1092. return self._proc_sparse(tarfile)
  1093. elif self.type in (XHDTYPE, XGLTYPE, SOLARIS_XHDTYPE):
  1094. return self._proc_pax(tarfile)
  1095. else:
  1096. return self._proc_builtin(tarfile)
  1097. def _proc_builtin(self, tarfile):
  1098. """Process a builtin type or an unknown type which
  1099. will be treated as a regular file.
  1100. """
  1101. self.offset_data = tarfile.fileobj.tell()
  1102. offset = self.offset_data
  1103. if self.isreg() or self.type not in SUPPORTED_TYPES:
  1104. # Skip the following data blocks.
  1105. offset += self._block(self.size)
  1106. tarfile.offset = offset
  1107. # Patch the TarInfo object with saved global
  1108. # header information.
  1109. self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
  1110. return self
  1111. def _proc_gnulong(self, tarfile):
  1112. """Process the blocks that hold a GNU longname
  1113. or longlink member.
  1114. """
  1115. buf = tarfile.fileobj.read(self._block(self.size))
  1116. # Fetch the next header and process it.
  1117. try:
  1118. next = self.fromtarfile(tarfile)
  1119. except HeaderError:
  1120. raise SubsequentHeaderError("missing or bad subsequent header")
  1121. # Patch the TarInfo object from the next header with
  1122. # the longname information.
  1123. next.offset = self.offset
  1124. if self.type == GNUTYPE_LONGNAME:
  1125. next.name = nts(buf)
  1126. elif self.type == GNUTYPE_LONGLINK:
  1127. next.linkname = nts(buf)
  1128. return next
  1129. def _proc_sparse(self, tarfile):
  1130. """Process a GNU sparse header plus extra headers.
  1131. """
  1132. buf = self.buf
  1133. sp = _ringbuffer()
  1134. pos = 386
  1135. lastpos = 0L
  1136. realpos = 0L
  1137. # There are 4 possible sparse structs in the
  1138. # first header.
  1139. for i in xrange(4):
  1140. try:
  1141. offset = nti(buf[pos:pos + 12])
  1142. numbytes = nti(buf[pos + 12:pos + 24])
  1143. except ValueError:
  1144. break
  1145. if offset > lastpos:
  1146. sp.append(_hole(lastpos, offset - lastpos))
  1147. sp.append(_data(offset, numbytes, realpos))
  1148. realpos += numbytes
  1149. lastpos = offset + numbytes
  1150. pos += 24
  1151. isextended = ord(buf[482])
  1152. origsize = nti(buf[483:495])
  1153. # If the isextended flag is given,
  1154. # there are extra headers to process.
  1155. while isextended == 1:
  1156. buf = tarfile.fileobj.read(BLOCKSIZE)
  1157. pos = 0
  1158. for i in xrange(21):
  1159. try:
  1160. offset = nti(buf[pos:pos + 12])
  1161. numbytes = nti(buf[pos + 12:pos + 24])
  1162. except ValueError:
  1163. break
  1164. if offset > lastpos:
  1165. sp.append(_hole(lastpos, offset - lastpos))
  1166. sp.append(_data(offset, numbytes, realpos))
  1167. realpos += numbytes
  1168. lastpos = offset + numbytes
  1169. pos += 24
  1170. isextended = ord(buf[504])
  1171. if lastpos < origsize:
  1172. sp.append(_hole(lastpos, origsize - lastpos))
  1173. self.sparse = sp
  1174. self.offset_data = tarfile.fileobj.tell()
  1175. tarfile.offset = self.offset_data + self._block(self.size)
  1176. self.size = origsize
  1177. return self
  1178. def _proc_pax(self, tarfile):
  1179. """Process an extended or global header as described in
  1180. POSIX.1-2001.
  1181. """
  1182. # Read the header information.
  1183. buf = tarfile.fileobj.read(self._block(self.size))
  1184. # A pax header stores supplemental information for either
  1185. # the following file (extended) or all following files
  1186. # (global).
  1187. if self.type == XGLTYPE:
  1188. pax_headers = tarfile.pax_headers
  1189. else:
  1190. pax_headers = tarfile.pax_headers.copy()
  1191. # Parse pax header information. A record looks like that:
  1192. # "%d %s=%s\n" % (length, keyword, value). length is the size
  1193. # of the complete record including the length field itself and
  1194. # the newline. keyword and value are both UTF-8 encoded strings.
  1195. regex = re.compile(r"(\d+) ([^=]+)=", re.U)
  1196. pos = 0
  1197. while True:
  1198. match = regex.match(buf, pos)
  1199. if not match:
  1200. break
  1201. length, keyword = match.groups()
  1202. length = int(length)
  1203. value = buf[match.end(2) + 1:match.start(1) + length - 1]
  1204. keyword = keyword.decode("utf8")
  1205. value = value.decode("utf8")
  1206. pax_headers[keyword] = value
  1207. pos += length
  1208. # Fetch the next header.
  1209. try:
  1210. next = self.fromtarfile(tarfile)
  1211. except HeaderError:
  1212. raise SubsequentHeaderError("missing or bad subsequent header")
  1213. if self.type in (XHDTYPE, SOLARIS_XHDTYPE):
  1214. # Patch the TarInfo object with the extended header info.
  1215. next._apply_pax_info(pax_headers, tarfile.encoding, tarfile.errors)
  1216. next.offset = self.offset
  1217. if "size" in pax_headers:
  1218. # If the extended header replaces the size field,
  1219. # we need to recalculate the offset where the next
  1220. # header starts.
  1221. offset = next.offset_data
  1222. if next.isreg() or next.type not in SUPPORTED_TYPES:
  1223. offset += next._block(next.size)
  1224. tarfile.offset = offset
  1225. return next
  1226. def _apply_pax_info(self, pax_headers, encoding, errors):
  1227. """Replace fields with supplemental information from a previous
  1228. pax extended or global header.
  1229. """
  1230. for keyword, value in pax_headers.iteritems():
  1231. if keyword not in PAX_FIELDS:
  1232. continue
  1233. if keyword == "path":
  1234. value = value.rstrip("/")
  1235. if keyword in PAX_NUMBER_FIELDS:
  1236. try:
  1237. value = PAX_NUMBER_FIELDS[keyword](value)
  1238. except ValueError:
  1239. value = 0
  1240. else:
  1241. value = uts(value, encoding, errors)
  1242. setattr(self, keyword, value)
  1243. self.pax_headers = pax_headers.copy()
  1244. def _block(self, count):
  1245. """Round up a byte count by BLOCKSIZE and return it,
  1246. e.g. _block(834) => 1024.
  1247. """
  1248. blocks, remainder = divmod(count, BLOCKSIZE)
  1249. if remainder:
  1250. blocks += 1
  1251. return blocks * BLOCKSIZE
  1252. def isreg(self):
  1253. return self.type in REGULAR_TYPES
  1254. def isfile(self):
  1255. return self.isreg()
  1256. def isdir(self):
  1257. return self.type == DIRTYPE
  1258. def issym(self):
  1259. return self.type == SYMTYPE
  1260. def islnk(self):
  1261. return self.type == LNKTYPE
  1262. def ischr(self):
  1263. return self.type == CHRTYPE
  1264. def isblk(self):
  1265. return self.type == BLKTYPE
  1266. def isfifo(self):
  1267. return self.type == FIFOTYPE
  1268. def issparse(self):
  1269. return self.type == GNUTYPE_SPARSE
  1270. def isdev(self):
  1271. return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE)
  1272. # class TarInfo
  1273. class TarFile(object):
  1274. """The TarFile Class provides an interface to tar archives.
  1275. """
  1276. debug = 0 # May be set from 0 (no msgs) to 3 (all msgs)
  1277. dereference = False # If true, add content of linked file to the
  1278. # tar file, else the link.
  1279. ignore_zeros = False # If true, skips empty or invalid blocks and
  1280. # continues processing.
  1281. errorlevel = 1 # If 0, fatal errors only appear in debug
  1282. # messages (if debug >= 0). If > 0, errors
  1283. # are passed to the caller as exceptions.
  1284. format = DEFAULT_FORMAT # The format to use when creating an archive.
  1285. encoding = ENCODING # Encoding for 8-bit character strings.
  1286. errors = None # Error handler for unicode conversion.
  1287. tarinfo = TarInfo # The default TarInfo class to use.
  1288. fileobject = ExFileObject # The default ExFileObject class to use.
  1289. def __init__(self, name=None, mode="r", fileobj=None, format=None,
  1290. tarinfo=None, dereference=None, ignore_zeros=None, encoding=None,
  1291. errors=None, pax_headers=None, debug=None, errorlevel=None):
  1292. """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
  1293. read from an existing archive, 'a' to append data to an existing
  1294. file or 'w' to create a new file overwriting an existing one. `mode'
  1295. defaults to 'r'.
  1296. If `fileobj' is given, it is used for reading or writing data. If it
  1297. can be determined, `mode' is overridden by `fileobj's mode.
  1298. `fileobj' is not closed, when TarFile is closed.
  1299. """
  1300. if len(mode) > 1 or mode not in "raw":
  1301. raise ValueError("mode must be 'r', 'a' or 'w'")
  1302. self.mode = mode
  1303. self._mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode]
  1304. if not fileobj:
  1305. if self.mode == "a" and not os.path.exists(name):
  1306. # Create nonexistent files in append mode.
  1307. self.mode = "w"
  1308. self._mode = "wb"
  1309. fileobj = bltn_open(name, self._mode)
  1310. self._extfileobj = False
  1311. else:
  1312. if name is None and hasattr(fileobj, "name"):
  1313. name = fileobj.name
  1314. if hasattr(fileobj, "mode"):
  1315. self._mode = fileobj.mode
  1316. self._extfileobj = True
  1317. self.name = os.path.abspath(name) if name else None
  1318. self.fileobj = fileobj
  1319. # Init attributes.
  1320. if format is not None:
  1321. self.format = format
  1322. if tarinfo is not None:
  1323. self.tarinfo = tarinfo
  1324. if dereference is not None:
  1325. self.dereference = dereference
  1326. if ignore_zeros is not None:
  1327. self.ignore_zeros = ignore_zeros
  1328. if encoding is not None:
  1329. self.encoding = encoding
  1330. if errors is not None:
  1331. self.errors = errors
  1332. elif mode == "r":
  1333. self.errors = "utf-8"
  1334. else:
  1335. self.errors = "strict"
  1336. if pax_headers is not None and self.format == PAX_FORMAT:
  1337. self.pax_headers = pax_headers
  1338. else:
  1339. self.pax_headers = {}
  1340. if debug is not None:
  1341. self.debug = debug
  1342. if errorlevel is not None:
  1343. self.errorlevel = errorlevel
  1344. # Init datastructures.
  1345. self.closed = False
  1346. self.members = [] # list of members as TarInfo objects
  1347. self._loaded = False # flag if all members have been read
  1348. self.offset = self.fileobj.tell()
  1349. # current position in the archive file
  1350. self.inodes = {} # dictionary caching the inodes of
  1351. # archive members already added
  1352. try:
  1353. if self.mode == "r":
  1354. self.firstmember = None
  1355. self.firstmember = self.next()
  1356. if self.mode == "a":
  1357. # Move to the end of the archive,
  1358. # before the first empty block.
  1359. while True:
  1360. self.fileobj.seek(self.offset)
  1361. try:
  1362. tarinfo = self.tarinfo.fromtarfile(self)
  1363. self.members.append(tarinfo)
  1364. except EOFHeaderError:
  1365. self.fileobj.seek(self.offset)
  1366. break
  1367. except HeaderError, e:
  1368. raise ReadError(str(e))
  1369. if self.mode in "aw":
  1370. self._loaded = True
  1371. if self.pax_headers:
  1372. buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy())
  1373. self.fileobj.write(buf)
  1374. self.offset += len(buf)
  1375. except:
  1376. if not self._extfileobj:
  1377. self.fileobj.close()
  1378. self.closed = True
  1379. raise
  1380. def _getposix(self):
  1381. return self.format == USTAR_FORMAT
  1382. def _setposix(self, value):
  1383. import warnings
  1384. warnings.warn("use the format attribute instead", DeprecationWarning,
  1385. 2)
  1386. if value:
  1387. self.format = USTAR_FORMAT
  1388. else:
  1389. self.format = GNU_FORMAT
  1390. posix = property(_getposix, _setposix)
  1391. #--------------------------------------------------------------------------
  1392. # Below are the classmethods which act as alternate constructors to the
  1393. # TarFile class. The open() method is the only one that is needed for
  1394. # public use; it is the "super"-constructor and is able to select an
  1395. # adequate "sub"-constructor for a particular compression using the mapping
  1396. # from OPEN_METH.
  1397. #
  1398. # This concept allows one to subclass TarFile without losing the comfort of
  1399. # the super-constructor. A sub-constructor is registered and made available
  1400. # by adding it to the mapping in OPEN_METH.
  1401. @classmethod
  1402. def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs):
  1403. """Open a tar archive for reading, writing or appending. Return
  1404. an appropriate TarFile class.
  1405. mode:
  1406. 'r' or 'r:*' open for reading with transparent compression
  1407. 'r:' open for reading exclusively uncompressed
  1408. 'r:gz' open for reading with gzip compression
  1409. 'r:bz2' open for reading with bzip2 compression
  1410. 'a' or 'a:' open for appending, creating the file if necessary
  1411. 'w' or 'w:' open for writing without compression
  1412. 'w:gz' open for writing with gzip compression
  1413. 'w:bz2' open for writing with bzip2 compression
  1414. 'r|*' open a stream of tar blocks with transparent compression
  1415. 'r|' open an uncompressed stream of tar blocks for reading
  1416. 'r|gz' open a gzip compressed stream of tar blocks
  1417. 'r|bz2' open a bzip2 compressed stream of tar blocks
  1418. 'w|' open an uncompressed stream for writing
  1419. 'w|gz' open a gzip compressed stream for writing
  1420. 'w|bz2' open a bzip2 compressed stream for writing
  1421. """
  1422. if not name and not fileobj:
  1423. raise ValueError("nothing to open")
  1424. if mode in ("r", "r:*"):
  1425. # Find out which *open() is appropriate for opening the file.
  1426. for comptype in cls.OPEN_METH:
  1427. func = getattr(cls, cls.OPEN_METH[comptype])
  1428. if fileobj is not None:
  1429. saved_pos = fileobj.tell()
  1430. try:
  1431. return func(name, "r", fileobj, **kwargs)
  1432. except (ReadError, CompressionError), e:
  1433. if fileobj is not None:
  1434. fileobj.seek(saved_pos)
  1435. continue
  1436. raise ReadError("file could not be opened successfully")
  1437. elif ":" in mode:
  1438. filemode, comptype = mode.split(":", 1)
  1439. filemode = filemode or "r"
  1440. comptype = comptype or "tar"
  1441. # Select the *open() function according to
  1442. # given compression.
  1443. if comptype in cls.OPEN_METH:
  1444. func = getattr(cls, cls.OPEN_METH[comptype])
  1445. else:
  1446. raise CompressionError("unknown compression type %r" % comptype)
  1447. return func(name, filemode, fileobj, **kwargs)
  1448. elif "|" in mode:
  1449. filemode, comptype = mode.split("|", 1)
  1450. filemode = filemode or "r"
  1451. comptype = comptype or "tar"
  1452. if filemode not in "rw":
  1453. raise ValueError("mode must be 'r' or 'w'")
  1454. t = cls(name, filemode,
  1455. _Stream(name, filemode, comptype, fileobj, bufsize),
  1456. **kwargs)
  1457. t._extfileobj = False
  1458. return t
  1459. elif mode in "aw":
  1460. return cls.taropen(name, mode, fileobj, **kwargs)
  1461. raise ValueError("undiscernible mode")
  1462. @classmethod
  1463. def taropen(cls, name, mode="r", fileobj=None, **kwargs):
  1464. """Open uncompressed tar archive name for reading or writing.
  1465. """
  1466. if len(mode) > 1 or mode not in "raw":
  1467. raise ValueError("mode must be 'r', 'a' or 'w'")
  1468. return cls(name, mode, fileobj, **kwargs)
  1469. @classmethod
  1470. def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
  1471. """Open gzip compressed tar archive name for reading or writing.
  1472. Appending is not allowed.
  1473. """
  1474. if len(mode) > 1 or mode not in "rw":
  1475. raise ValueError("mode must be 'r' or 'w'")
  1476. try:
  1477. import gzip
  1478. gzip.GzipFile
  1479. except (ImportError, AttributeError):
  1480. raise CompressionError("gzip module is not available")
  1481. if fileobj is None:
  1482. fileobj = bltn_open(name, mode + "b")
  1483. try:
  1484. t = cls.taropen(name, mode,
  1485. gzip.GzipFile(name, mode, compresslevel, fileobj),
  1486. **kwargs)
  1487. except IOError:
  1488. raise ReadError("not a gzip file")
  1489. t._extfileobj = False
  1490. return t
  1491. @classmethod
  1492. def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
  1493. """Open bzip2 compressed tar archive name for reading or writing.
  1494. Appending is not allowed.
  1495. """
  1496. if len(mode) > 1 or mode not in "rw":
  1497. raise ValueError("mode must be 'r' or 'w'.")
  1498. try:
  1499. import bz2
  1500. except ImportError:
  1501. raise CompressionError("bz2 module is not available")
  1502. if fileobj is not None:
  1503. fileobj = _BZ2Proxy(fileobj, mode)
  1504. else:
  1505. fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
  1506. try:
  1507. t = cls.taropen(name, mode, fileobj, **kwargs)
  1508. except (IOError, EOFError):
  1509. raise ReadError("not a bzip2 file")
  1510. t._extfileobj = False
  1511. return t
  1512. # All *open() methods are registered here.
  1513. OPEN_METH = {
  1514. "tar": "taropen", # uncompressed tar
  1515. "gz": "gzopen", # gzip compressed tar
  1516. "bz2": "bz2open" # bzip2 compressed tar
  1517. }
  1518. #--------------------------------------------------------------------------
  1519. # The public methods which TarFile provides:
  1520. def close(self):
  1521. """Close the TarFile. In write-mode, two finishing zero blocks are
  1522. appended to the archive.
  1523. """
  1524. if self.closed:
  1525. return
  1526. if self.mode in "aw":
  1527. self.fileobj.write(NUL * (BLOCKSIZE * 2))
  1528. self.offset += (BLOCKSIZE * 2)
  1529. # fill up the end with zero-blocks
  1530. # (like option -b20 for tar does)
  1531. blocks, remainder = divmod(self.offset, RECORDSIZE)
  1532. if remainder > 0:
  1533. self.fileobj.write(NUL * (RECORDSIZE - remainder))
  1534. if not self._extfileobj:
  1535. self.fileobj.close()
  1536. self.closed = True
  1537. def getmember(self, name):
  1538. """Return a TarInfo object for member `name'. If `name' can not be
  1539. found in the archive, KeyError is raised. If a member occurs more
  1540. than once in the archive, its last occurrence is assumed to be the
  1541. most up-to-date version.
  1542. """
  1543. tarinfo = self._getmember(name)
  1544. if tarinfo is None:
  1545. raise KeyError("filename %r not found" % name)
  1546. return tarinfo
  1547. def getmembers(self):
  1548. """Return the members of the archive as a list of TarInfo objects. The
  1549. list has the same order as the members in the archive.
  1550. """
  1551. self._check()
  1552. if not self._loaded: # if we want to obtain a list of
  1553. self._load() # all members, we first have to
  1554. # scan the whole archive.
  1555. return self.members
  1556. def getnames(self):
  1557. """Return the members of the archive as a list of their names. It has
  1558. the same order as the list returned by getmembers().
  1559. """
  1560. return [tarinfo.name for tarinfo in self.getmembers()]
  1561. def gettarinfo(self, name=None, arcname=None, fileobj=None):
  1562. """Create a TarInfo object for either the file `name' or the file
  1563. object `fileobj' (using os.fstat on its file descriptor). You can
  1564. modify some of the TarInfo's attributes before you add it using
  1565. addfile(). If given, `arcname' specifies an alternative name for the
  1566. file in the archive.
  1567. """
  1568. self._check("aw")
  1569. # When fileobj is given, replace name by
  1570. # fileobj's real name.
  1571. if fileobj is not None:
  1572. name = fileobj.name
  1573. # Building the name of the member in the archive.
  1574. # Backward slashes are converted to forward slashes,
  1575. # Absolute paths are turned to relative paths.
  1576. if arcname is None:
  1577. arcname = name
  1578. drv, arcname = os.path.splitdrive(arcname)
  1579. arcname = arcname.replace(os.sep, "/")
  1580. arcname = arcname.lstrip("/")
  1581. # Now, fill the TarInfo object with
  1582. # information specific for the file.
  1583. tarinfo = self.tarinfo()
  1584. tarinfo.tarfile = self
  1585. # Use os.stat or os.lstat, depending on platform
  1586. # and if symlinks shall be resolved.
  1587. if fileobj is None:
  1588. if hasattr(os, "lstat") and not self.dereference:
  1589. statres = os.lstat(name)
  1590. else:
  1591. statres = os.stat(name)
  1592. else:
  1593. statres = os.fstat(fileobj.fileno())
  1594. linkname = ""
  1595. stmd = statres.st_mode
  1596. if stat.S_ISREG(stmd):
  1597. inode = (statres.st_ino, statres.st_dev)
  1598. if not self.dereference and statres.st_nlink > 1 and \
  1599. inode in self.inodes and arcname != self.inodes[inode]:
  1600. # Is it a hardlink to an already
  1601. # archived file?
  1602. type = LNKTYPE
  1603. linkname = self.inodes[inode]
  1604. else:
  1605. # The inode is added only if its valid.
  1606. # For win32 it is always 0.
  1607. type = REGTYPE
  1608. if inode[0]:
  1609. self.inodes[inode] = arcname
  1610. elif stat.S_ISDIR(stmd):
  1611. type = DIRTYPE
  1612. elif stat.S_ISFIFO(stmd):
  1613. type = FIFOTYPE
  1614. elif stat.S_ISLNK(stmd):
  1615. type = SYMTYPE
  1616. linkname = os.readlink(name)
  1617. elif stat.S_ISCHR(stmd):
  1618. type = CHRTYPE
  1619. elif stat.S_ISBLK(stmd):
  1620. type = BLKTYPE
  1621. else:
  1622. return None
  1623. # Fill the TarInfo object with all
  1624. # information we can get.
  1625. tarinfo.name = arcname
  1626. tarinfo.mode = stmd
  1627. tarinfo.uid = statres.st_uid
  1628. tarinfo.gid = statres.st_gid
  1629. if type == REGTYPE:
  1630. tarinfo.size = statres.st_size
  1631. else:
  1632. tarinfo.size = 0L
  1633. tarinfo.mtime = statres.st_mtime
  1634. tarinfo.type = type
  1635. tarinfo.linkname = linkname
  1636. if pwd:
  1637. try:
  1638. tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0]
  1639. except KeyError:
  1640. pass
  1641. if grp:
  1642. try:
  1643. tarinfo.gname = grp.getgrgid(tarinfo.gid)[0]
  1644. except KeyError:
  1645. pass
  1646. if type in (CHRTYPE, BLKTYPE):
  1647. if hasattr(os, "major") and hasattr(os, "minor"):
  1648. tarinfo.devmajor = os.major(statres.st_rdev)
  1649. tarinfo.devminor = os.minor(statres.st_rdev)
  1650. return tarinfo
  1651. def list(self, verbose=True):
  1652. """Print a table of contents to sys.stdout. If `verbose' is False, only
  1653. the names of the members are printed. If it is True, an `ls -l'-like
  1654. output is produced.
  1655. """
  1656. self._check()
  1657. for tarinfo in self:
  1658. if verbose:
  1659. print filemode(tarinfo.mode),
  1660. print "%s/%s" % (tarinfo.uname or tarinfo.uid,
  1661. tarinfo.gname or tarinfo.gid),
  1662. if tarinfo.ischr() or tarinfo.isblk():
  1663. print "%10s" % ("%d,%d" \
  1664. % (tarinfo.devmajor, tarinfo.devminor)),
  1665. else:
  1666. print "%10d" % tarinfo.size,
  1667. print "%d-%02d-%02d %02d:%02d:%02d" \
  1668. % time.localtime(tarinfo.mtime)[:6],
  1669. print tarinfo.name + ("/" if tarinfo.isdir() else ""),
  1670. if verbose:
  1671. if tarinfo.issym():
  1672. print "->", tarinfo.linkname,
  1673. if tarinfo.islnk():
  1674. print "link to", tarinfo.linkname,
  1675. print
  1676. def add(self, name, arcname=None, recursive=True, exclude=None, filter=None):
  1677. """Add the file `name' to the archive. `name' may be any type of file
  1678. (directory, fifo, symbolic link, etc.). If given, `arcname'
  1679. specifies an alternative name for the file in the archive.
  1680. Directories are added recursively by default. This can be avoided by
  1681. setting `recursive' to False. `exclude' is a function that should
  1682. return True for each filename to be excluded. `filter' is a function
  1683. that expects a TarInfo object argument and returns the changed
  1684. TarInfo object, if it returns None the TarInfo object will be
  1685. excluded from the archive.
  1686. """
  1687. self._check("aw")
  1688. if arcname is None:
  1689. arcname = name
  1690. # Exclude pathnames.
  1691. if exclude is not None:
  1692. import warnings
  1693. warnings.warn("use the filter argument instead",
  1694. DeprecationWarning, 2)
  1695. if exclude(name):
  1696. self._dbg(2, "tarfile: Excluded %r" % name)
  1697. return
  1698. # Skip if somebody tries to archive the archive...
  1699. if self.name is not None and os.path.abspath(name) == self.name:
  1700. self._dbg(2, "tarfile: Skipped %r" % name)
  1701. return
  1702. self._dbg(1, name)
  1703. # Create a TarInfo object from the file.
  1704. tarinfo = self.gettarinfo(name, arcname)
  1705. if tarinfo is None:
  1706. self._dbg(1, "tarfile: Unsupported type %r" % name)
  1707. return
  1708. # Change or exclude the TarInfo object.
  1709. if filter is not None:
  1710. tarinfo = filter(tarinfo)
  1711. if tarinfo is None:
  1712. self._dbg(2, "tarfile: Excluded %r" % name)
  1713. return
  1714. # Append the tar header and data to the archive.
  1715. if tarinfo.isreg():
  1716. f = bltn_open(name, "rb")
  1717. self.addfile(tarinfo, f)
  1718. f.close()
  1719. elif tarinfo.isdir():
  1720. self.addfile(tarinfo)
  1721. if recursive:
  1722. for f in os.listdir(name):
  1723. self.add(os.path.join(name, f), os.path.join(arcname, f),
  1724. recursive, exclude, filter)
  1725. else:
  1726. self.addfile(tarinfo)
  1727. def addfile(self, tarinfo, fileobj=None):
  1728. """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
  1729. given, tarinfo.size bytes are read from it and added to the archive.
  1730. You can create TarInfo objects using gettarinfo().
  1731. On Windows platforms, `fileobj' should always be opened with mode
  1732. 'rb' to avoid irritation about the file size.
  1733. """
  1734. self._check("aw")
  1735. tarinfo = copy.copy(tarinfo)
  1736. buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
  1737. self.fileobj.write(buf)
  1738. self.offset += len(buf)
  1739. # If there's data to follow, append it.
  1740. if fileobj is not None:
  1741. copyfileobj(fileobj, self.fileobj, tarinfo.size)
  1742. blocks, remainder = divmod(tarinfo.size, BLOCKSIZE)
  1743. if remainder > 0:
  1744. self.fileobj.write(NUL * (BLOCKSIZE - remainder))
  1745. blocks += 1
  1746. self.offset += blocks * BLOCKSIZE
  1747. self.members.append(tarinfo)
  1748. def extractall(self, path=".", members=None):
  1749. """Extract all members from the archive to the current working
  1750. directory and set owner, modification time and permissions on
  1751. directories afterwards. `path' specifies a different directory
  1752. to extract to. `members' is optional and must be a subset of the
  1753. list returned by getmembers().
  1754. """
  1755. directories = []
  1756. if members is None:
  1757. members = self
  1758. for tarinfo in members:
  1759. if tarinfo.isdir():
  1760. # Extract directories with a safe mode.
  1761. directories.append(tarinfo)
  1762. tarinfo = copy.copy(tarinfo)
  1763. tarinfo.mode = 0700
  1764. self.extract(tarinfo, path)
  1765. # Reverse sort directories.
  1766. directories.sort(key=operator.attrgetter('name'))
  1767. directories.reverse()
  1768. # Set correct owner, mtime and filemode on directories.
  1769. for tarinfo in directories:
  1770. dirpath = os.path.join(path, tarinfo.name)
  1771. try:
  1772. self.chown(tarinfo, dirpath)
  1773. self.utime(tarinfo, dirpath)
  1774. self.chmod(tarinfo, dirpath)
  1775. except ExtractError, e:
  1776. if self.errorlevel > 1:
  1777. raise
  1778. else:
  1779. self._dbg(1, "tarfile: %s" % e)
  1780. def extract(self, member, path=""):
  1781. """Extract a member from the archive to the current working directory,
  1782. using its full name. Its file information is extracted as accurately
  1783. as possible. `member' may be a filename or a TarInfo object. You can
  1784. specify a different directory using `path'.
  1785. """
  1786. self._check("r")
  1787. if isinstance(member, basestring):
  1788. tarinfo = self.getmember(member)
  1789. else:
  1790. tarinfo = member
  1791. # Prepare the link target for makelink().
  1792. if tarinfo.islnk():
  1793. tarinfo._link_target = os.path.join(path, tarinfo.linkname)
  1794. try:
  1795. self._extract_member(tarinfo, os.path.join(path, tarinfo.name))
  1796. except EnvironmentError, e:
  1797. if self.errorlevel > 0:
  1798. raise
  1799. else:
  1800. if e.filename is None:
  1801. self._dbg(1, "tarfile: %s" % e.strerror)
  1802. else:
  1803. self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename))
  1804. except ExtractError, e:
  1805. if self.errorlevel > 1:
  1806. raise
  1807. else:
  1808. self._dbg(1, "tarfile: %s" % e)
  1809. def extractfile(self, member):
  1810. """Extract a member from the archive as a file object. `member' may be
  1811. a filename or a TarInfo object. If `member' is a regular file, a
  1812. file-like object is returned. If `member' is a link, a file-like
  1813. object is constructed from the link's target. If `member' is none of
  1814. the above, None is returned.
  1815. The file-like object is read-only and provides the following
  1816. methods: read(), readline(), readlines(), seek() and tell()
  1817. """
  1818. self._check("r")
  1819. if isinstance(member, basestring):
  1820. tarinfo = self.getmember(member)
  1821. else:
  1822. tarinfo = member
  1823. if tarinfo.isreg():
  1824. return self.fileobject(self, tarinfo)
  1825. elif tarinfo.type not in SUPPORTED_TYPES:
  1826. # If a member's type is unknown, it is treated as a
  1827. # regular file.
  1828. return self.fileobject(self, tarinfo)
  1829. elif tarinfo.islnk() or tarinfo.issym():
  1830. if isinstance(self.fileobj, _Stream):
  1831. # A small but ugly workaround for the case that someone tries
  1832. # to extract a (sym)link as a file-object from a non-seekable
  1833. # stream of tar blocks.
  1834. raise StreamError("cannot extract (sym)link as file object")
  1835. else:
  1836. # A (sym)link's file object is its target's file object.
  1837. return self.extractfile(self._find_link_target(tarinfo))
  1838. else:
  1839. # If there's no data associated with the member (directory, chrdev,
  1840. # blkdev, etc.), return None instead of a file object.
  1841. return None
  1842. def _extract_member(self, tarinfo, targetpath):
  1843. """Extract the TarInfo object tarinfo to a physical
  1844. file called targetpath.
  1845. """
  1846. # Fetch the TarInfo object for the given name
  1847. # and build the destination pathname, replacing
  1848. # forward slashes to platform specific separators.
  1849. targetpath = targetpath.rstrip("/")
  1850. targetpath = targetpath.replace("/", os.sep)
  1851. # Create all upper directories.
  1852. upperdirs = os.path.dirname(targetpath)
  1853. if upperdirs and not os.path.exists(upperdirs):
  1854. # Create directories that are not part of the archive with
  1855. # default permissions.
  1856. os.makedirs(upperdirs)
  1857. if tarinfo.islnk() or tarinfo.issym():
  1858. self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
  1859. else:
  1860. self._dbg(1, tarinfo.name)
  1861. if tarinfo.isreg():
  1862. self.makefile(tarinfo, targetpath)
  1863. elif tarinfo.isdir():
  1864. self.makedir(tarinfo, targetpath)
  1865. elif tarinfo.isfifo():
  1866. self.makefifo(tarinfo, targetpath)
  1867. elif tarinfo.ischr() or tarinfo.isblk():
  1868. self.makedev(tarinfo, targetpath)
  1869. elif tarinfo.islnk() or tarinfo.issym():
  1870. self.makelink(tarinfo, targetpath)
  1871. elif tarinfo.type not in SUPPORTED_TYPES:
  1872. self.makeunknown(tarinfo, targetpath)
  1873. else:
  1874. self.makefile(tarinfo, targetpath)
  1875. self.chown(tarinfo, targetpath)
  1876. if not tarinfo.issym():
  1877. self.chmod(tarinfo, targetpath)
  1878. self.utime(tarinfo, targetpath)
  1879. #--------------------------------------------------------------------------
  1880. # Below are the different file methods. They are called via
  1881. # _extract_member() when extract() is called. They can be replaced in a
  1882. # subclass to implement other functionality.
  1883. def makedir(self, tarinfo, targetpath):
  1884. """Make a directory called targetpath.
  1885. """
  1886. try:
  1887. # Use a safe mode for the directory, the real mode is set
  1888. # later in _extract_member().
  1889. os.mkdir(targetpath, 0700)
  1890. except EnvironmentError, e:
  1891. if e.errno != errno.EEXIST:
  1892. raise
  1893. def makefile(self, tarinfo, targetpath):
  1894. """Make a file called targetpath.
  1895. """
  1896. source = self.extractfile(tarinfo)
  1897. target = bltn_open(targetpath, "wb")
  1898. copyfileobj(source, target)
  1899. source.close()
  1900. target.close()
  1901. def makeunknown(self, tarinfo, targetpath):
  1902. """Make a file from a TarInfo object with an unknown type
  1903. at targetpath.
  1904. """
  1905. self.makefile(tarinfo, targetpath)
  1906. self._dbg(1, "tarfile: Unknown file type %r, " \
  1907. "extracted as regular file." % tarinfo.type)
  1908. def makefifo(self, tarinfo, targetpath):
  1909. """Make a fifo called targetpath.
  1910. """
  1911. if hasattr(os, "mkfifo"):
  1912. os.mkfifo(targetpath)
  1913. else:
  1914. raise ExtractError("fifo not supported by system")
  1915. def makedev(self, tarinfo, targetpath):
  1916. """Make a character or block device called targetpath.
  1917. """
  1918. if not hasattr(os, "mknod") or not hasattr(os, "makedev"):
  1919. raise ExtractError("special devices not supported by system")
  1920. mode = tarinfo.mode
  1921. if tarinfo.isblk():
  1922. mode |= stat.S_IFBLK
  1923. else:
  1924. mode |= stat.S_IFCHR
  1925. os.mknod(targetpath, mode,
  1926. os.makedev(tarinfo.devmajor, tarinfo.devminor))
  1927. def makelink(self, tarinfo, targetpath):
  1928. """Make a (symbolic) link called targetpath. If it cannot be created
  1929. (platform limitation), we try to make a copy of the referenced file
  1930. instead of a link.
  1931. """
  1932. if hasattr(os, "symlink") and hasattr(os, "link"):
  1933. # For systems that support symbolic and hard links.
  1934. if tarinfo.issym():
  1935. if os.path.lexists(targetpath):
  1936. os.unlink(targetpath)
  1937. os.symlink(tarinfo.linkname, targetpath)
  1938. else:
  1939. # See extract().
  1940. if os.path.exists(tarinfo._link_target):
  1941. if os.path.lexists(targetpath):
  1942. os.unlink(targetpath)
  1943. os.link(tarinfo._link_target, targetpath)
  1944. else:
  1945. self._extract_member(self._find_link_target(tarinfo), targetpath)
  1946. else:
  1947. try:
  1948. self._extract_member(self._find_link_target(tarinfo), targetpath)
  1949. except KeyError:
  1950. raise ExtractError("unable to resolve link inside archive")
  1951. def chown(self, tarinfo, targetpath):
  1952. """Set owner of targetpath according to tarinfo.
  1953. """
  1954. if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
  1955. # We have to be root to do so.
  1956. try:
  1957. g = grp.getgrnam(tarinfo.gname)[2]
  1958. except KeyError:
  1959. try:
  1960. g = grp.getgrgid(tarinfo.gid)[2]
  1961. except KeyError:
  1962. g = os.getgid()
  1963. try:
  1964. u = pwd.getpwnam(tarinfo.uname)[2]
  1965. except KeyError:
  1966. try:
  1967. u = pwd.getpwuid(tarinfo.uid)[2]
  1968. except KeyError:
  1969. u = os.getuid()
  1970. try:
  1971. if tarinfo.issym() and hasattr(os, "lchown"):
  1972. os.lchown(targetpath, u, g)
  1973. else:
  1974. if sys.platform != "os2emx":
  1975. os.chown(targetpath, u, g)
  1976. except EnvironmentError, e:
  1977. raise ExtractError("could not change owner")
  1978. def chmod(self, tarinfo, targetpath):
  1979. """Set file permissions of targetpath according to tarinfo.
  1980. """
  1981. if hasattr(os, 'chmod'):
  1982. try:
  1983. os.chmod(targetpath, tarinfo.mode)
  1984. except EnvironmentError, e:
  1985. raise ExtractError("could not change mode")
  1986. def utime(self, tarinfo, targetpath):
  1987. """Set modification time of targetpath according to tarinfo.
  1988. """
  1989. if not hasattr(os, 'utime'):
  1990. return
  1991. try:
  1992. os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
  1993. except EnvironmentError, e:
  1994. raise ExtractError("could not change modification time")
  1995. #--------------------------------------------------------------------------
  1996. def next(self):
  1997. """Return the next member of the archive as a TarInfo object, when
  1998. TarFile is opened for reading. Return None if there is no more
  1999. available.
  2000. """
  2001. self._check("ra")
  2002. if self.firstmember is not None:
  2003. m = self.firstmember
  2004. self.firstmember = None
  2005. return m
  2006. # Read the next block.
  2007. self.fileobj.seek(self.offset)
  2008. tarinfo = None
  2009. while True:
  2010. try:
  2011. tarinfo = self.tarinfo.fromtarfile(self)
  2012. except EOFHeaderError, e:
  2013. if self.ignore_zeros:
  2014. self._dbg(2, "0x%X: %s" % (self.offset, e))
  2015. self.offset += BLOCKSIZE
  2016. continue
  2017. except InvalidHeaderError, e:
  2018. if self.ignore_zeros:
  2019. self._dbg(2, "0x%X: %s" % (self.offset, e))
  2020. self.offset += BLOCKSIZE
  2021. continue
  2022. elif self.offset == 0:
  2023. raise ReadError(str(e))
  2024. except EmptyHeaderError:
  2025. if self.offset == 0:
  2026. raise ReadError("empty file")
  2027. except TruncatedHeaderError, e:
  2028. if self.offset == 0:
  2029. raise ReadError(str(e))
  2030. except SubsequentHeaderError, e:
  2031. raise ReadError(str(e))
  2032. break
  2033. if tarinfo is not None:
  2034. self.members.append(tarinfo)
  2035. else:
  2036. self._loaded = True
  2037. return tarinfo
  2038. #--------------------------------------------------------------------------
  2039. # Little helper methods:
  2040. def _getmember(self, name, tarinfo=None, normalize=False):
  2041. """Find an archive member by name from bottom to top.
  2042. If tarinfo is given, it is used as the starting point.
  2043. """
  2044. # Ensure that all members have been loaded.
  2045. members = self.getmembers()
  2046. # Limit the member search list up to tarinfo.
  2047. if tarinfo is not None:
  2048. members = members[:members.index(tarinfo)]
  2049. if normalize:
  2050. name = os.path.normpath(name)
  2051. for member in reversed(members):
  2052. if normalize:
  2053. member_name = os.path.normpath(member.name)
  2054. else:
  2055. member_name = member.name
  2056. if name == member_name:
  2057. return member
  2058. def _load(self):
  2059. """Read through the entire archive file and look for readable
  2060. members.
  2061. """
  2062. while True:
  2063. tarinfo = self.next()
  2064. if tarinfo is None:
  2065. break
  2066. self._loaded = True
  2067. def _check(self, mode=None):
  2068. """Check if TarFile is still open, and if the operation's mode
  2069. corresponds to TarFile's mode.
  2070. """
  2071. if self.closed:
  2072. raise IOError("%s is closed" % self.__class__.__name__)
  2073. if mode is not None and self.mode not in mode:
  2074. raise IOError("bad operation for mode %r" % self.mode)
  2075. def _find_link_target(self, tarinfo):
  2076. """Find the target member of a symlink or hardlink member in the
  2077. archive.
  2078. """
  2079. if tarinfo.issym():
  2080. # Always search the entire archive.
  2081. linkname = os.path.dirname(tarinfo.name) + "/" + tarinfo.linkname
  2082. limit = None
  2083. else:
  2084. # Search the archive before the link, because a hard link is
  2085. # just a reference to an already archived file.
  2086. linkname = tarinfo.linkname
  2087. limit = tarinfo
  2088. member = self._getmember(linkname, tarinfo=limit, normalize=True)
  2089. if member is None:
  2090. raise KeyError("linkname %r not found" % linkname)
  2091. return member
  2092. def __iter__(self):
  2093. """Provide an iterator object.
  2094. """
  2095. if self._loaded:
  2096. return iter(self.members)
  2097. else:
  2098. return TarIter(self)
  2099. def _dbg(self, level, msg):
  2100. """Write debugging output to sys.stderr.
  2101. """
  2102. if level <= self.debug:
  2103. print >> sys.stderr, msg
  2104. def __enter__(self):
  2105. self._check()
  2106. return self
  2107. def __exit__(self, type, value, traceback):
  2108. if type is None:
  2109. self.close()
  2110. else:
  2111. # An exception occurred. We must not call close() because
  2112. # it would try to write end-of-archive blocks and padding.
  2113. if not self._extfileobj:
  2114. self.fileobj.close()
  2115. self.closed = True
  2116. # class TarFile
  2117. class TarIter:
  2118. """Iterator Class.
  2119. for tarinfo in TarFile(...):
  2120. suite...
  2121. """
  2122. def __init__(self, tarfile):
  2123. """Construct a TarIter object.
  2124. """
  2125. self.tarfile = tarfile
  2126. self.index = 0
  2127. def __iter__(self):
  2128. """Return iterator object.
  2129. """
  2130. return self
  2131. def next(self):
  2132. """Return the next item using TarFile's next() method.
  2133. When all members have been read, set TarFile as _loaded.
  2134. """
  2135. # Fix for SF #1100429: Under rare circumstances it can
  2136. # happen that getmembers() is called during iteration,
  2137. # which will cause TarIter to stop prematurely.
  2138. if not self.tarfile._loaded:
  2139. tarinfo = self.tarfile.next()
  2140. if not tarinfo:
  2141. self.tarfile._loaded = True
  2142. raise StopIteration
  2143. else:
  2144. try:
  2145. tarinfo = self.tarfile.members[self.index]
  2146. except IndexError:
  2147. raise StopIteration
  2148. self.index += 1
  2149. return tarinfo
  2150. # Helper classes for sparse file support
  2151. class _section:
  2152. """Base class for _data and _hole.
  2153. """
  2154. def __init__(self, offset, size):
  2155. self.offset = offset
  2156. self.size = size
  2157. def __contains__(self, offset):
  2158. return self.offset <= offset < self.offset + self.size
  2159. class _data(_section):
  2160. """Represent a data section in a sparse file.
  2161. """
  2162. def __init__(self, offset, size, realpos):
  2163. _section.__init__(self, offset, size)
  2164. self.realpos = realpos
  2165. class _hole(_section):
  2166. """Represent a hole section in a sparse file.
  2167. """
  2168. pass
  2169. class _ringbuffer(list):
  2170. """Ringbuffer class which increases performance
  2171. over a regular list.
  2172. """
  2173. def __init__(self):
  2174. self.idx = 0
  2175. def find(self, offset):
  2176. idx = self.idx
  2177. while True:
  2178. item = self[idx]
  2179. if offset in item:
  2180. break
  2181. idx += 1
  2182. if idx == len(self):
  2183. idx = 0
  2184. if idx == self.idx:
  2185. # End of File
  2186. return None
  2187. self.idx = idx
  2188. return item
  2189. #---------------------------------------------
  2190. # zipfile compatible TarFile class
  2191. #---------------------------------------------
  2192. TAR_PLAIN = 0 # zipfile.ZIP_STORED
  2193. TAR_GZIPPED = 8 # zipfile.ZIP_DEFLATED
  2194. class TarFileCompat:
  2195. """TarFile class compatible with standard module zipfile's
  2196. ZipFile class.
  2197. """
  2198. def __init__(self, file, mode="r", compression=TAR_PLAIN):
  2199. from warnings import warnpy3k
  2200. warnpy3k("the TarFileCompat class has been removed in Python 3.0",
  2201. stacklevel=2)
  2202. if compression == TAR_PLAIN:
  2203. self.tarfile = TarFile.taropen(file, mode)
  2204. elif compression == TAR_GZIPPED:
  2205. self.tarfile = TarFile.gzopen(file, mode)
  2206. else:
  2207. raise ValueError("unknown compression constant")
  2208. if mode[0:1] == "r":
  2209. members = self.tarfile.getmembers()
  2210. for m in members:
  2211. m.filename = m.name
  2212. m.file_size = m.size
  2213. m.date_time = time.gmtime(m.mtime)[:6]
  2214. def namelist(self):
  2215. return map(lambda m: m.name, self.infolist())
  2216. def infolist(self):
  2217. return filter(lambda m: m.type in REGULAR_TYPES,
  2218. self.tarfile.getmembers())
  2219. def printdir(self):
  2220. self.tarfile.list()
  2221. def testzip(self):
  2222. return
  2223. def getinfo(self, name):
  2224. return self.tarfile.getmember(name)
  2225. def read(self, name):
  2226. return self.tarfile.extractfile(self.tarfile.getmember(name)).read()
  2227. def write(self, filename, arcname=None, compress_type=None):
  2228. self.tarfile.add(filename, arcname)
  2229. def writestr(self, zinfo, bytes):
  2230. try:
  2231. from cStringIO import StringIO
  2232. except ImportError:
  2233. from StringIO import StringIO
  2234. import calendar
  2235. tinfo = TarInfo(zinfo.filename)
  2236. tinfo.size = len(bytes)
  2237. tinfo.mtime = calendar.timegm(zinfo.date_time)
  2238. self.tarfile.addfile(tinfo, StringIO(bytes))
  2239. def close(self):
  2240. self.tarfile.close()
  2241. #class TarFileCompat
  2242. #--------------------
  2243. # exported functions
  2244. #--------------------
  2245. def is_tarfile(name):
  2246. """Return True if name points to a tar archive that we
  2247. are able to handle, else return False.
  2248. """
  2249. try:
  2250. t = open(name)
  2251. t.close()
  2252. return True
  2253. except TarError:
  2254. return False
  2255. bltn_open = open
  2256. open = TarFile.open