Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: tarfile.py
#! /usr/bin/python3.8
[0] Fix | Delete
#-------------------------------------------------------------------
[1] Fix | Delete
# tarfile.py
[2] Fix | Delete
#-------------------------------------------------------------------
[3] Fix | Delete
# Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de>
[4] Fix | Delete
# All rights reserved.
[5] Fix | Delete
#
[6] Fix | Delete
# Permission is hereby granted, free of charge, to any person
[7] Fix | Delete
# obtaining a copy of this software and associated documentation
[8] Fix | Delete
# files (the "Software"), to deal in the Software without
[9] Fix | Delete
# restriction, including without limitation the rights to use,
[10] Fix | Delete
# copy, modify, merge, publish, distribute, sublicense, and/or sell
[11] Fix | Delete
# copies of the Software, and to permit persons to whom the
[12] Fix | Delete
# Software is furnished to do so, subject to the following
[13] Fix | Delete
# conditions:
[14] Fix | Delete
#
[15] Fix | Delete
# The above copyright notice and this permission notice shall be
[16] Fix | Delete
# included in all copies or substantial portions of the Software.
[17] Fix | Delete
#
[18] Fix | Delete
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
[19] Fix | Delete
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
[20] Fix | Delete
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
[21] Fix | Delete
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
[22] Fix | Delete
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
[23] Fix | Delete
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
[24] Fix | Delete
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
[25] Fix | Delete
# OTHER DEALINGS IN THE SOFTWARE.
[26] Fix | Delete
#
[27] Fix | Delete
"""Read from and write to tar format archives.
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
version = "0.9.0"
[31] Fix | Delete
__author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"
[32] Fix | Delete
__credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."
[33] Fix | Delete
[34] Fix | Delete
#---------
[35] Fix | Delete
# Imports
[36] Fix | Delete
#---------
[37] Fix | Delete
from builtins import open as bltn_open
[38] Fix | Delete
import sys
[39] Fix | Delete
import os
[40] Fix | Delete
import io
[41] Fix | Delete
import shutil
[42] Fix | Delete
import stat
[43] Fix | Delete
import time
[44] Fix | Delete
import struct
[45] Fix | Delete
import copy
[46] Fix | Delete
import re
[47] Fix | Delete
import warnings
[48] Fix | Delete
[49] Fix | Delete
try:
[50] Fix | Delete
import pwd
[51] Fix | Delete
except ImportError:
[52] Fix | Delete
pwd = None
[53] Fix | Delete
try:
[54] Fix | Delete
import grp
[55] Fix | Delete
except ImportError:
[56] Fix | Delete
grp = None
[57] Fix | Delete
[58] Fix | Delete
# os.symlink on Windows prior to 6.0 raises NotImplementedError
[59] Fix | Delete
symlink_exception = (AttributeError, NotImplementedError)
[60] Fix | Delete
try:
[61] Fix | Delete
# OSError (winerror=1314) will be raised if the caller does not hold the
[62] Fix | Delete
# SeCreateSymbolicLinkPrivilege privilege
[63] Fix | Delete
symlink_exception += (OSError,)
[64] Fix | Delete
except NameError:
[65] Fix | Delete
pass
[66] Fix | Delete
[67] Fix | Delete
# from tarfile import *
[68] Fix | Delete
__all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError", "ReadError",
[69] Fix | Delete
"CompressionError", "StreamError", "ExtractError", "HeaderError",
[70] Fix | Delete
"ENCODING", "USTAR_FORMAT", "GNU_FORMAT", "PAX_FORMAT",
[71] Fix | Delete
"DEFAULT_FORMAT", "open"]
[72] Fix | Delete
[73] Fix | Delete
# If true, use the safer (but backwards-incompatible) 'tar' extraction filter,
[74] Fix | Delete
# rather than 'fully_trusted', by default.
[75] Fix | Delete
# The emitted warning is changed to match.
[76] Fix | Delete
_RH_SAFER_DEFAULT = True
[77] Fix | Delete
[78] Fix | Delete
# System-wide configuration file
[79] Fix | Delete
_CONFIG_FILENAME = '/etc/python/tarfile.cfg'
[80] Fix | Delete
[81] Fix | Delete
#---------------------------------------------------------
[82] Fix | Delete
# tar constants
[83] Fix | Delete
#---------------------------------------------------------
[84] Fix | Delete
NUL = b"\0" # the null character
[85] Fix | Delete
BLOCKSIZE = 512 # length of processing blocks
[86] Fix | Delete
RECORDSIZE = BLOCKSIZE * 20 # length of records
[87] Fix | Delete
GNU_MAGIC = b"ustar \0" # magic gnu tar string
[88] Fix | Delete
POSIX_MAGIC = b"ustar\x0000" # magic posix tar string
[89] Fix | Delete
[90] Fix | Delete
LENGTH_NAME = 100 # maximum length of a filename
[91] Fix | Delete
LENGTH_LINK = 100 # maximum length of a linkname
[92] Fix | Delete
LENGTH_PREFIX = 155 # maximum length of the prefix field
[93] Fix | Delete
[94] Fix | Delete
REGTYPE = b"0" # regular file
[95] Fix | Delete
AREGTYPE = b"\0" # regular file
[96] Fix | Delete
LNKTYPE = b"1" # link (inside tarfile)
[97] Fix | Delete
SYMTYPE = b"2" # symbolic link
[98] Fix | Delete
CHRTYPE = b"3" # character special device
[99] Fix | Delete
BLKTYPE = b"4" # block special device
[100] Fix | Delete
DIRTYPE = b"5" # directory
[101] Fix | Delete
FIFOTYPE = b"6" # fifo special device
[102] Fix | Delete
CONTTYPE = b"7" # contiguous file
[103] Fix | Delete
[104] Fix | Delete
GNUTYPE_LONGNAME = b"L" # GNU tar longname
[105] Fix | Delete
GNUTYPE_LONGLINK = b"K" # GNU tar longlink
[106] Fix | Delete
GNUTYPE_SPARSE = b"S" # GNU tar sparse file
[107] Fix | Delete
[108] Fix | Delete
XHDTYPE = b"x" # POSIX.1-2001 extended header
[109] Fix | Delete
XGLTYPE = b"g" # POSIX.1-2001 global header
[110] Fix | Delete
SOLARIS_XHDTYPE = b"X" # Solaris extended header
[111] Fix | Delete
[112] Fix | Delete
USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
[113] Fix | Delete
GNU_FORMAT = 1 # GNU tar format
[114] Fix | Delete
PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
[115] Fix | Delete
DEFAULT_FORMAT = PAX_FORMAT
[116] Fix | Delete
[117] Fix | Delete
#---------------------------------------------------------
[118] Fix | Delete
# tarfile constants
[119] Fix | Delete
#---------------------------------------------------------
[120] Fix | Delete
# File types that tarfile supports:
[121] Fix | Delete
SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
[122] Fix | Delete
SYMTYPE, DIRTYPE, FIFOTYPE,
[123] Fix | Delete
CONTTYPE, CHRTYPE, BLKTYPE,
[124] Fix | Delete
GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[125] Fix | Delete
GNUTYPE_SPARSE)
[126] Fix | Delete
[127] Fix | Delete
# File types that will be treated as a regular file.
[128] Fix | Delete
REGULAR_TYPES = (REGTYPE, AREGTYPE,
[129] Fix | Delete
CONTTYPE, GNUTYPE_SPARSE)
[130] Fix | Delete
[131] Fix | Delete
# File types that are part of the GNU tar format.
[132] Fix | Delete
GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
[133] Fix | Delete
GNUTYPE_SPARSE)
[134] Fix | Delete
[135] Fix | Delete
# Fields from a pax header that override a TarInfo attribute.
[136] Fix | Delete
PAX_FIELDS = ("path", "linkpath", "size", "mtime",
[137] Fix | Delete
"uid", "gid", "uname", "gname")
[138] Fix | Delete
[139] Fix | Delete
# Fields from a pax header that are affected by hdrcharset.
[140] Fix | Delete
PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}
[141] Fix | Delete
[142] Fix | Delete
# Fields in a pax header that are numbers, all other fields
[143] Fix | Delete
# are treated as strings.
[144] Fix | Delete
PAX_NUMBER_FIELDS = {
[145] Fix | Delete
"atime": float,
[146] Fix | Delete
"ctime": float,
[147] Fix | Delete
"mtime": float,
[148] Fix | Delete
"uid": int,
[149] Fix | Delete
"gid": int,
[150] Fix | Delete
"size": int
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
#---------------------------------------------------------
[154] Fix | Delete
# initialization
[155] Fix | Delete
#---------------------------------------------------------
[156] Fix | Delete
if os.name == "nt":
[157] Fix | Delete
ENCODING = "utf-8"
[158] Fix | Delete
else:
[159] Fix | Delete
ENCODING = sys.getfilesystemencoding()
[160] Fix | Delete
[161] Fix | Delete
#---------------------------------------------------------
[162] Fix | Delete
# Some useful functions
[163] Fix | Delete
#---------------------------------------------------------
[164] Fix | Delete
[165] Fix | Delete
def stn(s, length, encoding, errors):
[166] Fix | Delete
"""Convert a string to a null-terminated bytes object.
[167] Fix | Delete
"""
[168] Fix | Delete
if s is None:
[169] Fix | Delete
raise ValueError("metadata cannot contain None")
[170] Fix | Delete
s = s.encode(encoding, errors)
[171] Fix | Delete
return s[:length] + (length - len(s)) * NUL
[172] Fix | Delete
[173] Fix | Delete
def nts(s, encoding, errors):
[174] Fix | Delete
"""Convert a null-terminated bytes object to a string.
[175] Fix | Delete
"""
[176] Fix | Delete
p = s.find(b"\0")
[177] Fix | Delete
if p != -1:
[178] Fix | Delete
s = s[:p]
[179] Fix | Delete
return s.decode(encoding, errors)
[180] Fix | Delete
[181] Fix | Delete
def nti(s):
[182] Fix | Delete
"""Convert a number field to a python number.
[183] Fix | Delete
"""
[184] Fix | Delete
# There are two possible encodings for a number field, see
[185] Fix | Delete
# itn() below.
[186] Fix | Delete
if s[0] in (0o200, 0o377):
[187] Fix | Delete
n = 0
[188] Fix | Delete
for i in range(len(s) - 1):
[189] Fix | Delete
n <<= 8
[190] Fix | Delete
n += s[i + 1]
[191] Fix | Delete
if s[0] == 0o377:
[192] Fix | Delete
n = -(256 ** (len(s) - 1) - n)
[193] Fix | Delete
else:
[194] Fix | Delete
try:
[195] Fix | Delete
s = nts(s, "ascii", "strict")
[196] Fix | Delete
n = int(s.strip() or "0", 8)
[197] Fix | Delete
except ValueError:
[198] Fix | Delete
raise InvalidHeaderError("invalid header")
[199] Fix | Delete
return n
[200] Fix | Delete
[201] Fix | Delete
def itn(n, digits=8, format=DEFAULT_FORMAT):
[202] Fix | Delete
"""Convert a python number to a number field.
[203] Fix | Delete
"""
[204] Fix | Delete
# POSIX 1003.1-1988 requires numbers to be encoded as a string of
[205] Fix | Delete
# octal digits followed by a null-byte, this allows values up to
[206] Fix | Delete
# (8**(digits-1))-1. GNU tar allows storing numbers greater than
[207] Fix | Delete
# that if necessary. A leading 0o200 or 0o377 byte indicate this
[208] Fix | Delete
# particular encoding, the following digits-1 bytes are a big-endian
[209] Fix | Delete
# base-256 representation. This allows values up to (256**(digits-1))-1.
[210] Fix | Delete
# A 0o200 byte indicates a positive number, a 0o377 byte a negative
[211] Fix | Delete
# number.
[212] Fix | Delete
n = int(n)
[213] Fix | Delete
if 0 <= n < 8 ** (digits - 1):
[214] Fix | Delete
s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
[215] Fix | Delete
elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
[216] Fix | Delete
if n >= 0:
[217] Fix | Delete
s = bytearray([0o200])
[218] Fix | Delete
else:
[219] Fix | Delete
s = bytearray([0o377])
[220] Fix | Delete
n = 256 ** digits + n
[221] Fix | Delete
[222] Fix | Delete
for i in range(digits - 1):
[223] Fix | Delete
s.insert(1, n & 0o377)
[224] Fix | Delete
n >>= 8
[225] Fix | Delete
else:
[226] Fix | Delete
raise ValueError("overflow in number field")
[227] Fix | Delete
[228] Fix | Delete
return s
[229] Fix | Delete
[230] Fix | Delete
def calc_chksums(buf):
[231] Fix | Delete
"""Calculate the checksum for a member's header by summing up all
[232] Fix | Delete
characters except for the chksum field which is treated as if
[233] Fix | Delete
it was filled with spaces. According to the GNU tar sources,
[234] Fix | Delete
some tars (Sun and NeXT) calculate chksum with signed char,
[235] Fix | Delete
which will be different if there are chars in the buffer with
[236] Fix | Delete
the high bit set. So we calculate two checksums, unsigned and
[237] Fix | Delete
signed.
[238] Fix | Delete
"""
[239] Fix | Delete
unsigned_chksum = 256 + sum(struct.unpack_from("148B8x356B", buf))
[240] Fix | Delete
signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf))
[241] Fix | Delete
return unsigned_chksum, signed_chksum
[242] Fix | Delete
[243] Fix | Delete
def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
[244] Fix | Delete
"""Copy length bytes from fileobj src to fileobj dst.
[245] Fix | Delete
If length is None, copy the entire content.
[246] Fix | Delete
"""
[247] Fix | Delete
bufsize = bufsize or 16 * 1024
[248] Fix | Delete
if length == 0:
[249] Fix | Delete
return
[250] Fix | Delete
if length is None:
[251] Fix | Delete
shutil.copyfileobj(src, dst, bufsize)
[252] Fix | Delete
return
[253] Fix | Delete
[254] Fix | Delete
blocks, remainder = divmod(length, bufsize)
[255] Fix | Delete
for b in range(blocks):
[256] Fix | Delete
buf = src.read(bufsize)
[257] Fix | Delete
if len(buf) < bufsize:
[258] Fix | Delete
raise exception("unexpected end of data")
[259] Fix | Delete
dst.write(buf)
[260] Fix | Delete
[261] Fix | Delete
if remainder != 0:
[262] Fix | Delete
buf = src.read(remainder)
[263] Fix | Delete
if len(buf) < remainder:
[264] Fix | Delete
raise exception("unexpected end of data")
[265] Fix | Delete
dst.write(buf)
[266] Fix | Delete
return
[267] Fix | Delete
[268] Fix | Delete
def _safe_print(s):
[269] Fix | Delete
encoding = getattr(sys.stdout, 'encoding', None)
[270] Fix | Delete
if encoding is not None:
[271] Fix | Delete
s = s.encode(encoding, 'backslashreplace').decode(encoding)
[272] Fix | Delete
print(s, end=' ')
[273] Fix | Delete
[274] Fix | Delete
[275] Fix | Delete
class TarError(Exception):
[276] Fix | Delete
"""Base exception."""
[277] Fix | Delete
pass
[278] Fix | Delete
class ExtractError(TarError):
[279] Fix | Delete
"""General exception for extract errors."""
[280] Fix | Delete
pass
[281] Fix | Delete
class ReadError(TarError):
[282] Fix | Delete
"""Exception for unreadable tar archives."""
[283] Fix | Delete
pass
[284] Fix | Delete
class CompressionError(TarError):
[285] Fix | Delete
"""Exception for unavailable compression methods."""
[286] Fix | Delete
pass
[287] Fix | Delete
class StreamError(TarError):
[288] Fix | Delete
"""Exception for unsupported operations on stream-like TarFiles."""
[289] Fix | Delete
pass
[290] Fix | Delete
class HeaderError(TarError):
[291] Fix | Delete
"""Base exception for header errors."""
[292] Fix | Delete
pass
[293] Fix | Delete
class EmptyHeaderError(HeaderError):
[294] Fix | Delete
"""Exception for empty headers."""
[295] Fix | Delete
pass
[296] Fix | Delete
class TruncatedHeaderError(HeaderError):
[297] Fix | Delete
"""Exception for truncated headers."""
[298] Fix | Delete
pass
[299] Fix | Delete
class EOFHeaderError(HeaderError):
[300] Fix | Delete
"""Exception for end of file headers."""
[301] Fix | Delete
pass
[302] Fix | Delete
class InvalidHeaderError(HeaderError):
[303] Fix | Delete
"""Exception for invalid headers."""
[304] Fix | Delete
pass
[305] Fix | Delete
class SubsequentHeaderError(HeaderError):
[306] Fix | Delete
"""Exception for missing and invalid extended headers."""
[307] Fix | Delete
pass
[308] Fix | Delete
[309] Fix | Delete
#---------------------------
[310] Fix | Delete
# internal stream interface
[311] Fix | Delete
#---------------------------
[312] Fix | Delete
class _LowLevelFile:
[313] Fix | Delete
"""Low-level file object. Supports reading and writing.
[314] Fix | Delete
It is used instead of a regular file object for streaming
[315] Fix | Delete
access.
[316] Fix | Delete
"""
[317] Fix | Delete
[318] Fix | Delete
def __init__(self, name, mode):
[319] Fix | Delete
mode = {
[320] Fix | Delete
"r": os.O_RDONLY,
[321] Fix | Delete
"w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
[322] Fix | Delete
}[mode]
[323] Fix | Delete
if hasattr(os, "O_BINARY"):
[324] Fix | Delete
mode |= os.O_BINARY
[325] Fix | Delete
self.fd = os.open(name, mode, 0o666)
[326] Fix | Delete
[327] Fix | Delete
def close(self):
[328] Fix | Delete
os.close(self.fd)
[329] Fix | Delete
[330] Fix | Delete
def read(self, size):
[331] Fix | Delete
return os.read(self.fd, size)
[332] Fix | Delete
[333] Fix | Delete
def write(self, s):
[334] Fix | Delete
os.write(self.fd, s)
[335] Fix | Delete
[336] Fix | Delete
class _Stream:
[337] Fix | Delete
"""Class that serves as an adapter between TarFile and
[338] Fix | Delete
a stream-like object. The stream-like object only
[339] Fix | Delete
needs to have a read() or write() method and is accessed
[340] Fix | Delete
blockwise. Use of gzip or bzip2 compression is possible.
[341] Fix | Delete
A stream-like object could be for example: sys.stdin,
[342] Fix | Delete
sys.stdout, a socket, a tape device etc.
[343] Fix | Delete
[344] Fix | Delete
_Stream is intended to be used only internally.
[345] Fix | Delete
"""
[346] Fix | Delete
[347] Fix | Delete
def __init__(self, name, mode, comptype, fileobj, bufsize):
[348] Fix | Delete
"""Construct a _Stream object.
[349] Fix | Delete
"""
[350] Fix | Delete
self._extfileobj = True
[351] Fix | Delete
if fileobj is None:
[352] Fix | Delete
fileobj = _LowLevelFile(name, mode)
[353] Fix | Delete
self._extfileobj = False
[354] Fix | Delete
[355] Fix | Delete
if comptype == '*':
[356] Fix | Delete
# Enable transparent compression detection for the
[357] Fix | Delete
# stream interface
[358] Fix | Delete
fileobj = _StreamProxy(fileobj)
[359] Fix | Delete
comptype = fileobj.getcomptype()
[360] Fix | Delete
[361] Fix | Delete
self.name = name or ""
[362] Fix | Delete
self.mode = mode
[363] Fix | Delete
self.comptype = comptype
[364] Fix | Delete
self.fileobj = fileobj
[365] Fix | Delete
self.bufsize = bufsize
[366] Fix | Delete
self.buf = b""
[367] Fix | Delete
self.pos = 0
[368] Fix | Delete
self.closed = False
[369] Fix | Delete
[370] Fix | Delete
try:
[371] Fix | Delete
if comptype == "gz":
[372] Fix | Delete
try:
[373] Fix | Delete
import zlib
[374] Fix | Delete
except ImportError:
[375] Fix | Delete
raise CompressionError("zlib module is not available")
[376] Fix | Delete
self.zlib = zlib
[377] Fix | Delete
self.crc = zlib.crc32(b"")
[378] Fix | Delete
if mode == "r":
[379] Fix | Delete
self._init_read_gz()
[380] Fix | Delete
self.exception = zlib.error
[381] Fix | Delete
else:
[382] Fix | Delete
self._init_write_gz()
[383] Fix | Delete
[384] Fix | Delete
elif comptype == "bz2":
[385] Fix | Delete
try:
[386] Fix | Delete
import bz2
[387] Fix | Delete
except ImportError:
[388] Fix | Delete
raise CompressionError("bz2 module is not available")
[389] Fix | Delete
if mode == "r":
[390] Fix | Delete
self.dbuf = b""
[391] Fix | Delete
self.cmp = bz2.BZ2Decompressor()
[392] Fix | Delete
self.exception = OSError
[393] Fix | Delete
else:
[394] Fix | Delete
self.cmp = bz2.BZ2Compressor()
[395] Fix | Delete
[396] Fix | Delete
elif comptype == "xz":
[397] Fix | Delete
try:
[398] Fix | Delete
import lzma
[399] Fix | Delete
except ImportError:
[400] Fix | Delete
raise CompressionError("lzma module is not available")
[401] Fix | Delete
if mode == "r":
[402] Fix | Delete
self.dbuf = b""
[403] Fix | Delete
self.cmp = lzma.LZMADecompressor()
[404] Fix | Delete
self.exception = lzma.LZMAError
[405] Fix | Delete
else:
[406] Fix | Delete
self.cmp = lzma.LZMACompressor()
[407] Fix | Delete
[408] Fix | Delete
elif comptype != "tar":
[409] Fix | Delete
raise CompressionError("unknown compression type %r" % comptype)
[410] Fix | Delete
[411] Fix | Delete
except:
[412] Fix | Delete
if not self._extfileobj:
[413] Fix | Delete
self.fileobj.close()
[414] Fix | Delete
self.closed = True
[415] Fix | Delete
raise
[416] Fix | Delete
[417] Fix | Delete
def __del__(self):
[418] Fix | Delete
if hasattr(self, "closed") and not self.closed:
[419] Fix | Delete
self.close()
[420] Fix | Delete
[421] Fix | Delete
def _init_write_gz(self):
[422] Fix | Delete
"""Initialize for writing with gzip compression.
[423] Fix | Delete
"""
[424] Fix | Delete
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
[425] Fix | Delete
-self.zlib.MAX_WBITS,
[426] Fix | Delete
self.zlib.DEF_MEM_LEVEL,
[427] Fix | Delete
0)
[428] Fix | Delete
timestamp = struct.pack("<L", int(time.time()))
[429] Fix | Delete
self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
[430] Fix | Delete
if self.name.endswith(".gz"):
[431] Fix | Delete
self.name = self.name[:-3]
[432] Fix | Delete
# Honor "directory components removed" from RFC1952
[433] Fix | Delete
self.name = os.path.basename(self.name)
[434] Fix | Delete
# RFC1952 says we must use ISO-8859-1 for the FNAME field.
[435] Fix | Delete
self.__write(self.name.encode("iso-8859-1", "replace") + NUL)
[436] Fix | Delete
[437] Fix | Delete
def write(self, s):
[438] Fix | Delete
"""Write string s to the stream.
[439] Fix | Delete
"""
[440] Fix | Delete
if self.comptype == "gz":
[441] Fix | Delete
self.crc = self.zlib.crc32(s, self.crc)
[442] Fix | Delete
self.pos += len(s)
[443] Fix | Delete
if self.comptype != "tar":
[444] Fix | Delete
s = self.cmp.compress(s)
[445] Fix | Delete
self.__write(s)
[446] Fix | Delete
[447] Fix | Delete
def __write(self, s):
[448] Fix | Delete
"""Write string s to the stream if a whole new block
[449] Fix | Delete
is ready to be written.
[450] Fix | Delete
"""
[451] Fix | Delete
self.buf += s
[452] Fix | Delete
while len(self.buf) > self.bufsize:
[453] Fix | Delete
self.fileobj.write(self.buf[:self.bufsize])
[454] Fix | Delete
self.buf = self.buf[self.bufsize:]
[455] Fix | Delete
[456] Fix | Delete
def close(self):
[457] Fix | Delete
"""Close the _Stream object. No operation should be
[458] Fix | Delete
done on it afterwards.
[459] Fix | Delete
"""
[460] Fix | Delete
if self.closed:
[461] Fix | Delete
return
[462] Fix | Delete
[463] Fix | Delete
self.closed = True
[464] Fix | Delete
try:
[465] Fix | Delete
if self.mode == "w" and self.comptype != "tar":
[466] Fix | Delete
self.buf += self.cmp.flush()
[467] Fix | Delete
[468] Fix | Delete
if self.mode == "w" and self.buf:
[469] Fix | Delete
self.fileobj.write(self.buf)
[470] Fix | Delete
self.buf = b""
[471] Fix | Delete
if self.comptype == "gz":
[472] Fix | Delete
self.fileobj.write(struct.pack("<L", self.crc))
[473] Fix | Delete
self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
[474] Fix | Delete
finally:
[475] Fix | Delete
if not self._extfileobj:
[476] Fix | Delete
self.fileobj.close()
[477] Fix | Delete
[478] Fix | Delete
def _init_read_gz(self):
[479] Fix | Delete
"""Initialize for reading a gzip compressed fileobj.
[480] Fix | Delete
"""
[481] Fix | Delete
self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
[482] Fix | Delete
self.dbuf = b""
[483] Fix | Delete
[484] Fix | Delete
# taken from gzip.GzipFile with some alterations
[485] Fix | Delete
if self.__read(2) != b"\037\213":
[486] Fix | Delete
raise ReadError("not a gzip file")
[487] Fix | Delete
if self.__read(1) != b"\010":
[488] Fix | Delete
raise CompressionError("unsupported compression method")
[489] Fix | Delete
[490] Fix | Delete
flag = ord(self.__read(1))
[491] Fix | Delete
self.__read(6)
[492] Fix | Delete
[493] Fix | Delete
if flag & 4:
[494] Fix | Delete
xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
[495] Fix | Delete
self.read(xlen)
[496] Fix | Delete
if flag & 8:
[497] Fix | Delete
while True:
[498] Fix | Delete
s = self.__read(1)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function