Class: CPIO::ArchiveHeader
- Inherits:
-
Object
- Object
- CPIO::ArchiveHeader
- Defined in:
- lib/excavate/extractors/cpio/cpio_old_format.rb
Constant Summary collapse
- Magic =
'070707'- Fields =
[[6, :magic ], [6, :dev ], [6, :inode ], [6, :mode ], [6, :uid ], [6, :gid ], [6, :numlinks], [6, :rdev ], [11, :mtime ], [6, :namesize], [11, :filesize]]
- FieldDefaults =
{:magic => Integer(Magic), :dev => 0777777, :inode => 0, :mode => 0100444, :uid => 0, :gid => 0, :numlinks => 1, :rdev => 0, :mtime => lambda { Time.now.to_i }}
- FieldMaxValues =
Fields.inject({}) do |map,(width,name)| map[name] = Integer("0#{'7' * width}") map end
- HeaderSize =
Fields.inject(0) do |sum,(size,name)| sum + size end
- HeaderUnpackFormat =
Fields.collect do |size,name| "a%s" % size end.join('')
Class Method Summary collapse
- .from(io) ⇒ Object
- .unpack_data(data) ⇒ Object
- .verify_magic(data) ⇒ Object
- .verify_size(data) ⇒ Object
- .with_defaults(opts) ⇒ Object
Instance Method Summary collapse
-
#initialize(attrs) ⇒ ArchiveHeader
constructor
A new instance of ArchiveHeader.
- #to_data ⇒ Object
Constructor Details
#initialize(attrs) ⇒ ArchiveHeader
Returns a new instance of ArchiveHeader.
54 55 56 57 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 54 def initialize(attrs) @attrs = attrs check_attrs end |
Class Method Details
.from(io) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 59 def self.from(io) data = io.read(HeaderSize) verify_size(data) verify_magic(data) new(unpack_data(data)) end |
.unpack_data(data) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 101 def self.unpack_data(data) contents = {} data.unpack(HeaderUnpackFormat).zip(Fields) do |(chunk,(size,name))| contents[name] = Integer("0#{chunk}") end contents end |
.verify_magic(data) ⇒ Object
95 96 97 98 99 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 95 def self.verify_magic(data) unless data[0..Magic.size - 1] == Magic raise ArchiveFormatError, "Archive does not seem to be a valid CPIO archive with ASCII headers." end end |
.verify_size(data) ⇒ Object
89 90 91 92 93 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 89 def self.verify_size(data) unless data.size == HeaderSize raise ArchiveFormatError, "Header is not long enough to be a valid CPIO archive with ASCII headers." end end |
.with_defaults(opts) ⇒ Object
66 67 68 69 70 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 66 def self.with_defaults(opts) name = opts[:name] defaults = FieldDefaults.merge(:mode => opts[:mode], :filesize => opts[:filesize], :namesize => name.size + 1) new(defaults) end |
Instance Method Details
#to_data ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/excavate/extractors/cpio/cpio_old_format.rb', line 72 def to_data Fields.collect do |(width,name)| raise ArchiveFormatError, "Expected header to have key #{name}" unless @attrs.has_key?(name) val = @attrs[name].respond_to?(:to_proc) ? @attrs[name].call : @attrs[name] raise ArchiveFormatError, "Header value for #{name} exceeds max length of #{FieldMaxValues[name]}" if val > FieldMaxValues[name] sprintf("%0*o", Fields.rassoc(name).first, val) end.join('') end |