xcp.cpiofile

Read from and write to cpio format archives.

Of course: Only platforms with filesystems supporting hardlinks support extracting cpio files containing hardlinks, but Dom0 does.

Derived from Lars Gustäbel’s tarfile.py

exception xcp.cpiofile.CpioError[source]

Bases: Exception

Base exception.

class xcp.cpiofile.CpioFile(name: str | None = None, mode: str = 'r', fileobj: IO[bytes] | GzipFile | _Stream | None = None)[source]

Bases: object

The CpioFile Class provides an interface to cpio archives.

OPEN_METH = {'bz2': 'bz2open', 'cpio': 'cpioopen', 'gz': 'gzopen', 'xz': 'xzopen'}
add(name, arcname=None, recursive=True)[source]

Add the file name to the archive. name may be any type of file (directory, fifo, symbolic link, etc.). If given, arcname specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting recursive to False.

addfile(cpioinfo, fileobj=None)[source]

Add the CpioInfo object cpioinfo to the archive. If fileobj is given, cpioinfo.size bytes are read from it and added to the archive. You can create CpioInfo objects using getcpioinfo(). On Windows platforms, fileobj should always be opened with mode ‘rb’ to avoid irritation about the file size.

classmethod bz2open(name: str, mode: Literal['r', 'w'] = 'r', fileobj: IO[bytes] | None = None, compresslevel: int = 9) CpioFile[source]

Open bzip2 compressed cpio archive name for reading or writing, no appending

chmod(cpioinfo, targetpath)[source]

Set file permissions of targetpath according to cpioinfo.

chown(cpioinfo, targetpath)[source]

Set owner of targetpath according to cpioinfo.

close()[source]

Close the CpioFile. In write-mode, a trailer record is appended to the archive.

classmethod cpioopen(name: str, mode: str = 'r', fileobj: GzipFile | IO[bytes] | None = None) CpioFile[source]

Open uncompressed cpio archive name for reading or writing.

debug = 0
dereference = False
errorlevel = 0
extract(member, path='')[source]

Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. member may be a filename or a CpioInfo object. You can specify a different directory using path.

extractall(path='.', members=None)[source]

Extract all members from the archive to the current working directory and set owner, modification time and permissions on directories afterwards. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by getmembers().

extractfile(member: CpioInfo | str | bytes) ExFileObject | None[source]

Extract a member from the archive as a file object. member may be a filename or a CpioInfo object. If member is a regular file, a file-like object is returned. If member is a link, a file-like object is constructed from the link’s target. If member is none of the above, None is returned. The file-like object is read-only and provides the following methods: read(), readline(), readlines(), seek() and tell()

fileobject

alias of ExFileObject

getcpioinfo(name=None, arcname=None, fileobj=None)[source]

Create a CpioInfo object for either the file name or the file object fileobj (using os.fstat on its file descriptor). You can modify some of the CpioInfo’s attributes before you add it using addfile(). If given, arcname specifies an alternative name for the file in the archive.

getmember(name: str | bytes) CpioInfo[source]

Return a CpioInfo object for member name. If name can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version.

getmembers() List[CpioInfo][source]

Return the members of the archive as a list of CpioInfo objects. The list has the same order as the members in the archive.

getnames()[source]

Return the members of the archive as a list of their names. It has the same order as the list returned by getmembers().

classmethod gzopen(name, mode='r', fileobj=None, compresslevel=9)[source]

Open gzip compressed cpio archive name for reading or writing. Appending is not allowed.

list(verbose=True)[source]

Print a table of contents to sys.stdout. If verbose is False, only the names of the members are printed. If it is True, an ls -l-like output is produced.

makedev(cpioinfo, targetpath)[source]

Make a character or block device called targetpath.

makedir(cpioinfo, targetpath)[source]

Make a directory called targetpath.

makefifo(cpioinfo, targetpath)[source]

Make a fifo called targetpath.

makefile(cpioinfo, targetpath)[source]

Make a file called targetpath.

Make a (symbolic) link called targetpath. If it cannot be created (platform limitation), we try to make a copy of the referenced file instead of a link.

classmethod open(name=None, mode='r', fileobj=None, bufsize=10240)[source]

Open a cpio archive for reading, writing or appending. Return an appropriate CpioFile class.

mode:

  • r or r:* open for reading with transparent compression

  • r: open for reading exclusively uncompressed

  • r:gz open for reading with gzip compression

  • r:bz2 open for reading with bzip2 compression

  • r:xz open for reading with xz compression

  • a or a: open for appending

  • w or w: open for writing without compression

  • w:gz open for writing with gzip compression

  • w:bz2 open for writing with bzip2 compression

  • w:xz open for writing with xz compression

  • r|* open a stream of cpio blocks with transparent compression

  • r| open an uncompressed stream of cpio blocks for reading

  • r|gz open a gzip compressed stream of cpio blocks

  • r|bz2 open a bzip2 compressed stream of cpio blocks

  • r|xz open a xz compressed stream of cpio blocks

  • w| open an uncompressed stream for writing

  • w|gz open a gzip compressed stream for writing

  • w|bz2 open a bzip2 compressed stream for writing

  • w|xz open a xz compressed stream for writing

proc_member(cpioinfo)[source]

Process a builtin type member or an unknown member which will be treated as a regular file.

utime(cpioinfo, targetpath)[source]

Set modification time of targetpath according to cpioinfo.

classmethod xzopen(name: str, mode: Literal['r', 'w'] = 'r', fileobj: IO[bytes] | None = None, compresslevel: int = 6) CpioFile[source]

Open xz compressed cpio archive name for reading or writing. Appending is not allowed.

class xcp.cpiofile.CpioInfo(name='')[source]

Bases: object

Informational class which holds the details about an archive member given by a cpio header block. CpioInfo objects are returned by CpioFile.getmember(), CpioFile.getmembers() and CpioFile.getcpioinfo() and are usually created internally.

classmethod frombuf(buf)[source]

Construct a CpioInfo object from a string buffer.

isblk()[source]
ischr()[source]
isdev()[source]
isdir()[source]
isfifo()[source]
isfile()[source]
islnk()[source]
isreg()[source]
issparse()[source]
issym()[source]
tobuf()[source]

Return a cpio header as bytes

xcp.cpiofile.is_cpiofile(name)[source]

Return True if name points to a cpio archive that we are able to handle, else return False.