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