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