blob: 5f1a979ad0e563564e7dc1f9b20371f84cf88876 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002#-------------------------------------------------------------------
3# tarfile.py
4#-------------------------------------------------------------------
Christian Heimes9c1257e2007-11-04 11:37:22 +00005# Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de>
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00006# All rights reserved.
7#
8# Permission is hereby granted, free of charge, to any person
9# obtaining a copy of this software and associated documentation
10# files (the "Software"), to deal in the Software without
11# restriction, including without limitation the rights to use,
12# copy, modify, merge, publish, distribute, sublicense, and/or sell
13# copies of the Software, and to permit persons to whom the
14# Software is furnished to do so, subject to the following
15# conditions:
16#
17# The above copyright notice and this permission notice shall be
18# included in all copies or substantial portions of the Software.
19#
20# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28#
29"""Read from and write to tar format archives.
30"""
31
Guido van Rossumd8faa362007-04-27 19:54:29 +000032version = "0.9.0"
Guido van Rossum98297ee2007-11-06 21:34:58 +000033__author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"
Senthil Kumaran7c9719c2011-07-28 22:32:49 +080034__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 $"
Guido van Rossum98297ee2007-11-06 21:34:58 +000036__credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000037
38#---------
39# Imports
40#---------
Serhiy Storchakacf4a2f22015-03-11 17:18:03 +020041from builtins import open as bltn_open
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000042import sys
43import os
Eli Bendersky74c503b2012-01-03 06:26:13 +020044import io
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000045import shutil
46import stat
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000047import time
48import struct
Thomas Wouters89f507f2006-12-13 04:49:30 +000049import copy
Guido van Rossumd8faa362007-04-27 19:54:29 +000050import re
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000051
52try:
53 import grp, pwd
Brett Cannoncd171c82013-07-04 17:43:24 -040054except ImportError:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000055 grp = pwd = None
56
Brian Curtin16633fa2010-07-09 13:54:27 +000057# os.symlink on Windows prior to 6.0 raises NotImplementedError
58symlink_exception = (AttributeError, NotImplementedError)
59try:
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020060 # OSError (winerror=1314) will be raised if the caller does not hold the
Brian Curtin16633fa2010-07-09 13:54:27 +000061 # SeCreateSymbolicLinkPrivilege privilege
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020062 symlink_exception += (OSError,)
Brian Curtin16633fa2010-07-09 13:54:27 +000063except NameError:
64 pass
65
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000066# from tarfile import *
67__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
68
69#---------------------------------------------------------
70# tar constants
71#---------------------------------------------------------
Lars Gustäbelb506dc32007-08-07 18:36:16 +000072NUL = b"\0" # the null character
Guido van Rossumd8faa362007-04-27 19:54:29 +000073BLOCKSIZE = 512 # length of processing blocks
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000074RECORDSIZE = BLOCKSIZE * 20 # length of records
Lars Gustäbelb506dc32007-08-07 18:36:16 +000075GNU_MAGIC = b"ustar \0" # magic gnu tar string
76POSIX_MAGIC = b"ustar\x0000" # magic posix tar string
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000077
Guido van Rossumd8faa362007-04-27 19:54:29 +000078LENGTH_NAME = 100 # maximum length of a filename
79LENGTH_LINK = 100 # maximum length of a linkname
80LENGTH_PREFIX = 155 # maximum length of the prefix field
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000081
Lars Gustäbelb506dc32007-08-07 18:36:16 +000082REGTYPE = b"0" # regular file
83AREGTYPE = b"\0" # regular file
84LNKTYPE = b"1" # link (inside tarfile)
85SYMTYPE = b"2" # symbolic link
86CHRTYPE = b"3" # character special device
87BLKTYPE = b"4" # block special device
88DIRTYPE = b"5" # directory
89FIFOTYPE = b"6" # fifo special device
90CONTTYPE = b"7" # contiguous file
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000091
Lars Gustäbelb506dc32007-08-07 18:36:16 +000092GNUTYPE_LONGNAME = b"L" # GNU tar longname
93GNUTYPE_LONGLINK = b"K" # GNU tar longlink
94GNUTYPE_SPARSE = b"S" # GNU tar sparse file
Guido van Rossumd8faa362007-04-27 19:54:29 +000095
Lars Gustäbelb506dc32007-08-07 18:36:16 +000096XHDTYPE = b"x" # POSIX.1-2001 extended header
97XGLTYPE = b"g" # POSIX.1-2001 global header
98SOLARIS_XHDTYPE = b"X" # Solaris extended header
Guido van Rossumd8faa362007-04-27 19:54:29 +000099
100USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
101GNU_FORMAT = 1 # GNU tar format
102PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
103DEFAULT_FORMAT = GNU_FORMAT
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000104
105#---------------------------------------------------------
106# tarfile constants
107#---------------------------------------------------------
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108# File types that tarfile supports:
109SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
110 SYMTYPE, DIRTYPE, FIFOTYPE,
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000111 CONTTYPE, CHRTYPE, BLKTYPE,
112 GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
113 GNUTYPE_SPARSE)
114
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115# File types that will be treated as a regular file.
116REGULAR_TYPES = (REGTYPE, AREGTYPE,
117 CONTTYPE, GNUTYPE_SPARSE)
118
119# File types that are part of the GNU tar format.
120GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
121 GNUTYPE_SPARSE)
122
123# Fields from a pax header that override a TarInfo attribute.
124PAX_FIELDS = ("path", "linkpath", "size", "mtime",
125 "uid", "gid", "uname", "gname")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000126
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000127# Fields from a pax header that are affected by hdrcharset.
128PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}
129
Guido van Rossume7ba4952007-06-06 23:52:48 +0000130# Fields in a pax header that are numbers, all other fields
131# are treated as strings.
132PAX_NUMBER_FIELDS = {
133 "atime": float,
134 "ctime": float,
135 "mtime": float,
136 "uid": int,
137 "gid": int,
138 "size": int
139}
140
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000141#---------------------------------------------------------
Guido van Rossumd8faa362007-04-27 19:54:29 +0000142# initialization
143#---------------------------------------------------------
Victor Stinner0f35e2c2010-06-11 23:46:47 +0000144if os.name in ("nt", "ce"):
145 ENCODING = "utf-8"
146else:
147 ENCODING = sys.getfilesystemencoding()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148
149#---------------------------------------------------------
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000150# Some useful functions
151#---------------------------------------------------------
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000152
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000153def stn(s, length, encoding, errors):
154 """Convert a string to a null-terminated bytes object.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000155 """
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000156 s = s.encode(encoding, errors)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157 return s[:length] + (length - len(s)) * NUL
Thomas Wouters477c8d52006-05-27 19:21:47 +0000158
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000159def nts(s, encoding, errors):
160 """Convert a null-terminated bytes object to a string.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000161 """
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000162 p = s.find(b"\0")
163 if p != -1:
164 s = s[:p]
165 return s.decode(encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167def nti(s):
168 """Convert a number field to a python number.
169 """
170 # There are two possible encodings for a number field, see
171 # itn() below.
Lars Gustäbelac3d1372011-10-14 12:46:40 +0200172 if s[0] in (0o200, 0o377):
173 n = 0
174 for i in range(len(s) - 1):
175 n <<= 8
176 n += s[i + 1]
177 if s[0] == 0o377:
178 n = -(256 ** (len(s) - 1) - n)
179 else:
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000180 try:
Lars Gustäbelb7a688b2015-07-02 19:38:38 +0200181 s = nts(s, "ascii", "strict")
182 n = int(s.strip() or "0", 8)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000183 except ValueError:
Lars Gustäbel9520a432009-11-22 18:48:49 +0000184 raise InvalidHeaderError("invalid header")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000185 return n
186
Guido van Rossumd8faa362007-04-27 19:54:29 +0000187def itn(n, digits=8, format=DEFAULT_FORMAT):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000188 """Convert a python number to a number field.
189 """
190 # POSIX 1003.1-1988 requires numbers to be encoded as a string of
191 # octal digits followed by a null-byte, this allows values up to
192 # (8**(digits-1))-1. GNU tar allows storing numbers greater than
Lars Gustäbelac3d1372011-10-14 12:46:40 +0200193 # that if necessary. A leading 0o200 or 0o377 byte indicate this
194 # particular encoding, the following digits-1 bytes are a big-endian
195 # base-256 representation. This allows values up to (256**(digits-1))-1.
196 # A 0o200 byte indicates a positive number, a 0o377 byte a negative
197 # number.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 if 0 <= n < 8 ** (digits - 1):
Ethan Furmandf3ed242014-01-05 06:50:30 -0800199 s = bytes("%0*o" % (digits - 1, int(n)), "ascii") + NUL
Lars Gustäbelac3d1372011-10-14 12:46:40 +0200200 elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
201 if n >= 0:
202 s = bytearray([0o200])
203 else:
204 s = bytearray([0o377])
205 n = 256 ** digits + n
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206
Guido van Rossum805365e2007-05-07 22:24:25 +0000207 for i in range(digits - 1):
Lars Gustäbelac3d1372011-10-14 12:46:40 +0200208 s.insert(1, n & 0o377)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209 n >>= 8
Lars Gustäbelac3d1372011-10-14 12:46:40 +0200210 else:
211 raise ValueError("overflow in number field")
212
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 return s
214
215def calc_chksums(buf):
216 """Calculate the checksum for a member's header by summing up all
217 characters except for the chksum field which is treated as if
218 it was filled with spaces. According to the GNU tar sources,
219 some tars (Sun and NeXT) calculate chksum with signed char,
220 which will be different if there are chars in the buffer with
221 the high bit set. So we calculate two checksums, unsigned and
222 signed.
223 """
Ross Lagerwall468ff4c2012-05-17 19:49:27 +0200224 unsigned_chksum = 256 + sum(struct.unpack_from("148B8x356B", buf))
225 signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000226 return unsigned_chksum, signed_chksum
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000227
Lars Gustäbel03572682015-07-06 09:27:24 +0200228def copyfileobj(src, dst, length=None, exception=OSError):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000229 """Copy length bytes from fileobj src to fileobj dst.
230 If length is None, copy the entire content.
231 """
232 if length == 0:
233 return
234 if length is None:
235 shutil.copyfileobj(src, dst)
236 return
237
238 BUFSIZE = 16 * 1024
239 blocks, remainder = divmod(length, BUFSIZE)
Guido van Rossum805365e2007-05-07 22:24:25 +0000240 for b in range(blocks):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000241 buf = src.read(BUFSIZE)
242 if len(buf) < BUFSIZE:
Lars Gustäbel03572682015-07-06 09:27:24 +0200243 raise exception("unexpected end of data")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000244 dst.write(buf)
245
246 if remainder != 0:
247 buf = src.read(remainder)
248 if len(buf) < remainder:
Lars Gustäbel03572682015-07-06 09:27:24 +0200249 raise exception("unexpected end of data")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000250 dst.write(buf)
251 return
252
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000253def filemode(mode):
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200254 """Deprecated in this location; use stat.filemode."""
255 import warnings
256 warnings.warn("deprecated in favor of stat.filemode",
257 DeprecationWarning, 2)
258 return stat.filemode(mode)
259
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +0200260def _safe_print(s):
261 encoding = getattr(sys.stdout, 'encoding', None)
262 if encoding is not None:
263 s = s.encode(encoding, 'backslashreplace').decode(encoding)
264 print(s, end=' ')
265
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000266
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000267class TarError(Exception):
268 """Base exception."""
269 pass
270class ExtractError(TarError):
271 """General exception for extract errors."""
272 pass
273class ReadError(TarError):
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300274 """Exception for unreadable tar archives."""
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000275 pass
276class CompressionError(TarError):
277 """Exception for unavailable compression methods."""
278 pass
279class StreamError(TarError):
280 """Exception for unsupported operations on stream-like TarFiles."""
281 pass
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000282class HeaderError(TarError):
Lars Gustäbel9520a432009-11-22 18:48:49 +0000283 """Base exception for header errors."""
284 pass
285class EmptyHeaderError(HeaderError):
286 """Exception for empty headers."""
287 pass
288class TruncatedHeaderError(HeaderError):
289 """Exception for truncated headers."""
290 pass
291class EOFHeaderError(HeaderError):
292 """Exception for end of file headers."""
293 pass
294class InvalidHeaderError(HeaderError):
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000295 """Exception for invalid headers."""
296 pass
Lars Gustäbel9520a432009-11-22 18:48:49 +0000297class SubsequentHeaderError(HeaderError):
298 """Exception for missing and invalid extended headers."""
299 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000300
301#---------------------------
302# internal stream interface
303#---------------------------
304class _LowLevelFile:
305 """Low-level file object. Supports reading and writing.
306 It is used instead of a regular file object for streaming
307 access.
308 """
309
310 def __init__(self, name, mode):
311 mode = {
312 "r": os.O_RDONLY,
313 "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
314 }[mode]
315 if hasattr(os, "O_BINARY"):
316 mode |= os.O_BINARY
Lars Gustäbeld6eb70b2010-04-29 15:37:02 +0000317 self.fd = os.open(name, mode, 0o666)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000318
319 def close(self):
320 os.close(self.fd)
321
322 def read(self, size):
323 return os.read(self.fd, size)
324
325 def write(self, s):
326 os.write(self.fd, s)
327
328class _Stream:
329 """Class that serves as an adapter between TarFile and
330 a stream-like object. The stream-like object only
331 needs to have a read() or write() method and is accessed
332 blockwise. Use of gzip or bzip2 compression is possible.
333 A stream-like object could be for example: sys.stdin,
334 sys.stdout, a socket, a tape device etc.
335
336 _Stream is intended to be used only internally.
337 """
338
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000339 def __init__(self, name, mode, comptype, fileobj, bufsize):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000340 """Construct a _Stream object.
341 """
342 self._extfileobj = True
343 if fileobj is None:
344 fileobj = _LowLevelFile(name, mode)
345 self._extfileobj = False
346
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000347 if comptype == '*':
348 # Enable transparent compression detection for the
349 # stream interface
350 fileobj = _StreamProxy(fileobj)
351 comptype = fileobj.getcomptype()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000352
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000353 self.name = name or ""
354 self.mode = mode
355 self.comptype = comptype
356 self.fileobj = fileobj
357 self.bufsize = bufsize
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000358 self.buf = b""
Guido van Rossume2a383d2007-01-15 16:59:06 +0000359 self.pos = 0
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000360 self.closed = False
361
Antoine Pitrou605c2932010-09-23 20:15:14 +0000362 try:
363 if comptype == "gz":
364 try:
365 import zlib
Brett Cannoncd171c82013-07-04 17:43:24 -0400366 except ImportError:
Antoine Pitrou605c2932010-09-23 20:15:14 +0000367 raise CompressionError("zlib module is not available")
368 self.zlib = zlib
369 self.crc = zlib.crc32(b"")
370 if mode == "r":
371 self._init_read_gz()
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100372 self.exception = zlib.error
Antoine Pitrou605c2932010-09-23 20:15:14 +0000373 else:
374 self._init_write_gz()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000375
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100376 elif comptype == "bz2":
Antoine Pitrou605c2932010-09-23 20:15:14 +0000377 try:
378 import bz2
Brett Cannoncd171c82013-07-04 17:43:24 -0400379 except ImportError:
Antoine Pitrou605c2932010-09-23 20:15:14 +0000380 raise CompressionError("bz2 module is not available")
381 if mode == "r":
382 self.dbuf = b""
383 self.cmp = bz2.BZ2Decompressor()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200384 self.exception = OSError
Antoine Pitrou605c2932010-09-23 20:15:14 +0000385 else:
386 self.cmp = bz2.BZ2Compressor()
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100387
388 elif comptype == "xz":
389 try:
390 import lzma
Brett Cannoncd171c82013-07-04 17:43:24 -0400391 except ImportError:
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100392 raise CompressionError("lzma module is not available")
393 if mode == "r":
394 self.dbuf = b""
395 self.cmp = lzma.LZMADecompressor()
396 self.exception = lzma.LZMAError
397 else:
398 self.cmp = lzma.LZMACompressor()
399
400 elif comptype != "tar":
401 raise CompressionError("unknown compression type %r" % comptype)
402
Antoine Pitrou605c2932010-09-23 20:15:14 +0000403 except:
404 if not self._extfileobj:
405 self.fileobj.close()
406 self.closed = True
407 raise
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000408
409 def __del__(self):
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000410 if hasattr(self, "closed") and not self.closed:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000411 self.close()
412
413 def _init_write_gz(self):
414 """Initialize for writing with gzip compression.
415 """
416 self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
417 -self.zlib.MAX_WBITS,
418 self.zlib.DEF_MEM_LEVEL,
419 0)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000420 timestamp = struct.pack("<L", int(time.time()))
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000421 self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000422 if self.name.endswith(".gz"):
423 self.name = self.name[:-3]
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000424 # RFC1952 says we must use ISO-8859-1 for the FNAME field.
425 self.__write(self.name.encode("iso-8859-1", "replace") + NUL)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000426
427 def write(self, s):
428 """Write string s to the stream.
429 """
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000430 if self.comptype == "gz":
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000431 self.crc = self.zlib.crc32(s, self.crc)
432 self.pos += len(s)
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000433 if self.comptype != "tar":
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000434 s = self.cmp.compress(s)
435 self.__write(s)
436
437 def __write(self, s):
438 """Write string s to the stream if a whole new block
439 is ready to be written.
440 """
441 self.buf += s
442 while len(self.buf) > self.bufsize:
443 self.fileobj.write(self.buf[:self.bufsize])
444 self.buf = self.buf[self.bufsize:]
445
446 def close(self):
447 """Close the _Stream object. No operation should be
448 done on it afterwards.
449 """
450 if self.closed:
451 return
452
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000453 self.closed = True
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300454 try:
455 if self.mode == "w" and self.comptype != "tar":
456 self.buf += self.cmp.flush()
457
458 if self.mode == "w" and self.buf:
459 self.fileobj.write(self.buf)
460 self.buf = b""
461 if self.comptype == "gz":
462 # The native zlib crc is an unsigned 32-bit integer, but
463 # the Python wrapper implicitly casts that to a signed C
464 # long. So, on a 32-bit box self.crc may "look negative",
465 # while the same crc on a 64-bit box may "look positive".
466 # To avoid irksome warnings from the `struct` module, force
467 # it to look positive on all boxes.
468 self.fileobj.write(struct.pack("<L", self.crc & 0xffffffff))
469 self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
470 finally:
471 if not self._extfileobj:
472 self.fileobj.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000473
474 def _init_read_gz(self):
475 """Initialize for reading a gzip compressed fileobj.
476 """
477 self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000478 self.dbuf = b""
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000479
480 # taken from gzip.GzipFile with some alterations
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000481 if self.__read(2) != b"\037\213":
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482 raise ReadError("not a gzip file")
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000483 if self.__read(1) != b"\010":
Thomas Wouters477c8d52006-05-27 19:21:47 +0000484 raise CompressionError("unsupported compression method")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000485
486 flag = ord(self.__read(1))
487 self.__read(6)
488
489 if flag & 4:
490 xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
491 self.read(xlen)
492 if flag & 8:
493 while True:
494 s = self.__read(1)
495 if not s or s == NUL:
496 break
497 if flag & 16:
498 while True:
499 s = self.__read(1)
500 if not s or s == NUL:
501 break
502 if flag & 2:
503 self.__read(2)
504
505 def tell(self):
506 """Return the stream's file pointer position.
507 """
508 return self.pos
509
510 def seek(self, pos=0):
511 """Set the stream's file pointer to pos. Negative seeking
512 is forbidden.
513 """
514 if pos - self.pos >= 0:
515 blocks, remainder = divmod(pos - self.pos, self.bufsize)
Guido van Rossum805365e2007-05-07 22:24:25 +0000516 for i in range(blocks):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000517 self.read(self.bufsize)
518 self.read(remainder)
519 else:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000520 raise StreamError("seeking backwards is not allowed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000521 return self.pos
522
523 def read(self, size=None):
524 """Return the next size number of bytes from the stream.
525 If size is not defined, return all bytes of the stream
526 up to EOF.
527 """
528 if size is None:
529 t = []
530 while True:
531 buf = self._read(self.bufsize)
532 if not buf:
533 break
534 t.append(buf)
535 buf = "".join(t)
536 else:
537 buf = self._read(size)
538 self.pos += len(buf)
539 return buf
540
541 def _read(self, size):
542 """Return size bytes from the stream.
543 """
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000544 if self.comptype == "tar":
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000545 return self.__read(size)
546
547 c = len(self.dbuf)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000548 while c < size:
549 buf = self.__read(self.bufsize)
550 if not buf:
551 break
Guido van Rossumd8faa362007-04-27 19:54:29 +0000552 try:
553 buf = self.cmp.decompress(buf)
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100554 except self.exception:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000555 raise ReadError("invalid compressed data")
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000556 self.dbuf += buf
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000557 c += len(buf)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000558 buf = self.dbuf[:size]
559 self.dbuf = self.dbuf[size:]
560 return buf
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000561
562 def __read(self, size):
563 """Return size bytes from stream. If internal buffer is empty,
564 read another block from the stream.
565 """
566 c = len(self.buf)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000567 while c < size:
568 buf = self.fileobj.read(self.bufsize)
569 if not buf:
570 break
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000571 self.buf += buf
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000572 c += len(buf)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000573 buf = self.buf[:size]
574 self.buf = self.buf[size:]
575 return buf
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000576# class _Stream
577
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000578class _StreamProxy(object):
579 """Small proxy class that enables transparent compression
580 detection for the Stream interface (mode 'r|*').
581 """
582
583 def __init__(self, fileobj):
584 self.fileobj = fileobj
585 self.buf = self.fileobj.read(BLOCKSIZE)
586
587 def read(self, size):
588 self.read = self.fileobj.read
589 return self.buf
590
591 def getcomptype(self):
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100592 if self.buf.startswith(b"\x1f\x8b\x08"):
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000593 return "gz"
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100594 elif self.buf[0:3] == b"BZh" and self.buf[4:10] == b"1AY&SY":
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000595 return "bz2"
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100596 elif self.buf.startswith((b"\x5d\x00\x00\x80", b"\xfd7zXZ")):
597 return "xz"
598 else:
599 return "tar"
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000600
601 def close(self):
602 self.fileobj.close()
603# class StreamProxy
604
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000605#------------------------
606# Extraction file object
607#------------------------
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000608class _FileInFile(object):
609 """A thin wrapper around an existing file object that
610 provides a part of its data as an individual file
611 object.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000612 """
613
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000614 def __init__(self, fileobj, offset, size, blockinfo=None):
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000615 self.fileobj = fileobj
616 self.offset = offset
617 self.size = size
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000618 self.position = 0
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200619 self.name = getattr(fileobj, "name", None)
620 self.closed = False
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000621
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000622 if blockinfo is None:
623 blockinfo = [(0, size)]
624
625 # Construct a map with data and zero blocks.
626 self.map_index = 0
627 self.map = []
628 lastpos = 0
629 realpos = self.offset
630 for offset, size in blockinfo:
631 if offset > lastpos:
632 self.map.append((False, lastpos, offset, None))
633 self.map.append((True, offset, offset + size, realpos))
634 realpos += size
635 lastpos = offset + size
636 if lastpos < self.size:
637 self.map.append((False, lastpos, self.size, None))
638
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200639 def flush(self):
640 pass
641
642 def readable(self):
643 return True
644
645 def writable(self):
646 return False
647
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000648 def seekable(self):
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000649 return self.fileobj.seekable()
650
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000651 def tell(self):
652 """Return the current file position.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000653 """
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000654 return self.position
655
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200656 def seek(self, position, whence=io.SEEK_SET):
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000657 """Seek to a position in the file.
658 """
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200659 if whence == io.SEEK_SET:
660 self.position = min(max(position, 0), self.size)
661 elif whence == io.SEEK_CUR:
662 if position < 0:
663 self.position = max(self.position + position, 0)
664 else:
665 self.position = min(self.position + position, self.size)
666 elif whence == io.SEEK_END:
667 self.position = max(min(self.size + position, self.size), 0)
668 else:
669 raise ValueError("Invalid argument")
670 return self.position
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000671
672 def read(self, size=None):
673 """Read data from the file.
674 """
675 if size is None:
676 size = self.size - self.position
677 else:
678 size = min(size, self.size - self.position)
679
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000680 buf = b""
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000681 while size > 0:
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000682 while True:
683 data, start, stop, offset = self.map[self.map_index]
684 if start <= self.position < stop:
685 break
686 else:
687 self.map_index += 1
688 if self.map_index == len(self.map):
689 self.map_index = 0
690 length = min(size, stop - self.position)
691 if data:
Lars Gustäbeldd071042011-02-23 11:42:22 +0000692 self.fileobj.seek(offset + (self.position - start))
Lars Gustäbel03572682015-07-06 09:27:24 +0200693 b = self.fileobj.read(length)
694 if len(b) != length:
695 raise ReadError("unexpected end of data")
696 buf += b
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000697 else:
698 buf += NUL * length
699 size -= length
700 self.position += length
701 return buf
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000702
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200703 def readinto(self, b):
704 buf = self.read(len(b))
705 b[:len(buf)] = buf
706 return len(buf)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000707
708 def close(self):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000709 self.closed = True
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200710#class _FileInFile
Martin v. Löwisdf241532005-03-03 08:17:42 +0000711
Lars Gustäbelb062a2f2012-05-14 13:18:16 +0200712class ExFileObject(io.BufferedReader):
713
714 def __init__(self, tarfile, tarinfo):
715 fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
716 tarinfo.size, tarinfo.sparse)
717 super().__init__(fileobj)
718#class ExFileObject
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000719
720#------------------
721# Exported Classes
722#------------------
723class TarInfo(object):
724 """Informational class which holds the details about an
725 archive member given by a tar header block.
726 TarInfo objects are returned by TarFile.getmember(),
727 TarFile.getmembers() and TarFile.gettarinfo() and are
728 usually created internally.
729 """
730
Lars Gustäbelc2ea8c62008-04-14 10:05:48 +0000731 __slots__ = ("name", "mode", "uid", "gid", "size", "mtime",
732 "chksum", "type", "linkname", "uname", "gname",
733 "devmajor", "devminor",
734 "offset", "offset_data", "pax_headers", "sparse",
735 "tarfile", "_sparse_structs", "_link_target")
736
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000737 def __init__(self, name=""):
738 """Construct a TarInfo object. name is the optional name
739 of the member.
740 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000741 self.name = name # member name
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000742 self.mode = 0o644 # file permissions
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743 self.uid = 0 # user id
744 self.gid = 0 # group id
745 self.size = 0 # file size
746 self.mtime = 0 # modification time
747 self.chksum = 0 # header checksum
748 self.type = REGTYPE # member type
749 self.linkname = "" # link name
Lars Gustäbel331b8002010-10-04 15:18:47 +0000750 self.uname = "" # user name
751 self.gname = "" # group name
Thomas Wouters477c8d52006-05-27 19:21:47 +0000752 self.devmajor = 0 # device major number
753 self.devminor = 0 # device minor number
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000754
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755 self.offset = 0 # the tar header starts here
756 self.offset_data = 0 # the file's data starts here
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000757
Lars Gustäbelc2ea8c62008-04-14 10:05:48 +0000758 self.sparse = None # sparse member information
Guido van Rossumd8faa362007-04-27 19:54:29 +0000759 self.pax_headers = {} # pax header information
760
761 # In pax headers the "name" and "linkname" field are called
762 # "path" and "linkpath".
763 def _getpath(self):
764 return self.name
765 def _setpath(self, name):
766 self.name = name
767 path = property(_getpath, _setpath)
768
769 def _getlinkpath(self):
770 return self.linkname
771 def _setlinkpath(self, linkname):
772 self.linkname = linkname
773 linkpath = property(_getlinkpath, _setlinkpath)
774
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000775 def __repr__(self):
776 return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self))
777
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000778 def get_info(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000779 """Return the TarInfo's attributes as a dictionary.
780 """
781 info = {
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +0000782 "name": self.name,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000783 "mode": self.mode & 0o7777,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000784 "uid": self.uid,
785 "gid": self.gid,
786 "size": self.size,
787 "mtime": self.mtime,
788 "chksum": self.chksum,
789 "type": self.type,
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +0000790 "linkname": self.linkname,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000791 "uname": self.uname,
792 "gname": self.gname,
793 "devmajor": self.devmajor,
794 "devminor": self.devminor
795 }
796
797 if info["type"] == DIRTYPE and not info["name"].endswith("/"):
798 info["name"] += "/"
799
800 return info
801
Victor Stinnerde629d42010-05-05 21:43:57 +0000802 def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="surrogateescape"):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000803 """Return a tar header as a string of 512 byte blocks.
804 """
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000805 info = self.get_info()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000806
Guido van Rossumd8faa362007-04-27 19:54:29 +0000807 if format == USTAR_FORMAT:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000808 return self.create_ustar_header(info, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000809 elif format == GNU_FORMAT:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000810 return self.create_gnu_header(info, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000811 elif format == PAX_FORMAT:
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000812 return self.create_pax_header(info, encoding)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000813 else:
814 raise ValueError("invalid format")
815
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000816 def create_ustar_header(self, info, encoding, errors):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000817 """Return the object as a ustar header block.
818 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000819 info["magic"] = POSIX_MAGIC
820
821 if len(info["linkname"]) > LENGTH_LINK:
822 raise ValueError("linkname is too long")
823
824 if len(info["name"]) > LENGTH_NAME:
825 info["prefix"], info["name"] = self._posix_split_name(info["name"])
826
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000827 return self._create_header(info, USTAR_FORMAT, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000828
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000829 def create_gnu_header(self, info, encoding, errors):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000830 """Return the object as a GNU header block sequence.
831 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000832 info["magic"] = GNU_MAGIC
833
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000834 buf = b""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000835 if len(info["linkname"]) > LENGTH_LINK:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000836 buf += self._create_gnu_long_header(info["linkname"], GNUTYPE_LONGLINK, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000837
838 if len(info["name"]) > LENGTH_NAME:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000839 buf += self._create_gnu_long_header(info["name"], GNUTYPE_LONGNAME, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000840
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000841 return buf + self._create_header(info, GNU_FORMAT, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000842
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000843 def create_pax_header(self, info, encoding):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000844 """Return the object as a ustar header block. If it cannot be
845 represented this way, prepend a pax extended header sequence
846 with supplement information.
847 """
Guido van Rossumd8faa362007-04-27 19:54:29 +0000848 info["magic"] = POSIX_MAGIC
849 pax_headers = self.pax_headers.copy()
850
851 # Test string fields for values that exceed the field length or cannot
852 # be represented in ASCII encoding.
853 for name, hname, length in (
854 ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK),
855 ("uname", "uname", 32), ("gname", "gname", 32)):
856
Guido van Rossume7ba4952007-06-06 23:52:48 +0000857 if hname in pax_headers:
858 # The pax header has priority.
859 continue
860
Guido van Rossumd8faa362007-04-27 19:54:29 +0000861 # Try to encode the string as ASCII.
862 try:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000863 info[name].encode("ascii", "strict")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000864 except UnicodeEncodeError:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000865 pax_headers[hname] = info[name]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000866 continue
867
Guido van Rossume7ba4952007-06-06 23:52:48 +0000868 if len(info[name]) > length:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000869 pax_headers[hname] = info[name]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000870
871 # Test number fields for values that exceed the field limit or values
872 # that like to be stored as float.
873 for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000874 if name in pax_headers:
875 # The pax header has priority. Avoid overflow.
876 info[name] = 0
877 continue
878
Guido van Rossumd8faa362007-04-27 19:54:29 +0000879 val = info[name]
880 if not 0 <= val < 8 ** (digits - 1) or isinstance(val, float):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000881 pax_headers[name] = str(val)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000882 info[name] = 0
883
Guido van Rossume7ba4952007-06-06 23:52:48 +0000884 # Create a pax extended header if necessary.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000885 if pax_headers:
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000886 buf = self._create_pax_generic_header(pax_headers, XHDTYPE, encoding)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000887 else:
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000888 buf = b""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000889
Lars Gustäbel3741eff2007-08-21 12:17:05 +0000890 return buf + self._create_header(info, USTAR_FORMAT, "ascii", "replace")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000891
892 @classmethod
Lars Gustäbel3741eff2007-08-21 12:17:05 +0000893 def create_pax_global_header(cls, pax_headers):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000894 """Return the object as a pax global header block sequence.
895 """
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000896 return cls._create_pax_generic_header(pax_headers, XGLTYPE, "utf-8")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000897
898 def _posix_split_name(self, name):
899 """Split a name longer than 100 chars into a prefix
900 and a name part.
901 """
902 prefix = name[:LENGTH_PREFIX + 1]
903 while prefix and prefix[-1] != "/":
904 prefix = prefix[:-1]
905
906 name = name[len(prefix):]
907 prefix = prefix[:-1]
908
909 if not prefix or len(name) > LENGTH_NAME:
910 raise ValueError("name is too long")
911 return prefix, name
912
913 @staticmethod
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000914 def _create_header(info, format, encoding, errors):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000915 """Return a header block. info is a dictionary with file
916 information, format must be one of the *_FORMAT constants.
917 """
918 parts = [
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000919 stn(info.get("name", ""), 100, encoding, errors),
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000920 itn(info.get("mode", 0) & 0o7777, 8, format),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000921 itn(info.get("uid", 0), 8, format),
922 itn(info.get("gid", 0), 8, format),
923 itn(info.get("size", 0), 12, format),
924 itn(info.get("mtime", 0), 12, format),
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000925 b" ", # checksum field
Guido van Rossumd8faa362007-04-27 19:54:29 +0000926 info.get("type", REGTYPE),
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000927 stn(info.get("linkname", ""), 100, encoding, errors),
928 info.get("magic", POSIX_MAGIC),
Lars Gustäbel331b8002010-10-04 15:18:47 +0000929 stn(info.get("uname", ""), 32, encoding, errors),
930 stn(info.get("gname", ""), 32, encoding, errors),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931 itn(info.get("devmajor", 0), 8, format),
932 itn(info.get("devminor", 0), 8, format),
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000933 stn(info.get("prefix", ""), 155, encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000934 ]
935
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000936 buf = struct.pack("%ds" % BLOCKSIZE, b"".join(parts))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000937 chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
Lars Gustäbela280ca752007-08-28 07:34:33 +0000938 buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000939 return buf
940
941 @staticmethod
942 def _create_payload(payload):
943 """Return the string payload filled with zero bytes
944 up to the next 512 byte border.
945 """
946 blocks, remainder = divmod(len(payload), BLOCKSIZE)
947 if remainder > 0:
948 payload += (BLOCKSIZE - remainder) * NUL
949 return payload
950
951 @classmethod
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000952 def _create_gnu_long_header(cls, name, type, encoding, errors):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000953 """Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
954 for name.
955 """
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000956 name = name.encode(encoding, errors) + NUL
Guido van Rossumd8faa362007-04-27 19:54:29 +0000957
958 info = {}
959 info["name"] = "././@LongLink"
960 info["type"] = type
961 info["size"] = len(name)
962 info["magic"] = GNU_MAGIC
963
964 # create extended header + name blocks.
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000965 return cls._create_header(info, USTAR_FORMAT, encoding, errors) + \
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966 cls._create_payload(name)
967
968 @classmethod
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000969 def _create_pax_generic_header(cls, pax_headers, type, encoding):
970 """Return a POSIX.1-2008 extended or global header sequence
Guido van Rossumd8faa362007-04-27 19:54:29 +0000971 that contains a list of keyword, value pairs. The values
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000972 must be strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000973 """
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000974 # Check if one of the fields contains surrogate characters and thereby
975 # forces hdrcharset=BINARY, see _proc_pax() for more information.
976 binary = False
977 for keyword, value in pax_headers.items():
978 try:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000979 value.encode("utf-8", "strict")
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000980 except UnicodeEncodeError:
981 binary = True
982 break
983
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000984 records = b""
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000985 if binary:
986 # Put the hdrcharset field at the beginning of the header.
987 records += b"21 hdrcharset=BINARY\n"
988
Guido van Rossumd8faa362007-04-27 19:54:29 +0000989 for keyword, value in pax_headers.items():
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000990 keyword = keyword.encode("utf-8")
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000991 if binary:
992 # Try to restore the original byte representation of `value'.
993 # Needless to say, that the encoding must match the string.
994 value = value.encode(encoding, "surrogateescape")
995 else:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000996 value = value.encode("utf-8")
Lars Gustäbel1465cc22010-05-17 18:02:50 +0000997
Guido van Rossumd8faa362007-04-27 19:54:29 +0000998 l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n'
999 n = p = 0
1000 while True:
1001 n = l + len(str(p))
1002 if n == p:
1003 break
1004 p = n
Lars Gustäbela280ca752007-08-28 07:34:33 +00001005 records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +00001006
1007 # We use a hardcoded "././@PaxHeader" name like star does
1008 # instead of the one that POSIX recommends.
1009 info = {}
1010 info["name"] = "././@PaxHeader"
1011 info["type"] = type
1012 info["size"] = len(records)
1013 info["magic"] = POSIX_MAGIC
1014
1015 # Create pax header + record blocks.
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001016 return cls._create_header(info, USTAR_FORMAT, "ascii", "replace") + \
Guido van Rossumd8faa362007-04-27 19:54:29 +00001017 cls._create_payload(records)
1018
Guido van Rossum75b64e62005-01-16 00:16:11 +00001019 @classmethod
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001020 def frombuf(cls, buf, encoding, errors):
1021 """Construct a TarInfo object from a 512 byte bytes object.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001022 """
Lars Gustäbel9520a432009-11-22 18:48:49 +00001023 if len(buf) == 0:
1024 raise EmptyHeaderError("empty header")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 if len(buf) != BLOCKSIZE:
Lars Gustäbel9520a432009-11-22 18:48:49 +00001026 raise TruncatedHeaderError("truncated header")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027 if buf.count(NUL) == BLOCKSIZE:
Lars Gustäbel9520a432009-11-22 18:48:49 +00001028 raise EOFHeaderError("end of file header")
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001029
1030 chksum = nti(buf[148:156])
1031 if chksum not in calc_chksums(buf):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001032 raise InvalidHeaderError("bad checksum")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033
Guido van Rossumd8faa362007-04-27 19:54:29 +00001034 obj = cls()
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001035 obj.name = nts(buf[0:100], encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001036 obj.mode = nti(buf[100:108])
1037 obj.uid = nti(buf[108:116])
1038 obj.gid = nti(buf[116:124])
1039 obj.size = nti(buf[124:136])
1040 obj.mtime = nti(buf[136:148])
1041 obj.chksum = chksum
1042 obj.type = buf[156:157]
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001043 obj.linkname = nts(buf[157:257], encoding, errors)
1044 obj.uname = nts(buf[265:297], encoding, errors)
1045 obj.gname = nts(buf[297:329], encoding, errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001046 obj.devmajor = nti(buf[329:337])
1047 obj.devminor = nti(buf[337:345])
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001048 prefix = nts(buf[345:500], encoding, errors)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001049
Guido van Rossumd8faa362007-04-27 19:54:29 +00001050 # Old V7 tar format represents a directory as a regular
1051 # file with a trailing slash.
1052 if obj.type == AREGTYPE and obj.name.endswith("/"):
1053 obj.type = DIRTYPE
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001054
Lars Gustäbelc2ea8c62008-04-14 10:05:48 +00001055 # The old GNU sparse format occupies some of the unused
1056 # space in the buffer for up to 4 sparse structures.
1057 # Save the them for later processing in _proc_sparse().
1058 if obj.type == GNUTYPE_SPARSE:
1059 pos = 386
1060 structs = []
1061 for i in range(4):
1062 try:
1063 offset = nti(buf[pos:pos + 12])
1064 numbytes = nti(buf[pos + 12:pos + 24])
1065 except ValueError:
1066 break
1067 structs.append((offset, numbytes))
1068 pos += 24
1069 isextended = bool(buf[482])
1070 origsize = nti(buf[483:495])
1071 obj._sparse_structs = (structs, isextended, origsize)
1072
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073 # Remove redundant slashes from directories.
1074 if obj.isdir():
1075 obj.name = obj.name.rstrip("/")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001076
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077 # Reconstruct a ustar longname.
1078 if prefix and obj.type not in GNU_TYPES:
1079 obj.name = prefix + "/" + obj.name
1080 return obj
1081
1082 @classmethod
1083 def fromtarfile(cls, tarfile):
1084 """Return the next TarInfo object from TarFile object
1085 tarfile.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001086 """
Guido van Rossumd8faa362007-04-27 19:54:29 +00001087 buf = tarfile.fileobj.read(BLOCKSIZE)
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001088 obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001089 obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
1090 return obj._proc_member(tarfile)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001091
Guido van Rossumd8faa362007-04-27 19:54:29 +00001092 #--------------------------------------------------------------------------
1093 # The following are methods that are called depending on the type of a
1094 # member. The entry point is _proc_member() which can be overridden in a
1095 # subclass to add custom _proc_*() methods. A _proc_*() method MUST
1096 # implement the following
1097 # operations:
1098 # 1. Set self.offset_data to the position where the data blocks begin,
1099 # if there is data that follows.
1100 # 2. Set tarfile.offset to the position where the next member's header will
1101 # begin.
1102 # 3. Return self or another valid TarInfo object.
1103 def _proc_member(self, tarfile):
1104 """Choose the right processing method depending on
1105 the type and call it.
Thomas Wouters89f507f2006-12-13 04:49:30 +00001106 """
Guido van Rossumd8faa362007-04-27 19:54:29 +00001107 if self.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
1108 return self._proc_gnulong(tarfile)
1109 elif self.type == GNUTYPE_SPARSE:
1110 return self._proc_sparse(tarfile)
1111 elif self.type in (XHDTYPE, XGLTYPE, SOLARIS_XHDTYPE):
1112 return self._proc_pax(tarfile)
1113 else:
1114 return self._proc_builtin(tarfile)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001115
Guido van Rossumd8faa362007-04-27 19:54:29 +00001116 def _proc_builtin(self, tarfile):
1117 """Process a builtin type or an unknown type which
1118 will be treated as a regular file.
1119 """
1120 self.offset_data = tarfile.fileobj.tell()
1121 offset = self.offset_data
1122 if self.isreg() or self.type not in SUPPORTED_TYPES:
1123 # Skip the following data blocks.
1124 offset += self._block(self.size)
1125 tarfile.offset = offset
Thomas Wouters89f507f2006-12-13 04:49:30 +00001126
Guido van Rossume7ba4952007-06-06 23:52:48 +00001127 # Patch the TarInfo object with saved global
Guido van Rossumd8faa362007-04-27 19:54:29 +00001128 # header information.
Guido van Rossume7ba4952007-06-06 23:52:48 +00001129 self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001130
1131 return self
1132
1133 def _proc_gnulong(self, tarfile):
1134 """Process the blocks that hold a GNU longname
1135 or longlink member.
1136 """
1137 buf = tarfile.fileobj.read(self._block(self.size))
1138
1139 # Fetch the next header and process it.
Lars Gustäbel9520a432009-11-22 18:48:49 +00001140 try:
1141 next = self.fromtarfile(tarfile)
1142 except HeaderError:
1143 raise SubsequentHeaderError("missing or bad subsequent header")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001144
1145 # Patch the TarInfo object from the next header with
1146 # the longname information.
1147 next.offset = self.offset
1148 if self.type == GNUTYPE_LONGNAME:
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001149 next.name = nts(buf, tarfile.encoding, tarfile.errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001150 elif self.type == GNUTYPE_LONGLINK:
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001151 next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001152
1153 return next
1154
1155 def _proc_sparse(self, tarfile):
1156 """Process a GNU sparse header plus extra headers.
1157 """
Lars Gustäbelc2ea8c62008-04-14 10:05:48 +00001158 # We already collected some sparse structures in frombuf().
1159 structs, isextended, origsize = self._sparse_structs
1160 del self._sparse_structs
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161
Lars Gustäbelc2ea8c62008-04-14 10:05:48 +00001162 # Collect sparse structures from extended header blocks.
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001163 while isextended:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001164 buf = tarfile.fileobj.read(BLOCKSIZE)
1165 pos = 0
Guido van Rossum805365e2007-05-07 22:24:25 +00001166 for i in range(21):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001167 try:
1168 offset = nti(buf[pos:pos + 12])
1169 numbytes = nti(buf[pos + 12:pos + 24])
1170 except ValueError:
1171 break
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001172 if offset and numbytes:
1173 structs.append((offset, numbytes))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001174 pos += 24
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001175 isextended = bool(buf[504])
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001176 self.sparse = structs
Guido van Rossumd8faa362007-04-27 19:54:29 +00001177
1178 self.offset_data = tarfile.fileobj.tell()
1179 tarfile.offset = self.offset_data + self._block(self.size)
1180 self.size = origsize
Guido van Rossumd8faa362007-04-27 19:54:29 +00001181 return self
1182
1183 def _proc_pax(self, tarfile):
1184 """Process an extended or global header as described in
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001185 POSIX.1-2008.
Guido van Rossumd8faa362007-04-27 19:54:29 +00001186 """
1187 # Read the header information.
1188 buf = tarfile.fileobj.read(self._block(self.size))
1189
1190 # A pax header stores supplemental information for either
1191 # the following file (extended) or all following files
1192 # (global).
1193 if self.type == XGLTYPE:
1194 pax_headers = tarfile.pax_headers
1195 else:
1196 pax_headers = tarfile.pax_headers.copy()
1197
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001198 # Check if the pax header contains a hdrcharset field. This tells us
1199 # the encoding of the path, linkpath, uname and gname fields. Normally,
1200 # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1201 # implementations are allowed to store them as raw binary strings if
1202 # the translation to UTF-8 fails.
1203 match = re.search(br"\d+ hdrcharset=([^\n]+)\n", buf)
1204 if match is not None:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001205 pax_headers["hdrcharset"] = match.group(1).decode("utf-8")
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001206
1207 # For the time being, we don't care about anything other than "BINARY".
1208 # The only other value that is currently allowed by the standard is
1209 # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1210 hdrcharset = pax_headers.get("hdrcharset")
1211 if hdrcharset == "BINARY":
1212 encoding = tarfile.encoding
1213 else:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001214 encoding = "utf-8"
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001215
Guido van Rossumd8faa362007-04-27 19:54:29 +00001216 # Parse pax header information. A record looks like that:
1217 # "%d %s=%s\n" % (length, keyword, value). length is the size
1218 # of the complete record including the length field itself and
Guido van Rossume7ba4952007-06-06 23:52:48 +00001219 # the newline. keyword and value are both UTF-8 encoded strings.
Antoine Pitroufd036452008-08-19 17:56:33 +00001220 regex = re.compile(br"(\d+) ([^=]+)=")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001221 pos = 0
1222 while True:
1223 match = regex.match(buf, pos)
1224 if not match:
1225 break
1226
1227 length, keyword = match.groups()
1228 length = int(length)
1229 value = buf[match.end(2) + 1:match.start(1) + length - 1]
1230
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001231 # Normally, we could just use "utf-8" as the encoding and "strict"
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001232 # as the error handler, but we better not take the risk. For
1233 # example, GNU tar <= 1.23 is known to store filenames it cannot
1234 # translate to UTF-8 as raw strings (unfortunately without a
1235 # hdrcharset=BINARY header).
1236 # We first try the strict standard encoding, and if that fails we
1237 # fall back on the user's encoding and error handler.
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001238 keyword = self._decode_pax_field(keyword, "utf-8", "utf-8",
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001239 tarfile.errors)
1240 if keyword in PAX_NAME_FIELDS:
1241 value = self._decode_pax_field(value, encoding, tarfile.encoding,
1242 tarfile.errors)
1243 else:
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001244 value = self._decode_pax_field(value, "utf-8", "utf-8",
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001245 tarfile.errors)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001246
1247 pax_headers[keyword] = value
1248 pos += length
1249
Guido van Rossume7ba4952007-06-06 23:52:48 +00001250 # Fetch the next header.
Lars Gustäbel9520a432009-11-22 18:48:49 +00001251 try:
1252 next = self.fromtarfile(tarfile)
1253 except HeaderError:
1254 raise SubsequentHeaderError("missing or bad subsequent header")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001255
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001256 # Process GNU sparse information.
1257 if "GNU.sparse.map" in pax_headers:
1258 # GNU extended sparse format version 0.1.
1259 self._proc_gnusparse_01(next, pax_headers)
1260
1261 elif "GNU.sparse.size" in pax_headers:
1262 # GNU extended sparse format version 0.0.
1263 self._proc_gnusparse_00(next, pax_headers, buf)
1264
1265 elif pax_headers.get("GNU.sparse.major") == "1" and pax_headers.get("GNU.sparse.minor") == "0":
1266 # GNU extended sparse format version 1.0.
1267 self._proc_gnusparse_10(next, pax_headers, tarfile)
1268
Guido van Rossume7ba4952007-06-06 23:52:48 +00001269 if self.type in (XHDTYPE, SOLARIS_XHDTYPE):
Guido van Rossume7ba4952007-06-06 23:52:48 +00001270 # Patch the TarInfo object with the extended header info.
1271 next._apply_pax_info(pax_headers, tarfile.encoding, tarfile.errors)
1272 next.offset = self.offset
1273
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
1283 return next
1284
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001285 def _proc_gnusparse_00(self, next, pax_headers, buf):
1286 """Process a GNU tar extended sparse header, version 0.0.
1287 """
1288 offsets = []
1289 for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf):
1290 offsets.append(int(match.group(1)))
1291 numbytes = []
1292 for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf):
1293 numbytes.append(int(match.group(1)))
1294 next.sparse = list(zip(offsets, numbytes))
1295
1296 def _proc_gnusparse_01(self, next, pax_headers):
1297 """Process a GNU tar extended sparse header, version 0.1.
1298 """
1299 sparse = [int(x) for x in pax_headers["GNU.sparse.map"].split(",")]
1300 next.sparse = list(zip(sparse[::2], sparse[1::2]))
1301
1302 def _proc_gnusparse_10(self, next, pax_headers, tarfile):
1303 """Process a GNU tar extended sparse header, version 1.0.
1304 """
1305 fields = None
1306 sparse = []
1307 buf = tarfile.fileobj.read(BLOCKSIZE)
1308 fields, buf = buf.split(b"\n", 1)
1309 fields = int(fields)
1310 while len(sparse) < fields * 2:
1311 if b"\n" not in buf:
1312 buf += tarfile.fileobj.read(BLOCKSIZE)
1313 number, buf = buf.split(b"\n", 1)
1314 sparse.append(int(number))
1315 next.offset_data = tarfile.fileobj.tell()
1316 next.sparse = list(zip(sparse[::2], sparse[1::2]))
1317
Guido van Rossume7ba4952007-06-06 23:52:48 +00001318 def _apply_pax_info(self, pax_headers, encoding, errors):
1319 """Replace fields with supplemental information from a previous
1320 pax extended or global header.
1321 """
1322 for keyword, value in pax_headers.items():
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001323 if keyword == "GNU.sparse.name":
1324 setattr(self, "path", value)
1325 elif keyword == "GNU.sparse.size":
1326 setattr(self, "size", int(value))
1327 elif keyword == "GNU.sparse.realsize":
1328 setattr(self, "size", int(value))
1329 elif keyword in PAX_FIELDS:
1330 if keyword in PAX_NUMBER_FIELDS:
1331 try:
1332 value = PAX_NUMBER_FIELDS[keyword](value)
1333 except ValueError:
1334 value = 0
1335 if keyword == "path":
1336 value = value.rstrip("/")
1337 setattr(self, keyword, value)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001338
1339 self.pax_headers = pax_headers.copy()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001340
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001341 def _decode_pax_field(self, value, encoding, fallback_encoding, fallback_errors):
1342 """Decode a single field from a pax record.
1343 """
1344 try:
1345 return value.decode(encoding, "strict")
1346 except UnicodeDecodeError:
1347 return value.decode(fallback_encoding, fallback_errors)
1348
Guido van Rossumd8faa362007-04-27 19:54:29 +00001349 def _block(self, count):
1350 """Round up a byte count by BLOCKSIZE and return it,
1351 e.g. _block(834) => 1024.
1352 """
1353 blocks, remainder = divmod(count, BLOCKSIZE)
1354 if remainder:
1355 blocks += 1
1356 return blocks * BLOCKSIZE
Thomas Wouters89f507f2006-12-13 04:49:30 +00001357
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001358 def isreg(self):
1359 return self.type in REGULAR_TYPES
1360 def isfile(self):
1361 return self.isreg()
1362 def isdir(self):
1363 return self.type == DIRTYPE
1364 def issym(self):
1365 return self.type == SYMTYPE
1366 def islnk(self):
1367 return self.type == LNKTYPE
1368 def ischr(self):
1369 return self.type == CHRTYPE
1370 def isblk(self):
1371 return self.type == BLKTYPE
1372 def isfifo(self):
1373 return self.type == FIFOTYPE
1374 def issparse(self):
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00001375 return self.sparse is not None
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001376 def isdev(self):
1377 return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE)
1378# class TarInfo
1379
1380class TarFile(object):
1381 """The TarFile Class provides an interface to tar archives.
1382 """
1383
1384 debug = 0 # May be set from 0 (no msgs) to 3 (all msgs)
1385
1386 dereference = False # If true, add content of linked file to the
1387 # tar file, else the link.
1388
1389 ignore_zeros = False # If true, skips empty or invalid blocks and
1390 # continues processing.
1391
Lars Gustäbel365aff32009-12-13 11:42:29 +00001392 errorlevel = 1 # If 0, fatal errors only appear in debug
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001393 # messages (if debug >= 0). If > 0, errors
1394 # are passed to the caller as exceptions.
1395
Guido van Rossumd8faa362007-04-27 19:54:29 +00001396 format = DEFAULT_FORMAT # The format to use when creating an archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001397
Guido van Rossume7ba4952007-06-06 23:52:48 +00001398 encoding = ENCODING # Encoding for 8-bit character strings.
1399
1400 errors = None # Error handler for unicode conversion.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001401
Guido van Rossumd8faa362007-04-27 19:54:29 +00001402 tarinfo = TarInfo # The default TarInfo class to use.
1403
Lars Gustäbelb062a2f2012-05-14 13:18:16 +02001404 fileobject = ExFileObject # The file-object for extractfile().
Guido van Rossumd8faa362007-04-27 19:54:29 +00001405
1406 def __init__(self, name=None, mode="r", fileobj=None, format=None,
1407 tarinfo=None, dereference=None, ignore_zeros=None, encoding=None,
Victor Stinnerde629d42010-05-05 21:43:57 +00001408 errors="surrogateescape", pax_headers=None, debug=None, errorlevel=None):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001409 """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
1410 read from an existing archive, 'a' to append data to an existing
1411 file or 'w' to create a new file overwriting an existing one. `mode'
1412 defaults to 'r'.
1413 If `fileobj' is given, it is used for reading or writing data. If it
1414 can be determined, `mode' is overridden by `fileobj's mode.
1415 `fileobj' is not closed, when TarFile is closed.
1416 """
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001417 modes = {"r": "rb", "a": "r+b", "w": "wb"}
1418 if mode not in modes:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001419 raise ValueError("mode must be 'r', 'a' or 'w'")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001420 self.mode = mode
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001421 self._mode = modes[mode]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001422
1423 if not fileobj:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001424 if self.mode == "a" and not os.path.exists(name):
Thomas Wouterscf297e42007-02-23 15:07:44 +00001425 # Create nonexistent files in append mode.
Guido van Rossumd8faa362007-04-27 19:54:29 +00001426 self.mode = "w"
1427 self._mode = "wb"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001428 fileobj = bltn_open(name, self._mode)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001429 self._extfileobj = False
1430 else:
Serhiy Storchaka2c6a3ae2014-07-16 23:58:58 +03001431 if (name is None and hasattr(fileobj, "name") and
1432 isinstance(fileobj.name, (str, bytes))):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001433 name = fileobj.name
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001434 if hasattr(fileobj, "mode"):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001435 self._mode = fileobj.mode
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001436 self._extfileobj = True
Thomas Woutersed03b412007-08-28 21:37:11 +00001437 self.name = os.path.abspath(name) if name else None
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001438 self.fileobj = fileobj
1439
Guido van Rossumd8faa362007-04-27 19:54:29 +00001440 # Init attributes.
1441 if format is not None:
1442 self.format = format
1443 if tarinfo is not None:
1444 self.tarinfo = tarinfo
1445 if dereference is not None:
1446 self.dereference = dereference
1447 if ignore_zeros is not None:
1448 self.ignore_zeros = ignore_zeros
1449 if encoding is not None:
1450 self.encoding = encoding
Victor Stinnerde629d42010-05-05 21:43:57 +00001451 self.errors = errors
Guido van Rossume7ba4952007-06-06 23:52:48 +00001452
1453 if pax_headers is not None and self.format == PAX_FORMAT:
1454 self.pax_headers = pax_headers
1455 else:
1456 self.pax_headers = {}
1457
Guido van Rossumd8faa362007-04-27 19:54:29 +00001458 if debug is not None:
1459 self.debug = debug
1460 if errorlevel is not None:
1461 self.errorlevel = errorlevel
1462
1463 # Init datastructures.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001464 self.closed = False
1465 self.members = [] # list of members as TarInfo objects
1466 self._loaded = False # flag if all members have been read
Christian Heimesd8654cf2007-12-02 15:22:16 +00001467 self.offset = self.fileobj.tell()
1468 # current position in the archive file
Thomas Wouters477c8d52006-05-27 19:21:47 +00001469 self.inodes = {} # dictionary caching the inodes of
1470 # archive members already added
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001471
Lars Gustäbel7b465392009-11-18 20:29:25 +00001472 try:
1473 if self.mode == "r":
1474 self.firstmember = None
1475 self.firstmember = self.next()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001476
Lars Gustäbel7b465392009-11-18 20:29:25 +00001477 if self.mode == "a":
1478 # Move to the end of the archive,
1479 # before the first empty block.
Lars Gustäbel7b465392009-11-18 20:29:25 +00001480 while True:
Lars Gustäbel9520a432009-11-22 18:48:49 +00001481 self.fileobj.seek(self.offset)
1482 try:
1483 tarinfo = self.tarinfo.fromtarfile(self)
1484 self.members.append(tarinfo)
1485 except EOFHeaderError:
1486 self.fileobj.seek(self.offset)
Lars Gustäbel7b465392009-11-18 20:29:25 +00001487 break
Lars Gustäbel9520a432009-11-22 18:48:49 +00001488 except HeaderError as e:
1489 raise ReadError(str(e))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001490
Lars Gustäbel7b465392009-11-18 20:29:25 +00001491 if self.mode in "aw":
1492 self._loaded = True
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001493
Lars Gustäbel7b465392009-11-18 20:29:25 +00001494 if self.pax_headers:
1495 buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy())
1496 self.fileobj.write(buf)
1497 self.offset += len(buf)
1498 except:
1499 if not self._extfileobj:
1500 self.fileobj.close()
1501 self.closed = True
1502 raise
Guido van Rossumd8faa362007-04-27 19:54:29 +00001503
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001504 #--------------------------------------------------------------------------
1505 # Below are the classmethods which act as alternate constructors to the
1506 # TarFile class. The open() method is the only one that is needed for
1507 # public use; it is the "super"-constructor and is able to select an
1508 # adequate "sub"-constructor for a particular compression using the mapping
1509 # from OPEN_METH.
1510 #
1511 # This concept allows one to subclass TarFile without losing the comfort of
1512 # the super-constructor. A sub-constructor is registered and made available
1513 # by adding it to the mapping in OPEN_METH.
1514
Guido van Rossum75b64e62005-01-16 00:16:11 +00001515 @classmethod
Guido van Rossumd8faa362007-04-27 19:54:29 +00001516 def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001517 """Open a tar archive for reading, writing or appending. Return
1518 an appropriate TarFile class.
1519
1520 mode:
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001521 'r' or 'r:*' open for reading with transparent compression
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001522 'r:' open for reading exclusively uncompressed
1523 'r:gz' open for reading with gzip compression
1524 'r:bz2' open for reading with bzip2 compression
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001525 'r:xz' open for reading with lzma compression
Thomas Wouterscf297e42007-02-23 15:07:44 +00001526 'a' or 'a:' open for appending, creating the file if necessary
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001527 'w' or 'w:' open for writing without compression
1528 'w:gz' open for writing with gzip compression
1529 'w:bz2' open for writing with bzip2 compression
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001530 'w:xz' open for writing with lzma compression
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001531
1532 'r|*' open a stream of tar blocks with transparent compression
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001533 'r|' open an uncompressed stream of tar blocks for reading
1534 'r|gz' open a gzip compressed stream of tar blocks
1535 'r|bz2' open a bzip2 compressed stream of tar blocks
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001536 'r|xz' open an lzma compressed stream of tar blocks
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001537 'w|' open an uncompressed stream for writing
1538 'w|gz' open a gzip compressed stream for writing
1539 'w|bz2' open a bzip2 compressed stream for writing
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001540 'w|xz' open an lzma compressed stream for writing
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001541 """
1542
1543 if not name and not fileobj:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001544 raise ValueError("nothing to open")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001545
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001546 if mode in ("r", "r:*"):
1547 # Find out which *open() is appropriate for opening the file.
1548 for comptype in cls.OPEN_METH:
1549 func = getattr(cls, cls.OPEN_METH[comptype])
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001550 if fileobj is not None:
1551 saved_pos = fileobj.tell()
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001552 try:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001553 return func(name, "r", fileobj, **kwargs)
1554 except (ReadError, CompressionError) as e:
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001555 if fileobj is not None:
1556 fileobj.seek(saved_pos)
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001557 continue
Thomas Wouters477c8d52006-05-27 19:21:47 +00001558 raise ReadError("file could not be opened successfully")
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001559
1560 elif ":" in mode:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001561 filemode, comptype = mode.split(":", 1)
1562 filemode = filemode or "r"
1563 comptype = comptype or "tar"
1564
1565 # Select the *open() function according to
1566 # given compression.
1567 if comptype in cls.OPEN_METH:
1568 func = getattr(cls, cls.OPEN_METH[comptype])
1569 else:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001570 raise CompressionError("unknown compression type %r" % comptype)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001571 return func(name, filemode, fileobj, **kwargs)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001572
1573 elif "|" in mode:
1574 filemode, comptype = mode.split("|", 1)
1575 filemode = filemode or "r"
1576 comptype = comptype or "tar"
1577
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001578 if filemode not in ("r", "w"):
Thomas Wouters477c8d52006-05-27 19:21:47 +00001579 raise ValueError("mode must be 'r' or 'w'")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001580
Antoine Pitrou605c2932010-09-23 20:15:14 +00001581 stream = _Stream(name, filemode, comptype, fileobj, bufsize)
1582 try:
1583 t = cls(name, filemode, stream, **kwargs)
1584 except:
1585 stream.close()
1586 raise
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001587 t._extfileobj = False
1588 return t
1589
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001590 elif mode in ("a", "w"):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001591 return cls.taropen(name, mode, fileobj, **kwargs)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001592
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593 raise ValueError("undiscernible mode")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001594
Guido van Rossum75b64e62005-01-16 00:16:11 +00001595 @classmethod
Guido van Rossumd8faa362007-04-27 19:54:29 +00001596 def taropen(cls, name, mode="r", fileobj=None, **kwargs):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001597 """Open uncompressed tar archive name for reading or writing.
1598 """
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001599 if mode not in ("r", "a", "w"):
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600 raise ValueError("mode must be 'r', 'a' or 'w'")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001601 return cls(name, mode, fileobj, **kwargs)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001602
Guido van Rossum75b64e62005-01-16 00:16:11 +00001603 @classmethod
Guido van Rossumd8faa362007-04-27 19:54:29 +00001604 def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001605 """Open gzip compressed tar archive name for reading or writing.
1606 Appending is not allowed.
1607 """
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001608 if mode not in ("r", "w"):
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609 raise ValueError("mode must be 'r' or 'w'")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001610
1611 try:
1612 import gzip
Neal Norwitz4ec68242003-04-11 03:05:56 +00001613 gzip.GzipFile
1614 except (ImportError, AttributeError):
Thomas Wouters477c8d52006-05-27 19:21:47 +00001615 raise CompressionError("gzip module is not available")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001616
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001617 try:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001618 fileobj = gzip.GzipFile(name, mode + "b", compresslevel, fileobj)
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001619 except OSError:
1620 if fileobj is not None and mode == 'r':
1621 raise ReadError("not a gzip file")
1622 raise
1623
1624 try:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001625 t = cls.taropen(name, mode, fileobj, **kwargs)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001626 except OSError:
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001627 fileobj.close()
1628 if mode == 'r':
1629 raise ReadError("not a gzip file")
1630 raise
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001631 except:
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001632 fileobj.close()
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001633 raise
Serhiy Storchaka9fbec7a2014-01-18 15:53:05 +02001634 t._extfileobj = False
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001635 return t
1636
Guido van Rossum75b64e62005-01-16 00:16:11 +00001637 @classmethod
Guido van Rossumd8faa362007-04-27 19:54:29 +00001638 def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001639 """Open bzip2 compressed tar archive name for reading or writing.
1640 Appending is not allowed.
1641 """
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +02001642 if mode not in ("r", "w"):
Thomas Wouters477c8d52006-05-27 19:21:47 +00001643 raise ValueError("mode must be 'r' or 'w'.")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001644
1645 try:
1646 import bz2
Brett Cannoncd171c82013-07-04 17:43:24 -04001647 except ImportError:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001648 raise CompressionError("bz2 module is not available")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001649
Nadeem Vawdaaebcdba2012-06-04 23:31:20 +02001650 fileobj = bz2.BZ2File(fileobj or name, mode,
1651 compresslevel=compresslevel)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001652
1653 try:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001654 t = cls.taropen(name, mode, fileobj, **kwargs)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001655 except (OSError, EOFError):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001656 fileobj.close()
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001657 if mode == 'r':
1658 raise ReadError("not a bzip2 file")
1659 raise
Serhiy Storchakae413cde2014-01-18 16:28:08 +02001660 except:
1661 fileobj.close()
1662 raise
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001663 t._extfileobj = False
1664 return t
1665
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001666 @classmethod
Lars Gustäbelc5e11992012-01-18 14:01:17 +01001667 def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001668 """Open lzma compressed tar archive name for reading or writing.
1669 Appending is not allowed.
1670 """
1671 if mode not in ("r", "w"):
1672 raise ValueError("mode must be 'r' or 'w'")
1673
1674 try:
1675 import lzma
Brett Cannoncd171c82013-07-04 17:43:24 -04001676 except ImportError:
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001677 raise CompressionError("lzma module is not available")
1678
Nadeem Vawda33c34da2012-06-04 23:34:07 +02001679 fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001680
1681 try:
1682 t = cls.taropen(name, mode, fileobj, **kwargs)
1683 except (lzma.LZMAError, EOFError):
1684 fileobj.close()
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001685 if mode == 'r':
1686 raise ReadError("not an lzma file")
1687 raise
Serhiy Storchakae413cde2014-01-18 16:28:08 +02001688 except:
1689 fileobj.close()
1690 raise
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001691 t._extfileobj = False
1692 return t
1693
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001694 # All *open() methods are registered here.
1695 OPEN_METH = {
1696 "tar": "taropen", # uncompressed tar
1697 "gz": "gzopen", # gzip compressed tar
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +01001698 "bz2": "bz2open", # bzip2 compressed tar
1699 "xz": "xzopen" # lzma compressed tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001700 }
1701
1702 #--------------------------------------------------------------------------
1703 # The public methods which TarFile provides:
1704
1705 def close(self):
1706 """Close the TarFile. In write-mode, two finishing zero blocks are
1707 appended to the archive.
1708 """
1709 if self.closed:
1710 return
1711
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001712 self.closed = True
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +03001713 try:
1714 if self.mode in "aw":
1715 self.fileobj.write(NUL * (BLOCKSIZE * 2))
1716 self.offset += (BLOCKSIZE * 2)
1717 # fill up the end with zero-blocks
1718 # (like option -b20 for tar does)
1719 blocks, remainder = divmod(self.offset, RECORDSIZE)
1720 if remainder > 0:
1721 self.fileobj.write(NUL * (RECORDSIZE - remainder))
1722 finally:
1723 if not self._extfileobj:
1724 self.fileobj.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001725
1726 def getmember(self, name):
1727 """Return a TarInfo object for member `name'. If `name' can not be
1728 found in the archive, KeyError is raised. If a member occurs more
Mark Dickinson934896d2009-02-21 20:59:32 +00001729 than once in the archive, its last occurrence is assumed to be the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001730 most up-to-date version.
1731 """
Martin v. Löwisf3c56112004-09-18 09:08:52 +00001732 tarinfo = self._getmember(name)
1733 if tarinfo is None:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001734 raise KeyError("filename %r not found" % name)
Martin v. Löwisf3c56112004-09-18 09:08:52 +00001735 return tarinfo
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001736
1737 def getmembers(self):
1738 """Return the members of the archive as a list of TarInfo objects. The
1739 list has the same order as the members in the archive.
1740 """
1741 self._check()
1742 if not self._loaded: # if we want to obtain a list of
1743 self._load() # all members, we first have to
1744 # scan the whole archive.
1745 return self.members
1746
1747 def getnames(self):
1748 """Return the members of the archive as a list of their names. It has
1749 the same order as the list returned by getmembers().
1750 """
Martin v. Löwisf3c56112004-09-18 09:08:52 +00001751 return [tarinfo.name for tarinfo in self.getmembers()]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001752
1753 def gettarinfo(self, name=None, arcname=None, fileobj=None):
1754 """Create a TarInfo object for either the file `name' or the file
1755 object `fileobj' (using os.fstat on its file descriptor). You can
1756 modify some of the TarInfo's attributes before you add it using
1757 addfile(). If given, `arcname' specifies an alternative name for the
1758 file in the archive.
1759 """
1760 self._check("aw")
1761
1762 # When fileobj is given, replace name by
1763 # fileobj's real name.
1764 if fileobj is not None:
1765 name = fileobj.name
1766
1767 # Building the name of the member in the archive.
1768 # Backward slashes are converted to forward slashes,
1769 # Absolute paths are turned to relative paths.
1770 if arcname is None:
1771 arcname = name
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001772 drv, arcname = os.path.splitdrive(arcname)
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001773 arcname = arcname.replace(os.sep, "/")
1774 arcname = arcname.lstrip("/")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001775
1776 # Now, fill the TarInfo object with
1777 # information specific for the file.
Guido van Rossumd8faa362007-04-27 19:54:29 +00001778 tarinfo = self.tarinfo()
1779 tarinfo.tarfile = self
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001780
1781 # Use os.stat or os.lstat, depending on platform
1782 # and if symlinks shall be resolved.
1783 if fileobj is None:
1784 if hasattr(os, "lstat") and not self.dereference:
1785 statres = os.lstat(name)
1786 else:
1787 statres = os.stat(name)
1788 else:
1789 statres = os.fstat(fileobj.fileno())
1790 linkname = ""
1791
1792 stmd = statres.st_mode
1793 if stat.S_ISREG(stmd):
1794 inode = (statres.st_ino, statres.st_dev)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795 if not self.dereference and statres.st_nlink > 1 and \
1796 inode in self.inodes and arcname != self.inodes[inode]:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001797 # Is it a hardlink to an already
1798 # archived file?
1799 type = LNKTYPE
1800 linkname = self.inodes[inode]
1801 else:
1802 # The inode is added only if its valid.
1803 # For win32 it is always 0.
1804 type = REGTYPE
1805 if inode[0]:
1806 self.inodes[inode] = arcname
1807 elif stat.S_ISDIR(stmd):
1808 type = DIRTYPE
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001809 elif stat.S_ISFIFO(stmd):
1810 type = FIFOTYPE
1811 elif stat.S_ISLNK(stmd):
1812 type = SYMTYPE
1813 linkname = os.readlink(name)
1814 elif stat.S_ISCHR(stmd):
1815 type = CHRTYPE
1816 elif stat.S_ISBLK(stmd):
1817 type = BLKTYPE
1818 else:
1819 return None
1820
1821 # Fill the TarInfo object with all
1822 # information we can get.
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001823 tarinfo.name = arcname
1824 tarinfo.mode = stmd
1825 tarinfo.uid = statres.st_uid
1826 tarinfo.gid = statres.st_gid
Lars Gustäbel2470ff12010-06-03 10:11:52 +00001827 if type == REGTYPE:
Martin v. Löwis61d77e02004-08-20 06:35:46 +00001828 tarinfo.size = statres.st_size
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001829 else:
Guido van Rossume2a383d2007-01-15 16:59:06 +00001830 tarinfo.size = 0
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001831 tarinfo.mtime = statres.st_mtime
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001832 tarinfo.type = type
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001833 tarinfo.linkname = linkname
1834 if pwd:
1835 try:
1836 tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0]
1837 except KeyError:
1838 pass
1839 if grp:
1840 try:
1841 tarinfo.gname = grp.getgrgid(tarinfo.gid)[0]
1842 except KeyError:
1843 pass
1844
1845 if type in (CHRTYPE, BLKTYPE):
1846 if hasattr(os, "major") and hasattr(os, "minor"):
1847 tarinfo.devmajor = os.major(statres.st_rdev)
1848 tarinfo.devminor = os.minor(statres.st_rdev)
1849 return tarinfo
1850
1851 def list(self, verbose=True):
1852 """Print a table of contents to sys.stdout. If `verbose' is False, only
1853 the names of the members are printed. If it is True, an `ls -l'-like
1854 output is produced.
1855 """
1856 self._check()
1857
1858 for tarinfo in self:
1859 if verbose:
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001860 _safe_print(stat.filemode(tarinfo.mode))
1861 _safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
1862 tarinfo.gname or tarinfo.gid))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001863 if tarinfo.ischr() or tarinfo.isblk():
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001864 _safe_print("%10s" %
1865 ("%d,%d" % (tarinfo.devmajor, tarinfo.devminor)))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001866 else:
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001867 _safe_print("%10d" % tarinfo.size)
1868 _safe_print("%d-%02d-%02d %02d:%02d:%02d" \
1869 % time.localtime(tarinfo.mtime)[:6])
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001870
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001871 _safe_print(tarinfo.name + ("/" if tarinfo.isdir() else ""))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001872
1873 if verbose:
1874 if tarinfo.issym():
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001875 _safe_print("-> " + tarinfo.linkname)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001876 if tarinfo.islnk():
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +02001877 _safe_print("link to " + tarinfo.linkname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001878 print()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001879
Raymond Hettingera63a3122011-01-26 20:34:14 +00001880 def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001881 """Add the file `name' to the archive. `name' may be any type of file
1882 (directory, fifo, symbolic link, etc.). If given, `arcname'
1883 specifies an alternative name for the file in the archive.
1884 Directories are added recursively by default. This can be avoided by
Guido van Rossum486364b2007-06-30 05:01:58 +00001885 setting `recursive' to False. `exclude' is a function that should
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001886 return True for each filename to be excluded. `filter' is a function
1887 that expects a TarInfo object argument and returns the changed
1888 TarInfo object, if it returns None the TarInfo object will be
1889 excluded from the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001890 """
1891 self._check("aw")
1892
1893 if arcname is None:
1894 arcname = name
1895
Guido van Rossum486364b2007-06-30 05:01:58 +00001896 # Exclude pathnames.
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001897 if exclude is not None:
1898 import warnings
1899 warnings.warn("use the filter argument instead",
1900 DeprecationWarning, 2)
1901 if exclude(name):
1902 self._dbg(2, "tarfile: Excluded %r" % name)
1903 return
Guido van Rossum486364b2007-06-30 05:01:58 +00001904
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001905 # Skip if somebody tries to archive the archive...
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001906 if self.name is not None and os.path.abspath(name) == self.name:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001907 self._dbg(2, "tarfile: Skipped %r" % name)
1908 return
1909
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001910 self._dbg(1, name)
1911
1912 # Create a TarInfo object from the file.
1913 tarinfo = self.gettarinfo(name, arcname)
1914
1915 if tarinfo is None:
1916 self._dbg(1, "tarfile: Unsupported type %r" % name)
1917 return
1918
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001919 # Change or exclude the TarInfo object.
1920 if filter is not None:
1921 tarinfo = filter(tarinfo)
1922 if tarinfo is None:
1923 self._dbg(2, "tarfile: Excluded %r" % name)
1924 return
1925
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001926 # Append the tar header and data to the archive.
1927 if tarinfo.isreg():
Andrew Svetlov718df1d2012-11-29 14:20:47 +02001928 with bltn_open(name, "rb") as f:
1929 self.addfile(tarinfo, f)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001930
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001931 elif tarinfo.isdir():
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001932 self.addfile(tarinfo)
1933 if recursive:
1934 for f in os.listdir(name):
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001935 self.add(os.path.join(name, f), os.path.join(arcname, f),
Raymond Hettingera63a3122011-01-26 20:34:14 +00001936 recursive, exclude, filter=filter)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001937
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001938 else:
1939 self.addfile(tarinfo)
1940
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001941 def addfile(self, tarinfo, fileobj=None):
1942 """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
1943 given, tarinfo.size bytes are read from it and added to the archive.
1944 You can create TarInfo objects using gettarinfo().
1945 On Windows platforms, `fileobj' should always be opened with mode
1946 'rb' to avoid irritation about the file size.
1947 """
1948 self._check("aw")
1949
Thomas Wouters89f507f2006-12-13 04:49:30 +00001950 tarinfo = copy.copy(tarinfo)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001951
Guido van Rossume7ba4952007-06-06 23:52:48 +00001952 buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001953 self.fileobj.write(buf)
1954 self.offset += len(buf)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001955
1956 # If there's data to follow, append it.
1957 if fileobj is not None:
1958 copyfileobj(fileobj, self.fileobj, tarinfo.size)
1959 blocks, remainder = divmod(tarinfo.size, BLOCKSIZE)
1960 if remainder > 0:
1961 self.fileobj.write(NUL * (BLOCKSIZE - remainder))
1962 blocks += 1
1963 self.offset += blocks * BLOCKSIZE
1964
Martin v. Löwisf3c56112004-09-18 09:08:52 +00001965 self.members.append(tarinfo)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001966
Martin v. Löwis00a73e72005-03-04 19:40:34 +00001967 def extractall(self, path=".", members=None):
1968 """Extract all members from the archive to the current working
1969 directory and set owner, modification time and permissions on
1970 directories afterwards. `path' specifies a different directory
1971 to extract to. `members' is optional and must be a subset of the
1972 list returned by getmembers().
1973 """
1974 directories = []
1975
1976 if members is None:
1977 members = self
1978
1979 for tarinfo in members:
1980 if tarinfo.isdir():
Christian Heimes2202f872008-02-06 14:31:34 +00001981 # Extract directories with a safe mode.
Martin v. Löwis00a73e72005-03-04 19:40:34 +00001982 directories.append(tarinfo)
Christian Heimes2202f872008-02-06 14:31:34 +00001983 tarinfo = copy.copy(tarinfo)
1984 tarinfo.mode = 0o700
Martin v. Löwis16f344d2010-11-01 21:39:13 +00001985 # Do not set_attrs directories, as we will do that further down
1986 self.extract(tarinfo, path, set_attrs=not tarinfo.isdir())
Martin v. Löwis00a73e72005-03-04 19:40:34 +00001987
1988 # Reverse sort directories.
Raymond Hettingerd4cb56d2008-01-30 02:55:10 +00001989 directories.sort(key=lambda a: a.name)
Martin v. Löwis00a73e72005-03-04 19:40:34 +00001990 directories.reverse()
1991
1992 # Set correct owner, mtime and filemode on directories.
1993 for tarinfo in directories:
Christian Heimesfaf2f632008-01-06 16:59:19 +00001994 dirpath = os.path.join(path, tarinfo.name)
Martin v. Löwis00a73e72005-03-04 19:40:34 +00001995 try:
Christian Heimesfaf2f632008-01-06 16:59:19 +00001996 self.chown(tarinfo, dirpath)
1997 self.utime(tarinfo, dirpath)
1998 self.chmod(tarinfo, dirpath)
Guido van Rossumb940e112007-01-10 16:19:56 +00001999 except ExtractError as e:
Martin v. Löwis00a73e72005-03-04 19:40:34 +00002000 if self.errorlevel > 1:
2001 raise
2002 else:
2003 self._dbg(1, "tarfile: %s" % e)
2004
Martin v. Löwis16f344d2010-11-01 21:39:13 +00002005 def extract(self, member, path="", set_attrs=True):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002006 """Extract a member from the archive to the current working directory,
2007 using its full name. Its file information is extracted as accurately
2008 as possible. `member' may be a filename or a TarInfo object. You can
Martin v. Löwis16f344d2010-11-01 21:39:13 +00002009 specify a different directory using `path'. File attributes (owner,
2010 mtime, mode) are set unless `set_attrs' is False.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002011 """
2012 self._check("r")
2013
Guido van Rossum3172c5d2007-10-16 18:12:55 +00002014 if isinstance(member, str):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002015 tarinfo = self.getmember(member)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002016 else:
2017 tarinfo = member
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002018
Neal Norwitza4f651a2004-07-20 22:07:44 +00002019 # Prepare the link target for makelink().
2020 if tarinfo.islnk():
2021 tarinfo._link_target = os.path.join(path, tarinfo.linkname)
2022
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002023 try:
Martin v. Löwis16f344d2010-11-01 21:39:13 +00002024 self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
2025 set_attrs=set_attrs)
Andrew Svetlov3438fa42012-12-17 23:35:18 +02002026 except OSError as e:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002027 if self.errorlevel > 0:
2028 raise
2029 else:
2030 if e.filename is None:
2031 self._dbg(1, "tarfile: %s" % e.strerror)
2032 else:
2033 self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename))
Guido van Rossumb940e112007-01-10 16:19:56 +00002034 except ExtractError as e:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002035 if self.errorlevel > 1:
2036 raise
2037 else:
2038 self._dbg(1, "tarfile: %s" % e)
2039
2040 def extractfile(self, member):
2041 """Extract a member from the archive as a file object. `member' may be
Lars Gustäbel7a919e92012-05-05 18:15:03 +02002042 a filename or a TarInfo object. If `member' is a regular file or a
2043 link, an io.BufferedReader object is returned. Otherwise, None is
2044 returned.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002045 """
2046 self._check("r")
2047
Guido van Rossum3172c5d2007-10-16 18:12:55 +00002048 if isinstance(member, str):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002049 tarinfo = self.getmember(member)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002050 else:
2051 tarinfo = member
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002052
Lars Gustäbel7a919e92012-05-05 18:15:03 +02002053 if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES:
2054 # Members with unknown types are treated as regular files.
Lars Gustäbelb062a2f2012-05-14 13:18:16 +02002055 return self.fileobject(self, tarinfo)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002056
2057 elif tarinfo.islnk() or tarinfo.issym():
2058 if isinstance(self.fileobj, _Stream):
2059 # A small but ugly workaround for the case that someone tries
2060 # to extract a (sym)link as a file-object from a non-seekable
2061 # stream of tar blocks.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002062 raise StreamError("cannot extract (sym)link as file object")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002063 else:
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00002064 # A (sym)link's file object is its target's file object.
Lars Gustäbel1b512722010-06-03 12:45:16 +00002065 return self.extractfile(self._find_link_target(tarinfo))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002066 else:
2067 # If there's no data associated with the member (directory, chrdev,
2068 # blkdev, etc.), return None instead of a file object.
2069 return None
2070
Martin v. Löwis16f344d2010-11-01 21:39:13 +00002071 def _extract_member(self, tarinfo, targetpath, set_attrs=True):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002072 """Extract the TarInfo object tarinfo to a physical
2073 file called targetpath.
2074 """
2075 # Fetch the TarInfo object for the given name
2076 # and build the destination pathname, replacing
2077 # forward slashes to platform specific separators.
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00002078 targetpath = targetpath.rstrip("/")
2079 targetpath = targetpath.replace("/", os.sep)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002080
2081 # Create all upper directories.
2082 upperdirs = os.path.dirname(targetpath)
2083 if upperdirs and not os.path.exists(upperdirs):
Christian Heimes2202f872008-02-06 14:31:34 +00002084 # Create directories that are not part of the archive with
2085 # default permissions.
Thomas Woutersb2137042007-02-01 18:02:27 +00002086 os.makedirs(upperdirs)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002087
2088 if tarinfo.islnk() or tarinfo.issym():
2089 self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
2090 else:
2091 self._dbg(1, tarinfo.name)
2092
2093 if tarinfo.isreg():
2094 self.makefile(tarinfo, targetpath)
2095 elif tarinfo.isdir():
2096 self.makedir(tarinfo, targetpath)
2097 elif tarinfo.isfifo():
2098 self.makefifo(tarinfo, targetpath)
2099 elif tarinfo.ischr() or tarinfo.isblk():
2100 self.makedev(tarinfo, targetpath)
2101 elif tarinfo.islnk() or tarinfo.issym():
2102 self.makelink(tarinfo, targetpath)
2103 elif tarinfo.type not in SUPPORTED_TYPES:
2104 self.makeunknown(tarinfo, targetpath)
2105 else:
2106 self.makefile(tarinfo, targetpath)
2107
Martin v. Löwis16f344d2010-11-01 21:39:13 +00002108 if set_attrs:
2109 self.chown(tarinfo, targetpath)
2110 if not tarinfo.issym():
2111 self.chmod(tarinfo, targetpath)
2112 self.utime(tarinfo, targetpath)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002113
2114 #--------------------------------------------------------------------------
2115 # Below are the different file methods. They are called via
2116 # _extract_member() when extract() is called. They can be replaced in a
2117 # subclass to implement other functionality.
2118
2119 def makedir(self, tarinfo, targetpath):
2120 """Make a directory called targetpath.
2121 """
2122 try:
Christian Heimes2202f872008-02-06 14:31:34 +00002123 # Use a safe mode for the directory, the real mode is set
2124 # later in _extract_member().
2125 os.mkdir(targetpath, 0o700)
Florent Xicluna68f71a32011-10-28 16:06:23 +02002126 except FileExistsError:
2127 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002128
2129 def makefile(self, tarinfo, targetpath):
2130 """Make a file called targetpath.
2131 """
Lars Gustäbel9cbdd752010-10-29 09:08:19 +00002132 source = self.fileobj
2133 source.seek(tarinfo.offset_data)
Andrew Svetlov718df1d2012-11-29 14:20:47 +02002134 with bltn_open(targetpath, "wb") as target:
2135 if tarinfo.sparse is not None:
2136 for offset, size in tarinfo.sparse:
2137 target.seek(offset)
Lars Gustäbel03572682015-07-06 09:27:24 +02002138 copyfileobj(source, target, size, ReadError)
Andrew Svetlov718df1d2012-11-29 14:20:47 +02002139 else:
Lars Gustäbel03572682015-07-06 09:27:24 +02002140 copyfileobj(source, target, tarinfo.size, ReadError)
Andrew Svetlov718df1d2012-11-29 14:20:47 +02002141 target.seek(tarinfo.size)
2142 target.truncate()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002143
2144 def makeunknown(self, tarinfo, targetpath):
2145 """Make a file from a TarInfo object with an unknown type
2146 at targetpath.
2147 """
2148 self.makefile(tarinfo, targetpath)
2149 self._dbg(1, "tarfile: Unknown file type %r, " \
2150 "extracted as regular file." % tarinfo.type)
2151
2152 def makefifo(self, tarinfo, targetpath):
2153 """Make a fifo called targetpath.
2154 """
2155 if hasattr(os, "mkfifo"):
2156 os.mkfifo(targetpath)
2157 else:
Thomas Wouters477c8d52006-05-27 19:21:47 +00002158 raise ExtractError("fifo not supported by system")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002159
2160 def makedev(self, tarinfo, targetpath):
2161 """Make a character or block device called targetpath.
2162 """
2163 if not hasattr(os, "mknod") or not hasattr(os, "makedev"):
Thomas Wouters477c8d52006-05-27 19:21:47 +00002164 raise ExtractError("special devices not supported by system")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002165
2166 mode = tarinfo.mode
2167 if tarinfo.isblk():
2168 mode |= stat.S_IFBLK
2169 else:
2170 mode |= stat.S_IFCHR
2171
2172 os.mknod(targetpath, mode,
2173 os.makedev(tarinfo.devmajor, tarinfo.devminor))
2174
2175 def makelink(self, tarinfo, targetpath):
2176 """Make a (symbolic) link called targetpath. If it cannot be created
2177 (platform limitation), we try to make a copy of the referenced file
2178 instead of a link.
2179 """
Brian Curtind40e6f72010-07-08 21:39:08 +00002180 try:
Lars Gustäbel1b512722010-06-03 12:45:16 +00002181 # For systems that support symbolic and hard links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002182 if tarinfo.issym():
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00002183 os.symlink(tarinfo.linkname, targetpath)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002184 else:
Neal Norwitza4f651a2004-07-20 22:07:44 +00002185 # See extract().
Lars Gustäbel1b512722010-06-03 12:45:16 +00002186 if os.path.exists(tarinfo._link_target):
2187 os.link(tarinfo._link_target, targetpath)
2188 else:
Brian Curtin82df53e2010-09-24 21:04:05 +00002189 self._extract_member(self._find_link_target(tarinfo),
2190 targetpath)
Brian Curtin16633fa2010-07-09 13:54:27 +00002191 except symlink_exception:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002192 try:
Brian Curtin16633fa2010-07-09 13:54:27 +00002193 self._extract_member(self._find_link_target(tarinfo),
2194 targetpath)
Lars Gustäbel1b512722010-06-03 12:45:16 +00002195 except KeyError:
2196 raise ExtractError("unable to resolve link inside archive")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002197
2198 def chown(self, tarinfo, targetpath):
2199 """Set owner of targetpath according to tarinfo.
2200 """
2201 if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
2202 # We have to be root to do so.
2203 try:
2204 g = grp.getgrnam(tarinfo.gname)[2]
2205 except KeyError:
Lars Gustäbel2e7ddd32011-09-05 16:58:14 +02002206 g = tarinfo.gid
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002207 try:
2208 u = pwd.getpwnam(tarinfo.uname)[2]
2209 except KeyError:
Lars Gustäbel2e7ddd32011-09-05 16:58:14 +02002210 u = tarinfo.uid
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002211 try:
2212 if tarinfo.issym() and hasattr(os, "lchown"):
2213 os.lchown(targetpath, u, g)
2214 else:
Jesus Cea4791a242012-10-05 03:15:39 +02002215 os.chown(targetpath, u, g)
Andrew Svetlov3438fa42012-12-17 23:35:18 +02002216 except OSError as e:
Thomas Wouters477c8d52006-05-27 19:21:47 +00002217 raise ExtractError("could not change owner")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002218
2219 def chmod(self, tarinfo, targetpath):
2220 """Set file permissions of targetpath according to tarinfo.
2221 """
Jack Jansen834eff62003-03-07 12:47:06 +00002222 if hasattr(os, 'chmod'):
2223 try:
2224 os.chmod(targetpath, tarinfo.mode)
Andrew Svetlov3438fa42012-12-17 23:35:18 +02002225 except OSError as e:
Thomas Wouters477c8d52006-05-27 19:21:47 +00002226 raise ExtractError("could not change mode")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002227
2228 def utime(self, tarinfo, targetpath):
2229 """Set modification time of targetpath according to tarinfo.
2230 """
Jack Jansen834eff62003-03-07 12:47:06 +00002231 if not hasattr(os, 'utime'):
Tim Petersf9347782003-03-07 15:36:41 +00002232 return
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002233 try:
2234 os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime))
Andrew Svetlov3438fa42012-12-17 23:35:18 +02002235 except OSError as e:
Thomas Wouters477c8d52006-05-27 19:21:47 +00002236 raise ExtractError("could not change modification time")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002237
2238 #--------------------------------------------------------------------------
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002239 def next(self):
2240 """Return the next member of the archive as a TarInfo object, when
2241 TarFile is opened for reading. Return None if there is no more
2242 available.
2243 """
2244 self._check("ra")
2245 if self.firstmember is not None:
2246 m = self.firstmember
2247 self.firstmember = None
2248 return m
2249
Lars Gustäbel03572682015-07-06 09:27:24 +02002250 # Advance the file pointer.
2251 if self.offset != self.fileobj.tell():
2252 self.fileobj.seek(self.offset - 1)
2253 if not self.fileobj.read(1):
2254 raise ReadError("unexpected end of data")
2255
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002256 # Read the next block.
Lars Gustäbel9520a432009-11-22 18:48:49 +00002257 tarinfo = None
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002258 while True:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002259 try:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002260 tarinfo = self.tarinfo.fromtarfile(self)
Lars Gustäbel9520a432009-11-22 18:48:49 +00002261 except EOFHeaderError as e:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002262 if self.ignore_zeros:
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002263 self._dbg(2, "0x%X: %s" % (self.offset, e))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002264 self.offset += BLOCKSIZE
2265 continue
Lars Gustäbel9520a432009-11-22 18:48:49 +00002266 except InvalidHeaderError as e:
2267 if self.ignore_zeros:
2268 self._dbg(2, "0x%X: %s" % (self.offset, e))
2269 self.offset += BLOCKSIZE
2270 continue
2271 elif self.offset == 0:
2272 raise ReadError(str(e))
2273 except EmptyHeaderError:
2274 if self.offset == 0:
2275 raise ReadError("empty file")
2276 except TruncatedHeaderError as e:
2277 if self.offset == 0:
2278 raise ReadError(str(e))
2279 except SubsequentHeaderError as e:
2280 raise ReadError(str(e))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002281 break
2282
Lars Gustäbel9520a432009-11-22 18:48:49 +00002283 if tarinfo is not None:
2284 self.members.append(tarinfo)
2285 else:
2286 self._loaded = True
2287
Thomas Wouters477c8d52006-05-27 19:21:47 +00002288 return tarinfo
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002289
2290 #--------------------------------------------------------------------------
2291 # Little helper methods:
2292
Lars Gustäbel1b512722010-06-03 12:45:16 +00002293 def _getmember(self, name, tarinfo=None, normalize=False):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002294 """Find an archive member by name from bottom to top.
2295 If tarinfo is given, it is used as the starting point.
2296 """
Martin v. Löwisf3c56112004-09-18 09:08:52 +00002297 # Ensure that all members have been loaded.
2298 members = self.getmembers()
2299
Lars Gustäbel1b512722010-06-03 12:45:16 +00002300 # Limit the member search list up to tarinfo.
2301 if tarinfo is not None:
2302 members = members[:members.index(tarinfo)]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002303
Lars Gustäbel1b512722010-06-03 12:45:16 +00002304 if normalize:
2305 name = os.path.normpath(name)
2306
2307 for member in reversed(members):
2308 if normalize:
2309 member_name = os.path.normpath(member.name)
2310 else:
2311 member_name = member.name
2312
2313 if name == member_name:
2314 return member
Andrew M. Kuchling864bba12004-07-10 22:02:11 +00002315
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002316 def _load(self):
2317 """Read through the entire archive file and look for readable
2318 members.
2319 """
2320 while True:
2321 tarinfo = self.next()
2322 if tarinfo is None:
2323 break
2324 self._loaded = True
2325
2326 def _check(self, mode=None):
2327 """Check if TarFile is still open, and if the operation's mode
2328 corresponds to TarFile's mode.
2329 """
2330 if self.closed:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002331 raise OSError("%s is closed" % self.__class__.__name__)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002332 if mode is not None and self.mode not in mode:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002333 raise OSError("bad operation for mode %r" % self.mode)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002334
Lars Gustäbel1b512722010-06-03 12:45:16 +00002335 def _find_link_target(self, tarinfo):
2336 """Find the target member of a symlink or hardlink member in the
2337 archive.
2338 """
2339 if tarinfo.issym():
2340 # Always search the entire archive.
Lars Gustäbel1ef9eda2012-04-24 21:04:40 +02002341 linkname = "/".join(filter(None, (os.path.dirname(tarinfo.name), tarinfo.linkname)))
Lars Gustäbel1b512722010-06-03 12:45:16 +00002342 limit = None
2343 else:
2344 # Search the archive before the link, because a hard link is
2345 # just a reference to an already archived file.
2346 linkname = tarinfo.linkname
2347 limit = tarinfo
2348
2349 member = self._getmember(linkname, tarinfo=limit, normalize=True)
2350 if member is None:
2351 raise KeyError("linkname %r not found" % linkname)
2352 return member
2353
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002354 def __iter__(self):
2355 """Provide an iterator object.
2356 """
2357 if self._loaded:
2358 return iter(self.members)
2359 else:
2360 return TarIter(self)
2361
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002362 def _dbg(self, level, msg):
2363 """Write debugging output to sys.stderr.
2364 """
2365 if level <= self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002366 print(msg, file=sys.stderr)
Lars Gustäbel01385812010-03-03 12:08:54 +00002367
2368 def __enter__(self):
2369 self._check()
2370 return self
2371
2372 def __exit__(self, type, value, traceback):
2373 if type is None:
2374 self.close()
2375 else:
2376 # An exception occurred. We must not call close() because
2377 # it would try to write end-of-archive blocks and padding.
2378 if not self._extfileobj:
2379 self.fileobj.close()
2380 self.closed = True
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002381# class TarFile
2382
2383class TarIter:
2384 """Iterator Class.
2385
2386 for tarinfo in TarFile(...):
2387 suite...
2388 """
2389
2390 def __init__(self, tarfile):
2391 """Construct a TarIter object.
2392 """
2393 self.tarfile = tarfile
Martin v. Löwis637431b2005-03-03 23:12:42 +00002394 self.index = 0
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002395 def __iter__(self):
2396 """Return iterator object.
2397 """
2398 return self
Georg Brandla18af4e2007-04-21 15:47:16 +00002399 def __next__(self):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002400 """Return the next item using TarFile's next() method.
2401 When all members have been read, set TarFile as _loaded.
2402 """
Martin v. Löwis637431b2005-03-03 23:12:42 +00002403 # Fix for SF #1100429: Under rare circumstances it can
2404 # happen that getmembers() is called during iteration,
2405 # which will cause TarIter to stop prematurely.
Serhiy Storchaka263fab92013-05-09 14:22:26 +03002406
2407 if self.index == 0 and self.tarfile.firstmember is not None:
2408 tarinfo = self.tarfile.next()
2409 elif self.index < len(self.tarfile.members):
2410 tarinfo = self.tarfile.members[self.index]
2411 elif not self.tarfile._loaded:
Martin v. Löwis637431b2005-03-03 23:12:42 +00002412 tarinfo = self.tarfile.next()
2413 if not tarinfo:
2414 self.tarfile._loaded = True
2415 raise StopIteration
2416 else:
Serhiy Storchaka263fab92013-05-09 14:22:26 +03002417 raise StopIteration
Martin v. Löwis637431b2005-03-03 23:12:42 +00002418 self.index += 1
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002419 return tarinfo
2420
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002421#--------------------
2422# exported functions
2423#--------------------
2424def is_tarfile(name):
2425 """Return True if name points to a tar archive that we
2426 are able to handle, else return False.
2427 """
2428 try:
2429 t = open(name)
2430 t.close()
2431 return True
2432 except TarError:
2433 return False
2434
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002435open = TarFile.open
Serhiy Storchakad27b4552013-11-24 01:53:29 +02002436
2437
2438def main():
2439 import argparse
2440
2441 description = 'A simple command line interface for tarfile module.'
2442 parser = argparse.ArgumentParser(description=description)
2443 parser.add_argument('-v', '--verbose', action='store_true', default=False,
2444 help='Verbose output')
2445 group = parser.add_mutually_exclusive_group()
2446 group.add_argument('-l', '--list', metavar='<tarfile>',
2447 help='Show listing of a tarfile')
2448 group.add_argument('-e', '--extract', nargs='+',
2449 metavar=('<tarfile>', '<output_dir>'),
2450 help='Extract tarfile into target dir')
2451 group.add_argument('-c', '--create', nargs='+',
2452 metavar=('<name>', '<file>'),
2453 help='Create tarfile from sources')
2454 group.add_argument('-t', '--test', metavar='<tarfile>',
2455 help='Test if a tarfile is valid')
2456 args = parser.parse_args()
2457
2458 if args.test:
2459 src = args.test
2460 if is_tarfile(src):
2461 with open(src, 'r') as tar:
2462 tar.getmembers()
2463 print(tar.getmembers(), file=sys.stderr)
2464 if args.verbose:
2465 print('{!r} is a tar archive.'.format(src))
2466 else:
2467 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
2468
2469 elif args.list:
2470 src = args.list
2471 if is_tarfile(src):
2472 with TarFile.open(src, 'r:*') as tf:
2473 tf.list(verbose=args.verbose)
2474 else:
2475 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
2476
2477 elif args.extract:
2478 if len(args.extract) == 1:
2479 src = args.extract[0]
2480 curdir = os.curdir
2481 elif len(args.extract) == 2:
2482 src, curdir = args.extract
2483 else:
2484 parser.exit(1, parser.format_help())
2485
2486 if is_tarfile(src):
2487 with TarFile.open(src, 'r:*') as tf:
2488 tf.extractall(path=curdir)
2489 if args.verbose:
2490 if curdir == '.':
2491 msg = '{!r} file is extracted.'.format(src)
2492 else:
2493 msg = ('{!r} file is extracted '
2494 'into {!r} directory.').format(src, curdir)
2495 print(msg)
2496 else:
2497 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
2498
2499 elif args.create:
2500 tar_name = args.create.pop(0)
2501 _, ext = os.path.splitext(tar_name)
2502 compressions = {
2503 # gz
Serhiy Storchaka832dd5f2015-02-10 08:45:53 +02002504 '.gz': 'gz',
2505 '.tgz': 'gz',
Serhiy Storchakad27b4552013-11-24 01:53:29 +02002506 # xz
Serhiy Storchaka832dd5f2015-02-10 08:45:53 +02002507 '.xz': 'xz',
2508 '.txz': 'xz',
Serhiy Storchakad27b4552013-11-24 01:53:29 +02002509 # bz2
Serhiy Storchaka832dd5f2015-02-10 08:45:53 +02002510 '.bz2': 'bz2',
2511 '.tbz': 'bz2',
2512 '.tbz2': 'bz2',
2513 '.tb2': 'bz2',
Serhiy Storchakad27b4552013-11-24 01:53:29 +02002514 }
2515 tar_mode = 'w:' + compressions[ext] if ext in compressions else 'w'
2516 tar_files = args.create
2517
2518 with TarFile.open(tar_name, tar_mode) as tf:
2519 for file_name in tar_files:
2520 tf.add(file_name)
2521
2522 if args.verbose:
2523 print('{!r} file created.'.format(tar_name))
2524
2525 else:
2526 parser.exit(1, parser.format_help())
2527
2528if __name__ == '__main__':
2529 main()