Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Properties | Private Member Functions | Static Private Member Functions | Static Private Attributes
tarfile.TarInfo Class Reference
Inheritance diagram for tarfile.TarInfo:
Inheritance graph
[legend]
Collaboration diagram for tarfile.TarInfo:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __repr__
def get_info
def tobuf
def create_ustar_header
def create_gnu_header
def create_pax_header
def create_pax_global_header
def frombuf
def fromtarfile
def isreg
def isfile
def isdir
def issym
def islnk
def ischr
def isblk
def isfifo
def issparse
def isdev

Public Attributes

 name
 mode
 uid
 gid
 size
 mtime
 chksum
 type
 linkname
 uname
 gname
 devmajor
 devminor
 offset
 offset_data
 sparse
 pax_headers
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Properties

 path = property(_getpath, _setpath)
 linkpath = property(_getlinkpath, _setlinkpath)

Private Member Functions

def _getpath
def _setpath
def _getlinkpath
def _setlinkpath
def _posix_split_name
def _create_gnu_long_header
def _create_pax_generic_header
def _proc_member
def _proc_builtin
def _proc_gnulong
def _proc_sparse
def _proc_pax
def _proc_gnusparse_00
def _proc_gnusparse_01
def _proc_gnusparse_10
def _apply_pax_info
def _decode_pax_field
def _block

Static Private Member Functions

def _create_header
def _create_payload

Static Private Attributes

tuple __slots__

Detailed Description

Informational class which holds the details about an
   archive member given by a tar header block.
   TarInfo objects are returned by TarFile.getmember(),
   TarFile.getmembers() and TarFile.gettarinfo() and are
   usually created internally.

Definition at line 914 of file tarfile.py.


Constructor & Destructor Documentation

def tarfile.TarInfo.__init__ (   self,
  name = "" 
)
Construct a TarInfo object. name is the optional name
   of the member.

Definition at line 928 of file tarfile.py.

00928 
00929     def __init__(self, name=""):
00930         """Construct a TarInfo object. name is the optional name
00931            of the member.
00932         """
00933         self.name = name        # member name
00934         self.mode = 0o644       # file permissions
00935         self.uid = 0            # user id
00936         self.gid = 0            # group id
00937         self.size = 0           # file size
00938         self.mtime = 0          # modification time
00939         self.chksum = 0         # header checksum
00940         self.type = REGTYPE     # member type
00941         self.linkname = ""      # link name
00942         self.uname = ""         # user name
00943         self.gname = ""         # group name
00944         self.devmajor = 0       # device major number
00945         self.devminor = 0       # device minor number
00946 
00947         self.offset = 0         # the tar header starts here
00948         self.offset_data = 0    # the file's data starts here
00949 
00950         self.sparse = None      # sparse member information
00951         self.pax_headers = {}   # pax header information

Here is the caller graph for this function:


Member Function Documentation

def tarfile.TarInfo.__repr__ (   self)

Definition at line 966 of file tarfile.py.

00966 
00967     def __repr__(self):
00968         return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self))

def tarfile.TarInfo._apply_pax_info (   self,
  pax_headers,
  encoding,
  errors 
) [private]
Replace fields with supplemental information from a previous
   pax extended or global header.

Definition at line 1509 of file tarfile.py.

01509 
01510     def _apply_pax_info(self, pax_headers, encoding, errors):
01511         """Replace fields with supplemental information from a previous
01512            pax extended or global header.
01513         """
01514         for keyword, value in pax_headers.items():
01515             if keyword == "GNU.sparse.name":
01516                 setattr(self, "path", value)
01517             elif keyword == "GNU.sparse.size":
01518                 setattr(self, "size", int(value))
01519             elif keyword == "GNU.sparse.realsize":
01520                 setattr(self, "size", int(value))
01521             elif keyword in PAX_FIELDS:
01522                 if keyword in PAX_NUMBER_FIELDS:
01523                     try:
01524                         value = PAX_NUMBER_FIELDS[keyword](value)
01525                     except ValueError:
01526                         value = 0
01527                 if keyword == "path":
01528                     value = value.rstrip("/")
01529                 setattr(self, keyword, value)
01530 
01531         self.pax_headers = pax_headers.copy()

Here is the caller graph for this function:

def tarfile.TarInfo._block (   self,
  count 
) [private]
Round up a byte count by BLOCKSIZE and return it,
   e.g. _block(834) => 1024.

Definition at line 1540 of file tarfile.py.

01540 
01541     def _block(self, count):
01542         """Round up a byte count by BLOCKSIZE and return it,
01543            e.g. _block(834) => 1024.
01544         """
01545         blocks, remainder = divmod(count, BLOCKSIZE)
01546         if remainder:
01547             blocks += 1
01548         return blocks * BLOCKSIZE

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._create_gnu_long_header (   cls,
  name,
  type,
  encoding,
  errors 
) [private]
Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
   for name.

Definition at line 1143 of file tarfile.py.

01143 
01144     def _create_gnu_long_header(cls, name, type, encoding, errors):
01145         """Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
01146            for name.
01147         """
01148         name = name.encode(encoding, errors) + NUL
01149 
01150         info = {}
01151         info["name"] = "././@LongLink"
01152         info["type"] = type
01153         info["size"] = len(name)
01154         info["magic"] = GNU_MAGIC
01155 
01156         # create extended header + name blocks.
01157         return cls._create_header(info, USTAR_FORMAT, encoding, errors) + \
01158                 cls._create_payload(name)

Here is the caller graph for this function:

def tarfile.TarInfo._create_header (   info,
  format,
  encoding,
  errors 
) [static, private]
Return a header block. info is a dictionary with file
   information, format must be one of the *_FORMAT constants.

Definition at line 1105 of file tarfile.py.

01105 
01106     def _create_header(info, format, encoding, errors):
01107         """Return a header block. info is a dictionary with file
01108            information, format must be one of the *_FORMAT constants.
01109         """
01110         parts = [
01111             stn(info.get("name", ""), 100, encoding, errors),
01112             itn(info.get("mode", 0) & 0o7777, 8, format),
01113             itn(info.get("uid", 0), 8, format),
01114             itn(info.get("gid", 0), 8, format),
01115             itn(info.get("size", 0), 12, format),
01116             itn(info.get("mtime", 0), 12, format),
01117             b"        ", # checksum field
01118             info.get("type", REGTYPE),
01119             stn(info.get("linkname", ""), 100, encoding, errors),
01120             info.get("magic", POSIX_MAGIC),
01121             stn(info.get("uname", ""), 32, encoding, errors),
01122             stn(info.get("gname", ""), 32, encoding, errors),
01123             itn(info.get("devmajor", 0), 8, format),
01124             itn(info.get("devminor", 0), 8, format),
01125             stn(info.get("prefix", ""), 155, encoding, errors)
01126         ]
01127 
01128         buf = struct.pack("%ds" % BLOCKSIZE, b"".join(parts))
01129         chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
01130         buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:]
01131         return buf

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._create_pax_generic_header (   cls,
  pax_headers,
  type,
  encoding 
) [private]
Return a POSIX.1-2008 extended or global header sequence
   that contains a list of keyword, value pairs. The values
   must be strings.

Definition at line 1160 of file tarfile.py.

01160 
01161     def _create_pax_generic_header(cls, pax_headers, type, encoding):
01162         """Return a POSIX.1-2008 extended or global header sequence
01163            that contains a list of keyword, value pairs. The values
01164            must be strings.
01165         """
01166         # Check if one of the fields contains surrogate characters and thereby
01167         # forces hdrcharset=BINARY, see _proc_pax() for more information.
01168         binary = False
01169         for keyword, value in pax_headers.items():
01170             try:
01171                 value.encode("utf8", "strict")
01172             except UnicodeEncodeError:
01173                 binary = True
01174                 break
01175 
01176         records = b""
01177         if binary:
01178             # Put the hdrcharset field at the beginning of the header.
01179             records += b"21 hdrcharset=BINARY\n"
01180 
01181         for keyword, value in pax_headers.items():
01182             keyword = keyword.encode("utf8")
01183             if binary:
01184                 # Try to restore the original byte representation of `value'.
01185                 # Needless to say, that the encoding must match the string.
01186                 value = value.encode(encoding, "surrogateescape")
01187             else:
01188                 value = value.encode("utf8")
01189 
01190             l = len(keyword) + len(value) + 3   # ' ' + '=' + '\n'
01191             n = p = 0
01192             while True:
01193                 n = l + len(str(p))
01194                 if n == p:
01195                     break
01196                 p = n
01197             records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n"
01198 
01199         # We use a hardcoded "././@PaxHeader" name like star does
01200         # instead of the one that POSIX recommends.
01201         info = {}
01202         info["name"] = "././@PaxHeader"
01203         info["type"] = type
01204         info["size"] = len(records)
01205         info["magic"] = POSIX_MAGIC
01206 
01207         # Create pax header + record blocks.
01208         return cls._create_header(info, USTAR_FORMAT, "ascii", "replace") + \
01209                 cls._create_payload(records)

Here is the caller graph for this function:

def tarfile.TarInfo._create_payload (   payload) [static, private]
Return the string payload filled with zero bytes
   up to the next 512 byte border.

Definition at line 1133 of file tarfile.py.

01133 
01134     def _create_payload(payload):
01135         """Return the string payload filled with zero bytes
01136            up to the next 512 byte border.
01137         """
01138         blocks, remainder = divmod(len(payload), BLOCKSIZE)
01139         if remainder > 0:
01140             payload += (BLOCKSIZE - remainder) * NUL
01141         return payload

Here is the call graph for this function:

def tarfile.TarInfo._decode_pax_field (   self,
  value,
  encoding,
  fallback_encoding,
  fallback_errors 
) [private]
Decode a single field from a pax record.

Definition at line 1532 of file tarfile.py.

01532 
01533     def _decode_pax_field(self, value, encoding, fallback_encoding, fallback_errors):
01534         """Decode a single field from a pax record.
01535         """
01536         try:
01537             return value.decode(encoding, "strict")
01538         except UnicodeDecodeError:
01539             return value.decode(fallback_encoding, fallback_errors)

Here is the caller graph for this function:

def tarfile.TarInfo._getlinkpath (   self) [private]

Definition at line 960 of file tarfile.py.

00960 
00961     def _getlinkpath(self):
        return self.linkname
def tarfile.TarInfo._getpath (   self) [private]

Definition at line 954 of file tarfile.py.

00954 
00955     def _getpath(self):
        return self.name
def tarfile.TarInfo._posix_split_name (   self,
  name 
) [private]
Split a name longer than 100 chars into a prefix
   and a name part.

Definition at line 1089 of file tarfile.py.

01089 
01090     def _posix_split_name(self, name):
01091         """Split a name longer than 100 chars into a prefix
01092            and a name part.
01093         """
01094         prefix = name[:LENGTH_PREFIX + 1]
01095         while prefix and prefix[-1] != "/":
01096             prefix = prefix[:-1]
01097 
01098         name = name[len(prefix):]
01099         prefix = prefix[:-1]
01100 
01101         if not prefix or len(name) > LENGTH_NAME:
01102             raise ValueError("name is too long")
01103         return prefix, name

Here is the caller graph for this function:

def tarfile.TarInfo._proc_builtin (   self,
  tarfile 
) [private]
Process a builtin type or an unknown type which
   will be treated as a regular file.

Definition at line 1307 of file tarfile.py.

01307 
01308     def _proc_builtin(self, tarfile):
01309         """Process a builtin type or an unknown type which
01310            will be treated as a regular file.
01311         """
01312         self.offset_data = tarfile.fileobj.tell()
01313         offset = self.offset_data
01314         if self.isreg() or self.type not in SUPPORTED_TYPES:
01315             # Skip the following data blocks.
01316             offset += self._block(self.size)
01317         tarfile.offset = offset
01318 
01319         # Patch the TarInfo object with saved global
01320         # header information.
01321         self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
01322 
01323         return self

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._proc_gnulong (   self,
  tarfile 
) [private]
Process the blocks that hold a GNU longname
   or longlink member.

Definition at line 1324 of file tarfile.py.

01324 
01325     def _proc_gnulong(self, tarfile):
01326         """Process the blocks that hold a GNU longname
01327            or longlink member.
01328         """
01329         buf = tarfile.fileobj.read(self._block(self.size))
01330 
01331         # Fetch the next header and process it.
01332         try:
01333             next = self.fromtarfile(tarfile)
01334         except HeaderError:
01335             raise SubsequentHeaderError("missing or bad subsequent header")
01336 
01337         # Patch the TarInfo object from the next header with
01338         # the longname information.
01339         next.offset = self.offset
01340         if self.type == GNUTYPE_LONGNAME:
01341             next.name = nts(buf, tarfile.encoding, tarfile.errors)
01342         elif self.type == GNUTYPE_LONGLINK:
01343             next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
01344 
01345         return next

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._proc_gnusparse_00 (   self,
  next,
  pax_headers,
  buf 
) [private]
Process a GNU tar extended sparse header, version 0.0.

Definition at line 1476 of file tarfile.py.

01476 
01477     def _proc_gnusparse_00(self, next, pax_headers, buf):
01478         """Process a GNU tar extended sparse header, version 0.0.
01479         """
01480         offsets = []
01481         for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf):
01482             offsets.append(int(match.group(1)))
01483         numbytes = []
01484         for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf):
01485             numbytes.append(int(match.group(1)))
01486         next.sparse = list(zip(offsets, numbytes))

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._proc_gnusparse_01 (   self,
  next,
  pax_headers 
) [private]
Process a GNU tar extended sparse header, version 0.1.

Definition at line 1487 of file tarfile.py.

01487 
01488     def _proc_gnusparse_01(self, next, pax_headers):
01489         """Process a GNU tar extended sparse header, version 0.1.
01490         """
01491         sparse = [int(x) for x in pax_headers["GNU.sparse.map"].split(",")]
01492         next.sparse = list(zip(sparse[::2], sparse[1::2]))

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._proc_gnusparse_10 (   self,
  next,
  pax_headers,
  tarfile 
) [private]
Process a GNU tar extended sparse header, version 1.0.

Definition at line 1493 of file tarfile.py.

01493 
01494     def _proc_gnusparse_10(self, next, pax_headers, tarfile):
01495         """Process a GNU tar extended sparse header, version 1.0.
01496         """
01497         fields = None
01498         sparse = []
01499         buf = tarfile.fileobj.read(BLOCKSIZE)
01500         fields, buf = buf.split(b"\n", 1)
01501         fields = int(fields)
01502         while len(sparse) < fields * 2:
01503             if b"\n" not in buf:
01504                 buf += tarfile.fileobj.read(BLOCKSIZE)
01505             number, buf = buf.split(b"\n", 1)
01506             sparse.append(int(number))
01507         next.offset_data = tarfile.fileobj.tell()
01508         next.sparse = list(zip(sparse[::2], sparse[1::2]))

Here is the caller graph for this function:

def tarfile.TarInfo._proc_member (   self,
  tarfile 
) [private]
Choose the right processing method depending on
   the type and call it.

Definition at line 1294 of file tarfile.py.

01294 
01295     def _proc_member(self, tarfile):
01296         """Choose the right processing method depending on
01297            the type and call it.
01298         """
01299         if self.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
01300             return self._proc_gnulong(tarfile)
01301         elif self.type == GNUTYPE_SPARSE:
01302             return self._proc_sparse(tarfile)
01303         elif self.type in (XHDTYPE, XGLTYPE, SOLARIS_XHDTYPE):
01304             return self._proc_pax(tarfile)
01305         else:
01306             return self._proc_builtin(tarfile)

Here is the call graph for this function:

def tarfile.TarInfo._proc_pax (   self,
  tarfile 
) [private]
Process an extended or global header as described in
   POSIX.1-2008.

Definition at line 1374 of file tarfile.py.

01374 
01375     def _proc_pax(self, tarfile):
01376         """Process an extended or global header as described in
01377            POSIX.1-2008.
01378         """
01379         # Read the header information.
01380         buf = tarfile.fileobj.read(self._block(self.size))
01381 
01382         # A pax header stores supplemental information for either
01383         # the following file (extended) or all following files
01384         # (global).
01385         if self.type == XGLTYPE:
01386             pax_headers = tarfile.pax_headers
01387         else:
01388             pax_headers = tarfile.pax_headers.copy()
01389 
01390         # Check if the pax header contains a hdrcharset field. This tells us
01391         # the encoding of the path, linkpath, uname and gname fields. Normally,
01392         # these fields are UTF-8 encoded but since POSIX.1-2008 tar
01393         # implementations are allowed to store them as raw binary strings if
01394         # the translation to UTF-8 fails.
01395         match = re.search(br"\d+ hdrcharset=([^\n]+)\n", buf)
01396         if match is not None:
01397             pax_headers["hdrcharset"] = match.group(1).decode("utf8")
01398 
01399         # For the time being, we don't care about anything other than "BINARY".
01400         # The only other value that is currently allowed by the standard is
01401         # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
01402         hdrcharset = pax_headers.get("hdrcharset")
01403         if hdrcharset == "BINARY":
01404             encoding = tarfile.encoding
01405         else:
01406             encoding = "utf8"
01407 
01408         # Parse pax header information. A record looks like that:
01409         # "%d %s=%s\n" % (length, keyword, value). length is the size
01410         # of the complete record including the length field itself and
01411         # the newline. keyword and value are both UTF-8 encoded strings.
01412         regex = re.compile(br"(\d+) ([^=]+)=")
01413         pos = 0
01414         while True:
01415             match = regex.match(buf, pos)
01416             if not match:
01417                 break
01418 
01419             length, keyword = match.groups()
01420             length = int(length)
01421             value = buf[match.end(2) + 1:match.start(1) + length - 1]
01422 
01423             # Normally, we could just use "utf8" as the encoding and "strict"
01424             # as the error handler, but we better not take the risk. For
01425             # example, GNU tar <= 1.23 is known to store filenames it cannot
01426             # translate to UTF-8 as raw strings (unfortunately without a
01427             # hdrcharset=BINARY header).
01428             # We first try the strict standard encoding, and if that fails we
01429             # fall back on the user's encoding and error handler.
01430             keyword = self._decode_pax_field(keyword, "utf8", "utf8",
01431                     tarfile.errors)
01432             if keyword in PAX_NAME_FIELDS:
01433                 value = self._decode_pax_field(value, encoding, tarfile.encoding,
01434                         tarfile.errors)
01435             else:
01436                 value = self._decode_pax_field(value, "utf8", "utf8",
01437                         tarfile.errors)
01438 
01439             pax_headers[keyword] = value
01440             pos += length
01441 
01442         # Fetch the next header.
01443         try:
01444             next = self.fromtarfile(tarfile)
01445         except HeaderError:
01446             raise SubsequentHeaderError("missing or bad subsequent header")
01447 
01448         # Process GNU sparse information.
01449         if "GNU.sparse.map" in pax_headers:
01450             # GNU extended sparse format version 0.1.
01451             self._proc_gnusparse_01(next, pax_headers)
01452 
01453         elif "GNU.sparse.size" in pax_headers:
01454             # GNU extended sparse format version 0.0.
01455             self._proc_gnusparse_00(next, pax_headers, buf)
01456 
01457         elif pax_headers.get("GNU.sparse.major") == "1" and pax_headers.get("GNU.sparse.minor") == "0":
01458             # GNU extended sparse format version 1.0.
01459             self._proc_gnusparse_10(next, pax_headers, tarfile)
01460 
01461         if self.type in (XHDTYPE, SOLARIS_XHDTYPE):
01462             # Patch the TarInfo object with the extended header info.
01463             next._apply_pax_info(pax_headers, tarfile.encoding, tarfile.errors)
01464             next.offset = self.offset
01465 
01466             if "size" in pax_headers:
01467                 # If the extended header replaces the size field,
01468                 # we need to recalculate the offset where the next
01469                 # header starts.
01470                 offset = next.offset_data
01471                 if next.isreg() or next.type not in SUPPORTED_TYPES:
01472                     offset += next._block(next.size)
01473                 tarfile.offset = offset
01474 
01475         return next

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._proc_sparse (   self,
  tarfile 
) [private]
Process a GNU sparse header plus extra headers.

Definition at line 1346 of file tarfile.py.

01346 
01347     def _proc_sparse(self, tarfile):
01348         """Process a GNU sparse header plus extra headers.
01349         """
01350         # We already collected some sparse structures in frombuf().
01351         structs, isextended, origsize = self._sparse_structs
01352         del self._sparse_structs
01353 
01354         # Collect sparse structures from extended header blocks.
01355         while isextended:
01356             buf = tarfile.fileobj.read(BLOCKSIZE)
01357             pos = 0
01358             for i in range(21):
01359                 try:
01360                     offset = nti(buf[pos:pos + 12])
01361                     numbytes = nti(buf[pos + 12:pos + 24])
01362                 except ValueError:
01363                     break
01364                 if offset and numbytes:
01365                     structs.append((offset, numbytes))
01366                 pos += 24
01367             isextended = bool(buf[504])
01368         self.sparse = structs
01369 
01370         self.offset_data = tarfile.fileobj.tell()
01371         tarfile.offset = self.offset_data + self._block(self.size)
01372         self.size = origsize
01373         return self

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo._setlinkpath (   self,
  linkname 
) [private]

Definition at line 962 of file tarfile.py.

00962 
00963     def _setlinkpath(self, linkname):
        self.linkname = linkname
def tarfile.TarInfo._setpath (   self,
  name 
) [private]

Definition at line 956 of file tarfile.py.

00956 
00957     def _setpath(self, name):
        self.name = name
def tarfile.TarInfo.create_gnu_header (   self,
  info,
  encoding,
  errors 
)
Return the object as a GNU header block sequence.

Definition at line 1020 of file tarfile.py.

01020 
01021     def create_gnu_header(self, info, encoding, errors):
01022         """Return the object as a GNU header block sequence.
01023         """
01024         info["magic"] = GNU_MAGIC
01025 
01026         buf = b""
01027         if len(info["linkname"]) > LENGTH_LINK:
01028             buf += self._create_gnu_long_header(info["linkname"], GNUTYPE_LONGLINK, encoding, errors)
01029 
01030         if len(info["name"]) > LENGTH_NAME:
01031             buf += self._create_gnu_long_header(info["name"], GNUTYPE_LONGNAME, encoding, errors)
01032 
01033         return buf + self._create_header(info, GNU_FORMAT, encoding, errors)

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo.create_pax_global_header (   cls,
  pax_headers 
)
Return the object as a pax global header block sequence.

Definition at line 1084 of file tarfile.py.

01084 
01085     def create_pax_global_header(cls, pax_headers):
01086         """Return the object as a pax global header block sequence.
01087         """
01088         return cls._create_pax_generic_header(pax_headers, XGLTYPE, "utf8")

def tarfile.TarInfo.create_pax_header (   self,
  info,
  encoding 
)
Return the object as a ustar header block. If it cannot be
   represented this way, prepend a pax extended header sequence
   with supplement information.

Definition at line 1034 of file tarfile.py.

01034 
01035     def create_pax_header(self, info, encoding):
01036         """Return the object as a ustar header block. If it cannot be
01037            represented this way, prepend a pax extended header sequence
01038            with supplement information.
01039         """
01040         info["magic"] = POSIX_MAGIC
01041         pax_headers = self.pax_headers.copy()
01042 
01043         # Test string fields for values that exceed the field length or cannot
01044         # be represented in ASCII encoding.
01045         for name, hname, length in (
01046                 ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK),
01047                 ("uname", "uname", 32), ("gname", "gname", 32)):
01048 
01049             if hname in pax_headers:
01050                 # The pax header has priority.
01051                 continue
01052 
01053             # Try to encode the string as ASCII.
01054             try:
01055                 info[name].encode("ascii", "strict")
01056             except UnicodeEncodeError:
01057                 pax_headers[hname] = info[name]
01058                 continue
01059 
01060             if len(info[name]) > length:
01061                 pax_headers[hname] = info[name]
01062 
01063         # Test number fields for values that exceed the field limit or values
01064         # that like to be stored as float.
01065         for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)):
01066             if name in pax_headers:
01067                 # The pax header has priority. Avoid overflow.
01068                 info[name] = 0
01069                 continue
01070 
01071             val = info[name]
01072             if not 0 <= val < 8 ** (digits - 1) or isinstance(val, float):
01073                 pax_headers[name] = str(val)
01074                 info[name] = 0
01075 
01076         # Create a pax extended header if necessary.
01077         if pax_headers:
01078             buf = self._create_pax_generic_header(pax_headers, XHDTYPE, encoding)
01079         else:
01080             buf = b""
01081 
01082         return buf + self._create_header(info, USTAR_FORMAT, "ascii", "replace")

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo.create_ustar_header (   self,
  info,
  encoding,
  errors 
)
Return the object as a ustar header block.

Definition at line 1007 of file tarfile.py.

01007 
01008     def create_ustar_header(self, info, encoding, errors):
01009         """Return the object as a ustar header block.
01010         """
01011         info["magic"] = POSIX_MAGIC
01012 
01013         if len(info["linkname"]) > LENGTH_LINK:
01014             raise ValueError("linkname is too long")
01015 
01016         if len(info["name"]) > LENGTH_NAME:
01017             info["prefix"], info["name"] = self._posix_split_name(info["name"])
01018 
01019         return self._create_header(info, USTAR_FORMAT, encoding, errors)

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo.frombuf (   cls,
  buf,
  encoding,
  errors 
)
Construct a TarInfo object from a 512 byte bytes object.

Definition at line 1211 of file tarfile.py.

01211 
01212     def frombuf(cls, buf, encoding, errors):
01213         """Construct a TarInfo object from a 512 byte bytes object.
01214         """
01215         if len(buf) == 0:
01216             raise EmptyHeaderError("empty header")
01217         if len(buf) != BLOCKSIZE:
01218             raise TruncatedHeaderError("truncated header")
01219         if buf.count(NUL) == BLOCKSIZE:
01220             raise EOFHeaderError("end of file header")
01221 
01222         chksum = nti(buf[148:156])
01223         if chksum not in calc_chksums(buf):
01224             raise InvalidHeaderError("bad checksum")
01225 
01226         obj = cls()
01227         obj.name = nts(buf[0:100], encoding, errors)
01228         obj.mode = nti(buf[100:108])
01229         obj.uid = nti(buf[108:116])
01230         obj.gid = nti(buf[116:124])
01231         obj.size = nti(buf[124:136])
01232         obj.mtime = nti(buf[136:148])
01233         obj.chksum = chksum
01234         obj.type = buf[156:157]
01235         obj.linkname = nts(buf[157:257], encoding, errors)
01236         obj.uname = nts(buf[265:297], encoding, errors)
01237         obj.gname = nts(buf[297:329], encoding, errors)
01238         obj.devmajor = nti(buf[329:337])
01239         obj.devminor = nti(buf[337:345])
01240         prefix = nts(buf[345:500], encoding, errors)
01241 
01242         # Old V7 tar format represents a directory as a regular
01243         # file with a trailing slash.
01244         if obj.type == AREGTYPE and obj.name.endswith("/"):
01245             obj.type = DIRTYPE
01246 
01247         # The old GNU sparse format occupies some of the unused
01248         # space in the buffer for up to 4 sparse structures.
01249         # Save the them for later processing in _proc_sparse().
01250         if obj.type == GNUTYPE_SPARSE:
01251             pos = 386
01252             structs = []
01253             for i in range(4):
01254                 try:
01255                     offset = nti(buf[pos:pos + 12])
01256                     numbytes = nti(buf[pos + 12:pos + 24])
01257                 except ValueError:
01258                     break
01259                 structs.append((offset, numbytes))
01260                 pos += 24
01261             isextended = bool(buf[482])
01262             origsize = nti(buf[483:495])
01263             obj._sparse_structs = (structs, isextended, origsize)
01264 
01265         # Remove redundant slashes from directories.
01266         if obj.isdir():
01267             obj.name = obj.name.rstrip("/")
01268 
01269         # Reconstruct a ustar longname.
01270         if prefix and obj.type not in GNU_TYPES:
01271             obj.name = prefix + "/" + obj.name
01272         return obj

Here is the call graph for this function:

Here is the caller graph for this function:

def tarfile.TarInfo.fromtarfile (   cls,
  tarfile 
)
Return the next TarInfo object from TarFile object
   tarfile.

Definition at line 1274 of file tarfile.py.

01274 
01275     def fromtarfile(cls, tarfile):
01276         """Return the next TarInfo object from TarFile object
01277            tarfile.
01278         """
01279         buf = tarfile.fileobj.read(BLOCKSIZE)
01280         obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
01281         obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
01282         return obj._proc_member(tarfile)

Here is the caller graph for this function:

def tarfile.TarInfo.get_info (   self)
Return the TarInfo's attributes as a dictionary.

Definition at line 969 of file tarfile.py.

00969 
00970     def get_info(self):
00971         """Return the TarInfo's attributes as a dictionary.
00972         """
00973         info = {
00974             "name":     self.name,
00975             "mode":     self.mode & 0o7777,
00976             "uid":      self.uid,
00977             "gid":      self.gid,
00978             "size":     self.size,
00979             "mtime":    self.mtime,
00980             "chksum":   self.chksum,
00981             "type":     self.type,
00982             "linkname": self.linkname,
00983             "uname":    self.uname,
00984             "gname":    self.gname,
00985             "devmajor": self.devmajor,
00986             "devminor": self.devminor
00987         }
00988 
00989         if info["type"] == DIRTYPE and not info["name"].endswith("/"):
00990             info["name"] += "/"
00991 
00992         return info

Here is the caller graph for this function:

def tarfile.TarInfo.isblk (   self)

Definition at line 1561 of file tarfile.py.

01561 
01562     def isblk(self):
        return self.type == BLKTYPE
def tarfile.TarInfo.ischr (   self)

Definition at line 1559 of file tarfile.py.

01559 
01560     def ischr(self):
        return self.type == CHRTYPE
def tarfile.TarInfo.isdev (   self)

Definition at line 1567 of file tarfile.py.

01567 
01568     def isdev(self):
01569         return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE)
01570 # class TarInfo

def tarfile.TarInfo.isdir (   self)

Definition at line 1553 of file tarfile.py.

01553 
01554     def isdir(self):
        return self.type == DIRTYPE
def tarfile.TarInfo.isfifo (   self)

Definition at line 1563 of file tarfile.py.

01563 
01564     def isfifo(self):
        return self.type == FIFOTYPE
def tarfile.TarInfo.isfile (   self)

Definition at line 1551 of file tarfile.py.

01551 
01552     def isfile(self):
        return self.isreg()

Here is the call graph for this function:

def tarfile.TarInfo.islnk (   self)

Definition at line 1557 of file tarfile.py.

01557 
01558     def islnk(self):
        return self.type == LNKTYPE
def tarfile.TarInfo.isreg (   self)

Definition at line 1549 of file tarfile.py.

01549 
01550     def isreg(self):
        return self.type in REGULAR_TYPES

Here is the caller graph for this function:

def tarfile.TarInfo.issparse (   self)

Definition at line 1565 of file tarfile.py.

01565 
01566     def issparse(self):
        return self.sparse is not None
def tarfile.TarInfo.issym (   self)

Definition at line 1555 of file tarfile.py.

01555 
01556     def issym(self):
        return self.type == SYMTYPE
def tarfile.TarInfo.tobuf (   self,
  format = DEFAULT_FORMAT,
  encoding = ENCODING,
  errors = "surrogateescape" 
)
Return a tar header as a string of 512 byte blocks.

Definition at line 993 of file tarfile.py.

00993 
00994     def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="surrogateescape"):
00995         """Return a tar header as a string of 512 byte blocks.
00996         """
00997         info = self.get_info()
00998 
00999         if format == USTAR_FORMAT:
01000             return self.create_ustar_header(info, encoding, errors)
01001         elif format == GNU_FORMAT:
01002             return self.create_gnu_header(info, encoding, errors)
01003         elif format == PAX_FORMAT:
01004             return self.create_pax_header(info, encoding)
01005         else:
01006             raise ValueError("invalid format")

Here is the call graph for this function:


Member Data Documentation

tuple tarfile.TarInfo.__slots__ [static, private]
Initial value:
("name", "mode", "uid", "gid", "size", "mtime",
                 "chksum", "type", "linkname", "uname", "gname",
                 "devmajor", "devminor",
                 "offset", "offset_data", "pax_headers", "sparse",
                 "tarfile", "_sparse_structs", "_link_target")

Definition at line 922 of file tarfile.py.

Definition at line 938 of file tarfile.py.

Definition at line 943 of file tarfile.py.

Definition at line 944 of file tarfile.py.

Definition at line 935 of file tarfile.py.

Definition at line 942 of file tarfile.py.

Definition at line 940 of file tarfile.py.

Definition at line 933 of file tarfile.py.

Definition at line 937 of file tarfile.py.

Definition at line 932 of file tarfile.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 946 of file tarfile.py.

Definition at line 947 of file tarfile.py.

Definition at line 950 of file tarfile.py.

Definition at line 936 of file tarfile.py.

Definition at line 949 of file tarfile.py.

Definition at line 939 of file tarfile.py.

Definition at line 934 of file tarfile.py.

Definition at line 941 of file tarfile.py.


Property Documentation

Definition at line 964 of file tarfile.py.

tarfile.TarInfo.path = property(_getpath, _setpath) [static]

Definition at line 958 of file tarfile.py.


The documentation for this class was generated from the following file: