Class: CPIO::ArchiveHeader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.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_dataObject



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