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