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