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