]> git.saurik.com Git - apt.git/blob - cmdline/apt-get.cc
Fixed handling of MD5 failures
[apt.git] / cmdline / apt-get.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-get.cc,v 1.62 1999/06/04 02:31:37 jgg Exp $
4 /* ######################################################################
5
6 apt-get - Cover for dpkg
7
8 This is an allout cover for dpkg implementing a safer front end. It is
9 based largely on libapt-pkg.
10
11 The syntax is different,
12 apt-get [opt] command [things]
13 Where command is:
14 update - Resyncronize the package files from their sources
15 upgrade - Smart-Download the newest versions of all packages
16 dselect-upgrade - Follows dselect's changes to the Status: field
17 and installes new and removes old packages
18 dist-upgrade - Powerfull upgrader designed to handle the issues with
19 a new distribution.
20 install - Download and install a given package (by name, not by .deb)
21 check - Update the package cache and check for broken packages
22 clean - Erase the .debs downloaded to /var/cache/apt/archives and
23 the partial dir too
24
25 ##################################################################### */
26 /*}}}*/
27 // Include Files /*{{{*/
28 #include <apt-pkg/error.h>
29 #include <apt-pkg/cmndline.h>
30 #include <apt-pkg/init.h>
31 #include <apt-pkg/depcache.h>
32 #include <apt-pkg/sourcelist.h>
33 #include <apt-pkg/algorithms.h>
34 #include <apt-pkg/acquire-item.h>
35 #include <apt-pkg/dpkgpm.h>
36 #include <apt-pkg/strutl.h>
37 #include <apt-pkg/clean.h>
38 #include <apt-pkg/srcrecords.h>
39 #include <apt-pkg/version.h>
40 #include <apt-pkg/cachefile.h>
41
42 #include <config.h>
43
44 #include "acqprogress.h"
45
46 #include <fstream.h>
47 #include <termios.h>
48 #include <sys/ioctl.h>
49 #include <sys/stat.h>
50 #include <sys/vfs.h>
51 #include <signal.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <sys/wait.h>
56 /*}}}*/
57
58 ostream c0out;
59 ostream c1out;
60 ostream c2out;
61 ofstream devnull("/dev/null");
62 unsigned int ScreenWidth = 80;
63
64 // YnPrompt - Yes No Prompt. /*{{{*/
65 // ---------------------------------------------------------------------
66 /* Returns true on a Yes.*/
67 bool YnPrompt()
68 {
69 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
70 {
71 c1out << 'Y' << endl;
72 return true;
73 }
74
75 char C = 0;
76 char Jnk = 0;
77 read(STDIN_FILENO,&C,1);
78 while (C != '\n' && Jnk != '\n') read(STDIN_FILENO,&Jnk,1);
79
80 if (!(C == 'Y' || C == 'y' || C == '\n' || C == '\r'))
81 return false;
82 return true;
83 }
84 /*}}}*/
85 // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
86 // ---------------------------------------------------------------------
87 /* Returns true on a Yes.*/
88 bool AnalPrompt(const char *Text)
89 {
90 char Buf[1024];
91 cin.getline(Buf,sizeof(Buf));
92 if (strcmp(Buf,Text) == 0)
93 return true;
94 return false;
95 }
96 /*}}}*/
97 // ShowList - Show a list /*{{{*/
98 // ---------------------------------------------------------------------
99 /* This prints out a string of space seperated words with a title and
100 a two space indent line wraped to the current screen width. */
101 bool ShowList(ostream &out,string Title,string List)
102 {
103 if (List.empty() == true)
104 return true;
105
106 // Acount for the leading space
107 int ScreenWidth = ::ScreenWidth - 3;
108
109 out << Title << endl;
110 string::size_type Start = 0;
111 while (Start < List.size())
112 {
113 string::size_type End;
114 if (Start + ScreenWidth >= List.size())
115 End = List.size();
116 else
117 End = List.rfind(' ',Start+ScreenWidth);
118
119 if (End == string::npos || End < Start)
120 End = Start + ScreenWidth;
121 out << " " << string(List,Start,End - Start) << endl;
122 Start = End + 1;
123 }
124 return false;
125 }
126 /*}}}*/
127 // ShowBroken - Debugging aide /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This prints out the names of all the packages that are broken along
130 with the name of each each broken dependency and a quite version
131 description. */
132 void ShowBroken(ostream &out,pkgDepCache &Cache)
133 {
134 out << "Sorry, but the following packages have unmet dependencies:" << endl;
135 pkgCache::PkgIterator I = Cache.PkgBegin();
136 for (;I.end() != true; I++)
137 {
138 if (Cache[I].InstBroken() == false)
139 continue;
140
141 // Print out each package and the failed dependencies
142 out <<" " << I.Name() << ":";
143 int Indent = strlen(I.Name()) + 3;
144 bool First = true;
145 if (Cache[I].InstVerIter(Cache).end() == true)
146 {
147 cout << endl;
148 continue;
149 }
150
151 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
152 {
153 // Compute a single dependency element (glob or)
154 pkgCache::DepIterator Start;
155 pkgCache::DepIterator End;
156 D.GlobOr(Start,End);
157
158 if (Cache.IsImportantDep(End) == false ||
159 (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
160 continue;
161
162 if (First == false)
163 for (int J = 0; J != Indent; J++)
164 out << ' ';
165 First = false;
166
167 out << ' ' << End.DepType() << ": " << End.TargetPkg().Name();
168
169 // Show a quick summary of the version requirements
170 if (End.TargetVer() != 0)
171 out << " (" << End.CompType() << " " << End.TargetVer() <<
172 ")";
173
174 /* Show a summary of the target package if possible. In the case
175 of virtual packages we show nothing */
176
177 pkgCache::PkgIterator Targ = End.TargetPkg();
178 if (Targ->ProvidesList == 0)
179 {
180 out << " but ";
181 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
182 if (Ver.end() == false)
183 out << Ver.VerStr() << " is installed";
184 else
185 {
186 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
187 {
188 if (Targ->ProvidesList == 0)
189 out << "it is not installable";
190 else
191 out << "it is a virtual package";
192 }
193 else
194 out << "it is not installed";
195 }
196 }
197
198 out << endl;
199 }
200 }
201 }
202 /*}}}*/
203 // ShowNew - Show packages to newly install /*{{{*/
204 // ---------------------------------------------------------------------
205 /* */
206 void ShowNew(ostream &out,pkgDepCache &Dep)
207 {
208 /* Print out a list of packages that are going to be removed extra
209 to what the user asked */
210 pkgCache::PkgIterator I = Dep.PkgBegin();
211 string List;
212 for (;I.end() != true; I++)
213 if (Dep[I].NewInstall() == true)
214 List += string(I.Name()) + " ";
215 ShowList(out,"The following NEW packages will be installed:",List);
216 }
217 /*}}}*/
218 // ShowDel - Show packages to delete /*{{{*/
219 // ---------------------------------------------------------------------
220 /* */
221 void ShowDel(ostream &out,pkgDepCache &Dep)
222 {
223 /* Print out a list of packages that are going to be removed extra
224 to what the user asked */
225 pkgCache::PkgIterator I = Dep.PkgBegin();
226 string List;
227 for (;I.end() != true; I++)
228 if (Dep[I].Delete() == true)
229 List += string(I.Name()) + " ";
230
231 ShowList(out,"The following packages will be REMOVED:",List);
232 }
233 /*}}}*/
234 // ShowKept - Show kept packages /*{{{*/
235 // ---------------------------------------------------------------------
236 /* */
237 void ShowKept(ostream &out,pkgDepCache &Dep)
238 {
239 pkgCache::PkgIterator I = Dep.PkgBegin();
240 string List;
241 for (;I.end() != true; I++)
242 {
243 // Not interesting
244 if (Dep[I].Upgrade() == true || Dep[I].Upgradable() == false ||
245 I->CurrentVer == 0 || Dep[I].Delete() == true)
246 continue;
247
248 List += string(I.Name()) + " ";
249 }
250 ShowList(out,"The following packages have been kept back",List);
251 }
252 /*}}}*/
253 // ShowUpgraded - Show upgraded packages /*{{{*/
254 // ---------------------------------------------------------------------
255 /* */
256 void ShowUpgraded(ostream &out,pkgDepCache &Dep)
257 {
258 pkgCache::PkgIterator I = Dep.PkgBegin();
259 string List;
260 for (;I.end() != true; I++)
261 {
262 // Not interesting
263 if (Dep[I].Upgrade() == false || Dep[I].NewInstall() == true)
264 continue;
265
266 List += string(I.Name()) + " ";
267 }
268 ShowList(out,"The following packages will be upgraded",List);
269 }
270 /*}}}*/
271 // ShowHold - Show held but changed packages /*{{{*/
272 // ---------------------------------------------------------------------
273 /* */
274 bool ShowHold(ostream &out,pkgDepCache &Dep)
275 {
276 pkgCache::PkgIterator I = Dep.PkgBegin();
277 string List;
278 for (;I.end() != true; I++)
279 {
280 if (Dep[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
281 I->SelectedState == pkgCache::State::Hold)
282 List += string(I.Name()) + " ";
283 }
284
285 return ShowList(out,"The following held packages will be changed:",List);
286 }
287 /*}}}*/
288 // ShowEssential - Show an essential package warning /*{{{*/
289 // ---------------------------------------------------------------------
290 /* This prints out a warning message that is not to be ignored. It shows
291 all essential packages and their dependents that are to be removed.
292 It is insanely risky to remove the dependents of an essential package! */
293 bool ShowEssential(ostream &out,pkgDepCache &Dep)
294 {
295 pkgCache::PkgIterator I = Dep.PkgBegin();
296 string List;
297 bool *Added = new bool[Dep.HeaderP->PackageCount];
298 for (unsigned int I = 0; I != Dep.HeaderP->PackageCount; I++)
299 Added[I] = false;
300
301 for (;I.end() != true; I++)
302 {
303 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
304 continue;
305
306 // The essential package is being removed
307 if (Dep[I].Delete() == true)
308 {
309 if (Added[I->ID] == false)
310 {
311 Added[I->ID] = true;
312 List += string(I.Name()) + " ";
313 }
314 }
315
316 if (I->CurrentVer == 0)
317 continue;
318
319 // Print out any essential package depenendents that are to be removed
320 for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
321 {
322 // Skip everything but depends
323 if (D->Type != pkgCache::Dep::PreDepends &&
324 D->Type != pkgCache::Dep::Depends)
325 continue;
326
327 pkgCache::PkgIterator P = D.SmartTargetPkg();
328 if (Dep[P].Delete() == true)
329 {
330 if (Added[P->ID] == true)
331 continue;
332 Added[P->ID] = true;
333
334 char S[300];
335 sprintf(S,"%s (due to %s) ",P.Name(),I.Name());
336 List += S;
337 }
338 }
339 }
340
341 delete [] Added;
342 if (List.empty() == false)
343 out << "WARNING: The following essential packages will be removed" << endl;
344 return ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List);
345 }
346 /*}}}*/
347 // Stats - Show some statistics /*{{{*/
348 // ---------------------------------------------------------------------
349 /* */
350 void Stats(ostream &out,pkgDepCache &Dep)
351 {
352 unsigned long Upgrade = 0;
353 unsigned long Install = 0;
354 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
355 {
356 if (Dep[I].NewInstall() == true)
357 Install++;
358 else
359 if (Dep[I].Upgrade() == true)
360 Upgrade++;
361 }
362
363 out << Upgrade << " packages upgraded, " <<
364 Install << " newly installed, " <<
365 Dep.DelCount() << " to remove and " <<
366 Dep.KeepCount() << " not upgraded." << endl;
367
368 if (Dep.BadCount() != 0)
369 out << Dep.BadCount() << " packages not fully installed or removed." << endl;
370 }
371 /*}}}*/
372
373 // class CacheFile - Cover class for some dependency cache functions /*{{{*/
374 // ---------------------------------------------------------------------
375 /* */
376 class CacheFile : public pkgCacheFile
377 {
378 public:
379
380 bool CheckDeps(bool AllowBroken = false);
381 bool Open(bool WithLock = true)
382 {
383 OpTextProgress Prog(*_config);
384 return pkgCacheFile::Open(Prog,WithLock);
385 };
386 };
387 /*}}}*/
388 // CacheFile::Open - Open the cache file /*{{{*/
389 // ---------------------------------------------------------------------
390 /* This routine generates the caches and then opens the dependency cache
391 and verifies that the system is OK. */
392 bool CacheFile::CheckDeps(bool AllowBroken)
393 {
394 if (_error->PendingError() == true)
395 return false;
396
397 // Check that the system is OK
398 if (Cache->DelCount() != 0 || Cache->InstCount() != 0)
399 return _error->Error("Internal Error, non-zero counts");
400
401 // Apply corrections for half-installed packages
402 if (pkgApplyStatus(*Cache) == false)
403 return false;
404
405 // Nothing is broken
406 if (Cache->BrokenCount() == 0 || AllowBroken == true)
407 return true;
408
409 // Attempt to fix broken things
410 if (_config->FindB("APT::Get::Fix-Broken",false) == true)
411 {
412 c1out << "Correcting dependencies..." << flush;
413 if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0)
414 {
415 c1out << " failed." << endl;
416 ShowBroken(c1out,*this);
417
418 return _error->Error("Unable to correct dependencies");
419 }
420 if (pkgMinimizeUpgrade(*Cache) == false)
421 return _error->Error("Unable to minimize the upgrade set");
422
423 c1out << " Done" << endl;
424 }
425 else
426 {
427 c1out << "You might want to run `apt-get -f install' to correct these." << endl;
428 ShowBroken(c1out,*this);
429
430 return _error->Error("Unmet dependencies. Try using -f.");
431 }
432
433 return true;
434 }
435 /*}}}*/
436
437 // InstallPackages - Actually download and install the packages /*{{{*/
438 // ---------------------------------------------------------------------
439 /* This displays the informative messages describing what is going to
440 happen and then calls the download routines */
441 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,bool Saftey = true)
442 {
443 bool Fail = false;
444 bool Essential = false;
445
446 // Show all the various warning indicators
447 ShowDel(c1out,Cache);
448 ShowNew(c1out,Cache);
449 if (ShwKept == true)
450 ShowKept(c1out,Cache);
451 Fail |= !ShowHold(c1out,Cache);
452 if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
453 ShowUpgraded(c1out,Cache);
454 Essential = !ShowEssential(c1out,Cache);
455 Fail |= Essential;
456 Stats(c1out,Cache);
457
458 // Sanity check
459 if (Cache->BrokenCount() != 0)
460 {
461 ShowBroken(c1out,Cache);
462 return _error->Error("Internal Error, InstallPackages was called with broken packages!");
463 }
464
465 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
466 Cache->BadCount() == 0)
467 return true;
468
469 // Run the simulator ..
470 if (_config->FindB("APT::Get::Simulate") == true)
471 {
472 pkgSimulate PM(Cache);
473 return PM.DoInstall();
474 }
475
476 // Create the text record parser
477 pkgRecords Recs(Cache);
478 if (_error->PendingError() == true)
479 return false;
480
481 // Lock the archive directory
482 FileFd Lock;
483 if (_config->FindB("Debug::NoLocking",false) == false)
484 {
485 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
486 if (_error->PendingError() == true)
487 return _error->Error("Unable to lock the download directory");
488 }
489
490 // Create the download object
491 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
492 pkgAcquire Fetcher(&Stat);
493
494 // Read the source list
495 pkgSourceList List;
496 if (List.ReadMainList() == false)
497 return _error->Error("The list of sources could not be read.");
498
499 // Create the package manager and prepare to download
500 pkgDPkgPM PM(Cache);
501 if (PM.GetArchives(&Fetcher,&List,&Recs) == false ||
502 _error->PendingError() == true)
503 return false;
504
505 // Display statistics
506 unsigned long FetchBytes = Fetcher.FetchNeeded();
507 unsigned long FetchPBytes = Fetcher.PartialPresent();
508 unsigned long DebBytes = Fetcher.TotalNeeded();
509 if (DebBytes != Cache->DebSize())
510 {
511 c0out << DebBytes << ',' << Cache->DebSize() << endl;
512 c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
513 }
514
515 // Check for enough free space
516 struct statfs Buf;
517 string OutputDir = _config->FindDir("Dir::Cache::Archives");
518 if (statfs(OutputDir.c_str(),&Buf) != 0)
519 return _error->Errno("statfs","Couldn't determine free space in %s",
520 OutputDir.c_str());
521 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
522 return _error->Error("Sorry, you don't have enough free space in %s",
523 OutputDir.c_str());
524
525 // Number of bytes
526 c1out << "Need to get ";
527 if (DebBytes != FetchBytes)
528 c1out << SizeToStr(FetchBytes) << "b/" << SizeToStr(DebBytes) << 'b';
529 else
530 c1out << SizeToStr(DebBytes) << 'b';
531
532 c1out << " of archives. After unpacking ";
533
534 // Size delta
535 if (Cache->UsrSize() >= 0)
536 c1out << SizeToStr(Cache->UsrSize()) << "b will be used." << endl;
537 else
538 c1out << SizeToStr(-1*Cache->UsrSize()) << "b will be freed." << endl;
539
540 if (_error->PendingError() == true)
541 return false;
542
543 // Fail safe check
544 if (_config->FindI("quiet",0) >= 2 ||
545 _config->FindB("APT::Get::Assume-Yes",false) == true)
546 {
547 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
548 return _error->Error("There are problems and -y was used without --force-yes");
549 }
550
551 if (Essential == true && Saftey == true)
552 {
553 c2out << "You are about to do something potentially harmful" << endl;
554 c2out << "To continue type in the phrase 'Yes, I understand this may be bad'" << endl;
555 c2out << " ?] " << flush;
556 if (AnalPrompt("Yes, I understand this may be bad") == false)
557 {
558 c2out << "Abort." << endl;
559 exit(1);
560 }
561 }
562 else
563 {
564 // Prompt to continue
565 if (Ask == true || Fail == true)
566 {
567 if (_config->FindI("quiet",0) < 2 &&
568 _config->FindB("APT::Get::Assume-Yes",false) == false)
569 {
570 c2out << "Do you want to continue? [Y/n] " << flush;
571
572 if (YnPrompt() == false)
573 {
574 c2out << "Abort." << endl;
575 exit(1);
576 }
577 }
578 }
579 }
580
581 // Just print out the uris an exit if the --print-uris flag was used
582 if (_config->FindB("APT::Get::Print-URIs") == true)
583 {
584 pkgAcquire::UriIterator I = Fetcher.UriBegin();
585 for (; I != Fetcher.UriEnd(); I++)
586 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
587 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
588 return true;
589 }
590
591 // Run it
592 if (_config->FindB("APT::Get::No-Download",false) == false)
593 if( Fetcher.Run() == false)
594 return false;
595
596 // Print out errors
597 bool Failed = false;
598 bool Transient = false;
599 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
600 {
601 if ((*I)->Status == pkgAcquire::Item::StatDone &&
602 (*I)->Complete == true)
603 continue;
604
605 (*I)->Finished();
606
607 if ((*I)->Status == pkgAcquire::Item::StatIdle)
608 {
609 Transient = true;
610 Failed = true;
611 continue;
612 }
613
614 cerr << "Failed to fetch " << (*I)->DescURI() << endl;
615 cerr << " " << (*I)->ErrorText << endl;
616 Failed = true;
617 }
618
619 if (_config->FindB("APT::Get::Download-Only",false) == true)
620 {
621 if (Failed == false && _config->FindB("APT::Get::Fix-Missing",false) == false)
622 return _error->Error("Some files failed to download");
623 return true;
624 }
625
626 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
627 {
628 if (Transient == true)
629 {
630 c2out << "Upgrading with disk swapping is not supported in this version." << endl;
631 c2out << "Try running multiple times with --fix-missing" << endl;
632 }
633
634 return _error->Error("Unable to fetch some archives, maybe try with --fix-missing?");
635 }
636
637 // Try to deal with missing package files
638 if (PM.FixMissing() == false)
639 {
640 cerr << "Unable to correct missing packages." << endl;
641 return _error->Error("Aborting Install.");
642 }
643
644 Cache.ReleaseLock();
645 return PM.DoInstall();
646 }
647 /*}}}*/
648
649 // DoUpdate - Update the package lists /*{{{*/
650 // ---------------------------------------------------------------------
651 /* */
652 bool DoUpdate(CommandLine &)
653 {
654 // Get the source list
655 pkgSourceList List;
656 if (List.ReadMainList() == false)
657 return false;
658
659 // Lock the list directory
660 FileFd Lock;
661 if (_config->FindB("Debug::NoLocking",false) == false)
662 {
663 Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
664 if (_error->PendingError() == true)
665 return _error->Error("Unable to lock the list directory");
666 }
667
668 // Create the download object
669 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
670 pkgAcquire Fetcher(&Stat);
671
672 // Populate it with the source selection
673 pkgSourceList::const_iterator I;
674 for (I = List.begin(); I != List.end(); I++)
675 {
676 new pkgAcqIndex(&Fetcher,I);
677 if (_error->PendingError() == true)
678 return false;
679 }
680
681 // Run it
682 if (Fetcher.Run() == false)
683 return false;
684
685 // Clean out any old list files
686 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
687 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
688 return false;
689
690 // Prepare the cache.
691 CacheFile Cache;
692 if (Cache.Open() == false)
693 return false;
694
695 return true;
696 }
697 /*}}}*/
698 // DoUpgrade - Upgrade all packages /*{{{*/
699 // ---------------------------------------------------------------------
700 /* Upgrade all packages without installing new packages or erasing old
701 packages */
702 bool DoUpgrade(CommandLine &CmdL)
703 {
704 CacheFile Cache;
705 if (Cache.Open() == false || Cache.CheckDeps() == false)
706 return false;
707
708 // Do the upgrade
709 if (pkgAllUpgrade(Cache) == false)
710 {
711 ShowBroken(c1out,Cache);
712 return _error->Error("Internal Error, AllUpgrade broke stuff");
713 }
714
715 return InstallPackages(Cache,true);
716 }
717 /*}}}*/
718 // DoInstall - Install packages from the command line /*{{{*/
719 // ---------------------------------------------------------------------
720 /* Install named packages */
721 bool DoInstall(CommandLine &CmdL)
722 {
723 CacheFile Cache;
724 if (Cache.Open() == false || Cache.CheckDeps(CmdL.FileSize() != 1) == false)
725 return false;
726
727 // Enter the special broken fixing mode if the user specified arguments
728 bool BrokenFix = false;
729 if (Cache->BrokenCount() != 0)
730 BrokenFix = true;
731
732 unsigned int ExpectedInst = 0;
733 unsigned int Packages = 0;
734 pkgProblemResolver Fix(Cache);
735
736 bool DefRemove = false;
737 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
738 DefRemove = true;
739
740 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
741 {
742 // Duplicate the string
743 unsigned int Length = strlen(*I);
744 char S[300];
745 if (Length >= sizeof(S))
746 continue;
747 strcpy(S,*I);
748
749 // See if we are removing the package
750 bool Remove = DefRemove;
751 if (Cache->FindPkg(S).end() == true)
752 {
753 // Handle an optional end tag indicating what to do
754 if (S[Length - 1] == '-')
755 {
756 Remove = true;
757 S[--Length] = 0;
758 }
759 if (S[Length - 1] == '+')
760 {
761 Remove = false;
762 S[--Length] = 0;
763 }
764 }
765
766 // Locate the package
767 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
768 Packages++;
769 if (Pkg.end() == true)
770 return _error->Error("Couldn't find package %s",S);
771
772 // Handle the no-upgrade case
773 if (_config->FindB("APT::Get::no-upgrade",false) == true &&
774 Pkg->CurrentVer != 0)
775 {
776 c1out << "Skipping " << Pkg.Name() << ", it is already installed and no-upgrade is set." << endl;
777 continue;
778 }
779
780 // Check if there is something new to install
781 pkgDepCache::StateCache &State = (*Cache)[Pkg];
782 if (State.CandidateVer == 0)
783 {
784 if (Pkg->ProvidesList != 0)
785 {
786 c1out << "Package " << S << " is a virtual package provided by:" << endl;
787
788 pkgCache::PrvIterator I = Pkg.ProvidesList();
789 for (; I.end() == false; I++)
790 {
791 pkgCache::PkgIterator Pkg = I.OwnerPkg();
792
793 if ((*Cache)[Pkg].CandidateVerIter(*Cache) == I.OwnerVer())
794 {
795 if ((*Cache)[Pkg].Install() == true && (*Cache)[Pkg].NewInstall() == false)
796 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
797 " [Installed]"<< endl;
798 else
799 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
800 }
801 }
802 c1out << "You should explicly select one to install." << endl;
803 }
804 else
805 {
806 c1out << "Package " << S << " has no available version, but exists in the database." << endl;
807 c1out << "This typically means that the package was mentioned in a dependency and " << endl;
808 c1out << "never uploaded, or that it is an obsolete package." << endl;
809
810 string List;
811 pkgCache::DepIterator Dep = Pkg.RevDependsList();
812 for (; Dep.end() == false; Dep++)
813 {
814 if (Dep->Type != pkgCache::Dep::Replaces)
815 continue;
816 List += string(Dep.ParentPkg().Name()) + " ";
817 }
818 ShowList(c1out,"However the following packages replace it:",List);
819 }
820
821 return _error->Error("Package %s has no installation candidate",S);
822 }
823
824 Fix.Protect(Pkg);
825 if (Remove == true)
826 {
827 Fix.Remove(Pkg);
828 Cache->MarkDelete(Pkg);
829 continue;
830 }
831
832 // Install it
833 Cache->MarkInstall(Pkg,false);
834 if (State.Install() == false)
835 c1out << "Sorry, " << S << " is already the newest version" << endl;
836 else
837 ExpectedInst++;
838
839 // Install it with autoinstalling enabled.
840 if (State.InstBroken() == true && BrokenFix == false)
841 Cache->MarkInstall(Pkg,true);
842 }
843
844 /* If we are in the Broken fixing mode we do not attempt to fix the
845 problems. This is if the user invoked install without -f and gave
846 packages */
847 if (BrokenFix == true && Cache->BrokenCount() != 0)
848 {
849 c1out << "You might want to run `apt-get -f install' to correct these:" << endl;
850 ShowBroken(c1out,Cache);
851
852 return _error->Error("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).");
853 }
854
855 // Call the scored problem resolver
856 Fix.InstallProtect();
857 if (Fix.Resolve(true) == false)
858 _error->Discard();
859
860 // Now we check the state of the packages,
861 if (Cache->BrokenCount() != 0)
862 {
863 c1out << "Some packages could not be installed. This may mean that you have" << endl;
864 c1out << "requested an impossible situation or if you are using the unstable" << endl;
865 c1out << "distribution that some required packages have not yet been created" << endl;
866 c1out << "or been moved out of Incoming." << endl;
867 if (Packages == 1)
868 {
869 c1out << endl;
870 c1out << "Since you only requested a single operation it is extremely likely that" << endl;
871 c1out << "the package is simply not installable and a bug report against" << endl;
872 c1out << "that package should be filed." << endl;
873 }
874
875 c1out << "The following information may help to resolve the situation:" << endl;
876 c1out << endl;
877 ShowBroken(c1out,Cache);
878 return _error->Error("Sorry, broken packages");
879 }
880
881 /* Print out a list of packages that are going to be installed extra
882 to what the user asked */
883 if (Cache->InstCount() != ExpectedInst)
884 {
885 string List;
886 pkgCache::PkgIterator I = Cache->PkgBegin();
887 for (;I.end() != true; I++)
888 {
889 if ((*Cache)[I].Install() == false)
890 continue;
891
892 const char **J;
893 for (J = CmdL.FileList + 1; *J != 0; J++)
894 if (strcmp(*J,I.Name()) == 0)
895 break;
896
897 if (*J == 0)
898 List += string(I.Name()) + " ";
899 }
900
901 ShowList(c1out,"The following extra packages will be installed:",List);
902 }
903
904 // See if we need to prompt
905 if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
906 return InstallPackages(Cache,false,false);
907
908 return InstallPackages(Cache,false);
909 }
910 /*}}}*/
911 // DoDistUpgrade - Automatic smart upgrader /*{{{*/
912 // ---------------------------------------------------------------------
913 /* Intelligent upgrader that will install and remove packages at will */
914 bool DoDistUpgrade(CommandLine &CmdL)
915 {
916 CacheFile Cache;
917 if (Cache.Open() == false || Cache.CheckDeps() == false)
918 return false;
919
920 c0out << "Calculating Upgrade... " << flush;
921 if (pkgDistUpgrade(*Cache) == false)
922 {
923 c0out << "Failed" << endl;
924 ShowBroken(c1out,Cache);
925 return false;
926 }
927
928 c0out << "Done" << endl;
929
930 return InstallPackages(Cache,true);
931 }
932 /*}}}*/
933 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
934 // ---------------------------------------------------------------------
935 /* Follows dselect's selections */
936 bool DoDSelectUpgrade(CommandLine &CmdL)
937 {
938 CacheFile Cache;
939 if (Cache.Open() == false || Cache.CheckDeps() == false)
940 return false;
941
942 // Install everything with the install flag set
943 pkgCache::PkgIterator I = Cache->PkgBegin();
944 for (;I.end() != true; I++)
945 {
946 /* Install the package only if it is a new install, the autoupgrader
947 will deal with the rest */
948 if (I->SelectedState == pkgCache::State::Install)
949 Cache->MarkInstall(I,false);
950 }
951
952 /* Now install their deps too, if we do this above then order of
953 the status file is significant for | groups */
954 for (I = Cache->PkgBegin();I.end() != true; I++)
955 {
956 /* Install the package only if it is a new install, the autoupgrader
957 will deal with the rest */
958 if (I->SelectedState == pkgCache::State::Install)
959 Cache->MarkInstall(I,true);
960 }
961
962 // Apply erasures now, they override everything else.
963 for (I = Cache->PkgBegin();I.end() != true; I++)
964 {
965 // Remove packages
966 if (I->SelectedState == pkgCache::State::DeInstall ||
967 I->SelectedState == pkgCache::State::Purge)
968 Cache->MarkDelete(I);
969 }
970
971 /* Resolve any problems that dselect created, allupgrade cannot handle
972 such things. We do so quite agressively too.. */
973 if (Cache->BrokenCount() != 0)
974 {
975 pkgProblemResolver Fix(Cache);
976
977 // Hold back held packages.
978 if (_config->FindB("APT::Ingore-Hold",false) == false)
979 {
980 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
981 {
982 if (I->SelectedState == pkgCache::State::Hold)
983 {
984 Fix.Protect(I);
985 Cache->MarkKeep(I);
986 }
987 }
988 }
989
990 if (Fix.Resolve() == false)
991 {
992 ShowBroken(c1out,Cache);
993 return _error->Error("Internal Error, problem resolver broke stuff");
994 }
995 }
996
997 // Now upgrade everything
998 if (pkgAllUpgrade(Cache) == false)
999 {
1000 ShowBroken(c1out,Cache);
1001 return _error->Error("Internal Error, problem resolver broke stuff");
1002 }
1003
1004 return InstallPackages(Cache,false);
1005 }
1006 /*}}}*/
1007 // DoClean - Remove download archives /*{{{*/
1008 // ---------------------------------------------------------------------
1009 /* */
1010 bool DoClean(CommandLine &CmdL)
1011 {
1012 pkgAcquire Fetcher;
1013 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
1014 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
1015 return true;
1016 }
1017 /*}}}*/
1018 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
1019 // ---------------------------------------------------------------------
1020 /* This is similar to clean but it only purges things that cannot be
1021 downloaded, that is old versions of cached packages. */
1022 class LogCleaner : public pkgArchiveCleaner
1023 {
1024 protected:
1025 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
1026 {
1027 cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "b]" << endl;
1028 };
1029 };
1030
1031 bool DoAutoClean(CommandLine &CmdL)
1032 {
1033 CacheFile Cache;
1034 if (Cache.Open() == false)
1035 return false;
1036
1037 LogCleaner Cleaner;
1038
1039 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
1040 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
1041 }
1042 /*}}}*/
1043 // DoCheck - Perform the check operation /*{{{*/
1044 // ---------------------------------------------------------------------
1045 /* Opening automatically checks the system, this command is mostly used
1046 for debugging */
1047 bool DoCheck(CommandLine &CmdL)
1048 {
1049 CacheFile Cache;
1050 Cache.Open();
1051 Cache.CheckDeps();
1052
1053 return true;
1054 }
1055 /*}}}*/
1056 // DoSource - Fetch a source archive /*{{{*/
1057 // ---------------------------------------------------------------------
1058 /* Fetch souce packages */
1059 struct DscFile
1060 {
1061 string Package;
1062 string Version;
1063 string Dsc;
1064 };
1065
1066 bool DoSource(CommandLine &CmdL)
1067 {
1068 CacheFile Cache;
1069 if (Cache.Open(false) == false)
1070 return false;
1071
1072 if (CmdL.FileSize() <= 1)
1073 return _error->Error("Must specify at least one package to fetch source for");
1074
1075 // Read the source list
1076 pkgSourceList List;
1077 if (List.ReadMainList() == false)
1078 return _error->Error("The list of sources could not be read.");
1079
1080 // Create the text record parsers
1081 pkgRecords Recs(Cache);
1082 pkgSrcRecords SrcRecs(List);
1083 if (_error->PendingError() == true)
1084 return false;
1085
1086 // Create the download object
1087 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1088 pkgAcquire Fetcher(&Stat);
1089
1090 DscFile *Dsc = new DscFile[CmdL.FileSize()];
1091
1092 // Load the requestd sources into the fetcher
1093 unsigned J = 0;
1094 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1095 {
1096 string Src;
1097
1098 /* Lookup the version of the package we would install if we were to
1099 install a version and determine the source package name, then look
1100 in the archive for a source package of the same name. In theory
1101 we could stash the version string as well and match that too but
1102 today there aren't multi source versions in the archive. */
1103 pkgCache::PkgIterator Pkg = Cache->FindPkg(*I);
1104 if (Pkg.end() == false)
1105 {
1106 pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
1107 if (Ver.end() == false)
1108 {
1109 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1110 Src = Parse.SourcePkg();
1111 }
1112 }
1113
1114 // No source package name..
1115 if (Src.empty() == true)
1116 Src = *I;
1117
1118 // The best hit
1119 pkgSrcRecords::Parser *Last = 0;
1120 unsigned long Offset = 0;
1121 string Version;
1122 bool IsMatch = false;
1123
1124 // Iterate over all of the hits
1125 pkgSrcRecords::Parser *Parse;
1126 SrcRecs.Restart();
1127 while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
1128 {
1129 string Ver = Parse->Version();
1130
1131 // Skip name mismatches
1132 if (IsMatch == true && Parse->Package() != Src)
1133 continue;
1134
1135 // Newer version or an exact match
1136 if (Last == 0 || pkgVersionCompare(Version,Ver) < 0 ||
1137 (Parse->Package() == Src && IsMatch == false))
1138 {
1139 IsMatch = Parse->Package() == Src;
1140 Last = Parse;
1141 Offset = Parse->Offset();
1142 Version = Ver;
1143 }
1144 }
1145
1146 if (Last == 0)
1147 return _error->Error("Unable to find a source package for %s",Src.c_str());
1148
1149 // Back track
1150 vector<pkgSrcRecords::File> Lst;
1151 if (Last->Jump(Offset) == false || Last->Files(Lst) == false)
1152 return false;
1153
1154 // Load them into the fetcher
1155 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
1156 I != Lst.end(); I++)
1157 {
1158 // Try to guess what sort of file it is we are getting.
1159 string Comp;
1160 if (I->Path.find(".dsc") != string::npos)
1161 {
1162 Comp = "dsc";
1163 Dsc[J].Package = Last->Package();
1164 Dsc[J].Version = Last->Version();
1165 Dsc[J].Dsc = flNotDir(I->Path);
1166 }
1167
1168 if (I->Path.find(".tar.gz") != string::npos)
1169 Comp = "tar";
1170 if (I->Path.find(".diff.gz") != string::npos)
1171 Comp = "diff";
1172
1173 new pkgAcqFile(&Fetcher,Last->Source()->ArchiveURI(I->Path),
1174 I->MD5Hash,I->Size,Last->Source()->SourceInfo(Src,
1175 Last->Version(),Comp),Src);
1176 }
1177 }
1178
1179 // Display statistics
1180 unsigned long FetchBytes = Fetcher.FetchNeeded();
1181 unsigned long FetchPBytes = Fetcher.PartialPresent();
1182 unsigned long DebBytes = Fetcher.TotalNeeded();
1183
1184 // Check for enough free space
1185 struct statfs Buf;
1186 string OutputDir = ".";
1187 if (statfs(OutputDir.c_str(),&Buf) != 0)
1188 return _error->Errno("statfs","Couldn't determine free space in %s",
1189 OutputDir.c_str());
1190 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
1191 return _error->Error("Sorry, you don't have enough free space in %s",
1192 OutputDir.c_str());
1193
1194 // Number of bytes
1195 c1out << "Need to get ";
1196 if (DebBytes != FetchBytes)
1197 c1out << SizeToStr(FetchBytes) << "b/" << SizeToStr(DebBytes) << 'b';
1198 else
1199 c1out << SizeToStr(DebBytes) << 'b';
1200 c1out << " of source archives." << endl;
1201
1202 if (_config->FindB("APT::Get::Simulate",false) == true)
1203 {
1204 for (unsigned I = 0; I != J; I++)
1205 cout << "Fetch Source " << Dsc[I].Package << endl;
1206 return true;
1207 }
1208
1209 // Just print out the uris an exit if the --print-uris flag was used
1210 if (_config->FindB("APT::Get::Print-URIs") == true)
1211 {
1212 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1213 for (; I != Fetcher.UriEnd(); I++)
1214 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1215 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
1216 return true;
1217 }
1218
1219 // Run it
1220 if (Fetcher.Run() == false)
1221 return false;
1222
1223 // Print error messages
1224 bool Failed = false;
1225 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1226 {
1227 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1228 (*I)->Complete == true)
1229 continue;
1230
1231 cerr << "Failed to fetch " << (*I)->DescURI() << endl;
1232 cerr << " " << (*I)->ErrorText << endl;
1233 Failed = true;
1234 }
1235 if (Failed == true)
1236 return _error->Error("Failed to fetch some archives.");
1237
1238 if (_config->FindB("APT::Get::Download-only",false) == true)
1239 return true;
1240
1241 // Unpack the sources
1242 pid_t Process = ExecFork();
1243
1244 if (Process == 0)
1245 {
1246 for (unsigned I = 0; I != J; I++)
1247 {
1248 string Dir = Dsc[I].Package + '-' + pkgBaseVersion(Dsc[I].Version.c_str());
1249
1250 // See if the package is already unpacked
1251 struct stat Stat;
1252 if (stat(Dir.c_str(),&Stat) == 0 &&
1253 S_ISDIR(Stat.st_mode) != 0)
1254 {
1255 c0out << "Skipping unpack of already unpacked source in " << Dir << endl;
1256 }
1257 else
1258 {
1259 // Call dpkg-source
1260 char S[500];
1261 snprintf(S,sizeof(S),"%s -x %s",
1262 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
1263 Dsc[I].Dsc.c_str());
1264 if (system(S) != 0)
1265 {
1266 cerr << "Unpack command '" << S << "' failed." << endl;
1267 _exit(1);
1268 }
1269 }
1270
1271 // Try to compile it with dpkg-buildpackage
1272 if (_config->FindB("APT::Get::Compile",false) == true)
1273 {
1274 // Call dpkg-buildpackage
1275 char S[500];
1276 snprintf(S,sizeof(S),"cd %s && %s %s",
1277 Dir.c_str(),
1278 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
1279 _config->Find("DPkg::Build-Options","-b -uc").c_str());
1280
1281 if (system(S) != 0)
1282 {
1283 cerr << "Build command '" << S << "' failed." << endl;
1284 _exit(1);
1285 }
1286 }
1287 }
1288
1289 _exit(0);
1290 }
1291
1292 // Wait for the subprocess
1293 int Status = 0;
1294 while (waitpid(Process,&Status,0) != Process)
1295 {
1296 if (errno == EINTR)
1297 continue;
1298 return _error->Errno("waitpid","Couldn't wait for subprocess");
1299 }
1300
1301 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
1302 return _error->Error("Child process failed");
1303
1304 return true;
1305 }
1306 /*}}}*/
1307
1308 // ShowHelp - Show a help screen /*{{{*/
1309 // ---------------------------------------------------------------------
1310 /* */
1311 bool ShowHelp(CommandLine &CmdL)
1312 {
1313 cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
1314 " compiled on " << __DATE__ << " " << __TIME__ << endl;
1315 if (_config->FindB("version") == true)
1316 return 100;
1317
1318 cout << "Usage: apt-get [options] command" << endl;
1319 cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl;
1320 cout << endl;
1321 cout << "apt-get is a simple command line interface for downloading and" << endl;
1322 cout << "installing packages. The most frequently used commands are update" << endl;
1323 cout << "and install." << endl;
1324 cout << endl;
1325 cout << "Commands:" << endl;
1326 cout << " update - Retrieve new lists of packages" << endl;
1327 cout << " upgrade - Perform an upgrade" << endl;
1328 cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl;
1329 cout << " remove - Remove packages" << endl;
1330 cout << " source - Download source archives" << endl;
1331 cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
1332 cout << " dselect-upgrade - Follow dselect selections" << endl;
1333 cout << " clean - Erase downloaded archive files" << endl;
1334 cout << " autoclean - Erase old downloaded archive files" << endl;
1335 cout << " check - Verify that there are no broken dependencies" << endl;
1336 cout << endl;
1337 cout << "Options:" << endl;
1338 cout << " -h This help text." << endl;
1339 cout << " -q Loggable output - no progress indicator" << endl;
1340 cout << " -qq No output except for errors" << endl;
1341 cout << " -d Download only - do NOT install or unpack archives" << endl;
1342 cout << " -s No-act. Perform ordering simulation" << endl;
1343 cout << " -y Assume Yes to all queries and do not prompt" << endl;
1344 cout << " -f Attempt to continue if the integrity check fails" << endl;
1345 cout << " -m Attempt to continue if archives are unlocatable" << endl;
1346 cout << " -u Show a list of upgraded packages as well" << endl;
1347 cout << " -b Build the source package after fetching it" << endl;
1348 cout << " -c=? Read this configuration file" << endl;
1349 cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
1350 cout << "See the apt-get(8), sources.list(5) and apt.conf(5) manual" << endl;
1351 cout << "pages for more information." << endl;
1352 return 100;
1353 }
1354 /*}}}*/
1355 // GetInitialize - Initialize things for apt-get /*{{{*/
1356 // ---------------------------------------------------------------------
1357 /* */
1358 void GetInitialize()
1359 {
1360 _config->Set("quiet",0);
1361 _config->Set("help",false);
1362 _config->Set("APT::Get::Download-Only",false);
1363 _config->Set("APT::Get::Simulate",false);
1364 _config->Set("APT::Get::Assume-Yes",false);
1365 _config->Set("APT::Get::Fix-Broken",false);
1366 _config->Set("APT::Get::Force-Yes",false);
1367 }
1368 /*}}}*/
1369 // SigWinch - Window size change signal handler /*{{{*/
1370 // ---------------------------------------------------------------------
1371 /* */
1372 void SigWinch(int)
1373 {
1374 // Riped from GNU ls
1375 #ifdef TIOCGWINSZ
1376 struct winsize ws;
1377
1378 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
1379 ScreenWidth = ws.ws_col - 1;
1380 #endif
1381 }
1382 /*}}}*/
1383
1384 int main(int argc,const char *argv[])
1385 {
1386 CommandLine::Args Args[] = {
1387 {'h',"help","help",0},
1388 {'v',"version","version",0},
1389 {'q',"quiet","quiet",CommandLine::IntLevel},
1390 {'q',"silent","quiet",CommandLine::IntLevel},
1391 {'d',"download-only","APT::Get::Download-Only",0},
1392 {'b',"compile","APT::Get::Compile",0},
1393 {'b',"build","APT::Get::Compile",0},
1394 {'s',"simulate","APT::Get::Simulate",0},
1395 {'s',"just-print","APT::Get::Simulate",0},
1396 {'s',"recon","APT::Get::Simulate",0},
1397 {'s',"no-act","APT::Get::Simulate",0},
1398 {'y',"yes","APT::Get::Assume-Yes",0},
1399 {'y',"assume-yes","APT::Get::Assume-Yes",0},
1400 {'f',"fix-broken","APT::Get::Fix-Broken",0},
1401 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
1402 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
1403 {0,"no-download","APT::Get::No-Download",0},
1404 {0,"fix-missing","APT::Get::Fix-Missing",0},
1405 {0,"ignore-hold","APT::Ingore-Hold",0},
1406 {0,"no-upgrade","APT::Get::no-upgrade",0},
1407 {0,"force-yes","APT::Get::force-yes",0},
1408 {0,"print-uris","APT::Get::Print-URIs",0},
1409 {'c',"config-file",0,CommandLine::ConfigFile},
1410 {'o',"option",0,CommandLine::ArbItem},
1411 {0,0,0,0}};
1412 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
1413 {"upgrade",&DoUpgrade},
1414 {"install",&DoInstall},
1415 {"remove",&DoInstall},
1416 {"dist-upgrade",&DoDistUpgrade},
1417 {"dselect-upgrade",&DoDSelectUpgrade},
1418 {"clean",&DoClean},
1419 {"autoclean",&DoAutoClean},
1420 {"check",&DoCheck},
1421 {"source",&DoSource},
1422 {"help",&ShowHelp},
1423 {0,0}};
1424
1425 // Parse the command line and initialize the package library
1426 CommandLine CmdL(Args,_config);
1427 if (pkgInitialize(*_config) == false ||
1428 CmdL.Parse(argc,argv) == false)
1429 {
1430 _error->DumpErrors();
1431 return 100;
1432 }
1433
1434 // See if the help should be shown
1435 if (_config->FindB("help") == true ||
1436 _config->FindB("version") == true ||
1437 CmdL.FileSize() == 0)
1438 return ShowHelp(CmdL);
1439
1440 // Deal with stdout not being a tty
1441 if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
1442 _config->Set("quiet","1");
1443
1444 // Setup the output streams
1445 c0out.rdbuf(cout.rdbuf());
1446 c1out.rdbuf(cout.rdbuf());
1447 c2out.rdbuf(cout.rdbuf());
1448 if (_config->FindI("quiet",0) > 0)
1449 c0out.rdbuf(devnull.rdbuf());
1450 if (_config->FindI("quiet",0) > 1)
1451 c1out.rdbuf(devnull.rdbuf());
1452
1453 // Setup the signals
1454 signal(SIGPIPE,SIG_IGN);
1455 signal(SIGWINCH,SigWinch);
1456 SigWinch(0);
1457
1458 // Match the operation
1459 CmdL.DispatchArg(Cmds);
1460
1461 // Print any errors or warnings found during parsing
1462 if (_error->empty() == false)
1463 {
1464 bool Errors = _error->PendingError();
1465 _error->DumpErrors();
1466 return Errors == true?100:0;
1467 }
1468
1469 return 0;
1470 }