]> git.saurik.com Git - apt.git/blob - cmdline/apt-get.cc
e79bb391388e09d80e1fb792913198cfa14ca4dc
[apt.git] / cmdline / apt-get.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-get.cc,v 1.108 2001/07/01 20:49:08 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/strutl.h>
36 #include <apt-pkg/clean.h>
37 #include <apt-pkg/srcrecords.h>
38 #include <apt-pkg/version.h>
39 #include <apt-pkg/cachefile.h>
40 #include <apt-pkg/sptr.h>
41 #include <apt-pkg/versionmatch.h>
42
43 #include <config.h>
44 #include <apti18n.h>
45
46 #include "acqprogress.h"
47
48 #include <fstream.h>
49 #include <termios.h>
50 #include <sys/ioctl.h>
51 #include <sys/stat.h>
52 #include <sys/statvfs.h>
53 #include <signal.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <errno.h>
57 #include <regex.h>
58 #include <sys/wait.h>
59 /*}}}*/
60
61 using namespace std;
62
63 ostream c0out(0);
64 ostream c1out(0);
65 ostream c2out(0);
66 ofstream devnull("/dev/null");
67 unsigned int ScreenWidth = 80;
68
69 // class CacheFile - Cover class for some dependency cache functions /*{{{*/
70 // ---------------------------------------------------------------------
71 /* */
72 class CacheFile : public pkgCacheFile
73 {
74 static pkgCache *SortCache;
75 static int NameComp(const void *a,const void *b);
76
77 public:
78 pkgCache::Package **List;
79
80 void Sort();
81 bool CheckDeps(bool AllowBroken = false);
82 bool Open(bool WithLock = true)
83 {
84 OpTextProgress Prog(*_config);
85 if (pkgCacheFile::Open(Prog,WithLock) == false)
86 return false;
87 Sort();
88
89 return true;
90 };
91 bool OpenForInstall()
92 {
93 if (_config->FindB("APT::Get::Print-URIs") == true)
94 Open(false);
95 else
96 Open(true);
97 }
98 CacheFile() : List(0) {};
99 };
100 /*}}}*/
101
102 // YnPrompt - Yes No Prompt. /*{{{*/
103 // ---------------------------------------------------------------------
104 /* Returns true on a Yes.*/
105 bool YnPrompt()
106 {
107 // This needs to be a capital
108 const char *Yes = _("Y");
109
110 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
111 {
112 c1out << Yes << endl;
113 return true;
114 }
115
116 char C = 0;
117 char Jnk = 0;
118 if (read(STDIN_FILENO,&C,1) != 1)
119 return false;
120 while (C != '\n' && Jnk != '\n')
121 if (read(STDIN_FILENO,&Jnk,1) != 1)
122 return false;
123
124 if (!(toupper(C) == *Yes || C == '\n' || C == '\r'))
125 return false;
126 return true;
127 }
128 /*}}}*/
129 // AnalPrompt - Annoying Yes No Prompt. /*{{{*/
130 // ---------------------------------------------------------------------
131 /* Returns true on a Yes.*/
132 bool AnalPrompt(const char *Text)
133 {
134 char Buf[1024];
135 cin.getline(Buf,sizeof(Buf));
136 if (strcmp(Buf,Text) == 0)
137 return true;
138 return false;
139 }
140 /*}}}*/
141 // ShowList - Show a list /*{{{*/
142 // ---------------------------------------------------------------------
143 /* This prints out a string of space separated words with a title and
144 a two space indent line wraped to the current screen width. */
145 bool ShowList(ostream &out,string Title,string List)
146 {
147 if (List.empty() == true)
148 return true;
149
150 // Acount for the leading space
151 int ScreenWidth = ::ScreenWidth - 3;
152
153 out << Title << endl;
154 string::size_type Start = 0;
155 while (Start < List.size())
156 {
157 string::size_type End;
158 if (Start + ScreenWidth >= List.size())
159 End = List.size();
160 else
161 End = List.rfind(' ',Start+ScreenWidth);
162
163 if (End == string::npos || End < Start)
164 End = Start + ScreenWidth;
165 out << " " << string(List,Start,End - Start) << endl;
166 Start = End + 1;
167 }
168 return false;
169 }
170 /*}}}*/
171 // ShowBroken - Debugging aide /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This prints out the names of all the packages that are broken along
174 with the name of each each broken dependency and a quite version
175 description.
176
177 The output looks like:
178 Sorry, but the following packages have unmet dependencies:
179 exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
180 Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
181 Depends: libsasl7 but it is not going to be installed
182 */
183 void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
184 {
185 out << _("Sorry, but the following packages have unmet dependencies:") << endl;
186 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
187 {
188 pkgCache::PkgIterator I(Cache,Cache.List[J]);
189
190 if (Cache[I].InstBroken() == false)
191 continue;
192
193 // Print out each package and the failed dependencies
194 out <<" " << I.Name() << ":";
195 unsigned Indent = strlen(I.Name()) + 3;
196 bool First = true;
197 if (Cache[I].InstVerIter(Cache).end() == true)
198 {
199 cout << endl;
200 continue;
201 }
202
203 for (pkgCache::DepIterator D = Cache[I].InstVerIter(Cache).DependsList(); D.end() == false;)
204 {
205 // Compute a single dependency element (glob or)
206 pkgCache::DepIterator Start;
207 pkgCache::DepIterator End;
208 D.GlobOr(Start,End);
209
210 if (Cache->IsImportantDep(End) == false ||
211 (Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
212 continue;
213
214 bool FirstOr = true;
215 while (1)
216 {
217 if (First == false)
218 for (unsigned J = 0; J != Indent; J++)
219 out << ' ';
220 First = false;
221
222 if (FirstOr == false)
223 {
224 for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
225 out << ' ';
226 }
227 else
228 out << ' ' << End.DepType() << ": ";
229 FirstOr = false;
230
231 out << Start.TargetPkg().Name();
232
233 // Show a quick summary of the version requirements
234 if (Start.TargetVer() != 0)
235 out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
236
237 /* Show a summary of the target package if possible. In the case
238 of virtual packages we show nothing */
239 pkgCache::PkgIterator Targ = Start.TargetPkg();
240 if (Targ->ProvidesList == 0)
241 {
242 out << ' ';
243 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
244 if (Now == true)
245 Ver = Targ.CurrentVer();
246
247 if (Ver.end() == false)
248 {
249 if (Now == true)
250 ioprintf(out,_("but %s is installed"),Ver.VerStr());
251 else
252 ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
253 }
254 else
255 {
256 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
257 {
258 if (Targ->ProvidesList == 0)
259 out << _("but it is not installable");
260 else
261 out << _("but it is a virtual package");
262 }
263 else
264 out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
265 }
266 }
267
268 if (Start != End)
269 out << _(" or");
270 out << endl;
271
272 if (Start == End)
273 break;
274 Start++;
275 }
276 }
277 }
278 }
279 /*}}}*/
280 // ShowNew - Show packages to newly install /*{{{*/
281 // ---------------------------------------------------------------------
282 /* */
283 void ShowNew(ostream &out,CacheFile &Cache)
284 {
285 /* Print out a list of packages that are going to be removed extra
286 to what the user asked */
287 string List;
288 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
289 {
290 pkgCache::PkgIterator I(Cache,Cache.List[J]);
291 if (Cache[I].NewInstall() == true)
292 List += string(I.Name()) + " ";
293 }
294
295 ShowList(out,_("The following NEW packages will be installed:"),List);
296 }
297 /*}}}*/
298 // ShowDel - Show packages to delete /*{{{*/
299 // ---------------------------------------------------------------------
300 /* */
301 void ShowDel(ostream &out,CacheFile &Cache)
302 {
303 /* Print out a list of packages that are going to be removed extra
304 to what the user asked */
305 string List;
306 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
307 {
308 pkgCache::PkgIterator I(Cache,Cache.List[J]);
309 if (Cache[I].Delete() == true)
310 {
311 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
312 List += string(I.Name()) + "* ";
313 else
314 List += string(I.Name()) + " ";
315 }
316 }
317
318 ShowList(out,_("The following packages will be REMOVED:"),List);
319 }
320 /*}}}*/
321 // ShowKept - Show kept packages /*{{{*/
322 // ---------------------------------------------------------------------
323 /* */
324 void ShowKept(ostream &out,CacheFile &Cache)
325 {
326 string List;
327 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
328 {
329 pkgCache::PkgIterator I(Cache,Cache.List[J]);
330
331 // Not interesting
332 if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
333 I->CurrentVer == 0 || Cache[I].Delete() == true)
334 continue;
335
336 List += string(I.Name()) + " ";
337 }
338 ShowList(out,_("The following packages have been kept back"),List);
339 }
340 /*}}}*/
341 // ShowUpgraded - Show upgraded packages /*{{{*/
342 // ---------------------------------------------------------------------
343 /* */
344 void ShowUpgraded(ostream &out,CacheFile &Cache)
345 {
346 string List;
347 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
348 {
349 pkgCache::PkgIterator I(Cache,Cache.List[J]);
350
351 // Not interesting
352 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
353 continue;
354
355 List += string(I.Name()) + " ";
356 }
357 ShowList(out,_("The following packages will be upgraded"),List);
358 }
359 /*}}}*/
360 // ShowDowngraded - Show downgraded packages /*{{{*/
361 // ---------------------------------------------------------------------
362 /* */
363 bool ShowDowngraded(ostream &out,CacheFile &Cache)
364 {
365 string List;
366 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
367 {
368 pkgCache::PkgIterator I(Cache,Cache.List[J]);
369
370 // Not interesting
371 if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
372 continue;
373
374 List += string(I.Name()) + " ";
375 }
376 return ShowList(out,_("The following packages will be DOWNGRADED"),List);
377 }
378 /*}}}*/
379 // ShowHold - Show held but changed packages /*{{{*/
380 // ---------------------------------------------------------------------
381 /* */
382 bool ShowHold(ostream &out,CacheFile &Cache)
383 {
384 string List;
385 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
386 {
387 pkgCache::PkgIterator I(Cache,Cache.List[J]);
388 if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
389 I->SelectedState == pkgCache::State::Hold)
390 List += string(I.Name()) + " ";
391 }
392
393 return ShowList(out,_("The following held packages will be changed:"),List);
394 }
395 /*}}}*/
396 // ShowEssential - Show an essential package warning /*{{{*/
397 // ---------------------------------------------------------------------
398 /* This prints out a warning message that is not to be ignored. It shows
399 all essential packages and their dependents that are to be removed.
400 It is insanely risky to remove the dependents of an essential package! */
401 bool ShowEssential(ostream &out,CacheFile &Cache)
402 {
403 string List;
404 bool *Added = new bool[Cache->Head().PackageCount];
405 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
406 Added[I] = false;
407
408 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
409 {
410 pkgCache::PkgIterator I(Cache,Cache.List[J]);
411 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
412 (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
413 continue;
414
415 // The essential package is being removed
416 if (Cache[I].Delete() == true)
417 {
418 if (Added[I->ID] == false)
419 {
420 Added[I->ID] = true;
421 List += string(I.Name()) + " ";
422 }
423 }
424
425 if (I->CurrentVer == 0)
426 continue;
427
428 // Print out any essential package depenendents that are to be removed
429 for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
430 {
431 // Skip everything but depends
432 if (D->Type != pkgCache::Dep::PreDepends &&
433 D->Type != pkgCache::Dep::Depends)
434 continue;
435
436 pkgCache::PkgIterator P = D.SmartTargetPkg();
437 if (Cache[P].Delete() == true)
438 {
439 if (Added[P->ID] == true)
440 continue;
441 Added[P->ID] = true;
442
443 char S[300];
444 snprintf(S,sizeof(S),_("%s (due to %s) "),P.Name(),I.Name());
445 List += S;
446 }
447 }
448 }
449
450 delete [] Added;
451 return ShowList(out,_("WARNING: The following essential packages will be removed\n"
452 "This should NOT be done unless you know exactly what you are doing!"),List);
453 }
454 /*}}}*/
455 // Stats - Show some statistics /*{{{*/
456 // ---------------------------------------------------------------------
457 /* */
458 void Stats(ostream &out,pkgDepCache &Dep)
459 {
460 unsigned long Upgrade = 0;
461 unsigned long Downgrade = 0;
462 unsigned long Install = 0;
463 unsigned long ReInstall = 0;
464 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
465 {
466 if (Dep[I].NewInstall() == true)
467 Install++;
468 else
469 {
470 if (Dep[I].Upgrade() == true)
471 Upgrade++;
472 else
473 if (Dep[I].Downgrade() == true)
474 Downgrade++;
475 }
476
477 if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
478 ReInstall++;
479 }
480
481 ioprintf(out,_("%lu packages upgraded, %lu newly installed, "),
482 Upgrade,Install);
483
484 if (ReInstall != 0)
485 ioprintf(out,_("%lu reinstalled, "),ReInstall);
486 if (Downgrade != 0)
487 ioprintf(out,_("%lu downgraded, "),Downgrade);
488
489 ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
490 Dep.DelCount(),Dep.KeepCount());
491
492 if (Dep.BadCount() != 0)
493 ioprintf(out,_("%lu packages not fully installed or removed.\n"),
494 Dep.BadCount());
495 }
496 /*}}}*/
497
498 // CacheFile::NameComp - QSort compare by name /*{{{*/
499 // ---------------------------------------------------------------------
500 /* */
501 pkgCache *CacheFile::SortCache = 0;
502 int CacheFile::NameComp(const void *a,const void *b)
503 {
504 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
505 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
506
507 const pkgCache::Package &A = **(pkgCache::Package **)a;
508 const pkgCache::Package &B = **(pkgCache::Package **)b;
509
510 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
511 }
512 /*}}}*/
513 // CacheFile::Sort - Sort by name /*{{{*/
514 // ---------------------------------------------------------------------
515 /* */
516 void CacheFile::Sort()
517 {
518 delete [] List;
519 List = new pkgCache::Package *[Cache->Head().PackageCount];
520 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
521 pkgCache::PkgIterator I = Cache->PkgBegin();
522 for (;I.end() != true; I++)
523 List[I->ID] = I;
524
525 SortCache = *this;
526 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
527 }
528 /*}}}*/
529 // CacheFile::CheckDeps - Open the cache file /*{{{*/
530 // ---------------------------------------------------------------------
531 /* This routine generates the caches and then opens the dependency cache
532 and verifies that the system is OK. */
533 bool CacheFile::CheckDeps(bool AllowBroken)
534 {
535 if (_error->PendingError() == true)
536 return false;
537
538 // Check that the system is OK
539 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
540 return _error->Error("Internal Error, non-zero counts");
541
542 // Apply corrections for half-installed packages
543 if (pkgApplyStatus(*DCache) == false)
544 return false;
545
546 // Nothing is broken
547 if (DCache->BrokenCount() == 0 || AllowBroken == true)
548 return true;
549
550 // Attempt to fix broken things
551 if (_config->FindB("APT::Get::Fix-Broken",false) == true)
552 {
553 c1out << _("Correcting dependencies...") << flush;
554 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
555 {
556 c1out << _(" failed.") << endl;
557 ShowBroken(c1out,*this,true);
558
559 return _error->Error(_("Unable to correct dependencies"));
560 }
561 if (pkgMinimizeUpgrade(*DCache) == false)
562 return _error->Error(_("Unable to minimize the upgrade set"));
563
564 c1out << _(" Done") << endl;
565 }
566 else
567 {
568 c1out << _("You might want to run `apt-get -f install' to correct these.") << endl;
569 ShowBroken(c1out,*this,true);
570
571 return _error->Error(_("Unmet dependencies. Try using -f."));
572 }
573
574 return true;
575 }
576 /*}}}*/
577
578 // InstallPackages - Actually download and install the packages /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This displays the informative messages describing what is going to
581 happen and then calls the download routines */
582 bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
583 bool Saftey = true)
584 {
585 if (_config->FindB("APT::Get::Purge",false) == true)
586 {
587 pkgCache::PkgIterator I = Cache->PkgBegin();
588 for (; I.end() == false; I++)
589 {
590 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
591 Cache->MarkDelete(I,true);
592 }
593 }
594
595 bool Fail = false;
596 bool Essential = false;
597
598 // Show all the various warning indicators
599 ShowDel(c1out,Cache);
600 ShowNew(c1out,Cache);
601 if (ShwKept == true)
602 ShowKept(c1out,Cache);
603 Fail |= !ShowHold(c1out,Cache);
604 if (_config->FindB("APT::Get::Show-Upgraded",false) == true)
605 ShowUpgraded(c1out,Cache);
606 Fail |= !ShowDowngraded(c1out,Cache);
607 Essential = !ShowEssential(c1out,Cache);
608 Fail |= Essential;
609 Stats(c1out,Cache);
610
611 // Sanity check
612 if (Cache->BrokenCount() != 0)
613 {
614 ShowBroken(c1out,Cache,false);
615 return _error->Error("Internal Error, InstallPackages was called with broken packages!");
616 }
617
618 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
619 Cache->BadCount() == 0)
620 return true;
621
622 // No remove flag
623 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
624 return _error->Error(_("Packages need to be removed but Remove is disabled."));
625
626 // Run the simulator ..
627 if (_config->FindB("APT::Get::Simulate") == true)
628 {
629 pkgSimulate PM(Cache);
630 pkgPackageManager::OrderResult Res = PM.DoInstall();
631 if (Res == pkgPackageManager::Failed)
632 return false;
633 if (Res != pkgPackageManager::Completed)
634 return _error->Error("Internal Error, Ordering didn't finish");
635 return true;
636 }
637
638 // Create the text record parser
639 pkgRecords Recs(Cache);
640 if (_error->PendingError() == true)
641 return false;
642
643 // Lock the archive directory
644 FileFd Lock;
645 if (_config->FindB("Debug::NoLocking",false) == false &&
646 _config->FindB("APT::Get::Print-URIs") == false)
647 {
648 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
649 if (_error->PendingError() == true)
650 return _error->Error(_("Unable to lock the download directory"));
651 }
652
653 // Create the download object
654 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
655 pkgAcquire Fetcher(&Stat);
656
657 // Read the source list
658 pkgSourceList List;
659 if (List.ReadMainList() == false)
660 return _error->Error(_("The list of sources could not be read."));
661
662 // Create the package manager and prepare to download
663 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
664 if (PM->GetArchives(&Fetcher,&List,&Recs) == false ||
665 _error->PendingError() == true)
666 return false;
667
668 // Display statistics
669 double FetchBytes = Fetcher.FetchNeeded();
670 double FetchPBytes = Fetcher.PartialPresent();
671 double DebBytes = Fetcher.TotalNeeded();
672 if (DebBytes != Cache->DebSize())
673 {
674 c0out << DebBytes << ',' << Cache->DebSize() << endl;
675 c0out << "How odd.. The sizes didn't match, email apt@packages.debian.org" << endl;
676 }
677
678 // Number of bytes
679 if (DebBytes != FetchBytes)
680 ioprintf(c1out,_("Need to get %sB/%sB of archives. "),
681 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
682 else
683 ioprintf(c1out,_("Need to get %sB of archives. "),
684 SizeToStr(DebBytes).c_str());
685
686 // Size delta
687 if (Cache->UsrSize() >= 0)
688 ioprintf(c1out,_("After unpacking %sB will be used.\n"),
689 SizeToStr(Cache->UsrSize()).c_str());
690 else
691 ioprintf(c1out,_("After unpacking %sB will be freed.\n"),
692 SizeToStr(-1*Cache->UsrSize()).c_str());
693
694 if (_error->PendingError() == true)
695 return false;
696
697 /* Check for enough free space, but only if we are actually going to
698 download */
699 if (_config->FindB("APT::Get::Print-URIs") == false)
700 {
701 struct statvfs Buf;
702 string OutputDir = _config->FindDir("Dir::Cache::Archives");
703 if (statvfs(OutputDir.c_str(),&Buf) != 0)
704 return _error->Errno("statvfs","Couldn't determine free space in %s",
705 OutputDir.c_str());
706 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
707 return _error->Error(_("Sorry, you don't have enough free space in %s to hold all the .debs."),
708 OutputDir.c_str());
709 }
710
711 // Fail safe check
712 if (_config->FindI("quiet",0) >= 2 ||
713 _config->FindB("APT::Get::Assume-Yes",false) == true)
714 {
715 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
716 return _error->Error(_("There are problems and -y was used without --force-yes"));
717 }
718
719 if (Essential == true && Saftey == true)
720 {
721 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
722 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
723
724 const char *Prompt = _("Yes, do as I say!");
725 ioprintf(c2out,
726 _("You are about to do something potentially harmful\n"
727 "To continue type in the phrase '%s'\n"
728 " ?] "),Prompt);
729 c2out << flush;
730 if (AnalPrompt(Prompt) == false)
731 {
732 c2out << _("Abort.") << endl;
733 exit(1);
734 }
735 }
736 else
737 {
738 // Prompt to continue
739 if (Ask == true || Fail == true)
740 {
741 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
742 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
743
744 if (_config->FindI("quiet",0) < 2 &&
745 _config->FindB("APT::Get::Assume-Yes",false) == false)
746 {
747 c2out << _("Do you want to continue? [Y/n] ") << flush;
748
749 if (YnPrompt() == false)
750 {
751 c2out << _("Abort.") << endl;
752 exit(1);
753 }
754 }
755 }
756 }
757
758 // Just print out the uris an exit if the --print-uris flag was used
759 if (_config->FindB("APT::Get::Print-URIs") == true)
760 {
761 pkgAcquire::UriIterator I = Fetcher.UriBegin();
762 for (; I != Fetcher.UriEnd(); I++)
763 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
764 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
765 return true;
766 }
767
768 /* Unlock the dpkg lock if we are not going to be doing an install
769 after. */
770 if (_config->FindB("APT::Get::Download-Only",false) == true)
771 _system->UnLock();
772
773 // Run it
774 while (1)
775 {
776 bool Transient = false;
777 if (_config->FindB("APT::Get::Download",true) == false)
778 {
779 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
780 {
781 if ((*I)->Local == true)
782 {
783 I++;
784 continue;
785 }
786
787 // Close the item and check if it was found in cache
788 (*I)->Finished();
789 if ((*I)->Complete == false)
790 Transient = true;
791
792 // Clear it out of the fetch list
793 delete *I;
794 I = Fetcher.ItemsBegin();
795 }
796 }
797
798 if (Fetcher.Run() == pkgAcquire::Failed)
799 return false;
800
801 // Print out errors
802 bool Failed = false;
803 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
804 {
805 if ((*I)->Status == pkgAcquire::Item::StatDone &&
806 (*I)->Complete == true)
807 continue;
808
809 if ((*I)->Status == pkgAcquire::Item::StatIdle)
810 {
811 Transient = true;
812 // Failed = true;
813 continue;
814 }
815
816 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
817 (*I)->ErrorText.c_str());
818 Failed = true;
819 }
820
821 /* If we are in no download mode and missing files and there were
822 'failures' then the user must specify -m. Furthermore, there
823 is no such thing as a transient error in no-download mode! */
824 if (Transient == true &&
825 _config->FindB("APT::Get::Download",true) == false)
826 {
827 Transient = false;
828 Failed = true;
829 }
830
831 if (_config->FindB("APT::Get::Download-Only",false) == true)
832 {
833 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
834 return _error->Error(_("Some files failed to download"));
835 c1out << _("Download complete and in download only mode") << endl;
836 return true;
837 }
838
839 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
840 {
841 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
842 }
843
844 if (Transient == true && Failed == true)
845 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
846
847 // Try to deal with missing package files
848 if (Failed == true && PM->FixMissing() == false)
849 {
850 cerr << _("Unable to correct missing packages.") << endl;
851 return _error->Error(_("Aborting Install."));
852 }
853
854 _system->UnLock();
855 pkgPackageManager::OrderResult Res = PM->DoInstall();
856 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
857 return false;
858 if (Res == pkgPackageManager::Completed)
859 return true;
860
861 // Reload the fetcher object and loop again for media swapping
862 Fetcher.Shutdown();
863 if (PM->GetArchives(&Fetcher,&List,&Recs) == false)
864 return false;
865
866 _system->Lock();
867 }
868 }
869 /*}}}*/
870 // TryToInstall - Try to install a single package /*{{{*/
871 // ---------------------------------------------------------------------
872 /* This used to be inlined in DoInstall, but with the advent of regex package
873 name matching it was split out.. */
874 bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
875 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
876 unsigned int &ExpectedInst,bool AllowFail = true)
877 {
878 /* This is a pure virtual package and there is a single available
879 provides */
880 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
881 Pkg.ProvidesList()->NextProvides == 0)
882 {
883 pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
884 ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
885 Tmp.Name(),Pkg.Name());
886 Pkg = Tmp;
887 }
888
889 // Handle the no-upgrade case
890 if (_config->FindB("APT::Get::upgrade",true) == false &&
891 Pkg->CurrentVer != 0)
892 {
893 if (AllowFail == true)
894 ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
895 Pkg.Name());
896 return true;
897 }
898
899 // Check if there is something at all to install
900 pkgDepCache::StateCache &State = Cache[Pkg];
901 if (Remove == true && Pkg->CurrentVer == 0)
902 {
903 /* We want to continue searching for regex hits, so we return false here
904 otherwise this is not really an error. */
905 if (AllowFail == false)
906 return false;
907 ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
908 return true;
909 }
910
911 if (State.CandidateVer == 0 && Remove == false)
912 {
913 if (AllowFail == false)
914 return false;
915
916 if (Pkg->ProvidesList != 0)
917 {
918 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
919 Pkg.Name());
920
921 pkgCache::PrvIterator I = Pkg.ProvidesList();
922 for (; I.end() == false; I++)
923 {
924 pkgCache::PkgIterator Pkg = I.OwnerPkg();
925
926 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
927 {
928 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
929 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
930 _(" [Installed]") << endl;
931 else
932 c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
933 }
934 }
935 c1out << _("You should explicitly select one to install.") << endl;
936 }
937 else
938 {
939 ioprintf(c1out,
940 _("Package %s has no available version, but exists in the database.\n"
941 "This typically means that the package was mentioned in a dependency and\n"
942 "never uploaded, has been obsoleted or is not available with the contents\n"
943 "of sources.list\n"),Pkg.Name());
944
945 string List;
946 SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
947 memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
948 pkgCache::DepIterator Dep = Pkg.RevDependsList();
949 for (; Dep.end() == false; Dep++)
950 {
951 if (Dep->Type != pkgCache::Dep::Replaces)
952 continue;
953 if (Seen[Dep.ParentPkg()->ID] == true)
954 continue;
955 Seen[Dep.ParentPkg()->ID] = true;
956 List += string(Dep.ParentPkg().Name()) + " ";
957 }
958 ShowList(c1out,_("However the following packages replace it:"),List);
959 }
960
961 _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
962 return false;
963 }
964
965 Fix.Clear(Pkg);
966 Fix.Protect(Pkg);
967 if (Remove == true)
968 {
969 Fix.Remove(Pkg);
970 Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
971 return true;
972 }
973
974 // Install it
975 Cache.MarkInstall(Pkg,false);
976 if (State.Install() == false)
977 {
978 if (_config->FindB("APT::Get::ReInstall",false) == true)
979 {
980 if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
981 ioprintf(c1out,_("Sorry, re-installation of %s is not possible, it cannot be downloaded.\n"),
982 Pkg.Name());
983 else
984 Cache.SetReInstall(Pkg,true);
985 }
986 else
987 {
988 if (AllowFail == true)
989 ioprintf(c1out,_("Sorry, %s is already the newest version.\n"),
990 Pkg.Name());
991 }
992 }
993 else
994 ExpectedInst++;
995
996 // Install it with autoinstalling enabled.
997 if (State.InstBroken() == true && BrokenFix == false)
998 Cache.MarkInstall(Pkg,true);
999 return true;
1000 }
1001 /*}}}*/
1002 // TryToChangeVer - Try to change a candidate version /*{{{*/
1003 // ---------------------------------------------------------------------
1004 /* */
1005 bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
1006 const char *VerTag,bool IsRel)
1007 {
1008 pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release:pkgVersionMatch::Version));
1009
1010 pkgCache::VerIterator Ver = Match.Find(Pkg);
1011
1012 if (Ver.end() == true)
1013 {
1014 if (IsRel == true)
1015 return _error->Error(_("Release '%s' for '%s' was not found"),
1016 VerTag,Pkg.Name());
1017 return _error->Error(_("Version '%s' for '%s' was not found"),
1018 VerTag,Pkg.Name());
1019 }
1020
1021 if (strcmp(VerTag,Ver.VerStr()) != 0)
1022 {
1023 ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
1024 Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
1025 }
1026
1027 Cache.SetCandidateVersion(Ver);
1028 return true;
1029 }
1030 /*}}}*/
1031 // FindSrc - Find a source record /*{{{*/
1032 // ---------------------------------------------------------------------
1033 /* */
1034 pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
1035 pkgSrcRecords &SrcRecs,string &Src,
1036 pkgDepCache &Cache)
1037 {
1038 // We want to pull the version off the package specification..
1039 string VerTag;
1040 string TmpSrc = Name;
1041 string::size_type Slash = TmpSrc.rfind('=');
1042 if (Slash != string::npos)
1043 {
1044 VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
1045 TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
1046 }
1047
1048 /* Lookup the version of the package we would install if we were to
1049 install a version and determine the source package name, then look
1050 in the archive for a source package of the same name. In theory
1051 we could stash the version string as well and match that too but
1052 today there aren't multi source versions in the archive. */
1053 if (_config->FindB("APT::Get::Only-Source") == false &&
1054 VerTag.empty() == true)
1055 {
1056 pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
1057 if (Pkg.end() == false)
1058 {
1059 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
1060 if (Ver.end() == false)
1061 {
1062 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1063 Src = Parse.SourcePkg();
1064 }
1065 }
1066 }
1067
1068 // No source package name..
1069 if (Src.empty() == true)
1070 Src = TmpSrc;
1071
1072 // The best hit
1073 pkgSrcRecords::Parser *Last = 0;
1074 unsigned long Offset = 0;
1075 string Version;
1076 bool IsMatch = false;
1077
1078 // If we are matching by version then we need exact matches to be happy
1079 if (VerTag.empty() == false)
1080 IsMatch = true;
1081
1082 /* Iterate over all of the hits, which includes the resulting
1083 binary packages in the search */
1084 pkgSrcRecords::Parser *Parse;
1085 SrcRecs.Restart();
1086 while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
1087 {
1088 string Ver = Parse->Version();
1089
1090 // Skip name mismatches
1091 if (IsMatch == true && Parse->Package() != Src)
1092 continue;
1093
1094 if (VerTag.empty() == false)
1095 {
1096 /* Don't want to fall through because we are doing exact version
1097 matching. */
1098 if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
1099 continue;
1100
1101 Last = Parse;
1102 Offset = Parse->Offset();
1103 break;
1104 }
1105
1106 // Newer version or an exact match
1107 if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
1108 (Parse->Package() == Src && IsMatch == false))
1109 {
1110 IsMatch = Parse->Package() == Src;
1111 Last = Parse;
1112 Offset = Parse->Offset();
1113 Version = Ver;
1114 }
1115 }
1116
1117 if (Last == 0)
1118 return 0;
1119
1120 if (Last->Jump(Offset) == false)
1121 return 0;
1122
1123 return Last;
1124 }
1125 /*}}}*/
1126
1127 // DoUpdate - Update the package lists /*{{{*/
1128 // ---------------------------------------------------------------------
1129 /* */
1130 bool DoUpdate(CommandLine &CmdL)
1131 {
1132 if (CmdL.FileSize() != 1)
1133 return _error->Error(_("The update command takes no arguments"));
1134
1135 // Get the source list
1136 pkgSourceList List;
1137 if (List.ReadMainList() == false)
1138 return false;
1139
1140 // Lock the list directory
1141 FileFd Lock;
1142 if (_config->FindB("Debug::NoLocking",false) == false)
1143 {
1144 Lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
1145 if (_error->PendingError() == true)
1146 return _error->Error(_("Unable to lock the list directory"));
1147 }
1148
1149 // Create the download object
1150 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1151 pkgAcquire Fetcher(&Stat);
1152
1153 // Populate it with the source selection
1154 if (List.GetIndexes(&Fetcher) == false)
1155 return false;
1156
1157 // Run it
1158 if (Fetcher.Run() == pkgAcquire::Failed)
1159 return false;
1160
1161 bool Failed = false;
1162 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1163 {
1164 if ((*I)->Status == pkgAcquire::Item::StatDone)
1165 continue;
1166
1167 (*I)->Finished();
1168
1169 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1170 (*I)->ErrorText.c_str());
1171 Failed = true;
1172 }
1173
1174 // Clean out any old list files
1175 if (_config->FindB("APT::Get::List-Cleanup",true) == true)
1176 {
1177 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
1178 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
1179 return false;
1180 }
1181
1182 // Prepare the cache.
1183 CacheFile Cache;
1184 if (Cache.Open() == false)
1185 return false;
1186
1187 if (Failed == true)
1188 return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
1189
1190 return true;
1191 }
1192 /*}}}*/
1193 // DoUpgrade - Upgrade all packages /*{{{*/
1194 // ---------------------------------------------------------------------
1195 /* Upgrade all packages without installing new packages or erasing old
1196 packages */
1197 bool DoUpgrade(CommandLine &CmdL)
1198 {
1199 CacheFile Cache;
1200 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1201 return false;
1202
1203 // Do the upgrade
1204 if (pkgAllUpgrade(Cache) == false)
1205 {
1206 ShowBroken(c1out,Cache,false);
1207 return _error->Error(_("Internal Error, AllUpgrade broke stuff"));
1208 }
1209
1210 return InstallPackages(Cache,true);
1211 }
1212 /*}}}*/
1213 // DoInstall - Install packages from the command line /*{{{*/
1214 // ---------------------------------------------------------------------
1215 /* Install named packages */
1216 bool DoInstall(CommandLine &CmdL)
1217 {
1218 CacheFile Cache;
1219 if (Cache.OpenForInstall() == false ||
1220 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
1221 return false;
1222
1223 // Enter the special broken fixing mode if the user specified arguments
1224 bool BrokenFix = false;
1225 if (Cache->BrokenCount() != 0)
1226 BrokenFix = true;
1227
1228 unsigned int ExpectedInst = 0;
1229 unsigned int Packages = 0;
1230 pkgProblemResolver Fix(Cache);
1231
1232 bool DefRemove = false;
1233 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
1234 DefRemove = true;
1235
1236 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
1237 {
1238 // Duplicate the string
1239 unsigned int Length = strlen(*I);
1240 char S[300];
1241 if (Length >= sizeof(S))
1242 continue;
1243 strcpy(S,*I);
1244
1245 // See if we are removing and special indicators..
1246 bool Remove = DefRemove;
1247 char *VerTag = 0;
1248 bool VerIsRel = false;
1249 while (Cache->FindPkg(S).end() == true)
1250 {
1251 // Handle an optional end tag indicating what to do
1252 if (S[Length - 1] == '-')
1253 {
1254 Remove = true;
1255 S[--Length] = 0;
1256 continue;
1257 }
1258
1259 if (S[Length - 1] == '+')
1260 {
1261 Remove = false;
1262 S[--Length] = 0;
1263 continue;
1264 }
1265
1266 char *Slash = strchr(S,'=');
1267 if (Slash != 0)
1268 {
1269 VerIsRel = false;
1270 *Slash = 0;
1271 VerTag = Slash + 1;
1272 }
1273
1274 Slash = strchr(S,'/');
1275 if (Slash != 0)
1276 {
1277 VerIsRel = true;
1278 *Slash = 0;
1279 VerTag = Slash + 1;
1280 }
1281
1282 break;
1283 }
1284
1285 // Locate the package
1286 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
1287 Packages++;
1288 if (Pkg.end() == true)
1289 {
1290 // Check if the name is a regex
1291 const char *I;
1292 for (I = S; *I != 0; I++)
1293 if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
1294 break;
1295 if (*I == 0)
1296 return _error->Error(_("Couldn't find package %s"),S);
1297
1298 // Regexs must always be confirmed
1299 ExpectedInst += 1000;
1300
1301 // Compile the regex pattern
1302 regex_t Pattern;
1303 int Res;
1304 if ((Res = regcomp(&Pattern,S,REG_EXTENDED | REG_ICASE |
1305 REG_NOSUB)) != 0)
1306 {
1307 char Error[300];
1308 regerror(Res,&Pattern,Error,sizeof(Error));
1309 return _error->Error(_("Regex compilation error - %s"),Error);
1310 }
1311
1312 // Run over the matches
1313 bool Hit = false;
1314 for (Pkg = Cache->PkgBegin(); Pkg.end() == false; Pkg++)
1315 {
1316 if (regexec(&Pattern,Pkg.Name(),0,0,0) != 0)
1317 continue;
1318
1319 if (VerTag != 0)
1320 if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
1321 return false;
1322
1323 Hit |= TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,
1324 ExpectedInst,false);
1325 }
1326 regfree(&Pattern);
1327
1328 if (Hit == false)
1329 return _error->Error(_("Couldn't find package %s"),S);
1330 }
1331 else
1332 {
1333 if (VerTag != 0)
1334 if (TryToChangeVer(Pkg,Cache,VerTag,VerIsRel) == false)
1335 return false;
1336 if (TryToInstall(Pkg,Cache,Fix,Remove,BrokenFix,ExpectedInst) == false)
1337 return false;
1338 }
1339 }
1340
1341 /* If we are in the Broken fixing mode we do not attempt to fix the
1342 problems. This is if the user invoked install without -f and gave
1343 packages */
1344 if (BrokenFix == true && Cache->BrokenCount() != 0)
1345 {
1346 c1out << _("You might want to run `apt-get -f install' to correct these:") << endl;
1347 ShowBroken(c1out,Cache,false);
1348
1349 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
1350 }
1351
1352 // Call the scored problem resolver
1353 Fix.InstallProtect();
1354 if (Fix.Resolve(true) == false)
1355 _error->Discard();
1356
1357 // Now we check the state of the packages,
1358 if (Cache->BrokenCount() != 0)
1359 {
1360 c1out <<
1361 _("Some packages could not be installed. This may mean that you have\n"
1362 "requested an impossible situation or if you are using the unstable\n"
1363 "distribution that some required packages have not yet been created\n"
1364 "or been moved out of Incoming.") << endl;
1365 if (Packages == 1)
1366 {
1367 c1out << endl;
1368 c1out <<
1369 _("Since you only requested a single operation it is extremely likely that\n"
1370 "the package is simply not installable and a bug report against\n"
1371 "that package should be filed.") << endl;
1372 }
1373
1374 c1out << _("The following information may help to resolve the situation:") << endl;
1375 c1out << endl;
1376 ShowBroken(c1out,Cache,false);
1377 return _error->Error(_("Sorry, broken packages"));
1378 }
1379
1380 /* Print out a list of packages that are going to be installed extra
1381 to what the user asked */
1382 if (Cache->InstCount() != ExpectedInst)
1383 {
1384 string List;
1385 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1386 {
1387 pkgCache::PkgIterator I(Cache,Cache.List[J]);
1388 if ((*Cache)[I].Install() == false)
1389 continue;
1390
1391 const char **J;
1392 for (J = CmdL.FileList + 1; *J != 0; J++)
1393 if (strcmp(*J,I.Name()) == 0)
1394 break;
1395
1396 if (*J == 0)
1397 List += string(I.Name()) + " ";
1398 }
1399
1400 ShowList(c1out,_("The following extra packages will be installed:"),List);
1401 }
1402
1403 // See if we need to prompt
1404 if (Cache->InstCount() == ExpectedInst && Cache->DelCount() == 0)
1405 return InstallPackages(Cache,false,false);
1406
1407 return InstallPackages(Cache,false);
1408 }
1409 /*}}}*/
1410 // DoDistUpgrade - Automatic smart upgrader /*{{{*/
1411 // ---------------------------------------------------------------------
1412 /* Intelligent upgrader that will install and remove packages at will */
1413 bool DoDistUpgrade(CommandLine &CmdL)
1414 {
1415 CacheFile Cache;
1416 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1417 return false;
1418
1419 c0out << _("Calculating Upgrade... ") << flush;
1420 if (pkgDistUpgrade(*Cache) == false)
1421 {
1422 c0out << _("Failed") << endl;
1423 ShowBroken(c1out,Cache,false);
1424 return false;
1425 }
1426
1427 c0out << _("Done") << endl;
1428
1429 return InstallPackages(Cache,true);
1430 }
1431 /*}}}*/
1432 // DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
1433 // ---------------------------------------------------------------------
1434 /* Follows dselect's selections */
1435 bool DoDSelectUpgrade(CommandLine &CmdL)
1436 {
1437 CacheFile Cache;
1438 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1439 return false;
1440
1441 // Install everything with the install flag set
1442 pkgCache::PkgIterator I = Cache->PkgBegin();
1443 for (;I.end() != true; I++)
1444 {
1445 /* Install the package only if it is a new install, the autoupgrader
1446 will deal with the rest */
1447 if (I->SelectedState == pkgCache::State::Install)
1448 Cache->MarkInstall(I,false);
1449 }
1450
1451 /* Now install their deps too, if we do this above then order of
1452 the status file is significant for | groups */
1453 for (I = Cache->PkgBegin();I.end() != true; I++)
1454 {
1455 /* Install the package only if it is a new install, the autoupgrader
1456 will deal with the rest */
1457 if (I->SelectedState == pkgCache::State::Install)
1458 Cache->MarkInstall(I,true);
1459 }
1460
1461 // Apply erasures now, they override everything else.
1462 for (I = Cache->PkgBegin();I.end() != true; I++)
1463 {
1464 // Remove packages
1465 if (I->SelectedState == pkgCache::State::DeInstall ||
1466 I->SelectedState == pkgCache::State::Purge)
1467 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
1468 }
1469
1470 /* Resolve any problems that dselect created, allupgrade cannot handle
1471 such things. We do so quite agressively too.. */
1472 if (Cache->BrokenCount() != 0)
1473 {
1474 pkgProblemResolver Fix(Cache);
1475
1476 // Hold back held packages.
1477 if (_config->FindB("APT::Ignore-Hold",false) == false)
1478 {
1479 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
1480 {
1481 if (I->SelectedState == pkgCache::State::Hold)
1482 {
1483 Fix.Protect(I);
1484 Cache->MarkKeep(I);
1485 }
1486 }
1487 }
1488
1489 if (Fix.Resolve() == false)
1490 {
1491 ShowBroken(c1out,Cache,false);
1492 return _error->Error("Internal Error, problem resolver broke stuff");
1493 }
1494 }
1495
1496 // Now upgrade everything
1497 if (pkgAllUpgrade(Cache) == false)
1498 {
1499 ShowBroken(c1out,Cache,false);
1500 return _error->Error("Internal Error, problem resolver broke stuff");
1501 }
1502
1503 return InstallPackages(Cache,false);
1504 }
1505 /*}}}*/
1506 // DoClean - Remove download archives /*{{{*/
1507 // ---------------------------------------------------------------------
1508 /* */
1509 bool DoClean(CommandLine &CmdL)
1510 {
1511 if (_config->FindB("APT::Get::Simulate") == true)
1512 {
1513 cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
1514 _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
1515 return true;
1516 }
1517
1518 // Lock the archive directory
1519 FileFd Lock;
1520 if (_config->FindB("Debug::NoLocking",false) == false)
1521 {
1522 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1523 if (_error->PendingError() == true)
1524 return _error->Error(_("Unable to lock the download directory"));
1525 }
1526
1527 pkgAcquire Fetcher;
1528 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
1529 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
1530 return true;
1531 }
1532 /*}}}*/
1533 // DoAutoClean - Smartly remove downloaded archives /*{{{*/
1534 // ---------------------------------------------------------------------
1535 /* This is similar to clean but it only purges things that cannot be
1536 downloaded, that is old versions of cached packages. */
1537 class LogCleaner : public pkgArchiveCleaner
1538 {
1539 protected:
1540 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
1541 {
1542 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
1543
1544 if (_config->FindB("APT::Get::Simulate") == false)
1545 unlink(File);
1546 };
1547 };
1548
1549 bool DoAutoClean(CommandLine &CmdL)
1550 {
1551 // Lock the archive directory
1552 FileFd Lock;
1553 if (_config->FindB("Debug::NoLocking",false) == false)
1554 {
1555 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1556 if (_error->PendingError() == true)
1557 return _error->Error(_("Unable to lock the download directory"));
1558 }
1559
1560 CacheFile Cache;
1561 if (Cache.Open() == false)
1562 return false;
1563
1564 LogCleaner Cleaner;
1565
1566 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
1567 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
1568 }
1569 /*}}}*/
1570 // DoCheck - Perform the check operation /*{{{*/
1571 // ---------------------------------------------------------------------
1572 /* Opening automatically checks the system, this command is mostly used
1573 for debugging */
1574 bool DoCheck(CommandLine &CmdL)
1575 {
1576 CacheFile Cache;
1577 Cache.Open();
1578 Cache.CheckDeps();
1579
1580 return true;
1581 }
1582 /*}}}*/
1583 // DoSource - Fetch a source archive /*{{{*/
1584 // ---------------------------------------------------------------------
1585 /* Fetch souce packages */
1586 struct DscFile
1587 {
1588 string Package;
1589 string Version;
1590 string Dsc;
1591 };
1592
1593 bool DoSource(CommandLine &CmdL)
1594 {
1595 CacheFile Cache;
1596 if (Cache.Open(false) == false)
1597 return false;
1598
1599 if (CmdL.FileSize() <= 1)
1600 return _error->Error(_("Must specify at least one package to fetch source for"));
1601
1602 // Read the source list
1603 pkgSourceList List;
1604 if (List.ReadMainList() == false)
1605 return _error->Error(_("The list of sources could not be read."));
1606
1607 // Create the text record parsers
1608 pkgRecords Recs(Cache);
1609 pkgSrcRecords SrcRecs(List);
1610 if (_error->PendingError() == true)
1611 return false;
1612
1613 // Create the download object
1614 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1615 pkgAcquire Fetcher(&Stat);
1616
1617 DscFile *Dsc = new DscFile[CmdL.FileSize()];
1618
1619 // Load the requestd sources into the fetcher
1620 unsigned J = 0;
1621 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1622 {
1623 string Src;
1624 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
1625
1626 if (Last == 0)
1627 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
1628
1629 // Back track
1630 vector<pkgSrcRecords::File> Lst;
1631 if (Last->Files(Lst) == false)
1632 return false;
1633
1634 // Load them into the fetcher
1635 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
1636 I != Lst.end(); I++)
1637 {
1638 // Try to guess what sort of file it is we are getting.
1639 if (I->Type == "dsc")
1640 {
1641 Dsc[J].Package = Last->Package();
1642 Dsc[J].Version = Last->Version();
1643 Dsc[J].Dsc = flNotDir(I->Path);
1644 }
1645
1646 // Diff only mode only fetches .diff files
1647 if (_config->FindB("APT::Get::Diff-Only",false) == true &&
1648 I->Type != "diff")
1649 continue;
1650
1651 // Tar only mode only fetches .tar files
1652 if (_config->FindB("APT::Get::Tar-Only",false) == true &&
1653 I->Type != "tar")
1654 continue;
1655
1656 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
1657 I->MD5Hash,I->Size,
1658 Last->Index().SourceInfo(*Last,*I),Src);
1659 }
1660 }
1661
1662 // Display statistics
1663 double FetchBytes = Fetcher.FetchNeeded();
1664 double FetchPBytes = Fetcher.PartialPresent();
1665 double DebBytes = Fetcher.TotalNeeded();
1666
1667 // Check for enough free space
1668 struct statvfs Buf;
1669 string OutputDir = ".";
1670 if (statvfs(OutputDir.c_str(),&Buf) != 0)
1671 return _error->Errno("statvfs","Couldn't determine free space in %s",
1672 OutputDir.c_str());
1673 if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
1674 return _error->Error(_("Sorry, you don't have enough free space in %s"),
1675 OutputDir.c_str());
1676
1677 // Number of bytes
1678 if (DebBytes != FetchBytes)
1679 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
1680 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
1681 else
1682 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
1683 SizeToStr(DebBytes).c_str());
1684
1685 if (_config->FindB("APT::Get::Simulate",false) == true)
1686 {
1687 for (unsigned I = 0; I != J; I++)
1688 ioprintf(cout,_("Fetch Source %s\n"),Dsc[I].Package.c_str());
1689 return true;
1690 }
1691
1692 // Just print out the uris an exit if the --print-uris flag was used
1693 if (_config->FindB("APT::Get::Print-URIs") == true)
1694 {
1695 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1696 for (; I != Fetcher.UriEnd(); I++)
1697 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
1698 I->Owner->FileSize << ' ' << I->Owner->MD5Sum() << endl;
1699 return true;
1700 }
1701
1702 // Run it
1703 if (Fetcher.Run() == pkgAcquire::Failed)
1704 return false;
1705
1706 // Print error messages
1707 bool Failed = false;
1708 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
1709 {
1710 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1711 (*I)->Complete == true)
1712 continue;
1713
1714 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1715 (*I)->ErrorText.c_str());
1716 Failed = true;
1717 }
1718 if (Failed == true)
1719 return _error->Error(_("Failed to fetch some archives."));
1720
1721 if (_config->FindB("APT::Get::Download-only",false) == true)
1722 {
1723 c1out << _("Download complete and in download only mode") << endl;
1724 return true;
1725 }
1726
1727 // Unpack the sources
1728 pid_t Process = ExecFork();
1729
1730 if (Process == 0)
1731 {
1732 for (unsigned I = 0; I != J; I++)
1733 {
1734 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
1735
1736 // Diff only mode only fetches .diff files
1737 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
1738 _config->FindB("APT::Get::Tar-Only",false) == true ||
1739 Dsc[I].Dsc.empty() == true)
1740 continue;
1741
1742 // See if the package is already unpacked
1743 struct stat Stat;
1744 if (stat(Dir.c_str(),&Stat) == 0 &&
1745 S_ISDIR(Stat.st_mode) != 0)
1746 {
1747 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
1748 Dir.c_str());
1749 }
1750 else
1751 {
1752 // Call dpkg-source
1753 char S[500];
1754 snprintf(S,sizeof(S),"%s -x %s",
1755 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
1756 Dsc[I].Dsc.c_str());
1757 if (system(S) != 0)
1758 {
1759 fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
1760 _exit(1);
1761 }
1762 }
1763
1764 // Try to compile it with dpkg-buildpackage
1765 if (_config->FindB("APT::Get::Compile",false) == true)
1766 {
1767 // Call dpkg-buildpackage
1768 char S[500];
1769 snprintf(S,sizeof(S),"cd %s && %s %s",
1770 Dir.c_str(),
1771 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
1772 _config->Find("DPkg::Build-Options","-b -uc").c_str());
1773
1774 if (system(S) != 0)
1775 {
1776 fprintf(stderr,_("Build command '%s' failed.\n"),S);
1777 _exit(1);
1778 }
1779 }
1780 }
1781
1782 _exit(0);
1783 }
1784
1785 // Wait for the subprocess
1786 int Status = 0;
1787 while (waitpid(Process,&Status,0) != Process)
1788 {
1789 if (errno == EINTR)
1790 continue;
1791 return _error->Errno("waitpid","Couldn't wait for subprocess");
1792 }
1793
1794 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
1795 return _error->Error(_("Child process failed"));
1796
1797 return true;
1798 }
1799 /*}}}*/
1800 // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
1801 // ---------------------------------------------------------------------
1802 /* This function will look at the build depends list of the given source
1803 package and install the necessary packages to make it true, or fail. */
1804 bool DoBuildDep(CommandLine &CmdL)
1805 {
1806 CacheFile Cache;
1807 if (Cache.Open(true) == false)
1808 return false;
1809
1810 if (CmdL.FileSize() <= 1)
1811 return _error->Error(_("Must specify at least one package to check builddeps for"));
1812
1813 // Read the source list
1814 pkgSourceList List;
1815 if (List.ReadMainList() == false)
1816 return _error->Error(_("The list of sources could not be read."));
1817
1818 // Create the text record parsers
1819 pkgRecords Recs(Cache);
1820 pkgSrcRecords SrcRecs(List);
1821 if (_error->PendingError() == true)
1822 return false;
1823
1824 // Create the download object
1825 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1826 pkgAcquire Fetcher(&Stat);
1827
1828 unsigned J = 0;
1829 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1830 {
1831 string Src;
1832 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
1833 if (Last == 0)
1834 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
1835
1836 // Process the build-dependencies
1837 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
1838 if (Last->BuildDepends(BuildDeps) == false)
1839 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
1840
1841 if (BuildDeps.size() == 0)
1842 {
1843 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
1844 continue;
1845 }
1846
1847 // Install the requested packages
1848 unsigned int ExpectedInst = 0;
1849 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
1850 pkgProblemResolver Fix(Cache);
1851 for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
1852 {
1853 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
1854 if (Pkg.end() == true)
1855 return _error->Error(_("%s dependency on %s cannot be satisfied because the package %s cannot be found"),
1856 Last->BuildDepType((*D).Type),Src.c_str(),(*D).Package.c_str());
1857 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
1858
1859 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
1860 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
1861 {
1862 /*
1863 * conflict; need to remove if we have an installed version
1864 * that satisfies the version criterial
1865 */
1866 if (IV.end() == false &&
1867 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
1868 TryToInstall(Pkg,Cache,Fix,true,false,ExpectedInst);
1869 }
1870 else
1871 {
1872 /*
1873 * If this is a virtual package, we need to check the list of
1874 * packages that provide it and see if any of those are
1875 * installed
1876 */
1877 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
1878 for (; Prv.end() != true; Prv++)
1879 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
1880 break;
1881
1882 if (Prv.end() == true)
1883 {
1884 /*
1885 * depends; need to install or upgrade if we don't have the
1886 * package installed or if the version does not satisfy the
1887 * build dep. This is complicated by the fact that if we
1888 * depend on a version lower than what we already have
1889 * installed it is not clear what should be done; in practice
1890 * this case should be rare though and right now nothing
1891 * is done about it :-(
1892 */
1893 if (IV.end() == true ||
1894 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
1895 TryToInstall(Pkg,Cache,Fix,false,false,ExpectedInst);
1896 }
1897 }
1898 }
1899
1900 Fix.InstallProtect();
1901 if (Fix.Resolve(true) == false)
1902 _error->Discard();
1903
1904 // Now we check the state of the packages,
1905 if (Cache->BrokenCount() != 0)
1906 return _error->Error(_("Some broken packages were found while trying to process build-dependencies.\n"
1907 "You might want to run `apt-get -f install' to correct these."));
1908 }
1909
1910 if (InstallPackages(Cache, false, true) == false)
1911 return _error->Error(_("Failed to process build dependencies"));
1912 return true;
1913 }
1914 /*}}}*/
1915
1916 // DoMoo - Never Ask, Never Tell /*{{{*/
1917 // ---------------------------------------------------------------------
1918 /* */
1919 bool DoMoo(CommandLine &CmdL)
1920 {
1921 cout <<
1922 " (__) \n"
1923 " (oo) \n"
1924 " /------\\/ \n"
1925 " / | || \n"
1926 " * /\\---/\\ \n"
1927 " ~~ ~~ \n"
1928 "....\"Have you mooed today?\"...\n";
1929
1930 return true;
1931 }
1932 /*}}}*/
1933 // ShowHelp - Show a help screen /*{{{*/
1934 // ---------------------------------------------------------------------
1935 /* */
1936 bool ShowHelp(CommandLine &CmdL)
1937 {
1938 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
1939 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
1940
1941 if (_config->FindB("version") == true)
1942 {
1943 cout << _("Supported Modules:") << endl;
1944
1945 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
1946 {
1947 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
1948 if (_system != 0 && _system->VS == VS)
1949 cout << '*';
1950 else
1951 cout << ' ';
1952 cout << "Ver: " << VS->Label << endl;
1953
1954 /* Print out all the packaging systems that will work with
1955 this VS */
1956 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
1957 {
1958 pkgSystem *Sys = pkgSystem::GlobalList[J];
1959 if (_system == Sys)
1960 cout << '*';
1961 else
1962 cout << ' ';
1963 if (Sys->VS->TestCompatibility(*VS) == true)
1964 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
1965 }
1966 }
1967
1968 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
1969 {
1970 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
1971 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
1972 }
1973
1974 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
1975 {
1976 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
1977 cout << " Idx: " << Type->Label << endl;
1978 }
1979
1980 return true;
1981 }
1982
1983 cout <<
1984 _("Usage: apt-get [options] command\n"
1985 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
1986 " apt-get [options] source pkg1 [pkg2 ...]\n"
1987 "\n"
1988 "apt-get is a simple command line interface for downloading and\n"
1989 "installing packages. The most frequently used commands are update\n"
1990 "and install.\n"
1991 "\n"
1992 "Commands:\n"
1993 " update - Retrieve new lists of packages\n"
1994 " upgrade - Perform an upgrade\n"
1995 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
1996 " remove - Remove packages\n"
1997 " source - Download source archives\n"
1998 " build-dep - Configure build-dependencies for source packages\n"
1999 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
2000 " dselect-upgrade - Follow dselect selections\n"
2001 " clean - Erase downloaded archive files\n"
2002 " autoclean - Erase old downloaded archive files\n"
2003 " check - Verify that there are no broken dependencies\n"
2004 "\n"
2005 "Options:\n"
2006 " -h This help text.\n"
2007 " -q Loggable output - no progress indicator\n"
2008 " -qq No output except for errors\n"
2009 " -d Download only - do NOT install or unpack archives\n"
2010 " -s No-act. Perform ordering simulation\n"
2011 " -y Assume Yes to all queries and do not prompt\n"
2012 " -f Attempt to continue if the integrity check fails\n"
2013 " -m Attempt to continue if archives are unlocatable\n"
2014 " -u Show a list of upgraded packages as well\n"
2015 " -b Build the source package after fetching it\n"
2016 " -c=? Read this configuration file\n"
2017 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n"
2018 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
2019 "pages for more information and options.\n"
2020 " This APT has Super Cow Powers.\n");
2021 return true;
2022 }
2023 /*}}}*/
2024 // GetInitialize - Initialize things for apt-get /*{{{*/
2025 // ---------------------------------------------------------------------
2026 /* */
2027 void GetInitialize()
2028 {
2029 _config->Set("quiet",0);
2030 _config->Set("help",false);
2031 _config->Set("APT::Get::Download-Only",false);
2032 _config->Set("APT::Get::Simulate",false);
2033 _config->Set("APT::Get::Assume-Yes",false);
2034 _config->Set("APT::Get::Fix-Broken",false);
2035 _config->Set("APT::Get::Force-Yes",false);
2036 _config->Set("APT::Get::APT::Get::No-List-Cleanup",true);
2037 }
2038 /*}}}*/
2039 // SigWinch - Window size change signal handler /*{{{*/
2040 // ---------------------------------------------------------------------
2041 /* */
2042 void SigWinch(int)
2043 {
2044 // Riped from GNU ls
2045 #ifdef TIOCGWINSZ
2046 struct winsize ws;
2047
2048 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
2049 ScreenWidth = ws.ws_col - 1;
2050 #endif
2051 }
2052 /*}}}*/
2053
2054 int main(int argc,const char *argv[])
2055 {
2056 CommandLine::Args Args[] = {
2057 {'h',"help","help",0},
2058 {'v',"version","version",0},
2059 {'q',"quiet","quiet",CommandLine::IntLevel},
2060 {'q',"silent","quiet",CommandLine::IntLevel},
2061 {'d',"download-only","APT::Get::Download-Only",0},
2062 {'b',"compile","APT::Get::Compile",0},
2063 {'b',"build","APT::Get::Compile",0},
2064 {'s',"simulate","APT::Get::Simulate",0},
2065 {'s',"just-print","APT::Get::Simulate",0},
2066 {'s',"recon","APT::Get::Simulate",0},
2067 {'s',"dry-run","APT::Get::Simulate",0},
2068 {'s',"no-act","APT::Get::Simulate",0},
2069 {'y',"yes","APT::Get::Assume-Yes",0},
2070 {'y',"assume-yes","APT::Get::Assume-Yes",0},
2071 {'f',"fix-broken","APT::Get::Fix-Broken",0},
2072 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
2073 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
2074 {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
2075 {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
2076 {0,"download","APT::Get::Download",0},
2077 {0,"fix-missing","APT::Get::Fix-Missing",0},
2078 {0,"ignore-hold","APT::Ignore-Hold",0},
2079 {0,"upgrade","APT::Get::upgrade",0},
2080 {0,"force-yes","APT::Get::force-yes",0},
2081 {0,"print-uris","APT::Get::Print-URIs",0},
2082 {0,"diff-only","APT::Get::Diff-Only",0},
2083 {0,"tar-only","APT::Get::tar-Only",0},
2084 {0,"purge","APT::Get::Purge",0},
2085 {0,"list-cleanup","APT::Get::List-Cleanup",0},
2086 {0,"reinstall","APT::Get::ReInstall",0},
2087 {0,"trivial-only","APT::Get::Trivial-Only",0},
2088 {0,"remove","APT::Get::Remove",0},
2089 {0,"only-source","APT::Get::Only-Source",0},
2090 {'c',"config-file",0,CommandLine::ConfigFile},
2091 {'o',"option",0,CommandLine::ArbItem},
2092 {0,0,0,0}};
2093 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
2094 {"upgrade",&DoUpgrade},
2095 {"install",&DoInstall},
2096 {"remove",&DoInstall},
2097 {"dist-upgrade",&DoDistUpgrade},
2098 {"dselect-upgrade",&DoDSelectUpgrade},
2099 {"build-dep",&DoBuildDep},
2100 {"clean",&DoClean},
2101 {"autoclean",&DoAutoClean},
2102 {"check",&DoCheck},
2103 {"source",&DoSource},
2104 {"moo",&DoMoo},
2105 {"help",&ShowHelp},
2106 {0,0}};
2107
2108 // Parse the command line and initialize the package library
2109 CommandLine CmdL(Args,_config);
2110 if (pkgInitConfig(*_config) == false ||
2111 CmdL.Parse(argc,argv) == false ||
2112 pkgInitSystem(*_config,_system) == false)
2113 {
2114 if (_config->FindB("version") == true)
2115 ShowHelp(CmdL);
2116
2117 _error->DumpErrors();
2118 return 100;
2119 }
2120
2121 // See if the help should be shown
2122 if (_config->FindB("help") == true ||
2123 _config->FindB("version") == true ||
2124 CmdL.FileSize() == 0)
2125 {
2126 ShowHelp(CmdL);
2127 return 0;
2128 }
2129
2130 // Deal with stdout not being a tty
2131 if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
2132 _config->Set("quiet","1");
2133
2134 // Setup the output streams
2135 c0out.rdbuf(cout.rdbuf());
2136 c1out.rdbuf(cout.rdbuf());
2137 c2out.rdbuf(cout.rdbuf());
2138 if (_config->FindI("quiet",0) > 0)
2139 c0out.rdbuf(devnull.rdbuf());
2140 if (_config->FindI("quiet",0) > 1)
2141 c1out.rdbuf(devnull.rdbuf());
2142
2143 // Setup the signals
2144 signal(SIGPIPE,SIG_IGN);
2145 signal(SIGWINCH,SigWinch);
2146 SigWinch(0);
2147
2148 // Match the operation
2149 CmdL.DispatchArg(Cmds);
2150
2151 // Print any errors or warnings found during parsing
2152 if (_error->empty() == false)
2153 {
2154 bool Errors = _error->PendingError();
2155 _error->DumpErrors();
2156 return Errors == true?100:0;
2157 }
2158
2159 return 0;
2160 }