NDEVR
API Documentation
TAR

Provides utilities for reading and extracting TAR and TAR.GZ archives. More...

Classes

struct  FileReader
 Reader implementation for uncompressed files using std::ifstream. More...
struct  GzReader
 Reader implementation for gzip-compressed files using zlib's gzFile interface. More...
struct  Reader
 Abstract base class for sequential byte stream reading. More...
struct  TarHdr
 Represents the raw 512-byte POSIX TAR header structure (USTAR format). More...

Static Public Member Functions

static bool all_zero (const u8 *p, size_t n)
 Checks whether all bytes in a buffer are zero.
static void apply_mode (const fs::path &p, u64 mode)
 Applies POSIX file permissions to the given path based on a TAR mode value.
static void apply_mtime (const fs::path &p, u64 mtime)
 Sets the last modification time of a file to the given Unix timestamp.
static int Extract (const File &src, const File &dest)
 Extracts all entries from a TAR or TAR.GZ archive to a destination directory.
static std::string hdr_name (const TarHdr &h)
 Extracts the full file name from a TAR header, combining prefix and name fields.
static u64 parse_octal (const char *p, size_t n)
 Parses an octal number from a TAR header field string.
static bool read_block (Reader &r, u8 *buf)
 Reads a single 512-byte block from the reader.
static std::string read_pax_string (const std::string &rec)
 Returns a PAX extended attribute record string as-is.
static bool skip_bytes (Reader &r, u64 n, std::vector< u8 > &tmp)
 Skips over a specified number of bytes in the reader stream.
static bool starts_with (const fs::path &a, const fs::path &base)
 Checks whether path a starts with the given base path using weakly canonical forms.
static u64 tar_checksum (const u8 *h)
 Computes the TAR checksum for a 512-byte header block.
static bool valid_hdr (const u8 *blk)
 Validates a 512-byte TAR header block by verifying its checksum.

Detailed Description

Provides utilities for reading and extracting TAR and TAR.GZ archives.


Definition at line 18 of file TarReader.h.

Member Function Documentation

◆ all_zero()

bool TAR::all_zero ( const u8 * p,
size_t n )
inlinestatic

Checks whether all bytes in a buffer are zero.

Parameters
[in]pPointer to the buffer.
[in]nNumber of bytes to check.
Returns
True if all bytes are zero, false otherwise.

Definition at line 138 of file TarReader.h.

Referenced by Extract().

◆ apply_mode()

void TAR::apply_mode ( const fs::path & p,
u64 mode )
inlinestatic

Applies POSIX file permissions to the given path based on a TAR mode value.

Parameters
[in]pThe filesystem path to apply permissions to.
[in]modeThe POSIX mode bits to apply.

Definition at line 240 of file TarReader.h.

Referenced by Extract().

◆ apply_mtime()

void TAR::apply_mtime ( const fs::path & p,
u64 mtime )
inlinestatic

Sets the last modification time of a file to the given Unix timestamp.

Parameters
[in]pThe filesystem path to update.
[in]mtimeThe modification time as seconds since Unix epoch.

Definition at line 260 of file TarReader.h.

Referenced by Extract().

◆ Extract()

int TAR::Extract ( const File & src,
const File & dest )
inlinestatic

Extracts all entries from a TAR or TAR.GZ archive to a destination directory.

Supports regular files, directories, symbolic links, hard links, PAX extended headers, and GNU long name/link extensions. Automatically detects gzip compression by inspecting the file signature.

Parameters
[in]srcThe source TAR or TAR.GZ archive file.
[in]destThe destination directory to extract into.
Returns
0 on success, or a negative error code on failure: -1 if the destination directory could not be created, -2 if the source file could not be opened, -3 if the reader failed to initialize, -4 if a block read failed, -5 if a header checksum was invalid, -6 through -16 for various extraction errors.

Definition at line 312 of file TarReader.h.

References all_zero(), apply_mode(), apply_mtime(), String::c_str(), TAR::FileReader::f, TAR::GzReader::g, hdr_name(), TAR::TarHdr::linkname, TAR::TarHdr::magic, TAR::TarHdr::mode, TAR::TarHdr::mtime, name, parse_octal(), read_block(), TAR::TarHdr::size, skip_bytes(), starts_with(), type, TAR::TarHdr::typeflag, and valid_hdr().

◆ hdr_name()

std::string TAR::hdr_name ( const TarHdr & h)
inlinestatic

Extracts the full file name from a TAR header, combining prefix and name fields.

Parameters
[in]hThe TAR header to extract the name from.
Returns
The full entry name as a string.

Definition at line 202 of file TarReader.h.

References TAR::TarHdr::name, and TAR::TarHdr::prefix.

Referenced by Extract().

◆ parse_octal()

u64 TAR::parse_octal ( const char * p,
size_t n )
inlinestatic

Parses an octal number from a TAR header field string.

Parameters
[in]pPointer to the octal string.
[in]nMaximum number of characters to parse.
Returns
The parsed unsigned 64-bit integer value.

Definition at line 149 of file TarReader.h.

Referenced by Extract(), and valid_hdr().

◆ read_block()

bool TAR::read_block ( Reader & r,
u8 * buf )
inlinestatic

Reads a single 512-byte block from the reader.

Parameters
[in]rThe reader to read from.
[in]bufBuffer to store the 512-byte block.
Returns
True if the block was successfully read, false otherwise.

Definition at line 273 of file TarReader.h.

References TAR::Reader::read_exact().

Referenced by Extract().

◆ read_pax_string()

std::string TAR::read_pax_string ( const std::string & rec)
inlinestatic

Returns a PAX extended attribute record string as-is.

Parameters
[in]recThe PAX record string.
Returns
The unmodified record string.

Definition at line 231 of file TarReader.h.

◆ skip_bytes()

bool TAR::skip_bytes ( Reader & r,
u64 n,
std::vector< u8 > & tmp )
inlinestatic

Skips over a specified number of bytes in the reader stream.

Parameters
[in]rThe reader to skip bytes from.
[in]nNumber of bytes to skip.
[in]tmpTemporary buffer used for reading and discarding data.
Returns
True if all bytes were successfully skipped, false otherwise.

Definition at line 284 of file TarReader.h.

References TAR::Reader::read_exact().

Referenced by Extract().

◆ starts_with()

bool TAR::starts_with ( const fs::path & a,
const fs::path & base )
inlinestatic

Checks whether path a starts with the given base path using weakly canonical forms.

Parameters
[in]aThe path to check.
[in]baseThe base path to compare against.
Returns
True if a begins with base, false otherwise.

Definition at line 122 of file TarReader.h.

Referenced by Extract().

◆ tar_checksum()

u64 TAR::tar_checksum ( const u8 * h)
inlinestatic

Computes the TAR checksum for a 512-byte header block.

Parameters
[in]hPointer to the 512-byte header block.
Returns
The computed checksum value.

Definition at line 165 of file TarReader.h.

Referenced by valid_hdr().

◆ valid_hdr()

bool TAR::valid_hdr ( const u8 * blk)
inlinestatic

Validates a 512-byte TAR header block by verifying its checksum.

Parameters
[in]blkPointer to the 512-byte header block.
Returns
True if the checksum is valid, false otherwise.

Definition at line 220 of file TarReader.h.

References TAR::TarHdr::chksum, parse_octal(), and tar_checksum().

Referenced by Extract().


The documentation for this class was generated from the following file: