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