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