Class: RPMFile::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/rpm/header.rb

Constant Summary collapse

HEADER_SIGNED_TYPE =
5
HEADER_MAGIC =
"\x8e\xad\xe8\x01\x00\x00\x00\x00"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpm) ⇒ Header

Returns a new instance of Header.



15
16
17
18
# File 'lib/rpm/header.rb', line 15

def initialize(rpm)
  @rpm = rpm
  @tags = []
end

Instance Attribute Details

#data_lengthObject

rpmlib calls this field ‘dl’ unhelpfully



10
11
12
# File 'lib/rpm/header.rb', line 10

def data_length
  @data_length
end

#index_countObject

rpmlib calls this field ‘il’ unhelpfully



9
10
11
# File 'lib/rpm/header.rb', line 9

def index_count
  @index_count
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/rpm/header.rb', line 6

def length
  @length
end

#magicObject

8-byte string magic



8
9
10
# File 'lib/rpm/header.rb', line 8

def magic
  @magic
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/rpm/header.rb', line 5

def tags
  @tags
end

Instance Method Details

#readObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rpm/header.rb', line 20

def read
  # TODO(sissel): update the comments here to reflect learnings about rpm
  # internals
  # At this point assume we've read and consumed the lead and signature.
  #len = @rpm.signature.index_length + @rpm.signature
  #
  # header size is
  #     ( @rpm.signature.index_length * size of a header entry )
  #     + @rpm.signature.data_length
  #
  # header 'entries' are an
  #   int32 (tag id), int32 (tag type), int32  (offset), uint32 (count)
  #
  # See rpm's header.c, the headerLoad method function for reference.

  # Header always starts with HEADER_MAGIC + index_count(2bytes) +
  # data_length(2bytes)
  data = @rpm.file.read(16).unpack("a8NN")
  # TODO(sissel): @index_count is really a count, rename?
  @magic, @index_count, @data_length = data
  validate

  entry_size = 16 # tag id, type, offset, count == 16 bytes
  @index_size = @index_count * entry_size
  tag_data = @rpm.file.read(@index_size)
  data = @rpm.file.read(@data_length)

  #ap :data => data

  (0 ... @index_count).each do |i|
    offset = i * entry_size
    entry_data = tag_data[i * entry_size, entry_size]
    entry = entry_data.unpack("NNNN")
    entry << data
    tag = ::RPMFile::Tag.new(*entry)
    @tags << tag

    #ap tag.tag => {
      #:type => tag.type,
      #:offset => tag.offset,
      #:count => tag.count,
      #:value => (tag.value rescue "???"),
    #}
  end # each index
  @length = @magic.size + @index_size + @data_length
end

#validateObject

def write



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rpm/header.rb', line 74

def validate
  # TODO(sissel): raise real exceptions
  if @magic != ::RPMFile::Header::HEADER_MAGIC
    raise "Header magic did not match; got #{@magic.inspect}, " \
          "expected #{::RPMFile::Header::HEADER_MAGIC.inspect}"
  end

  #if !(0..32).include?(@index_count)
    #raise "Invalid 'index_count' value #{@index_count}, expected to be in range [0..32]"
  #end

  #if !(0..8192).include?(@data_length)
    #raise "Invalid 'data_length' value #{@data_length}, expected to be in range [0..8192]"
  #end
end

#writeObject

def read



67
68
69
70
71
72
# File 'lib/rpm/header.rb', line 67

def write
  raise "not implemented yet"
  # Sort tags by type (integer value)
  # emit all tags in order
  # then emit all data segments in same order
end