7 use File::Basename qw/basename dirname/;
10 use vars qw/$VERSION/;
13 @ISA = qw/DynaLoader/;
15 bootstrap RPM2 $VERSION;
17 foreach my $tag (keys %RPM2::constants) {
21 return $RPM2::constants{[[tag]]};
27 $sub =~ s/\[\[method\]\]/$method/g;
28 $sub =~ s/\[\[tag\]\]/$tag/g;
40 my $self = bless { }, "RPM2::DB";
42 $class->add_macro("_dbpath", $params{-path});
43 $self->{c_db} = RPM2::_open_rpm_db($params{-readwrite} ? 1 : 0);
44 $class->delete_macro("_dbpath");
47 $self->{c_db} = RPM2::_open_rpm_db($params{-readwrite} ? 1 : 0);
58 or die "Can't open $file: $!";
62 my ($hdr) = RPM2::_read_from_file(*FH);
65 push @ret, RPM2::Header->_new_raw($hdr);
77 if (RPM2->rpm_api_version > 4.0 and not defined $flags) {
78 $flags = RPM2->vsf_default;
83 or die "Can't open $file: $!";
85 my $hdr = RPM2::_read_package_info(*FH, $flags);
88 my ($filename, $path) = (basename($file), realpath(dirname($file)));
89 $hdr = RPM2::Header->_new_raw($hdr, File::Spec->catfile($path, $filename));
93 sub create_transaction
99 return undef if (RPM2->rpm_api_version <= 4.0);
100 if(not defined $flags) {
101 $flags = RPM2->vsf_default;
104 $t = RPM2::_create_transaction($flags);
105 $t = RPM2::Transaction->_new_raw($t);
115 return RPM2::PackageIterator->new_iterator($self, "RPMTAG_NAME")
121 return RPM2::PackageIterator->new_iterator($self)->expand_iter();
124 sub find_by_name_iter {
128 return RPM2::PackageIterator->new_iterator($self, "RPMTAG_NAME", $name);
134 return $self->find_by_name_iter($name)->expand_iter;
137 sub find_by_provides_iter {
141 return RPM2::PackageIterator->new_iterator($self, "RPMTAG_PROVIDES", $name);
143 sub find_by_provides {
147 return $self->find_by_provides_iter($name)->expand_iter;
150 sub find_by_requires_iter {
154 return RPM2::PackageIterator->new_iterator($self, "RPMTAG_REQUIRENAME", $name);
157 sub find_by_requires {
161 return $self->find_by_requires_iter($name)->expand_iter;
164 sub find_by_file_iter {
168 return RPM2::PackageIterator->new_iterator($self, "RPMTAG_BASENAMES", $name);
175 return $self->find_by_file_iter($name)->expand_iter;
178 package RPM2::Header;
180 use overload '<=>' => \&op_spaceship,
181 'cmp' => \&op_spaceship,
186 my $c_header = shift;
187 my $filename = shift;
190 my $self = bless { }, $class;
191 $self->{c_header} = $c_header;
192 $self->{filename} = $filename if defined $filename;
193 $self->{db_offset} = $offset if defined $offset;
202 $tag = uc "RPMTAG_$tag";
204 die "tag $tag invalid"
205 unless exists $RPM2::header_tag_map{$tag};
207 return $self->{c_header}->tag_by_id($RPM2::header_tag_map{$tag});
214 return RPM2::C::Header::_header_sprintf($self->{c_header}, $format);
221 return RPM2::C::Header::_header_compare($h1->{c_header}, $h2->{c_header});
227 return defined($self) && defined($self->{c_header});
234 my $ret = $h1->compare($h2);
236 # rpmvercmp can return any neg/pos number; normalize here to -1, 0, 1
237 return 1 if $ret > 0;
238 return -1 if $ret < 0;
242 sub is_source_package {
245 return RPM2::C::Header::_header_is_source($self->{c_header});
250 if (exists $self->{filename}) {
251 return $self->{filename};
258 if (exists $self->{db_offset}) {
259 return $self->{db_offset};
266 my $epoch = $self->tag('epoch');
269 $epoch_str = "$epoch:" if defined $epoch;
271 my $ret = $epoch_str . join("-", map { $self->tag($_) } qw/name version release/);
276 foreach my $tag (keys %RPM2::header_tag_map) {
277 $tag =~ s/^RPMTAG_//g;
282 return $self->tag("[[tag]]");
286 my $method = lc $tag;
287 $sub =~ s/\[\[method\]\]/$method/g;
288 $sub =~ s/\[\[tag\]\]/$tag/g;
299 if (not exists $self->{files}) {
300 my @base_names = $self->tag('basenames');
301 my @dir_names = $self->tag('dirnames');
302 my @dir_indexes = $self->tag('dirindexes');
305 foreach (0 .. $#base_names) {
306 push @files, $dir_names[$dir_indexes[$_]] . $base_names[$_];
309 $self->{files} = \@files;
312 return @{$self->{files}};
318 if (not exists $self->{changelog}) {
319 my @cltimes = $self->tag('CHANGELOGTIME');
320 my @clnames = $self->tag('CHANGELOGNAME');
321 my @cltexts = $self->tag('CHANGELOGTEXT');
324 foreach (0 .. $#cltimes) {
326 { time => $cltimes[$_],
327 name => $clnames[$_],
328 text => $cltexts[$_],
332 $self->{changelog} = \@changelog;
335 return @{$self->{changelog}};
338 package RPM2::PackageIterator;
346 my $self = bless { db => $db }, $class;
347 $self->{c_iter} = RPM2::C::DB::_init_iterator($db->{c_db},
348 $RPM2::header_tag_map{$tag},
350 defined $key ? length $key : 0);
357 return unless $self->{c_iter};
358 my ($hdr, $offset) = $self->{c_iter}->_iterator_next();
361 my $ret = RPM2::Header->_new_raw($hdr, undef, $offset);
369 while (my $h = $self->next) {
376 # make sure c_iter is destroyed before {db} so that we always free an
377 # iterator before we free the db it came from
381 delete $self->{c_iter};
384 package RPM2::Transaction;
388 my $c_transaction = shift;
390 my $self = bless { }, $class;
391 $self->{c_transaction} = $c_transaction;
399 my $upgrade = shift || 0;
403 # Must have a header to add
404 return 0 if(!defined($h));
408 $fn = $h->filename();
410 # XXX: Need to add relocations at some point, but I think we live
411 # without this for now (until I need it (-;).
412 return RPM2::C::Transaction::_add_install($self->{'c_transaction'},
413 $h->{'c_header'}, $fn, $upgrade)
423 # Must have a header to add
424 return 0 if(!defined($h));
428 $db_offset = $h->offset();
429 return 0 if(!defined($db_offset));
431 # XXX: Need to add relocations at some point, but I think we live
432 # without this for now (until I need it (-;).
433 return RPM2::C::Transaction::_add_delete($self->{'c_transaction'},
434 $h->{'c_header'}, $db_offset)
440 return $self->{'c_transaction'}->_element_count();
446 return $self->{'c_transaction'}->_close_db();
452 return $self->{'c_transaction'}->_check();
458 return $self->{'c_transaction'}->_order();
465 $type = 0 if(!defined($type));
467 return $self->{'c_transaction'}->_elements($type);
472 my $ok_probs = shift || '';
473 my $ignore_probs = shift || 0;
475 return RPM2::C::Transaction::_run($self->{'c_transaction'}, $ok_probs,
479 # Preloaded methods go here.
483 # Below is stub documentation for your module. You better edit it!
487 RPM2 - Perl bindings for the RPM Package Manager API
493 my $db = RPM2->open_rpm_db();
495 my $i = $db->find_all_iter();
496 print "The following packages are installed (aka, 'rpm -qa'):\n";
497 while (my $pkg = $i->next) {
498 print $pkg->as_nvre, "\n";
501 $i = $db->find_by_name_iter("kernel");
502 print "The following kernels are installed (aka, 'rpm -q kernel'):\n";
503 while (my $pkg = $i->next) {
504 print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";
507 $i = $db->find_by_provides_iter("kernel");
508 print "The following packages provide 'kernel' (aka, 'rpm -q --whatprovides kernel'):\n";
509 while (my $pkg = $i->next) {
510 print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";
513 print "The following packages are installed (aka, 'rpm -qa' once more):\n";
514 foreach my $pkg ($db->find_by_file("/bin/sh")) {
515 print $pkg->as_nvre, "\n";
518 my $pkg = RPM2->open_package("/tmp/XFree86-4.1.0-15.src.rpm");
519 print "Package opened: ", $pkg->as_nvre(), ", is source: ", $pkg->is_source_package, "\n";
523 The RPM2 module provides an object-oriented interface to querying both
524 the installed RPM database as well as files on the filesystem.
528 Pretty much all use of the class starts here. There are three main
529 entrypoints into the package -- either through the database of
530 installed rpms (aka the rpmdb), through a file on the filesystem
531 (such as kernel-2.4.9-31.src.rpm or kernel-2.4.9-31.i386.rpm, or via
534 You can have multiple RPM databases open at once, as well as running
535 multiple queries on each. That being said if you expect to run a transaction
536 to install or erase some rpms, you will need to cause any RPM2::DB and
537 RPM2::PackageIterator objects to go out of scope. For instance:
539 $db = RPM2->open_rpm_db();
540 $i = $db->find_by_name("vim");
541 $t = create_transaction();
542 while($pkg = $i->next()) {
547 Would end up in a dead lock waiting for $db, and $i (the RPM2::DB and
548 RPM2::PackageIterator) objects to releaase their read lock on the database.
549 The correct way of handling this then would be to do the following
550 before running the transaction:
555 That is to explicitly cause the RPM2::DB and RPM2::PackageIterator objects to
560 =item open_rpm_db(-path => "/path/to/db")
562 As it sounds, it opens the RPM database, and returns it as an object.
563 The path to the database (i.e. C<-path>) is optional.
565 =item open_package("foo-1.1-14.noarch.rpm")
567 Opens a specific package (RPM or SRPM). Returns a Header object.
569 =item create_transaction(RPM2->vsf_default)
571 Creates an RPM2::Transaction. This can be used to install and
572 remove packages. It, also, exposes the dependency ordering functionality.
573 It takes as an optional argument verify signature flags. The following
576 =item RPM2->vsf_default
578 You don't ever have to specify this, but you could if you wanted to do so.
579 This will check headers, not require a files payload, and support all the
580 various hash and signature formats that rpm supports.
582 =item RPM2->vsf_nohdrchk
584 Don't check the header.
586 =item RPM2->vsf_needpayload
588 Require that a files payload be part of the RPM (Chip is this right?).
590 =item RPM2->vsf_nosha1header
593 =item RPM2->vsf_nomd5header
595 =item RPM2->vsf_nodsaheader
597 =item RPM2->vsf_norsaheader
599 =item RPM2->vsf_nosha1
601 =item RPM2->vsf_nomd5
603 =item RPM2->vsf_nodsa
605 =item RPM2->vsf_norsa
609 =head1 RPM DB object methods
613 =item find_all_iter()
615 Returns an iterator object that iterates over the entire database.
619 Returns an list of all of the results of the find_all_iter() method.
621 =item find_by_file_iter($filename)
623 Returns an iterator that returns all packages that contain a given file.
625 =item find_by_file($filename)
627 Ditto, except it just returns the list
629 =item find_by_name_iter($package_name)
631 You get the idea. This one is for iterating by package name.
633 =item find_by_name($package_name)
635 Ditto, except it returns a list.
637 =item find_by_provides_iter($provides_string)
639 This one iterates over provides.
641 =item find_by_provides($provides_string)
643 Ditto, except it returns a list.
645 =item find_by_requires_iter($requires_string)
647 This one iterates over requires.
649 =item find_by_requires($requires_string)
651 Ditto, except it returns a list.
655 =head1 RPM Database Iterator Methods
657 Once you have a a database iterator, then you simply need to step
658 through all the different package headers in the result set via the
665 Return the next package header in the result set.
669 Return the list of all the package headers in the result set of the iterator.
673 =head1 RPM Header object methods
675 In addition to the following methods, all tags have simple accessors;
676 $hdr->epoch() is equivalent to $hdr->tag('epoch').
678 The <=> and cmp operators can be used to compare versions of two packages.
682 =item $hdr->tag($tagname)
684 Returns the value of the tag $tagname.
686 =item $hdr->tagformat($format)
690 =item $hdr->is_source_package()
692 Returns a true value if the package is a source package, false otherwise.
694 =item $hdr->filename()
696 Returns the filename of the package.
700 Returns the rpm database offset for the package.
702 =item $hdr->as_nvre()
704 Returns a string formatted like:
706 epoch:name-version-release
708 If epoch is undefined for this package, it and the leading colon are omitted.
714 =item $hdr->changelog()
716 Returns a list of hash refs containing the change log data of the package.
717 The hash keys represent individual change log entries, and their keys are:
718 C<time> (the time of the changelog entry), C<name> (the "name", ie. often
719 the email address of the author of the entry), and C<text> (the text of the
724 =head1 Transaction object methods
726 Transactions are what allow you to install, upgrade, and remove rpms.
727 Transactions are created, have elements added to them (i.e. package headers)
728 and are ran. When run the updates to the system and the rpm database are
729 treated as on "transaction" which is assigned a transaction id. This can
730 be queried in install packages as the INSTALLTID, and for repackaged packages
731 they have the REMOVETID set.
735 =item add_install($pkg, $upgrade)
737 Adds a package to a transaction for installation. If you want this to
738 be done as a package upgrade, then be sure to set the second optional
739 parameter to 1. It will return 0 on failure and 1 on success. Note,
740 this should be obvious, but the package header must come from an rpm file,
741 not from the RPM database.
743 =item add_erase($pkg)
745 Adds a package to a transaction for erasure. The package header should
746 come from the database (i.e. via an iterator) and not an rpm file.
748 =item element_count()
750 Returns the number of elements in a transaction (this is the sum of the
751 install and erase elements.
755 Closes the rpm database. This is needed for some ordering of
756 transactions for non-install purposes.
760 Verify that the dependencies for this transaction are met. Returns
761 0 on failure and 1 on success.
765 Order the elements in dependency order.
769 Return a list of elements as they are presently ordered. Note, this
770 returns the NEVR's not the package headers.
774 Run the transaction. This will automatically check for dependency
775 satisfaction, and order the transaction.
781 Make package installation and removal better (-;.
783 Signature validation.
797 Chip Turner E<lt>cturner@redhat.comE<gt>
802 The original L<RPM> module.