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