219N/A# Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de> 219N/A# Permission is hereby granted, free of charge, to any person 219N/A# obtaining a copy of this software and associated documentation 219N/A# files (the "Software"), to deal in the Software without 219N/A# restriction, including without limitation the rights to use, 219N/A# copy, modify, merge, publish, distribute, sublicense, and/or sell 219N/A# copies of the Software, and to permit persons to whom the 219N/A# Software is furnished to do so, subject to the following 219N/A# The above copyright notice and this permission notice shall be 219N/A# included in all copies or substantial portions of the Software. 219N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 219N/A# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 219N/A# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 219N/A# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 219N/A# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 219N/A# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 219N/A# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 219N/A# OTHER DEALINGS IN THE SOFTWARE. 219N/A"""Read from and write to cpio format archives. 219N/A# Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 219N/A# XXX matches actual cpio archives and /etc/magic, but not archives.h 219N/ACMN_BBS =
0o143561 # Cpio Magic Number for Byte-Swap header 219N/A """General exception for extract errors.""" 219N/A """Exception for unreadble cpio archives.""" 219N/A """Exception for unavailable compression methods.""" 219N/A """Exception for unsupported operations on stream-like CpioFiles.""" 219N/A#--------------------------- 219N/A# internal stream interface 219N/A#--------------------------- 219N/A """Low-level file object. Supports reading and writing. 219N/A It is used instead of a regular file object for streaming 219N/A """Class that serves as an adapter between CpioFile and 219N/A a stream-like object. The stream-like object only 219N/A needs to have a read() or write() method and is accessed 219N/A blockwise. Use of gzip or bzip2 compression is possible. 219N/A A stream-like object could be for example: sys.stdin, 219N/A sys.stdout, a socket, a tape device etc. 219N/A _Stream is intended to be used only internally. 219N/A """Construct a _Stream object. 219N/A """Initialize for writing with gzip compression. 219N/A """Write string s to the stream. 219N/A """Write string s to the stream if a whole new block 219N/A is ready to be written. 219N/A """Close the _Stream object. No operation should be 219N/A """Initialize for reading a gzip compressed fileobj. 219N/A # taken from gzip.GzipFile with some alterations 219N/A """Return the stream's file pointer position. 219N/A """Set the stream's file pointer to pos. Negative seeking 219N/A """Return the next size number of bytes from the stream. 219N/A If size is not defined, return all bytes of the stream 219N/A # print("reading {0} bytes to {1} ({2})".format(size, self.pos, self.fileobj.tell())) 219N/A """Return size bytes from the stream. 219N/A """Return size bytes from stream. If internal buffer is empty, 219N/A read another block from the stream. 219N/A#------------------------ 219N/A# Extraction file object 219N/A#------------------------ 219N/A """File-like object for reading an archive member. 219N/A Is returned by CpioFile.extractfile(). 219N/A """Read a line with approx. size. If size is negative, 219N/A read a whole line. readline() and read() must not 219N/A """Return a list with all (following) lines. 219N/A """Return the current file position. 219N/A """Seek to a position in the file. 219N/A """Close the file object. 219N/A """Informational class which holds the details about an 219N/A archive member given by a cpio header block. 219N/A CpioInfo objects are returned by CpioFile.getmember(), 219N/A CpioFile.getmembers() and CpioFile.getcpioinfo() and are 219N/A usually created internally. 219N/A """Construct a CpioInfo object. name is the optional name 219N/A """Construct a CpioInfo object from a buffer. The buffer should 219N/A be at least 6 octets long to determine the type of archive. The 219N/A rest of the data will be read in on demand. 219N/A # Read enough for the ASCII magic 219N/A # Header is padded to halfword boundaries 219N/A # Pad to the nearest 4 byte block, 0-3 bytes. 219N/A # This isn't in tarfile, but it's too useful. It's required 219N/A # modifications to frombuf(), as well as CpioFile.next() to pass the 219N/A # CpioFile object in. I'm not sure that isn't poor OO style. 219N/A """Return a file-like object which can be read to extract the contents. 219N/A """The CpioFile Class provides an interface to cpio archives. 219N/A """Open an (uncompressed) cpio archive `name'. `mode' is either 'r' to 219N/A read from an existing archive, 'a' to append data to an existing 219N/A file or 'w' to create a new file overwriting an existing one. `mode' 219N/A If `fileobj' is given, it is used for reading or writing data. If it 219N/A can be determined, `mode' is overridden by `fileobj's mode. 219N/A `fileobj' is not closed, when CpioFile is closed. 219N/A # Copy constructor: just copy fileobj over and reset the 219N/A # _Stream object's idea of where we are back to the 219N/A # beginning. Everything else will be reset normally. 219N/A # XXX clear closed flag? 219N/A # Move to the end of the archive, 219N/A # before the first empty block. 219N/A #-------------------------------------------------------------------------- 219N/A # Below are the classmethods which act as alternate constructors to the 219N/A # CpioFile class. The open() method is the only one that is needed for 219N/A # public use; it is the "super"-constructor and is able to select an 219N/A # adequate "sub"-constructor for a particular compression using the mapping 219N/A # This concept allows one to subclass CpioFile without losing the comfort of 219N/A # the super-constructor. A sub-constructor is registered and made available 219N/A # by adding it to the mapping in OPEN_METH. 219N/A """Open a cpio archive for reading, writing or appending. Return 219N/A an appropriate CpioFile class. 219N/A 'r' open for reading with transparent compression 219N/A 'r:' open for reading exclusively uncompressed 219N/A 'r:gz' open for reading with gzip compression 219N/A 'r:bz2' open for reading with bzip2 compression 219N/A 'a' or 'a:' open for appending 219N/A 'w' or 'w:' open for writing without compression 219N/A 'w:gz' open for writing with gzip compression 219N/A 'w:bz2' open for writing with bzip2 compression 219N/A 'r|' open an uncompressed stream of cpio blocks for reading 219N/A 'r|gz' open a gzip compressed stream of cpio blocks 219N/A 'r|bz2' open a bzip2 compressed stream of cpio blocks 219N/A 'w|' open an uncompressed stream for writing 219N/A 'w|gz' open a gzip compressed stream for writing 219N/A 'w|bz2' open a bzip2 compressed stream for writing 219N/A # Select the *open() function according to 219N/A # Find out which *open() is appropriate for opening the file. 219N/A """Open uncompressed cpio archive name for reading or writing. 219N/A """Open gzip compressed cpio archive name for reading or writing. 219N/A Appending is not allowed. 219N/A """Open bzip2 compressed cpio archive name for reading or writing. 219N/A Appending is not allowed. 219N/A """Open 7z compressed cpio archive name for reading, writing. 219N/A Appending is not allowed 219N/A # To extract: 7z e -so <fname> 219N/A # To create an archive: 7z a -si <fname> 219N/A # All *open() methods are registered here. 219N/A "cpio":
"cpioopen",
# uncompressed 219N/A "gz":
"gzopen",
# gzip compressed 219N/A "bz2":
"bz2open",
# bzip2 compressed 219N/A "p7z":
"p7zopen" # 7z compressed 219N/A """Return a CpioInfo object for member `name'. If `name' can not be 219N/A found in the archive, KeyError is raised. If a member occurs more 219N/A than once in the archive, its last occurence is assumed to be the 219N/A most up-to-date version. 219N/A """Return the members of the archive as a list of CpioInfo objects. The 219N/A list has the same order as the members in the archive. 219N/A # scan the whole archive. 219N/A # Read in enough for frombuf() to be able to determine 219N/A # what kind of archive it is. It will have to read the 219N/A # if cpioinfo.chksum != calc_chksum(buf): 219N/A # self._dbg(1, "cpiofile: Bad Checksum {0!r}".format(cpioinfo.name)) 219N/A # XXX deal with other types 219N/A """Find the next cpio archive glommed on to the end of the current one. 219N/A Some applications, like Solaris package datastreams, concatenate 219N/A multiple cpio archives together, separated by a bit of padding. 219N/A This routine puts all the file pointers in position to start 219N/A reading from the next archive, which can be done by creating a 219N/A new CpioFile object given the original one as an argument (after 219N/A this routine is called). 219N/A """Return the next cpio archive glommed on to the end of the current one. 219N/A Return the CpioFile object based on the repositioning done by 219N/A # f = cf.extractfile(ci) 219N/A # for l in f.readlines():