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