make pool.setarch() do the right thing
[platform/upstream/libsolv.git] / examples / p5solv
1 #!/usr/bin/perl -w
2
3 use POSIX;
4 use Fcntl;
5 use Config::IniFiles;
6 use Data::Dumper;
7 use solv;
8 use Devel::Peek;
9 use FileHandle;
10 use File::Temp ();
11 use strict;
12
13 package Repo::generic;
14
15 sub new {
16   my ($class, $alias, $type, $attr) = @_;
17   my $r = { %{$attr || {}} };
18   $r->{'alias'} = $alias;
19   $r->{'type'} = $type;
20   return bless $r, $class;
21 }
22
23 sub calc_cookie_fp {
24   my ($self, $fp) = @_;
25   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
26   $chksum->add("1.1");
27   $chksum->add_fp($fp);
28   return $chksum->raw();
29 }
30
31 sub calc_cookie_file {
32   my ($self, $filename) = @_;
33   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
34   $chksum->add("1.1");
35   $chksum->add_stat($filename);
36   return $chksum->raw();
37 }
38
39 sub calc_cookie_ext {
40   my ($self, $f, $cookie) = @_;
41   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
42   $chksum->add("1.1");
43   $chksum->add($cookie);
44   $chksum->add_fstat(fileno($f));
45   my $extcookie = $chksum->raw();
46   substr($extcookie, 0, 1) = chr(1) if ord(substr($extcookie, 0, 1)) == 0;
47   return $extcookie;
48 }
49
50 sub cachepath {
51   my ($self, $ext) = @_;
52   my $path = $self->{'alias'};
53   $path =~ s/^\./_/s;
54   $path .= $ext ? "_$ext.solvx" : '.solv';
55   $path =~ s/\//_/gs;
56   return "/var/cache/solv/$path";
57 }
58
59 sub load {
60   my ($self, $pool) = @_;
61   $self->{'handle'} = $pool->add_repo($self->{'alias'});
62   $self->{'handle'}->{'appdata'} = $self;
63   $self->{'handle'}->{'priority'} = 99 - $self->{'priority'};
64   my $dorefresh = $self->{'autorefresh'};
65   if ($dorefresh) {
66     my @s = stat($self->cachepath());
67     $dorefresh = 0 if @s && ($self->{'metadata_expire'} == -1 || time() - $s[9] < $self->{'metadata_expire'});
68   }
69   $self->{'cookie'} = '';
70   if (!$dorefresh && $self->usecachedrepo()) {
71     print "repo: '$self->{'alias'}' cached\n";
72     return 1;
73   }
74   return 0;
75 }
76
77 sub load_ext {
78   return 0;
79 }
80
81 sub download {
82   my ($self, $file, $uncompress, $chksum, $markincomplete) = @_;
83   if (!$self->{'baseurl'}) {
84     print "$self->{'alias'}: no baseurl\n";
85     return undef;
86   }
87   my $url = $self->{'baseurl'};
88   $url =~ s/\/$//;
89   $url .= "/$file";
90   open(my $f, '+>', undef) || die;
91   fcntl($f, Fcntl::F_SETFD, 0);
92   my $st = system('curl', '-f', '-s', '-L', '-o', "/dev/fd/".fileno($f), '--', $url);
93   if (POSIX::lseek(fileno($f), 0, POSIX::SEEK_END) == 0 && ($st == 0 || !$chksum)) {
94     return undef;
95   }
96   POSIX::lseek(fileno($f), 0, POSIX::SEEK_SET);
97   if ($st) {
98     print "$file: download error #$st\n";
99     $self->{'incomplete'} = 1 if $markincomplete;
100     return undef;
101   }
102   if ($chksum) {
103     my $fchksum = solv::Chksum->new($chksum->{'type'});
104     $fchksum->add_fd(fileno($f));
105     if ($fchksum != $chksum) {
106       print "$file: checksum error\n";
107       $self->{'incomplete'} = 1 if $markincomplete;
108       return undef;
109     }
110   }
111   if ($uncompress) {
112     return solv::xfopen_dup($file, fileno($f));
113   } else {
114     return solv::xfopen_dup(undef, fileno($f));
115   }
116 }
117
118 sub usecachedrepo {
119   my ($self, $ext, $mark) = @_;
120   my $cookie = $ext ? $self->{'extcookie'} : $self->{'cookie'};
121   my $handle = $self->{'handle'};
122   my $cachepath = $self->cachepath($ext);
123   my $fextcookie;
124   if (sysopen(my $f, $cachepath, POSIX::O_RDONLY)) {
125     sysseek($f, -32, Fcntl::SEEK_END);
126     my $fcookie = '';
127     return undef if sysread($f, $fcookie, 32) != 32;
128     return undef if $cookie && $fcookie ne $cookie;
129     if ($self->{'type'} ne 'system' && !$ext) {
130       sysseek($f, -32 * 2, Fcntl::SEEK_END);
131       return undef if sysread($f, $fextcookie, 32) != 32;
132     }
133     sysseek($f, 0, Fcntl::SEEK_SET);
134     my $fd = solv::xfopen_dup(undef, fileno($f));
135     my $flags = $ext ? $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES : 0;
136     $flags |= $solv::Repo::REPO_LOCALPOOL if $ext && $ext ne 'DL';
137     if (!$self->{'handle'}->add_solv($fd, $flags)) {
138       solv::xfclose($fd);
139       return undef;
140     }
141     solv::xfclose($fd);
142     $self->{'cookie'} = $fcookie unless $ext;
143     $self->{'extcookie'} = $fextcookie if $fextcookie;
144     utime undef, undef, $f if $mark;
145     return 1;
146   }
147   return undef;
148 }
149
150 sub writecachedrepo {
151   my ($self, $ext, $info) = @_;
152   return if $self->{'incomplete'} && !$ext;
153   mkdir("/var/cache/solv", 0755) unless -d "/var/cache/solv";
154   my ($f, $tmpname);
155   eval {
156     ($f, $tmpname) = File::Temp::tempfile(".newsolv-XXXXXX", 'DIR' => '/var/cache/solv');
157   };
158   return unless $f;
159   chmod 0444, $f;
160   my $ff = solv::xfopen_dup(undef, fileno($f));
161   if (!$info) {
162     $self->{'handle'}->write($ff);
163   } elsif ($ext) {
164     $info->write($ff);
165   } else {
166      $self->{'handle'}->write_first_repodata($ff);
167   }
168   solv::xfclose($ff);
169   if ($self->{'type'} ne 'system' && !$ext) {
170     $self->{'extcookie'} ||= $self->calc_cookie_ext($f, $self->{'cookie'});
171     syswrite($f, $self->{'extcookie'});
172   }
173   syswrite($f, $ext ? $self->{'extcookie'} : $self->{'cookie'});
174   close($f);
175   if ($self->{'handle'}->iscontiguous()) {
176     $f = solv::xfopen($tmpname);
177     if ($f) {
178       if (!$ext) {
179         $self->{'handle'}->empty();
180         die("internal error, cannot reload solv file\n") unless $self->{'handle'}->add_solv($f, $solv::Repo::SOLV_ADD_NO_STUBS);
181       } else {
182         $info->extend_to_repo();
183         $info->add_solv($f, $solv::Repo::REPO_EXTEND_SOLVABLES);
184       }
185       solv::xfclose($f);
186     }
187   }
188   rename($tmpname, $self->cachepath($ext));
189 }
190
191 sub packagespath {
192   my ($self) = @_;
193   return '';
194 }
195
196 package Repo::rpmmd;
197
198 our @ISA = ('Repo::generic');
199
200 sub find {
201   my ($self, $what) = @_;
202   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_TYPE, $what, $solv::Dataiterator::SEARCH_STRING);
203   $di->prepend_keyname($solv::REPOSITORY_REPOMD);
204   for my $d (@$di) {
205     my $dp = $d->parentpos();
206     my $filename = $dp->lookup_str($solv::REPOSITORY_REPOMD_LOCATION);
207     next unless $filename;
208     my $chksum = $dp->lookup_checksum($solv::REPOSITORY_REPOMD_CHECKSUM);
209     if (!$chksum) {
210       print "no $filename file checksum!\n";
211       return (undef, undef);
212     }
213     return ($filename, $chksum);
214   }
215   return (undef, undef);
216 }
217
218 sub add_ext {
219   my ($self, $repodata, $what, $ext) = @_;
220   my ($filename, $chksum) = $self->find($what);
221   ($filename, $chksum) = $self->find('prestodelta') if !$filename && $what eq 'deltainfo';
222   return unless $filename;
223   my $handle = $repodata->new_handle();
224   $repodata->set_poolstr($handle, $solv::REPOSITORY_REPOMD_TYPE, $what);
225   $repodata->set_str($handle, $solv::REPOSITORY_REPOMD_LOCATION, $filename);
226   $repodata->set_checksum($handle, $solv::REPOSITORY_REPOMD_CHECKSUM, $chksum);
227   if ($ext eq 'DL') {
228     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
229     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
230   } elsif ($ext eq 'FL') {
231     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_FILELIST);
232     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRSTRARRAY);
233   }
234   $repodata->add_flexarray($solv::SOLVID_META, $solv::REPOSITORY_EXTERNAL, $handle);
235 }
236
237 sub add_exts {
238   my ($self) = @_;
239   my $repodata = $self->{'handle'}->add_repodata(0);
240   $self->add_ext($repodata, 'deltainfo', 'DL');
241   $self->add_ext($repodata, 'filelists', 'FL');
242   $repodata->internalize();
243 }
244
245 sub load_ext {
246   my ($self, $repodata) = @_;
247   my $repomdtype = $repodata->lookup_str($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_TYPE);
248   my $ext;
249   if ($repomdtype eq 'filelists') {
250     $ext = 'FL';
251   } elsif ($repomdtype eq 'deltainfo') {
252     $ext = 'DL';
253   } else {
254     return 0;
255   }
256   print("[$self->{'alias'}:$ext: ");
257   STDOUT->flush();
258   if ($self->usecachedrepo($ext)) {
259     print "cached]\n";
260     return 1;
261   }
262   print "fetching]\n";
263   my $filename = $repodata->lookup_str($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_LOCATION);
264   my $filechksum = $repodata->lookup_checksum($solv::SOLVID_META, $solv::REPOSITORY_REPOMD_CHECKSUM);
265   my $f = $self->download($filename, 1, $filechksum);
266   return 0 unless $f;
267   if ($ext eq 'FL') {
268     $self->{'handle'}->add_rpmmd($f, 'FL', $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES);
269   } elsif ($ext eq 'FL') {
270     $self->{'handle'}->add_deltainfoxml($f, $solv::Repo::REPO_USE_LOADING);
271   }
272   solv::xfclose($f);
273   $self->writecachedrepo($ext, $repodata);
274   return 1;
275 }
276
277 sub load {
278   my ($self, $pool) = @_;
279   return 1 if $self->Repo::generic::load($pool);
280   print "rpmmd repo '$self->{'alias'}': ";
281   STDOUT->flush();
282   my $f = $self->download("repodata/repomd.xml");
283   if (!$f) {
284     print "no repomd.xml file, skipped\n";
285     $self->{'handle'}->free(1);
286     delete $self->{'handle'};
287     return undef;
288   }
289   $self->{'cookie'} = $self->calc_cookie_fp($f);
290   if ($self->usecachedrepo(undef, 1)) {
291     print "cached\n";
292     solv::xfclose($f);
293     return 1;
294   }
295   $self->{'handle'}->add_repomdxml($f, 0);
296   solv::xfclose($f);
297   print "fetching\n";
298   my ($filename, $filechksum) = $self->find('primary');
299   if ($filename) {
300     $f = $self->download($filename, 1, $filechksum, 1);
301     if ($f) {
302       $self->{'handle'}->add_rpmmd($f, undef, 0);
303       solv::xfclose($f);
304     }
305     return undef if $self->{'incomplete'};
306   }
307   ($filename, $filechksum) = $self->find('updateinfo');
308   if ($filename) {
309     $f = $self->download($filename, 1, $filechksum, 1);
310     if ($f) {
311       $self->{'handle'}->add_updateinfoxml($f, 0);
312       solv::xfclose($f);
313     }
314   }
315   $self->add_exts();
316   $self->writecachedrepo();
317   $self->{'handle'}->create_stubs();
318   return 1;
319 }
320
321 package Repo::susetags;
322
323 our @ISA = ('Repo::generic');
324
325 sub find {
326   my ($self, $what) = @_;
327   
328   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME, $what, $solv::Dataiterator::SEARCH_STRING);
329   $di->prepend_keyname($solv::SUSETAGS_FILE);
330   for my $d (@$di) {
331     my $dp = $d->parentpos();
332     my $chksum = $dp->lookup_checksum($solv::SUSETAGS_FILE_CHECKSUM);
333     return ($what, $chksum);
334   }
335   return (undef, undef);
336 }
337
338 my %langtags = (
339   $solv::SOLVABLE_SUMMARY     => $solv::REPOKEY_TYPE_STR,
340   $solv::SOLVABLE_DESCRIPTION => $solv::REPOKEY_TYPE_STR,
341   $solv::SOLVABLE_EULA        => $solv::REPOKEY_TYPE_STR,
342   $solv::SOLVABLE_MESSAGEINS  => $solv::REPOKEY_TYPE_STR,
343   $solv::SOLVABLE_MESSAGEDEL  => $solv::REPOKEY_TYPE_STR,
344   $solv::SOLVABLE_CATEGORY    => $solv::REPOKEY_TYPE_ID,
345 );
346
347 sub add_ext {
348   my ($self, $repodata, $what, $ext) = @_;
349   my ($filename, $chksum) = $self->find($what);
350   my $handle = $repodata->new_handle();
351   $repodata->set_str($handle, $solv::SUSETAGS_FILE_NAME, $filename);
352   $repodata->set_checksum($handle, $solv::SUSETAGS_FILE_CHECKSUM, $chksum);
353   if ($ext eq 'DL') {
354     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOSITORY_DELTAINFO);
355     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_FLEXARRAY);
356   } elsif ($ext eq 'DU') {
357     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_DISKUSAGE);
358     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRNUMNUMARRAY);
359   } elsif ($ext eq 'FL') {
360     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::SOLVABLE_FILELIST);
361     $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $solv::REPOKEY_TYPE_DIRSTRARRAY);
362   } else {
363     for my $langid (sort {$a <=> $b} keys %langtags) {
364       $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $self->{'handle'}->{'pool'}->id2langid($langid, $ext, 1));
365       $repodata->add_idarray($handle, $solv::REPOSITORY_KEYS, $langtags{$langid});
366     }
367   }
368   $repodata->add_flexarray($solv::SOLVID_META, $solv::REPOSITORY_EXTERNAL, $handle);
369 }
370
371 sub add_exts {
372   my ($self) = @_;
373   my $repodata = $self->{'handle'}->add_repodata(0);
374   my $di = $self->{'handle'}->Dataiterator($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME, undef, 0);
375   $di->prepend_keyname($solv::SUSETAGS_FILE);
376   for my $d (@$di) {
377     my $filename = $d->str();
378     next unless $filename && $filename =~ /^packages\.(..)(?:\..*)$/;
379     next if $1 eq 'en' || $1 eq 'gz';
380     $self->add_ext($repodata, $filename, $1);
381   }
382   $repodata->internalize();
383 }
384
385 sub load_ext {
386   my ($self, $repodata) = @_;
387   my $filename = $repodata->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_FILE_NAME);
388   my $ext = substr($filename, 9, 2);
389   print("[$self->{'alias'}:$ext: ");
390   STDOUT->flush();
391   if ($self->usecachedrepo($ext)) {
392     print "cached]\n";
393     return 1;
394   }
395   print "fetching]\n";
396   my $defvendorid = $self->{'handle'}->lookup_id($solv::SOLVID_META, $solv::SUSETAGS_DEFAULTVENDOR);
397   my $descrdir = $self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DESCRDIR) || 'suse/setup/descr'; 
398   my $filechksum = $repodata->lookup_checksum($solv::SOLVID_META, $solv::SUSETAGS_FILE_CHECKSUM);
399   my $f = $self->download("$descrdir/$filename", 1, $filechksum);
400   return 0 unless $f;
401   $self->{'handle'}->add_susetags($f, $defvendorid, $ext, $solv::Repo::REPO_USE_LOADING|$solv::Repo::REPO_EXTEND_SOLVABLES);
402   solv::xfclose($f);
403   $self->writecachedrepo($ext, $repodata);
404   return 1;
405 }
406
407 sub load {
408   my ($self, $pool) = @_;
409   return 1 if $self->Repo::generic::load($pool);
410   print "susetags repo '$self->{'alias'}': ";
411   STDOUT->flush();
412   my $f = $self->download("content");
413   if (!$f) {
414     print "no content file, skipped\n";
415     $self->{'handle'}->free(1);
416     delete $self->{'handle'};
417     return undef;
418   }
419   $self->{'cookie'} = $self->calc_cookie_fp($f);
420   if ($self->usecachedrepo(undef, 1)) {
421     print "cached\n";
422     solv::xfclose($f);
423     return 1;
424   }
425   $self->{'handle'}->add_content($f, 0);
426   solv::xfclose($f);
427   print "fetching\n";
428   my $defvendorid = $self->{'handle'}->lookup_id($solv::SOLVID_META, $solv::SUSETAGS_DEFAULTVENDOR);
429   my $descrdir = $self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DESCRDIR) || 'suse/setup/descr'; 
430   my ($filename, $filechksum) = $self->find('packages.gz');
431   ($filename, $filechksum) = $self->find('packages') unless $filename;
432   if ($filename) {
433     $f = $self->download("$descrdir/$filename", 1, $filechksum, 1);
434     if ($f) {
435       $self->{'handle'}->add_susetags($f, $defvendorid, undef, $solv::Repo::REPO_NO_INTERNALIZE|$solv::Repo::SUSETAGS_RECORD_SHARES);
436       solv::xfclose($f);
437       ($filename, $filechksum) = $self->find('packages.en.gz');
438       ($filename, $filechksum) = $self->find('packages.en') unless $filename;
439       if ($filename) {
440         $f = $self->download("$descrdir/$filename", 1, $filechksum, 1);
441         if ($f) {
442           $self->{'handle'}->add_susetags($f, $defvendorid, undef, $solv::Repo::REPO_NO_INTERNALIZE|$solv::Repo::REPO_REUSE_REPODATA|$solv::Repo::REPO_EXTEND_SOLVABLES);
443           solv::xfclose($f);
444         }
445       }
446       $self->{'handle'}->internalize();
447     }
448   }
449   $self->add_exts();
450   $self->writecachedrepo();
451   $self->{'handle'}->create_stubs();
452   return undef;
453 }
454
455 sub packagespath {
456   my ($self) = @_;
457   return ($self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DATADIR) || 'suse') . '/';
458 }
459
460 package Repo::unknown;
461
462 our @ISA = ('Repo::generic');
463
464 sub load {
465   my ($self, $pool) = @_;
466   print "unsupported repo '$self->{'alias'}': skipped\n";
467   return 0;
468 }
469
470 package Repo::system;
471
472 our @ISA = ('Repo::generic');
473
474 sub load {
475   my ($self, $pool) = @_;
476
477   $self->{'handle'} = $pool->add_repo($self->{'alias'});
478   $self->{'handle'}->{'appdata'} = $self;
479   $pool->{'installed'} = $self->{'handle'};
480   print "rpm database: ";
481   $self->{'cookie'} = $self->calc_cookie_file('/var/lib/rpm/Packages');
482   if ($self->usecachedrepo()) {
483     print "cached\n";
484     return 1;
485   }
486   print "reading\n";
487   if (defined(&solv::Repo::add_products)) {
488     $self->{'handle'}->add_products("/etc/products.d", $solv::Repo::REPO_NO_INTERNALIZE);
489   }
490   $self->{'handle'}->add_rpmdb(undef, $solv::Repo::REPO_REUSE_REPODATA);
491   $self->writecachedrepo();
492   return 1;
493 }
494
495 package main;
496
497 sub load_stub {
498   my ($repodata) = @_;
499   my $repo = $repodata->{'repo'}->{'appdata'};
500   return $repo ? $repo->load_ext($repodata) : 0;
501 }
502
503 die("Usage: p5solv COMMAND [ARGS]\n") unless @ARGV;
504 my $cmd = shift @ARGV;
505 $cmd = 'list' if $cmd eq 'li';
506 $cmd = 'install' if $cmd eq 'in';
507 $cmd = 'erase' if $cmd eq 'rm';
508 $cmd = 'verify' if $cmd eq 've';
509 $cmd = 'search' if $cmd eq 'se';
510
511 my @repos;
512 my @reposdirs;
513 if (-d '/etc/zypp/repos.d') {
514   @reposdirs = ( '/etc/zypp/repos.d' );
515 } else {
516   @reposdirs = ( '/etc/yum/repos.d' );
517 }
518 for my $reposdir (@reposdirs) {
519   next unless -d $reposdir;
520   next unless opendir(DIR, $reposdir);
521   for my $reponame (sort(grep {/\.repo$/} readdir(DIR))) {
522     my $cfg = new Config::IniFiles('-file' => "$reposdir/$reponame");
523     for my $alias ($cfg->Sections()) {
524       my $repoattr = {'alias' => $alias, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900};
525       for my $p ($cfg->Parameters($alias)) {
526         $repoattr->{$p} = $cfg->val($alias, $p);
527       }
528       my $repo;
529       if ($repoattr->{'type'} eq 'rpm-md') {
530         $repo = Repo::rpmmd->new($alias, 'repomd', $repoattr);
531       } elsif ($repoattr->{'type'} eq 'yast2') {
532         $repo = Repo::susetags->new($alias, 'susetags', $repoattr);
533       } else {
534         $repo = Repo::unknown->new($alias, 'unknown', $repoattr);
535       }
536       push @repos, $repo;
537     }
538   }
539 }
540
541 my $pool = solv::Pool->new();
542 $pool->setarch();
543 $pool->set_loadcallback(\&load_stub);
544
545 my $sysrepo = Repo::system->new('@System', 'system');
546 $sysrepo->load($pool);
547 for my $repo (@repos) {
548   $repo->load($pool) if $repo->{'enabled'};
549 }
550
551 if ($cmd eq 'search') {
552   my %matches;
553   my $di = $pool->Dataiterator(0, $solv::SOLVABLE_NAME, $ARGV[0], $solv::Dataiterator::SEARCH_SUBSTRING | $solv::Dataiterator::SEARCH_NOCASE);
554   for my $d (@$di) {
555     $matches{$d->{'solvid'}} = 1;
556   }
557   for my $solvid (sort keys %matches) {
558     my $s = $pool->{'solvables'}->[$solvid];
559     print "- ".$s->str()." [$s->{'repo'}->{'name'}] ".$s->lookup_str($solv::SOLVABLE_SUMMARY)."\n";
560   }
561   exit(0);
562 }
563
564 my @addedprovides = $pool->addfileprovides_queue();
565 $pool->createwhatprovides();
566
567 my @jobs;
568 for my $arg (@ARGV) {
569   my $flags = $solv::Selection::SELECTION_NAME | $solv::Selection::SELECTION_PROVIDES | $solv::Selection::SELECTION_GLOB;
570   if ($arg =~ /^\//) {
571     $flags |= $solv::Selection::SELECTION_FILELIST;
572     $flags |= $solv::Selection::SELECTION_INSTALLED_ONLY if $cmd eq 'erase';
573   }
574   my $sel = $pool->select($arg, $flags);
575   if ($sel->isempty()) {
576     $sel = $pool->select($arg, $flags | $solv::Selection::SELECTION_NOCASE);
577     print "[ignoring case for '$arg']\n" unless $sel->isempty();
578   }
579   die("nothing matches '$arg'\n") if $sel->isempty();
580   print "[using file list match for '$arg']\n" if $sel->flags() & $solv::Selection::SELECTION_FILELIST;
581   print "[using capability match for '$arg']\n" if $sel->flags() & $solv::Selection::SELECTION_PROVIDES;
582   push @jobs, $sel->jobs(0);
583 }
584 if (!@jobs && ($cmd eq 'up' || $cmd eq 'dup' || $cmd eq 'verify')) {
585   my $sel = $pool->Selection();
586   $sel->addsimple($solv::Job::SOLVER_SOLVABLE_ALL, 0);
587   push @jobs, $sel->jobs(0);
588 }
589
590 if ($cmd eq 'list' || $cmd eq 'info') {
591   die("no package matched.\n") unless @jobs;
592   for my $job (@jobs) {
593     for my $s ($job->solvables()) {
594       if ($cmd eq 'info') {
595         printf "Name:        %s\n", $s->str();
596         printf "Repo:        %s\n", $s->{'repo'}->{'name'};
597         printf "Summary:     %s\n", $s->lookup_str($solv::SOLVABLE_SUMMARY);
598         my $str = $s->lookup_str($solv::SOLVABLE_URL);
599         printf "Url:         %s\n", $str if $str;
600         $str = $s->lookup_str($solv::SOLVABLE_LICENSE);
601         printf "License:     %s\n", $str if $str;
602         printf "Description:\n%s\n", $s->lookup_str($solv::SOLVABLE_DESCRIPTION);
603       } else {
604         printf "  - %s [%s]\n", $s->str(), $s->{'repo'}->{'name'};
605         printf "    %s\n", $s->lookup_str($solv::SOLVABLE_SUMMARY);
606       }
607     }
608   }
609   exit 0;
610 }
611
612 if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cmd eq 'verify') {
613   die("no package matched.\n") unless @jobs;
614   for my $job (@jobs) {
615     if ($cmd eq 'up') {
616       if ($job->{'how'} == $solv::Job::SOLVER_SOLVABLE_ALL || grep {$_->isinstalled()} $job->solvables()) {
617         $job->{'how'} |= $solv::Job::SOLVER_UPDATE;
618       } else {
619         $job->{'how'} |= $solv::Job::SOLVER_INSTALL;
620       }
621     } elsif ($cmd eq 'install') {
622         $job->{'how'} |= $solv::Job::SOLVER_INSTALL;
623     } elsif ($cmd eq 'erase') {
624         $job->{'how'} |= $solv::Job::SOLVER_ERASE;
625     } elsif ($cmd eq 'dup') {
626         $job->{'how'} |= $solv::Job::SOLVER_DISTUPGRADE;
627     } elsif ($cmd eq 'verify') {
628         $job->{'how'} |= $solv::Job::SOLVER_VERIFY;
629     }
630   }
631   my $solver;
632   while (1) {
633     $solver = $pool->Solver();
634     $solver->set_flag($solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1);
635     $solver->set_flag($solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if $cmd eq 'erase';
636     my @problems = $solver->solve(\@jobs);
637     last unless @problems;
638     for my $problem (@problems) {
639       print "Problem $problem->{'id'}/".@problems.":\n";
640       my $r = $problem->findproblemrule();
641       my $ri = $r->info();
642       print $ri->problemstr()."\n";
643       my @solutions = $problem->solutions();
644       for my $solution (@solutions) {
645         print "  Solution $solution->{'id'}:\n";
646         for my $element ($solution->elements(1)) {
647           print "  - ".$element->str()."\n";
648         }
649         print "\n";
650       }
651       my $sol;
652       while (1) {
653         print "Please choose a solution: ";
654         $sol = <STDIN>;
655         chomp $sol;
656         last if $sol eq 's' || $sol eq 'q' || ($sol =~ /^\d+$/ && $sol >= 1 && $sol <= @solutions);
657       }
658       next if $sol eq 's';
659       exit(1) if $sol eq 'q';
660       my $solution = $solutions[$sol - 1];
661       for my $element ($solution->elements()) {
662         my $newjob = $element->Job();
663         if ($element->{'type'} == $solv::Solver::SOLVER_SOLUTION_JOB) {
664           $jobs[$element->{'jobidx'}] = $newjob;
665         } else {
666           push @jobs, $newjob if $newjob && !grep {$_ == $newjob} @jobs;
667         }
668       }
669     }
670   }
671   my $trans = $solver->transaction();
672   undef $solver;
673   if ($trans->isempty()) {
674     print "Nothing to do.\n";
675     exit 0;
676   }
677   print "\nTransaction summary:\n\n";
678   for my $c ($trans->classify()) {
679     if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
680       print "$c->{'count'} erased packages:\n";
681     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_INSTALL) {
682       print "$c->{'count'} installed packages:\n";
683     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_REINSTALLED) {
684       print "$c->{'count'} reinstalled packages:\n";
685     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
686       print "$c->{'count'} downgraded packages:\n";
687     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_CHANGED) {
688       print "$c->{'count'} changed packages:\n";
689     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED) {
690       print "$c->{'count'} upgraded packages:\n";
691     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE) {
692       printf "$c->{'count'} vendor changes from '%s' to '%s':\n", $pool->id2str($c->{'fromid'}), $pool->id2str($c->{'toid'});
693     } elsif ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE) {
694       printf "$c->{'count'} arch changes from '%s' to '%s':\n", $pool->id2str($c->{'fromid'}), $pool->id2str($c->{'toid'});
695     } else {
696       next;
697     }
698     for my $p ($c->solvables()) {
699       if ($c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_UPGRADED || $c->{'type'} == $solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED) {
700         my $other = $trans->othersolvable($p);
701         printf "  - %s -> %s\n", $p->str(), $other->str();
702       } else {
703         printf "  - %s\n", $p->str();
704       }
705     }
706     print "\n";
707   }
708   printf "install size change: %d K\n\n", $trans->calc_installsizechange();
709   while (1) {
710     print("OK to continue (y/n)? ");
711     my $yn = <STDIN>;
712     chomp $yn;
713     last if $yn eq 'y';
714     exit(1) if $yn eq 'n';
715   }
716   my @newpkgs = $trans->newpackages();
717   my %newpkgsfps;
718   if (@newpkgs) {
719     my $downloadsize = 0;
720     $downloadsize += $_->lookup_num($solv::SOLVABLE_DOWNLOADSIZE) for @newpkgs;
721     printf "Downloading %d packages, %d K\n", scalar(@newpkgs), $downloadsize;
722     for my $p (@newpkgs) {
723       my $repo = $p->{'repo'}->{'appdata'};
724       my ($location, $medianr) = $p->lookup_location();
725       next unless $location;
726       $location = $repo->packagespath() . $location;
727       my $chksum = $p->lookup_checksum($solv::SOLVABLE_CHECKSUM);
728       my $f = $repo->download($location, 0, $chksum);
729       die("\n$repo->{'alias'}: $location not found in repository\n") unless $f;
730       $newpkgsfps{$p->{'id'}} = $f;
731       print ".";
732       STDOUT->flush();
733     }
734     print "\n";
735   }
736   print "Committing transaction:\n\n";
737   $trans->order(0);
738   for my $p ($trans->steps()) {
739     my $steptype = $trans->steptype($p, $solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY);
740     if ($steptype == $solv::Transaction::SOLVER_TRANSACTION_ERASE) {
741       print "erase ".$p->str()."\n";
742       next unless $p->lookup_num($solv::RPM_RPMDBID);
743       my $evr = $p->{'evr'};
744       $evr =~ s/^[0-9]+://;     # strip epoch
745       system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "$p->{'name'}-$evr.$p->{'arch'}") && die("rpm failed: $?\n");
746     } elsif ($steptype == $solv::Transaction::SOLVER_TRANSACTION_INSTALL || $steptype == $solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL) {
747       print "install ".$p->str()."\n";
748       my $f = $newpkgsfps{$p->{'id'}};
749       my $mode = $steptype == $solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i';
750       system('rpm', $mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/".solv::xfileno($f)) && die("rpm failed: $?\n");
751       solv::xfclose($f);
752       delete $newpkgsfps{$p->{'id'}};
753     }
754   }
755 }
756
757 exit 0;