Cross package maker. DEB/RPM generation or conversion. Derived from jordansissel/fpm.

⌈⌋ ⎇ branch:  cross package maker


Check-in [9d18c8d302]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:- moved to lib/rpm/...
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9d18c8d302e2fc41d681377875c5ad06de146257
User & Date: jls@semicomplete.com 2011-05-14 07:50:16
Context
2011-05-16
07:18
- default version comparison should be >=0, as '>0' is deprecated. https://github.com/jordansissel/fpm/issues/33 check-in: 4466f209ec user: jls@semicomplete.com tags: trunk
2011-05-14
07:50
- moved to lib/rpm/... check-in: 9d18c8d302 user: jls@semicomplete.com tags: trunk
07:49
- factor out the rpm bits into separate class files - include all known rpm 'tag' types check-in: 35f668b84f user: jls@semicomplete.com tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Deleted experimental/LEARN.

1
http://www.rpm.org/max-rpm/s1-rpm-file-format-rpm-file-format.html
<


Deleted experimental/rpmlib.rb.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
require "ap"

# Much of the code here is derived from knowledge gained by reading the rpm
# source code and this site:
# http://www.rpm.org/max-rpm/s1-rpm-file-format-rpm-file-format.html

class RPMFile 
  attr_reader :file

  def initialize(file)
    if file.is_a?(String)
      file = File.new(file, "r")
    end
    @file = file

    # Make sure we're at the beginning of the file.
    @file.seek(0, IO::SEEK_SET)
  end # def initialize

  class Lead
    #struct rpmlead {
    attr_accessor :magic #unsigned char magic[4]; 
    attr_accessor :major #unsigned char major;
    attr_accessor :minor #unsigned char minor;
    attr_accessor :type  #short type;
    attr_accessor :archnum #short archnum;
    attr_accessor :name #char name[66];
    attr_accessor :osnum #short osnum;
    attr_accessor :signature_type #short signature_type;
    attr_accessor :reserved #char reserved[16];
    #}
    #
    attr_accessor :length

    def type
      case @type
      when 0
        return :binary
      when 1
        return :source
      else
        raise "Unknown package 'type' value #{@type}"
      end
    end # def type
    
    def read(file)
      # Use 'A' here instead of 'a' to trim nulls.
      @length = 96
      data = file.read(@length).unpack("A4CCnnA66nnA16")
      @magic, @major, @minor, @type, @archnum, @name, \
        @osnum, @signature_type, @reserved = data

      return nil
    end # def read

    def write(file)
      data = [ @magic, @major, @minor, @type, @archnum, @name, \
               @osnum, @signature_type, @reserved ].pack("a4CCnnA66nna16")
      file.write(data)
    end # def write
  end # class ::RPMFile::Lead

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

    attr_accessor :magic  # 8-byte string magic
    attr_accessor :index_length  # rpmlib calls this field 'il' unhelpfully
    attr_accessor :data_length   # rpmlib calls this field 'dl' unhelpfully

    attr_accessor :length

    def read(file)
      # TODO(sissel): in RPM version major (@lead.major) < 3, there is no
      # header magic? RPM's code seems to confirm.

      # Signature reads 16 bytes (4 x int32)
      @length = 16
      data = file.read(@length).unpack("a8NN")
      @magic, @index_length, @data_length = data
      validate
      # data[0..1] is compared against HEADER_MAGIC
      # data[2] must be between [0, 32] inclusive
      # data[3] must be between [0, 8192] inclusive
    end # def raed

    def write(file)
    end # def write

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

      if !(0..32).include?(@index_length)
        raise "Invalid 'index_length' value #{@index_length}, 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 # def validate
  end # class ::RPMFile::Signature

  class Header
    attr_reader :tags
    attr_reader :length

    attr_accessor :magic  # 8-byte string magic
    attr_accessor :index_length  # rpmlib calls this field 'il' unhelpfully
    attr_accessor :data_length   # rpmlib calls this field 'dl' unhelpfully

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

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

    def read
      # 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_length(2bytes) +
      # data_length(2bytes)
      @length = 16
      data = @rpm.file.read(@length).unpack("a8NN")
      # TODO(sissel): @index_length is really a count, rename?
      @magic, @index_length, @data_length = data
      validate

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

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

        # TODO(sissel): This section is pretty F'd up.
        # Maybe I should just support RPM 3 (centos5 and fedora14 do it)
        if i == 0  # first tag
          # First tag must be <100 (if not, it is an 'old' package and
          # therefore we fabricate a 'region' tag that goes in first.
          if tag.tag_as_int >= 100 
            # I don't really feel like supporting this. If you need it, please let me know.
            raise "Unsupported RPM (Legacy RPM?)"
            # 7 == :binary (region tag type), 61 = :header_image
            #legacy_tag = Tag.new(7, 61, entry_size, 0 - @rpm.signature.data_length, data)
            #@tags.unshift(legacy_tag)
          end

          rdl = ril = nil
          if tag.offset > 0
            rdl = -(data[offset,1].unpack("C")).first
            ril = rdl / entry_size
          else
            ril = @rpm.signature.index_length
            rdl = ril * entry_size
            tag.tag = 61 # :header_image
          end
          tag.offset = -rdl
        end # first tag handling

        #ap tag.tag => {
          #:type => tag.type, 
          #:offset => tag.offset,
          #:count => tag.count,
          #:value => tag.value,
        #}
      end # each index
      #@length = (@rpm.signature.index_length * entry_size) + @rpm.signature.data_length
    end # def read

    def write
    end # def write

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

      if !(0..32).include?(@index_length)
        raise "Invalid 'index_length' value #{@index_length}, 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 # def validate
  end # class ::RPMFile::Header

  class Tag
    attr_accessor :tag
    attr_accessor :type
    attr_accessor :offset
    attr_accessor :count

    # This data can be found mostly in rpmtag.h
    TAG = {
      61 => :header_image,
      62 => :signature,
      267 => :dsa,
      268 => :rsa,
      269 => :sha1,
      1000 => :name,
      1002 => :release,
      1004 => :summary,
      1005 => :description,
      1007 => :buildhost,
      1124 => :payload_format,
      1125 => :payload_compressor,
    }

    # See 'rpmTagType' enum in rpmtag.h
    TYPE = {
      0 => :null,
      1 => :char,
      2 => :int8,
      3 => :int16,
      4 => :int32,
      5 => :int64,
      6 => :string,
      7 => :binary,
      8 => :string_array,
      9 => :i18nstring,
    }

    def initialize(tag_id, type, offset, count, data)
      @tag = tag_id
      @type = type
      @offset = offset
      @count = count

      @data = data
    end # def initialize

    def tag
      Tag::TAG[@tag] or @tag
    end # def tag

    def tag_as_int
      @tag
    end

    def type
      Tag::TYPE[@type] or @type
    end # def type

    def value
      case type
        when :string
          return @data[@offset .. -1].gsub(/\0.*/, "")
        when :binary
          return @data[@offset, @count]
        when :int32
          return @data[@offset, 4].unpack("N")
        when :int16
          return @data[@offset, 2].unpack("n")
      end
    end # def value
  end # class Tag

  public
  def lead
    if @lead.nil?
      @lead = ::RPMFile::Lead.new
      @lead.read(@file)
    end
    return @lead
  end # def lead

  public
  def signature
    lead

    # If signature_type is not 5 (HEADER_SIGNED_TYPE), no signature.
    if @lead.signature_type != Signature::HEADER_SIGNED_TYPE
      @signature = false
      return
    end

    if @signature.nil?
      @signature = ::RPMFile::Signature.new
      @signature.read(@file)
    end

    return @signature
  end # def signature

  public
  def header
    #signature
    lead
    if @header.nil?
      @header = ::RPMFile::Header.new(self)
      @header.read
    end
    return @header
    # http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch15s03s03.html
  end

  # Returns a file descriptor. On first invocation, it seeks to the start of the payload
  public
  def payload
    header
    if @payload.nil?
      @payload = @file.clone
      @payload.seek(@lead.length + @signature.length + @header.length, IO::SEEK_SET)
    end

    return @payload
  end # def payload
end # class RPMFile
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<