Lines Matching +full:tar +full:- +full:stream
2 #-------------------------------------------------------------------
4 #-------------------------------------------------------------------
29 """Read from and write to tar format archives.
36 #---------
38 #---------
76 #---------------------------------------------------------
77 # tar constants
78 #---------------------------------------------------------
82 GNU_MAGIC = b"ustar \0" # magic gnu tar string
83 POSIX_MAGIC = b"ustar\x0000" # magic posix tar string
99 GNUTYPE_LONGNAME = b"L" # GNU tar longname
100 GNUTYPE_LONGLINK = b"K" # GNU tar longlink
101 GNUTYPE_SPARSE = b"S" # GNU tar sparse file
103 XHDTYPE = b"x" # POSIX.1-2001 extended header
104 XGLTYPE = b"g" # POSIX.1-2001 global header
107 USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
108 GNU_FORMAT = 1 # GNU tar format
109 PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
112 #---------------------------------------------------------
114 #---------------------------------------------------------
126 # File types that are part of the GNU tar format.
148 #---------------------------------------------------------
150 #---------------------------------------------------------
152 ENCODING = "utf-8"
156 #---------------------------------------------------------
158 #---------------------------------------------------------
161 """Convert a string to a null-terminated bytes object.
166 return s[:length] + (length - len(s)) * NUL
169 """Convert a null-terminated bytes object to a string.
172 if p != -1:
183 for i in range(len(s) - 1):
187 n = -(256 ** (len(s) - 1) - n)
199 # POSIX 1003.1-1988 requires numbers to be encoded as a string of
200 # octal digits followed by a null-byte, this allows values up to
201 # (8**(digits-1))-1. GNU tar allows storing numbers greater than
203 # particular encoding, the following digits-1 bytes are a big-endian
204 # base-256 representation. This allows values up to (256**(digits-1))-1.
209 if 0 <= n < 8 ** (digits - 1):
210 s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
211 elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
218 for i in range(digits - 1):
229 it was filled with spaces. According to the GNU tar sources,
278 """Exception for unreadable tar archives."""
284 """Exception for unsupported operations on stream-like TarFiles."""
305 #---------------------------
306 # internal stream interface
307 #---------------------------
309 """Low-level file object. Supports reading and writing.
334 a stream-like object. The stream-like object only
337 A stream-like object could be for example: sys.stdin,
353 # stream interface
404 elif comptype != "tar":
421 -self.zlib.MAX_WBITS,
427 self.name = self.name[:-3]
430 # RFC1952 says we must use ISO-8859-1 for the FNAME field.
431 self.__write(self.name.encode("iso-8859-1", "replace") + NUL)
434 """Write string s to the stream.
439 if self.comptype != "tar":
444 """Write string s to the stream if a whole new block
461 if self.mode == "w" and self.comptype != "tar":
477 self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
506 """Return the stream's file pointer position.
511 """Set the stream's file pointer to pos. Negative seeking
514 if pos - self.pos >= 0:
515 blocks, remainder = divmod(pos - self.pos, self.bufsize)
524 """Return the next size number of bytes from the stream."""
531 """Return size bytes from the stream.
533 if self.comptype == "tar":
558 """Return size bytes from stream. If internal buffer is empty,
559 read another block from the stream.
576 detection for the Stream interface (mode 'r|*').
595 return "tar"
601 #------------------------
603 #------------------------
672 size = self.size - self.position
674 size = min(size, self.size - self.position)
686 length = min(size, stop - self.position)
688 self.fileobj.seek(offset + (self.position - start))
695 size -= length
717 #-----------------------------
719 #-----------------------------
757 # Strip leading / (tar's directory separator) from filenames.
769 # Limit permissions (no high bits, and go-w)
826 "tar": tar_filter,
830 #------------------
832 #------------------
839 archive member given by a tar header block.
862 offset = 'The tar header starts here.',
864 pax_headers = ('A dictionary containing key-value pairs of an '
890 self.offset = 0 # the tar header starts here
974 """Return a tar header as a string of 512 byte blocks.
1053 if not 0 <= val_int < 8 ** (digits - 1):
1079 return cls._create_pax_generic_header(pax_headers, XGLTYPE, "utf-8")
1135 chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
1136 buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:]
1146 payload += (BLOCKSIZE - remainder) * NUL
1168 """Return a POSIX.1-2008 extended or global header sequence
1177 value.encode("utf-8", "strict")
1188 keyword = keyword.encode("utf-8")
1194 value = value.encode("utf-8")
1248 # Old V7 tar format represents a directory as a regular
1287 obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
1290 #--------------------------------------------------------------------------
1393 POSIX.1-2008.
1408 # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1410 # the translation to UTF-8 fails.
1413 pax_headers["hdrcharset"] = match.group(1).decode("utf-8")
1417 # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1422 encoding = "utf-8"
1427 # the newline. keyword and value are both UTF-8 encoded strings.
1439 value = buf[match.end(2) + 1:match.start(1) + length - 1]
1441 # Normally, we could just use "utf-8" as the encoding and "strict"
1443 # example, GNU tar <= 1.23 is known to store filenames it cannot
1444 # translate to UTF-8 as raw strings (unfortunately without a
1448 keyword = self._decode_pax_field(keyword, "utf-8", "utf-8",
1454 value = self._decode_pax_field(value, "utf-8", "utf-8",
1496 """Process a GNU tar extended sparse header, version 0.0.
1507 """Process a GNU tar extended sparse header, version 0.1.
1513 """Process a GNU tar extended sparse header, version 1.0.
1609 """The TarFile Class provides an interface to tar archives.
1615 # tar file, else the link.
1626 encoding = ENCODING # Encoding for 8-bit character strings.
1632 fileobject = ExFileObject # The file-object for extractfile().
1640 """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
1736 #--------------------------------------------------------------------------
1739 # public use; it is the "super"-constructor and is able to select an
1740 # adequate "sub"-constructor for a particular compression using the mapping
1744 # the super-constructor. A sub-constructor is registered and made available
1749 """Open a tar archive for reading, writing or appending. Return
1773 'r|*' open a stream of tar blocks with transparent compression
1774 'r|' open an uncompressed stream of tar blocks for reading
1775 'r|gz' open a gzip compressed stream of tar blocks
1776 'r|bz2' open a bzip2 compressed stream of tar blocks
1777 'r|xz' open an lzma compressed stream of tar blocks
1778 'w|' open an uncompressed stream for writing
1779 'w|gz' open a gzip compressed stream for writing
1780 'w|bz2' open a bzip2 compressed stream for writing
1781 'w|xz' open an lzma compressed stream for writing
1799 error_msgs.append(f'- method {comptype}: {e!r}')
1809 comptype = comptype or "tar"
1822 comptype = comptype or "tar"
1827 stream = _Stream(name, filemode, comptype, fileobj, bufsize)
1829 t = cls(name, filemode, stream, **kwargs)
1831 stream.close()
1843 """Open uncompressed tar archive name for reading or writing.
1851 """Open gzip compressed tar archive name for reading or writing.
1884 """Open bzip2 compressed tar archive name for reading or writing.
1912 """Open lzma compressed tar archive name for reading or writing.
1940 "tar": "taropen", # uncompressed tar
1941 "gz": "gzopen", # gzip compressed tar
1942 "bz2": "bz2open", # bzip2 compressed tar
1943 "xz": "xzopen" # lzma compressed tar
1946 #--------------------------------------------------------------------------
1950 """Close the TarFile. In write-mode, two finishing zero blocks are
1961 # fill up the end with zero-blocks
1962 # (like option -b20 for tar does)
1965 self.fileobj.write(NUL * (RECORDSIZE - remainder))
1974 most up-to-date version.
2098 the names of the members are printed. If it is True, an `ls -l'-like
2120 _safe_print("????-??-?? ??:??:??")
2122 _safe_print("%d-%02d-%02d %02d:%02d:%02d" \
2129 _safe_print("-> " + tarinfo.linkname)
2170 # Append the tar header and data to the archive.
2204 self.fileobj.write(NUL * (BLOCKSIZE - remainder))
2329 """Handle non-fatal error (ExtractError) according to errorlevel"""
2368 # to extract a (sym)link as a file-object from a non-seekable
2369 # stream of tar blocks.
2398 self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
2423 #--------------------------------------------------------------------------
2539 g = -1
2541 u = -1
2573 #--------------------------------------------------------------------------
2589 self.fileobj.seek(self.offset - 1)
2636 #--------------------------------------------------------------------------
2763 # it would try to write end-of-archive blocks and padding.
2768 #--------------------
2770 #--------------------
2773 """Return True if name points to a tar archive that we
2776 'name' should be a string, file, or file-like object.
2796 description = 'A simple command-line interface for tarfile module.'
2798 parser.add_argument('-v', '--verbose', action='store_true', default=False,
2800 parser.add_argument('--filter', metavar='<filtername>',
2805 group.add_argument('-l', '--list', metavar='<tarfile>',
2807 group.add_argument('-e', '--extract', nargs='+',
2810 group.add_argument('-c', '--create', nargs='+',
2813 group.add_argument('-t', '--test', metavar='<tarfile>',
2819 parser.exit(1, '--filter is only valid for extraction\n')
2824 with open(src, 'r') as tar:
2825 tar.getmembers()
2826 print(tar.getmembers(), file=sys.stderr)
2828 print('{!r} is a tar archive.'.format(src))
2830 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
2838 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
2860 parser.exit(1, '{!r} is not a tar archive.\n'.format(src))