1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
// cmdline_util.cc
//
// Copyright 2004 Daniel Burrows
#include "cmdline_util.h"
#include "cmdline_common.h"
#include "cmdline_progress.h"
#include "cmdline_show.h" // For operator<<
#include <aptitude.h>
#include <ui.h>
#include <progress.h>
#include <generic/apt/apt.h>
#include <generic/apt/config_signal.h>
#include <generic/apt/download_manager.h>
#include <generic/apt/download_signal_log.h>
#include <generic/apt/matchers.h>
#include <cwidget/fragment.h>
#include <cwidget/toplevel.h>
#include <cwidget/generic/util/ssprintf.h>
#include <sigc++/bind.h>
#include <sigc++/functors/ptr_fun.h>
#include <apt-pkg/error.h>
#include <apt-pkg/indexfile.h>
#include <apt-pkg/metaindex.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/version.h>
#include <algorithm>
namespace cw = cwidget;
namespace
{
const int no_install_run_from_ui_preview_return = -1;
const int ui_preview_install_failed_return = -1;
// Completion routine for the UI preview; causes the program to
// return 0 if the install succeded and 1 otherwise.
//
// There's a question in my mind of what to do if the user cancels
// the preview or runs multiple previews. At the moment we return
// -1 if the preview is cancelled and the result of the last install
// run otherwise.
void ui_preview_complete(bool success, int *result)
{
*result = success ? 0 : ui_preview_install_failed_return;
}
}
void ui_preview()
{
int result = no_install_run_from_ui_preview_return;
ui_init();
file_quit.connect(sigc::ptr_fun(cwidget::toplevel::exitmain));
install_finished.connect(sigc::bind(sigc::ptr_fun(ui_preview_complete),
&result));
do_package_run_or_show_preview();
ui_main();
exit(result);
}
void ui_solution_screen()
{
ui_init();
file_quit.connect(sigc::ptr_fun(cwidget::toplevel::exitmain));
progress_ref p = gen_progress_bar();
do_new_package_view(*p.unsafe_get_ref());
do_examine_solution();
ui_main();
exit(0);
}
void cmdline_show_stringlist(strvector &items)
{
int loc=2;
printf(" ");
for(strvector::iterator i=items.begin(); i!=items.end(); ++i)
{
if(loc + i->size() > (unsigned)(screen_width - 5))
{
printf("\n ");
loc=2;
}
printf("%s ", i->c_str());
loc+=i->size()+1;
}
printf("\n");
}
void cmdline_show_pkglist(pkgvector &items)
{
strvector tmp;
for(pkgvector::iterator i=items.begin(); i!=items.end(); ++i)
tmp.push_back(i->Name());
cmdline_show_stringlist(tmp);
}
pkgCache::VerIterator cmdline_find_ver(pkgCache::PkgIterator pkg,
cmdline_version_source source,
string sourcestr)
{
switch(source)
{
case cmdline_version_curr_or_cand:
if(!pkg.CurrentVer().end())
return pkg.CurrentVer();
// Fall-through.
case cmdline_version_cand:
{
pkgCache::VerIterator candver=(*apt_cache_file)[pkg].CandidateVerIter(*apt_cache_file);
if(candver.end())
{
if(source == cmdline_version_cand)
printf(_("No candidate version found for %s\n"), pkg.Name());
else
printf(_("No current or candidate version found for %s\n"), pkg.Name());
}
return candver;
}
case cmdline_version_archive:
for(pkgCache::VerIterator ver=pkg.VersionList(); !ver.end(); ++ver)
for(pkgCache::VerFileIterator verfile=ver.FileList();
!verfile.end(); ++verfile)
{
pkgCache::PkgFileIterator pkgfile=verfile.File();
if(pkgfile.Archive() && sourcestr==pkgfile.Archive())
return ver;
}
printf(_("Unable to find an archive \"%s\" for the package \"%s\"\n"),
sourcestr.c_str(),
pkg.Name());
return pkgCache::VerIterator(*apt_cache_file, 0);
case cmdline_version_version:
for(pkgCache::VerIterator ver=pkg.VersionList(); !ver.end(); ++ver)
if(sourcestr==ver.VerStr())
return ver;
printf(_("Unable to find a version \"%s\" for the package \"%s\"\n"),
sourcestr.c_str(),
pkg.Name());
return pkgCache::VerIterator(*apt_cache_file, 0);
default:
printf(_("Internal error: invalid value %i passed to cmdline_find_ver!\n"),
source);
return pkg.VersionList();
}
}
bool cmdline_parse_source(const string &input,
cmdline_version_source &source,
string &package,
string &sourcestr)
{
string scratch=input;
source=cmdline_version_cand;
sourcestr="";
if(scratch.find('/')!=scratch.npos)
{
source=cmdline_version_archive;
// Use the last one.
string::size_type loc=scratch.rfind('/');
sourcestr=string(scratch, loc+1);
scratch.erase(loc);
}
if(scratch.find('=')!=scratch.npos)
{
if(source==cmdline_version_archive)
{
printf(_("You cannot specify both an archive and a version for a package\n"));
return false;
}
source=cmdline_version_version;
string::size_type loc=scratch.rfind('=');
sourcestr=string(scratch, loc+1);
scratch.erase(loc);
}
package=scratch;
return true;
}
namespace
{
/** \brief Used to display some statistics about how the cache
* changed after an update.
*
* Perhaps this should be extended to other commands too?
*/
class stats
{
int num_broken;
int num_upgradable;
int num_new;
// A set of package names that are obsolete. Names are stored
// since iterators aren't preserved over cache refreshes.
std::set<std::string> obsolete;
public:
stats(int _num_broken, int _num_upgradable, int _num_new,
const std::set<std::string> &_obsolete)
: num_broken(_num_broken),
num_upgradable(_num_upgradable),
num_new(_num_new),
obsolete(_obsolete)
{
}
int get_num_broken() const { return num_broken; }
int get_num_upgradable() const { return num_upgradable; }
int get_num_new() const { return num_new; }
const std::set<std::string> &get_obsolete() const { return obsolete; }
};
stats compute_apt_stats()
{
int num_upgradable = 0;
int num_new = 0;
int num_broken = 0;
std::set<std::string> obsolete;
if(apt_cache_file == NULL)
return stats(0, 0, 0, obsolete);
for(pkgCache::PkgIterator p = (*apt_cache_file)->PkgBegin();
!p.end(); ++p)
{
if(pkg_obsolete(p))
obsolete.insert(p.Name());
const pkgDepCache::StateCache &state = (*apt_cache_file)[p];
const aptitudeDepCache::aptitude_state &estate =
(*apt_cache_file)->get_ext_state(p);
// Q: this will count to-be-removed packages, should it?
if(state.Status != 2 && state.Upgradable())
++num_upgradable;
if(state.NowBroken())
++num_broken;
if((state.Status != 2 || state.CandidateVer != NULL) &&
estate.new_package)
++num_new;
}
return stats(num_broken, num_upgradable, num_new, obsolete);
}
void show_stats_change(stats initial, stats final,
bool show_all,
bool show_unchanged)
{
using cw::fragf;
using cw::util::ssprintf;
std::vector<cw::fragment *> fragments;
if(show_all ||
(show_unchanged && final.get_num_broken() != 0) ||
final.get_num_broken() != initial.get_num_broken())
{
std::string change =
ssprintf(ngettext("%d broken [%+d]",
"%d broken [%+d]",
final.get_num_broken()),
final.get_num_broken(),
final.get_num_broken() - initial.get_num_broken());
fragments.push_back(cw::text_fragment(change));
}
if(show_all ||
(show_unchanged && final.get_num_upgradable() != 0) ||
final.get_num_upgradable() != initial.get_num_upgradable())
{
std::string change =
ssprintf(ngettext("%d update [%+d]",
"%d updates [%+d]",
final.get_num_upgradable()),
final.get_num_upgradable(),
final.get_num_upgradable() - initial.get_num_upgradable());
fragments.push_back(cw::text_fragment(change));
}
if(show_all ||
(show_unchanged && final.get_num_new() != 0) ||
final.get_num_new() != initial.get_num_new())
{
std::string change =
ssprintf(ngettext("%d new [%+d]",
"%d new [%+d]",
final.get_num_new()),
final.get_num_new(),
final.get_num_new() - initial.get_num_new());
fragments.push_back(cw::text_fragment(change));
}
std::vector<cw::fragment *> output_fragments;
if(fragments.size() > 0)
output_fragments.push_back(fragf(_("Current status: %F."),
cw::join_fragments(fragments, L", ")));
std::cout << std::endl;
std::vector<std::string> new_obsolete;
std::set_difference(final.get_obsolete().begin(), final.get_obsolete().end(),
initial.get_obsolete().begin(), initial.get_obsolete().end(),
std::back_insert_iterator<std::vector<std::string> >(new_obsolete));
const unsigned int obsolete_list_limit = aptcfg->FindI(PACKAGE "::Max-Obsolete-List-Length", 50);
if(new_obsolete.size() > obsolete_list_limit)
{
output_fragments.push_back(cw::text_fragment(ssprintf(ngettext("There is %d newly obsolete package.",
"There are %d newly obsolete packages.",
new_obsolete.size()),
(int)new_obsolete.size())));
}
else if(new_obsolete.size() > 0)
{
std::vector<cw::fragment *> package_name_fragments;
for(std::vector<std::string>::const_iterator it =
new_obsolete.begin(); it != new_obsolete.end(); ++it)
package_name_fragments.push_back(cw::text_fragment(*it));
output_fragments.push_back(cw::dropbox(cw::text_fragment(ssprintf(ngettext("There is %d newly obsolete package: ",
"There are %d newly obsolete packages: ",
new_obsolete.size()),
(int)new_obsolete.size())),
wrapbox(cw::join_fragments(package_name_fragments, L", "))));
}
if(output_fragments.size() > 0)
{
cw::fragment *f = join_fragments(output_fragments, L"\n");
update_screen_width();
std::cout << f->layout(screen_width, screen_width, cw::style());
delete f;
}
}
}
download_manager::result cmdline_do_download(download_manager *m,
int verbose)
{
stats initial_stats(0, 0, 0, std::set<std::string>());
OpTextProgress progress(aptcfg->FindI("Quiet", 0));
if(aptcfg->FindI("Quiet", 0) == 0)
{
// This does exactly what we want: nothing if the cache is
// already loaded, and loads the cache with a minimum of frills
// otherwise.
OpProgress tmpProgress;
// Dump errors so we don't spuriously think we failed.
_error->DumpErrors();
apt_load_cache(&tmpProgress, false, NULL);
initial_stats = compute_apt_stats();
}
std::auto_ptr<download_signal_log> log(gen_cmdline_download_progress());
// Dump errors here because prepare() might check for pending errors
// and think something failed.
_error->DumpErrors();
if(!m->prepare(progress, *log.get(), log.get()))
return download_manager::failure;
download_manager::result finish_res;
do
{
pkgAcquire::RunResult download_res = m->do_download();
finish_res = m->finish(download_res, progress);
}
while(finish_res == download_manager::do_again);
stats final_stats(0, 0, 0, std::set<std::string>());
if(aptcfg->FindI("Quiet", 0) == 0)
{
OpProgress tmpProgress;
apt_load_cache(&tmpProgress, false, NULL);
final_stats = compute_apt_stats();
show_stats_change(initial_stats, final_stats,
verbose >= 1, verbose >= 2);
}
return finish_res;
}
bool cmdline_is_search_pattern(const std::string &s)
{
return s.find_first_of("~?") != s.npos;
}
namespace aptitude
{
namespace cmdline
{
namespace
{
source_package find_source_package(const std::string &source_name,
const std::string &source_version)
{
if(apt_source_list == NULL)
return NULL;
pkgSrcRecords records(*apt_source_list);
records.Restart();
pkgSrcRecords::Parser *parser = records.Find(source_name.c_str());
while(parser != NULL && parser->Version() != source_version)
parser = records.Find(source_name.c_str());
return parser;
}
// Find the most recent source package for the given name.
source_package find_source_package(const std::string &source_name)
{
if(apt_source_list == NULL)
return NULL;
pkgSrcRecords records(*apt_source_list);
records.Restart();
pkgSrcRecords::Parser *parser = records.Find(source_name.c_str());
source_package rval = parser;
while(parser != NULL)
{
if(_system->VS->CmpVersion(rval.get_version(), parser->Version()) < 0)
rval = parser;
parser = records.Find(source_name.c_str());
}
return rval;
}
}
source_package::source_package()
{
}
source_package::source_package(pkgSrcRecords::Parser *parser)
{
if(parser != NULL)
{
package = parser->Package();
version = parser->Version();
maintainer = parser->Maintainer();
section = parser->Section();
if(parser->Binaries() != NULL)
{
for(const char **b = parser->Binaries(); *b != NULL; ++b)
binaries.push_back(*b);
}
parser->BuildDepends(build_deps, false);
}
}
source_package find_source_by_archive(const std::string &source_name,
const std::string &archive)
{
if(apt_source_list == NULL)
return NULL;
for(pkgSourceList::const_iterator i = apt_source_list->begin();
i != apt_source_list->end(); ++i)
{
if((*i)->GetDist() != archive)
continue;
vector<pkgIndexFile *> *indexes = (*i)->GetIndexFiles();
for(vector<pkgIndexFile *>::const_iterator j = indexes->begin();
j != indexes->end(); ++j)
{
std::auto_ptr<pkgSrcRecords::Parser> p((*j)->CreateSrcParser());
if(_error->PendingError())
return source_package();
if(p.get() != 0)
{
// Step through the file until we reach the end or find
// the package:
while(p.get()->Step() == true)
{
if(_error->PendingError() == true)
return source_package();
if(p.get()->Package() == source_name)
return source_package(p.get());
}
}
}
}
return source_package();
}
source_package find_source_package(const std::string &package,
cmdline_version_source version_source,
const std::string &version_source_string_orig)
{
// This will be set below to the package archive if necessary.
std::string version_source_string(version_source_string_orig);
if(apt_cache_file == NULL || apt_package_records == NULL || apt_source_list == NULL)
return NULL;
string default_release = aptcfg->Find("APT::Default-Release");
if(version_source == cmdline_version_cand && !default_release.empty())
{
version_source = cmdline_version_archive;
version_source_string = default_release;
}
pkgCache::PkgIterator pkg = (*apt_cache_file)->FindPkg(package);
std::string source_package_name = package;
source_package rval;
// First look for a source of the given name.
switch(version_source)
{
case cmdline_version_cand:
rval = find_source_package(source_package_name);
break;
case cmdline_version_curr_or_cand:
break;
// Do this later since we need to refer to the installed
// version.
case cmdline_version_archive:
_error->DumpErrors();
rval = find_source_by_archive(source_package_name, version_source_string);
break;
case cmdline_version_version:
rval = find_source_package(source_package_name, version_source_string);
break;
}
if(rval.valid())
return rval;
// If no source of that name exists, try to find a real package
// and use its source.
if(!pkg.end())
{
pkgCache::VerIterator ver = cmdline_find_ver(pkg,
version_source,
version_source_string);
if(!ver.end())
{
pkgRecords::Parser &rec =
apt_package_records->Lookup(ver.FileList());
if(!rec.SourcePkg().empty())
source_package_name = rec.SourcePkg();
if(version_source == cmdline_version_version)
{
const std::string source_version =
rec.SourceVer().empty() ? ver.VerStr() : rec.SourceVer();
rval = find_source_package(source_package_name, source_version);
}
else if(version_source == cmdline_version_curr_or_cand)
{
const std::string source_version =
rec.SourceVer().empty() ? ver.VerStr() : rec.SourceVer();
rval = find_source_package(source_package_name, source_version);
}
}
// Last-ditch effort: if no matching version was found but
// a source package can be found, use that and try again below.
else if(!pkg.VersionList().end())
{
pkgRecords::Parser &rec =
apt_package_records->Lookup(pkg.VersionList().FileList());
if(!rec.SourcePkg().empty())
source_package_name = rec.SourcePkg();
}
}
if(!rval.valid())
{
switch(version_source)
{
case cmdline_version_cand:
rval = find_source_package(source_package_name);
break;
case cmdline_version_curr_or_cand:
break; // We would have tried already if it was possible.
case cmdline_version_archive:
_error->DumpErrors();
rval = find_source_by_archive(source_package_name, version_source_string);
break;
case cmdline_version_version:
rval = find_source_package(source_package_name, version_source_string);
break;
}
}
return rval;
}
void apply_user_tags(const std::vector<tag_application> &user_tags)
{
using namespace matching;
for(pkgCache::PkgIterator pkg = (*apt_cache_file)->PkgBegin();
!pkg.end(); ++pkg)
{
for(std::vector<tag_application>::const_iterator it =
user_tags.begin(); it != user_tags.end(); ++it)
{
bool applicable = false;
if(it->get_matcher() != NULL)
{
if(matching::apply_matcher(it->get_matcher(), pkg,
*apt_cache_file,
*apt_package_records))
applicable = true;
}
else
{
const pkgDepCache::StateCache &state = (*apt_cache_file)[pkg];
// Perhaps we should somehow filter out automatic
// changes here, but that's hard and might be
// unpredictable (which would make the user sad).
// Instead we just add the tag to all packages that
// are being modified.
if(!state.Keep())
applicable = true;
}
if(applicable)
{
if(it->get_is_add())
(*apt_cache_file)->attach_user_tag(pkg, it->get_tag(), NULL);
else
(*apt_cache_file)->detach_user_tag(pkg, it->get_tag(), NULL);
}
}
}
}
std::wstring de_columnize(const cwidget::config::column_definition_list &columns,
cwidget::config::column_generator &columnizer,
cwidget::config::column_parameters &p)
{
using namespace cwidget::config;
// TODO: this should move into cwidget in the future, as a new
// mode of operation for layout_columns().
std::wstring output;
for(column_definition_list::const_iterator it = columns.begin();
it != columns.end();
++it)
{
if(it->type == column_definition::COLUMN_LITERAL)
output += it->arg;
else
{
eassert(it->type == column_definition::COLUMN_GENERATED ||
it->type == column_definition::COLUMN_PARAM);
if(it->type == column_definition::COLUMN_GENERATED)
{
cwidget::column_disposition disp = columnizer.setup_column(it->ival);
output += disp.text;
}
else
{
if(p.param_count() <= it->ival)
output += L"###";
else
output += p.get_param(it->ival);
}
}
}
return output;
}
}
}
|