Debian Codegraph @

examples/p5solv

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