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