Pkg.CPIO — API for parsing CPIO archives¶
Overview¶
The Pkg.CPIO module contains the API for parsing CPIO archives.
CPIO is a stream-based Unix archive format that concatenates per-file headers followed by file data. The module supports all common header variants: old binary (little- and big-endian, magic 0o070707), POSIX old ASCII / odc (magic "070707"), SVR4 ASCII / newc (magic "070701"), and SVR4 ASCII with CRC (magic "070702"). Every variant terminates the archive with an entry whose name is "TRAILER!!!". CPIO is commonly produced by find | cpio -o and is the on-disk format used by Linux initramfs images.
Enumerating CPIO Entries¶
The following code example demonstrates how to enumerate entries in a CPIO archive:
from Pro.Core import *
from Pkg.CPIO import *
def enumerateEntries(fname):
c = createContainerFromFile(fname)
if c.isNull():
return
obj = CPIOObject()
if not obj.Load(c) or not obj.Initialize():
return
print("Variant:", obj.GetVariant())
offs = None
while True:
e, offs = obj.NextEntry(offs)
if e is None:
break
print(e.name, e.data_size, oct(e.mode))
Module API¶
Pkg.CPIO module API.
Classes:
This class represents a single entry in a CPIO archive.
This class represents a CPIO archive.
- class CPIOEntry¶
This class represents a single entry in a CPIO archive.
Instances are produced by
CPIOObject.NextEntry()andCPIOObject.GetEntry(). Fields reflect the values stored in the on-disk header after decoding; binary, octal, and hexadecimal field encodings are all normalised to plain Python integers.Attributes:
The header checksum (only set for the
"crc"variant; always zero otherwise).The byte offset of the file data in the archive stream.
The size of the file data in bytes.
The major number of the device containing the file.
The minor number of the device containing the file.
The group id.
The byte offset of the header in the archive stream.
The size of the header in bytes.
The inode number.
The file mode, including the file-type bits in the upper bits.
The modification time, as a Unix epoch timestamp.
The file name, decoded as UTF-8 with replacement.
The byte offset of the file name in the archive stream.
The size of the file name in bytes, including the trailing NUL.
The byte offset of the next header in the archive stream.
The number of hard links.
The major number of the device the file refers to (for device nodes).
The minor number of the device the file refers to (for device nodes).
The user id.
The header variant:
"bin","bin_be","odc","newc", or"crc".
- check: int¶
The header checksum (only set for the
"crc"variant; always zero otherwise).
- data_offset: int¶
The byte offset of the file data in the archive stream.
- data_size: int¶
The size of the file data in bytes.
- dev_major: int¶
The major number of the device containing the file.
- dev_minor: int¶
The minor number of the device containing the file.
- gid: int¶
The group id.
- header_offset: int¶
The byte offset of the header in the archive stream.
- header_size: int¶
The size of the header in bytes.
- ino: int¶
The inode number.
- mode: int¶
The file mode, including the file-type bits in the upper bits.
- mtime: int¶
The modification time, as a Unix epoch timestamp.
- name: str¶
The file name, decoded as UTF-8 with replacement.
- name_offset: int¶
The byte offset of the file name in the archive stream.
- namesize: int¶
The size of the file name in bytes, including the trailing NUL.
- next_offset: int¶
The byte offset of the next header in the archive stream.
- nlink: int¶
The number of hard links.
- rdev_major: int¶
The major number of the device the file refers to (for device nodes).
- rdev_minor: int¶
The minor number of the device the file refers to (for device nodes).
- uid: int¶
The user id.
- variant: str¶
The header variant:
"bin","bin_be","odc","newc", or"crc".
- class CPIOObject¶
Bases:
Pro.Core.CFFObjectThis class represents a CPIO archive.
The CPIO archive format is a stream-based Unix archive that concatenates per-file headers followed by file data. CPIO comes in several header variants: old binary (magic
0o070707stored as 2 bytes), old POSIX ASCII / odc (magic"070707", 76-byte header), SVR4 ASCII / newc (magic"070701", 110-byte header), and SVR4 ASCII with CRC (magic"070702"). Every variant terminates the archive with an entry whose name is"TRAILER!!!". CPIO is commonly produced byfind | cpio -oand used as the on-disk format for Linux initramfs images.Methods:
DumpDefects(out)Dumps a human-readable description of the recorded defects to a text stream.
DumpEntry(entry, out)Dumps information about an entry to a text stream.
Returns the offset of the byte after the trailer entry (including any trailing zero padding to the next 512-byte block).
GetEntry(offset)Retrieves an entry by its offset.
GetEntryData(entry)Retrieves the data of an entry.
Returns the header variant detected in the archive.
Returns whether any defects were recorded while parsing the archive.
IsDirectory(entry)Checks whether an entry is a directory.
NextEntry([curoffs])Iterates over the entries.
Parses the archive.
- DumpDefects(out: Pro.Core.NTTextStream) → None¶
Dumps a human-readable description of the recorded defects to a text stream.
- Parameters
out (NTTextStream) – The output text stream.
See also
HasDefects().
- DumpEntry(entry: Any, out: Pro.Core.NTTextStream) → None¶
Dumps information about an entry to a text stream.
- Parameters
entry (Any) – The entry.
out (NTTextStream) – The output text stream.
- GetEndOffset() → int¶
Returns the offset of the byte after the trailer entry (including any trailing zero padding to the next 512-byte block).
- Returns
Returns the offset of the byte just past the end of the archive.
- Return type
int
- GetEntry(offset: int) → Any¶
Retrieves an entry by its offset.
- Parameters
offset (int) – The entry offset.
- Returns
Returns the entry if successful; otherwise returns
None.See also
NextEntry().
- GetEntryData(entry: Any) → Pro.Core.NTContainer¶
Retrieves the data of an entry.
- Parameters
entry (Any) – The entry.
- Returns
Returns the data if successful; otherwise returns an invalid container.
- Return type
See also
NextEntry()andGetEntry().
- GetVariant() → str¶
Returns the header variant detected in the archive.
- Returns
One of
"bin"(old binary, little-endian),"bin_be"(old binary, big-endian),"odc"(POSIX old ASCII),"newc"(SVR4 ASCII, no CRC), or"crc"(SVR4 ASCII with CRC).- Return type
str
- HasDefects() → bool¶
Returns whether any defects were recorded while parsing the archive.
A defect is recorded when the archive is malformed in some way that did not prevent the parser from extracting at least the minimum amount of structural evidence required to recognise it as CPIO: most commonly a missing
"TRAILER!!!"entry (the archive is truncated), a header whose magic does not match any known variant in the middle of the stream, or size fields that fail to advance the stream cursor.
- Returns
Returns
Trueif at least one defect was recorded; otherwise returnsFalse.- Return type
bool
See also
DumpDefects().
- IsDirectory(entry: Any) → bool¶
Checks whether an entry is a directory.
- Parameters
entry (Any) – The entry.
- Returns
Returns
Trueif the entry is a directory; otherwise returnsFalse.- Return type
bool
See also
NextEntry()andGetEntry().
- NextEntry(curoffs: Optional[int] = None) → Tuple[Any, int]¶
Iterates over the entries.
- Parameters
curoffs (Optional[int]) – The current offset.
- Returns
Returns a tuple containing the next entry and its offset if successful; otherwise returns a tuple containing two
Nonevalues.- Return type
Tuple[Any, int]
See also
GetEntry().
- ParseArchive() → bool¶
Parses the archive.
- Returns
Returns
Trueif successful; otherwise returnsFalse.- Return type
bool