]> git.saurik.com Git - apt.git/blob - cmdline/apt-get.cc
Fixed protect bug
[apt.git] / cmdline / apt-get.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-get.cc,v 1.38 1999/02/01 08:11:57 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/pkgcachegen.h>
34 #include <apt-pkg/algorithms.h>
35 #include <apt-pkg/acquire-item.h>
36 #include <apt-pkg/dpkgpm.h>
37 #include <apt-pkg/dpkginit.h>
38 #include <apt-pkg/strutl.h>
39 #include <apt-pkg/clean.h>
40
41 #include <config.h>
42
43 #include "acqprogress.h"
44
45 #include <fstream.h>
46 #include <termios.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 #include <signal.h>
50 #include <stdio.h>
51 /*}}}*/
52
53 ostream c0out;
54 ostream c1out;
55 ostream c2out;
56 ofstream devnull("/dev/null");
57 unsigned int ScreenWidth = 80;
58
59 // YnPrompt - Yes No Prompt. /*{{{*/
60 // ---------------------------------------------------------------------
61 /* Returns true on a Yes.*/
62 bool YnPrompt()
63 {
64 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
65 {
66 c1out << 'Y' << endl;
67 return true;
68 }
69
70 char C = 0;
71 char Jnk = 0;
72 read(STDIN_FILENO,&C,1);
73 while (C != '\n' && Jnk != '\n') read(STDIN_FILENO,&Jnk,1);
74
75 if (!(C == 'Y' || C == 'y' || C == '\n' || C == '\r'))
76 return false;
77 return true;
78 }
79 /*}}}*/
80 // ShowList - Show a list /*{{{*/
81 // ---------------------------------------------------------------------
82 /* This prints out a string of space seperated words with a title and
83 a two space indent line wraped to the current screen width. */
84 bool ShowList(ostream &out,string Title,string List)
85 {
86 if (List.empty() == true)
87 return true;
88
89 // Acount for the leading space
90 int ScreenWidth = ::ScreenWidth - 3;
91
92 out << Title << endl;
93 string::size_type Start = 0;
94 while (Start < List.size())
95 {
96 string::size_type End;
97 if (Start + ScreenWidth >= List.size())
98 End = List.size();
99 else
100 End = List.rfind(' ',Start+ScreenWidth);
101
102 if (End == string::npos || End < Start)
103 End = Start + ScreenWidth;
104 out << " " << string(List,Start,End - Start) << endl;
105 Start = End + 1;
106 }
107 return false;
108 }
109 /*}}}*/
110 // ShowBroken - Debugging aide /*{{{*/
111 // ---------------------------------------------------------------------
112 /* This prints out the names of all the packages that are broken along
113 with the name of each each broken dependency and a quite version
114 description. */
115 void ShowBroken(ostream &out,pkgDepCache &Cache)
116 {
117 out << "Sorry, but the following packages have unmet dependencies:" << endl;
118 pkgCache::PkgIterator I = Cache.PkgBegin();
119 for (;I.end() != true; I++)
120 {
121 if (Cache[I].InstBroken() == false)
122 continue;
123
124 // Print out each package and the failed dependencies
125 out <<" " << I.Name() << ":";
126 int Indent = strlen(I.Name()) + 3;
127 bool First = true;
128 if (Cache[I].InstVerIter(Cache).end() == true)
129 {
130 cout << endl;
131 continue;
132 }
133
134 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
135 {
136 // Compute a single dependency element (glob or)
137 pkgCache::DepIterator Start;
138 pkgCache::DepIterator End;
139 D.GlobOr(Start,End);
140
141 if (Cache.IsImportantDep(End) == false ||
142 (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
143 continue;
144
145 if (First == false)
146 for (int J = 0; J != Indent; J++)
147 out << ' ';
148 First = false;
149
150 cout << ' ' << End.DepType() << ": " << End.TargetPkg().Name();
151
152 // Show a quick summary of the version requirements
153 if (End.TargetVer() != 0)
154 out << " (" << End.CompType() << " " << End.TargetVer() <<
155 ")";
156
157 /* Show a summary of the target package if possible. In the case
158 of virtual packages we show nothing */
159
160 pkgCache::PkgIterator Targ = End.TargetPkg();
161 if (Targ->ProvidesList == 0)
162 {
163 out << " but ";
164 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
165 if (Ver.end() == false)
166 out << Ver.VerStr() << " is installed";
167 else
168 {
169 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
170 {
171 if (Targ->ProvidesList == 0)
172 out << "it is not installable";
173 else
174 out << "it is a virtual package";
175 }
176 else
177 out << "it is not installed";
178 }
179 }
180
181 out << endl;
182 }
183 }
184 }
185 /*}}}*/
186 // ShowNew - Show packages to newly install /*{{{*/
187 // ---------------------------------------------------------------------
188 /* */
189 void ShowNew(ostream &out,pkgDepCache &Dep)
190 {
191 /* Print out a list of packages that are going to be removed extra
192 to what the user asked */
193 pkgCache::PkgIterator I = Dep.PkgBegin();
194 string List;
195 for (;I.end() != true; I++)
196 if (Dep[I].NewInstall() == true)
197 List += string(I.Name()) + " ";
198 ShowList(out,"The following NEW packages will be installed:",List);
199 }
200 /*}}}*/
201 // ShowDel - Show packages to delete /*{{{*/
202 // ---------------------------------------------------------------------
203 /* */
204 void ShowDel(ostream &out,pkgDepCache &Dep)
205 {
206 /* Print out a list of packages that are going to be removed extra
207 to what the user asked */
208 pkgCache::PkgIterator I = Dep.PkgBegin();
209 string List;
210 for (;I.end() != true; I++)
211 if (Dep[I].Delete() == true)
212 List += string(I.Name()) + " ";
213
214 ShowList(out,"The following packages will be REMOVED:",List);
215 }
216 /*}}}*/
217 // ShowKept - Show kept packages /*{{{*/
218 // ---------------------------------------------------------------------
219 /* */
220 void ShowKept(ostream &out,pkgDepCache &Dep)
221 {
222 pkgCache::PkgIterator I = Dep.PkgBegin();
223 string List;
224 for (;I.end() != true; I++)
225 {
226 // Not interesting
227 if (Dep[I].Upgrade() == true || Dep[I].Upgradable() == false ||
228 I->CurrentVer == 0 || Dep[I].Delete() == true)
229 continue;
230
231 List += string(I.Name()) + " ";
232 }
233 ShowList(out,"The following packages have been kept back",List);
234 }
235 /*}}}*/
236 // ShowUpgraded - Show upgraded packages /*{{{*/
237 // ---------------------------------------------------------------------
238 /* */
239 void ShowUpgraded(ostream &out,pkgDepCache &Dep)
240 {
241 pkgCache::PkgIterator I = Dep.PkgBegin();
242 string List;
243 for (;I.end() != true; I++)
244 {
245 // Not interesting
246 if (Dep[I].Upgrade() == false || Dep[I].NewInstall() == true)
247 continue;
248
249 List += string(I.Name()) + " ";
250 }
251 ShowList(out,"The following packages will be upgraded",List);
252 }
253 /*}}}*/
254 // ShowHold - Show held but changed packages /*{{{*/
255 // ---------------------------------------------------------------------
256 /* */
257 bool ShowHold(ostream &out,pkgDepCache &Dep)
258 {
259 pkgCache::PkgIterator I = Dep.PkgBegin();
260 string List;
261 for (;I.end() != true; I++)
262 {
263 if (Dep[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
264 I->SelectedState == pkgCache::State::Hold)
265 List += string(I.Name()) + " ";
266 }
267
268 return ShowList(out,"The following held packages will be changed:",List);
269 }
270 /*}}}*/
271 // ShowEssential - Show an essential package warning /*{{{*/
272 // ---------------------------------------------------------------------
273 /* This prints out a warning message that is not to be ignored. It shows
274 all essential packages and their dependents that are to be removed.
275 It is insanely risky to remove the dependents of an essential package! */
276 bool ShowEssential(ostream &out,pkgDepCache &Dep)
277 {
278 pkgCache::PkgIterator I = Dep.PkgBegin();
279 string List;
280 bool *Added = new bool[Dep.HeaderP->PackageCount];
281 for (unsigned int I = 0; I != Dep.HeaderP->PackageCount; I++)
282 Added[I] = false;
283
284 for (;I.end() != true; I++)
285 {
286 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential)
287 continue;
288
289 // The essential package is being removed
290 if (Dep[I].Delete() == true)
291 {
292 if (Added[I->ID] == false)
293 {
294 Added[I->ID] = true;
295 List += string(I.Name()) + " ";
296 }
297 }
298
299 if (I->CurrentVer == 0)
300 continue;
301
302 // Print out any essential package depenendents that are to be removed
303 for (pkgDepCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
304 {
305 // Skip everything but depends
306 if (D->Type != pkgCache::Dep::PreDepends &&
307 D->Type != pkgCache::Dep::Depends)
308 continue;
309
310 pkgCache::PkgIterator P = D.SmartTargetPkg();
311 if (Dep[P].Delete() == true)
312 {
313 if (Added[P->ID] == true)
314 continue;
315 Added[P->ID] = true;
316
317 char S[300];
318 sprintf(S,"%s (due to %s) ",P.Name(),I.Name());
319 List += S;
320 }
321 }
322 }
323
324 delete [] Added;
325 if (List.empty() == false)
326 out << "WARNING: The following essential packages will be removed" << endl;
327 return ShowList(out,"This should NOT be done unless you know exactly what you are doing!",List);
328 }
329 /*}}}*/
330 // Stats - Show some statistics /*{{{*/
331 // ---------------------------------------------------------------------
332 /* */
333 void Stats(ostream &out,pkgDepCache &Dep)
334 {
335 unsigned long Upgrade = 0;
336 unsigned long Install = 0;
337 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
338 {
339 if (Dep[I].NewInstall() == true)
340 Install++;
341 else
342 if (Dep[I].Upgrade() == true)
343 Upgrade++;
344 }
345
346 out << Upgrade << " packages upgraded, " <<
347 Install << " newly installed, " <<
348 Dep.DelCount() << " to remove and " <<
349 Dep.KeepCount() << " not upgraded." << endl;
350
351 if (Dep.BadCount() != 0)
352 out << Dep.BadCount() << " packages not fully installed or removed." << endl;
353 }
354 /*}}}*/
355
356 // class CacheFile - Cover class for some dependency cache functions /*{{{*/
357 // ---------------------------------------------------------------------
358 /* */
359 class CacheFile
360 {
361 public:
362
363 FileFd *File;
364 MMap *Map;
365 pkgDepCache *Cache;
366 pkgDpkgLock Lock;
367
368 inline operator pkgDepCache &() {return *Cache;};
369 inline pkgDepCache *operator ->() {return Cache;};
370 inline pkgDepCache &operator *() {return *Cache;};
371
372 bool Open(bool AllowBroken = false);
373 CacheFile() : File(0), Map(0), Cache(0) {};
374 ~CacheFile()
375 {
376 delete Cache;
377 delete Map;
378 delete File;
379 }
380 };
381 /*}}}*/
382 // CacheFile::Open - Open the cache file /*{{{*/
383 // ---------------------------------------------------------------------
384 /* This routine generates the caches and then opens the dependency cache
385 and verifies that the system is OK. */
386 bool CacheFile::Open(bool AllowBroken)
387 {
388 if (_error->PendingError() == true)
389 return false;
390
391 // Create a progress class
392 OpTextProgress Progress(*_config);
393
394 // Read the source list
395 pkgSourceList List;
396 if (List.ReadMainList() == false)
397 return _error->Error("The list of sources could not be read.");
398
399 // Build all of the caches
400 pkgMakeStatusCache(List,Progress);
401 if (_error->PendingError() == true)
402 return _error->Error("The package lists or status file could not be parsed or opened.");
403 if (_error->empty() == false)
404 _error->Warning("You may want to run apt-get update to correct theses missing files");
405
406 Progress.Done();
407
408 // Open the cache file
409 File = new FileFd(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
410 if (_error->PendingError() == true)
411 return false;
412
413 Map = new MMap(*File,MMap::Public | MMap::ReadOnly);
414 if (_error->PendingError() == true)
415 return false;
416
417 Cache = new pkgDepCache(*Map,Progress);
418 if (_error->PendingError() == true)
419 return false;
420
421 Progress.Done();
422
423 // Check that the system is OK
424 if (Cache->DelCount() != 0 || Cache->InstCount() != 0)
425 return _error->Error("Internal Error, non-zero counts");
426
427 // Apply corrections for half-installed packages
428 if (pkgApplyStatus(*Cache) == false)
429 return false;
430
431 // Nothing is broken
432 if (Cache->BrokenCount() == 0 || AllowBroken == true)
433 return true;
434
435 // Attempt to fix broken things
436 if (_config->FindB("APT::Get::Fix-Broken",false) == true)
437 {
438 c1out << "Correcting dependencies..." << flush;
439 if (pkgFixBroken(*Cache) == false || Cache->BrokenCount() != 0)
440 {
441 c1out << " failed." << endl;
442 ShowBroken(c1out,*this);
443
444 return _error->Error("Unable to correct dependencies");
445 }
446 if (pkgMinimizeUpgrade(*Cache) == false)
447 return _error->Error("Unable to minimize the upgrade set");
448
449 c1out << " Done" << endl;
450 }
451 else
452 {
453 c1out << "You might want to run `apt-get -f install' to correct these." << endl;
454 ShowBroken(c1out,*this);
455
456 return _error->Error("Unmet dependencies. Try using -f.");
457 }
458
459 return true;
460 }
461 /*}}}*/
462
463 // InstallPackages - Actually download and install the packages /*{{{*/
464 // ---------------------------------------------------------------------
465 /* This displays the informative messages describing what is going to
466 happen and then calls the download routines */
467 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true)
468 {
469 bool Fail = false;
470
471 // Show all the various warning indicators
472 ShowDel(c1out,Cache);
473 ShowNew(c1out,Cache);
474 if (ShwKept == true)
475 ShowKept(c1out,Cache);
476 Fail |= !ShowHold(c1out,Cache);
477 if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
478 ShowUpgraded(c1out,Cache);
479 Fail |= !ShowEssential(c1out,Cache);
480 Stats(c1out,Cache);
481
482 // Sanity check
483 if (Cache->BrokenCount() != 0)
484 {
485 ShowBroken(c1out,Cache);
486 return _error->Error("Internal Error, InstallPackages was called with broken packages!");
487 }
488
489 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
490 Cache->BadCount() == 0)
491 return true;
492
493 // Run the simulator ..
494 if (_config->FindB("APT::Get::Simulate") == true)
495 {
496 pkgSimulate PM(Cache);
497 return PM.DoInstall();
498 }
499
500 // Create the text record parser
501 pkgRecords Recs(Cache);
502 if (_error->PendingError() == true)
503 return false;
504
505 // Lock the archive directory
506 if (_config->FindB("Debug::NoLocking",false) == false)
507 {
508 FileFd Lock(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
509 if (_error->PendingError() == true)
510 return _error->Error("Unable to lock the download directory");
511 }
512
513 // Create the download object
514 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
515 pkgAcquire Fetcher(&Stat);
516
517 // Read the source list
518 pkgSourceList List;
519 if (List.ReadMainList() == false)
520 return _error->Error("The list of sources could not be read.");
521
522 // Create the package manager and prepare to download
523 pkgDPkgPM PM(Cache);
524 if (PM.GetArchives(&Fetcher,&List,&Recs) == false)
525 return false;
526
527 // Display statistics
528 unsigned long FetchBytes = Fetcher.FetchNeeded();
529 unsigned long DebBytes = Fetcher.TotalNeeded();
530 if (DebBytes != Cache->DebSize())
531 {
532 c0out << DebBytes << ',' << Cache->DebSize() << endl;
533 c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
534 }
535
536 // Number of bytes
537 c2out << "Need to get ";
538 if (DebBytes != FetchBytes)
539 c2out << SizeToStr(FetchBytes) << "b/" << SizeToStr(DebBytes) << 'b';
540 else
541 c2out << SizeToStr(DebBytes) << 'b';
542
543 c1out << " of archives. After unpacking ";
544
545 // Size delta
546 if (Cache->UsrSize() >= 0)
547 c2out << SizeToStr(Cache->UsrSize()) << "b will be used." << endl;
548 else
549 c2out << SizeToStr(-1*Cache->UsrSize()) << "b will be freed." << endl;
550
551 if (_error->PendingError() == true)
552 return false;
553
554 // Fail safe check
555 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
556 {
557 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
558 return _error->Error("There are problems and -y was used without --force-yes");
559 }
560
561 // Prompt to continue
562 if (Ask == true)
563 {
564 if (_config->FindI("quiet",0) < 2 ||
565 _config->FindB("APT::Get::Assume-Yes",false) == false)
566 c2out << "Do you want to continue? [Y/n] " << flush;
567
568 if (YnPrompt() == false)
569 exit(1);
570 }
571
572 if (_config->FindB("APT::Get::Print-URIs") == true)
573 {
574 pkgAcquire::UriIterator I = Fetcher.UriBegin();
575 for (; I != Fetcher.UriEnd(); I++)
576 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
577 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
578 return true;
579 }
580
581 // Run it
582 if (Fetcher.Run() == false)
583 return false;
584
585 // Print out errors
586 bool Failed = false;
587 bool Transient = false;
588 for (pkgAcquire::Item **I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
589 {
590 if ((*I)->Status == pkgAcquire::Item::StatDone &&
591 (*I)->Complete == true)
592 continue;
593
594 if ((*I)->Status == pkgAcquire::Item::StatIdle)
595 {
596 Transient = true;
597 Failed = true;
598 continue;
599 }
600
601 cerr << "Failed to fetch " << (*I)->Describe() << endl;
602 cerr << " " << (*I)->ErrorText << endl;
603 Failed = true;
604 }
605
606 if (_config->FindB("APT::Get::Download-Only",false) == true)
607 return true;
608
609 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
610 {
611 if (Transient == true)
612 {
613 c2out << "Upgrading with disk swapping is not supported in this version." << endl;
614 c2out << "Try running multiple times with --fix-missing" << endl;
615 }
616
617 return _error->Error("Unable to fetch some archives, maybe try with --fix-missing?");
618 }
619
620 // Try to deal with missing package files
621 if (PM.FixMissing() == false)
622 {
623 cerr << "Unable to correct missing packages." << endl;
624 return _error->Error("Aborting Install.");
625 }
626
627 Cache.Lock.Close();
628 return PM.DoInstall();
629 }
630 /*}}}*/
631
632 // DoUpdate - Update the package lists /*{{{*/
633 // ---------------------------------------------------------------------
634 /* */
635 bool DoUpdate(CommandLine &)
636 {
637 // Get the source list
638 pkgSourceList List;
639 if (List.ReadMainList() == false)
640 return false;
641
642 // Lock the list directory
643 if (_config->FindB("Debug::NoLocking",false) == false)
644 {
645 FileFd Lock(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
646 if (_error->PendingError() == true)
647 return _error->Error("Unable to lock the list directory");
648 }
649
650 // Create the download object
651 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
652 pkgAcquire Fetcher(&Stat);
653
654 // Populate it with the source selection
655 pkgSourceList::const_iterator I;
656 for (I = List.begin(); I != List.end(); I++)
657 {
658 new pkgAcqIndex(&Fetcher,I);
659 if (_error->PendingError() == true)
660 return false;
661 }
662
663 // Run it
664 if (Fetcher.Run() == false)
665 return false;
666
667 // Clean out any old list files
668 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
669 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
670 return false;
671
672 // Prepare the cache.
673 CacheFile Cache;
674 if (Cache.Open() == false)
675 return false;
676
677 return true;
678 }
679 /*}}}*/
680 // DoUpgrade - Upgrade all packages /*{{{*/
681 // ---------------------------------------------------------------------
682 /* Upgrade all packages without installing new packages or erasing old
683 packages */
684 bool DoUpgrade(CommandLine &CmdL)
685 {
686 CacheFile Cache;
687 if (Cache.Open() == false)
688 return false;
689
690 // Do the upgrade
691 if (pkgAllUpgrade(Cache) == false)
692 {
693 ShowBroken(c1out,Cache);
694 return _error->Error("Internal Error, AllUpgrade broke stuff");
695 }
696
697 return InstallPackages(Cache,true);
698 }
699 /*}}}*/
700 // DoInstall - Install packages from the command line /*{{{*/
701 // ---------------------------------------------------------------------
702 /* Install named packages */
703 bool DoInstall(CommandLine &CmdL)
704 {
705 CacheFile Cache;
706 if (Cache.Open(CmdL.FileSize() != 1) == false)
707 return false;
708
709 // Enter the special broken fixing mode if the user specified arguments
710 bool BrokenFix = false;
711 if (Cache->BrokenCount() != 0)
712 BrokenFix = true;
713
714 unsigned int ExpectedInst = 0;
715 unsigned int Packages = 0;
716 pkgProblemResolver Fix(Cache);
717
718 bool DefRemove = false;
719 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
720 DefRemove = true;
721
722 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
723 {
724 // Duplicate the string
725 unsigned int Length = strlen(*I);
726 char S[300];
727 if (Length >= sizeof(S))
728 continue;
729 strcpy(S,*I);
730
731 // See if we are removing the package
732 bool Remove = DefRemove;
733 if (Cache->FindPkg(S).end() == true)
734 {
735 // Handle an optional end tag indicating what to do
736 if (S[Length - 1] == '-')
737 {
738 Remove = true;
739 S[--Length] = 0;
740 }
741 if (S[Length - 1] == '+')
742 {
743 Remove = false;
744 S[--Length] = 0;
745 }
746 }
747
748 // Locate the package
749 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
750 Packages++;
751 if (Pkg.end() == true)
752 return _error->Error("Couldn't find package %s",S);
753
754 // Handle the no-upgrade case
755 if (_config->FindB("APT::Get::no-upgrade",false) == true &&
756 Pkg->CurrentVer != 0)
757 {
758 c1out << "Skipping " << Pkg.Name() << ", it is already installed and no-upgrade is set." << endl;
759 continue;
760 }
761
762 // Check if there is something new to install
763 pkgDepCache::StateCache &State = (*Cache)[Pkg];
764 if (State.CandidateVer == 0)
765 {
766 if (Pkg->ProvidesList != 0)
767 {
768 c1out << "Package " << S << " is a virtual package provided by:" << endl;
769
770 pkgCache::PrvIterator I = Pkg.ProvidesList();
771 for (; I.end() == false; I++)
772 {
773 pkgCache::PkgIterator Pkg = I.OwnerPkg();
774
775 if ((*Cache)[Pkg].CandidateVerIter(*Cache) == I.OwnerVer())
776 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
777
778 if ((*Cache)[Pkg].InstVerIter(*Cache) == I.OwnerVer())
779 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
780 " [Installed]"<< endl;
781 }
782 c1out << "You should explicly select one to install." << endl;
783 }
784 else
785 {
786 c1out << "Package " << S << " has no available version, but exists in the database." << endl;
787 c1out << "This typically means that the package was mentioned in a dependency and " << endl;
788 c1out << "never uploaded, or that it is an obsolete package." << endl;
789
790 string List;
791 pkgCache::DepIterator Dep = Pkg.RevDependsList();
792 for (; Dep.end() == false; Dep++)
793 {
794 if (Dep->Type != pkgCache::Dep::Replaces)
795 continue;
796 List += string(Dep.ParentPkg().Name()) + " ";
797 }
798 ShowList(c1out,"However the following packages replace it:",List);
799 }
800
801 return _error->Error("Package %s has no installation candidate",S);
802 }
803
804 Fix.Protect(Pkg);
805 if (Remove == true)
806 {
807 Fix.Remove(Pkg);
808 Cache->MarkDelete(Pkg);
809 continue;
810 }
811
812 // Install it
813 Cache->MarkInstall(Pkg,false);
814 if (State.Install() == false)
815 c1out << "Sorry, " << S << " is already the newest version" << endl;
816 else
817 ExpectedInst++;
818
819 // Install it with autoinstalling enabled.
820 if (State.InstBroken() == true && BrokenFix == false)
821 Cache->MarkInstall(Pkg,true);
822 }
823
824 /* If we are in the Broken fixing mode we do not attempt to fix the
825 problems. This is if the user invoked install without -f and gave
826 packages */
827 if (BrokenFix == true && Cache->BrokenCount() != 0)
828 {
829 c1out << "You might want to run `apt-get -f install' to correct these." << endl;
830 ShowBroken(c1out,Cache);
831
832 return _error->Error("Unmet dependencies. Try using -f.");
833 }
834
835 // Call the scored problem resolver
836 Fix.InstallProtect();
837 if (Fix.Resolve(true) == false)
838 _error->Discard();
839
840 // Now we check the state of the packages,
841 if (Cache->BrokenCount() != 0)
842 {
843 c1out << "Some packages could not be installed. This may mean that you have" << endl;
844 c1out << "requested an impossible situation or if you are using the unstable" << endl;
845 c1out << "distribution that some required packages have not yet been created" << endl;
846 c1out << "or been moved out of Incoming." << endl;
847 if (Packages == 1)
848 {
849 c1out << endl;
850 c1out << "Since you only requested a single operation it is extremely likely that" << endl;
851 c1out << "the package is simply not installable and a bug report against" << endl;
852 c1out << "that package should be filed." << endl;
853 }
854
855 c1out << "The following information may help to resolve the situation:" << endl;
856 c1out << endl;
857 ShowBroken(c1out,Cache);
858 return _error->Error("Sorry, broken packages");
859 }
860
861 /* Print out a list of packages that are going to be installed extra
862 to what the user asked */
863 if (Cache->InstCount() != ExpectedInst)
864 {
865 string List;
866 pkgCache::PkgIterator I = Cache->PkgBegin();
867 for (;I.end() != true; I++)
868 {
869 if ((*Cache)[I].Install() == false)
870 continue;
871
872 const char **J;
873 for (J = CmdL.FileList + 1; *J != 0; J++)
874 if (strcmp(*J,I.Name()) == 0)
875 break;
876
877 if (*J == 0)
878 List += string(I.Name()) + " ";
879 }
880
881 ShowList(c1out,"The following extra packages will be installed:",List);
882 }
883
884 // See if we need to prompt
885 if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
886 return InstallPackages(Cache,false,false);
887
888 return InstallPackages(Cache,false);
889 }
890 /*}}}*/
891 // DoDistUpgrade - Automatic smart upgrader /*{{{*/
892 // ---------------------------------------------------------------------
893 /* Intelligent upgrader that will install and remove packages at will */
894 bool DoDistUpgrade(CommandLine &CmdL)
895 {
896 CacheFile Cache;
897 if (Cache.Open() == false)
898 return false;
899
900 c0out << "Calculating Upgrade... " << flush;
901 if (pkgDistUpgrade(*Cache) == false)
902 {
903 c0out << "Failed" << endl;
904 ShowBroken(c1out,Cache);
905 return false;
906 }
907
908 c0out << "Done" << endl;
909
910 return InstallPackages(Cache,true);
911 }
912 /*}}}*/
913 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
914 // ---------------------------------------------------------------------
915 /* Follows dselect's selections */
916 bool DoDSelectUpgrade(CommandLine &CmdL)
917 {
918 CacheFile Cache;
919 if (Cache.Open() == false)
920 return false;
921
922 // Install everything with the install flag set
923 pkgCache::PkgIterator I = Cache->PkgBegin();
924 for (;I.end() != true; I++)
925 {
926 /* Install the package only if it is a new install, the autoupgrader
927 will deal with the rest */
928 if (I->SelectedState == pkgCache::State::Install)
929 Cache->MarkInstall(I,false);
930 }
931
932 /* Now install their deps too, if we do this above then order of
933 the status file is significant for | groups */
934 for (I = Cache->PkgBegin();I.end() != true; I++)
935 {
936 /* Install the package only if it is a new install, the autoupgrader
937 will deal with the rest */
938 if (I->SelectedState == pkgCache::State::Install)
939 Cache->MarkInstall(I,true);
940 }
941
942 // Apply erasures now, they override everything else.
943 for (I = Cache->PkgBegin();I.end() != true; I++)
944 {
945 // Remove packages
946 if (I->SelectedState == pkgCache::State::DeInstall ||
947 I->SelectedState == pkgCache::State::Purge)
948 Cache->MarkDelete(I);
949 }
950
951 /* Resolve any problems that dselect created, allupgrade cannot handle
952 such things. We do so quite agressively too.. */
953 if (Cache->BrokenCount() != 0)
954 {
955 pkgProblemResolver Fix(Cache);
956
957 // Hold back held packages.
958 if (_config->FindB("APT::Ingore-Hold",false) == false)
959 {
960 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
961 {
962 if (I->SelectedState == pkgCache::State::Hold)
963 {
964 Fix.Protect(I);
965 Cache->MarkKeep(I);
966 }
967 }
968 }
969
970 if (Fix.Resolve() == false)
971 {
972 ShowBroken(c1out,Cache);
973 return _error->Error("Internal Error, problem resolver broke stuff");
974 }
975 }
976
977 // Now upgrade everything
978 if (pkgAllUpgrade(Cache) == false)
979 {
980 ShowBroken(c1out,Cache);
981 return _error->Error("Internal Error, problem resolver broke stuff");
982 }
983
984 return InstallPackages(Cache,false);
985 }
986 /*}}}*/
987 // DoClean - Remove download archives /*{{{*/
988 // ---------------------------------------------------------------------
989 /* */
990 bool DoClean(CommandLine &CmdL)
991 {
992 pkgAcquire Fetcher;
993 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
994 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
995 return true;
996 }
997 /*}}}*/
998 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
999 // ---------------------------------------------------------------------
1000 /* This is similar to clean but it only purges things that cannot be
1001 downloaded, that is old versions of cached packages. */
1002 bool DoAutoClean(CommandLine &CmdL)
1003 {
1004 CacheFile Cache;
1005 if (Cache.Open(true) == false)
1006 return false;
1007
1008 class LogCleaner : public pkgArchiveCleaner
1009 {
1010 protected:
1011 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
1012 {
1013 cout << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "b]" << endl;
1014 };
1015 } Cleaner;
1016
1017 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
1018 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
1019 }
1020 /*}}}*/
1021 // DoCheck - Perform the check operation /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* Opening automatically checks the system, this command is mostly used
1024 for debugging */
1025 bool DoCheck(CommandLine &CmdL)
1026 {
1027 CacheFile Cache;
1028 Cache.Open();
1029
1030 return true;
1031 }
1032 /*}}}*/
1033
1034 // ShowHelp - Show a help screen /*{{{*/
1035 // ---------------------------------------------------------------------
1036 /* */
1037 bool ShowHelp(CommandLine &CmdL)
1038 {
1039 cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
1040 " compiled on " << __DATE__ << " " << __TIME__ << endl;
1041 if (_config->FindB("version") == true)
1042 return 100;
1043
1044 cout << "Usage: apt-get [options] command" << endl;
1045 cout << " apt-get [options] install pkg1 [pkg2 ...]" << endl;
1046 cout << endl;
1047 cout << "apt-get is a simple command line interface for downloading and" << endl;
1048 cout << "installing packages. The most frequently used commands are update" << endl;
1049 cout << "and install." << endl;
1050 cout << endl;
1051 cout << "Commands:" << endl;
1052 cout << " update - Retrieve new lists of packages" << endl;
1053 cout << " upgrade - Perform an upgrade" << endl;
1054 cout << " install - Install new packages (pkg is libc6 not libc6.deb)" << endl;
1055 cout << " remove - Remove packages" << endl;
1056 cout << " dist-upgrade - Distribution upgrade, see apt-get(8)" << endl;
1057 cout << " dselect-upgrade - Follow dselect selections" << endl;
1058 cout << " clean - Erase downloaded archive files" << endl;
1059 cout << " autoclean - Erase old downloaded archive files" << endl;
1060 cout << " check - Verify that there are no broken dependencies" << endl;
1061 cout << endl;
1062 cout << "Options:" << endl;
1063 cout << " -h This help text." << endl;
1064 cout << " -q Loggable output - no progress indicator" << endl;
1065 cout << " -qq No output except for errors" << endl;
1066 cout << " -d Download only - do NOT install or unpack archives" << endl;
1067 cout << " -s No-act. Perform ordering simulation" << endl;
1068 cout << " -y Assume Yes to all queries and do not prompt" << endl;
1069 cout << " -f Attempt to continue if the integrity check fails" << endl;
1070 cout << " -m Attempt to continue if archives are unlocatable" << endl;
1071 cout << " -u Show a list of upgraded packages as well" << endl;
1072 cout << " -c=? Read this configuration file" << endl;
1073 cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
1074 cout << "See the apt-get(8), sources.list(5) and apt.conf(5) manual" << endl;
1075 cout << "pages for more information." << endl;
1076 return 100;
1077 }
1078 /*}}}*/
1079 // GetInitialize - Initialize things for apt-get /*{{{*/
1080 // ---------------------------------------------------------------------
1081 /* */
1082 void GetInitialize()
1083 {
1084 _config->Set("quiet",0);
1085 _config->Set("help",false);
1086 _config->Set("APT::Get::Download-Only",false);
1087 _config->Set("APT::Get::Simulate",false);
1088 _config->Set("APT::Get::Assume-Yes",false);
1089 _config->Set("APT::Get::Fix-Broken",false);
1090 _config->Set("APT::Get::Force-Yes",false);
1091 }
1092 /*}}}*/
1093 // SigWinch - Window size change signal handler /*{{{*/
1094 // ---------------------------------------------------------------------
1095 /* */
1096 void SigWinch(int)
1097 {
1098 // Riped from GNU ls
1099 #ifdef TIOCGWINSZ
1100 struct winsize ws;
1101
1102 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
1103 ScreenWidth = ws.ws_col - 1;
1104 #endif
1105 }
1106 /*}}}*/
1107
1108 int main(int argc,const char *argv[])
1109 {
1110 CommandLine::Args Args[] = {
1111 {'h',"help","help",0},
1112 {'v',"version","version",0},
1113 {'q',"quiet","quiet",CommandLine::IntLevel},
1114 {'q',"silent","quiet",CommandLine::IntLevel},
1115 {'d',"download-only","APT::Get::Download-Only",0},
1116 {'s',"simulate","APT::Get::Simulate",0},
1117 {'s',"just-print","APT::Get::Simulate",0},
1118 {'s',"recon","APT::Get::Simulate",0},
1119 {'s',"no-act","APT::Get::Simulate",0},
1120 {'y',"yes","APT::Get::Assume-Yes",0},
1121 {'y',"assume-yes","APT::Get::Assume-Yes",0},
1122 {'f',"fix-broken","APT::Get::Fix-Broken",0},
1123 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
1124 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
1125 {0,"fix-missing","APT::Get::Fix-Missing",0},
1126 {0,"ignore-hold","APT::Ingore-Hold",0},
1127 {0,"no-upgrade","APT::Get::no-upgrade",0},
1128 {0,"force-yes","APT::Get::force-yes",0},
1129 {0,"print-uris","APT::Get::Print-URIs",0},
1130 {'c',"config-file",0,CommandLine::ConfigFile},
1131 {'o',"option",0,CommandLine::ArbItem},
1132 {0,0,0,0}};
1133 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
1134 {"upgrade",&DoUpgrade},
1135 {"install",&DoInstall},
1136 {"remove",&DoInstall},
1137 {"dist-upgrade",&DoDistUpgrade},
1138 {"dselect-upgrade",&DoDSelectUpgrade},
1139 {"clean",&DoClean},
1140 {"autoclean",&DoAutoClean},
1141 {"check",&DoCheck},
1142 {"help",&ShowHelp},
1143 {0,0}};
1144
1145 // Parse the command line and initialize the package library
1146 CommandLine CmdL(Args,_config);
1147 if (pkgInitialize(*_config) == false ||
1148 CmdL.Parse(argc,argv) == false)
1149 {
1150 _error->DumpErrors();
1151 return 100;
1152 }
1153
1154 // See if the help should be shown
1155 if (_config->FindB("help") == true ||
1156 _config->FindB("version") == true ||
1157 CmdL.FileSize() == 0)
1158 return ShowHelp(CmdL);
1159
1160 // Setup the output streams
1161 c0out.rdbuf(cout.rdbuf());
1162 c1out.rdbuf(cout.rdbuf());
1163 c2out.rdbuf(cout.rdbuf());
1164 if (_config->FindI("quiet",0) > 0)
1165 c0out.rdbuf(devnull.rdbuf());
1166 if (_config->FindI("quiet",0) > 1)
1167 c1out.rdbuf(devnull.rdbuf());
1168
1169 // Setup the signals
1170 signal(SIGPIPE,SIG_IGN);
1171 signal(SIGWINCH,SigWinch);
1172 SigWinch(0);
1173
1174 // Match the operation
1175 CmdL.DispatchArg(Cmds);
1176
1177 // Print any errors or warnings found during parsing
1178 if (_error->empty() == false)
1179 {
1180 bool Errors = _error->PendingError();
1181 _error->DumpErrors();
1182 return Errors == true?100:0;
1183 }
1184
1185 return 0;
1186 }