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