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