Class: RPM::Package

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Package) initialize(hdr = nil)

Returns a new instance of Package



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/rpm/package.rb', line 322

def initialize(hdr=nil)
  if hdr.nil?
    @hdr = ::FFI::AutoPointer.new(RPM::FFI.headerNew, Header.method(:release))
  elsif hdr.is_a?(::FFI::Pointer)
    # ref
    hdr = RPM::FFI.headerLink(hdr)
    @hdr = ::FFI::AutoPointer.new(hdr, Package.method(:release))
  else
    raise "Can't initialize header with '#{hdr}'"
  end
end

Class Method Details

+ (Object) create(name, version)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rpm/package.rb', line 19

def self.create(name, version)
  if not name.is_a?(String)
    raise TypeError, "illegal argument type: name should be String"
  end
  if not version.is_a?(RPM::Version)
    raise TypeError, "illegal argument type: version should be RPM::Version"
  end
  hdr = RPM::FFI.headerNew
  if RPM::FFI.headerPutString(hdr, :name, name) != 1
    raise "Can't set package name: #{name}"
  end
  if RPM::FFI.headerPutString(hdr, :version, version.v) != 1
    raise "Can't set package version: #{version.v}"
  end
  if version.e
    if RPM::FFI.headerPutUint32(hdr, :epoch, version.e) != 1
      raise "Can't set package epoch: #{version.e}"
    end
  end
  Package.new(hdr)
end

+ (Object) open(filename)



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rpm/package.rb', line 293

def self.open(filename)
  # it sucks not using the std File.open here
  hdr = ::FFI::MemoryPointer.new(:pointer)
  fd = nil
  begin
    fd = RPM::FFI.Fopen(filename, 'r')
    if RPM::FFI.Ferror(fd) != 0
      raise "#{filename} : #{RPM::FFI.Fstrerror(fd)}"
    end
    RPM.transaction do |ts|
      rc = RPM::FFI.rpmReadPackageFile(ts.ptr, fd, filename, hdr)
    end
  ensure
    RPM::FFI.Fclose(fd) unless fd.nil?
  end
  Package.new(hdr.get_pointer(0))
end

+ (Object) release(ptr)



312
313
314
# File 'lib/rpm/package.rb', line 312

def self.release(ptr)
  RPM::FFI.headerFree(ptr)
end

+ (Object) release_td(ptr)



317
318
319
# File 'lib/rpm/package.rb', line 317

def self.release_td(ptr)
  RPM::FFI.rpmtdFree(ptr)
end

Instance Method Details

- (Object) [](tag)

Access a header entry

or if you have the old ruby-rpm compat loaded

  require 'rpm/compat'
  pkg[RPM::TAG_NAME] => "xmlgraphics-fop"

Examples:

pkg => #<RPM::Package name="xmlgraphics-fop", version=#<RPM::Version v="1.0", r="22.4">>
pkg[:name] => "xmlgraphics-fop"

Parameters:

  • tag (Number)

    Tag to return

Returns:

  • Value of the entry

  • (String, Fixnum, Array<String>, Array<Fixnum>, nil)

    The value of the entry



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
# File 'lib/rpm/package.rb', line 223

def [](tag)
  val = nil
  tagc = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))

  return nil if (RPM::FFI.headerGet(ptr, tag, tagc, 
                  RPM::FFI::HEADERGET_MINMEM) == 0)

  type = RPM::FFI.rpmtdType(tagc)
  count = RPM::FFI.rpmtdCount(tagc)
  ret_type = RPM::FFI.rpmTagGetReturnType(tag)

  method_name = case type
    when :int8_type, :char_type, :int16_type, :int32_type, :int64_type then :rpmtdGetNumber
    when :string_type, :string_array_type, :bin_type then :rpmtdGetString
    else raise NotImplementedError, "Don't know how to retrieve type '#{type}'"
  end

  is_array = case
    when count > 1 then true
    when ret_type == :array_return_type then true
    when type == :string_array_type then true
    else false
  end

  if is_array
    ret = []
    RPM::FFI.rpmtdInit(tagc)
    while RPM::FFI.rpmtdNext(tagc) != -1
      ret << RPM::FFI.send(method_name, tagc)
    end
    return ret
  end
  
  return RPM::FFI.send(method_name, tagc)
end

- (Object) add_binary(tag, val)

Add a binary value to the package header

Parameters:

  • tag (Number)

    Tag

  • val (String)

    Value

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/rpm/package.rb', line 75

def add_binary(tag, val)
  raise NotImplementedError
end

- (Object) add_dependency(dep)

Add a dependency to the package header

Parameters:

Raises:

  • (NotImplementedError)


43
44
45
46
47
48
49
# File 'lib/rpm/package.rb', line 43

def add_dependency(dep)
  unless dep.is_a?(Dependency)
    raise TypeError.new("illegal argument type: must be a Dependency")
  end

  raise NotImplementedError
end

- (Object) add_int32(tag, val)

Add a int32 value to the package header

Parameters:

  • tag (Number)

    Tag

  • val (Number)

    Value

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/rpm/package.rb', line 54

def add_int32(tag, val)
  raise NotImplementedError
end

- (Object) add_string(tag, val)

Add a binary value to the package header

Parameters:

  • tag (Number)

    Tag

  • val (String)

    String to add

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/rpm/package.rb', line 68

def add_string(tag, val)
  raise NotImplementedError
end

- (Object) add_string_array(tag, val)

Add a list of strings to the package header

Parameters:

  • tag (Number)

    Tag

  • val (Array<String>)

    Strings to add

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/rpm/package.rb', line 61

def add_string_array(tag, val)
  raise NotImplementedError
end

- (String) arch

Returns This package architecture

Returns:

  • (String)

    This package architecture



265
266
267
# File 'lib/rpm/package.rb', line 265

def arch
  self[:arch]
end

- (Array<RPM::Changelog>) changelog

Returns changelog of the package as an array

Returns:

  • (Array<RPM::Changelog>)

    changelog of the package as an array



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rpm/package.rb', line 187

def changelog
  entries = []
  nametd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))
  timetd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))
  texttd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))

  min = RPM::FFI::HEADERGET_MINMEM
  return deps if (RPM::FFI.headerGet(@hdr, :changelogtime, timetd, min) != 1)
  return deps if (RPM::FFI.headerGet(@hdr, :changelogname, nametd, min) != 1)
  return deps if (RPM::FFI.headerGet(@hdr, :changelogtext, texttd, min) != 1)

  RPM::FFI.rpmtdInit(timetd)
  while RPM::FFI.rpmtdNext(timetd) != -1
    entry = RPM::ChangeLog.new
    entry.time = RPM::FFI.rpmtdGetUint32(timetd)
    entry.name = RPM::FFI.rpmtdNextString(nametd)
    entry.text = RPM::FFI.rpmtdNextString(texttd)
    entries << entry
  end
  entries
end

- (Array<RPM::Conflicts>) conflicts

Returns Conflicts list for this package

Returns:

  • (Array<RPM::Conflicts>)

    Conflicts list for this package



177
178
179
# File 'lib/rpm/package.rb', line 177

def conflicts
  dependencies(RPM::conflicts, :conflictsname, :conflictsversion, :conflictsflags)
end

- (Object) delete_tag(tag)

Deletes a tag of the package header

Parameters:

  • tag (Number)

    Tag

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/rpm/package.rb', line 81

def delete_tag(tag)
  raise NotImplementedError
end

- (Array<RPM::Dependency>) dependencies(klass, nametag, versiontag, flagtag)

Returns Dependencies for klass

Examples:

dependencies(RPM::Provide, :providename, :provideversion, :provideflags)

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rpm/package.rb', line 145

def dependencies(klass, nametag, versiontag, flagtag)
  deps = []

  nametd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))
  versiontd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))
  flagtd = ::FFI::AutoPointer.new(RPM::FFI.rpmtdNew, Package.method(:release_td))

  min = RPM::FFI::HEADERGET_MINMEM
  return deps if (RPM::FFI.headerGet(@hdr, nametag, nametd, min) != 1)
  return deps if (RPM::FFI.headerGet(@hdr, versiontag, versiontd, min) != 1)
  return deps if (RPM::FFI.headerGet(@hdr, flagtag, flagtd, min) != 1)

  RPM::FFI.rpmtdInit(nametd)
  while RPM::FFI.rpmtdNext(nametd) != -1
    deps << klass.new(RPM::FFI.rpmtdGetString(nametd),
              RPM::Version.new(RPM::FFI.rpmtdNextString(versiontd)),
              RPM::FFI.rpmtdNextUint32(flagtd).read_uint, self)
  end
  deps
end

- (Array<RPM::File>) files

Returns File list for this package

Returns:

  • (Array<RPM::File>)

    File list for this package



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
# File 'lib/rpm/package.rb', line 101

def files
  basenames = self[:basenames]

  return [] if basenames.nil?

  dirnames = self[:dirnames]
  diridxs = self[:dirindexes]
  statelist = self[:filestates]
  flaglist = self[:fileflags]
  sizelist = self[:filesizes]
  modelist = self[:filemodes]
  mtimelist = self[:filemtimes]
  rdevlist = self[:filerdevs]
  linklist = self[:filelinktos]
  md5list = self[:filemd5s]
  ownerlist = self[:fileusername]
  grouplist = self[:filegroupname]

  ret = []

  basenames.each_with_index do |basename, i|

    file = RPM::File.new("#{dirnames[diridxs[i]]}#{basenames[i]}",
            md5list[i],
            linklist[i],
            sizelist[i],
            mtimelist[i],
            ownerlist[i],
            grouplist[i],
            rdevlist[i],
            modelist[i],
            flaglist.nil? ? RPM::FFI::FileAttrs[:none] : flaglist[i],
            statelist.nil? ? RPM::FFI::FileState[:normal] : statelist[i]
    )
    ret << file
  end
  ret
end

- (Package) load(data)

Create a new package object from data

Parameters:

  • str (String)

    Header data

Returns:

Raises:

  • (NotImplementedError)


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

def load(data)
  raise NotImplementedError
end

- (String) name

Returns This package name

Returns:

  • (String)

    This package name



260
261
262
# File 'lib/rpm/package.rb', line 260

def name
  self[:name]
end

- (Array<RPM::Obsolete>) obsoletes

Returns Obsoletes list for this package

Returns:

  • (Array<RPM::Obsolete>)

    Obsoletes list for this package



182
183
184
# File 'lib/rpm/package.rb', line 182

def obsoletes
  dependencies(RPM::Obsoletes, :obsoletename, :obsoleteversion, :obsoleteflags)
end

- (Array<RPM::Provide>) provides

Returns Provides list for this package

Returns:



167
168
169
# File 'lib/rpm/package.rb', line 167

def provides
  dependencies(RPM::Provide, :providename, :provideversion, :provideflags)
end

- (RPM::FFI::Header) ptr

Returns header pointer

Returns:

  • (RPM::FFI::Header)

    header pointer



336
337
338
# File 'lib/rpm/package.rb', line 336

def ptr
  @hdr
end

- (Array<RPM::Require>) requires

Returns Requires list for this package

Returns:



172
173
174
# File 'lib/rpm/package.rb', line 172

def requires
  dependencies(RPM::Require, :requirename, :requireversion, :requireflags)
end

- (Number) signature

Returns This package signature

Returns:

  • (Number)

    This package signature



96
97
98
# File 'lib/rpm/package.rb', line 96

def signature
  sprintf('%{sigmd5}')
end

- (Object) sprintf(fmt)

Returns a formated string

Examples:

pkg.sprintf("%{name}") => "apache2"

Returns:

  • a formated string



88
89
90
91
92
93
# File 'lib/rpm/package.rb', line 88

def sprintf(fmt)
  error = ::FFI::MemoryPointer.new(:pointer, 1)
  val = RPM::FFI.headerFormat(@hdr, fmt, error)
  raise error.get_pointer(0).read_string if val.null?
  val.read_string
end

- (String) to_s

String representation of the package: "name-version-release-arch"

Returns:

  • (String)


286
287
288
289
290
291
# File 'lib/rpm/package.rb', line 286

def to_s
  return "" if name.nil?
  return name if version.nil?
  return "#{name}-#{version}" if arch.nil?
  return "#{name}-#{version}-#{arch}"
end

- (Version) version

Returns Version for this package

Returns:

  • (Version)

    Version for this package



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rpm/package.rb', line 272

def version
  v_ptr = ::FFI::MemoryPointer.new(:pointer, 1)
  r_ptr = ::FFI::MemoryPointer.new(:pointer, 1)

  RPM::FFI.headerNVR(ptr, nil, v_ptr, r_ptr)
  v = v_ptr.read_pointer.read_string
  r = r_ptr.read_pointer.read_string
  v_ptr.free
  r_ptr.free
  Version.new(v, r, self[:epoch])
end