Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tarfile.py 90KB

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