]> git.saurik.com Git - apt.git/blob - cmdline/apt-get.cc
'apt-get update' no longer does 'Building Dependency Tree'.
[apt.git] / cmdline / apt-get.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-get.cc,v 1.120 2002/04/27 04:28:04 jgg 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 <apt-pkg/error.h>
29 #include <apt-pkg/cmndline.h>
30 #include <apt-pkg/init.h>
31 #include <apt-pkg/depcache.h>
32 #include <apt-pkg/sourcelist.h>
33 #include <apt-pkg/algorithms.h>
34 #include <apt-pkg/acquire-item.h>
35 #include <apt-pkg/strutl.h>
36 #include <apt-pkg/clean.h>
37 #include <apt-pkg/srcrecords.h>
38 #include <apt-pkg/version.h>
39 #include <apt-pkg/cachefile.h>
40 #include <apt-pkg/sptr.h>
41 #include <apt-pkg/versionmatch.h>
42
43 #include <config.h>
44 #include <apti18n.h>
45
46 #include "acqprogress.h"
47
48 #include <locale.h>
49 #include <fstream>
50 #include <termios.h>
51 #include <sys/ioctl.h>
52 #include <sys/stat.h>
53 #include <sys/statvfs.h>
54 #include <signal.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <errno.h>
58 #include <regex.h>
59 #include <sys/wait.h>
60 /*}}}*/
61
62 using namespace std;
63
64 ostream c0out(0);
65 ostream c1out(0);
66 ostream c2out(0);
67 ofstream devnull("/dev/null");
68 unsigned int ScreenWidth = 80;
69
70 // class CacheFile - Cover class for some dependency cache functions /*{{{*/
71 // ---------------------------------------------------------------------
72 /* */
73 class CacheFile : public pkgCacheFile
74 {
75 static pkgCache *SortCache;
76 static int NameComp(const void *a,const void *b);
77
78 public:
79 pkgCache::Package **List;
80
81 void Sort();
82 bool CheckDeps(bool AllowBroken = false);
83 bool BuildCaches(bool WithLock = true)
84 {
85 OpTextProgress Prog(*_config);
86 if (pkgCacheFile::BuildCaches(Prog,WithLock) == false)
87 return false;
88 return true;
89 }
90 bool Open(bool WithLock = true)
91 {
92 OpTextProgress Prog(*_config);
93 if (pkgCacheFile::Open(Prog,WithLock) == false)
94 return false;
95 Sort();
96
97 return true;
98 };
99 bool OpenForInstall()
100 {
101 if (_config->FindB("APT::Get::Print-URIs") == true)
102 return Open(false);
103 else
104 return Open(true);
105 }
106 CacheFile() : List(0) {};
107 };
108 /*}}}*/
109
110 // YnPrompt - Yes No Prompt. /*{{{*/
111 // ---------------------------------------------------------------------
112 /* Returns true on a Yes.*/
113 bool YnPrompt()
114 {
115 // This needs to be a capital
116 const char *Yes = _("Y");
117
118 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
119 {
120 c1out << Yes << endl;
121 return true;
122 }
123
124 char C = 0;
125 char Jnk = 0;
126 if (read(STDIN_FILENO,&C,1) != 1)
127 return false;
128 while (C != '\n' && Jnk != '\n')
129 if (read(STDIN_FILENO,&Jnk,1) != 1)
130 return false;
131
132 if (!(toupper(C) == *Yes || C == '\n' || C == '\r'))
133 return false;
134 return true;
135 }
136 /*}}}*/
137 // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
138 // ---------------------------------------------------------------------
139 /* Returns true on a Yes.*/
140 bool AnalPrompt(const char *Text)
141 {
142 char Buf[1024];
143 cin.getline(Buf,sizeof(Buf));
144 if (strcmp(Buf,Text) == 0)
145 return true;
146 return false;
147 }
148 /*}}}*/
149 // ShowList - Show a list /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This prints out a string of space separated words with a title and
152 a two space indent line wraped to the current screen width. */
153 bool ShowList(ostream &out,string Title,string List)
154 {
155 if (List.empty() == true)
156 return true;
157
158 // Acount for the leading space
159 int ScreenWidth = ::ScreenWidth - 3;
160
161 out << Title << endl;
162 string::size_type Start = 0;
163 while (Start < List.size())
164 {
165 string::size_type End;
166 if (Start + ScreenWidth >= List.size())
167 End = List.size();
168 else
169 End = List.rfind(' ',Start+ScreenWidth);
170
171 if (End == string::npos || End < Start)
172 End = Start + ScreenWidth;
173 out << " " << string(List,Start,End - Start) << endl;
174 Start = End + 1;
175 }
176 return false;
177 }
178 /*}}}*/
179 // ShowBroken - Debugging aide /*{{{*/
180 // ---------------------------------------------------------------------
181 /* This prints out the names of all the packages that are broken along
182 with the name of each each broken dependency and a quite version
183 description.
184
185 The output looks like:
186 Sorry, but the following packages have unmet dependencies:
187 exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
188 Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
189 Depends: libsasl7 but it is not going to be installed
190 */
191 void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
192 {
193 out << _("Sorry, but the following packages have unmet dependencies:") << endl;
194 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
195 {
196 pkgCache::PkgIterator I(Cache,Cache.List[J]);
197
198 if (Now == true)
199 {
200 if (Cache[I].NowBroken() == false)
201 continue;
202 }
203 else
204 {
205 if (Cache[I].InstBroken() == false)
206 continue;
207 }
208
209 // Print out each package and the failed dependencies
210 out <<" " << I.Name() << ":";
211 unsigned Indent = strlen(I.Name()) + 3;
212 bool First = true;
213 pkgCache::VerIterator Ver;
214
215 if (Now == true)
216 Ver = I.CurrentVer();
217 else
218 Ver = Cache[I].InstVerIter(Cache);
219
220 if (Ver.end() == true)
221 {
222 out << endl;
223 continue;
224 }
225
226 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
227 {
228 // Compute a single dependency element (glob or)
229 pkgCache::DepIterator Start;
230 pkgCache::DepIterator End;
231 D.GlobOr(Start,End);
232
233 if (Cache->IsImportantDep(End) == false)
234 continue;
235
236 if (Now == true)
237 {
238 if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
239 continue;
240 }
241 else
242 {
243 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
244 continue;
245 }
246
247 bool FirstOr = true;
248 while (1)
249 {
250 if (First == false)
251 for (unsigned J = 0; J != Indent; J++)
252 out << ' ';
253 First = false;
254
255 if (FirstOr == false)
256 {
257 for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
258 out << ' ';
259 }
260 else
261 out << ' ' << End.DepType() << ": ";
262 FirstOr = false;
263
264 out << Start.TargetPkg().Name();
265
266 // Show a quick summary of the version requirements
267 if (Start.TargetVer() != 0)
268 out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
269
270 /* Show a summary of the target package if possible. In the case
271 of virtual packages we show nothing */
272 pkgCache::PkgIterator Targ = Start.TargetPkg();
273 if (Targ->ProvidesList == 0)
274 {
275 out << ' ';
276 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
277 if (Now == true)
278 Ver = Targ.CurrentVer();
279
280 if (Ver.end() == false)
281 {
282 if (Now == true)
283 ioprintf(out,_("but %s is installed"),Ver.VerStr());
284 else
285 ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
286 }
287 else
288 {
289 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
290 {
291 if (Targ->ProvidesList == 0)
292 out << _("but it is not installable");
293 else
294 out << _("but it is a virtual package");
295 }
296 else
297 out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
298 }
299 }
300
301 if (Start != End)
302 out << _(" or");
303 out << endl;
304
305 if (Start == End)
306 break;
307 Start++;
308 }
309 }
310 }
311 }
312 /*}}}*/
313 // ShowNew - Show packages to newly install /*{{{*/
314 // ---------------------------------------------------------------------
315 /* */
316 void ShowNew(ostream &out,CacheFile &Cache)
317 {
318 /* Print out a list of packages that are going to be removed extra
319 to what the user asked */
320 string List;
321 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
322 {
323 pkgCache::PkgIterator I(Cache,Cache.List[J]);
324 if (Cache[I].NewInstall() == true)
325 List += string(I.Name()) + " ";
326 }
327
328 ShowList(out,_("The following NEW packages will be installed:"),List);
329 }
330 /*}}}*/
331 // ShowDel - Show packages to delete /*{{{*/
332 // ---------------------------------------------------------------------
333 /* */
334 void ShowDel(ostream &out,CacheFile &Cache)
335 {
336 /* Print out a list of packages that are going to be removed extra
337 to what the user asked */
338 string List;
339 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
340 {
341 pkgCache::PkgIterator I(Cache,Cache.List[J]);
342 if (Cache[I].Delete() == true)
343 {
344 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
345 List += string(I.Name()) + "* ";
346 else
347 List += string(I.Name()) + " ";
348 }
349 }
350
351 ShowList(out,_("The following packages will be REMOVED:"),List);
352 }
353 /*}}}*/
354 // ShowKept - Show kept packages /*{{{*/
355 // ---------------------------------------------------------------------
356 /* */
357 void ShowKept(ostream &out,CacheFile &Cache)
358 {
359 string List;
360 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
361 {
362 pkgCache::PkgIterator I(Cache,Cache.List[J]);
363
364 // Not interesting
365 if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
366 I->CurrentVer == 0 || Cache[I].Delete() == true)
367 continue;
368
369 List += string(I.Name()) + " ";
370 }
371 ShowList(out,_("The following packages have been kept back"),List);
372 }
373 /*}}}*/
374 // ShowUpgraded - Show upgraded packages /*{{{*/
375 // ---------------------------------------------------------------------
376 /* */
377 void ShowUpgraded(ostream &out,CacheFile &Cache)
378 {
379 string List;
380 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
381 {
382 pkgCache::PkgIterator I(Cache,Cache.List[J]);
383
384 // Not interesting
385 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
386 continue;
387
388 List += string(I.Name()) + " ";
389 }
390 ShowList(out,_("The following packages will be upgraded"),List);
391 }
392 /*}}}*/
393 // ShowDowngraded - Show downgraded packages /*{{{*/
394 // ---------------------------------------------------------------------
395 /* */
396 bool ShowDowngraded(ostream &out,CacheFile &Cache)
397 {
398 string List;
399 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
400 {
401 pkgCache::PkgIterator I(Cache,Cache.List[J]);
402
403 // Not interesting
404 if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
405 continue;
406
407 List += string(I.Name()) + " ";
408 }
409 return ShowList(out,_("The following packages will be DOWNGRADED"),List);
410 }
411 /*}}}*/
412 // ShowHold - Show held but changed packages /*{{{*/
413 // ---------------------------------------------------------------------
414 /* */
415 bool ShowHold(ostream &out,CacheFile &Cache)
416 {
417 string List;
418 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
419 {
420 pkgCache::PkgIterator I(Cache,Cache.List[J]);
421 if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
422 I->SelectedState == pkgCache::State::Hold)
423 List += string(I.Name()) + " ";
424 }
425
426 return ShowList(out,_("The following held packages will be changed:"),List);
427 }
428 /*}}}*/
429 // ShowEssential - Show an essential package warning /*{{{*/
430 // ---------------------------------------------------------------------
431 /* This prints out a warning message that is not to be ignored. It shows
432 all essential packages and their dependents that are to be removed.
433 It is insanely risky to remove the dependents of an essential package! */
434 bool ShowEssential(ostream &out,CacheFile &Cache)
435 {
436 string List;
437 bool *Added = new bool[Cache->Head().PackageCount];
438 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
439 Added[I] = false;
440
441 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
442 {
443 pkgCache::PkgIterator I(Cache,Cache.List[J]);
444 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
445 (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
446 continue;
447
448 // The essential package is being removed
449 if (Cache[I].Delete() == true)
450 {
451 if (Added[I->ID] == false)
452 {
453 Added[I->ID] = true;
454 List += string(I.Name()) + " ";
455 }
456 }
457
458 if (I->CurrentVer == 0)
459 continue;
460
461 // Print out any essential package depenendents that are to be removed
462 for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
463 {
464 // Skip everything but depends
465 if (D->Type != pkgCache::Dep::PreDepends &&
466 D->Type != pkgCache::Dep::Depends)
467 continue;
468
469 pkgCache::PkgIterator P = D.SmartTargetPkg();
470 if (Cache[P].Delete() == true)
471 {
472 if (Added[P->ID] == true)
473 continue;
474 Added[P->ID] = true;
475
476 char S[300];
477 snprintf(S,sizeof(S),_("%s (due to %s) "),P.Name(),I.Name());
478 List += S;
479 }
480 }
481 }
482
483 delete [] Added;
484 return ShowList(out,_("WARNING: The following essential packages will be removed\n"
485 "This should NOT be done unless you know exactly what you are doing!"),List);
486 }
487 /*}}}*/
488 // Stats - Show some statistics /*{{{*/
489 // ---------------------------------------------------------------------
490 /* */
491 void Stats(ostream &out,pkgDepCache &Dep)
492 {
493 unsigned long Upgrade = 0;
494 unsigned long Downgrade = 0;
495 unsigned long Install = 0;
496 unsigned long ReInstall = 0;
497 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
498 {
499 if (Dep[I].NewInstall() == true)
500 Install++;
501 else
502 {
503 if (Dep[I].Upgrade() == true)
504 Upgrade++;
505 else
506 if (Dep[I].Downgrade() == true)
507 Downgrade++;
508 }
509
510 if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
511 ReInstall++;
512 }
513
514 ioprintf(out,_("%lu packages upgraded, %lu newly installed, "),
515 Upgrade,Install);
516
517 if (ReInstall != 0)
518 ioprintf(out,_("%lu reinstalled, "),ReInstall);
519 if (Downgrade != 0)
520 ioprintf(out,_("%lu downgraded, "),Downgrade);
521
522 ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
523 Dep.DelCount(),Dep.KeepCount());
524
525 if (Dep.BadCount() != 0)
526 ioprintf(out,_("%lu packages not fully installed or removed.\n"),
527 Dep.BadCount());
528 }
529 /*}}}*/
530
531 // CacheFile::NameComp - QSort compare by name /*{{{*/
532 // ---------------------------------------------------------------------
533 /* */
534 pkgCache *CacheFile::SortCache = 0;
535 int CacheFile::NameComp(const void *a,const void *b)
536 {
537 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
538 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
539
540 const pkgCache::Package &A = **(pkgCache::Package **)a;
541 const pkgCache::Package &B = **(pkgCache::Package **)b;
542
543 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
544 }
545 /*}}}*/
546 // CacheFile::Sort - Sort by name /*{{{*/
547 // ---------------------------------------------------------------------
548 /* */
549 void CacheFile::Sort()
550 {
551 delete [] List;
552 List = new pkgCache::Package *[Cache->Head().PackageCount];
553 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
554 pkgCache::PkgIterator I = Cache->PkgBegin();
555 for (;I.end() != true; I++)
556 List[I->ID] = I;
557
558 SortCache = *this;
559 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
560 }
561 /*}}}*/
562 // CacheFile::CheckDeps - Open the cache file /*{{{*/
563 // ---------------------------------------------------------------------
564 /* This routine generates the caches and then opens the dependency cache
565 and verifies that the system is OK. */
566 bool CacheFile::CheckDeps(bool AllowBroken)
567 {
568 if (_error->PendingError() == true)
569 return false;
570
571 // Check that the system is OK
572 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
573 return _error->Error("Internal Error, non-zero counts");
574
575 // Apply corrections for half-installed packages
576 if (pkgApplyStatus(*DCache) == false)
577 return false;
578
579 // Nothing is broken
580 if (DCache->BrokenCount() == 0 || AllowBroken == true)
581 return true;
582
583 // Attempt to fix broken things
584 if (_config->FindB("APT::Get::Fix-Broken",false) == true)
585 {
586 c1out << _("Correcting dependencies...") << flush;
587 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
588 {
589 c1out << _(" failed.") << endl;
590 ShowBroken(c1out,*this,true);
591
592 return _error->Error(_("Unable to correct dependencies"));
593 }
594 if (pkgMinimizeUpgrade(*DCache) == false)
595 return _error->Error(_("Unable to minimize the upgrade set"));
596
597 c1out << _(" Done") << endl;
598 }
599 else
600 {
601 c1out << _("You might want to run `apt-get -f install' to correct these.") << endl;
602 ShowBroken(c1out,*this,true);
603
604 return _error->Error(_("Unmet dependencies. Try using -f."));
605 }
606
607 return true;
608 }
609 /*}}}*/
610
611 // InstallPackages - Actually download and install the packages /*{{{*/
612 // ---------------------------------------------------------------------
613 /* This displays the informative messages describing what is going to
614 happen and then calls the download routines */
615 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
616 bool Saftey = true)
617 {
618 if (_config->FindB("APT::Get::Purge",false) == true)
619 {
620 pkgCache::PkgIterator I = Cache->PkgBegin();
621 for (; I.end() == false; I++)
622 {
623 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
624 Cache->MarkDelete(I,true);
625 }
626 }
627
628 bool Fail = false;
629 bool Essential = false;
630
631 // Show all the various warning indicators
632 ShowDel(c1out,Cache);
633 ShowNew(c1out,Cache);
634 if (ShwKept == true)
635 ShowKept(c1out,Cache);
636 Fail |= !ShowHold(c1out,Cache);
637 if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
638 ShowUpgraded(c1out,Cache);
639 Fail |= !ShowDowngraded(c1out,Cache);
640 Essential = !ShowEssential(c1out,Cache);
641 Fail |= Essential;
642 Stats(c1out,Cache);
643
644 // Sanity check
645 if (Cache->BrokenCount() != 0)
646 {
647 ShowBroken(c1out,Cache,false);
648 return _error->Error("Internal Error, InstallPackages was called with broken packages!");
649 }
650
651 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
652 Cache->BadCount() == 0)
653 return true;
654
655 // No remove flag
656 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
657 return _error->Error(_("Packages need to be removed but Remove is disabled."));
658
659 // Run the simulator ..
660 if (_config->FindB("APT::Get::Simulate") == true)
661 {
662 pkgSimulate PM(Cache);
663 pkgPackageManager::OrderResult Res = PM.DoInstall();
664 if (Res == pkgPackageManager::Failed)
665 return false;
666 if (Res != pkgPackageManager::Completed)
667 return _error->Error("Internal Error, Ordering didn't finish");
668 return true;
669 }
670
671 // Create the text record parser
672 pkgRecords Recs(Cache);
673 if (_error->PendingError() == true)
674 return false;
675
676 // Lock the archive directory
677 FileFd Lock;
678 if (_config->FindB("Debug::NoLocking",false) == false &&
679 _config->FindB("APT::Get::Print-URIs") == false)
680 {
681 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
682 if (_error->PendingError() == true)
683 return _error->Error(_("Unable to lock the download directory"));
684 }
685
686 // Create the download object
687 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
688 pkgAcquire Fetcher(&Stat);
689
690 // Read the source list
691 pkgSourceList List;
692 if (List.ReadMainList() == false)
693 return _error->Error(_("The list of sources could not be read."));
694
695 // Create the package manager and prepare to download
696 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
697 if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
698 _error->PendingError() == true)
699 return false;
700
701 // Display statistics
702 double FetchBytes = Fetcher.FetchNeeded();
703 double FetchPBytes = Fetcher.PartialPresent();
704 double DebBytes = Fetcher.TotalNeeded();
705 if (DebBytes != Cache->DebSize())
706 {
707 c0out << DebBytes << ',' << Cache->DebSize() << endl;
708 c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
709 }
710
711 // Number of bytes
712 if (DebBytes != FetchBytes)
713 ioprintf(c1out,_("Need to get %sB/%sB of archives. "),
714 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
715 else
716 ioprintf(c1out,_("Need to get %sB of archives. "),
717 SizeToStr(DebBytes).c_str());
718
719 // Size delta
720 if (Cache->UsrSize() >= 0)
721 ioprintf(c1out,_("After unpacking %sB will be used.\n"),
722 SizeToStr(Cache->UsrSize()).c_str());
723 else
724 ioprintf(c1out,_("After unpacking %sB will be freed.\n"),
725 SizeToStr(-1*Cache->UsrSize()).c_str());
726
727 if (_error->PendingError() == true)
728 return false;
729
730 /* Check for enough free space, but only if we are actually going to
731 download */
732 if (_config->FindB("APT::Get::Print-URIs") == false &&
733 _config->FindB("APT::Get::Download",true) == true)
734 {
735 struct statvfs Buf;
736 string OutputDir = _config->FindDir("Dir::Cache::Archives");
737 if (statvfs(OutputDir.c_str(),&Buf) != 0)
738 return _error->Errno("statvfs","Couldn't determine free space in %s",
739 OutputDir.c_str());
740 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
741 return _error->Error(_("Sorry, you don't have enough free space in %s to hold all the .debs."),
742 OutputDir.c_str());
743 }
744
745 // Fail safe check
746 if (_config->FindI("quiet",0) >= 2 ||
747 _config->FindB("APT::Get::Assume-Yes",false) == true)
748 {
749 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
750 return _error->Error(_("There are problems and -y was used without --force-yes"));
751 }
752
753 if (Essential == true && Saftey == true)
754 {
755 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
756 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
757
758 const char *Prompt = _("Yes, do as I say!");
759 ioprintf(c2out,
760 _("You are about to do something potentially harmful\n"
761 "To continue type in the phrase '%s'\n"
762 " ?] "),Prompt);
763 c2out << flush;
764 if (AnalPrompt(Prompt) == false)
765 {
766 c2out << _("Abort.") << endl;
767 exit(1);
768 }
769 }
770 else
771 {
772 // Prompt to continue
773 if (Ask == true || Fail == true)
774 {
775 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
776 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
777
778 if (_config->FindI("quiet",0) < 2 &&
779 _config->FindB("APT::Get::Assume-Yes",false) == false)
780 {
781 c2out << _("Do you want to continue? [Y/n] ") << flush;
782
783 if (YnPrompt() == false)
784 {
785 c2out << _("Abort.") << endl;
786 exit(1);
787 }
788 }
789 }
790 }
791
792 // Just print out the uris an exit if the --print-uris flag was used
793 if (_config->FindB("APT::Get::Print-URIs") == true)
794 {
795 pkgAcquire::UriIterator I = Fetcher.UriBegin();
796 for (; I != Fetcher.UriEnd(); I++)
797 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
798 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
799 return true;
800 }
801
802 /* Unlock the dpkg lock if we are not going to be doing an install
803 after. */
804 if (_config->FindB("APT::Get::Download-Only",false) == true)
805 _system->UnLock();
806
807 // Run it
808 while (1)
809 {
810 bool Transient = false;
811 if (_config->FindB("APT::Get::Download",true) == false)
812 {
813 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
814 {
815 if ((*I)->Local == true)
816 {
817 I++;
818 continue;
819 }
820
821 // Close the item and check if it was found in cache
822 (*I)->Finished();
823 if ((*I)->Complete == false)
824 Transient = true;
825
826 // Clear it out of the fetch list
827 delete *I;
828 I = Fetcher.ItemsBegin();
829 }
830 }
831
832 if (Fetcher.Run() == pkgAcquire::Failed)
833 return false;
834
835 // Print out errors
836 bool Failed = false;
837 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
838 {
839 if ((*I)->Status == pkgAcquire::Item::StatDone &&
840 (*I)->Complete == true)
841 continue;
842
843 if ((*I)->Status == pkgAcquire::Item::StatIdle)
844 {
845 Transient = true;
846 // Failed = true;
847 continue;
848 }
849
850 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
851 (*I)->ErrorText.c_str());
852 Failed = true;
853 }
854
855 /* If we are in no download mode and missing files and there were
856 'failures' then the user must specify -m. Furthermore, there
857 is no such thing as a transient error in no-download mode! */
858 if (Transient == true &&
859 _config->FindB("APT::Get::Download",true) == false)
860 {
861 Transient = false;
862 Failed = true;
863 }
864
865 if (_config->FindB("APT::Get::Download-Only",false) == true)
866 {
867 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
868 return _error->Error(_("Some files failed to download"));
869 c1out << _("Download complete and in download only mode") << endl;
870 return true;
871 }
872
873 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
874 {
875 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
876 }
877
878 if (Transient == true && Failed == true)
879 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
880
881 // Try to deal with missing package files
882 if (Failed == true && PM->FixMissing() == false)
883 {
884 cerr << _("Unable to correct missing packages.") << endl;
885 return _error->Error(_("Aborting Install."));
886 }
887
888 _system->UnLock();
889 pkgPackageManager::OrderResult Res = PM->DoInstall();
890 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
891 return false;
892 if (Res == pkgPackageManager::Completed)
893 return true;
894
895 // Reload the fetcher object and loop again for media swapping
896 Fetcher.Shutdown();
897 if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
898 return false;
899
900 _system->Lock();
901 }
902 }
903 /*}}}*/
904 // TryToInstall - Try to install a single package /*{{{*/
905 // ---------------------------------------------------------------------
906 /* This used to be inlined in DoInstall, but with the advent of regex package
907 name matching it was split out.. */
908 bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
909 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
910 unsigned int &ExpectedInst,bool AllowFail = true)
911 {
912 /* This is a pure virtual package and there is a single available
913 provides */
914 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
915 Pkg.ProvidesList()->NextProvides == 0)
916 {
917 pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
918 ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
919 Tmp.Name(),Pkg.Name());
920 Pkg = Tmp;
921 }
922
923 // Handle the no-upgrade case
924 if (_config->FindB("APT::Get::upgrade",true) == false &&
925 Pkg->CurrentVer != 0)
926 {
927 if (AllowFail == true)
928 ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
929 Pkg.Name());
930 return true;
931 }
932
933 // Check if there is something at all to install
934 pkgDepCache::StateCache &State = Cache[Pkg];
935 if (Remove == true && Pkg->CurrentVer == 0)
936 {
937 Fix.Clear(Pkg);
938 Fix.Protect(Pkg);
939 Fix.Remove(Pkg);
940
941 /* We want to continue searching for regex hits, so we return false here
942 otherwise this is not really an error. */
943 if (AllowFail == false)
944 return false;
945
946 ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
947 return true;
948 }
949
950 if (State.CandidateVer == 0 && Remove == false)
951 {
952 if (AllowFail == false)
953 return false;
954
955 if (Pkg->ProvidesList != 0)
956 {
957 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
958 Pkg.Name());
959
960 pkgCache::PrvIterator I = Pkg.ProvidesList();
961 for (; I.end() == false; I++)
962 {
963 pkgCache::PkgIterator Pkg = I.OwnerPkg();
964
965 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
966 {
967 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
968 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
969 _(" [Installed]") << endl;
970 else
971 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
972 }
973 }
974 c1out << _("You should explicitly select one to install.") << endl;
975 }
976 else
977 {
978 ioprintf(c1out,
979 _("Package %s has no available version, but exists in the database.\n"
980 "This typically means that the package was mentioned in a dependency and\n"
981 "never uploaded, has been obsoleted or is not available with the contents\n"
982 "of sources.list\n"),Pkg.Name());
983
984 string List;
985 SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
986 memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
987 pkgCache::DepIterator Dep = Pkg.RevDependsList();
988 for (; Dep.end() == false; Dep++)
989 {
990 if (Dep->Type != pkgCache::Dep::Replaces)
991 continue;
992 if (Seen[Dep.ParentPkg()->ID] == true)
993 continue;
994 Seen[Dep.ParentPkg()->ID] = true;
995 List += string(Dep.ParentPkg().Name()) + " ";
996 }
997 ShowList(c1out,_("However the following packages replace it:"),List);
998 }
999
1000 _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
1001 return false;
1002 }
1003
1004 Fix.Clear(Pkg);
1005 Fix.Protect(Pkg);
1006 if (Remove == true)
1007 {
1008 Fix.Remove(Pkg);
1009 Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
1010 return true;
1011 }
1012
1013 // Install it
1014 Cache.MarkInstall(Pkg,false);
1015 if (State.Install() == false)
1016 {
1017 if (_config->FindB("APT::Get::ReInstall",false) == true)
1018 {
1019 if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
1020 ioprintf(c1out,_("Sorry, re-installation of %s is not possible, it cannot be downloaded.\n"),
1021 Pkg.Name());
1022 else
1023 Cache.SetReInstall(Pkg,true);
1024 }
1025 else
1026 {
1027 if (AllowFail == true)
1028 ioprintf(c1out,_("Sorry, %s is already the newest version.\n"),
1029 Pkg.Name());
1030 }
1031 }
1032 else
1033 ExpectedInst++;
1034
1035 // Install it with autoinstalling enabled.
1036 if (State.InstBroken() == true && BrokenFix == false)
1037 Cache.MarkInstall(Pkg,true);
1038 return true;
1039 }
1040 /*}}}*/
1041 // TryToChangeVer - Try to change a candidate version /*{{{*/
1042 // ---------------------------------------------------------------------
1043 /* */
1044 bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
1045 const char *VerTag,bool IsRel)
1046 {
1047 pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release :
1048 pkgVersionMatch::Version));
1049
1050 pkgCache::VerIterator Ver = Match.Find(Pkg);
1051
1052 if (Ver.end() == true)
1053 {
1054 if (IsRel == true)
1055 return _error->Error(_("Release '%s' for '%s' was not found"),
1056 VerTag,Pkg.Name());
1057 return _error->Error(_("Version '%s' for '%s' was not found"),
1058 VerTag,Pkg.Name());
1059 }
1060
1061 if (strcmp(VerTag,Ver.VerStr()) != 0)
1062 {
1063 ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
1064 Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
1065 }
1066
1067 Cache.SetCandidateVersion(Ver);
1068 return true;
1069 }
1070 /*}}}*/
1071 // FindSrc - Find a source record /*{{{*/
1072 // ---------------------------------------------------------------------
1073 /* */
1074 pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
1075 pkgSrcRecords &SrcRecs,string &Src,
1076 pkgDepCache &Cache)
1077 {
1078 // We want to pull the version off the package specification..
1079 string VerTag;
1080 string TmpSrc = Name;
1081 string::size_type Slash = TmpSrc.rfind('=');
1082 if (Slash != string::npos)
1083 {
1084 VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
1085 TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
1086 }
1087
1088 /* Lookup the version of the package we would install if we were to
1089 install a version and determine the source package name, then look
1090 in the archive for a source package of the same name. In theory
1091 we could stash the version string as well and match that too but
1092 today there aren't multi source versions in the archive. */
1093 if (_config->FindB("APT::Get::Only-Source") == false &&
1094 VerTag.empty() == true)
1095 {
1096 pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
1097 if (Pkg.end() == false)
1098 {
1099 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
1100 if (Ver.end() == false)
1101 {
1102 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1103 Src = Parse.SourcePkg();
1104 }
1105 }
1106 }
1107
1108 // No source package name..
1109 if (Src.empty() == true)
1110 Src = TmpSrc;
1111
1112 // The best hit
1113 pkgSrcRecords::Parser *Last = 0;
1114 unsigned long Offset = 0;
1115 string Version;
1116 bool IsMatch = false;
1117
1118 // If we are matching by version then we need exact matches to be happy
1119 if (VerTag.empty() == false)
1120 IsMatch = true;
1121
1122 /* Iterate over all of the hits, which includes the resulting
1123 binary packages in the search */
1124 pkgSrcRecords::Parser *Parse;
1125 SrcRecs.Restart();
1126 while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
1127 {
1128 string Ver = Parse->Version();
1129
1130 // Skip name mismatches
1131 if (IsMatch == true && Parse->Package() != Src)
1132 continue;
1133
1134 if (VerTag.empty() == false)
1135 {
1136 /* Don't want to fall through because we are doing exact version
1137 matching. */
1138 if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
1139 continue;
1140
1141 Last = Parse;
1142 Offset = Parse->Offset();
1143 break;
1144 }
1145
1146 // Newer version or an exact match
1147 if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
1148 (Parse->Package() == Src && IsMatch == false))
1149 {
1150 IsMatch = Parse->Package() == Src;
1151 Last = Parse;
1152 Offset = Parse->Offset();
1153 Version = Ver;
1154 }
1155 }
1156
1157 if (Last == 0)
1158 return 0;
1159
1160 if (Last->Jump(Offset) == false)
1161 return 0;
1162
1163 return Last;
1164 }
1165 /*}}}*/
1166
1167 // DoUpdate - Update the package lists /*{{{*/
1168 // ---------------------------------------------------------------------
1169 /* */
1170 bool DoUpdate(CommandLine &CmdL)
1171 {
1172 if (CmdL.FileSize() != 1)
1173 return _error->Error(_("The update command takes no arguments"));
1174
1175 // Get the source list
1176 pkgSourceList List;
1177 if (List.ReadMainList() == false)
1178 return false;
1179
1180 // Lock the list directory
1181 FileFd Lock;
1182 if (_config->FindB("Debug::NoLocking",false) == false)
1183 {
1184 Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
1185 if (_error->PendingError() == true)
1186 return _error->Error(_("Unable to lock the list directory"));
1187 }
1188
1189 // Create the download object
1190 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1191 pkgAcquire Fetcher(&Stat);
1192
1193 // Populate it with the source selection
1194 if (List.GetIndexes(&Fetcher) == false)
1195 return false;
1196
1197 // Just print out the uris an exit if the --print-uris flag was used
1198 if (_config->FindB("APT::Get::Print-URIs") == true)
1199 {
1200 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1201 for (; I != Fetcher.UriEnd(); I++)
1202 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1203 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
1204 return true;
1205 }
1206
1207 // Run it
1208 if (Fetcher.Run() == pkgAcquire::Failed)
1209 return false;
1210
1211 bool Failed = false;
1212 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1213 {
1214 if ((*I)->Status == pkgAcquire::Item::StatDone)
1215 continue;
1216
1217 (*I)->Finished();
1218
1219 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1220 (*I)->ErrorText.c_str());
1221 Failed = true;
1222 }
1223
1224 // Clean out any old list files
1225 if (_config->FindB("APT::Get::List-Cleanup",true) == true)
1226 {
1227 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
1228 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
1229 return false;
1230 }
1231
1232 // Prepare the cache.
1233 CacheFile Cache;
1234 if (Cache.BuildCaches() == false)
1235 return false;
1236
1237 if (Failed == true)
1238 return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
1239
1240 return true;
1241 }
1242 /*}}}*/
1243 // DoUpgrade - Upgrade all packages /*{{{*/
1244 // ---------------------------------------------------------------------
1245 /* Upgrade all packages without installing new packages or erasing old
1246 packages */
1247 bool DoUpgrade(CommandLine &CmdL)
1248 {
1249 CacheFile Cache;
1250 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1251 return false;
1252
1253 // Do the upgrade
1254 if (pkgAllUpgrade(Cache) == false)
1255 {
1256 ShowBroken(c1out,Cache,false);
1257 return _error->Error(_("Internal Error, AllUpgrade broke stuff"));
1258 }
1259
1260 return InstallPackages(Cache,true);
1261 }
1262 /*}}}*/
1263 // DoInstall - Install packages from the command line /*{{{*/
1264 // ---------------------------------------------------------------------
1265 /* Install named packages */
1266 bool DoInstall(CommandLine &CmdL)
1267 {
1268 CacheFile Cache;
1269 if (Cache.OpenForInstall() == false ||
1270 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
1271 return false;
1272
1273 // Enter the special broken fixing mode if the user specified arguments
1274 bool BrokenFix = false;
1275 if (Cache->BrokenCount() != 0)
1276 BrokenFix = true;
1277
1278 unsigned int ExpectedInst = 0;
1279 unsigned int Packages = 0;
1280 pkgProblemResolver Fix(Cache);
1281
1282 bool DefRemove = false;
1283 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
1284 DefRemove = true;
1285
1286 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1287 {
1288 // Duplicate the string
1289 unsigned int Length = strlen(*I);
1290 char S[300];
1291 if (Length >= sizeof(S))
1292 continue;
1293 strcpy(S,*I);
1294
1295 // See if we are removing and special indicators..
1296 bool Remove = DefRemove;
1297 char *VerTag = 0;
1298 bool VerIsRel = false;
1299 while (Cache->FindPkg(S).end() == true)
1300 {
1301 // Handle an optional end tag indicating what to do
1302 if (S[Length - 1] == '-')
1303 {
1304 Remove = true;
1305 S[--Length] = 0;
1306 continue;
1307 }
1308
1309 if (S[Length - 1] == '+')
1310 {
1311 Remove = false;
1312 S[--Length] = 0;
1313 continue;
1314 }
1315
1316 char *Slash = strchr(S,'=');
1317 if (Slash != 0)
1318 {
1319 VerIsRel = false;
1320 *Slash = 0;
1321 VerTag = Slash + 1;
1322 }
1323
1324 Slash = strchr(S,'/');
1325 if (Slash != 0)
1326 {
1327 VerIsRel = true;
1328 *Slash = 0;
1329 VerTag = Slash + 1;
1330 }
1331
1332 break;
1333 }
1334
1335 // Locate the package
1336 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
1337 Packages++;
1338 if (Pkg.end() == true)
1339 {
1340 // Check if the name is a regex
1341 const char *I;
1342 for (I = S; *I != 0; I++)
1343 if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
1344 break;
1345 if (*I == 0)
1346 return _error->Error(_("Couldn't find package %s"),S);
1347
1348 // Regexs must always be confirmed
1349 ExpectedInst += 1000;
1350
1351 // Compile the regex pattern
1352 regex_t Pattern;
1353 int Res;
1354 if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
1355 REG_NOSUB)) != 0)
1356 {
1357 char Error[300];
1358 regerror(Res,&Pattern,Error,sizeof(Error));
1359 return _error->Error(_("Regex compilation error - %s"),Error);
1360 }
1361
1362 // Run over the matches
1363 bool Hit = false;
1364 for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
1365 {
1366 if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
1367 continue;
1368
1369 if (VerTag != 0)
1370 if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
1371 return false;
1372
1373 Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
1374 ExpectedInst,false);
1375 }
1376 regfree(&Pattern);
1377
1378 if (Hit == false)
1379 return _error->Error(_("Couldn't find package %s"),S);
1380 }
1381 else
1382 {
1383 if (VerTag != 0)
1384 if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
1385 return false;
1386 if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
1387 return false;
1388 }
1389 }
1390
1391 /* If we are in the Broken fixing mode we do not attempt to fix the
1392 problems. This is if the user invoked install without -f and gave
1393 packages */
1394 if (BrokenFix == true && Cache->BrokenCount() != 0)
1395 {
1396 c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
1397 ShowBroken(c1out,Cache,false);
1398
1399 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
1400 }
1401
1402 // Call the scored problem resolver
1403 Fix.InstallProtect();
1404 if (Fix.Resolve(true) == false)
1405 _error->Discard();
1406
1407 // Now we check the state of the packages,
1408 if (Cache->BrokenCount() != 0)
1409 {
1410 c1out <<
1411 _("Some packages could not be installed. This may mean that you have\n"
1412 "requested an impossible situation or if you are using the unstable\n"
1413 "distribution that some required packages have not yet been created\n"
1414 "or been moved out of Incoming.") << endl;
1415 if (Packages == 1)
1416 {
1417 c1out << endl;
1418 c1out <<
1419 _("Since you only requested a single operation it is extremely likely that\n"
1420 "the package is simply not installable and a bug report against\n"
1421 "that package should be filed.") << endl;
1422 }
1423
1424 c1out << _("The following information may help to resolve the situation:") << endl;
1425 c1out << endl;
1426 ShowBroken(c1out,Cache,false);
1427 return _error->Error(_("Sorry, broken packages"));
1428 }
1429
1430 /* Print out a list of packages that are going to be installed extra
1431 to what the user asked */
1432 if (Cache->InstCount() != ExpectedInst)
1433 {
1434 string List;
1435 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1436 {
1437 pkgCache::PkgIterator I(Cache,Cache.List[J]);
1438 if ((*Cache)[I].Install() == false)
1439 continue;
1440
1441 const char **J;
1442 for (J = CmdL.FileList + 1; *J != 0; J++)
1443 if (strcmp(*J,I.Name()) == 0)
1444 break;
1445
1446 if (*J == 0)
1447 List += string(I.Name()) + " ";
1448 }
1449
1450 ShowList(c1out,_("The following extra packages will be installed:"),List);
1451 }
1452
1453 // See if we need to prompt
1454 if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
1455 return InstallPackages(Cache,false,false);
1456
1457 return InstallPackages(Cache,false);
1458 }
1459 /*}}}*/
1460 // DoDistUpgrade - Automatic smart upgrader /*{{{*/
1461 // ---------------------------------------------------------------------
1462 /* Intelligent upgrader that will install and remove packages at will */
1463 bool DoDistUpgrade(CommandLine &CmdL)
1464 {
1465 CacheFile Cache;
1466 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1467 return false;
1468
1469 c0out << _("Calculating Upgrade... ") << flush;
1470 if (pkgDistUpgrade(*Cache) == false)
1471 {
1472 c0out << _("Failed") << endl;
1473 ShowBroken(c1out,Cache,false);
1474 return false;
1475 }
1476
1477 c0out << _("Done") << endl;
1478
1479 return InstallPackages(Cache,true);
1480 }
1481 /*}}}*/
1482 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
1483 // ---------------------------------------------------------------------
1484 /* Follows dselect's selections */
1485 bool DoDSelectUpgrade(CommandLine &CmdL)
1486 {
1487 CacheFile Cache;
1488 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1489 return false;
1490
1491 // Install everything with the install flag set
1492 pkgCache::PkgIterator I = Cache->PkgBegin();
1493 for (;I.end() != true; I++)
1494 {
1495 /* Install the package only if it is a new install, the autoupgrader
1496 will deal with the rest */
1497 if (I->SelectedState == pkgCache::State::Install)
1498 Cache->MarkInstall(I,false);
1499 }
1500
1501 /* Now install their deps too, if we do this above then order of
1502 the status file is significant for | groups */
1503 for (I = Cache->PkgBegin();I.end() != true; I++)
1504 {
1505 /* Install the package only if it is a new install, the autoupgrader
1506 will deal with the rest */
1507 if (I->SelectedState == pkgCache::State::Install)
1508 Cache->MarkInstall(I,true);
1509 }
1510
1511 // Apply erasures now, they override everything else.
1512 for (I = Cache->PkgBegin();I.end() != true; I++)
1513 {
1514 // Remove packages
1515 if (I->SelectedState == pkgCache::State::DeInstall ||
1516 I->SelectedState == pkgCache::State::Purge)
1517 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
1518 }
1519
1520 /* Resolve any problems that dselect created, allupgrade cannot handle
1521 such things. We do so quite agressively too.. */
1522 if (Cache->BrokenCount() != 0)
1523 {
1524 pkgProblemResolver Fix(Cache);
1525
1526 // Hold back held packages.
1527 if (_config->FindB("APT::Ignore-Hold",false) == false)
1528 {
1529 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
1530 {
1531 if (I->SelectedState == pkgCache::State::Hold)
1532 {
1533 Fix.Protect(I);
1534 Cache->MarkKeep(I);
1535 }
1536 }
1537 }
1538
1539 if (Fix.Resolve() == false)
1540 {
1541 ShowBroken(c1out,Cache,false);
1542 return _error->Error("Internal Error, problem resolver broke stuff");
1543 }
1544 }
1545
1546 // Now upgrade everything
1547 if (pkgAllUpgrade(Cache) == false)
1548 {
1549 ShowBroken(c1out,Cache,false);
1550 return _error->Error("Internal Error, problem resolver broke stuff");
1551 }
1552
1553 return InstallPackages(Cache,false);
1554 }
1555 /*}}}*/
1556 // DoClean - Remove download archives /*{{{*/
1557 // ---------------------------------------------------------------------
1558 /* */
1559 bool DoClean(CommandLine &CmdL)
1560 {
1561 if (_config->FindB("APT::Get::Simulate") == true)
1562 {
1563 cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
1564 _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
1565 return true;
1566 }
1567
1568 // Lock the archive directory
1569 FileFd Lock;
1570 if (_config->FindB("Debug::NoLocking",false) == false)
1571 {
1572 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1573 if (_error->PendingError() == true)
1574 return _error->Error(_("Unable to lock the download directory"));
1575 }
1576
1577 pkgAcquire Fetcher;
1578 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
1579 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
1580 return true;
1581 }
1582 /*}}}*/
1583 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
1584 // ---------------------------------------------------------------------
1585 /* This is similar to clean but it only purges things that cannot be
1586 downloaded, that is old versions of cached packages. */
1587 class LogCleaner : public pkgArchiveCleaner
1588 {
1589 protected:
1590 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
1591 {
1592 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
1593
1594 if (_config->FindB("APT::Get::Simulate") == false)
1595 unlink(File);
1596 };
1597 };
1598
1599 bool DoAutoClean(CommandLine &CmdL)
1600 {
1601 // Lock the archive directory
1602 FileFd Lock;
1603 if (_config->FindB("Debug::NoLocking",false) == false)
1604 {
1605 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1606 if (_error->PendingError() == true)
1607 return _error->Error(_("Unable to lock the download directory"));
1608 }
1609
1610 CacheFile Cache;
1611 if (Cache.Open() == false)
1612 return false;
1613
1614 LogCleaner Cleaner;
1615
1616 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
1617 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
1618 }
1619 /*}}}*/
1620 // DoCheck - Perform the check operation /*{{{*/
1621 // ---------------------------------------------------------------------
1622 /* Opening automatically checks the system, this command is mostly used
1623 for debugging */
1624 bool DoCheck(CommandLine &CmdL)
1625 {
1626 CacheFile Cache;
1627 Cache.Open();
1628 Cache.CheckDeps();
1629
1630 return true;
1631 }
1632 /*}}}*/
1633 // DoSource - Fetch a source archive /*{{{*/
1634 // ---------------------------------------------------------------------
1635 /* Fetch souce packages */
1636 struct DscFile
1637 {
1638 string Package;
1639 string Version;
1640 string Dsc;
1641 };
1642
1643 bool DoSource(CommandLine &CmdL)
1644 {
1645 CacheFile Cache;
1646 if (Cache.Open(false) == false)
1647 return false;
1648
1649 if (CmdL.FileSize() <= 1)
1650 return _error->Error(_("Must specify at least one package to fetch source for"));
1651
1652 // Read the source list
1653 pkgSourceList List;
1654 if (List.ReadMainList() == false)
1655 return _error->Error(_("The list of sources could not be read."));
1656
1657 // Create the text record parsers
1658 pkgRecords Recs(Cache);
1659 pkgSrcRecords SrcRecs(List);
1660 if (_error->PendingError() == true)
1661 return false;
1662
1663 // Create the download object
1664 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1665 pkgAcquire Fetcher(&Stat);
1666
1667 DscFile *Dsc = new DscFile[CmdL.FileSize()];
1668
1669 // Load the requestd sources into the fetcher
1670 unsigned J = 0;
1671 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1672 {
1673 string Src;
1674 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
1675
1676 if (Last == 0)
1677 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
1678
1679 // Back track
1680 vector<pkgSrcRecords::File> Lst;
1681 if (Last->Files(Lst) == false)
1682 return false;
1683
1684 // Load them into the fetcher
1685 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
1686 I != Lst.end(); I++)
1687 {
1688 // Try to guess what sort of file it is we are getting.
1689 if (I->Type == "dsc")
1690 {
1691 Dsc[J].Package = Last->Package();
1692 Dsc[J].Version = Last->Version();
1693 Dsc[J].Dsc = flNotDir(I->Path);
1694 }
1695
1696 // Diff only mode only fetches .diff files
1697 if (_config->FindB("APT::Get::Diff-Only",false) == true &&
1698 I->Type != "diff")
1699 continue;
1700
1701 // Tar only mode only fetches .tar files
1702 if (_config->FindB("APT::Get::Tar-Only",false) == true &&
1703 I->Type != "tar")
1704 continue;
1705
1706 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
1707 I->MD5Hash,I->Size,
1708 Last->Index().SourceInfo(*Last,*I),Src);
1709 }
1710 }
1711
1712 // Display statistics
1713 double FetchBytes = Fetcher.FetchNeeded();
1714 double FetchPBytes = Fetcher.PartialPresent();
1715 double DebBytes = Fetcher.TotalNeeded();
1716
1717 // Check for enough free space
1718 struct statvfs Buf;
1719 string OutputDir = ".";
1720 if (statvfs(OutputDir.c_str(),&Buf) != 0)
1721 return _error->Errno("statvfs","Couldn't determine free space in %s",
1722 OutputDir.c_str());
1723 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
1724 return _error->Error(_("Sorry, you don't have enough free space in %s"),
1725 OutputDir.c_str());
1726
1727 // Number of bytes
1728 if (DebBytes != FetchBytes)
1729 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
1730 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
1731 else
1732 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
1733 SizeToStr(DebBytes).c_str());
1734
1735 if (_config->FindB("APT::Get::Simulate",false) == true)
1736 {
1737 for (unsigned I = 0; I != J; I++)
1738 ioprintf(cout,_("Fetch Source %s\n"),Dsc[I].Package.c_str());
1739 return true;
1740 }
1741
1742 // Just print out the uris an exit if the --print-uris flag was used
1743 if (_config->FindB("APT::Get::Print-URIs") == true)
1744 {
1745 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1746 for (; I != Fetcher.UriEnd(); I++)
1747 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1748 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
1749 return true;
1750 }
1751
1752 // Run it
1753 if (Fetcher.Run() == pkgAcquire::Failed)
1754 return false;
1755
1756 // Print error messages
1757 bool Failed = false;
1758 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1759 {
1760 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1761 (*I)->Complete == true)
1762 continue;
1763
1764 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1765 (*I)->ErrorText.c_str());
1766 Failed = true;
1767 }
1768 if (Failed == true)
1769 return _error->Error(_("Failed to fetch some archives."));
1770
1771 if (_config->FindB("APT::Get::Download-only",false) == true)
1772 {
1773 c1out << _("Download complete and in download only mode") << endl;
1774 return true;
1775 }
1776
1777 // Unpack the sources
1778 pid_t Process = ExecFork();
1779
1780 if (Process == 0)
1781 {
1782 for (unsigned I = 0; I != J; I++)
1783 {
1784 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
1785
1786 // Diff only mode only fetches .diff files
1787 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
1788 _config->FindB("APT::Get::Tar-Only",false) == true ||
1789 Dsc[I].Dsc.empty() == true)
1790 continue;
1791
1792 // See if the package is already unpacked
1793 struct stat Stat;
1794 if (stat(Dir.c_str(),&Stat) == 0 &&
1795 S_ISDIR(Stat.st_mode) != 0)
1796 {
1797 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
1798 Dir.c_str());
1799 }
1800 else
1801 {
1802 // Call dpkg-source
1803 char S[500];
1804 snprintf(S,sizeof(S),"%s -x %s",
1805 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
1806 Dsc[I].Dsc.c_str());
1807 if (system(S) != 0)
1808 {
1809 fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
1810 _exit(1);
1811 }
1812 }
1813
1814 // Try to compile it with dpkg-buildpackage
1815 if (_config->FindB("APT::Get::Compile",false) == true)
1816 {
1817 // Call dpkg-buildpackage
1818 char S[500];
1819 snprintf(S,sizeof(S),"cd %s && %s %s",
1820 Dir.c_str(),
1821 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
1822 _config->Find("DPkg::Build-Options","-b -uc").c_str());
1823
1824 if (system(S) != 0)
1825 {
1826 fprintf(stderr,_("Build command '%s' failed.\n"),S);
1827 _exit(1);
1828 }
1829 }
1830 }
1831
1832 _exit(0);
1833 }
1834
1835 // Wait for the subprocess
1836 int Status = 0;
1837 while (waitpid(Process,&Status,0) != Process)
1838 {
1839 if (errno == EINTR)
1840 continue;
1841 return _error->Errno("waitpid","Couldn't wait for subprocess");
1842 }
1843
1844 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
1845 return _error->Error(_("Child process failed"));
1846
1847 return true;
1848 }
1849 /*}}}*/
1850 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
1851 // ---------------------------------------------------------------------
1852 /* This function will look at the build depends list of the given source
1853 package and install the necessary packages to make it true, or fail. */
1854 bool DoBuildDep(CommandLine &CmdL)
1855 {
1856 CacheFile Cache;
1857 if (Cache.Open(true) == false)
1858 return false;
1859
1860 if (CmdL.FileSize() <= 1)
1861 return _error->Error(_("Must specify at least one package to check builddeps for"));
1862
1863 // Read the source list
1864 pkgSourceList List;
1865 if (List.ReadMainList() == false)
1866 return _error->Error(_("The list of sources could not be read."));
1867
1868 // Create the text record parsers
1869 pkgRecords Recs(Cache);
1870 pkgSrcRecords SrcRecs(List);
1871 if (_error->PendingError() == true)
1872 return false;
1873
1874 // Create the download object
1875 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1876 pkgAcquire Fetcher(&Stat);
1877
1878 unsigned J = 0;
1879 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1880 {
1881 string Src;
1882 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
1883 if (Last == 0)
1884 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
1885
1886 // Process the build-dependencies
1887 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
1888 if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only",false)) == false)
1889 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
1890
1891 // Also ensure that build-essential packages are present
1892 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
1893 if (Opts)
1894 Opts = Opts->Child;
1895 for (; Opts; Opts = Opts->Next)
1896 {
1897 if (Opts->Value.empty() == true)
1898 continue;
1899
1900 pkgSrcRecords::Parser::BuildDepRec rec;
1901 rec.Package = Opts->Value;
1902 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
1903 rec.Op = 0;
1904 BuildDeps.insert(BuildDeps.begin(), rec);
1905 }
1906
1907 if (BuildDeps.size() == 0)
1908 {
1909 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
1910 continue;
1911 }
1912
1913 // Install the requested packages
1914 unsigned int ExpectedInst = 0;
1915 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
1916 pkgProblemResolver Fix(Cache);
1917 for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
1918 {
1919 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
1920 if (Pkg.end() == true)
1921 {
1922 /* for a build-conflict; ignore unknown packages */
1923 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
1924 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
1925 continue;
1926
1927 return _error->Error(_("%s dependency on %s cannot be satisfied because the package %s cannot be found"),
1928 Last->BuildDepType((*D).Type),Src.c_str(),(*D).Package.c_str());
1929 }
1930 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
1931
1932 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
1933 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
1934 {
1935 /*
1936 * conflict; need to remove if we have an installed version
1937 * that satisfies the version criterial
1938 */
1939 if (IV.end() == false &&
1940 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
1941 TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
1942 }
1943 else
1944 {
1945 /*
1946 * If this is a virtual package, we need to check the list of
1947 * packages that provide it and see if any of those are
1948 * installed
1949 */
1950 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
1951 for (; Prv.end() != true; Prv++)
1952 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
1953 break;
1954
1955 if (Prv.end() == true)
1956 {
1957 /*
1958 * depends; need to install or upgrade if we don't have the
1959 * package installed or if the version does not satisfy the
1960 * build dep. This is complicated by the fact that if we
1961 * depend on a version lower than what we already have
1962 * installed it is not clear what should be done; in practice
1963 * this case should be rare though and right now nothing
1964 * is done about it :-(
1965 */
1966 if (IV.end() == true ||
1967 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
1968 TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst);
1969 }
1970 }
1971 }
1972
1973 Fix.InstallProtect();
1974 if (Fix.Resolve(true) == false)
1975 _error->Discard();
1976
1977 // Now we check the state of the packages,
1978 if (Cache->BrokenCount() != 0)
1979 return _error->Error(_("Some broken packages were found while trying to process build-dependencies.\n"
1980 "You might want to run `apt-get -f install' to correct these."));
1981 }
1982
1983 if (InstallPackages(Cache, false, true) == false)
1984 return _error->Error(_("Failed to process build dependencies"));
1985 return true;
1986 }
1987 /*}}}*/
1988
1989 // DoMoo - Never Ask, Never Tell /*{{{*/
1990 // ---------------------------------------------------------------------
1991 /* */
1992 bool DoMoo(CommandLine &CmdL)
1993 {
1994 cout <<
1995 " (__) \n"
1996 " (oo) \n"
1997 " /------\\/ \n"
1998 " / | || \n"
1999 " * /\\---/\\ \n"
2000 " ~~ ~~ \n"
2001 "....\"Have you mooed today?\"...\n";
2002
2003 return true;
2004 }
2005 /*}}}*/
2006 // ShowHelp - Show a help screen /*{{{*/
2007 // ---------------------------------------------------------------------
2008 /* */
2009 bool ShowHelp(CommandLine &CmdL)
2010 {
2011 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
2012 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
2013
2014 if (_config->FindB("version") == true)
2015 {
2016 cout << _("Supported Modules:") << endl;
2017
2018 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
2019 {
2020 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
2021 if (_system != 0 && _system->VS == VS)
2022 cout << '*';
2023 else
2024 cout << ' ';
2025 cout << "Ver: " << VS->Label << endl;
2026
2027 /* Print out all the packaging systems that will work with
2028 this VS */
2029 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
2030 {
2031 pkgSystem *Sys = pkgSystem::GlobalList[J];
2032 if (_system == Sys)
2033 cout << '*';
2034 else
2035 cout << ' ';
2036 if (Sys->VS->TestCompatibility(*VS) == true)
2037 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
2038 }
2039 }
2040
2041 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
2042 {
2043 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
2044 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
2045 }
2046
2047 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
2048 {
2049 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
2050 cout << " Idx: " << Type->Label << endl;
2051 }
2052
2053 return true;
2054 }
2055
2056 cout <<
2057 _("Usage: apt-get [options] command\n"
2058 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
2059 " apt-get [options] source pkg1 [pkg2 ...]\n"
2060 "\n"
2061 "apt-get is a simple command line interface for downloading and\n"
2062 "installing packages. The most frequently used commands are update\n"
2063 "and install.\n"
2064 "\n"
2065 "Commands:\n"
2066 " update - Retrieve new lists of packages\n"
2067 " upgrade - Perform an upgrade\n"
2068 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
2069 " remove - Remove packages\n"
2070 " source - Download source archives\n"
2071 " build-dep - Configure build-dependencies for source packages\n"
2072 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
2073 " dselect-upgrade - Follow dselect selections\n"
2074 " clean - Erase downloaded archive files\n"
2075 " autoclean - Erase old downloaded archive files\n"
2076 " check - Verify that there are no broken dependencies\n"
2077 "\n"
2078 "Options:\n"
2079 " -h This help text.\n"
2080 " -q Loggable output - no progress indicator\n"
2081 " -qq No output except for errors\n"
2082 " -d Download only - do NOT install or unpack archives\n"
2083 " -s No-act. Perform ordering simulation\n"
2084 " -y Assume Yes to all queries and do not prompt\n"
2085 " -f Attempt to continue if the integrity check fails\n"
2086 " -m Attempt to continue if archives are unlocatable\n"
2087 " -u Show a list of upgraded packages as well\n"
2088 " -b Build the source package after fetching it\n"
2089 " -c=? Read this configuration file\n"
2090 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
2091 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
2092 "pages for more information and options.\n"
2093 " This APT has Super Cow Powers.\n");
2094 return true;
2095 }
2096 /*}}}*/
2097 // GetInitialize - Initialize things for apt-get /*{{{*/
2098 // ---------------------------------------------------------------------
2099 /* */
2100 void GetInitialize()
2101 {
2102 _config->Set("quiet",0);
2103 _config->Set("help",false);
2104 _config->Set("APT::Get::Download-Only",false);
2105 _config->Set("APT::Get::Simulate",false);
2106 _config->Set("APT::Get::Assume-Yes",false);
2107 _config->Set("APT::Get::Fix-Broken",false);
2108 _config->Set("APT::Get::Force-Yes",false);
2109 _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
2110 }
2111 /*}}}*/
2112 // SigWinch - Window size change signal handler /*{{{*/
2113 // ---------------------------------------------------------------------
2114 /* */
2115 void SigWinch(int)
2116 {
2117 // Riped from GNU ls
2118 #ifdef TIOCGWINSZ
2119 struct winsize ws;
2120
2121 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
2122 ScreenWidth = ws.ws_col - 1;
2123 #endif
2124 }
2125 /*}}}*/
2126
2127 int main(int argc,const char *argv[])
2128 {
2129 CommandLine::Args Args[] = {
2130 {'h',"help","help",0},
2131 {'v',"version","version",0},
2132 {'q',"quiet","quiet",CommandLine::IntLevel},
2133 {'q',"silent","quiet",CommandLine::IntLevel},
2134 {'d',"download-only","APT::Get::Download-Only",0},
2135 {'b',"compile","APT::Get::Compile",0},
2136 {'b',"build","APT::Get::Compile",0},
2137 {'s',"simulate","APT::Get::Simulate",0},
2138 {'s',"just-print","APT::Get::Simulate",0},
2139 {'s',"recon","APT::Get::Simulate",0},
2140 {'s',"dry-run","APT::Get::Simulate",0},
2141 {'s',"no-act","APT::Get::Simulate",0},
2142 {'y',"yes","APT::Get::Assume-Yes",0},
2143 {'y',"assume-yes","APT::Get::Assume-Yes",0},
2144 {'f',"fix-broken","APT::Get::Fix-Broken",0},
2145 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
2146 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
2147 {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
2148 {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
2149 {0,"download","APT::Get::Download",0},
2150 {0,"fix-missing","APT::Get::Fix-Missing",0},
2151 {0,"ignore-hold","APT::Ignore-Hold",0},
2152 {0,"upgrade","APT::Get::upgrade",0},
2153 {0,"force-yes","APT::Get::force-yes",0},
2154 {0,"print-uris","APT::Get::Print-URIs",0},
2155 {0,"diff-only","APT::Get::Diff-Only",0},
2156 {0,"tar-only","APT::Get::tar-Only",0},
2157 {0,"purge","APT::Get::Purge",0},
2158 {0,"list-cleanup","APT::Get::List-Cleanup",0},
2159 {0,"reinstall","APT::Get::ReInstall",0},
2160 {0,"trivial-only","APT::Get::Trivial-Only",0},
2161 {0,"remove","APT::Get::Remove",0},
2162 {0,"only-source","APT::Get::Only-Source",0},
2163 {0,"arch-only","APT::Get::Arch-Only",0},
2164 {'c',"config-file",0,CommandLine::ConfigFile},
2165 {'o',"option",0,CommandLine::ArbItem},
2166 {0,0,0,0}};
2167 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
2168 {"upgrade",&DoUpgrade},
2169 {"install",&DoInstall},
2170 {"remove",&DoInstall},
2171 {"dist-upgrade",&DoDistUpgrade},
2172 {"dselect-upgrade",&DoDSelectUpgrade},
2173 {"build-dep",&DoBuildDep},
2174 {"clean",&DoClean},
2175 {"autoclean",&DoAutoClean},
2176 {"check",&DoCheck},
2177 {"source",&DoSource},
2178 {"moo",&DoMoo},
2179 {"help",&ShowHelp},
2180 {0,0}};
2181
2182 // Set up gettext support
2183 setlocale(LC_ALL,"");
2184 textdomain(PACKAGE);
2185
2186 // Parse the command line and initialize the package library
2187 CommandLine CmdL(Args,_config);
2188 if (pkgInitConfig(*_config) == false ||
2189 CmdL.Parse(argc,argv) == false ||
2190 pkgInitSystem(*_config,_system) == false)
2191 {
2192 if (_config->FindB("version") == true)
2193 ShowHelp(CmdL);
2194
2195 _error->DumpErrors();
2196 return 100;
2197 }
2198
2199 // See if the help should be shown
2200 if (_config->FindB("help") == true ||
2201 _config->FindB("version") == true ||
2202 CmdL.FileSize() == 0)
2203 {
2204 ShowHelp(CmdL);
2205 return 0;
2206 }
2207
2208 // Deal with stdout not being a tty
2209 if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
2210 _config->Set("quiet","1");
2211
2212 // Setup the output streams
2213 c0out.rdbuf(cout.rdbuf());
2214 c1out.rdbuf(cout.rdbuf());
2215 c2out.rdbuf(cout.rdbuf());
2216 if (_config->FindI("quiet",0) > 0)
2217 c0out.rdbuf(devnull.rdbuf());
2218 if (_config->FindI("quiet",0) > 1)
2219 c1out.rdbuf(devnull.rdbuf());
2220
2221 // Setup the signals
2222 signal(SIGPIPE,SIG_IGN);
2223 signal(SIGWINCH,SigWinch);
2224 SigWinch(0);
2225
2226 // Match the operation
2227 CmdL.DispatchArg(Cmds);
2228
2229 // Print any errors or warnings found during parsing
2230 if (_error->empty() == false)
2231 {
2232 bool Errors = _error->PendingError();
2233 _error->DumpErrors();
2234 return Errors == true?100:0;
2235 }
2236
2237 return 0;
2238 }