Class: Omnizip::Formats::Cpio::Reader

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/cpio/reader.rb

Overview

CPIO archive reader

Reads CPIO archives in newc, CRC, ODC, and binary formats. Automatically detects format from magic number.

Examples:

Read CPIO archive

reader = Cpio::Reader.new('archive.cpio')
reader.open
reader.entries.each { |entry| puts entry.name }

Extract CPIO archive

reader = Cpio::Reader.new('archive.cpio')
reader.open
reader.extract_all('output/')

Constant Summary

Constants included from Constants

Constants::MAGIC_BINARY, Constants::MAGIC_CRC, Constants::MAGIC_NEWC, Constants::MAGIC_ODC, Constants::NEWC_ALIGNMENT, Constants::NEWC_HEADER_SIZE, Constants::S_IFBLK, Constants::S_IFCHR, Constants::S_IFDIR, Constants::S_IFIFO, Constants::S_IFLNK, Constants::S_IFMT, Constants::S_IFREG, Constants::S_IFSOCK, Constants::S_IRGRP, Constants::S_IROTH, Constants::S_IRUSR, Constants::S_IRWXG, Constants::S_IRWXO, Constants::S_IRWXU, Constants::S_ISGID, Constants::S_ISUID, Constants::S_ISVTX, Constants::S_IWGRP, Constants::S_IWOTH, Constants::S_IWUSR, Constants::S_IXGRP, Constants::S_IXOTH, Constants::S_IXUSR, Constants::TRAILER_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Reader

Initialize CPIO reader

Parameters:

  • file_path (String)

    Path to CPIO archive



37
38
39
40
41
# File 'lib/omnizip/formats/cpio/reader.rb', line 37

def initialize(file_path)
  @file_path = file_path
  @entries = []
  @format = nil
end

Instance Attribute Details

#entriesArray<Entry> (readonly)

Returns Parsed entries.

Returns:

  • (Array<Entry>)

    Parsed entries



29
30
31
# File 'lib/omnizip/formats/cpio/reader.rb', line 29

def entries
  @entries
end

#file_pathString (readonly)

Returns Archive file path.

Returns:

  • (String)

    Archive file path



26
27
28
# File 'lib/omnizip/formats/cpio/reader.rb', line 26

def file_path
  @file_path
end

#formatSymbol? (readonly)

Returns Detected format.

Returns:

  • (Symbol, nil)

    Detected format



32
33
34
# File 'lib/omnizip/formats/cpio/reader.rb', line 32

def format
  @format
end

Instance Method Details

#extract_all(output_dir) ⇒ Object

Extract all entries to directory

Parameters:

  • output_dir (String)

    Output directory



75
76
77
78
79
80
81
82
83
84
# File 'lib/omnizip/formats/cpio/reader.rb', line 75

def extract_all(output_dir)
  FileUtils.mkdir_p(output_dir)

  @entries.each do |entry|
    next if entry.trailer?

    output_path = File.join(output_dir, entry.name)
    extract_single_entry(entry, output_path)
  end
end

#extract_entry(entry_name, output_path) ⇒ Object

Extract entry to output path

Parameters:

  • entry_name (String)

    Entry name to extract

  • output_path (String)

    Destination path

Raises:

  • (RuntimeError)

    if entry not found



65
66
67
68
69
70
# File 'lib/omnizip/formats/cpio/reader.rb', line 65

def extract_entry(entry_name, output_path)
  entry = @entries.find { |e| e.name == entry_name }
  raise "Entry not found: #{entry_name}" unless entry

  extract_single_entry(entry, output_path)
end

#format_nameString

Get archive format

Returns:

  • (String)

    Human-readable format name



89
90
91
92
93
94
95
96
97
# File 'lib/omnizip/formats/cpio/reader.rb', line 89

def format_name
  case @format
  when :newc then "CPIO newc (SVR4)"
  when :crc then "CPIO newc with CRC"
  when :odc then "CPIO ODC (portable)"
  when :binary then "CPIO binary (old)"
  else "Unknown"
  end
end

#listArray<Entry>

List all entries

Returns:

  • (Array<Entry>)

    All entries except trailer



56
57
58
# File 'lib/omnizip/formats/cpio/reader.rb', line 56

def list
  @entries.reject(&:trailer?)
end

#openObject

Open and parse CPIO archive

Raises:

  • (RuntimeError)

    if file cannot be opened or parsed



46
47
48
49
50
51
# File 'lib/omnizip/formats/cpio/reader.rb', line 46

def open
  File.open(@file_path, "rb") do |io|
    parse_archive(io)
  end
  self
end