Back to index

python3.2  3.2.2
Classes | Functions | Variables
tarfile Namespace Reference

Classes

class  TarError
class  ExtractError
class  ReadError
class  CompressionError
class  StreamError
class  HeaderError
class  EmptyHeaderError
class  TruncatedHeaderError
class  EOFHeaderError
class  InvalidHeaderError
class  SubsequentHeaderError
class  _LowLevelFile
class  _Stream
class  _StreamProxy
class  _BZ2Proxy
class  _FileInFile
class  ExFileObject
class  TarInfo
class  TarFile
class  TarIter

Functions

def stn
def nts
def nti
def itn
def calc_chksums
def copyfileobj
def filemode
def is_tarfile

Variables

string __version__ = "$Revision$"
string version = "0.9.0"
string __author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"
string __date__ = "$Date: 2011-02-25 17:42:01 +0200 (Fri, 25 Feb 2011) $"
string __cvsid__ = "$Id: tarfile.py 88586 2011-02-25 15:42:01Z marc-andre.lemburg $"
string __credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."
 grp = pwdNone
tuple symlink_exception = (AttributeError, NotImplementedError)
list __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]
string NUL = "\0"
int BLOCKSIZE = 512
int RECORDSIZE = 20
string GNU_MAGIC = "ustar \0"
string POSIX_MAGIC = "ustar\x0000"
int LENGTH_NAME = 100
int LENGTH_LINK = 100
int LENGTH_PREFIX = 155
string REGTYPE = "0"
string AREGTYPE = "\0"
string LNKTYPE = "1"
string SYMTYPE = "2"
string CHRTYPE = "3"
string BLKTYPE = "4"
string DIRTYPE = "5"
string FIFOTYPE = "6"
string CONTTYPE = "7"
string GNUTYPE_LONGNAME = "L"
string GNUTYPE_LONGLINK = "K"
string GNUTYPE_SPARSE = "S"
string XHDTYPE = "x"
string XGLTYPE = "g"
string SOLARIS_XHDTYPE = "X"
int USTAR_FORMAT = 0
int GNU_FORMAT = 1
int PAX_FORMAT = 2
 DEFAULT_FORMAT = GNU_FORMAT
tuple SUPPORTED_TYPES
tuple REGULAR_TYPES
tuple GNU_TYPES
tuple PAX_FIELDS
dictionary PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}
dictionary PAX_NUMBER_FIELDS
int S_IFLNK = 0
int S_IFREG = 0
int S_IFBLK = 0
int S_IFDIR = 0
int S_IFCHR = 0
int S_IFIFO = 0
int TSUID = 0
int TSGID = 0
int TSVTX = 0
int TUREAD = 0
int TUWRITE = 0
int TUEXEC = 0
int TGREAD = 0
int TGWRITE = 0
int TGEXEC = 0
int TOREAD = 0
int TOWRITE = 0
int TOEXEC = 0
string ENCODING = "utf-8"
tuple filemode_table
 bltn_open = open
 open = TarFile.open

Function Documentation

def tarfile.calc_chksums (   buf)
Calculate the checksum for a member's header by summing up all
   characters except for the chksum field which is treated as if
   it was filled with spaces. According to the GNU tar sources,
   some tars (Sun and NeXT) calculate chksum with signed char,
   which will be different if there are chars in the buffer with
   the high bit set. So we calculate two checksums, unsigned and
   signed.

Definition at line 238 of file tarfile.py.

00238 
00239 def calc_chksums(buf):
00240     """Calculate the checksum for a member's header by summing up all
00241        characters except for the chksum field which is treated as if
00242        it was filled with spaces. According to the GNU tar sources,
00243        some tars (Sun and NeXT) calculate chksum with signed char,
00244        which will be different if there are chars in the buffer with
00245        the high bit set. So we calculate two checksums, unsigned and
00246        signed.
00247     """
00248     unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512]))
00249     signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512]))
00250     return unsigned_chksum, signed_chksum

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.copyfileobj (   src,
  dst,
  length = None 
)
Copy length bytes from fileobj src to fileobj dst.
   If length is None, copy the entire content.

Definition at line 251 of file tarfile.py.

00251 
00252 def copyfileobj(src, dst, length=None):
00253     """Copy length bytes from fileobj src to fileobj dst.
00254        If length is None, copy the entire content.
00255     """
00256     if length == 0:
00257         return
00258     if length is None:
00259         shutil.copyfileobj(src, dst)
00260         return
00261 
00262     BUFSIZE = 16 * 1024
00263     blocks, remainder = divmod(length, BUFSIZE)
00264     for b in range(blocks):
00265         buf = src.read(BUFSIZE)
00266         if len(buf) < BUFSIZE:
00267             raise IOError("end of file reached")
00268         dst.write(buf)
00269 
00270     if remainder != 0:
00271         buf = src.read(remainder)
00272         if len(buf) < remainder:
00273             raise IOError("end of file reached")
00274         dst.write(buf)
00275     return

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.filemode (   mode)
Convert a file's mode to a string of the form
   -rwxrwxrwx.
   Used by TarFile.list()

Definition at line 303 of file tarfile.py.

00303 
00304 def filemode(mode):
00305     """Convert a file's mode to a string of the form
00306        -rwxrwxrwx.
00307        Used by TarFile.list()
00308     """
00309     perm = []
00310     for table in filemode_table:
00311         for bit, char in table:
00312             if mode & bit == bit:
00313                 perm.append(char)
00314                 break
00315         else:
00316             perm.append("-")
00317     return "".join(perm)

Here is the caller graph for this function:

def tarfile.is_tarfile (   name)
Return True if name points to a tar archive that we
   are able to handle, else return False.

Definition at line 2589 of file tarfile.py.

02589 
02590 def is_tarfile(name):
02591     """Return True if name points to a tar archive that we
02592        are able to handle, else return False.
02593     """
02594     try:
02595         t = open(name)
02596         t.close()
02597         return True
02598     except TarError:
02599         return False

def tarfile.itn (   n,
  digits = 8,
  format = DEFAULT_FORMAT 
)
Convert a python number to a number field.

Definition at line 211 of file tarfile.py.

00211 
00212 def itn(n, digits=8, format=DEFAULT_FORMAT):
00213     """Convert a python number to a number field.
00214     """
00215     # POSIX 1003.1-1988 requires numbers to be encoded as a string of
00216     # octal digits followed by a null-byte, this allows values up to
00217     # (8**(digits-1))-1. GNU tar allows storing numbers greater than
00218     # that if necessary. A leading 0o200 byte indicates this particular
00219     # encoding, the following digits-1 bytes are a big-endian
00220     # representation. This allows values up to (256**(digits-1))-1.
00221     if 0 <= n < 8 ** (digits - 1):
00222         s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
00223     else:
00224         if format != GNU_FORMAT or n >= 256 ** (digits - 1):
00225             raise ValueError("overflow in number field")
00226 
00227         if n < 0:
00228             # XXX We mimic GNU tar's behaviour with negative numbers,
00229             # this could raise OverflowError.
00230             n = struct.unpack("L", struct.pack("l", n))[0]
00231 
00232         s = bytearray()
00233         for i in range(digits - 1):
00234             s.insert(0, n & 0o377)
00235             n >>= 8
00236         s.insert(0, 0o200)
00237     return s

Here is the caller graph for this function:

def tarfile.nti (   s)
Convert a number field to a python number.

Definition at line 194 of file tarfile.py.

00194 
00195 def nti(s):
00196     """Convert a number field to a python number.
00197     """
00198     # There are two possible encodings for a number field, see
00199     # itn() below.
00200     if s[0] != chr(0o200):
00201         try:
00202             n = int(nts(s, "ascii", "strict") or "0", 8)
00203         except ValueError:
00204             raise InvalidHeaderError("invalid header")
00205     else:
00206         n = 0
00207         for i in range(len(s) - 1):
00208             n <<= 8
00209             n += ord(s[i + 1])
00210     return n

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.nts (   s,
  encoding,
  errors 
)
Convert a null-terminated bytes object to a string.

Definition at line 186 of file tarfile.py.

00186 
00187 def nts(s, encoding, errors):
00188     """Convert a null-terminated bytes object to a string.
00189     """
00190     p = s.find(b"\0")
00191     if p != -1:
00192         s = s[:p]
00193     return s.decode(encoding, errors)

Here is the caller graph for this function:

def tarfile.stn (   s,
  length,
  encoding,
  errors 
)
Convert a string to a null-terminated bytes object.

Definition at line 180 of file tarfile.py.

00180 
00181 def stn(s, length, encoding, errors):
00182     """Convert a string to a null-terminated bytes object.
00183     """
00184     s = s.encode(encoding, errors)
00185     return s[:length] + (length - len(s)) * NUL

Here is the caller graph for this function:


Variable Documentation

list tarfile.__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"]

Definition at line 68 of file tarfile.py.

string tarfile.__author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"

Definition at line 35 of file tarfile.py.

string tarfile.__credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."

Definition at line 38 of file tarfile.py.

string tarfile.__cvsid__ = "$Id: tarfile.py 88586 2011-02-25 15:42:01Z marc-andre.lemburg $"

Definition at line 37 of file tarfile.py.

string tarfile.__date__ = "$Date: 2011-02-25 17:42:01 +0200 (Fri, 25 Feb 2011) $"

Definition at line 36 of file tarfile.py.

string tarfile.__version__ = "$Revision$"

Definition at line 32 of file tarfile.py.

Definition at line 86 of file tarfile.py.

Definition at line 90 of file tarfile.py.

Definition at line 76 of file tarfile.py.

Definition at line 2600 of file tarfile.py.

Definition at line 89 of file tarfile.py.

Definition at line 93 of file tarfile.py.

Definition at line 106 of file tarfile.py.

Definition at line 91 of file tarfile.py.

tuple tarfile.ENCODING = "utf-8"

Definition at line 172 of file tarfile.py.

Definition at line 92 of file tarfile.py.

Initial value:
00001 (
00002     ((S_IFLNK,      "l"),
00003      (S_IFREG,      "-"),
00004      (S_IFBLK,      "b"),
00005      (S_IFDIR,      "d"),
00006      (S_IFCHR,      "c"),
00007      (S_IFIFO,      "p")),
00008 
00009     ((TUREAD,       "r"),),
00010     ((TUWRITE,      "w"),),
00011     ((TUEXEC|TSUID, "s"),
00012      (TSUID,        "S"),
00013      (TUEXEC,       "x")),
00014 
00015     ((TGREAD,       "r"),),
00016     ((TGWRITE,      "w"),),
00017     ((TGEXEC|TSGID, "s"),
00018      (TSGID,        "S"),
00019      (TGEXEC,       "x")),
00020 
00021     ((TOREAD,       "r"),),
00022     ((TOWRITE,      "w"),),
00023     ((TOEXEC|TSVTX, "t"),
00024      (TSVTX,        "T"),
00025      (TOEXEC,       "x"))
00026 )

Definition at line 276 of file tarfile.py.

Definition at line 104 of file tarfile.py.

string tarfile.GNU_MAGIC = "ustar \0"

Definition at line 78 of file tarfile.py.

Initial value:
00001 (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
00002              GNUTYPE_SPARSE)

Definition at line 123 of file tarfile.py.

Definition at line 96 of file tarfile.py.

Definition at line 95 of file tarfile.py.

Definition at line 97 of file tarfile.py.

tarfile.grp = pwdNone

Definition at line 56 of file tarfile.py.

Definition at line 82 of file tarfile.py.

Definition at line 81 of file tarfile.py.

Definition at line 83 of file tarfile.py.

Definition at line 87 of file tarfile.py.

Definition at line 75 of file tarfile.py.

Definition at line 2601 of file tarfile.py.

Initial value:
00001 ("path", "linkpath", "size", "mtime",
00002               "uid", "gid", "uname", "gname")

Definition at line 127 of file tarfile.py.

Definition at line 105 of file tarfile.py.

dictionary tarfile.PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}

Definition at line 131 of file tarfile.py.

Initial value:
00001 {
00002     "atime": float,
00003     "ctime": float,
00004     "mtime": float,
00005     "uid": int,
00006     "gid": int,
00007     "size": int
00008 }

Definition at line 135 of file tarfile.py.

string tarfile.POSIX_MAGIC = "ustar\x0000"

Definition at line 79 of file tarfile.py.

Definition at line 77 of file tarfile.py.

Definition at line 85 of file tarfile.py.

Initial value:
00001 (REGTYPE, AREGTYPE,
00002                  CONTTYPE, GNUTYPE_SPARSE)

Definition at line 119 of file tarfile.py.

Definition at line 149 of file tarfile.py.

Definition at line 151 of file tarfile.py.

Definition at line 150 of file tarfile.py.

Definition at line 152 of file tarfile.py.

Definition at line 147 of file tarfile.py.

Definition at line 148 of file tarfile.py.

Definition at line 101 of file tarfile.py.

Initial value:
00001 (REGTYPE, AREGTYPE, LNKTYPE,
00002                    SYMTYPE, DIRTYPE, FIFOTYPE,
00003                    CONTTYPE, CHRTYPE, BLKTYPE,
00004                    GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
00005                    GNUTYPE_SPARSE)

Definition at line 112 of file tarfile.py.

tuple tarfile.symlink_exception = (AttributeError, NotImplementedError)

Definition at line 59 of file tarfile.py.

Definition at line 88 of file tarfile.py.

Definition at line 163 of file tarfile.py.

Definition at line 161 of file tarfile.py.

Definition at line 162 of file tarfile.py.

Definition at line 166 of file tarfile.py.

Definition at line 164 of file tarfile.py.

Definition at line 165 of file tarfile.py.

Definition at line 155 of file tarfile.py.

Definition at line 154 of file tarfile.py.

Definition at line 156 of file tarfile.py.

Definition at line 160 of file tarfile.py.

Definition at line 158 of file tarfile.py.

Definition at line 159 of file tarfile.py.

Definition at line 103 of file tarfile.py.

Definition at line 34 of file tarfile.py.

Definition at line 100 of file tarfile.py.

Definition at line 99 of file tarfile.py.