* apt-pkg/cacheiterators.h:
[ntk/apt.git] / cmdline / apt-get.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz Exp $
4 /* ######################################################################
5
6 apt-get - Cover for dpkg
7
8 This is an allout cover for dpkg implementing a safer front end. It is
9 based largely on libapt-pkg.
10
11 The syntax is different,
12 apt-get [opt] command [things]
13 Where command is:
14 update - Resyncronize the package files from their sources
15 upgrade - Smart-Download the newest versions of all packages
16 dselect-upgrade - Follows dselect's changes to the Status: field
17 and installes new and removes old packages
18 dist-upgrade - Powerfull upgrader designed to handle the issues with
19 a new distribution.
20 install - Download and install a given package (by name, not by .deb)
21 check - Update the package cache and check for broken packages
22 clean - Erase the .debs downloaded to /var/cache/apt/archives and
23 the partial dir too
24
25 ##################################################################### */
26 /*}}}*/
27 // Include Files /*{{{*/
28 #include <config.h>
29
30 #include <apt-pkg/aptconfiguration.h>
31 #include <apt-pkg/error.h>
32 #include <apt-pkg/cmndline.h>
33 #include <apt-pkg/init.h>
34 #include <apt-pkg/depcache.h>
35 #include <apt-pkg/sourcelist.h>
36 #include <apt-pkg/algorithms.h>
37 #include <apt-pkg/acquire-item.h>
38 #include <apt-pkg/strutl.h>
39 #include <apt-pkg/fileutl.h>
40 #include <apt-pkg/clean.h>
41 #include <apt-pkg/srcrecords.h>
42 #include <apt-pkg/version.h>
43 #include <apt-pkg/cachefile.h>
44 #include <apt-pkg/cacheset.h>
45 #include <apt-pkg/sptr.h>
46 #include <apt-pkg/md5.h>
47 #include <apt-pkg/versionmatch.h>
48 #include <apt-pkg/progress.h>
49 #include <apt-pkg/pkgsystem.h>
50 #include <apt-pkg/pkgrecords.h>
51 #include <apt-pkg/indexfile.h>
52
53 #include "acqprogress.h"
54
55 #include <set>
56 #include <locale.h>
57 #include <langinfo.h>
58 #include <fstream>
59 #include <termios.h>
60 #include <sys/ioctl.h>
61 #include <sys/stat.h>
62 #include <sys/statfs.h>
63 #include <sys/statvfs.h>
64 #include <signal.h>
65 #include <unistd.h>
66 #include <stdio.h>
67 #include <errno.h>
68 #include <regex.h>
69 #include <sys/wait.h>
70 #include <sstream>
71
72 #include <apti18n.h>
73 /*}}}*/
74
75 #define RAMFS_MAGIC 0x858458f6
76
77 using namespace std;
78
79 ostream c0out(0);
80 ostream c1out(0);
81 ostream c2out(0);
82 ofstream devnull("/dev/null");
83 unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
84
85 // class CacheFile - Cover class for some dependency cache functions /*{{{*/
86 // ---------------------------------------------------------------------
87 /* */
88 class CacheFile : public pkgCacheFile
89 {
90 static pkgCache *SortCache;
91 static int NameComp(const void *a,const void *b);
92
93 public:
94 pkgCache::Package **List;
95
96 void Sort();
97 bool CheckDeps(bool AllowBroken = false);
98 bool BuildCaches(bool WithLock = true)
99 {
100 OpTextProgress Prog(*_config);
101 if (pkgCacheFile::BuildCaches(&Prog,WithLock) == false)
102 return false;
103 return true;
104 }
105 bool Open(bool WithLock = true)
106 {
107 OpTextProgress Prog(*_config);
108 if (pkgCacheFile::Open(&Prog,WithLock) == false)
109 return false;
110 Sort();
111
112 return true;
113 };
114 bool OpenForInstall()
115 {
116 if (_config->FindB("APT::Get::Print-URIs") == true)
117 return Open(false);
118 else
119 return Open(true);
120 }
121 CacheFile() : List(0) {};
122 ~CacheFile() {
123 delete[] List;
124 }
125 };
126 /*}}}*/
127
128 // YnPrompt - Yes No Prompt. /*{{{*/
129 // ---------------------------------------------------------------------
130 /* Returns true on a Yes.*/
131 bool YnPrompt(bool Default=true)
132 {
133 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
134 {
135 c1out << _("Y") << endl;
136 return true;
137 }
138 else if (_config->FindB("APT::Get::Assume-No",false) == true)
139 {
140 c1out << _("N") << endl;
141 return false;
142 }
143
144 char response[1024] = "";
145 cin.getline(response, sizeof(response));
146
147 if (!cin)
148 return false;
149
150 if (strlen(response) == 0)
151 return Default;
152
153 regex_t Pattern;
154 int Res;
155
156 Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
157 REG_EXTENDED|REG_ICASE|REG_NOSUB);
158
159 if (Res != 0) {
160 char Error[300];
161 regerror(Res,&Pattern,Error,sizeof(Error));
162 return _error->Error(_("Regex compilation error - %s"),Error);
163 }
164
165 Res = regexec(&Pattern, response, 0, NULL, 0);
166 if (Res == 0)
167 return true;
168 return false;
169 }
170 /*}}}*/
171 // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
172 // ---------------------------------------------------------------------
173 /* Returns true on a Yes.*/
174 bool AnalPrompt(const char *Text)
175 {
176 char Buf[1024];
177 cin.getline(Buf,sizeof(Buf));
178 if (strcmp(Buf,Text) == 0)
179 return true;
180 return false;
181 }
182 /*}}}*/
183 // ShowList - Show a list /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This prints out a string of space separated words with a title and
186 a two space indent line wraped to the current screen width. */
187 bool ShowList(ostream &out,string Title,string List,string VersionsList)
188 {
189 if (List.empty() == true)
190 return true;
191 // trim trailing space
192 int NonSpace = List.find_last_not_of(' ');
193 if (NonSpace != -1)
194 {
195 List = List.erase(NonSpace + 1);
196 if (List.empty() == true)
197 return true;
198 }
199
200 // Acount for the leading space
201 int ScreenWidth = ::ScreenWidth - 3;
202
203 out << Title << endl;
204 string::size_type Start = 0;
205 string::size_type VersionsStart = 0;
206 while (Start < List.size())
207 {
208 if(_config->FindB("APT::Get::Show-Versions",false) == true &&
209 VersionsList.size() > 0) {
210 string::size_type End;
211 string::size_type VersionsEnd;
212
213 End = List.find(' ',Start);
214 VersionsEnd = VersionsList.find('\n', VersionsStart);
215
216 out << " " << string(List,Start,End - Start) << " (" <<
217 string(VersionsList,VersionsStart,VersionsEnd - VersionsStart) <<
218 ")" << endl;
219
220 if (End == string::npos || End < Start)
221 End = Start + ScreenWidth;
222
223 Start = End + 1;
224 VersionsStart = VersionsEnd + 1;
225 } else {
226 string::size_type End;
227
228 if (Start + ScreenWidth >= List.size())
229 End = List.size();
230 else
231 End = List.rfind(' ',Start+ScreenWidth);
232
233 if (End == string::npos || End < Start)
234 End = Start + ScreenWidth;
235 out << " " << string(List,Start,End - Start) << endl;
236 Start = End + 1;
237 }
238 }
239
240 return false;
241 }
242 /*}}}*/
243 // ShowBroken - Debugging aide /*{{{*/
244 // ---------------------------------------------------------------------
245 /* This prints out the names of all the packages that are broken along
246 with the name of each each broken dependency and a quite version
247 description.
248
249 The output looks like:
250 The following packages have unmet dependencies:
251 exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
252 Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
253 Depends: libsasl7 but it is not going to be installed
254 */
255 void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
256 {
257 out << _("The following packages have unmet dependencies:") << endl;
258 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
259 {
260 pkgCache::PkgIterator I(Cache,Cache.List[J]);
261
262 if (Now == true)
263 {
264 if (Cache[I].NowBroken() == false)
265 continue;
266 }
267 else
268 {
269 if (Cache[I].InstBroken() == false)
270 continue;
271 }
272
273 // Print out each package and the failed dependencies
274 out << " " << I.FullName(true) << " :";
275 unsigned const Indent = I.FullName(true).size() + 3;
276 bool First = true;
277 pkgCache::VerIterator Ver;
278
279 if (Now == true)
280 Ver = I.CurrentVer();
281 else
282 Ver = Cache[I].InstVerIter(Cache);
283
284 if (Ver.end() == true)
285 {
286 out << endl;
287 continue;
288 }
289
290 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
291 {
292 // Compute a single dependency element (glob or)
293 pkgCache::DepIterator Start;
294 pkgCache::DepIterator End;
295 D.GlobOr(Start,End); // advances D
296
297 if (Cache->IsImportantDep(End) == false)
298 continue;
299
300 if (Now == true)
301 {
302 if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
303 continue;
304 }
305 else
306 {
307 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
308 continue;
309 }
310
311 bool FirstOr = true;
312 while (1)
313 {
314 if (First == false)
315 for (unsigned J = 0; J != Indent; J++)
316 out << ' ';
317 First = false;
318
319 if (FirstOr == false)
320 {
321 for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
322 out << ' ';
323 }
324 else
325 out << ' ' << End.DepType() << ": ";
326 FirstOr = false;
327
328 out << Start.TargetPkg().FullName(true);
329
330 // Show a quick summary of the version requirements
331 if (Start.TargetVer() != 0)
332 out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
333
334 /* Show a summary of the target package if possible. In the case
335 of virtual packages we show nothing */
336 pkgCache::PkgIterator Targ = Start.TargetPkg();
337 if (Targ->ProvidesList == 0)
338 {
339 out << ' ';
340 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
341 if (Now == true)
342 Ver = Targ.CurrentVer();
343
344 if (Ver.end() == false)
345 {
346 if (Now == true)
347 ioprintf(out,_("but %s is installed"),Ver.VerStr());
348 else
349 ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
350 }
351 else
352 {
353 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
354 {
355 if (Targ->ProvidesList == 0)
356 out << _("but it is not installable");
357 else
358 out << _("but it is a virtual package");
359 }
360 else
361 out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
362 }
363 }
364
365 if (Start != End)
366 out << _(" or");
367 out << endl;
368
369 if (Start == End)
370 break;
371 ++Start;
372 }
373 }
374 }
375 }
376 /*}}}*/
377 // ShowNew - Show packages to newly install /*{{{*/
378 // ---------------------------------------------------------------------
379 /* */
380 void ShowNew(ostream &out,CacheFile &Cache)
381 {
382 /* Print out a list of packages that are going to be installed extra
383 to what the user asked */
384 string List;
385 string VersionsList;
386 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
387 {
388 pkgCache::PkgIterator I(Cache,Cache.List[J]);
389 if (Cache[I].NewInstall() == true) {
390 List += I.FullName(true) + " ";
391 VersionsList += string(Cache[I].CandVersion) + "\n";
392 }
393 }
394
395 ShowList(out,_("The following NEW packages will be installed:"),List,VersionsList);
396 }
397 /*}}}*/
398 // ShowDel - Show packages to delete /*{{{*/
399 // ---------------------------------------------------------------------
400 /* */
401 void ShowDel(ostream &out,CacheFile &Cache)
402 {
403 /* Print out a list of packages that are going to be removed extra
404 to what the user asked */
405 string List;
406 string VersionsList;
407 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
408 {
409 pkgCache::PkgIterator I(Cache,Cache.List[J]);
410 if (Cache[I].Delete() == true)
411 {
412 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
413 List += I.FullName(true) + "* ";
414 else
415 List += I.FullName(true) + " ";
416
417 VersionsList += string(Cache[I].CandVersion)+ "\n";
418 }
419 }
420
421 ShowList(out,_("The following packages will be REMOVED:"),List,VersionsList);
422 }
423 /*}}}*/
424 // ShowKept - Show kept packages /*{{{*/
425 // ---------------------------------------------------------------------
426 /* */
427 void ShowKept(ostream &out,CacheFile &Cache)
428 {
429 string List;
430 string VersionsList;
431 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
432 {
433 pkgCache::PkgIterator I(Cache,Cache.List[J]);
434
435 // Not interesting
436 if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
437 I->CurrentVer == 0 || Cache[I].Delete() == true)
438 continue;
439
440 List += I.FullName(true) + " ";
441 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
442 }
443 ShowList(out,_("The following packages have been kept back:"),List,VersionsList);
444 }
445 /*}}}*/
446 // ShowUpgraded - Show upgraded packages /*{{{*/
447 // ---------------------------------------------------------------------
448 /* */
449 void ShowUpgraded(ostream &out,CacheFile &Cache)
450 {
451 string List;
452 string VersionsList;
453 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
454 {
455 pkgCache::PkgIterator I(Cache,Cache.List[J]);
456
457 // Not interesting
458 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
459 continue;
460
461 List += I.FullName(true) + " ";
462 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
463 }
464 ShowList(out,_("The following packages will be upgraded:"),List,VersionsList);
465 }
466 /*}}}*/
467 // ShowDowngraded - Show downgraded packages /*{{{*/
468 // ---------------------------------------------------------------------
469 /* */
470 bool ShowDowngraded(ostream &out,CacheFile &Cache)
471 {
472 string List;
473 string VersionsList;
474 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
475 {
476 pkgCache::PkgIterator I(Cache,Cache.List[J]);
477
478 // Not interesting
479 if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
480 continue;
481
482 List += I.FullName(true) + " ";
483 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
484 }
485 return ShowList(out,_("The following packages will be DOWNGRADED:"),List,VersionsList);
486 }
487 /*}}}*/
488 // ShowHold - Show held but changed packages /*{{{*/
489 // ---------------------------------------------------------------------
490 /* */
491 bool ShowHold(ostream &out,CacheFile &Cache)
492 {
493 string List;
494 string VersionsList;
495 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
496 {
497 pkgCache::PkgIterator I(Cache,Cache.List[J]);
498 if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
499 I->SelectedState == pkgCache::State::Hold) {
500 List += I.FullName(true) + " ";
501 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
502 }
503 }
504
505 return ShowList(out,_("The following held packages will be changed:"),List,VersionsList);
506 }
507 /*}}}*/
508 // ShowEssential - Show an essential package warning /*{{{*/
509 // ---------------------------------------------------------------------
510 /* This prints out a warning message that is not to be ignored. It shows
511 all essential packages and their dependents that are to be removed.
512 It is insanely risky to remove the dependents of an essential package! */
513 bool ShowEssential(ostream &out,CacheFile &Cache)
514 {
515 string List;
516 string VersionsList;
517 bool *Added = new bool[Cache->Head().PackageCount];
518 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
519 Added[I] = false;
520
521 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
522 {
523 pkgCache::PkgIterator I(Cache,Cache.List[J]);
524 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
525 (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
526 continue;
527
528 // The essential package is being removed
529 if (Cache[I].Delete() == true)
530 {
531 if (Added[I->ID] == false)
532 {
533 Added[I->ID] = true;
534 List += I.FullName(true) + " ";
535 //VersionsList += string(Cache[I].CurVersion) + "\n"; ???
536 }
537 }
538 else
539 continue;
540
541 if (I->CurrentVer == 0)
542 continue;
543
544 // Print out any essential package depenendents that are to be removed
545 for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; ++D)
546 {
547 // Skip everything but depends
548 if (D->Type != pkgCache::Dep::PreDepends &&
549 D->Type != pkgCache::Dep::Depends)
550 continue;
551
552 pkgCache::PkgIterator P = D.SmartTargetPkg();
553 if (Cache[P].Delete() == true)
554 {
555 if (Added[P->ID] == true)
556 continue;
557 Added[P->ID] = true;
558
559 char S[300];
560 snprintf(S,sizeof(S),_("%s (due to %s) "),P.FullName(true).c_str(),I.FullName(true).c_str());
561 List += S;
562 //VersionsList += "\n"; ???
563 }
564 }
565 }
566
567 delete [] Added;
568 return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
569 "This should NOT be done unless you know exactly what you are doing!"),List,VersionsList);
570 }
571
572 /*}}}*/
573 // Stats - Show some statistics /*{{{*/
574 // ---------------------------------------------------------------------
575 /* */
576 void Stats(ostream &out,pkgDepCache &Dep)
577 {
578 unsigned long Upgrade = 0;
579 unsigned long Downgrade = 0;
580 unsigned long Install = 0;
581 unsigned long ReInstall = 0;
582 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; ++I)
583 {
584 if (Dep[I].NewInstall() == true)
585 Install++;
586 else
587 {
588 if (Dep[I].Upgrade() == true)
589 Upgrade++;
590 else
591 if (Dep[I].Downgrade() == true)
592 Downgrade++;
593 }
594
595 if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
596 ReInstall++;
597 }
598
599 ioprintf(out,_("%lu upgraded, %lu newly installed, "),
600 Upgrade,Install);
601
602 if (ReInstall != 0)
603 ioprintf(out,_("%lu reinstalled, "),ReInstall);
604 if (Downgrade != 0)
605 ioprintf(out,_("%lu downgraded, "),Downgrade);
606
607 ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
608 Dep.DelCount(),Dep.KeepCount());
609
610 if (Dep.BadCount() != 0)
611 ioprintf(out,_("%lu not fully installed or removed.\n"),
612 Dep.BadCount());
613 }
614 /*}}}*/
615 // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
616 class CacheSetHelperAPTGet : public APT::CacheSetHelper {
617 /** \brief stream message should be printed to */
618 std::ostream &out;
619 /** \brief were things like Task or RegEx used to select packages? */
620 bool explicitlyNamed;
621
622 APT::PackageSet virtualPkgs;
623
624 public:
625 std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
626
627 CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
628 explicitlyNamed = true;
629 }
630
631 virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, string const &pattern) {
632 ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
633 Pkg.FullName(true).c_str(), pattern.c_str());
634 explicitlyNamed = false;
635 }
636 virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, string const &pattern) {
637 ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
638 Pkg.FullName(true).c_str(), pattern.c_str());
639 explicitlyNamed = false;
640 }
641 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
642 string const &ver, bool const verIsRel) {
643 if (ver == Ver.VerStr())
644 return;
645 selectedByRelease.push_back(make_pair(Ver, ver));
646 }
647
648 bool showVirtualPackageErrors(pkgCacheFile &Cache) {
649 if (virtualPkgs.empty() == true)
650 return true;
651 for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
652 Pkg != virtualPkgs.end(); ++Pkg) {
653 if (Pkg->ProvidesList != 0) {
654 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
655 Pkg.FullName(true).c_str());
656
657 pkgCache::PrvIterator I = Pkg.ProvidesList();
658 unsigned short provider = 0;
659 for (; I.end() == false; ++I) {
660 pkgCache::PkgIterator Pkg = I.OwnerPkg();
661
662 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
663 c1out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
664 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
665 c1out << _(" [Installed]");
666 c1out << endl;
667 ++provider;
668 }
669 }
670 // if we found no candidate which provide this package, show non-candidates
671 if (provider == 0)
672 for (I = Pkg.ProvidesList(); I.end() == false; ++I)
673 c1out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
674 << _(" [Not candidate version]") << endl;
675 else
676 out << _("You should explicitly select one to install.") << endl;
677 } else {
678 ioprintf(c1out,
679 _("Package %s is not available, but is referred to by another package.\n"
680 "This may mean that the package is missing, has been obsoleted, or\n"
681 "is only available from another source\n"),Pkg.FullName(true).c_str());
682
683 string List;
684 string VersionsList;
685 SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
686 memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
687 for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
688 Dep.end() == false; ++Dep) {
689 if (Dep->Type != pkgCache::Dep::Replaces)
690 continue;
691 if (Seen[Dep.ParentPkg()->ID] == true)
692 continue;
693 Seen[Dep.ParentPkg()->ID] = true;
694 List += Dep.ParentPkg().FullName(true) + " ";
695 //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
696 }
697 ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
698 }
699 c1out << std::endl;
700 }
701 return false;
702 }
703
704 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
705 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::CANDIDATE);
706 if (verset.empty() == false)
707 return *(verset.begin());
708 else if (ShowError == true) {
709 _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
710 virtualPkgs.insert(Pkg);
711 }
712 return pkgCache::VerIterator(Cache, 0);
713 }
714
715 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
716 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::NEWEST);
717 if (verset.empty() == false)
718 return *(verset.begin());
719 if (ShowError == true)
720 ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
721 return pkgCache::VerIterator(Cache, 0);
722 }
723
724 APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
725 APT::VersionSet::Version const &select) {
726 /* This is a pure virtual package and there is a single available
727 candidate providing it. */
728 if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
729 return APT::VersionSet();
730
731 pkgCache::PkgIterator Prov;
732 bool found_one = false;
733 for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
734 pkgCache::VerIterator const PVer = P.OwnerVer();
735 pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
736
737 /* Ignore versions that are not a candidate. */
738 if (Cache[PPkg].CandidateVer != PVer)
739 continue;
740
741 if (found_one == false) {
742 Prov = PPkg;
743 found_one = true;
744 } else if (PPkg != Prov) {
745 // same group, so it's a foreign package
746 if (PPkg->Group == Prov->Group) {
747 // do we already have the requested arch?
748 if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
749 strcmp(Prov.Arch(), "all") == 0 ||
750 unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
751 continue;
752 // see which architecture we prefer more and switch to it
753 std::vector<std::string> archs = APT::Configuration::getArchitectures();
754 if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
755 Prov = PPkg;
756 continue;
757 }
758 found_one = false; // we found at least two
759 break;
760 }
761 }
762
763 if (found_one == true) {
764 ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
765 Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
766 return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
767 }
768 return APT::VersionSet();
769 }
770
771 inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
772
773 };
774 /*}}}*/
775 // TryToInstall - Mark a package for installation /*{{{*/
776 struct TryToInstall {
777 pkgCacheFile* Cache;
778 pkgProblemResolver* Fix;
779 bool FixBroken;
780 unsigned long AutoMarkChanged;
781 APT::PackageSet doAutoInstallLater;
782
783 TryToInstall(pkgCacheFile &Cache, pkgProblemResolver *PM, bool const FixBroken) : Cache(&Cache), Fix(PM),
784 FixBroken(FixBroken), AutoMarkChanged(0) {};
785
786 void operator() (pkgCache::VerIterator const &Ver) {
787 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
788
789 Cache->GetDepCache()->SetCandidateVersion(Ver);
790 pkgDepCache::StateCache &State = (*Cache)[Pkg];
791
792 // Handle the no-upgrade case
793 if (_config->FindB("APT::Get::upgrade",true) == false && Pkg->CurrentVer != 0)
794 ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
795 Pkg.FullName(true).c_str());
796 // Ignore request for install if package would be new
797 else if (_config->FindB("APT::Get::Only-Upgrade", false) == true && Pkg->CurrentVer == 0)
798 ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"),
799 Pkg.FullName(true).c_str());
800 else {
801 if (Fix != NULL) {
802 Fix->Clear(Pkg);
803 Fix->Protect(Pkg);
804 }
805 Cache->GetDepCache()->MarkInstall(Pkg,false);
806
807 if (State.Install() == false) {
808 if (_config->FindB("APT::Get::ReInstall",false) == true) {
809 if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
810 ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
811 Pkg.FullName(true).c_str());
812 else
813 Cache->GetDepCache()->SetReInstall(Pkg, true);
814 } else
815 ioprintf(c1out,_("%s is already the newest version.\n"),
816 Pkg.FullName(true).c_str());
817 }
818
819 // Install it with autoinstalling enabled (if we not respect the minial
820 // required deps or the policy)
821 if (FixBroken == false)
822 doAutoInstallLater.insert(Pkg);
823 }
824
825 // see if we need to fix the auto-mark flag
826 // e.g. apt-get install foo
827 // where foo is marked automatic
828 if (State.Install() == false &&
829 (State.Flags & pkgCache::Flag::Auto) &&
830 _config->FindB("APT::Get::ReInstall",false) == false &&
831 _config->FindB("APT::Get::Only-Upgrade",false) == false &&
832 _config->FindB("APT::Get::Download-Only",false) == false)
833 {
834 ioprintf(c1out,_("%s set to manually installed.\n"),
835 Pkg.FullName(true).c_str());
836 Cache->GetDepCache()->MarkAuto(Pkg,false);
837 AutoMarkChanged++;
838 }
839 }
840
841 bool propergateReleaseCandiateSwitching(std::list<std::pair<pkgCache::VerIterator, std::string> > start, std::ostream &out)
842 {
843 for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
844 s != start.end(); ++s)
845 Cache->GetDepCache()->SetCandidateVersion(s->first);
846
847 bool Success = true;
848 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
849 for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
850 s != start.end(); ++s)
851 {
852 Changed.push_back(std::make_pair(s->first, pkgCache::VerIterator(*Cache)));
853 // We continue here even if it failed to enhance the ShowBroken output
854 Success &= Cache->GetDepCache()->SetCandidateRelease(s->first, s->second, Changed);
855 }
856 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
857 c != Changed.end(); ++c)
858 {
859 if (c->second.end() == true)
860 ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
861 c->first.VerStr(), c->first.RelStr().c_str(), c->first.ParentPkg().FullName(true).c_str());
862 else if (c->first.ParentPkg()->Group != c->second.ParentPkg()->Group)
863 {
864 pkgCache::VerIterator V = (*Cache)[c->first.ParentPkg()].CandidateVerIter(*Cache);
865 ioprintf(out, _("Selected version '%s' (%s) for '%s' because of '%s'\n"), V.VerStr(),
866 V.RelStr().c_str(), V.ParentPkg().FullName(true).c_str(), c->second.ParentPkg().FullName(true).c_str());
867 }
868 }
869 return Success;
870 }
871
872 void doAutoInstall() {
873 for (APT::PackageSet::const_iterator P = doAutoInstallLater.begin();
874 P != doAutoInstallLater.end(); ++P) {
875 pkgDepCache::StateCache &State = (*Cache)[P];
876 if (State.InstBroken() == false && State.InstPolicyBroken() == false)
877 continue;
878 Cache->GetDepCache()->MarkInstall(P, true);
879 }
880 doAutoInstallLater.clear();
881 }
882 };
883 /*}}}*/
884 // TryToRemove - Mark a package for removal /*{{{*/
885 struct TryToRemove {
886 pkgCacheFile* Cache;
887 pkgProblemResolver* Fix;
888 bool PurgePkgs;
889
890 TryToRemove(pkgCacheFile &Cache, pkgProblemResolver *PM) : Cache(&Cache), Fix(PM),
891 PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
892
893 void operator() (pkgCache::VerIterator const &Ver)
894 {
895 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
896
897 if (Fix != NULL)
898 {
899 Fix->Clear(Pkg);
900 Fix->Protect(Pkg);
901 Fix->Remove(Pkg);
902 }
903
904 if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
905 (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
906 {
907 ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.FullName(true).c_str());
908 // MarkInstall refuses to install packages on hold
909 Pkg->SelectedState = pkgCache::State::Hold;
910 }
911 else
912 Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs);
913 }
914 };
915 /*}}}*/
916 // CacheFile::NameComp - QSort compare by name /*{{{*/
917 // ---------------------------------------------------------------------
918 /* */
919 pkgCache *CacheFile::SortCache = 0;
920 int CacheFile::NameComp(const void *a,const void *b)
921 {
922 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
923 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
924
925 const pkgCache::Package &A = **(pkgCache::Package **)a;
926 const pkgCache::Package &B = **(pkgCache::Package **)b;
927
928 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
929 }
930 /*}}}*/
931 // CacheFile::Sort - Sort by name /*{{{*/
932 // ---------------------------------------------------------------------
933 /* */
934 void CacheFile::Sort()
935 {
936 delete [] List;
937 List = new pkgCache::Package *[Cache->Head().PackageCount];
938 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
939 pkgCache::PkgIterator I = Cache->PkgBegin();
940 for (;I.end() != true; ++I)
941 List[I->ID] = I;
942
943 SortCache = *this;
944 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
945 }
946 /*}}}*/
947 // CacheFile::CheckDeps - Open the cache file /*{{{*/
948 // ---------------------------------------------------------------------
949 /* This routine generates the caches and then opens the dependency cache
950 and verifies that the system is OK. */
951 bool CacheFile::CheckDeps(bool AllowBroken)
952 {
953 bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
954
955 if (_error->PendingError() == true)
956 return false;
957
958 // Check that the system is OK
959 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
960 return _error->Error("Internal error, non-zero counts");
961
962 // Apply corrections for half-installed packages
963 if (pkgApplyStatus(*DCache) == false)
964 return false;
965
966 if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
967 {
968 FixBroken = true;
969 if ((DCache->PolicyBrokenCount() > 0))
970 {
971 // upgrade all policy-broken packages with ForceImportantDeps=True
972 for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); ++I)
973 if ((*DCache)[I].NowPolicyBroken() == true)
974 DCache->MarkInstall(I,true,0, false, true);
975 }
976 }
977
978 // Nothing is broken
979 if (DCache->BrokenCount() == 0 || AllowBroken == true)
980 return true;
981
982 // Attempt to fix broken things
983 if (FixBroken == true)
984 {
985 c1out << _("Correcting dependencies...") << flush;
986 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
987 {
988 c1out << _(" failed.") << endl;
989 ShowBroken(c1out,*this,true);
990
991 return _error->Error(_("Unable to correct dependencies"));
992 }
993 if (pkgMinimizeUpgrade(*DCache) == false)
994 return _error->Error(_("Unable to minimize the upgrade set"));
995
996 c1out << _(" Done") << endl;
997 }
998 else
999 {
1000 c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
1001 ShowBroken(c1out,*this,true);
1002
1003 return _error->Error(_("Unmet dependencies. Try using -f."));
1004 }
1005
1006 return true;
1007 }
1008 /*}}}*/
1009 // CheckAuth - check if each download comes form a trusted source /*{{{*/
1010 // ---------------------------------------------------------------------
1011 /* */
1012 static bool CheckAuth(pkgAcquire& Fetcher)
1013 {
1014 string UntrustedList;
1015 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
1016 {
1017 if (!(*I)->IsTrusted())
1018 {
1019 UntrustedList += string((*I)->ShortDesc()) + " ";
1020 }
1021 }
1022
1023 if (UntrustedList == "")
1024 {
1025 return true;
1026 }
1027
1028 ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
1029
1030 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
1031 {
1032 c2out << _("Authentication warning overridden.\n");
1033 return true;
1034 }
1035
1036 if (_config->FindI("quiet",0) < 2
1037 && _config->FindB("APT::Get::Assume-Yes",false) == false)
1038 {
1039 c2out << _("Install these packages without verification [y/N]? ") << flush;
1040 if (!YnPrompt(false))
1041 return _error->Error(_("Some packages could not be authenticated"));
1042
1043 return true;
1044 }
1045 else if (_config->FindB("APT::Get::Force-Yes",false) == true)
1046 {
1047 return true;
1048 }
1049
1050 return _error->Error(_("There are problems and -y was used without --force-yes"));
1051 }
1052 /*}}}*/
1053 // InstallPackages - Actually download and install the packages /*{{{*/
1054 // ---------------------------------------------------------------------
1055 /* This displays the informative messages describing what is going to
1056 happen and then calls the download routines */
1057 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
1058 bool Safety = true)
1059 {
1060 if (_config->FindB("APT::Get::Purge",false) == true)
1061 {
1062 pkgCache::PkgIterator I = Cache->PkgBegin();
1063 for (; I.end() == false; ++I)
1064 {
1065 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
1066 Cache->MarkDelete(I,true);
1067 }
1068 }
1069
1070 bool Fail = false;
1071 bool Essential = false;
1072
1073 // Show all the various warning indicators
1074 ShowDel(c1out,Cache);
1075 ShowNew(c1out,Cache);
1076 if (ShwKept == true)
1077 ShowKept(c1out,Cache);
1078 Fail |= !ShowHold(c1out,Cache);
1079 if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
1080 ShowUpgraded(c1out,Cache);
1081 Fail |= !ShowDowngraded(c1out,Cache);
1082 if (_config->FindB("APT::Get::Download-Only",false) == false)
1083 Essential = !ShowEssential(c1out,Cache);
1084 Fail |= Essential;
1085 Stats(c1out,Cache);
1086
1087 // Sanity check
1088 if (Cache->BrokenCount() != 0)
1089 {
1090 ShowBroken(c1out,Cache,false);
1091 return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
1092 }
1093
1094 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
1095 Cache->BadCount() == 0)
1096 return true;
1097
1098 // No remove flag
1099 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
1100 return _error->Error(_("Packages need to be removed but remove is disabled."));
1101
1102 // Run the simulator ..
1103 if (_config->FindB("APT::Get::Simulate") == true)
1104 {
1105 pkgSimulate PM(Cache);
1106 int status_fd = _config->FindI("APT::Status-Fd",-1);
1107 pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd);
1108 if (Res == pkgPackageManager::Failed)
1109 return false;
1110 if (Res != pkgPackageManager::Completed)
1111 return _error->Error(_("Internal error, Ordering didn't finish"));
1112 return true;
1113 }
1114
1115 // Create the text record parser
1116 pkgRecords Recs(Cache);
1117 if (_error->PendingError() == true)
1118 return false;
1119
1120 // Create the download object
1121 pkgAcquire Fetcher;
1122 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1123 if (_config->FindB("APT::Get::Print-URIs", false) == true)
1124 {
1125 // force a hashsum for compatibility reasons
1126 _config->CndSet("Acquire::ForceHash", "md5sum");
1127 }
1128 else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false)
1129 return false;
1130
1131 // Read the source list
1132 if (Cache.BuildSourceList() == false)
1133 return false;
1134 pkgSourceList *List = Cache.GetSourceList();
1135
1136 // Create the package manager and prepare to download
1137 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
1138 if (PM->GetArchives(&Fetcher,List,&Recs) == false ||
1139 _error->PendingError() == true)
1140 return false;
1141
1142 // Display statistics
1143 unsigned long long FetchBytes = Fetcher.FetchNeeded();
1144 unsigned long long FetchPBytes = Fetcher.PartialPresent();
1145 unsigned long long DebBytes = Fetcher.TotalNeeded();
1146 if (DebBytes != Cache->DebSize())
1147 {
1148 c0out << DebBytes << ',' << Cache->DebSize() << endl;
1149 c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << endl;
1150 }
1151
1152 // Number of bytes
1153 if (DebBytes != FetchBytes)
1154 //TRANSLATOR: The required space between number and unit is already included
1155 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
1156 ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
1157 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
1158 else if (DebBytes != 0)
1159 //TRANSLATOR: The required space between number and unit is already included
1160 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
1161 ioprintf(c1out,_("Need to get %sB of archives.\n"),
1162 SizeToStr(DebBytes).c_str());
1163
1164 // Size delta
1165 if (Cache->UsrSize() >= 0)
1166 //TRANSLATOR: The required space between number and unit is already included
1167 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
1168 ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
1169 SizeToStr(Cache->UsrSize()).c_str());
1170 else
1171 //TRANSLATOR: The required space between number and unit is already included
1172 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
1173 ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
1174 SizeToStr(-1*Cache->UsrSize()).c_str());
1175
1176 if (_error->PendingError() == true)
1177 return false;
1178
1179 /* Check for enough free space, but only if we are actually going to
1180 download */
1181 if (_config->FindB("APT::Get::Print-URIs") == false &&
1182 _config->FindB("APT::Get::Download",true) == true)
1183 {
1184 struct statvfs Buf;
1185 string OutputDir = _config->FindDir("Dir::Cache::Archives");
1186 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
1187 if (errno == EOVERFLOW)
1188 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
1189 OutputDir.c_str());
1190 else
1191 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
1192 OutputDir.c_str());
1193 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
1194 {
1195 struct statfs Stat;
1196 if (statfs(OutputDir.c_str(),&Stat) != 0
1197 #if HAVE_STRUCT_STATFS_F_TYPE
1198 || unsigned(Stat.f_type) != RAMFS_MAGIC
1199 #endif
1200 )
1201 return _error->Error(_("You don't have enough free space in %s."),
1202 OutputDir.c_str());
1203 }
1204 }
1205
1206 // Fail safe check
1207 if (_config->FindI("quiet",0) >= 2 ||
1208 _config->FindB("APT::Get::Assume-Yes",false) == true)
1209 {
1210 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
1211 return _error->Error(_("There are problems and -y was used without --force-yes"));
1212 }
1213
1214 if (Essential == true && Safety == true)
1215 {
1216 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
1217 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
1218
1219 const char *Prompt = _("Yes, do as I say!");
1220 ioprintf(c2out,
1221 _("You are about to do something potentially harmful.\n"
1222 "To continue type in the phrase '%s'\n"
1223 " ?] "),Prompt);
1224 c2out << flush;
1225 if (AnalPrompt(Prompt) == false)
1226 {
1227 c2out << _("Abort.") << endl;
1228 exit(1);
1229 }
1230 }
1231 else
1232 {
1233 // Prompt to continue
1234 if (Ask == true || Fail == true)
1235 {
1236 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
1237 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
1238
1239 if (_config->FindI("quiet",0) < 2 &&
1240 _config->FindB("APT::Get::Assume-Yes",false) == false)
1241 {
1242 c2out << _("Do you want to continue [Y/n]? ") << flush;
1243
1244 if (YnPrompt() == false)
1245 {
1246 c2out << _("Abort.") << endl;
1247 exit(1);
1248 }
1249 }
1250 }
1251 }
1252
1253 // Just print out the uris an exit if the --print-uris flag was used
1254 if (_config->FindB("APT::Get::Print-URIs") == true)
1255 {
1256 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1257 for (; I != Fetcher.UriEnd(); ++I)
1258 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1259 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
1260 return true;
1261 }
1262
1263 if (!CheckAuth(Fetcher))
1264 return false;
1265
1266 /* Unlock the dpkg lock if we are not going to be doing an install
1267 after. */
1268 if (_config->FindB("APT::Get::Download-Only",false) == true)
1269 _system->UnLock();
1270
1271 // Run it
1272 while (1)
1273 {
1274 bool Transient = false;
1275 if (_config->FindB("APT::Get::Download",true) == false)
1276 {
1277 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
1278 {
1279 if ((*I)->Local == true)
1280 {
1281 ++I;
1282 continue;
1283 }
1284
1285 // Close the item and check if it was found in cache
1286 (*I)->Finished();
1287 if ((*I)->Complete == false)
1288 Transient = true;
1289
1290 // Clear it out of the fetch list
1291 delete *I;
1292 I = Fetcher.ItemsBegin();
1293 }
1294 }
1295
1296 if (Fetcher.Run() == pkgAcquire::Failed)
1297 return false;
1298
1299 // Print out errors
1300 bool Failed = false;
1301 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
1302 {
1303 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1304 (*I)->Complete == true)
1305 continue;
1306
1307 if ((*I)->Status == pkgAcquire::Item::StatIdle)
1308 {
1309 Transient = true;
1310 // Failed = true;
1311 continue;
1312 }
1313
1314 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1315 (*I)->ErrorText.c_str());
1316 Failed = true;
1317 }
1318
1319 /* If we are in no download mode and missing files and there were
1320 'failures' then the user must specify -m. Furthermore, there
1321 is no such thing as a transient error in no-download mode! */
1322 if (Transient == true &&
1323 _config->FindB("APT::Get::Download",true) == false)
1324 {
1325 Transient = false;
1326 Failed = true;
1327 }
1328
1329 if (_config->FindB("APT::Get::Download-Only",false) == true)
1330 {
1331 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
1332 return _error->Error(_("Some files failed to download"));
1333 c1out << _("Download complete and in download only mode") << endl;
1334 return true;
1335 }
1336
1337 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
1338 {
1339 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
1340 }
1341
1342 if (Transient == true && Failed == true)
1343 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
1344
1345 // Try to deal with missing package files
1346 if (Failed == true && PM->FixMissing() == false)
1347 {
1348 cerr << _("Unable to correct missing packages.") << endl;
1349 return _error->Error(_("Aborting install."));
1350 }
1351
1352 _system->UnLock();
1353 int status_fd = _config->FindI("APT::Status-Fd",-1);
1354 pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd);
1355 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
1356 return false;
1357 if (Res == pkgPackageManager::Completed)
1358 break;
1359
1360 // Reload the fetcher object and loop again for media swapping
1361 Fetcher.Shutdown();
1362 if (PM->GetArchives(&Fetcher,List,&Recs) == false)
1363 return false;
1364
1365 _system->Lock();
1366 }
1367
1368 std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages();
1369 if (disappearedPkgs.empty() == true)
1370 return true;
1371
1372 string disappear;
1373 for (std::set<std::string>::const_iterator d = disappearedPkgs.begin();
1374 d != disappearedPkgs.end(); ++d)
1375 disappear.append(*d).append(" ");
1376
1377 ShowList(c1out, P_("The following package disappeared from your system as\n"
1378 "all files have been overwritten by other packages:",
1379 "The following packages disappeared from your system as\n"
1380 "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, "");
1381 c0out << _("Note: This is done automatic and on purpose by dpkg.") << std::endl;
1382
1383 return true;
1384 }
1385 /*}}}*/
1386 // TryToInstallBuildDep - Try to install a single package /*{{{*/
1387 // ---------------------------------------------------------------------
1388 /* This used to be inlined in DoInstall, but with the advent of regex package
1389 name matching it was split out.. */
1390 bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
1391 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
1392 bool AllowFail = true)
1393 {
1394 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
1395 {
1396 CacheSetHelperAPTGet helper(c1out);
1397 helper.showErrors(AllowFail == false);
1398 pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
1399 if (Ver.end() == false)
1400 Pkg = Ver.ParentPkg();
1401 else if (helper.showVirtualPackageErrors(Cache) == false)
1402 return AllowFail;
1403 }
1404
1405 if (_config->FindB("Debug::BuildDeps",false) == true)
1406 {
1407 if (Remove == true)
1408 cout << " Trying to remove " << Pkg << endl;
1409 else
1410 cout << " Trying to install " << Pkg << endl;
1411 }
1412
1413 if (Remove == true)
1414 {
1415 TryToRemove RemoveAction(Cache, &Fix);
1416 RemoveAction(Pkg.VersionList());
1417 } else if (Cache[Pkg].CandidateVer != 0) {
1418 TryToInstall InstallAction(Cache, &Fix, BrokenFix);
1419 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
1420 InstallAction.doAutoInstall();
1421 } else
1422 return AllowFail;
1423
1424 return true;
1425 }
1426 /*}}}*/
1427 // FindSrc - Find a source record /*{{{*/
1428 // ---------------------------------------------------------------------
1429 /* */
1430 pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
1431 pkgSrcRecords &SrcRecs,string &Src,
1432 pkgDepCache &Cache)
1433 {
1434 string VerTag;
1435 string DefRel = _config->Find("APT::Default-Release");
1436 string TmpSrc = Name;
1437
1438 // extract the version/release from the pkgname
1439 const size_t found = TmpSrc.find_last_of("/=");
1440 if (found != string::npos) {
1441 if (TmpSrc[found] == '/')
1442 DefRel = TmpSrc.substr(found+1);
1443 else
1444 VerTag = TmpSrc.substr(found+1);
1445 TmpSrc = TmpSrc.substr(0,found);
1446 }
1447
1448 /* Lookup the version of the package we would install if we were to
1449 install a version and determine the source package name, then look
1450 in the archive for a source package of the same name. */
1451 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
1452 const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
1453 if (MatchSrcOnly == false && Pkg.end() == false)
1454 {
1455 if(VerTag.empty() == false || DefRel.empty() == false)
1456 {
1457 bool fuzzy = false;
1458 // we have a default release, try to locate the pkg. we do it like
1459 // this because GetCandidateVer() will not "downgrade", that means
1460 // "apt-get source -t stable apt" won't work on a unstable system
1461 for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
1462 {
1463 // try first only exact matches, later fuzzy matches
1464 if (Ver.end() == true)
1465 {
1466 if (fuzzy == true)
1467 break;
1468 fuzzy = true;
1469 Ver = Pkg.VersionList();
1470 // exit right away from the Pkg.VersionList() loop if we
1471 // don't have any versions
1472 if (Ver.end() == true)
1473 break;
1474 }
1475 // We match against a concrete version (or a part of this version)
1476 if (VerTag.empty() == false &&
1477 (fuzzy == true || Cache.VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
1478 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
1479 continue;
1480
1481 for (pkgCache::VerFileIterator VF = Ver.FileList();
1482 VF.end() == false; ++VF)
1483 {
1484 /* If this is the status file, and the current version is not the
1485 version in the status file (ie it is not installed, or somesuch)
1486 then it is not a candidate for installation, ever. This weeds
1487 out bogus entries that may be due to config-file states, or
1488 other. */
1489 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
1490 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
1491 continue;
1492
1493 // or we match against a release
1494 if(VerTag.empty() == false ||
1495 (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
1496 (VF.File().Codename() != 0 && VF.File().Codename() == DefRel))
1497 {
1498 pkgRecords::Parser &Parse = Recs.Lookup(VF);
1499 Src = Parse.SourcePkg();
1500 // no SourcePkg name, so it is the "binary" name
1501 if (Src.empty() == true)
1502 Src = TmpSrc;
1503 // the Version we have is possibly fuzzy or includes binUploads,
1504 // so we use the Version of the SourcePkg (empty if same as package)
1505 VerTag = Parse.SourceVer();
1506 if (VerTag.empty() == true)
1507 VerTag = Ver.VerStr();
1508 break;
1509 }
1510 }
1511 if (Src.empty() == false)
1512 break;
1513 }
1514 if (Src.empty() == true)
1515 {
1516 // Sources files have no codename information
1517 if (VerTag.empty() == true && DefRel.empty() == false)
1518 {
1519 _error->Error(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
1520 return 0;
1521 }
1522 }
1523 }
1524 if (Src.empty() == true)
1525 {
1526 // if we don't have found a fitting package yet so we will
1527 // choose a good candidate and proceed with that.
1528 // Maybe we will find a source later on with the right VerTag
1529 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
1530 if (Ver.end() == false)
1531 {
1532 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1533 Src = Parse.SourcePkg();
1534 if (VerTag.empty() == true)
1535 VerTag = Parse.SourceVer();
1536 }
1537 }
1538 }
1539
1540 if (Src.empty() == true)
1541 Src = TmpSrc;
1542 else
1543 {
1544 /* if we have a source pkg name, make sure to only search
1545 for srcpkg names, otherwise apt gets confused if there
1546 is a binary package "pkg1" and a source package "pkg1"
1547 with the same name but that comes from different packages */
1548 MatchSrcOnly = true;
1549 if (Src != TmpSrc)
1550 {
1551 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
1552 }
1553 }
1554
1555 // The best hit
1556 pkgSrcRecords::Parser *Last = 0;
1557 unsigned long Offset = 0;
1558 string Version;
1559
1560 /* Iterate over all of the hits, which includes the resulting
1561 binary packages in the search */
1562 pkgSrcRecords::Parser *Parse;
1563 while (true)
1564 {
1565 SrcRecs.Restart();
1566 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
1567 {
1568 const string Ver = Parse->Version();
1569
1570 // Ignore all versions which doesn't fit
1571 if (VerTag.empty() == false &&
1572 Cache.VS().CmpVersion(VerTag, Ver) != 0) // exact match
1573 continue;
1574
1575 // Newer version or an exact match? Save the hit
1576 if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
1577 Last = Parse;
1578 Offset = Parse->Offset();
1579 Version = Ver;
1580 }
1581
1582 // was the version check above an exact match? If so, we don't need to look further
1583 if (VerTag.empty() == false && VerTag.size() == Ver.size())
1584 break;
1585 }
1586 if (Last != 0 || VerTag.empty() == true)
1587 break;
1588 //if (VerTag.empty() == false && Last == 0)
1589 _error->Error(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
1590 return 0;
1591 }
1592
1593 if (Last == 0 || Last->Jump(Offset) == false)
1594 return 0;
1595
1596 return Last;
1597 }
1598 /*}}}*/
1599 // DoUpdate - Update the package lists /*{{{*/
1600 // ---------------------------------------------------------------------
1601 /* */
1602 bool DoUpdate(CommandLine &CmdL)
1603 {
1604 if (CmdL.FileSize() != 1)
1605 return _error->Error(_("The update command takes no arguments"));
1606
1607 CacheFile Cache;
1608
1609 // Get the source list
1610 if (Cache.BuildSourceList() == false)
1611 return false;
1612 pkgSourceList *List = Cache.GetSourceList();
1613
1614 // Create the progress
1615 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1616
1617 // Just print out the uris an exit if the --print-uris flag was used
1618 if (_config->FindB("APT::Get::Print-URIs") == true)
1619 {
1620 // force a hashsum for compatibility reasons
1621 _config->CndSet("Acquire::ForceHash", "md5sum");
1622
1623 // get a fetcher
1624 pkgAcquire Fetcher;
1625 if (Fetcher.Setup(&Stat) == false)
1626 return false;
1627
1628 // Populate it with the source selection and get all Indexes
1629 // (GetAll=true)
1630 if (List->GetIndexes(&Fetcher,true) == false)
1631 return false;
1632
1633 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1634 for (; I != Fetcher.UriEnd(); ++I)
1635 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1636 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
1637 return true;
1638 }
1639
1640 // do the work
1641 if (_config->FindB("APT::Get::Download",true) == true)
1642 ListUpdate(Stat, *List);
1643
1644 // Rebuild the cache.
1645 pkgCacheFile::RemoveCaches();
1646 if (Cache.BuildCaches() == false)
1647 return false;
1648
1649 return true;
1650 }
1651 /*}}}*/
1652 // DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
1653 // ---------------------------------------------------------------------
1654 /* Remove unused automatic packages */
1655 bool DoAutomaticRemove(CacheFile &Cache)
1656 {
1657 bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
1658 bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
1659 bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
1660
1661 pkgDepCache::ActionGroup group(*Cache);
1662 if(Debug)
1663 std::cout << "DoAutomaticRemove()" << std::endl;
1664
1665 if (doAutoRemove == true &&
1666 _config->FindB("APT::Get::Remove",true) == false)
1667 {
1668 c1out << _("We are not supposed to delete stuff, can't start "
1669 "AutoRemover") << std::endl;
1670 return false;
1671 }
1672
1673 bool purgePkgs = _config->FindB("APT::Get::Purge", false);
1674 bool smallList = (hideAutoRemove == false &&
1675 strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0);
1676
1677 string autoremovelist, autoremoveversions;
1678 unsigned long autoRemoveCount = 0;
1679 APT::PackageSet tooMuch;
1680 // look over the cache to see what can be removed
1681 for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); ! Pkg.end(); ++Pkg)
1682 {
1683 if (Cache[Pkg].Garbage)
1684 {
1685 if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
1686 if(Debug)
1687 std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl;
1688
1689 if (doAutoRemove)
1690 {
1691 if(Pkg.CurrentVer() != 0 &&
1692 Pkg->CurrentState != pkgCache::State::ConfigFiles)
1693 Cache->MarkDelete(Pkg, purgePkgs);
1694 else
1695 Cache->MarkKeep(Pkg, false, false);
1696 }
1697 else
1698 {
1699 // if the package is a new install and already garbage we don't need to
1700 // install it in the first place, so nuke it instead of show it
1701 if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0)
1702 {
1703 if (Pkg.CandVersion() != 0)
1704 tooMuch.insert(Pkg);
1705 Cache->MarkDelete(Pkg, false);
1706 }
1707 // only show stuff in the list that is not yet marked for removal
1708 else if(hideAutoRemove == false && Cache[Pkg].Delete() == false)
1709 {
1710 ++autoRemoveCount;
1711 // we don't need to fill the strings if we don't need them
1712 if (smallList == false)
1713 {
1714 autoremovelist += Pkg.FullName(true) + " ";
1715 autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
1716 }
1717 }
1718 }
1719 }
1720 }
1721
1722 // we could have removed a new dependency of a garbage package,
1723 // so check if a reverse depends is broken and if so install it again.
1724 if (tooMuch.empty() == false && (Cache->BrokenCount() != 0 || Cache->PolicyBrokenCount() != 0))
1725 {
1726 bool Changed;
1727 do {
1728 Changed = false;
1729 for (APT::PackageSet::const_iterator Pkg = tooMuch.begin();
1730 Pkg != tooMuch.end() && Changed == false; ++Pkg)
1731 {
1732 APT::PackageSet too;
1733 too.insert(Pkg);
1734 for (pkgCache::PrvIterator Prv = Cache[Pkg].CandidateVerIter(Cache).ProvidesList();
1735 Prv.end() == false; ++Prv)
1736 too.insert(Prv.ParentPkg());
1737 for (APT::PackageSet::const_iterator P = too.begin();
1738 P != too.end() && Changed == false; ++P) {
1739 for (pkgCache::DepIterator R = P.RevDependsList();
1740 R.end() == false; ++R)
1741 {
1742 if (R.IsNegative() == true ||
1743 Cache->IsImportantDep(R) == false)
1744 continue;
1745 pkgCache::PkgIterator N = R.ParentPkg();
1746 if (N.end() == true || (N->CurrentVer == 0 && (*Cache)[N].Install() == false))
1747 continue;
1748 if (Debug == true)
1749 std::clog << "Save " << Pkg << " as another installed garbage package depends on it" << std::endl;
1750 Cache->MarkInstall(Pkg, false);
1751 if (hideAutoRemove == false)
1752 {
1753 ++autoRemoveCount;
1754 if (smallList == false)
1755 {
1756 autoremovelist += Pkg.FullName(true) + " ";
1757 autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
1758 }
1759 }
1760 tooMuch.erase(Pkg);
1761 Changed = true;
1762 break;
1763 }
1764 }
1765 }
1766 } while (Changed == true);
1767 }
1768
1769 // Now see if we had destroyed anything (if we had done anything)
1770 if (Cache->BrokenCount() != 0)
1771 {
1772 c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
1773 "shouldn't happen. Please file a bug report against apt.") << endl;
1774 c1out << endl;
1775 c1out << _("The following information may help to resolve the situation:") << endl;
1776 c1out << endl;
1777 ShowBroken(c1out,Cache,false);
1778
1779 return _error->Error(_("Internal Error, AutoRemover broke stuff"));
1780 }
1781
1782 // if we don't remove them, we should show them!
1783 if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
1784 {
1785 if (smallList == false)
1786 ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
1787 "The following packages were automatically installed and are no longer required:",
1788 autoRemoveCount), autoremovelist, autoremoveversions);
1789 else
1790 ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
1791 "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
1792 c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
1793 }
1794 return true;
1795 }
1796 /*}}}*/
1797 // DoUpgrade - Upgrade all packages /*{{{*/
1798 // ---------------------------------------------------------------------
1799 /* Upgrade all packages without installing new packages or erasing old
1800 packages */
1801 bool DoUpgrade(CommandLine &CmdL)
1802 {
1803 CacheFile Cache;
1804 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1805 return false;
1806
1807 // Do the upgrade
1808 if (pkgAllUpgrade(Cache) == false)
1809 {
1810 ShowBroken(c1out,Cache,false);
1811 return _error->Error(_("Internal error, AllUpgrade broke stuff"));
1812 }
1813
1814 return InstallPackages(Cache,true);
1815 }
1816 /*}}}*/
1817 // DoInstall - Install packages from the command line /*{{{*/
1818 // ---------------------------------------------------------------------
1819 /* Install named packages */
1820 bool DoInstall(CommandLine &CmdL)
1821 {
1822 CacheFile Cache;
1823 if (Cache.OpenForInstall() == false ||
1824 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
1825 return false;
1826
1827 // Enter the special broken fixing mode if the user specified arguments
1828 bool BrokenFix = false;
1829 if (Cache->BrokenCount() != 0)
1830 BrokenFix = true;
1831
1832 pkgProblemResolver* Fix = NULL;
1833 if (_config->FindB("APT::Get::CallResolver", true) == true)
1834 Fix = new pkgProblemResolver(Cache);
1835
1836 static const unsigned short MOD_REMOVE = 1;
1837 static const unsigned short MOD_INSTALL = 2;
1838
1839 unsigned short fallback = MOD_INSTALL;
1840 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
1841 fallback = MOD_REMOVE;
1842 else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
1843 {
1844 _config->Set("APT::Get::Purge", true);
1845 fallback = MOD_REMOVE;
1846 }
1847 else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
1848 {
1849 _config->Set("APT::Get::AutomaticRemove", "true");
1850 fallback = MOD_REMOVE;
1851 }
1852
1853 std::list<APT::VersionSet::Modifier> mods;
1854 mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+",
1855 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE));
1856 mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-",
1857 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST));
1858 CacheSetHelperAPTGet helper(c0out);
1859 std::map<unsigned short, APT::VersionSet> verset = APT::VersionSet::GroupedFromCommandLine(Cache,
1860 CmdL.FileList + 1, mods, fallback, helper);
1861
1862 if (_error->PendingError() == true)
1863 {
1864 helper.showVirtualPackageErrors(Cache);
1865 if (Fix != NULL)
1866 delete Fix;
1867 return false;
1868 }
1869
1870 unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 };
1871
1872 TryToInstall InstallAction(Cache, Fix, BrokenFix);
1873 TryToRemove RemoveAction(Cache, Fix);
1874
1875 // new scope for the ActionGroup
1876 {
1877 pkgDepCache::ActionGroup group(Cache);
1878
1879 for (unsigned short i = 0; order[i] != 0; ++i)
1880 {
1881 if (order[i] == MOD_INSTALL)
1882 InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
1883 else if (order[i] == MOD_REMOVE)
1884 RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
1885 }
1886
1887 if (Fix != NULL && _config->FindB("APT::Get::AutoSolving", true) == true)
1888 {
1889 for (unsigned short i = 0; order[i] != 0; ++i)
1890 {
1891 if (order[i] != MOD_INSTALL)
1892 continue;
1893 InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
1894 InstallAction.doAutoInstall();
1895 }
1896 }
1897
1898 if (_error->PendingError() == true)
1899 {
1900 if (Fix != NULL)
1901 delete Fix;
1902 return false;
1903 }
1904
1905 /* If we are in the Broken fixing mode we do not attempt to fix the
1906 problems. This is if the user invoked install without -f and gave
1907 packages */
1908 if (BrokenFix == true && Cache->BrokenCount() != 0)
1909 {
1910 c1out << _("You might want to run 'apt-get -f install' to correct these:") << endl;
1911 ShowBroken(c1out,Cache,false);
1912 if (Fix != NULL)
1913 delete Fix;
1914 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
1915 }
1916
1917 if (Fix != NULL)
1918 {
1919 // Call the scored problem resolver
1920 Fix->InstallProtect();
1921 Fix->Resolve(true);
1922 delete Fix;
1923 }
1924
1925 // Now we check the state of the packages,
1926 if (Cache->BrokenCount() != 0)
1927 {
1928 c1out <<
1929 _("Some packages could not be installed. This may mean that you have\n"
1930 "requested an impossible situation or if you are using the unstable\n"
1931 "distribution that some required packages have not yet been created\n"
1932 "or been moved out of Incoming.") << endl;
1933 /*
1934 if (Packages == 1)
1935 {
1936 c1out << endl;
1937 c1out <<
1938 _("Since you only requested a single operation it is extremely likely that\n"
1939 "the package is simply not installable and a bug report against\n"
1940 "that package should be filed.") << endl;
1941 }
1942 */
1943
1944 c1out << _("The following information may help to resolve the situation:") << endl;
1945 c1out << endl;
1946 ShowBroken(c1out,Cache,false);
1947 if (_error->PendingError() == true)
1948 return false;
1949 else
1950 return _error->Error(_("Broken packages"));
1951 }
1952 }
1953 if (!DoAutomaticRemove(Cache))
1954 return false;
1955
1956 /* Print out a list of packages that are going to be installed extra
1957 to what the user asked */
1958 if (Cache->InstCount() != verset[MOD_INSTALL].size())
1959 {
1960 string List;
1961 string VersionsList;
1962 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1963 {
1964 pkgCache::PkgIterator I(Cache,Cache.List[J]);
1965 if ((*Cache)[I].Install() == false)
1966 continue;
1967 pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache);
1968
1969 if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end())
1970 continue;
1971
1972 List += I.FullName(true) + " ";
1973 VersionsList += string(Cache[I].CandVersion) + "\n";
1974 }
1975
1976 ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
1977 }
1978
1979 /* Print out a list of suggested and recommended packages */
1980 {
1981 string SuggestsList, RecommendsList, List;
1982 string SuggestsVersions, RecommendsVersions;
1983 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1984 {
1985 pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
1986
1987 /* Just look at the ones we want to install */
1988 if ((*Cache)[Pkg].Install() == false)
1989 continue;
1990
1991 // get the recommends/suggests for the candidate ver
1992 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
1993 for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
1994 {
1995 pkgCache::DepIterator Start;
1996 pkgCache::DepIterator End;
1997 D.GlobOr(Start,End); // advances D
1998
1999 // FIXME: we really should display a or-group as a or-group to the user
2000 // the problem is that ShowList is incapable of doing this
2001 string RecommendsOrList,RecommendsOrVersions;
2002 string SuggestsOrList,SuggestsOrVersions;
2003 bool foundInstalledInOrGroup = false;
2004 for(;;)
2005 {
2006 /* Skip if package is installed already, or is about to be */
2007 string target = Start.TargetPkg().FullName(true) + " ";
2008 pkgCache::PkgIterator const TarPkg = Start.TargetPkg();
2009 if (TarPkg->SelectedState == pkgCache::State::Install ||
2010 TarPkg->SelectedState == pkgCache::State::Hold ||
2011 Cache[Start.TargetPkg()].Install())
2012 {
2013 foundInstalledInOrGroup=true;
2014 break;
2015 }
2016
2017 /* Skip if we already saw it */
2018 if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
2019 {
2020 foundInstalledInOrGroup=true;
2021 break;
2022 }
2023
2024 // this is a dep on a virtual pkg, check if any package that provides it
2025 // should be installed
2026 if(Start.TargetPkg().ProvidesList() != 0)
2027 {
2028 pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
2029 for (; I.end() == false; ++I)
2030 {
2031 pkgCache::PkgIterator Pkg = I.OwnerPkg();
2032 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
2033 Pkg.CurrentVer() != 0)
2034 foundInstalledInOrGroup=true;
2035 }
2036 }
2037
2038 if (Start->Type == pkgCache::Dep::Suggests)
2039 {
2040 SuggestsOrList += target;
2041 SuggestsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
2042 }
2043
2044 if (Start->Type == pkgCache::Dep::Recommends)
2045 {
2046 RecommendsOrList += target;
2047 RecommendsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
2048 }
2049
2050 if (Start >= End)
2051 break;
2052 ++Start;
2053 }
2054
2055 if(foundInstalledInOrGroup == false)
2056 {
2057 RecommendsList += RecommendsOrList;
2058 RecommendsVersions += RecommendsOrVersions;
2059 SuggestsList += SuggestsOrList;
2060 SuggestsVersions += SuggestsOrVersions;
2061 }
2062
2063 }
2064 }
2065
2066 ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
2067 ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
2068
2069 }
2070
2071 // if nothing changed in the cache, but only the automark information
2072 // we write the StateFile here, otherwise it will be written in
2073 // cache.commit()
2074 if (InstallAction.AutoMarkChanged > 0 &&
2075 Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
2076 Cache->BadCount() == 0 &&
2077 _config->FindB("APT::Get::Simulate",false) == false)
2078 Cache->writeStateFile(NULL);
2079
2080 // See if we need to prompt
2081 // FIXME: check if really the packages in the set are going to be installed
2082 if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0)
2083 return InstallPackages(Cache,false,false);
2084
2085 return InstallPackages(Cache,false);
2086 }
2087 /*}}}*/
2088 /* mark packages as automatically/manually installed. {{{*/
2089 bool DoMarkAuto(CommandLine &CmdL)
2090 {
2091 bool Action = true;
2092 int AutoMarkChanged = 0;
2093 OpTextProgress progress;
2094 CacheFile Cache;
2095 if (Cache.Open() == false)
2096 return false;
2097
2098 if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
2099 Action = true;
2100 else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
2101 Action = false;
2102
2103 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
2104 {
2105 const char *S = *I;
2106 // Locate the package
2107 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
2108 if (Pkg.end() == true) {
2109 return _error->Error(_("Couldn't find package %s"),S);
2110 }
2111 else
2112 {
2113 if (!Action)
2114 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
2115 else
2116 ioprintf(c1out,_("%s set to automatically installed.\n"),
2117 Pkg.Name());
2118
2119 Cache->MarkAuto(Pkg,Action);
2120 AutoMarkChanged++;
2121 }
2122 }
2123
2124 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
2125
2126 if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
2127 return Cache->writeStateFile(NULL);
2128 return false;
2129 }
2130 /*}}}*/
2131 // DoDistUpgrade - Automatic smart upgrader /*{{{*/
2132 // ---------------------------------------------------------------------
2133 /* Intelligent upgrader that will install and remove packages at will */
2134 bool DoDistUpgrade(CommandLine &CmdL)
2135 {
2136 CacheFile Cache;
2137 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
2138 return false;
2139
2140 c0out << _("Calculating upgrade... ") << flush;
2141 if (pkgDistUpgrade(*Cache) == false)
2142 {
2143 c0out << _("Failed") << endl;
2144 ShowBroken(c1out,Cache,false);
2145 return false;
2146 }
2147
2148 c0out << _("Done") << endl;
2149
2150 return InstallPackages(Cache,true);
2151 }
2152 /*}}}*/
2153 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
2154 // ---------------------------------------------------------------------
2155 /* Follows dselect's selections */
2156 bool DoDSelectUpgrade(CommandLine &CmdL)
2157 {
2158 CacheFile Cache;
2159 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
2160 return false;
2161
2162 pkgDepCache::ActionGroup group(Cache);
2163
2164 // Install everything with the install flag set
2165 pkgCache::PkgIterator I = Cache->PkgBegin();
2166 for (;I.end() != true; ++I)
2167 {
2168 /* Install the package only if it is a new install, the autoupgrader
2169 will deal with the rest */
2170 if (I->SelectedState == pkgCache::State::Install)
2171 Cache->MarkInstall(I,false);
2172 }
2173
2174 /* Now install their deps too, if we do this above then order of
2175 the status file is significant for | groups */
2176 for (I = Cache->PkgBegin();I.end() != true; ++I)
2177 {
2178 /* Install the package only if it is a new install, the autoupgrader
2179 will deal with the rest */
2180 if (I->SelectedState == pkgCache::State::Install)
2181 Cache->MarkInstall(I,true);
2182 }
2183
2184 // Apply erasures now, they override everything else.
2185 for (I = Cache->PkgBegin();I.end() != true; ++I)
2186 {
2187 // Remove packages
2188 if (I->SelectedState == pkgCache::State::DeInstall ||
2189 I->SelectedState == pkgCache::State::Purge)
2190 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
2191 }
2192
2193 /* Resolve any problems that dselect created, allupgrade cannot handle
2194 such things. We do so quite agressively too.. */
2195 if (Cache->BrokenCount() != 0)
2196 {
2197 pkgProblemResolver Fix(Cache);
2198
2199 // Hold back held packages.
2200 if (_config->FindB("APT::Ignore-Hold",false) == false)
2201 {
2202 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
2203 {
2204 if (I->SelectedState == pkgCache::State::Hold)
2205 {
2206 Fix.Protect(I);
2207 Cache->MarkKeep(I);
2208 }
2209 }
2210 }
2211
2212 if (Fix.Resolve() == false)
2213 {
2214 ShowBroken(c1out,Cache,false);
2215 return _error->Error(_("Internal error, problem resolver broke stuff"));
2216 }
2217 }
2218
2219 // Now upgrade everything
2220 if (pkgAllUpgrade(Cache) == false)
2221 {
2222 ShowBroken(c1out,Cache,false);
2223 return _error->Error(_("Internal error, problem resolver broke stuff"));
2224 }
2225
2226 return InstallPackages(Cache,false);
2227 }
2228 /*}}}*/
2229 // DoClean - Remove download archives /*{{{*/
2230 // ---------------------------------------------------------------------
2231 /* */
2232 bool DoClean(CommandLine &CmdL)
2233 {
2234 std::string const archivedir = _config->FindDir("Dir::Cache::archives");
2235 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
2236 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
2237
2238 if (_config->FindB("APT::Get::Simulate") == true)
2239 {
2240 cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< endl
2241 << "Del " << pkgcache << " " << srcpkgcache << endl;
2242 return true;
2243 }
2244
2245 // Lock the archive directory
2246 FileFd Lock;
2247 if (_config->FindB("Debug::NoLocking",false) == false)
2248 {
2249 Lock.Fd(GetLock(archivedir + "lock"));
2250 if (_error->PendingError() == true)
2251 return _error->Error(_("Unable to lock the download directory"));
2252 }
2253
2254 pkgAcquire Fetcher;
2255 Fetcher.Clean(archivedir);
2256 Fetcher.Clean(archivedir + "partial/");
2257
2258 pkgCacheFile::RemoveCaches();
2259
2260 return true;
2261 }
2262 /*}}}*/
2263 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
2264 // ---------------------------------------------------------------------
2265 /* This is similar to clean but it only purges things that cannot be
2266 downloaded, that is old versions of cached packages. */
2267 class LogCleaner : public pkgArchiveCleaner
2268 {
2269 protected:
2270 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
2271 {
2272 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
2273
2274 if (_config->FindB("APT::Get::Simulate") == false)
2275 unlink(File);
2276 };
2277 };
2278
2279 bool DoAutoClean(CommandLine &CmdL)
2280 {
2281 // Lock the archive directory
2282 FileFd Lock;
2283 if (_config->FindB("Debug::NoLocking",false) == false)
2284 {
2285 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2286 if (_error->PendingError() == true)
2287 return _error->Error(_("Unable to lock the download directory"));
2288 }
2289
2290 CacheFile Cache;
2291 if (Cache.Open() == false)
2292 return false;
2293
2294 LogCleaner Cleaner;
2295
2296 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
2297 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
2298 }
2299 /*}}}*/
2300 // DoDownload - download a binary /*{{{*/
2301 // ---------------------------------------------------------------------
2302 bool DoDownload(CommandLine &CmdL)
2303 {
2304 CacheFile Cache;
2305 if (Cache.ReadOnlyOpen() == false)
2306 return false;
2307
2308 APT::CacheSetHelper helper(c0out);
2309 APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
2310 CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
2311
2312 if (verset.empty() == true)
2313 return false;
2314
2315 pkgAcquire Fetcher;
2316 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
2317 if (_config->FindB("APT::Get::Print-URIs") == false)
2318 Fetcher.Setup(&Stat);
2319
2320 pkgRecords Recs(Cache);
2321 pkgSourceList *SrcList = Cache.GetSourceList();
2322 for (APT::VersionList::const_iterator Ver = verset.begin();
2323 Ver != verset.end();
2324 ++Ver)
2325 {
2326 string descr;
2327 // get the right version
2328 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
2329 pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
2330 pkgCache::VerFileIterator Vf = Ver.FileList();
2331 if (Vf.end() == true)
2332 return _error->Error("Can not find VerFile");
2333 pkgCache::PkgFileIterator F = Vf.File();
2334 pkgIndexFile *index;
2335 if(SrcList->FindIndex(F, index) == false)
2336 return _error->Error("FindIndex failed");
2337 string uri = index->ArchiveURI(rec.FileName());
2338 strprintf(descr, _("Downloading %s %s"), Pkg.Name(), Ver.VerStr());
2339 // get the most appropriate hash
2340 HashString hash;
2341 if (rec.SHA512Hash() != "")
2342 hash = HashString("sha512", rec.SHA512Hash());
2343 if (rec.SHA256Hash() != "")
2344 hash = HashString("sha256", rec.SHA256Hash());
2345 else if (rec.SHA1Hash() != "")
2346 hash = HashString("sha1", rec.SHA1Hash());
2347 else if (rec.MD5Hash() != "")
2348 hash = HashString("md5", rec.MD5Hash());
2349 // get the file
2350 new pkgAcqFile(&Fetcher, uri, hash.toStr(), (*Ver)->Size, descr, Pkg.Name(), ".");
2351 }
2352
2353 // Just print out the uris and exit if the --print-uris flag was used
2354 if (_config->FindB("APT::Get::Print-URIs") == true)
2355 {
2356 pkgAcquire::UriIterator I = Fetcher.UriBegin();
2357 for (; I != Fetcher.UriEnd(); ++I)
2358 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
2359 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
2360 return true;
2361 }
2362
2363 return (Fetcher.Run() == pkgAcquire::Continue);
2364 }
2365 /*}}}*/
2366 // DoCheck - Perform the check operation /*{{{*/
2367 // ---------------------------------------------------------------------
2368 /* Opening automatically checks the system, this command is mostly used
2369 for debugging */
2370 bool DoCheck(CommandLine &CmdL)
2371 {
2372 CacheFile Cache;
2373 Cache.Open();
2374 Cache.CheckDeps();
2375
2376 return true;
2377 }
2378 /*}}}*/
2379 // DoSource - Fetch a source archive /*{{{*/
2380 // ---------------------------------------------------------------------
2381 /* Fetch souce packages */
2382 struct DscFile
2383 {
2384 string Package;
2385 string Version;
2386 string Dsc;
2387 };
2388
2389 bool DoSource(CommandLine &CmdL)
2390 {
2391 CacheFile Cache;
2392 if (Cache.Open(false) == false)
2393 return false;
2394
2395 if (CmdL.FileSize() <= 1)
2396 return _error->Error(_("Must specify at least one package to fetch source for"));
2397
2398 // Read the source list
2399 if (Cache.BuildSourceList() == false)
2400 return false;
2401 pkgSourceList *List = Cache.GetSourceList();
2402
2403 // Create the text record parsers
2404 pkgRecords Recs(Cache);
2405 pkgSrcRecords SrcRecs(*List);
2406 if (_error->PendingError() == true)
2407 return false;
2408
2409 // Create the download object
2410 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
2411 pkgAcquire Fetcher;
2412 Fetcher.SetLog(&Stat);
2413
2414 DscFile *Dsc = new DscFile[CmdL.FileSize()];
2415
2416 // insert all downloaded uris into this set to avoid downloading them
2417 // twice
2418 set<string> queued;
2419
2420 // Diff only mode only fetches .diff files
2421 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
2422 // Tar only mode only fetches .tar files
2423 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
2424 // Dsc only mode only fetches .dsc files
2425 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
2426
2427 // Load the requestd sources into the fetcher
2428 unsigned J = 0;
2429 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
2430 {
2431 string Src;
2432 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
2433
2434 if (Last == 0) {
2435 delete[] Dsc;
2436 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
2437 }
2438
2439 string srec = Last->AsStr();
2440 string::size_type pos = srec.find("\nVcs-");
2441 while (pos != string::npos)
2442 {
2443 pos += strlen("\nVcs-");
2444 string vcs = srec.substr(pos,srec.find(":",pos)-pos);
2445 if(vcs == "Browser")
2446 {
2447 pos = srec.find("\nVcs-", pos);
2448 continue;
2449 }
2450 pos += vcs.length()+2;
2451 string::size_type epos = srec.find("\n", pos);
2452 string uri = srec.substr(pos,epos-pos).c_str();
2453 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
2454 "the '%s' version control system at:\n"
2455 "%s\n"),
2456 Src.c_str(), vcs.c_str(), uri.c_str());
2457 if(vcs == "Bzr")
2458 ioprintf(c1out,_("Please use:\n"
2459 "bzr get %s\n"
2460 "to retrieve the latest (possibly unreleased) "
2461 "updates to the package.\n"),
2462 uri.c_str());
2463 break;
2464 }
2465
2466 // Back track
2467 vector<pkgSrcRecords::File> Lst;
2468 if (Last->Files(Lst) == false) {
2469 delete[] Dsc;
2470 return false;
2471 }
2472
2473 // Load them into the fetcher
2474 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
2475 I != Lst.end(); ++I)
2476 {
2477 // Try to guess what sort of file it is we are getting.
2478 if (I->Type == "dsc")
2479 {
2480 Dsc[J].Package = Last->Package();
2481 Dsc[J].Version = Last->Version();
2482 Dsc[J].Dsc = flNotDir(I->Path);
2483 }
2484
2485 // Handle the only options so that multiple can be used at once
2486 if (diffOnly == true || tarOnly == true || dscOnly == true)
2487 {
2488 if ((diffOnly == true && I->Type == "diff") ||
2489 (tarOnly == true && I->Type == "tar") ||
2490 (dscOnly == true && I->Type == "dsc"))
2491 ; // Fine, we want this file downloaded
2492 else
2493 continue;
2494 }
2495
2496 // don't download the same uri twice (should this be moved to
2497 // the fetcher interface itself?)
2498 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
2499 continue;
2500 queued.insert(Last->Index().ArchiveURI(I->Path));
2501
2502 // check if we have a file with that md5 sum already localy
2503 if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
2504 {
2505 FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
2506 MD5Summation sum;
2507 sum.AddFD(Fd.Fd(), Fd.Size());
2508 Fd.Close();
2509 if((string)sum.Result() == I->MD5Hash)
2510 {
2511 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
2512 flNotDir(I->Path).c_str());
2513 continue;
2514 }
2515 }
2516
2517 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
2518 I->MD5Hash,I->Size,
2519 Last->Index().SourceInfo(*Last,*I),Src);
2520 }
2521 }
2522
2523 // Display statistics
2524 unsigned long long FetchBytes = Fetcher.FetchNeeded();
2525 unsigned long long FetchPBytes = Fetcher.PartialPresent();
2526 unsigned long long DebBytes = Fetcher.TotalNeeded();
2527
2528 // Check for enough free space
2529 struct statvfs Buf;
2530 string OutputDir = ".";
2531 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
2532 delete[] Dsc;
2533 if (errno == EOVERFLOW)
2534 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
2535 OutputDir.c_str());
2536 else
2537 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
2538 OutputDir.c_str());
2539 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
2540 {
2541 struct statfs Stat;
2542 if (statfs(OutputDir.c_str(),&Stat) != 0
2543 #if HAVE_STRUCT_STATFS_F_TYPE
2544 || unsigned(Stat.f_type) != RAMFS_MAGIC
2545 #endif
2546 ) {
2547 delete[] Dsc;
2548 return _error->Error(_("You don't have enough free space in %s"),
2549 OutputDir.c_str());
2550 }
2551 }
2552
2553 // Number of bytes
2554 if (DebBytes != FetchBytes)
2555 //TRANSLATOR: The required space between number and unit is already included
2556 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
2557 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
2558 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
2559 else
2560 //TRANSLATOR: The required space between number and unit is already included
2561 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
2562 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
2563 SizeToStr(DebBytes).c_str());
2564
2565 if (_config->FindB("APT::Get::Simulate",false) == true)
2566 {
2567 for (unsigned I = 0; I != J; I++)
2568 ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
2569 delete[] Dsc;
2570 return true;
2571 }
2572
2573 // Just print out the uris an exit if the --print-uris flag was used
2574 if (_config->FindB("APT::Get::Print-URIs") == true)
2575 {
2576 pkgAcquire::UriIterator I = Fetcher.UriBegin();
2577 for (; I != Fetcher.UriEnd(); ++I)
2578 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
2579 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
2580 delete[] Dsc;
2581 return true;
2582 }
2583
2584 // Run it
2585 if (Fetcher.Run() == pkgAcquire::Failed)
2586 {
2587 delete[] Dsc;
2588 return false;
2589 }
2590
2591 // Print error messages
2592 bool Failed = false;
2593 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
2594 {
2595 if ((*I)->Status == pkgAcquire::Item::StatDone &&
2596 (*I)->Complete == true)
2597 continue;
2598
2599 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
2600 (*I)->ErrorText.c_str());
2601 Failed = true;
2602 }
2603 if (Failed == true)
2604 {
2605 delete[] Dsc;
2606 return _error->Error(_("Failed to fetch some archives."));
2607 }
2608
2609 if (_config->FindB("APT::Get::Download-only",false) == true)
2610 {
2611 c1out << _("Download complete and in download only mode") << endl;
2612 delete[] Dsc;
2613 return true;
2614 }
2615
2616 // Unpack the sources
2617 pid_t Process = ExecFork();
2618
2619 if (Process == 0)
2620 {
2621 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
2622 for (unsigned I = 0; I != J; ++I)
2623 {
2624 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
2625
2626 // Diff only mode only fetches .diff files
2627 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
2628 _config->FindB("APT::Get::Tar-Only",false) == true ||
2629 Dsc[I].Dsc.empty() == true)
2630 continue;
2631
2632 // See if the package is already unpacked
2633 struct stat Stat;
2634 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
2635 S_ISDIR(Stat.st_mode) != 0)
2636 {
2637 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
2638 Dir.c_str());
2639 }
2640 else
2641 {
2642 // Call dpkg-source
2643 char S[500];
2644 snprintf(S,sizeof(S),"%s -x %s",
2645 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
2646 Dsc[I].Dsc.c_str());
2647 if (system(S) != 0)
2648 {
2649 fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
2650 fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
2651 _exit(1);
2652 }
2653 }
2654
2655 // Try to compile it with dpkg-buildpackage
2656 if (_config->FindB("APT::Get::Compile",false) == true)
2657 {
2658 string buildopts = _config->Find("APT::Get::Host-Architecture");
2659 if (buildopts.empty() == false)
2660 buildopts = "-a " + buildopts + " ";
2661 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
2662
2663 // Call dpkg-buildpackage
2664 char S[500];
2665 snprintf(S,sizeof(S),"cd %s && %s %s",
2666 Dir.c_str(),
2667 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
2668 buildopts.c_str());
2669
2670 if (system(S) != 0)
2671 {
2672 fprintf(stderr,_("Build command '%s' failed.\n"),S);
2673 _exit(1);
2674 }
2675 }
2676 }
2677
2678 _exit(0);
2679 }
2680 delete[] Dsc;
2681
2682 // Wait for the subprocess
2683 int Status = 0;
2684 while (waitpid(Process,&Status,0) != Process)
2685 {
2686 if (errno == EINTR)
2687 continue;
2688 return _error->Errno("waitpid","Couldn't wait for subprocess");
2689 }
2690
2691 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
2692 return _error->Error(_("Child process failed"));
2693
2694 return true;
2695 }
2696 /*}}}*/
2697 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
2698 // ---------------------------------------------------------------------
2699 /* This function will look at the build depends list of the given source
2700 package and install the necessary packages to make it true, or fail. */
2701 bool DoBuildDep(CommandLine &CmdL)
2702 {
2703 CacheFile Cache;
2704
2705 _config->Set("APT::Install-Recommends", false);
2706
2707 if (Cache.Open(true) == false)
2708 return false;
2709
2710 if (CmdL.FileSize() <= 1)
2711 return _error->Error(_("Must specify at least one package to check builddeps for"));
2712
2713 // Read the source list
2714 if (Cache.BuildSourceList() == false)
2715 return false;
2716 pkgSourceList *List = Cache.GetSourceList();
2717
2718 // Create the text record parsers
2719 pkgRecords Recs(Cache);
2720 pkgSrcRecords SrcRecs(*List);
2721 if (_error->PendingError() == true)
2722 return false;
2723
2724 // Create the download object
2725 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
2726 pkgAcquire Fetcher;
2727 if (Fetcher.Setup(&Stat) == false)
2728 return false;
2729
2730 bool StripMultiArch;
2731 string hostArch = _config->Find("APT::Get::Host-Architecture");
2732 if (hostArch.empty() == false)
2733 {
2734 std::vector<std::string> archs = APT::Configuration::getArchitectures();
2735 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
2736 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
2737 StripMultiArch = false;
2738 }
2739 else
2740 StripMultiArch = true;
2741
2742 unsigned J = 0;
2743 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
2744 {
2745 string Src;
2746 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
2747 if (Last == 0)
2748 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
2749
2750 // Process the build-dependencies
2751 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
2752 if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
2753 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
2754
2755 // Also ensure that build-essential packages are present
2756 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
2757 if (Opts)
2758 Opts = Opts->Child;
2759 for (; Opts; Opts = Opts->Next)
2760 {
2761 if (Opts->Value.empty() == true)
2762 continue;
2763
2764 pkgSrcRecords::Parser::BuildDepRec rec;
2765 rec.Package = Opts->Value;
2766 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
2767 rec.Op = 0;
2768 BuildDeps.push_back(rec);
2769 }
2770
2771 if (BuildDeps.empty() == true)
2772 {
2773 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
2774 continue;
2775 }
2776
2777 // Install the requested packages
2778 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
2779 pkgProblemResolver Fix(Cache);
2780 bool skipAlternatives = false; // skip remaining alternatives in an or group
2781 for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
2782 {
2783 bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
2784
2785 if (skipAlternatives == true)
2786 {
2787 /*
2788 * if there are alternatives, we've already picked one, so skip
2789 * the rest
2790 *
2791 * TODO: this means that if there's a build-dep on A|B and B is
2792 * installed, we'll still try to install A; more importantly,
2793 * if A is currently broken, we cannot go back and try B. To fix
2794 * this would require we do a Resolve cycle for each package we
2795 * add to the install list. Ugh
2796 */
2797 if (!hasAlternatives)
2798 skipAlternatives = false; // end of or group
2799 continue;
2800 }
2801
2802 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
2803 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
2804 {
2805 pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
2806 // Build-conflicts on unknown packages are silently ignored
2807 if (Grp.end() == true)
2808 continue;
2809
2810 for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
2811 {
2812 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
2813 /*
2814 * Remove if we have an installed version that satisfies the
2815 * version criteria
2816 */
2817 if (IV.end() == false &&
2818 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2819 TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
2820 }
2821 }
2822 else // BuildDep || BuildDepIndep
2823 {
2824 if (_config->FindB("Debug::BuildDeps",false) == true)
2825 cout << "Looking for " << (*D).Package << "...\n";
2826
2827 pkgCache::PkgIterator Pkg;
2828
2829 // Cross-Building?
2830 if (StripMultiArch == false)
2831 {
2832 size_t const colon = D->Package.find(":");
2833 if (colon != string::npos &&
2834 (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0))
2835 Pkg = Cache->FindPkg(D->Package.substr(0,colon));
2836 else
2837 Pkg = Cache->FindPkg(D->Package);
2838
2839 // We need to decide if host or build arch, so find a version we can look at
2840 pkgCache::VerIterator Ver;
2841
2842 // a bad version either is invalid or doesn't satify dependency
2843 #define BADVER(Ver) Ver.end() == true || \
2844 (Ver.end() == false && D->Version.empty() == false && \
2845 Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false)
2846
2847 if (Pkg.end() == false)
2848 {
2849 Ver = (*Cache)[Pkg].InstVerIter(*Cache);
2850 if (BADVER(Ver))
2851 Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
2852 }
2853 if (BADVER(Ver))
2854 {
2855 pkgCache::PkgIterator HostPkg = Cache->FindPkg(D->Package, hostArch);
2856 if (HostPkg.end() == false)
2857 {
2858 Ver = (*Cache)[HostPkg].InstVerIter(*Cache);
2859 if (BADVER(Ver))
2860 Ver = (*Cache)[HostPkg].CandidateVerIter(*Cache);
2861 }
2862 }
2863 if ((BADVER(Ver)) == false)
2864 {
2865 string forbidden;
2866 if (Ver->MultiArch == pkgCache::Version::None || Ver->MultiArch == pkgCache::Version::All);
2867 else if (Ver->MultiArch == pkgCache::Version::Same)
2868 {
2869 if (colon != string::npos)
2870 Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
2871 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
2872 forbidden = "Multi-Arch: same";
2873 // :native gets the buildArch
2874 }
2875 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
2876 {
2877 if (colon != string::npos)
2878 forbidden = "Multi-Arch: foreign";
2879 }
2880 else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
2881 {
2882 if (colon == string::npos)
2883 Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
2884 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
2885 {
2886 // prefer any installed over preferred non-installed architectures
2887 pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
2888 // we don't check for version here as we are better of with upgrading than remove and install
2889 for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
2890 if (Pkg.CurrentVer().end() == false)
2891 break;
2892 if (Pkg.end() == true)
2893 Pkg = Grp.FindPreferredPkg(true);
2894 }
2895 // native gets buildArch
2896 }
2897 if (forbidden.empty() == false)
2898 {
2899 if (_config->FindB("Debug::BuildDeps",false) == true)
2900 cout << " :any is not allowed from M-A: same package " << (*D).Package << endl;
2901 if (hasAlternatives)
2902 continue;
2903 return _error->Error(_("%s dependency for %s can't be satisfied "
2904 "because %s is not allowed on '%s' packages"),
2905 Last->BuildDepType(D->Type), Src.c_str(),
2906 D->Package.c_str(), "Multi-Arch: same");
2907 }
2908 }
2909 else if (_config->FindB("Debug::BuildDeps",false) == true)
2910 cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
2911 #undef BADVER
2912 }
2913 else
2914 Pkg = Cache->FindPkg(D->Package);
2915
2916 if (Pkg.end() == true)
2917 {
2918 if (_config->FindB("Debug::BuildDeps",false) == true)
2919 cout << " (not found)" << (*D).Package << endl;
2920
2921 if (hasAlternatives)
2922 continue;
2923
2924 return _error->Error(_("%s dependency for %s cannot be satisfied "
2925 "because the package %s cannot be found"),
2926 Last->BuildDepType((*D).Type),Src.c_str(),
2927 (*D).Package.c_str());
2928 }
2929
2930 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
2931 if (IV.end() == false)
2932 {
2933 if (_config->FindB("Debug::BuildDeps",false) == true)
2934 cout << " Is installed\n";
2935
2936 if (D->Version.empty() == true ||
2937 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2938 {
2939 skipAlternatives = hasAlternatives;
2940 continue;
2941 }
2942
2943 if (_config->FindB("Debug::BuildDeps",false) == true)
2944 cout << " ...but the installed version doesn't meet the version requirement\n";
2945
2946 if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
2947 return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
2948 Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
2949 }
2950
2951 // Only consider virtual packages if there is no versioned dependency
2952 if ((*D).Version.empty() == true)
2953 {
2954 /*
2955 * If this is a virtual package, we need to check the list of
2956 * packages that provide it and see if any of those are
2957 * installed
2958 */
2959 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
2960 for (; Prv.end() != true; ++Prv)
2961 {
2962 if (_config->FindB("Debug::BuildDeps",false) == true)
2963 cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
2964
2965 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
2966 break;
2967 }
2968
2969 if (Prv.end() == false)
2970 {
2971 if (_config->FindB("Debug::BuildDeps",false) == true)
2972 cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
2973 skipAlternatives = hasAlternatives;
2974 continue;
2975 }
2976 }
2977 else // versioned dependency
2978 {
2979 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
2980 if (CV.end() == true ||
2981 Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
2982 {
2983 if (hasAlternatives)
2984 continue;
2985 else if (CV.end() == false)
2986 return _error->Error(_("%s dependency for %s cannot be satisfied "
2987 "because candidate version of package %s "
2988 "can't satisfy version requirements"),
2989 Last->BuildDepType(D->Type), Src.c_str(),
2990 D->Package.c_str());
2991 else
2992 return _error->Error(_("%s dependency for %s cannot be satisfied "
2993 "because package %s has no candidate version"),
2994 Last->BuildDepType(D->Type), Src.c_str(),
2995 D->Package.c_str());
2996 }
2997 }
2998
2999 if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false) == true)
3000 {
3001 // We successfully installed something; skip remaining alternatives
3002 skipAlternatives = hasAlternatives;
3003 if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
3004 Cache->MarkAuto(Pkg, true);
3005 continue;
3006 }
3007 else if (hasAlternatives)
3008 {
3009 if (_config->FindB("Debug::BuildDeps",false) == true)
3010 cout << " Unsatisfiable, trying alternatives\n";
3011 continue;
3012 }
3013 else
3014 {
3015 return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
3016 Last->BuildDepType((*D).Type),
3017 Src.c_str(),
3018 (*D).Package.c_str());
3019 }
3020 }
3021 }
3022
3023 Fix.InstallProtect();
3024 if (Fix.Resolve(true) == false)
3025 _error->Discard();
3026
3027 // Now we check the state of the packages,
3028 if (Cache->BrokenCount() != 0)
3029 {
3030 ShowBroken(cout, Cache, false);
3031 return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
3032 }
3033 }
3034
3035 if (InstallPackages(Cache, false, true) == false)
3036 return _error->Error(_("Failed to process build dependencies"));
3037 return true;
3038 }
3039 /*}}}*/
3040 // GetChangelogPath - return a path pointing to a changelog file or dir /*{{{*/
3041 // ---------------------------------------------------------------------
3042 /* This returns a "path" string for the changelog url construction.
3043 * Please note that its not complete, it either needs a "/changelog"
3044 * appended (for the packages.debian.org/changelogs site) or a
3045 * ".changelog" (for third party sites that store the changelog in the
3046 * pool/ next to the deb itself)
3047 * Example return: "pool/main/a/apt/apt_0.8.8ubuntu3"
3048 */
3049 string GetChangelogPath(CacheFile &Cache,
3050 pkgCache::PkgIterator Pkg,
3051 pkgCache::VerIterator Ver)
3052 {
3053 string path;
3054
3055 pkgRecords Recs(Cache);
3056 pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
3057 string srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
3058 string ver = Ver.VerStr();
3059 // if there is a source version it always wins
3060 if (rec.SourceVer() != "")
3061 ver = rec.SourceVer();
3062 path = flNotFile(rec.FileName());
3063 path += srcpkg + "_" + StripEpoch(ver);
3064 return path;
3065 }
3066 /*}}}*/
3067 // GuessThirdPartyChangelogUri - return url /*{{{*/
3068 // ---------------------------------------------------------------------
3069 /* Contruct a changelog file path for third party sites that do not use
3070 * packages.debian.org/changelogs
3071 * This simply uses the ArchiveURI() of the source pkg and looks for
3072 * a .changelog file there, Example for "mediabuntu":
3073 * apt-get changelog mplayer-doc:
3074 * http://packages.medibuntu.org/pool/non-free/m/mplayer/mplayer_1.0~rc4~try1.dsfg1-1ubuntu1+medibuntu1.changelog
3075 */
3076 bool GuessThirdPartyChangelogUri(CacheFile &Cache,
3077 pkgCache::PkgIterator Pkg,
3078 pkgCache::VerIterator Ver,
3079 string &out_uri)
3080 {
3081 // get the binary deb server path
3082 pkgCache::VerFileIterator Vf = Ver.FileList();
3083 if (Vf.end() == true)
3084 return false;
3085 pkgCache::PkgFileIterator F = Vf.File();
3086 pkgIndexFile *index;
3087 pkgSourceList *SrcList = Cache.GetSourceList();
3088 if(SrcList->FindIndex(F, index) == false)
3089 return false;
3090
3091 // get archive uri for the binary deb
3092 string path_without_dot_changelog = GetChangelogPath(Cache, Pkg, Ver);
3093 out_uri = index->ArchiveURI(path_without_dot_changelog + ".changelog");
3094
3095 // now strip away the filename and add srcpkg_srcver.changelog
3096 return true;
3097 }
3098 /*}}}*/
3099 // DownloadChangelog - Download the changelog /*{{{*/
3100 // ---------------------------------------------------------------------
3101 bool DownloadChangelog(CacheFile &CacheFile, pkgAcquire &Fetcher,
3102 pkgCache::VerIterator Ver, string targetfile)
3103 /* Download a changelog file for the given package version to
3104 * targetfile. This will first try the server from Apt::Changelogs::Server
3105 * (http://packages.debian.org/changelogs by default) and if that gives
3106 * a 404 tries to get it from the archive directly (see
3107 * GuessThirdPartyChangelogUri for details how)
3108 */
3109 {
3110 string path;
3111 string descr;
3112 string server;
3113 string changelog_uri;
3114
3115 // data structures we need
3116 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
3117
3118 // make the server root configurable
3119 server = _config->Find("Apt::Changelogs::Server",
3120 "http://packages.debian.org/changelogs");
3121 path = GetChangelogPath(CacheFile, Pkg, Ver);
3122 strprintf(changelog_uri, "%s/%s/changelog", server.c_str(), path.c_str());
3123 if (_config->FindB("APT::Get::Print-URIs", false) == true)
3124 {
3125 std::cout << '\'' << changelog_uri << '\'' << std::endl;
3126 return true;
3127 }
3128
3129 strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), changelog_uri.c_str());
3130 // queue it
3131 new pkgAcqFile(&Fetcher, changelog_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
3132
3133 // try downloading it, if that fails, try third-party-changelogs location
3134 // FIXME: Fetcher.Run() is "Continue" even if I get a 404?!?
3135 Fetcher.Run();
3136 if (!FileExists(targetfile))
3137 {
3138 string third_party_uri;
3139 if (GuessThirdPartyChangelogUri(CacheFile, Pkg, Ver, third_party_uri))
3140 {
3141 strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), third_party_uri.c_str());
3142 new pkgAcqFile(&Fetcher, third_party_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
3143 Fetcher.Run();
3144 }
3145 }
3146
3147 if (FileExists(targetfile))
3148 return true;
3149
3150 // error
3151 return _error->Error("changelog download failed");
3152 }
3153 /*}}}*/
3154 // DisplayFileInPager - Display File with pager /*{{{*/
3155 void DisplayFileInPager(string filename)
3156 {
3157 pid_t Process = ExecFork();
3158 if (Process == 0)
3159 {
3160 const char *Args[3];
3161 Args[0] = "/usr/bin/sensible-pager";
3162 Args[1] = filename.c_str();
3163 Args[2] = 0;
3164 execvp(Args[0],(char **)Args);
3165 exit(100);
3166 }
3167
3168 // Wait for the subprocess
3169 ExecWait(Process, "sensible-pager", false);
3170 }
3171 /*}}}*/
3172 // DoChangelog - Get changelog from the command line /*{{{*/
3173 // ---------------------------------------------------------------------
3174 bool DoChangelog(CommandLine &CmdL)
3175 {
3176 CacheFile Cache;
3177 if (Cache.ReadOnlyOpen() == false)
3178 return false;
3179
3180 APT::CacheSetHelper helper(c0out);
3181 APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
3182 CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
3183 if (verset.empty() == true)
3184 return false;
3185 pkgAcquire Fetcher;
3186
3187 if (_config->FindB("APT::Get::Print-URIs", false) == true)
3188 for (APT::VersionList::const_iterator Ver = verset.begin();
3189 Ver != verset.end(); ++Ver)
3190 return DownloadChangelog(Cache, Fetcher, Ver, "");
3191
3192 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
3193 Fetcher.Setup(&Stat);
3194
3195 bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
3196
3197 char tmpname[100];
3198 char* tmpdir = NULL;
3199 if (downOnly == false)
3200 {
3201 const char* const tmpDir = getenv("TMPDIR");
3202 if (tmpDir != NULL && *tmpDir != '\0')
3203 snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", tmpDir);
3204 else
3205 strncpy(tmpname, "/tmp/apt-changelog-XXXXXX", sizeof(tmpname));
3206 tmpdir = mkdtemp(tmpname);
3207 if (tmpdir == NULL)
3208 return _error->Errno("mkdtemp", "mkdtemp failed");
3209 }
3210
3211 for (APT::VersionList::const_iterator Ver = verset.begin();
3212 Ver != verset.end();
3213 ++Ver)
3214 {
3215 string changelogfile;
3216 if (downOnly == false)
3217 changelogfile.append(tmpname).append("changelog");
3218 else
3219 changelogfile.append(Ver.ParentPkg().Name()).append(".changelog");
3220 if (DownloadChangelog(Cache, Fetcher, Ver, changelogfile) && downOnly == false)
3221 {
3222 DisplayFileInPager(changelogfile);
3223 // cleanup temp file
3224 unlink(changelogfile.c_str());
3225 }
3226 }
3227 // clenaup tmp dir
3228 if (tmpdir != NULL)
3229 rmdir(tmpdir);
3230 return true;
3231 }
3232 /*}}}*/
3233 // DoMoo - Never Ask, Never Tell /*{{{*/
3234 // ---------------------------------------------------------------------
3235 /* */
3236 bool DoMoo(CommandLine &CmdL)
3237 {
3238 cout <<
3239 " (__) \n"
3240 " (oo) \n"
3241 " /------\\/ \n"
3242 " / | || \n"
3243 " * /\\---/\\ \n"
3244 " ~~ ~~ \n"
3245 "....\"Have you mooed today?\"...\n";
3246
3247 return true;
3248 }
3249 /*}}}*/
3250 // ShowHelp - Show a help screen /*{{{*/
3251 // ---------------------------------------------------------------------
3252 /* */
3253 bool ShowHelp(CommandLine &CmdL)
3254 {
3255 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
3256 COMMON_ARCH,__DATE__,__TIME__);
3257
3258 if (_config->FindB("version") == true)
3259 {
3260 cout << _("Supported modules:") << endl;
3261
3262 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
3263 {
3264 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
3265 if (_system != 0 && _system->VS == VS)
3266 cout << '*';
3267 else
3268 cout << ' ';
3269 cout << "Ver: " << VS->Label << endl;
3270
3271 /* Print out all the packaging systems that will work with
3272 this VS */
3273 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
3274 {
3275 pkgSystem *Sys = pkgSystem::GlobalList[J];
3276 if (_system == Sys)
3277 cout << '*';
3278 else
3279 cout << ' ';
3280 if (Sys->VS->TestCompatibility(*VS) == true)
3281 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
3282 }
3283 }
3284
3285 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
3286 {
3287 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
3288 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
3289 }
3290
3291 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
3292 {
3293 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
3294 cout << " Idx: " << Type->Label << endl;
3295 }
3296
3297 return true;
3298 }
3299
3300 cout <<
3301 _("Usage: apt-get [options] command\n"
3302 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
3303 " apt-get [options] source pkg1 [pkg2 ...]\n"
3304 "\n"
3305 "apt-get is a simple command line interface for downloading and\n"
3306 "installing packages. The most frequently used commands are update\n"
3307 "and install.\n"
3308 "\n"
3309 "Commands:\n"
3310 " update - Retrieve new lists of packages\n"
3311 " upgrade - Perform an upgrade\n"
3312 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
3313 " remove - Remove packages\n"
3314 " autoremove - Remove automatically all unused packages\n"
3315 " purge - Remove packages and config files\n"
3316 " source - Download source archives\n"
3317 " build-dep - Configure build-dependencies for source packages\n"
3318 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
3319 " dselect-upgrade - Follow dselect selections\n"
3320 " clean - Erase downloaded archive files\n"
3321 " autoclean - Erase old downloaded archive files\n"
3322 " check - Verify that there are no broken dependencies\n"
3323 " changelog - Download and display the changelog for the given package\n"
3324 " download - Download the binary package into the current directory\n"
3325 "\n"
3326 "Options:\n"
3327 " -h This help text.\n"
3328 " -q Loggable output - no progress indicator\n"
3329 " -qq No output except for errors\n"
3330 " -d Download only - do NOT install or unpack archives\n"
3331 " -s No-act. Perform ordering simulation\n"
3332 " -y Assume Yes to all queries and do not prompt\n"
3333 " -f Attempt to correct a system with broken dependencies in place\n"
3334 " -m Attempt to continue if archives are unlocatable\n"
3335 " -u Show a list of upgraded packages as well\n"
3336 " -b Build the source package after fetching it\n"
3337 " -V Show verbose version numbers\n"
3338 " -c=? Read this configuration file\n"
3339 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
3340 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
3341 "pages for more information and options.\n"
3342 " This APT has Super Cow Powers.\n");
3343 return true;
3344 }
3345 /*}}}*/
3346 // SigWinch - Window size change signal handler /*{{{*/
3347 // ---------------------------------------------------------------------
3348 /* */
3349 void SigWinch(int)
3350 {
3351 // Riped from GNU ls
3352 #ifdef TIOCGWINSZ
3353 struct winsize ws;
3354
3355 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
3356 ScreenWidth = ws.ws_col - 1;
3357 #endif
3358 }
3359 /*}}}*/
3360 int main(int argc,const char *argv[]) /*{{{*/
3361 {
3362 CommandLine::Args Args[] = {
3363 {'h',"help","help",0},
3364 {'v',"version","version",0},
3365 {'V',"verbose-versions","APT::Get::Show-Versions",0},
3366 {'q',"quiet","quiet",CommandLine::IntLevel},
3367 {'q',"silent","quiet",CommandLine::IntLevel},
3368 {'d',"download-only","APT::Get::Download-Only",0},
3369 {'b',"compile","APT::Get::Compile",0},
3370 {'b',"build","APT::Get::Compile",0},
3371 {'s',"simulate","APT::Get::Simulate",0},
3372 {'s',"just-print","APT::Get::Simulate",0},
3373 {'s',"recon","APT::Get::Simulate",0},
3374 {'s',"dry-run","APT::Get::Simulate",0},
3375 {'s',"no-act","APT::Get::Simulate",0},
3376 {'y',"yes","APT::Get::Assume-Yes",0},
3377 {'y',"assume-yes","APT::Get::Assume-Yes",0},
3378 {0,"assume-no","APT::Get::Assume-No",0},
3379 {'f',"fix-broken","APT::Get::Fix-Broken",0},
3380 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
3381 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
3382 {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
3383 {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
3384 {'a',"host-architecture","APT::Get::Host-Architecture",CommandLine::HasArg},
3385 {0,"download","APT::Get::Download",0},
3386 {0,"fix-missing","APT::Get::Fix-Missing",0},
3387 {0,"ignore-hold","APT::Ignore-Hold",0},
3388 {0,"upgrade","APT::Get::upgrade",0},
3389 {0,"only-upgrade","APT::Get::Only-Upgrade",0},
3390 {0,"force-yes","APT::Get::force-yes",0},
3391 {0,"print-uris","APT::Get::Print-URIs",0},
3392 {0,"diff-only","APT::Get::Diff-Only",0},
3393 {0,"debian-only","APT::Get::Diff-Only",0},
3394 {0,"tar-only","APT::Get::Tar-Only",0},
3395 {0,"dsc-only","APT::Get::Dsc-Only",0},
3396 {0,"purge","APT::Get::Purge",0},
3397 {0,"list-cleanup","APT::Get::List-Cleanup",0},
3398 {0,"reinstall","APT::Get::ReInstall",0},
3399 {0,"trivial-only","APT::Get::Trivial-Only",0},
3400 {0,"remove","APT::Get::Remove",0},
3401 {0,"only-source","APT::Get::Only-Source",0},
3402 {0,"arch-only","APT::Get::Arch-Only",0},
3403 {0,"auto-remove","APT::Get::AutomaticRemove",0},
3404 {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
3405 {0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean},
3406 {0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean},
3407 {0,"fix-policy","APT::Get::Fix-Policy-Broken",0},
3408 {0,"solver","APT::Solver",CommandLine::HasArg},
3409 {'c',"config-file",0,CommandLine::ConfigFile},
3410 {'o',"option",0,CommandLine::ArbItem},
3411 {0,0,0,0}};
3412 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
3413 {"upgrade",&DoUpgrade},
3414 {"install",&DoInstall},
3415 {"remove",&DoInstall},
3416 {"purge",&DoInstall},
3417 {"autoremove",&DoInstall},
3418 {"markauto",&DoMarkAuto},
3419 {"unmarkauto",&DoMarkAuto},
3420 {"dist-upgrade",&DoDistUpgrade},
3421 {"dselect-upgrade",&DoDSelectUpgrade},
3422 {"build-dep",&DoBuildDep},
3423 {"clean",&DoClean},
3424 {"autoclean",&DoAutoClean},
3425 {"check",&DoCheck},
3426 {"source",&DoSource},
3427 {"download",&DoDownload},
3428 {"changelog",&DoChangelog},
3429 {"moo",&DoMoo},
3430 {"help",&ShowHelp},
3431 {0,0}};
3432
3433 // Set up gettext support
3434 setlocale(LC_ALL,"");
3435 textdomain(PACKAGE);
3436
3437 // Parse the command line and initialize the package library
3438 CommandLine CmdL(Args,_config);
3439 if (pkgInitConfig(*_config) == false ||
3440 CmdL.Parse(argc,argv) == false ||
3441 pkgInitSystem(*_config,_system) == false)
3442 {
3443 if (_config->FindB("version") == true)
3444 ShowHelp(CmdL);
3445
3446 _error->DumpErrors();
3447 return 100;
3448 }
3449
3450 // See if the help should be shown
3451 if (_config->FindB("help") == true ||
3452 _config->FindB("version") == true ||
3453 CmdL.FileSize() == 0)
3454 {
3455 ShowHelp(CmdL);
3456 return 0;
3457 }
3458
3459 // simulate user-friendly if apt-get has no root privileges
3460 if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true &&
3461 (CmdL.FileSize() == 0 ||
3462 (strcmp(CmdL.FileList[0], "source") != 0 && strcmp(CmdL.FileList[0], "download") != 0 &&
3463 strcmp(CmdL.FileList[0], "changelog") != 0)))
3464 {
3465 if (_config->FindB("APT::Get::Show-User-Simulation-Note",true) == true)
3466 cout << _("NOTE: This is only a simulation!\n"
3467 " apt-get needs root privileges for real execution.\n"
3468 " Keep also in mind that locking is deactivated,\n"
3469 " so don't depend on the relevance to the real current situation!"
3470 ) << std::endl;
3471 _config->Set("Debug::NoLocking",true);
3472 }
3473
3474 // Deal with stdout not being a tty
3475 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
3476 _config->Set("quiet","1");
3477
3478 // Setup the output streams
3479 c0out.rdbuf(cout.rdbuf());
3480 c1out.rdbuf(cout.rdbuf());
3481 c2out.rdbuf(cout.rdbuf());
3482 if (_config->FindI("quiet",0) > 0)
3483 c0out.rdbuf(devnull.rdbuf());
3484 if (_config->FindI("quiet",0) > 1)
3485 c1out.rdbuf(devnull.rdbuf());
3486
3487 // Setup the signals
3488 signal(SIGPIPE,SIG_IGN);
3489 signal(SIGWINCH,SigWinch);
3490 SigWinch(0);
3491
3492 // Match the operation
3493 CmdL.DispatchArg(Cmds);
3494
3495 // Print any errors or warnings found during parsing
3496 bool const Errors = _error->PendingError();
3497 if (_config->FindI("quiet",0) > 0)
3498 _error->DumpErrors();
3499 else
3500 _error->DumpErrors(GlobalError::DEBUG);
3501 return Errors == true ? 100 : 0;
3502 }
3503 /*}}}*/