really do not show packages in the extra section if they were
[ntk/apt.git] / cmdline / apt-get.cc
CommitLineData
5ec427c2
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
640c5d94 3// $Id: apt-get.cc,v 1.156 2004/08/28 01:05:16 mdz 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 /*{{{*/
4b12ea90
JAK
28#define _LARGEFILE_SOURCE
29#define _LARGEFILE64_SOURCE
30
086bb6d7 31#include <apt-pkg/aptconfiguration.h>
0a8e3465
AL
32#include <apt-pkg/error.h>
33#include <apt-pkg/cmndline.h>
34#include <apt-pkg/init.h>
35#include <apt-pkg/depcache.h>
36#include <apt-pkg/sourcelist.h>
0a8e3465 37#include <apt-pkg/algorithms.h>
0919e3f9 38#include <apt-pkg/acquire-item.h>
cdcc6d34 39#include <apt-pkg/strutl.h>
1bc849af 40#include <apt-pkg/clean.h>
36375005
AL
41#include <apt-pkg/srcrecords.h>
42#include <apt-pkg/version.h>
2d11135a 43#include <apt-pkg/cachefile.h>
8fde7239 44#include <apt-pkg/cacheset.h>
b2e465d6 45#include <apt-pkg/sptr.h>
092ae175 46#include <apt-pkg/md5.h>
b2e465d6 47#include <apt-pkg/versionmatch.h>
ffee1c2b 48
0a8e3465 49#include <config.h>
b2e465d6 50#include <apti18n.h>
0a8e3465 51
0919e3f9
AL
52#include "acqprogress.h"
53
092ae175 54#include <set>
233c2b66 55#include <locale.h>
c8ca0ce1 56#include <langinfo.h>
90f057fd 57#include <fstream>
d7827aca
AL
58#include <termios.h>
59#include <sys/ioctl.h>
1bc849af 60#include <sys/stat.h>
885d204b 61#include <sys/statfs.h>
101030ab 62#include <sys/statvfs.h>
d7827aca 63#include <signal.h>
65a1e968 64#include <unistd.h>
3e3221ba 65#include <stdio.h>
d6e79b75 66#include <errno.h>
c373c37a 67#include <regex.h>
54676e1a 68#include <sys/wait.h>
afb1e2e3 69#include <sstream>
4b12ea90
JAK
70
71#define statfs statfs64
72#define statvfs statvfs64
0a8e3465
AL
73 /*}}}*/
74
885d204b
OS
75#define RAMFS_MAGIC 0x858458f6
76
076d01b0
AL
77using namespace std;
78
79ostream c0out(0);
80ostream c1out(0);
81ostream c2out(0);
0a8e3465 82ofstream devnull("/dev/null");
463870e4 83unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
0a8e3465 84
1089ca89
AL
85// class CacheFile - Cover class for some dependency cache functions /*{{{*/
86// ---------------------------------------------------------------------
87/* */
88class CacheFile : public pkgCacheFile
89{
90 static pkgCache *SortCache;
91 static int NameComp(const void *a,const void *b);
92
93 public:
94 pkgCache::Package **List;
95
96 void Sort();
97 bool CheckDeps(bool AllowBroken = false);
0077d829
AL
98 bool BuildCaches(bool WithLock = true)
99 {
100 OpTextProgress Prog(*_config);
ea4b220b 101 if (pkgCacheFile::BuildCaches(&Prog,WithLock) == false)
0077d829
AL
102 return false;
103 return true;
104 }
1089ca89
AL
105 bool Open(bool WithLock = true)
106 {
107 OpTextProgress Prog(*_config);
ea4b220b 108 if (pkgCacheFile::Open(&Prog,WithLock) == false)
1089ca89
AL
109 return false;
110 Sort();
b2e465d6 111
1089ca89
AL
112 return true;
113 };
c37b9502
AL
114 bool OpenForInstall()
115 {
116 if (_config->FindB("APT::Get::Print-URIs") == true)
079a992d 117 return Open(false);
c37b9502 118 else
079a992d 119 return Open(true);
c37b9502 120 }
1089ca89 121 CacheFile() : List(0) {};
7a9f09bd
MV
122 ~CacheFile() {
123 delete[] List;
124 }
1089ca89
AL
125};
126 /*}}}*/
127
a6568219
AL
128// YnPrompt - Yes No Prompt. /*{{{*/
129// ---------------------------------------------------------------------
130/* Returns true on a Yes.*/
7db98ffc 131bool YnPrompt(bool Default=true)
a6568219
AL
132{
133 if (_config->FindB("APT::Get::Assume-Yes",false) == true)
134 {
10cda9fe 135 c1out << _("Y") << endl;
a6568219
AL
136 return true;
137 }
10cda9fe
AL
138
139 char response[1024] = "";
140 cin.getline(response, sizeof(response));
141
142 if (!cin)
b2e465d6 143 return false;
10cda9fe
AL
144
145 if (strlen(response) == 0)
7db98ffc 146 return Default;
10cda9fe
AL
147
148 regex_t Pattern;
149 int Res;
150
151 Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
152 REG_EXTENDED|REG_ICASE|REG_NOSUB);
153
154 if (Res != 0) {
155 char Error[300];
156 regerror(Res,&Pattern,Error,sizeof(Error));
157 return _error->Error(_("Regex compilation error - %s"),Error);
158 }
a6568219 159
10cda9fe
AL
160 Res = regexec(&Pattern, response, 0, NULL, 0);
161 if (Res == 0)
162 return true;
163 return false;
a6568219
AL
164}
165 /*}}}*/
6f86c974
AL
166// AnalPrompt - Annoying Yes No Prompt. /*{{{*/
167// ---------------------------------------------------------------------
168/* Returns true on a Yes.*/
169bool AnalPrompt(const char *Text)
170{
171 char Buf[1024];
172 cin.getline(Buf,sizeof(Buf));
173 if (strcmp(Buf,Text) == 0)
174 return true;
175 return false;
176}
177 /*}}}*/
0a8e3465
AL
178// ShowList - Show a list /*{{{*/
179// ---------------------------------------------------------------------
b2e465d6 180/* This prints out a string of space separated words with a title and
0a8e3465 181 a two space indent line wraped to the current screen width. */
ac625538 182bool ShowList(ostream &out,string Title,string List,string VersionsList)
0a8e3465
AL
183{
184 if (List.empty() == true)
83d89a9f 185 return true;
4968036c
AL
186 // trim trailing space
187 int NonSpace = List.find_last_not_of(' ');
188 if (NonSpace != -1)
189 {
190 List = List.erase(NonSpace + 1);
191 if (List.empty() == true)
192 return true;
193 }
0a8e3465
AL
194
195 // Acount for the leading space
196 int ScreenWidth = ::ScreenWidth - 3;
197
198 out << Title << endl;
199 string::size_type Start = 0;
ac625538 200 string::size_type VersionsStart = 0;
0a8e3465
AL
201 while (Start < List.size())
202 {
ac625538
AL
203 if(_config->FindB("APT::Get::Show-Versions",false) == true &&
204 VersionsList.size() > 0) {
205 string::size_type End;
206 string::size_type VersionsEnd;
207
208 End = List.find(' ',Start);
209 VersionsEnd = VersionsList.find('\n', VersionsStart);
210
211 out << " " << string(List,Start,End - Start) << " (" <<
212 string(VersionsList,VersionsStart,VersionsEnd - VersionsStart) <<
213 ")" << endl;
03b9be80
AL
214
215 if (End == string::npos || End < Start)
216 End = Start + ScreenWidth;
217
ac625538
AL
218 Start = End + 1;
219 VersionsStart = VersionsEnd + 1;
220 } else {
221 string::size_type End;
222
223 if (Start + ScreenWidth >= List.size())
224 End = List.size();
225 else
226 End = List.rfind(' ',Start+ScreenWidth);
227
228 if (End == string::npos || End < Start)
229 End = Start + ScreenWidth;
230 out << " " << string(List,Start,End - Start) << endl;
231 Start = End + 1;
232 }
0a8e3465 233 }
ac625538 234
83d89a9f 235 return false;
0a8e3465
AL
236}
237 /*}}}*/
238// ShowBroken - Debugging aide /*{{{*/
239// ---------------------------------------------------------------------
240/* This prints out the names of all the packages that are broken along
241 with the name of each each broken dependency and a quite version
b2e465d6
AL
242 description.
243
244 The output looks like:
677cbcbc 245 The following packages have unmet dependencies:
b2e465d6
AL
246 exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
247 Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
248 Depends: libsasl7 but it is not going to be installed
249 */
421c8d10 250void ShowBroken(ostream &out,CacheFile &Cache,bool Now)
0a8e3465 251{
677cbcbc 252 out << _("The following packages have unmet dependencies:") << endl;
1089ca89 253 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 254 {
1089ca89
AL
255 pkgCache::PkgIterator I(Cache,Cache.List[J]);
256
079a992d
AL
257 if (Now == true)
258 {
259 if (Cache[I].NowBroken() == false)
260 continue;
261 }
262 else
263 {
264 if (Cache[I].InstBroken() == false)
265 continue;
266 }
267
303a1703 268 // Print out each package and the failed dependencies
75ce2062
DK
269 out << " " << I.FullName(true) << " :";
270 unsigned const Indent = I.FullName(true).size() + 3;
303a1703 271 bool First = true;
079a992d
AL
272 pkgCache::VerIterator Ver;
273
274 if (Now == true)
275 Ver = I.CurrentVer();
276 else
277 Ver = Cache[I].InstVerIter(Cache);
278
279 if (Ver.end() == true)
0a8e3465 280 {
079a992d 281 out << endl;
303a1703
AL
282 continue;
283 }
284
079a992d 285 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
303a1703 286 {
30e1eab5
AL
287 // Compute a single dependency element (glob or)
288 pkgCache::DepIterator Start;
289 pkgCache::DepIterator End;
d20333af 290 D.GlobOr(Start,End); // advances D
76fbce56 291
079a992d 292 if (Cache->IsImportantDep(End) == false)
303a1703 293 continue;
079a992d
AL
294
295 if (Now == true)
296 {
297 if ((Cache[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
298 continue;
299 }
300 else
301 {
302 if ((Cache[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
303 continue;
304 }
305
648e3cb4
AL
306 bool FirstOr = true;
307 while (1)
0a8e3465 308 {
648e3cb4
AL
309 if (First == false)
310 for (unsigned J = 0; J != Indent; J++)
311 out << ' ';
312 First = false;
313
314 if (FirstOr == false)
315 {
316 for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
317 out << ' ';
318 }
0a8e3465 319 else
648e3cb4
AL
320 out << ' ' << End.DepType() << ": ";
321 FirstOr = false;
322
75ce2062 323 out << Start.TargetPkg().FullName(true);
648e3cb4
AL
324
325 // Show a quick summary of the version requirements
326 if (Start.TargetVer() != 0)
b2e465d6 327 out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
648e3cb4
AL
328
329 /* Show a summary of the target package if possible. In the case
330 of virtual packages we show nothing */
331 pkgCache::PkgIterator Targ = Start.TargetPkg();
332 if (Targ->ProvidesList == 0)
7e798dd7 333 {
b2e465d6 334 out << ' ';
648e3cb4 335 pkgCache::VerIterator Ver = Cache[Targ].InstVerIter(Cache);
f0ec51c2
AL
336 if (Now == true)
337 Ver = Targ.CurrentVer();
079a992d 338
648e3cb4 339 if (Ver.end() == false)
b2e465d6
AL
340 {
341 if (Now == true)
342 ioprintf(out,_("but %s is installed"),Ver.VerStr());
343 else
344 ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
345 }
648e3cb4 346 else
303a1703 347 {
648e3cb4
AL
348 if (Cache[Targ].CandidateVerIter(Cache).end() == true)
349 {
350 if (Targ->ProvidesList == 0)
b2e465d6 351 out << _("but it is not installable");
648e3cb4 352 else
b2e465d6 353 out << _("but it is a virtual package");
648e3cb4 354 }
303a1703 355 else
b2e465d6 356 out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
648e3cb4
AL
357 }
358 }
359
360 if (Start != End)
b2e465d6 361 out << _(" or");
648e3cb4
AL
362 out << endl;
363
364 if (Start == End)
365 break;
366 Start++;
367 }
303a1703 368 }
0a8e3465
AL
369 }
370}
371 /*}}}*/
372// ShowNew - Show packages to newly install /*{{{*/
373// ---------------------------------------------------------------------
374/* */
1089ca89 375void ShowNew(ostream &out,CacheFile &Cache)
0a8e3465 376{
89260e53 377 /* Print out a list of packages that are going to be installed extra
0a8e3465 378 to what the user asked */
0a8e3465 379 string List;
ac625538 380 string VersionsList;
1089ca89
AL
381 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
382 {
383 pkgCache::PkgIterator I(Cache,Cache.List[J]);
ac625538 384 if (Cache[I].NewInstall() == true) {
803ea2a8
DK
385 if (Cache[I].CandidateVerIter(Cache).Pseudo() == true)
386 continue;
75ce2062 387 List += I.FullName(true) + " ";
ac625538
AL
388 VersionsList += string(Cache[I].CandVersion) + "\n";
389 }
1089ca89
AL
390 }
391
ac625538 392 ShowList(out,_("The following NEW packages will be installed:"),List,VersionsList);
0a8e3465
AL
393}
394 /*}}}*/
395// ShowDel - Show packages to delete /*{{{*/
396// ---------------------------------------------------------------------
397/* */
1089ca89 398void ShowDel(ostream &out,CacheFile &Cache)
0a8e3465
AL
399{
400 /* Print out a list of packages that are going to be removed extra
401 to what the user asked */
0a8e3465 402 string List;
ac625538 403 string VersionsList;
1089ca89 404 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
fc4b5c9f 405 {
1089ca89
AL
406 pkgCache::PkgIterator I(Cache,Cache.List[J]);
407 if (Cache[I].Delete() == true)
fc4b5c9f 408 {
803ea2a8
DK
409 if (Cache[I].CandidateVerIter(Cache).Pseudo() == true)
410 continue;
1089ca89 411 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
75ce2062 412 List += I.FullName(true) + "* ";
fc4b5c9f 413 else
75ce2062 414 List += I.FullName(true) + " ";
ac625538
AL
415
416 VersionsList += string(Cache[I].CandVersion)+ "\n";
fc4b5c9f
AL
417 }
418 }
3d615484 419
ac625538 420 ShowList(out,_("The following packages will be REMOVED:"),List,VersionsList);
0a8e3465
AL
421}
422 /*}}}*/
423// ShowKept - Show kept packages /*{{{*/
424// ---------------------------------------------------------------------
425/* */
f292686b 426void ShowKept(ostream &out,CacheFile &Cache)
0a8e3465 427{
0a8e3465 428 string List;
ac625538 429 string VersionsList;
f292686b 430 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 431 {
f292686b
AL
432 pkgCache::PkgIterator I(Cache,Cache.List[J]);
433
0a8e3465 434 // Not interesting
f292686b
AL
435 if (Cache[I].Upgrade() == true || Cache[I].Upgradable() == false ||
436 I->CurrentVer == 0 || Cache[I].Delete() == true)
0a8e3465
AL
437 continue;
438
75ce2062 439 List += I.FullName(true) + " ";
ac625538 440 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
0a8e3465 441 }
aee7bceb 442 ShowList(out,_("The following packages have been kept back:"),List,VersionsList);
0a8e3465
AL
443}
444 /*}}}*/
445// ShowUpgraded - Show upgraded packages /*{{{*/
446// ---------------------------------------------------------------------
447/* */
1089ca89 448void ShowUpgraded(ostream &out,CacheFile &Cache)
0a8e3465 449{
0a8e3465 450 string List;
ac625538 451 string VersionsList;
1089ca89 452 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 453 {
1089ca89
AL
454 pkgCache::PkgIterator I(Cache,Cache.List[J]);
455
0a8e3465 456 // Not interesting
1089ca89 457 if (Cache[I].Upgrade() == false || Cache[I].NewInstall() == true)
0a8e3465 458 continue;
803ea2a8
DK
459 if (Cache[I].CandidateVerIter(Cache).Pseudo() == true)
460 continue;
461
75ce2062 462 List += I.FullName(true) + " ";
ac625538 463 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
0a8e3465 464 }
aee7bceb 465 ShowList(out,_("The following packages will be upgraded:"),List,VersionsList);
b2e465d6
AL
466}
467 /*}}}*/
468// ShowDowngraded - Show downgraded packages /*{{{*/
469// ---------------------------------------------------------------------
470/* */
471bool ShowDowngraded(ostream &out,CacheFile &Cache)
472{
473 string List;
ac625538 474 string VersionsList;
b2e465d6
AL
475 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
476 {
477 pkgCache::PkgIterator I(Cache,Cache.List[J]);
478
479 // Not interesting
480 if (Cache[I].Downgrade() == false || Cache[I].NewInstall() == true)
481 continue;
803ea2a8
DK
482 if (Cache[I].CandidateVerIter(Cache).Pseudo() == true)
483 continue;
484
75ce2062 485 List += I.FullName(true) + " ";
ac625538 486 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
b2e465d6 487 }
aee7bceb 488 return ShowList(out,_("The following packages will be DOWNGRADED:"),List,VersionsList);
0a8e3465
AL
489}
490 /*}}}*/
491// ShowHold - Show held but changed packages /*{{{*/
492// ---------------------------------------------------------------------
493/* */
1089ca89 494bool ShowHold(ostream &out,CacheFile &Cache)
0a8e3465 495{
0a8e3465 496 string List;
ac625538 497 string VersionsList;
1089ca89 498 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 499 {
1089ca89
AL
500 pkgCache::PkgIterator I(Cache,Cache.List[J]);
501 if (Cache[I].InstallVer != (pkgCache::Version *)I.CurrentVer() &&
ac625538 502 I->SelectedState == pkgCache::State::Hold) {
75ce2062 503 List += I.FullName(true) + " ";
ac625538
AL
504 VersionsList += string(Cache[I].CurVersion) + " => " + Cache[I].CandVersion + "\n";
505 }
0a8e3465
AL
506 }
507
ac625538 508 return ShowList(out,_("The following held packages will be changed:"),List,VersionsList);
0a8e3465
AL
509}
510 /*}}}*/
511// ShowEssential - Show an essential package warning /*{{{*/
512// ---------------------------------------------------------------------
513/* This prints out a warning message that is not to be ignored. It shows
514 all essential packages and their dependents that are to be removed.
515 It is insanely risky to remove the dependents of an essential package! */
1089ca89 516bool ShowEssential(ostream &out,CacheFile &Cache)
0a8e3465 517{
0a8e3465 518 string List;
ac625538 519 string VersionsList;
b2e465d6
AL
520 bool *Added = new bool[Cache->Head().PackageCount];
521 for (unsigned int I = 0; I != Cache->Head().PackageCount; I++)
0a8e3465
AL
522 Added[I] = false;
523
1089ca89 524 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 525 {
1089ca89 526 pkgCache::PkgIterator I(Cache,Cache.List[J]);
b2e465d6
AL
527 if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
528 (I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
0a8e3465
AL
529 continue;
530
531 // The essential package is being removed
1089ca89 532 if (Cache[I].Delete() == true)
0a8e3465
AL
533 {
534 if (Added[I->ID] == false)
535 {
536 Added[I->ID] = true;
75ce2062 537 List += I.FullName(true) + " ";
ac625538 538 //VersionsList += string(Cache[I].CurVersion) + "\n"; ???
0a8e3465
AL
539 }
540 }
a02f24e0
DK
541 else
542 continue;
543
0a8e3465
AL
544 if (I->CurrentVer == 0)
545 continue;
546
547 // Print out any essential package depenendents that are to be removed
b2e465d6 548 for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; D++)
0a8e3465 549 {
3e3221ba
AL
550 // Skip everything but depends
551 if (D->Type != pkgCache::Dep::PreDepends &&
552 D->Type != pkgCache::Dep::Depends)
553 continue;
554
0a8e3465 555 pkgCache::PkgIterator P = D.SmartTargetPkg();
1089ca89 556 if (Cache[P].Delete() == true)
0a8e3465
AL
557 {
558 if (Added[P->ID] == true)
559 continue;
560 Added[P->ID] = true;
3e3221ba
AL
561
562 char S[300];
75ce2062 563 snprintf(S,sizeof(S),_("%s (due to %s) "),P.FullName(true).c_str(),I.FullName(true).c_str());
3e3221ba 564 List += S;
ac625538 565 //VersionsList += "\n"; ???
0a8e3465
AL
566 }
567 }
568 }
569
83d89a9f 570 delete [] Added;
080bf1be 571 return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
ac625538 572 "This should NOT be done unless you know exactly what you are doing!"),List,VersionsList);
0a8e3465 573}
7db98ffc 574
0a8e3465
AL
575 /*}}}*/
576// Stats - Show some statistics /*{{{*/
577// ---------------------------------------------------------------------
578/* */
579void Stats(ostream &out,pkgDepCache &Dep)
580{
581 unsigned long Upgrade = 0;
b2e465d6 582 unsigned long Downgrade = 0;
0a8e3465 583 unsigned long Install = 0;
d0c59649 584 unsigned long ReInstall = 0;
0a8e3465
AL
585 for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; I++)
586 {
42d71ab5
DK
587 if (pkgCache::VerIterator(Dep, Dep[I].CandidateVer).Pseudo() == true)
588 continue;
589
0a8e3465
AL
590 if (Dep[I].NewInstall() == true)
591 Install++;
592 else
b2e465d6 593 {
0a8e3465
AL
594 if (Dep[I].Upgrade() == true)
595 Upgrade++;
b2e465d6
AL
596 else
597 if (Dep[I].Downgrade() == true)
598 Downgrade++;
599 }
600
d0c59649
AL
601 if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
602 ReInstall++;
0a8e3465
AL
603 }
604
2adb5fda 605 ioprintf(out,_("%lu upgraded, %lu newly installed, "),
b2e465d6
AL
606 Upgrade,Install);
607
d0c59649 608 if (ReInstall != 0)
b2e465d6
AL
609 ioprintf(out,_("%lu reinstalled, "),ReInstall);
610 if (Downgrade != 0)
611 ioprintf(out,_("%lu downgraded, "),Downgrade);
0a8e3465 612
2d425135 613 ioprintf(out,_("%lu to remove and %lu not upgraded.\n"),
b2e465d6
AL
614 Dep.DelCount(),Dep.KeepCount());
615
0a8e3465 616 if (Dep.BadCount() != 0)
2adb5fda 617 ioprintf(out,_("%lu not fully installed or removed.\n"),
b2e465d6 618 Dep.BadCount());
0a8e3465
AL
619}
620 /*}}}*/
21d4c9f1
DK
621// CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
622class CacheSetHelperAPTGet : public APT::CacheSetHelper {
623 /** \brief stream message should be printed to */
624 std::ostream &out;
625 /** \brief were things like Task or RegEx used to select packages? */
626 bool explicitlyNamed;
627
628 APT::PackageSet virtualPkgs;
629
630public:
2c085486
DK
631 std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
632
21d4c9f1
DK
633 CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
634 explicitlyNamed = true;
635 }
636
637 virtual void showTaskSelection(APT::PackageSet const &pkgset, string const &pattern) {
638 for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
639 ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
640 Pkg.FullName(true).c_str(), pattern.c_str());
641 explicitlyNamed = false;
642 }
643 virtual void showRegExSelection(APT::PackageSet const &pkgset, string const &pattern) {
644 for (APT::PackageSet::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
645 ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
646 Pkg.FullName(true).c_str(), pattern.c_str());
647 explicitlyNamed = false;
648 }
649 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
650 string const &ver, bool const &verIsRel) {
2c085486
DK
651 if (ver == Ver.VerStr())
652 return;
653 selectedByRelease.push_back(make_pair(Ver, ver));
21d4c9f1
DK
654 }
655
656 bool showVirtualPackageErrors(pkgCacheFile &Cache) {
657 if (virtualPkgs.empty() == true)
658 return true;
659 for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
660 Pkg != virtualPkgs.end(); ++Pkg) {
661 if (Pkg->ProvidesList != 0) {
662 ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
663 Pkg.FullName(true).c_str());
664
665 pkgCache::PrvIterator I = Pkg.ProvidesList();
666 unsigned short provider = 0;
667 for (; I.end() == false; ++I) {
668 pkgCache::PkgIterator Pkg = I.OwnerPkg();
669
670 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
671 out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
672 if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
673 out << _(" [Installed]");
674 out << endl;
675 ++provider;
676 }
677 }
678 // if we found no candidate which provide this package, show non-candidates
679 if (provider == 0)
680 for (I = Pkg.ProvidesList(); I.end() == false; I++)
681 out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
682 << _(" [Not candidate version]") << endl;
683 else
684 out << _("You should explicitly select one to install.") << endl;
685 } else {
686 ioprintf(out,
687 _("Package %s is not available, but is referred to by another package.\n"
688 "This may mean that the package is missing, has been obsoleted, or\n"
689 "is only available from another source\n"),Pkg.FullName(true).c_str());
690
691 string List;
692 string VersionsList;
693 SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
694 memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
695 for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
696 Dep.end() == false; Dep++) {
697 if (Dep->Type != pkgCache::Dep::Replaces)
698 continue;
699 if (Seen[Dep.ParentPkg()->ID] == true)
700 continue;
701 Seen[Dep.ParentPkg()->ID] = true;
702 List += Dep.ParentPkg().FullName(true) + " ";
703 //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
704 }
705 ShowList(out,_("However the following packages replace it:"),List,VersionsList);
706 }
707 out << std::endl;
708 }
709 return false;
710 }
711
712 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
713 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::CANDIDATE);
714 if (verset.empty() == false)
715 return *(verset.begin());
716 if (ShowError == true) {
717 _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
718 virtualPkgs.insert(Pkg);
719 }
720 return pkgCache::VerIterator(Cache, 0);
721 }
722
723 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
724 APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, APT::VersionSet::NEWEST);
725 if (verset.empty() == false)
726 return *(verset.begin());
727 if (ShowError == true)
728 ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
729 return pkgCache::VerIterator(Cache, 0);
730 }
731
732 APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
733 APT::VersionSet::Version const &select) {
734 /* This is a pure virtual package and there is a single available
735 candidate providing it. */
736 if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
737 return APT::VersionSet();
738
739 pkgCache::PkgIterator Prov;
740 bool found_one = false;
741 for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
742 pkgCache::VerIterator const PVer = P.OwnerVer();
743 pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
744
745 /* Ignore versions that are not a candidate. */
746 if (Cache[PPkg].CandidateVer != PVer)
747 continue;
748
749 if (found_one == false) {
750 Prov = PPkg;
751 found_one = true;
752 } else if (PPkg != Prov) {
753 found_one = false; // we found at least two
754 break;
755 }
756 }
757
758 if (found_one == true) {
759 ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
760 Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
761 return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
762 }
763 return APT::VersionSet();
764 }
765
766 inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
767
768};
769 /*}}}*/
770// TryToInstall - Mark a package for installation /*{{{*/
771struct TryToInstall {
772 pkgCacheFile* Cache;
773 pkgProblemResolver* Fix;
774 bool FixBroken;
775 unsigned long AutoMarkChanged;
6806db8a 776 APT::PackageSet doAutoInstallLater;
21d4c9f1
DK
777
778 TryToInstall(pkgCacheFile &Cache, pkgProblemResolver &PM, bool const &FixBroken) : Cache(&Cache), Fix(&PM),
779 FixBroken(FixBroken), AutoMarkChanged(0) {};
780
781 void operator() (pkgCache::VerIterator const &Ver) {
782 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
2fbfb111 783
21d4c9f1
DK
784 Cache->GetDepCache()->SetCandidateVersion(Ver);
785 pkgDepCache::StateCache &State = (*Cache)[Pkg];
786
787 // Handle the no-upgrade case
788 if (_config->FindB("APT::Get::upgrade",true) == false && Pkg->CurrentVer != 0)
789 ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
790 Pkg.FullName(true).c_str());
791 // Ignore request for install if package would be new
792 else if (_config->FindB("APT::Get::Only-Upgrade", false) == true && Pkg->CurrentVer == 0)
793 ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"),
794 Pkg.FullName(true).c_str());
795 else {
796 Fix->Clear(Pkg);
797 Fix->Protect(Pkg);
798 Cache->GetDepCache()->MarkInstall(Pkg,false);
799
800 if (State.Install() == false) {
801 if (_config->FindB("APT::Get::ReInstall",false) == true) {
802 if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
803 ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
804 Pkg.FullName(true).c_str());
805 else
806 Cache->GetDepCache()->SetReInstall(Pkg, true);
807 } else
808 ioprintf(c1out,_("%s is already the newest version.\n"),
809 Pkg.FullName(true).c_str());
810 }
811
812 // Install it with autoinstalling enabled (if we not respect the minial
813 // required deps or the policy)
6806db8a
DK
814 if (FixBroken == false)
815 doAutoInstallLater.insert(Pkg);
21d4c9f1
DK
816 }
817
818 // see if we need to fix the auto-mark flag
819 // e.g. apt-get install foo
820 // where foo is marked automatic
821 if (State.Install() == false &&
822 (State.Flags & pkgCache::Flag::Auto) &&
823 _config->FindB("APT::Get::ReInstall",false) == false &&
824 _config->FindB("APT::Get::Only-Upgrade",false) == false &&
825 _config->FindB("APT::Get::Download-Only",false) == false)
826 {
827 ioprintf(c1out,_("%s set to manually installed.\n"),
828 Pkg.FullName(true).c_str());
829 Cache->GetDepCache()->MarkAuto(Pkg,false);
830 AutoMarkChanged++;
831 }
832 }
6806db8a 833
2c085486
DK
834 bool propergateReleaseCandiateSwitching(std::list<std::pair<pkgCache::VerIterator, std::string> > start, std::ostream &out)
835 {
836 bool Success = true;
837 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
838 for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
839 s != start.end(); ++s)
840 {
841 Changed.push_back(std::make_pair(s->first, pkgCache::VerIterator(*Cache)));
842 // We continue here even if it failed to enhance the ShowBroken output
843 Success &= Cache->GetDepCache()->SetCandidateRelease(s->first, s->second, Changed);
844 }
845 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
846 c != Changed.end(); ++c)
847 {
848 if (c->second.end() == true)
849 ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
850 c->first.VerStr(), c->first.RelStr().c_str(), c->first.ParentPkg().FullName(true).c_str());
851 else if (c->first.ParentPkg()->Group != c->second.ParentPkg()->Group)
852 {
853 pkgCache::VerIterator V = (*Cache)[c->first.ParentPkg()].CandidateVerIter(*Cache);
854 ioprintf(out, _("Selected version '%s' (%s) for '%s' because of '%s'\n"), V.VerStr(),
855 V.RelStr().c_str(), V.ParentPkg().FullName(true).c_str(), c->second.ParentPkg().FullName(true).c_str());
856 }
857 }
858 return Success;
859 }
860
6806db8a
DK
861 void doAutoInstall() {
862 for (APT::PackageSet::const_iterator P = doAutoInstallLater.begin();
863 P != doAutoInstallLater.end(); ++P) {
864 pkgDepCache::StateCache &State = (*Cache)[P];
865 if (State.InstBroken() == false && State.InstPolicyBroken() == false)
866 continue;
867 Cache->GetDepCache()->MarkInstall(P, true);
868 }
869 doAutoInstallLater.clear();
870 }
21d4c9f1
DK
871};
872 /*}}}*/
873// TryToRemove - Mark a package for removal /*{{{*/
874struct TryToRemove {
875 pkgCacheFile* Cache;
876 pkgProblemResolver* Fix;
877 bool FixBroken;
6cb1583a 878 bool PurgePkgs;
21d4c9f1
DK
879 unsigned long AutoMarkChanged;
880
6cb1583a
DK
881 TryToRemove(pkgCacheFile &Cache, pkgProblemResolver &PM) : Cache(&Cache), Fix(&PM),
882 PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
21d4c9f1
DK
883
884 void operator() (pkgCache::VerIterator const &Ver)
885 {
886 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
887
888 Fix->Clear(Pkg);
889 Fix->Protect(Pkg);
890 Fix->Remove(Pkg);
891
6cb1583a
DK
892 if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
893 (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
21d4c9f1
DK
894 ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.FullName(true).c_str());
895 else
6cb1583a 896 Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs);
21d4c9f1
DK
897 }
898};
899 /*}}}*/
1089ca89 900// CacheFile::NameComp - QSort compare by name /*{{{*/
0a8e3465
AL
901// ---------------------------------------------------------------------
902/* */
1089ca89
AL
903pkgCache *CacheFile::SortCache = 0;
904int CacheFile::NameComp(const void *a,const void *b)
0a8e3465 905{
8508b1df
AL
906 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
907 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
0a8e3465 908
1089ca89
AL
909 const pkgCache::Package &A = **(pkgCache::Package **)a;
910 const pkgCache::Package &B = **(pkgCache::Package **)b;
911
912 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
913}
914 /*}}}*/
915// CacheFile::Sort - Sort by name /*{{{*/
916// ---------------------------------------------------------------------
917/* */
918void CacheFile::Sort()
919{
920 delete [] List;
921 List = new pkgCache::Package *[Cache->Head().PackageCount];
922 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
923 pkgCache::PkgIterator I = Cache->PkgBegin();
924 for (;I.end() != true; I++)
925 List[I->ID] = I;
926
927 SortCache = *this;
928 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
929}
0a8e3465 930 /*}}}*/
b2e465d6 931// CacheFile::CheckDeps - Open the cache file /*{{{*/
0a8e3465
AL
932// ---------------------------------------------------------------------
933/* This routine generates the caches and then opens the dependency cache
934 and verifies that the system is OK. */
2d11135a 935bool CacheFile::CheckDeps(bool AllowBroken)
0a8e3465 936{
4ef9a929
MV
937 bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
938
d38b7b3d
AL
939 if (_error->PendingError() == true)
940 return false;
0a8e3465 941
0a8e3465 942 // Check that the system is OK
b2e465d6 943 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
db0db9fe 944 return _error->Error("Internal error, non-zero counts");
0a8e3465
AL
945
946 // Apply corrections for half-installed packages
b2e465d6 947 if (pkgApplyStatus(*DCache) == false)
0a8e3465
AL
948 return false;
949
4ef9a929
MV
950 if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
951 {
952 FixBroken = true;
953 if ((DCache->PolicyBrokenCount() > 0))
954 {
955 // upgrade all policy-broken packages with ForceImportantDeps=True
956 for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); I++)
957 if ((*DCache)[I].NowPolicyBroken() == true)
7610bb3d 958 DCache->MarkInstall(I,true,0, false, true);
4ef9a929
MV
959 }
960 }
961
0a8e3465 962 // Nothing is broken
b2e465d6 963 if (DCache->BrokenCount() == 0 || AllowBroken == true)
0a8e3465
AL
964 return true;
965
966 // Attempt to fix broken things
4ef9a929 967 if (FixBroken == true)
0a8e3465 968 {
b2e465d6
AL
969 c1out << _("Correcting dependencies...") << flush;
970 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
0a8e3465 971 {
b2e465d6 972 c1out << _(" failed.") << endl;
421c8d10 973 ShowBroken(c1out,*this,true);
0a8e3465 974
b2e465d6 975 return _error->Error(_("Unable to correct dependencies"));
0a8e3465 976 }
b2e465d6
AL
977 if (pkgMinimizeUpgrade(*DCache) == false)
978 return _error->Error(_("Unable to minimize the upgrade set"));
0a8e3465 979
b2e465d6 980 c1out << _(" Done") << endl;
0a8e3465
AL
981 }
982 else
983 {
b5647402 984 c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
421c8d10 985 ShowBroken(c1out,*this,true);
0a8e3465 986
b2e465d6 987 return _error->Error(_("Unmet dependencies. Try using -f."));
0a8e3465
AL
988 }
989
990 return true;
991}
92fcbfc1
DK
992 /*}}}*/
993// CheckAuth - check if each download comes form a trusted source /*{{{*/
994// ---------------------------------------------------------------------
995/* */
7db98ffc
MZ
996static bool CheckAuth(pkgAcquire& Fetcher)
997{
998 string UntrustedList;
999 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
1000 {
1001 if (!(*I)->IsTrusted())
1002 {
1003 UntrustedList += string((*I)->ShortDesc()) + " ";
1004 }
1005 }
1006
1007 if (UntrustedList == "")
1008 {
1009 return true;
1010 }
1011
1012 ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
1013
1014 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
1015 {
2a7e07c7 1016 c2out << _("Authentication warning overridden.\n");
7db98ffc
MZ
1017 return true;
1018 }
1019
1020 if (_config->FindI("quiet",0) < 2
1021 && _config->FindB("APT::Get::Assume-Yes",false) == false)
1022 {
db0db9fe 1023 c2out << _("Install these packages without verification [y/N]? ") << flush;
7db98ffc
MZ
1024 if (!YnPrompt(false))
1025 return _error->Error(_("Some packages could not be authenticated"));
1026
1027 return true;
1028 }
1029 else if (_config->FindB("APT::Get::Force-Yes",false) == true)
1030 {
1031 return true;
1032 }
1033
1034 return _error->Error(_("There are problems and -y was used without --force-yes"));
1035}
0a8e3465 1036 /*}}}*/
0a8e3465
AL
1037// InstallPackages - Actually download and install the packages /*{{{*/
1038// ---------------------------------------------------------------------
1039/* This displays the informative messages describing what is going to
1040 happen and then calls the download routines */
a3eaf954 1041bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
80fbda96 1042 bool Safety = true)
0a8e3465 1043{
fc4b5c9f
AL
1044 if (_config->FindB("APT::Get::Purge",false) == true)
1045 {
1046 pkgCache::PkgIterator I = Cache->PkgBegin();
1047 for (; I.end() == false; I++)
d556d1a1
AL
1048 {
1049 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
1050 Cache->MarkDelete(I,true);
1051 }
fc4b5c9f
AL
1052 }
1053
83d89a9f 1054 bool Fail = false;
6f86c974 1055 bool Essential = false;
83d89a9f 1056
a6568219 1057 // Show all the various warning indicators
0a8e3465
AL
1058 ShowDel(c1out,Cache);
1059 ShowNew(c1out,Cache);
1060 if (ShwKept == true)
1061 ShowKept(c1out,Cache);
7a215bee 1062 Fail |= !ShowHold(c1out,Cache);
906fbf88 1063 if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
0a8e3465 1064 ShowUpgraded(c1out,Cache);
b2e465d6 1065 Fail |= !ShowDowngraded(c1out,Cache);
5d1d0738
AL
1066 if (_config->FindB("APT::Get::Download-Only",false) == false)
1067 Essential = !ShowEssential(c1out,Cache);
6f86c974 1068 Fail |= Essential;
0a8e3465 1069 Stats(c1out,Cache);
7db98ffc 1070
0a8e3465 1071 // Sanity check
d38b7b3d 1072 if (Cache->BrokenCount() != 0)
0a8e3465 1073 {
421c8d10 1074 ShowBroken(c1out,Cache,false);
2a7e07c7 1075 return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
0a8e3465
AL
1076 }
1077
d0c59649 1078 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
d38b7b3d 1079 Cache->BadCount() == 0)
c60d151b 1080 return true;
03e39e59 1081
d150b09d 1082 // No remove flag
b2e465d6 1083 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
db0db9fe 1084 return _error->Error(_("Packages need to be removed but remove is disabled."));
d150b09d 1085
03e39e59
AL
1086 // Run the simulator ..
1087 if (_config->FindB("APT::Get::Simulate") == true)
1088 {
1089 pkgSimulate PM(Cache);
2a7e07c7
MV
1090 int status_fd = _config->FindI("APT::Status-Fd",-1);
1091 pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd);
281daf46
AL
1092 if (Res == pkgPackageManager::Failed)
1093 return false;
1094 if (Res != pkgPackageManager::Completed)
2a7e07c7 1095 return _error->Error(_("Internal error, Ordering didn't finish"));
281daf46 1096 return true;
03e39e59
AL
1097 }
1098
1099 // Create the text record parser
1100 pkgRecords Recs(Cache);
83d89a9f
AL
1101 if (_error->PendingError() == true)
1102 return false;
1cd1c398 1103
03e39e59 1104 // Create the download object
1cd1c398 1105 pkgAcquire Fetcher;
03e39e59 1106 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
a722b2c5
DK
1107 if (_config->FindB("APT::Get::Print-URIs", false) == true)
1108 {
1109 // force a hashsum for compatibility reasons
1110 _config->CndSet("Acquire::ForceHash", "md5sum");
a722b2c5
DK
1111 }
1112 else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false)
1cd1c398 1113 return false;
03e39e59
AL
1114
1115 // Read the source list
1bb8cd67
DK
1116 if (Cache.BuildSourceList() == false)
1117 return false;
1118 pkgSourceList *List = Cache.GetSourceList();
03e39e59
AL
1119
1120 // Create the package manager and prepare to download
b2e465d6 1121 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
1bb8cd67 1122 if (PM->GetArchives(&Fetcher,List,&Recs) == false ||
424c3bc0 1123 _error->PendingError() == true)
03e39e59
AL
1124 return false;
1125
7a1b1f8b 1126 // Display statistics
3a882565
DK
1127 unsigned long long FetchBytes = Fetcher.FetchNeeded();
1128 unsigned long long FetchPBytes = Fetcher.PartialPresent();
1129 unsigned long long DebBytes = Fetcher.TotalNeeded();
d38b7b3d
AL
1130 if (DebBytes != Cache->DebSize())
1131 {
1132 c0out << DebBytes << ',' << Cache->DebSize() << endl;
2a7e07c7 1133 c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << endl;
d38b7b3d 1134 }
138d4b3d 1135
7a1b1f8b 1136 // Number of bytes
a6568219 1137 if (DebBytes != FetchBytes)
4d8d8112
DK
1138 //TRANSLATOR: The required space between number and unit is already included
1139 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
ac7fd99c 1140 ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
b2e465d6 1141 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
813603a0 1142 else if (DebBytes != 0)
4d8d8112
DK
1143 //TRANSLATOR: The required space between number and unit is already included
1144 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
ac7fd99c 1145 ioprintf(c1out,_("Need to get %sB of archives.\n"),
b2e465d6
AL
1146 SizeToStr(DebBytes).c_str());
1147
10bb1f5f
AL
1148 // Size delta
1149 if (Cache->UsrSize() >= 0)
4d8d8112
DK
1150 //TRANSLATOR: The required space between number and unit is already included
1151 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
813603a0 1152 ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
b2e465d6 1153 SizeToStr(Cache->UsrSize()).c_str());
10bb1f5f 1154 else
4d8d8112
DK
1155 //TRANSLATOR: The required space between number and unit is already included
1156 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
813603a0 1157 ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
b2e465d6 1158 SizeToStr(-1*Cache->UsrSize()).c_str());
10bb1f5f
AL
1159
1160 if (_error->PendingError() == true)
1161 return false;
31a0531d 1162
01b64152
AL
1163 /* Check for enough free space, but only if we are actually going to
1164 download */
18e20d63
AL
1165 if (_config->FindB("APT::Get::Print-URIs") == false &&
1166 _config->FindB("APT::Get::Download",true) == true)
01b64152
AL
1167 {
1168 struct statvfs Buf;
1169 string OutputDir = _config->FindDir("Dir::Cache::Archives");
c1ce032a
DK
1170 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
1171 if (errno == EOVERFLOW)
1172 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
1173 OutputDir.c_str());
1174 else
1175 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
1176 OutputDir.c_str());
1177 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
885d204b
OS
1178 {
1179 struct statfs Stat;
f64196e8
DK
1180 if (statfs(OutputDir.c_str(),&Stat) != 0
1181#if HAVE_STRUCT_STATFS_F_TYPE
1182 || unsigned(Stat.f_type) != RAMFS_MAGIC
1183#endif
1184 )
885d204b
OS
1185 return _error->Error(_("You don't have enough free space in %s."),
1186 OutputDir.c_str());
1187 }
01b64152
AL
1188 }
1189
83d89a9f 1190 // Fail safe check
0c95c765
AL
1191 if (_config->FindI("quiet",0) >= 2 ||
1192 _config->FindB("APT::Get::Assume-Yes",false) == true)
83d89a9f
AL
1193 {
1194 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
b2e465d6 1195 return _error->Error(_("There are problems and -y was used without --force-yes"));
83d89a9f 1196 }
83d89a9f 1197
80fbda96 1198 if (Essential == true && Safety == true)
6f86c974 1199 {
d150b09d 1200 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
b2e465d6 1201 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
d150b09d 1202
b2e465d6
AL
1203 const char *Prompt = _("Yes, do as I say!");
1204 ioprintf(c2out,
080bf1be 1205 _("You are about to do something potentially harmful.\n"
b2e465d6
AL
1206 "To continue type in the phrase '%s'\n"
1207 " ?] "),Prompt);
1208 c2out << flush;
1209 if (AnalPrompt(Prompt) == false)
6f86c974 1210 {
b2e465d6 1211 c2out << _("Abort.") << endl;
a6568219 1212 exit(1);
6f86c974
AL
1213 }
1214 }
1215 else
d150b09d 1216 {
6f86c974 1217 // Prompt to continue
38262e68 1218 if (Ask == true || Fail == true)
6f86c974 1219 {
d150b09d 1220 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
b2e465d6 1221 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
d150b09d 1222
0c95c765 1223 if (_config->FindI("quiet",0) < 2 &&
6f86c974 1224 _config->FindB("APT::Get::Assume-Yes",false) == false)
0c95c765 1225 {
db0db9fe 1226 c2out << _("Do you want to continue [Y/n]? ") << flush;
6f86c974 1227
0c95c765
AL
1228 if (YnPrompt() == false)
1229 {
b2e465d6 1230 c2out << _("Abort.") << endl;
0c95c765
AL
1231 exit(1);
1232 }
1233 }
6f86c974
AL
1234 }
1235 }
1236
36375005 1237 // Just print out the uris an exit if the --print-uris flag was used
f7a08e33
AL
1238 if (_config->FindB("APT::Get::Print-URIs") == true)
1239 {
1240 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1241 for (; I != Fetcher.UriEnd(); I++)
1242 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 1243 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
f7a08e33
AL
1244 return true;
1245 }
b2e465d6 1246
7db98ffc
MZ
1247 if (!CheckAuth(Fetcher))
1248 return false;
1249
b2e465d6
AL
1250 /* Unlock the dpkg lock if we are not going to be doing an install
1251 after. */
1252 if (_config->FindB("APT::Get::Download-Only",false) == true)
1253 _system->UnLock();
83d89a9f 1254
03e39e59 1255 // Run it
281daf46 1256 while (1)
30e1eab5 1257 {
a3eaf954 1258 bool Transient = false;
b2e465d6 1259 if (_config->FindB("APT::Get::Download",true) == false)
a3eaf954 1260 {
076d01b0 1261 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
a3eaf954
AL
1262 {
1263 if ((*I)->Local == true)
1264 {
1265 I++;
1266 continue;
1267 }
1268
1269 // Close the item and check if it was found in cache
1270 (*I)->Finished();
1271 if ((*I)->Complete == false)
1272 Transient = true;
1273
1274 // Clear it out of the fetch list
1275 delete *I;
1276 I = Fetcher.ItemsBegin();
1277 }
1278 }
1279
1280 if (Fetcher.Run() == pkgAcquire::Failed)
1281 return false;
30e1eab5 1282
281daf46
AL
1283 // Print out errors
1284 bool Failed = false;
076d01b0 1285 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
f01fe790 1286 {
281daf46
AL
1287 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1288 (*I)->Complete == true)
1289 continue;
1290
281daf46
AL
1291 if ((*I)->Status == pkgAcquire::Item::StatIdle)
1292 {
1293 Transient = true;
1294 // Failed = true;
1295 continue;
1296 }
a3eaf954 1297
b2e465d6
AL
1298 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1299 (*I)->ErrorText.c_str());
f01fe790 1300 Failed = true;
f01fe790 1301 }
5ec427c2 1302
a3eaf954 1303 /* If we are in no download mode and missing files and there were
5ec427c2
AL
1304 'failures' then the user must specify -m. Furthermore, there
1305 is no such thing as a transient error in no-download mode! */
a3eaf954 1306 if (Transient == true &&
b2e465d6 1307 _config->FindB("APT::Get::Download",true) == false)
5ec427c2
AL
1308 {
1309 Transient = false;
1310 Failed = true;
1311 }
f01fe790 1312
281daf46
AL
1313 if (_config->FindB("APT::Get::Download-Only",false) == true)
1314 {
1315 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
b2e465d6
AL
1316 return _error->Error(_("Some files failed to download"));
1317 c1out << _("Download complete and in download only mode") << endl;
281daf46
AL
1318 return true;
1319 }
1320
8195ae46 1321 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
f01fe790 1322 {
b2e465d6 1323 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
f01fe790
AL
1324 }
1325
281daf46 1326 if (Transient == true && Failed == true)
b2e465d6 1327 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
281daf46
AL
1328
1329 // Try to deal with missing package files
b2e465d6 1330 if (Failed == true && PM->FixMissing() == false)
281daf46 1331 {
b2e465d6 1332 cerr << _("Unable to correct missing packages.") << endl;
db0db9fe 1333 return _error->Error(_("Aborting install."));
281daf46 1334 }
afb1e2e3 1335
b2e465d6 1336 _system->UnLock();
2a7e07c7
MV
1337 int status_fd = _config->FindI("APT::Status-Fd",-1);
1338 pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd);
281daf46
AL
1339 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
1340 return false;
1341 if (Res == pkgPackageManager::Completed)
642ebc1a 1342 break;
281daf46
AL
1343
1344 // Reload the fetcher object and loop again for media swapping
1345 Fetcher.Shutdown();
1bb8cd67 1346 if (PM->GetArchives(&Fetcher,List,&Recs) == false)
281daf46 1347 return false;
b2e465d6
AL
1348
1349 _system->Lock();
642ebc1a
DK
1350 }
1351
1352 std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages();
1353 if (disappearedPkgs.empty() == true)
1354 return true;
1355
1356 string disappear;
1357 for (std::set<std::string>::const_iterator d = disappearedPkgs.begin();
1358 d != disappearedPkgs.end(); ++d)
1359 disappear.append(*d).append(" ");
1360
1361 ShowList(c1out, P_("The following package disappeared from your system as\n"
1362 "all files have been overwritten by other packages:",
1363 "The following packages disappeared from your system as\n"
1364 "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, "");
1365 c0out << _("Note: This is done automatic and on purpose by dpkg.") << std::endl;
1366
1367 return true;
0a8e3465
AL
1368}
1369 /*}}}*/
b8ad5512 1370// TryToInstallBuildDep - Try to install a single package /*{{{*/
c373c37a
AL
1371// ---------------------------------------------------------------------
1372/* This used to be inlined in DoInstall, but with the advent of regex package
1373 name matching it was split out.. */
21d4c9f1 1374bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
c373c37a 1375 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
70e706ad 1376 bool AllowFail = true)
c373c37a 1377{
21d4c9f1 1378 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
c373c37a 1379 {
21d4c9f1
DK
1380 CacheSetHelperAPTGet helper(c1out);
1381 helper.showErrors(AllowFail == false);
1382 pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
1383 if (Ver.end() == false)
1384 Pkg = Ver.ParentPkg();
1385 else if (helper.showVirtualPackageErrors(Cache) == false)
1386 return AllowFail;
c373c37a 1387 }
61d6a8de 1388
c373c37a
AL
1389 if (Remove == true)
1390 {
21d4c9f1
DK
1391 TryToRemove RemoveAction(Cache, Fix);
1392 RemoveAction(Pkg.VersionList());
1393 } else if (Cache[Pkg].CandidateVer != 0) {
1394 TryToInstall InstallAction(Cache, Fix, BrokenFix);
1395 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
6806db8a 1396 InstallAction.doAutoInstall();
21d4c9f1
DK
1397 } else
1398 return AllowFail;
60681f93 1399
b2e465d6
AL
1400 return true;
1401}
1402 /*}}}*/
1403// FindSrc - Find a source record /*{{{*/
1404// ---------------------------------------------------------------------
1405/* */
1406pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
1407 pkgSrcRecords &SrcRecs,string &Src,
1408 pkgDepCache &Cache)
1409{
b2e465d6 1410 string VerTag;
fb3dc579 1411 string DefRel = _config->Find("APT::Default-Release");
b2e465d6 1412 string TmpSrc = Name;
aca056a9 1413
fb3dc579
MV
1414 // extract the version/release from the pkgname
1415 const size_t found = TmpSrc.find_last_of("/=");
1416 if (found != string::npos) {
1417 if (TmpSrc[found] == '/')
1418 DefRel = TmpSrc.substr(found+1);
1419 else
1420 VerTag = TmpSrc.substr(found+1);
ebf6c42d
DK
1421 TmpSrc = TmpSrc.substr(0,found);
1422 }
aca056a9 1423
fb3dc579
MV
1424 /* Lookup the version of the package we would install if we were to
1425 install a version and determine the source package name, then look
1426 in the archive for a source package of the same name. */
1427 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
1428 const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
1429 if (MatchSrcOnly == false && Pkg.end() == false)
aca056a9 1430 {
fb3dc579 1431 if(VerTag.empty() == false || DefRel.empty() == false)
aca056a9 1432 {
e84adb76 1433 bool fuzzy = false;
fb3dc579
MV
1434 // we have a default release, try to locate the pkg. we do it like
1435 // this because GetCandidateVer() will not "downgrade", that means
1436 // "apt-get source -t stable apt" won't work on a unstable system
e84adb76 1437 for (pkgCache::VerIterator Ver = Pkg.VersionList();; Ver++)
aca056a9 1438 {
e84adb76
DK
1439 // try first only exact matches, later fuzzy matches
1440 if (Ver.end() == true)
1441 {
1442 if (fuzzy == true)
1443 break;
1444 fuzzy = true;
1445 Ver = Pkg.VersionList();
259f688a
MV
1446 // exit right away from the Pkg.VersionList() loop if we
1447 // don't have any versions
1448 if (Ver.end() == true)
1449 break;
e84adb76
DK
1450 }
1451 // We match against a concrete version (or a part of this version)
1452 if (VerTag.empty() == false &&
1453 (fuzzy == true || Cache.VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
1454 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
1455 continue;
1456
fb3dc579
MV
1457 for (pkgCache::VerFileIterator VF = Ver.FileList();
1458 VF.end() == false; VF++)
aca056a9 1459 {
fb3dc579
MV
1460 /* If this is the status file, and the current version is not the
1461 version in the status file (ie it is not installed, or somesuch)
1462 then it is not a candidate for installation, ever. This weeds
1463 out bogus entries that may be due to config-file states, or
1464 other. */
1465 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
1466 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
1467 continue;
1468
fb3dc579
MV
1469 // or we match against a release
1470 if(VerTag.empty() == false ||
1471 (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
1472 (VF.File().Codename() != 0 && VF.File().Codename() == DefRel))
1473 {
1474 pkgRecords::Parser &Parse = Recs.Lookup(VF);
1475 Src = Parse.SourcePkg();
61690a7e
MV
1476 // no SourcePkg name, so it is the "binary" name
1477 if (Src.empty() == true)
1478 Src = TmpSrc;
e84adb76
DK
1479 // the Version we have is possibly fuzzy or includes binUploads,
1480 // so we use the Version of the SourcePkg (empty if same as package)
1481 VerTag = Parse.SourceVer();
61690a7e
MV
1482 if (VerTag.empty() == true)
1483 VerTag = Ver.VerStr();
fb3dc579
MV
1484 break;
1485 }
aca056a9 1486 }
61690a7e
MV
1487 if (Src.empty() == false)
1488 break;
aca056a9 1489 }
fb3dc579
MV
1490 if (Src.empty() == true)
1491 {
61690a7e 1492 // Sources files have no codename information
ddff663f
MV
1493 if (VerTag.empty() == true && DefRel.empty() == false)
1494 {
1495 _error->Error(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
1496 return 0;
1497 }
fb3dc579 1498 }
026f60e2 1499 }
61690a7e 1500 if (Src.empty() == true)
b2e465d6 1501 {
61690a7e
MV
1502 // if we don't have found a fitting package yet so we will
1503 // choose a good candidate and proceed with that.
1504 // Maybe we will find a source later on with the right VerTag
ce6162be 1505 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
fb3dc579 1506 if (Ver.end() == false)
b2e465d6
AL
1507 {
1508 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1509 Src = Parse.SourcePkg();
61690a7e
MV
1510 if (VerTag.empty() == true)
1511 VerTag = Parse.SourceVer();
b2e465d6 1512 }
fb3dc579
MV
1513 }
1514 }
1515
1516 if (Src.empty() == true)
1517 Src = TmpSrc;
1518 else
1519 {
1520 /* if we have a source pkg name, make sure to only search
1521 for srcpkg names, otherwise apt gets confused if there
1522 is a binary package "pkg1" and a source package "pkg1"
1523 with the same name but that comes from different packages */
1524 MatchSrcOnly = true;
1525 if (Src != TmpSrc)
1526 {
1527 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
1528 }
b2e465d6 1529 }
89ad8e7c 1530
b2e465d6
AL
1531 // The best hit
1532 pkgSrcRecords::Parser *Last = 0;
1533 unsigned long Offset = 0;
1534 string Version;
89ad8e7c 1535
b2e465d6
AL
1536 /* Iterate over all of the hits, which includes the resulting
1537 binary packages in the search */
1538 pkgSrcRecords::Parser *Parse;
fb3dc579 1539 while (true)
b2e465d6 1540 {
fb3dc579
MV
1541 SrcRecs.Restart();
1542 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
b2e465d6 1543 {
fb3dc579
MV
1544 const string Ver = Parse->Version();
1545
1546 // Ignore all versions which doesn't fit
e84adb76
DK
1547 if (VerTag.empty() == false &&
1548 Cache.VS().CmpVersion(VerTag, Ver) != 0) // exact match
b2e465d6 1549 continue;
fb3dc579
MV
1550
1551 // Newer version or an exact match? Save the hit
1552 if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
1553 Last = Parse;
1554 Offset = Parse->Offset();
1555 Version = Ver;
1556 }
1557
1558 // was the version check above an exact match? If so, we don't need to look further
1559 if (VerTag.empty() == false && VerTag.size() == Ver.size())
1560 break;
b2e465d6 1561 }
fb3dc579
MV
1562 if (Last != 0 || VerTag.empty() == true)
1563 break;
1564 //if (VerTag.empty() == false && Last == 0)
ddff663f
MV
1565 _error->Error(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
1566 return 0;
b2e465d6 1567 }
fb3dc579 1568
ce6162be 1569 if (Last == 0 || Last->Jump(Offset) == false)
b2e465d6 1570 return 0;
fb3dc579 1571
b2e465d6
AL
1572 return Last;
1573}
1574 /*}}}*/
0a8e3465
AL
1575// DoUpdate - Update the package lists /*{{{*/
1576// ---------------------------------------------------------------------
1577/* */
b2e465d6 1578bool DoUpdate(CommandLine &CmdL)
0a8e3465 1579{
b2e465d6
AL
1580 if (CmdL.FileSize() != 1)
1581 return _error->Error(_("The update command takes no arguments"));
1bb8cd67
DK
1582
1583 CacheFile Cache;
1584
0919e3f9 1585 // Get the source list
1bb8cd67 1586 if (Cache.BuildSourceList() == false)
0919e3f9 1587 return false;
1bb8cd67 1588 pkgSourceList *List = Cache.GetSourceList();
0919e3f9 1589
89b70b5a 1590 // Create the progress
0919e3f9 1591 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
89b70b5a 1592
f0863b21
AL
1593 // Just print out the uris an exit if the --print-uris flag was used
1594 if (_config->FindB("APT::Get::Print-URIs") == true)
1595 {
a722b2c5
DK
1596 // force a hashsum for compatibility reasons
1597 _config->CndSet("Acquire::ForceHash", "md5sum");
1598
89b70b5a 1599 // get a fetcher
1cd1c398
DK
1600 pkgAcquire Fetcher;
1601 if (Fetcher.Setup(&Stat) == false)
1602 return false;
89b70b5a 1603
7db98ffc
MZ
1604 // Populate it with the source selection and get all Indexes
1605 // (GetAll=true)
1bb8cd67 1606 if (List->GetIndexes(&Fetcher,true) == false)
7db98ffc
MZ
1607 return false;
1608
f0863b21
AL
1609 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1610 for (; I != Fetcher.UriEnd(); I++)
1611 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 1612 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
f0863b21
AL
1613 return true;
1614 }
7db98ffc 1615
89b70b5a 1616 // do the work
e88d983a 1617 if (_config->FindB("APT::Get::Download",true) == true)
1bb8cd67 1618 ListUpdate(Stat, *List);
e88d983a 1619
89b70b5a 1620 // Rebuild the cache.
0077d829 1621 if (Cache.BuildCaches() == false)
0919e3f9
AL
1622 return false;
1623
1624 return true;
afb1e2e3
MV
1625}
1626 /*}}}*/
1627// DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
1628// ---------------------------------------------------------------------
1629/* Remove unused automatic packages */
1630bool DoAutomaticRemove(CacheFile &Cache)
1631{
9d2938d4 1632 bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
b255ff1a 1633 bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
7898bd97 1634 bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
afb1e2e3 1635
03dbbc98 1636 pkgDepCache::ActionGroup group(*Cache);
9d2938d4 1637 if(Debug)
120365ce 1638 std::cout << "DoAutomaticRemove()" << std::endl;
afb1e2e3 1639
03dbbc98
DK
1640 if (doAutoRemove == true &&
1641 _config->FindB("APT::Get::Remove",true) == false)
afb1e2e3 1642 {
3d0de656
MV
1643 c1out << _("We are not supposed to delete stuff, can't start "
1644 "AutoRemover") << std::endl;
03dbbc98 1645 return false;
3d0de656 1646 }
74a05226 1647
03dbbc98
DK
1648 bool purgePkgs = _config->FindB("APT::Get::Purge", false);
1649 bool smallList = (hideAutoRemove == false &&
a8dfff90
DK
1650 strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0) ||
1651 // we don't want to autoremove and we don't want to see it, so don't generate lists
1652 (doAutoRemove == false && hideAutoRemove == true);
03dbbc98 1653
9d2938d4 1654 string autoremovelist, autoremoveversions;
03dbbc98 1655 unsigned long autoRemoveCount = 0;
9d2938d4
MV
1656 // look over the cache to see what can be removed
1657 for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); ! Pkg.end(); ++Pkg)
afb1e2e3 1658 {
9d2938d4
MV
1659 if (Cache[Pkg].Garbage)
1660 {
1661 if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
1662 if(Debug)
75ce2062 1663 std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl;
03dbbc98 1664
3d0de656 1665 if (doAutoRemove)
9d2938d4
MV
1666 {
1667 if(Pkg.CurrentVer() != 0 &&
1668 Pkg->CurrentState != pkgCache::State::ConfigFiles)
03dbbc98 1669 Cache->MarkDelete(Pkg, purgePkgs);
9d2938d4 1670 else
74a05226 1671 Cache->MarkKeep(Pkg, false, false);
9d2938d4 1672 }
03dbbc98
DK
1673 else
1674 {
a8dfff90
DK
1675 // if the package is a new install and already garbage we don't need to
1676 // install it in the first place, so nuke it instead of show it
1677 if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0)
1678 Cache->MarkDelete(Pkg, false);
03dbbc98 1679 // only show stuff in the list that is not yet marked for removal
a8dfff90 1680 else if(Cache[Pkg].Delete() == false)
03dbbc98 1681 {
f0f2f956 1682 ++autoRemoveCount;
03dbbc98 1683 // we don't need to fill the strings if we don't need them
f0f2f956 1684 if (smallList == false)
03dbbc98 1685 {
75ce2062 1686 autoremovelist += Pkg.FullName(true) + " ";
03dbbc98
DK
1687 autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
1688 }
1689 }
1690 }
9d2938d4 1691 }
afb1e2e3 1692 }
a8dfff90
DK
1693
1694 // Now see if we had destroyed anything (if we had done anything)
1695 if (Cache->BrokenCount() != 0)
1696 {
1697 c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
1698 "shouldn't happen. Please file a bug report against apt.") << endl;
1699 c1out << endl;
1700 c1out << _("The following information may help to resolve the situation:") << endl;
1701 c1out << endl;
1702 ShowBroken(c1out,Cache,false);
1703
1704 return _error->Error(_("Internal Error, AutoRemover broke stuff"));
1705 }
1706
03dbbc98
DK
1707 // if we don't remove them, we should show them!
1708 if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
1709 {
1710 if (smallList == false)
d204fc7a 1711 ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
f0f2f956
DK
1712 "The following packages were automatically installed and are no longer required:",
1713 autoRemoveCount), autoremovelist, autoremoveversions);
03dbbc98 1714 else
f0f2f956
DK
1715 ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
1716 "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
9d2938d4 1717 c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
03dbbc98 1718 }
afb1e2e3 1719 return true;
db1e7193 1720}
92fcbfc1 1721 /*}}}*/
db1e7193
MV
1722// DoUpgrade - Upgrade all packages /*{{{*/
1723// ---------------------------------------------------------------------
1724/* Upgrade all packages without installing new packages or erasing old
1725 packages */
1726bool DoUpgrade(CommandLine &CmdL)
1727{
1728 CacheFile Cache;
1729 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1730 return false;
1731
1732 // Do the upgrade
1733 if (pkgAllUpgrade(Cache) == false)
1734 {
1735 ShowBroken(c1out,Cache,false);
1736 return _error->Error(_("Internal error, AllUpgrade broke stuff"));
1737 }
1738
1739 return InstallPackages(Cache,true);
afb1e2e3
MV
1740}
1741 /*}}}*/
0a8e3465
AL
1742// DoInstall - Install packages from the command line /*{{{*/
1743// ---------------------------------------------------------------------
1744/* Install named packages */
1745bool DoInstall(CommandLine &CmdL)
1746{
1747 CacheFile Cache;
c37b9502
AL
1748 if (Cache.OpenForInstall() == false ||
1749 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
0a8e3465
AL
1750 return false;
1751
7c57fe64
AL
1752 // Enter the special broken fixing mode if the user specified arguments
1753 bool BrokenFix = false;
1754 if (Cache->BrokenCount() != 0)
1755 BrokenFix = true;
1756
0a8e3465 1757 pkgProblemResolver Fix(Cache);
31367812 1758
e67c0834
DK
1759 static const unsigned short MOD_REMOVE = 1;
1760 static const unsigned short MOD_INSTALL = 2;
1761
1762 unsigned short fallback = MOD_INSTALL;
303a1703 1763 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
e67c0834 1764 fallback = MOD_REMOVE;
e47c7d16
MV
1765 else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
1766 {
1767 _config->Set("APT::Get::Purge", true);
e67c0834 1768 fallback = MOD_REMOVE;
e47c7d16 1769 }
74a05226 1770 else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
0a8e3465 1771 {
54668e4e 1772 _config->Set("APT::Get::AutomaticRemove", "true");
e67c0834 1773 fallback = MOD_REMOVE;
54668e4e 1774 }
70e706ad
DK
1775
1776 std::list<APT::VersionSet::Modifier> mods;
e67c0834 1777 mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+",
b8ad5512 1778 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE));
e67c0834 1779 mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-",
b8ad5512 1780 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST));
70e706ad
DK
1781 CacheSetHelperAPTGet helper(c0out);
1782 std::map<unsigned short, APT::VersionSet> verset = APT::VersionSet::GroupedFromCommandLine(Cache,
1783 CmdL.FileList + 1, mods, fallback, helper);
31367812 1784
70e706ad 1785 if (_error->PendingError() == true)
b8ad5512
DK
1786 {
1787 helper.showVirtualPackageErrors(Cache);
70e706ad 1788 return false;
b8ad5512 1789 }
31367812 1790
e67c0834
DK
1791 unsigned short order[] = { 0, 0, 0 };
1792 if (fallback == MOD_INSTALL) {
1793 order[0] = MOD_INSTALL;
1794 order[1] = MOD_REMOVE;
1795 } else {
1796 order[0] = MOD_REMOVE;
1797 order[1] = MOD_INSTALL;
1798 }
1799
b8ad5512
DK
1800 TryToInstall InstallAction(Cache, Fix, BrokenFix);
1801 TryToRemove RemoveAction(Cache, Fix);
1802
70e706ad
DK
1803 // new scope for the ActionGroup
1804 {
1805 pkgDepCache::ActionGroup group(Cache);
b8ad5512 1806
e67c0834 1807 for (unsigned short i = 0; order[i] != 0; ++i)
31367812 1808 {
6806db8a 1809 if (order[i] == MOD_INSTALL) {
b8ad5512 1810 InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
2c085486 1811 InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
6806db8a
DK
1812 InstallAction.doAutoInstall();
1813 }
e67c0834 1814 else if (order[i] == MOD_REMOVE)
b8ad5512 1815 RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
54668e4e 1816 }
0a8e3465 1817
31367812
DK
1818 if (_error->PendingError() == true)
1819 return false;
1820
54668e4e
MV
1821 /* If we are in the Broken fixing mode we do not attempt to fix the
1822 problems. This is if the user invoked install without -f and gave
1823 packages */
1824 if (BrokenFix == true && Cache->BrokenCount() != 0)
1825 {
b5647402 1826 c1out << _("You might want to run 'apt-get -f install' to correct these:") << endl;
54668e4e 1827 ShowBroken(c1out,Cache,false);
7c57fe64 1828
54668e4e
MV
1829 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
1830 }
7c57fe64 1831
54668e4e
MV
1832 // Call the scored problem resolver
1833 Fix.InstallProtect();
1834 if (Fix.Resolve(true) == false)
1835 _error->Discard();
0a8e3465 1836
54668e4e
MV
1837 // Now we check the state of the packages,
1838 if (Cache->BrokenCount() != 0)
303a1703 1839 {
b2e465d6 1840 c1out <<
54668e4e
MV
1841 _("Some packages could not be installed. This may mean that you have\n"
1842 "requested an impossible situation or if you are using the unstable\n"
1843 "distribution that some required packages have not yet been created\n"
1844 "or been moved out of Incoming.") << endl;
ecd414ef 1845 /*
54668e4e
MV
1846 if (Packages == 1)
1847 {
1848 c1out << endl;
1849 c1out <<
1850 _("Since you only requested a single operation it is extremely likely that\n"
1851 "the package is simply not installable and a bug report against\n"
1852 "that package should be filed.") << endl;
1853 }
ecd414ef 1854 */
303a1703 1855
54668e4e
MV
1856 c1out << _("The following information may help to resolve the situation:") << endl;
1857 c1out << endl;
1858 ShowBroken(c1out,Cache,false);
1859 return _error->Error(_("Broken packages"));
1860 }
120365ce 1861 }
5a68ea79
MV
1862 if (!DoAutomaticRemove(Cache))
1863 return false;
afb1e2e3 1864
0a8e3465
AL
1865 /* Print out a list of packages that are going to be installed extra
1866 to what the user asked */
e67c0834 1867 if (Cache->InstCount() != verset[MOD_INSTALL].size())
0a8e3465
AL
1868 {
1869 string List;
ac625538 1870 string VersionsList;
1089ca89 1871 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 1872 {
1089ca89 1873 pkgCache::PkgIterator I(Cache,Cache.List[J]);
0a8e3465
AL
1874 if ((*Cache)[I].Install() == false)
1875 continue;
6a2512be
DK
1876 pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache);
1877 if (Cand.Pseudo() == true)
1878 continue;
0a8e3465 1879
6a2512be
DK
1880 if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end())
1881 continue;
1882
1883 List += I.FullName(true) + " ";
1884 VersionsList += string(Cache[I].CandVersion) + "\n";
0a8e3465
AL
1885 }
1886
ac625538 1887 ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
0a8e3465
AL
1888 }
1889
a7e41689
AL
1890 /* Print out a list of suggested and recommended packages */
1891 {
1892 string SuggestsList, RecommendsList, List;
72122b62 1893 string SuggestsVersions, RecommendsVersions;
a7e41689
AL
1894 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1895 {
29f37db8 1896 pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
a7e41689
AL
1897
1898 /* Just look at the ones we want to install */
29f37db8 1899 if ((*Cache)[Pkg].Install() == false)
a7e41689
AL
1900 continue;
1901
29f37db8
MV
1902 // get the recommends/suggests for the candidate ver
1903 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
1904 for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
1905 {
1906 pkgCache::DepIterator Start;
1907 pkgCache::DepIterator End;
1908 D.GlobOr(Start,End); // advances D
1909
cd33a786
MV
1910 // FIXME: we really should display a or-group as a or-group to the user
1911 // the problem is that ShowList is incapable of doing this
29f37db8
MV
1912 string RecommendsOrList,RecommendsOrVersions;
1913 string SuggestsOrList,SuggestsOrVersions;
1914 bool foundInstalledInOrGroup = false;
1915 for(;;)
1916 {
1917 /* Skip if package is installed already, or is about to be */
75ce2062 1918 string target = Start.TargetPkg().FullName(true) + " ";
2d847a59
DK
1919 pkgCache::PkgIterator const TarPkg = Start.TargetPkg();
1920 if (TarPkg->SelectedState == pkgCache::State::Install ||
d1aa9162 1921 TarPkg->SelectedState == pkgCache::State::Hold ||
2d847a59 1922 Cache[Start.TargetPkg()].Install())
29f37db8
MV
1923 {
1924 foundInstalledInOrGroup=true;
1925 break;
1926 }
1927
1928 /* Skip if we already saw it */
1929 if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
1930 {
1931 foundInstalledInOrGroup=true;
1932 break;
1933 }
1934
1935 // this is a dep on a virtual pkg, check if any package that provides it
1936 // should be installed
1937 if(Start.TargetPkg().ProvidesList() != 0)
1938 {
1939 pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
1940 for (; I.end() == false; I++)
1941 {
1942 pkgCache::PkgIterator Pkg = I.OwnerPkg();
1943 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
1944 Pkg.CurrentVer() != 0)
1945 foundInstalledInOrGroup=true;
1946 }
1947 }
1948
1949 if (Start->Type == pkgCache::Dep::Suggests)
1950 {
1951 SuggestsOrList += target;
1952 SuggestsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
1953 }
1954
1955 if (Start->Type == pkgCache::Dep::Recommends)
1956 {
1957 RecommendsOrList += target;
1958 RecommendsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
1959 }
1960
1961 if (Start >= End)
1962 break;
1963 Start++;
1964 }
1965
1966 if(foundInstalledInOrGroup == false)
1967 {
1968 RecommendsList += RecommendsOrList;
1969 RecommendsVersions += RecommendsOrVersions;
1970 SuggestsList += SuggestsOrList;
1971 SuggestsVersions += SuggestsOrVersions;
1972 }
1973
1974 }
a7e41689 1975 }
29f37db8 1976
72122b62
AL
1977 ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
1978 ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
a7e41689
AL
1979
1980 }
1981
e4b74e4b
MV
1982 // if nothing changed in the cache, but only the automark information
1983 // we write the StateFile here, otherwise it will be written in
1984 // cache.commit()
b8ad5512 1985 if (InstallAction.AutoMarkChanged > 0 &&
e4b74e4b 1986 Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
9964a721
MV
1987 Cache->BadCount() == 0 &&
1988 _config->FindB("APT::Get::Simulate",false) == false)
e4b74e4b
MV
1989 Cache->writeStateFile(NULL);
1990
03e39e59 1991 // See if we need to prompt
70e706ad 1992 // FIXME: check if really the packages in the set are going to be installed
e67c0834 1993 if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0)
2c3bc8bb 1994 return InstallPackages(Cache,false,false);
13e8426f 1995
03e39e59 1996 return InstallPackages(Cache,false);
0a8e3465 1997}
d63a1458 1998
d63a1458
JAK
1999/* mark packages as automatically/manually installed. */
2000bool DoMarkAuto(CommandLine &CmdL)
2001{
2002 bool Action = true;
2003 int AutoMarkChanged = 0;
2004 OpTextProgress progress;
2005 CacheFile Cache;
2006 if (Cache.Open() == false)
2007 return false;
2008
2009 if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
2010 Action = true;
2011 else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
2012 Action = false;
2013
2014 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
2015 {
2016 const char *S = *I;
2017 // Locate the package
2018 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
2019 if (Pkg.end() == true) {
2020 return _error->Error(_("Couldn't find package %s"),S);
2021 }
2022 else
2023 {
2024 if (!Action)
2025 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
2026 else
2027 ioprintf(c1out,_("%s set to automatically installed.\n"),
2028 Pkg.Name());
2029
2030 Cache->MarkAuto(Pkg,Action);
2031 AutoMarkChanged++;
2032 }
2033 }
2034 if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
2035 return Cache->writeStateFile(NULL);
2036 return false;
2037}
0a8e3465
AL
2038 /*}}}*/
2039// DoDistUpgrade - Automatic smart upgrader /*{{{*/
2040// ---------------------------------------------------------------------
2041/* Intelligent upgrader that will install and remove packages at will */
2042bool DoDistUpgrade(CommandLine &CmdL)
2043{
2044 CacheFile Cache;
c37b9502 2045 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
0a8e3465
AL
2046 return false;
2047
db0db9fe 2048 c0out << _("Calculating upgrade... ") << flush;
0a8e3465
AL
2049 if (pkgDistUpgrade(*Cache) == false)
2050 {
b2e465d6 2051 c0out << _("Failed") << endl;
421c8d10 2052 ShowBroken(c1out,Cache,false);
0a8e3465
AL
2053 return false;
2054 }
2055
b2e465d6 2056 c0out << _("Done") << endl;
0a8e3465
AL
2057
2058 return InstallPackages(Cache,true);
2059}
2060 /*}}}*/
2061// DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
2062// ---------------------------------------------------------------------
2063/* Follows dselect's selections */
2064bool DoDSelectUpgrade(CommandLine &CmdL)
2065{
2066 CacheFile Cache;
c37b9502 2067 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
0a8e3465
AL
2068 return false;
2069
a4decc40
MV
2070 pkgDepCache::ActionGroup group(Cache);
2071
0a8e3465
AL
2072 // Install everything with the install flag set
2073 pkgCache::PkgIterator I = Cache->PkgBegin();
2074 for (;I.end() != true; I++)
2075 {
2076 /* Install the package only if it is a new install, the autoupgrader
2077 will deal with the rest */
2078 if (I->SelectedState == pkgCache::State::Install)
2079 Cache->MarkInstall(I,false);
2080 }
2081
2082 /* Now install their deps too, if we do this above then order of
2083 the status file is significant for | groups */
2084 for (I = Cache->PkgBegin();I.end() != true; I++)
2085 {
2086 /* Install the package only if it is a new install, the autoupgrader
2087 will deal with the rest */
2088 if (I->SelectedState == pkgCache::State::Install)
2f45c76a 2089 Cache->MarkInstall(I,true);
0a8e3465
AL
2090 }
2091
2092 // Apply erasures now, they override everything else.
2093 for (I = Cache->PkgBegin();I.end() != true; I++)
2094 {
2095 // Remove packages
2096 if (I->SelectedState == pkgCache::State::DeInstall ||
2097 I->SelectedState == pkgCache::State::Purge)
d556d1a1 2098 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
0a8e3465
AL
2099 }
2100
2f45c76a
AL
2101 /* Resolve any problems that dselect created, allupgrade cannot handle
2102 such things. We do so quite agressively too.. */
2103 if (Cache->BrokenCount() != 0)
2104 {
2105 pkgProblemResolver Fix(Cache);
2106
2107 // Hold back held packages.
b2e465d6 2108 if (_config->FindB("APT::Ignore-Hold",false) == false)
2f45c76a
AL
2109 {
2110 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
2111 {
2112 if (I->SelectedState == pkgCache::State::Hold)
2113 {
2114 Fix.Protect(I);
2115 Cache->MarkKeep(I);
2116 }
2117 }
2118 }
2119
2120 if (Fix.Resolve() == false)
2121 {
421c8d10 2122 ShowBroken(c1out,Cache,false);
2a7e07c7 2123 return _error->Error(_("Internal error, problem resolver broke stuff"));
2f45c76a
AL
2124 }
2125 }
2126
2127 // Now upgrade everything
0a8e3465
AL
2128 if (pkgAllUpgrade(Cache) == false)
2129 {
421c8d10 2130 ShowBroken(c1out,Cache,false);
2a7e07c7 2131 return _error->Error(_("Internal error, problem resolver broke stuff"));
0a8e3465
AL
2132 }
2133
2134 return InstallPackages(Cache,false);
2135}
2136 /*}}}*/
2137// DoClean - Remove download archives /*{{{*/
2138// ---------------------------------------------------------------------
2139/* */
2140bool DoClean(CommandLine &CmdL)
2141{
8b067c22
AL
2142 if (_config->FindB("APT::Get::Simulate") == true)
2143 {
2144 cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
2145 _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
2146 return true;
2147 }
2148
1b6d659c
AL
2149 // Lock the archive directory
2150 FileFd Lock;
2151 if (_config->FindB("Debug::NoLocking",false) == false)
2152 {
2153 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2154 if (_error->PendingError() == true)
b2e465d6 2155 return _error->Error(_("Unable to lock the download directory"));
1b6d659c
AL
2156 }
2157
7a1b1f8b
AL
2158 pkgAcquire Fetcher;
2159 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
2160 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
0a8e3465
AL
2161 return true;
2162}
2163 /*}}}*/
1bc849af
AL
2164// DoAutoClean - Smartly remove downloaded archives /*{{{*/
2165// ---------------------------------------------------------------------
2166/* This is similar to clean but it only purges things that cannot be
2167 downloaded, that is old versions of cached packages. */
65a1e968
AL
2168class LogCleaner : public pkgArchiveCleaner
2169{
2170 protected:
2171 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
2172 {
4cc8bab0 2173 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
c1e78ee5
AL
2174
2175 if (_config->FindB("APT::Get::Simulate") == false)
2176 unlink(File);
65a1e968
AL
2177 };
2178};
2179
1bc849af
AL
2180bool DoAutoClean(CommandLine &CmdL)
2181{
1b6d659c
AL
2182 // Lock the archive directory
2183 FileFd Lock;
2184 if (_config->FindB("Debug::NoLocking",false) == false)
2185 {
2186 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2187 if (_error->PendingError() == true)
b2e465d6 2188 return _error->Error(_("Unable to lock the download directory"));
1b6d659c
AL
2189 }
2190
1bc849af 2191 CacheFile Cache;
2d11135a 2192 if (Cache.Open() == false)
1bc849af
AL
2193 return false;
2194
65a1e968 2195 LogCleaner Cleaner;
1bc849af
AL
2196
2197 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
2198 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
2199}
2200 /*}}}*/
0a8e3465
AL
2201// DoCheck - Perform the check operation /*{{{*/
2202// ---------------------------------------------------------------------
2203/* Opening automatically checks the system, this command is mostly used
2204 for debugging */
2205bool DoCheck(CommandLine &CmdL)
2206{
2207 CacheFile Cache;
2208 Cache.Open();
2d11135a 2209 Cache.CheckDeps();
0a8e3465
AL
2210
2211 return true;
2212}
2213 /*}}}*/
36375005
AL
2214// DoSource - Fetch a source archive /*{{{*/
2215// ---------------------------------------------------------------------
2d11135a 2216/* Fetch souce packages */
fb0ee66e
AL
2217struct DscFile
2218{
2219 string Package;
2220 string Version;
2221 string Dsc;
2222};
2223
36375005
AL
2224bool DoSource(CommandLine &CmdL)
2225{
2226 CacheFile Cache;
2d11135a 2227 if (Cache.Open(false) == false)
36375005
AL
2228 return false;
2229
2d11135a 2230 if (CmdL.FileSize() <= 1)
b2e465d6 2231 return _error->Error(_("Must specify at least one package to fetch source for"));
2d11135a 2232
36375005 2233 // Read the source list
1bb8cd67
DK
2234 if (Cache.BuildSourceList() == false)
2235 return false;
2236 pkgSourceList *List = Cache.GetSourceList();
36375005
AL
2237
2238 // Create the text record parsers
2239 pkgRecords Recs(Cache);
1bb8cd67 2240 pkgSrcRecords SrcRecs(*List);
36375005
AL
2241 if (_error->PendingError() == true)
2242 return false;
2243
2244 // Create the download object
2245 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1cd1c398
DK
2246 pkgAcquire Fetcher;
2247 if (Fetcher.Setup(&Stat) == false)
2248 return false;
fb0ee66e
AL
2249
2250 DscFile *Dsc = new DscFile[CmdL.FileSize()];
36375005 2251
092ae175
MV
2252 // insert all downloaded uris into this set to avoid downloading them
2253 // twice
2254 set<string> queued;
8545b536
DK
2255
2256 // Diff only mode only fetches .diff files
2257 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
2258 // Tar only mode only fetches .tar files
2259 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
2260 // Dsc only mode only fetches .dsc files
2261 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
2262
36375005 2263 // Load the requestd sources into the fetcher
fb0ee66e
AL
2264 unsigned J = 0;
2265 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
36375005
AL
2266 {
2267 string Src;
b2e465d6 2268 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
36375005
AL
2269
2270 if (Last == 0)
b2e465d6 2271 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
36375005 2272
3238423f
MV
2273 string srec = Last->AsStr();
2274 string::size_type pos = srec.find("\nVcs-");
774a6687 2275 while (pos != string::npos)
3238423f
MV
2276 {
2277 pos += strlen("\nVcs-");
2278 string vcs = srec.substr(pos,srec.find(":",pos)-pos);
774a6687
MV
2279 if(vcs == "Browser")
2280 {
2281 pos = srec.find("\nVcs-", pos);
2282 continue;
2283 }
3238423f
MV
2284 pos += vcs.length()+2;
2285 string::size_type epos = srec.find("\n", pos);
2286 string uri = srec.substr(pos,epos-pos).c_str();
b799e134 2287 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
3238423f 2288 "the '%s' version control system at:\n"
8756297c 2289 "%s\n"),
3238423f
MV
2290 Src.c_str(), vcs.c_str(), uri.c_str());
2291 if(vcs == "Bzr")
927677f0
MV
2292 ioprintf(c1out,_("Please use:\n"
2293 "bzr get %s\n"
3d513bbd 2294 "to retrieve the latest (possibly unreleased) "
b799e134 2295 "updates to the package.\n"),
3238423f 2296 uri.c_str());
b799e134 2297 break;
3238423f
MV
2298 }
2299
36375005
AL
2300 // Back track
2301 vector<pkgSrcRecords::File> Lst;
b2e465d6 2302 if (Last->Files(Lst) == false)
36375005
AL
2303 return false;
2304
2305 // Load them into the fetcher
2306 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
2307 I != Lst.end(); I++)
2308 {
2309 // Try to guess what sort of file it is we are getting.
b2e465d6 2310 if (I->Type == "dsc")
fb0ee66e 2311 {
fb0ee66e
AL
2312 Dsc[J].Package = Last->Package();
2313 Dsc[J].Version = Last->Version();
2314 Dsc[J].Dsc = flNotDir(I->Path);
2315 }
092ae175 2316
8545b536
DK
2317 // Handle the only options so that multiple can be used at once
2318 if (diffOnly == true || tarOnly == true || dscOnly == true)
2319 {
2320 if ((diffOnly == true && I->Type == "diff") ||
2321 (tarOnly == true && I->Type == "tar") ||
2322 (dscOnly == true && I->Type == "dsc"))
2323 ; // Fine, we want this file downloaded
2324 else
2325 continue;
2326 }
1979e742 2327
092ae175
MV
2328 // don't download the same uri twice (should this be moved to
2329 // the fetcher interface itself?)
2330 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
2331 continue;
2332 queued.insert(Last->Index().ArchiveURI(I->Path));
2333
2334 // check if we have a file with that md5 sum already localy
2335 if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
2336 {
2337 FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
2338 MD5Summation sum;
2339 sum.AddFD(Fd.Fd(), Fd.Size());
2340 Fd.Close();
2341 if((string)sum.Result() == I->MD5Hash)
2342 {
443cb67c 2343 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
092ae175
MV
2344 flNotDir(I->Path).c_str());
2345 continue;
2346 }
2347 }
2348
b2e465d6
AL
2349 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
2350 I->MD5Hash,I->Size,
2351 Last->Index().SourceInfo(*Last,*I),Src);
36375005
AL
2352 }
2353 }
2354
2355 // Display statistics
3a882565
DK
2356 unsigned long long FetchBytes = Fetcher.FetchNeeded();
2357 unsigned long long FetchPBytes = Fetcher.PartialPresent();
2358 unsigned long long DebBytes = Fetcher.TotalNeeded();
36375005
AL
2359
2360 // Check for enough free space
f332b62b 2361 struct statvfs Buf;
36375005 2362 string OutputDir = ".";
c1ce032a
DK
2363 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
2364 if (errno == EOVERFLOW)
2365 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
2366 OutputDir.c_str());
2367 else
2368 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
2369 OutputDir.c_str());
2370 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
885d204b
OS
2371 {
2372 struct statfs Stat;
f64196e8
DK
2373 if (statfs(OutputDir.c_str(),&Stat) != 0
2374#if HAVE_STRUCT_STATFS_F_TYPE
2375 || unsigned(Stat.f_type) != RAMFS_MAGIC
2376#endif
2377 )
885d204b
OS
2378 return _error->Error(_("You don't have enough free space in %s"),
2379 OutputDir.c_str());
2380 }
36375005
AL
2381
2382 // Number of bytes
36375005 2383 if (DebBytes != FetchBytes)
4d8d8112
DK
2384 //TRANSLATOR: The required space between number and unit is already included
2385 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
2386 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
2387 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
36375005 2388 else
4d8d8112
DK
2389 //TRANSLATOR: The required space between number and unit is already included
2390 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
2391 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
2392 SizeToStr(DebBytes).c_str());
2393
2c0c53b3
AL
2394 if (_config->FindB("APT::Get::Simulate",false) == true)
2395 {
2396 for (unsigned I = 0; I != J; I++)
db0db9fe 2397 ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
3a4477a4 2398 delete[] Dsc;
2c0c53b3
AL
2399 return true;
2400 }
2401
36375005
AL
2402 // Just print out the uris an exit if the --print-uris flag was used
2403 if (_config->FindB("APT::Get::Print-URIs") == true)
2404 {
2405 pkgAcquire::UriIterator I = Fetcher.UriBegin();
2406 for (; I != Fetcher.UriEnd(); I++)
2407 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 2408 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
3a4477a4 2409 delete[] Dsc;
36375005
AL
2410 return true;
2411 }
2412
2413 // Run it
024d1123 2414 if (Fetcher.Run() == pkgAcquire::Failed)
36375005
AL
2415 return false;
2416
2417 // Print error messages
fb0ee66e 2418 bool Failed = false;
076d01b0 2419 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
36375005
AL
2420 {
2421 if ((*I)->Status == pkgAcquire::Item::StatDone &&
2422 (*I)->Complete == true)
2423 continue;
2424
b2e465d6
AL
2425 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
2426 (*I)->ErrorText.c_str());
fb0ee66e 2427 Failed = true;
36375005 2428 }
fb0ee66e 2429 if (Failed == true)
b2e465d6 2430 return _error->Error(_("Failed to fetch some archives."));
fb0ee66e
AL
2431
2432 if (_config->FindB("APT::Get::Download-only",false) == true)
b2e465d6
AL
2433 {
2434 c1out << _("Download complete and in download only mode") << endl;
3a4477a4 2435 delete[] Dsc;
fb0ee66e 2436 return true;
b2e465d6
AL
2437 }
2438
fb0ee66e 2439 // Unpack the sources
54676e1a
AL
2440 pid_t Process = ExecFork();
2441
2442 if (Process == 0)
fb0ee66e 2443 {
827d04d3 2444 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
54676e1a 2445 for (unsigned I = 0; I != J; I++)
fb0ee66e 2446 {
b2e465d6 2447 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
fb0ee66e 2448
17c0e8e1
AL
2449 // Diff only mode only fetches .diff files
2450 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
a3eaf954
AL
2451 _config->FindB("APT::Get::Tar-Only",false) == true ||
2452 Dsc[I].Dsc.empty() == true)
17c0e8e1 2453 continue;
a3eaf954 2454
54676e1a
AL
2455 // See if the package is already unpacked
2456 struct stat Stat;
827d04d3 2457 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
54676e1a
AL
2458 S_ISDIR(Stat.st_mode) != 0)
2459 {
b2e465d6
AL
2460 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
2461 Dir.c_str());
54676e1a
AL
2462 }
2463 else
2464 {
2465 // Call dpkg-source
2466 char S[500];
2467 snprintf(S,sizeof(S),"%s -x %s",
2468 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
2469 Dsc[I].Dsc.c_str());
2470 if (system(S) != 0)
2471 {
b2e465d6 2472 fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
14cd494a 2473 fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
54676e1a
AL
2474 _exit(1);
2475 }
2476 }
2477
2478 // Try to compile it with dpkg-buildpackage
2479 if (_config->FindB("APT::Get::Compile",false) == true)
2480 {
2481 // Call dpkg-buildpackage
2482 char S[500];
2483 snprintf(S,sizeof(S),"cd %s && %s %s",
2484 Dir.c_str(),
2485 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
2486 _config->Find("DPkg::Build-Options","-b -uc").c_str());
2487
2488 if (system(S) != 0)
2489 {
b2e465d6 2490 fprintf(stderr,_("Build command '%s' failed.\n"),S);
54676e1a
AL
2491 _exit(1);
2492 }
2493 }
2494 }
2495
2496 _exit(0);
2497 }
3a4477a4
DK
2498 delete[] Dsc;
2499
54676e1a
AL
2500 // Wait for the subprocess
2501 int Status = 0;
2502 while (waitpid(Process,&Status,0) != Process)
2503 {
2504 if (errno == EINTR)
2505 continue;
2506 return _error->Errno("waitpid","Couldn't wait for subprocess");
2507 }
2508
2509 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
b2e465d6
AL
2510 return _error->Error(_("Child process failed"));
2511
2512 return true;
2513}
2514 /*}}}*/
2515// DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
2516// ---------------------------------------------------------------------
2517/* This function will look at the build depends list of the given source
2518 package and install the necessary packages to make it true, or fail. */
2519bool DoBuildDep(CommandLine &CmdL)
2520{
2521 CacheFile Cache;
2522 if (Cache.Open(true) == false)
2523 return false;
2524
2525 if (CmdL.FileSize() <= 1)
2526 return _error->Error(_("Must specify at least one package to check builddeps for"));
2527
2528 // Read the source list
1bb8cd67
DK
2529 if (Cache.BuildSourceList() == false)
2530 return false;
2531 pkgSourceList *List = Cache.GetSourceList();
54676e1a 2532
b2e465d6
AL
2533 // Create the text record parsers
2534 pkgRecords Recs(Cache);
1bb8cd67 2535 pkgSrcRecords SrcRecs(*List);
b2e465d6
AL
2536 if (_error->PendingError() == true)
2537 return false;
2538
2539 // Create the download object
2540 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1cd1c398
DK
2541 pkgAcquire Fetcher;
2542 if (Fetcher.Setup(&Stat) == false)
2543 return false;
b2e465d6
AL
2544
2545 unsigned J = 0;
086bb6d7 2546 bool const StripMultiArch = APT::Configuration::getArchitectures().size() <= 1;
b2e465d6
AL
2547 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
2548 {
2549 string Src;
2550 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
2551 if (Last == 0)
2552 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
2553
2554 // Process the build-dependencies
2555 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
086bb6d7 2556 if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
b2e465d6
AL
2557 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
2558
7d6f9f8f
AL
2559 // Also ensure that build-essential packages are present
2560 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
2561 if (Opts)
2562 Opts = Opts->Child;
2563 for (; Opts; Opts = Opts->Next)
2564 {
2565 if (Opts->Value.empty() == true)
2566 continue;
2567
2568 pkgSrcRecords::Parser::BuildDepRec rec;
2569 rec.Package = Opts->Value;
2570 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
2571 rec.Op = 0;
58d76831 2572 BuildDeps.push_back(rec);
7d6f9f8f
AL
2573 }
2574
b2e465d6
AL
2575 if (BuildDeps.size() == 0)
2576 {
2577 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
2578 continue;
2579 }
2580
2581 // Install the requested packages
b2e465d6
AL
2582 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
2583 pkgProblemResolver Fix(Cache);
58d76831 2584 bool skipAlternatives = false; // skip remaining alternatives in an or group
b2e465d6
AL
2585 for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
2586 {
58d76831
AL
2587 bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
2588
2589 if (skipAlternatives == true)
2590 {
2591 if (!hasAlternatives)
2592 skipAlternatives = false; // end of or group
2593 continue;
2594 }
2595
aa2d22be
AL
2596 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
2597 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
d3fc0061 2598 {
aa2d22be
AL
2599 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
2600 // Build-conflicts on unknown packages are silently ignored
2601 if (Pkg.end() == true)
2602 continue;
2603
2604 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
2605
2606 /*
2607 * Remove if we have an installed version that satisfies the
2608 * version criteria
2609 */
2610 if (IV.end() == false &&
2611 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
b8ad5512 2612 TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
d3fc0061 2613 }
aa2d22be
AL
2614 else // BuildDep || BuildDepIndep
2615 {
2616 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
58d76831
AL
2617 if (_config->FindB("Debug::BuildDeps",false) == true)
2618 cout << "Looking for " << (*D).Package << "...\n";
2619
aa2d22be
AL
2620 if (Pkg.end() == true)
2621 {
58d76831
AL
2622 if (_config->FindB("Debug::BuildDeps",false) == true)
2623 cout << " (not found)" << (*D).Package << endl;
2624
2625 if (hasAlternatives)
2626 continue;
2627
2628 return _error->Error(_("%s dependency for %s cannot be satisfied "
2629 "because the package %s cannot be found"),
2630 Last->BuildDepType((*D).Type),Src.c_str(),
2631 (*D).Package.c_str());
aa2d22be
AL
2632 }
2633
2634 /*
2635 * if there are alternatives, we've already picked one, so skip
2636 * the rest
2637 *
2638 * TODO: this means that if there's a build-dep on A|B and B is
2639 * installed, we'll still try to install A; more importantly,
2640 * if A is currently broken, we cannot go back and try B. To fix
2641 * this would require we do a Resolve cycle for each package we
2642 * add to the install list. Ugh
2643 */
aa2d22be 2644
cfa5659c
AL
2645 /*
2646 * If this is a virtual package, we need to check the list of
2647 * packages that provide it and see if any of those are
2648 * installed
2649 */
2650 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
a8a0fdcf
AL
2651 for (; Prv.end() != true; Prv++)
2652 {
58d76831 2653 if (_config->FindB("Debug::BuildDeps",false) == true)
75ce2062 2654 cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
58d76831 2655
cfa5659c
AL
2656 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
2657 break;
cb99271c 2658 }
e5002e30
AL
2659
2660 // Get installed version and version we are going to install
2661 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
e5002e30 2662
58d76831
AL
2663 if ((*D).Version[0] != '\0') {
2664 // Versioned dependency
cb99271c
AL
2665
2666 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
2667
2668 for (; CV.end() != true; CV++)
2669 {
2670 if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2671 break;
2672 }
2673 if (CV.end() == true)
085bedac 2674 {
34e88622
AL
2675 if (hasAlternatives)
2676 {
2677 continue;
2678 }
2679 else
2680 {
cb99271c
AL
2681 return _error->Error(_("%s dependency for %s cannot be satisfied "
2682 "because no available versions of package %s "
2683 "can satisfy version requirements"),
2684 Last->BuildDepType((*D).Type),Src.c_str(),
2685 (*D).Package.c_str());
34e88622 2686 }
085bedac 2687 }
e5002e30 2688 }
58d76831
AL
2689 else
2690 {
2691 // Only consider virtual packages if there is no versioned dependency
2692 if (Prv.end() == false)
2693 {
2694 if (_config->FindB("Debug::BuildDeps",false) == true)
75ce2062 2695 cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
58d76831
AL
2696 skipAlternatives = hasAlternatives;
2697 continue;
2698 }
2699 }
cfa5659c 2700
58d76831
AL
2701 if (IV.end() == false)
2702 {
2703 if (_config->FindB("Debug::BuildDeps",false) == true)
2704 cout << " Is installed\n";
2705
2706 if (Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2707 {
2708 skipAlternatives = hasAlternatives;
2709 continue;
2710 }
2711
2712 if (_config->FindB("Debug::BuildDeps",false) == true)
2713 cout << " ...but the installed version doesn't meet the version requirement\n";
2714
2715 if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
2716 {
2717 return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
2718 Last->BuildDepType((*D).Type),
2719 Src.c_str(),
75ce2062 2720 Pkg.FullName(true).c_str());
58d76831
AL
2721 }
2722 }
2723
2724
2725 if (_config->FindB("Debug::BuildDeps",false) == true)
2726 cout << " Trying to install " << (*D).Package << endl;
2727
b8ad5512 2728 if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false) == true)
58d76831
AL
2729 {
2730 // We successfully installed something; skip remaining alternatives
2731 skipAlternatives = hasAlternatives;
d59228b0 2732 if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
496a05c6 2733 Cache->MarkAuto(Pkg, true);
58d76831
AL
2734 continue;
2735 }
2736 else if (hasAlternatives)
2737 {
2738 if (_config->FindB("Debug::BuildDeps",false) == true)
2739 cout << " Unsatisfiable, trying alternatives\n";
2740 continue;
2741 }
2742 else
2743 {
2744 return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
2745 Last->BuildDepType((*D).Type),
2746 Src.c_str(),
2747 (*D).Package.c_str());
2748 }
b2e465d6
AL
2749 }
2750 }
2751
2752 Fix.InstallProtect();
2753 if (Fix.Resolve(true) == false)
2754 _error->Discard();
2755
2756 // Now we check the state of the packages,
2757 if (Cache->BrokenCount() != 0)
0dae8ac5
DK
2758 {
2759 ShowBroken(cout, Cache, false);
2760 return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
2761 }
b2e465d6
AL
2762 }
2763
2764 if (InstallPackages(Cache, false, true) == false)
2765 return _error->Error(_("Failed to process build dependencies"));
36375005
AL
2766 return true;
2767}
2768 /*}}}*/
b2e465d6
AL
2769// DoMoo - Never Ask, Never Tell /*{{{*/
2770// ---------------------------------------------------------------------
2771/* */
2772bool DoMoo(CommandLine &CmdL)
2773{
2774 cout <<
2775 " (__) \n"
2776 " (oo) \n"
2777 " /------\\/ \n"
2778 " / | || \n"
2779 " * /\\---/\\ \n"
2780 " ~~ ~~ \n"
2781 "....\"Have you mooed today?\"...\n";
2782
2783 return true;
2784}
2785 /*}}}*/
0a8e3465
AL
2786// ShowHelp - Show a help screen /*{{{*/
2787// ---------------------------------------------------------------------
2788/* */
212ad54a 2789bool ShowHelp(CommandLine &CmdL)
0a8e3465 2790{
5b28c804
OS
2791 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
2792 COMMON_ARCH,__DATE__,__TIME__);
b2e465d6 2793
04aa15a8 2794 if (_config->FindB("version") == true)
b2e465d6 2795 {
db0db9fe 2796 cout << _("Supported modules:") << endl;
b2e465d6
AL
2797
2798 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
2799 {
2800 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
2801 if (_system != 0 && _system->VS == VS)
2802 cout << '*';
2803 else
2804 cout << ' ';
2805 cout << "Ver: " << VS->Label << endl;
2806
2807 /* Print out all the packaging systems that will work with
2808 this VS */
2809 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
2810 {
2811 pkgSystem *Sys = pkgSystem::GlobalList[J];
2812 if (_system == Sys)
2813 cout << '*';
2814 else
2815 cout << ' ';
2816 if (Sys->VS->TestCompatibility(*VS) == true)
2817 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
2818 }
2819 }
2820
2821 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
2822 {
2823 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
2824 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
2825 }
2826
2827 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
2828 {
2829 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
2830 cout << " Idx: " << Type->Label << endl;
2831 }
2832
2833 return true;
2834 }
2835
2836 cout <<
2837 _("Usage: apt-get [options] command\n"
2838 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
2839 " apt-get [options] source pkg1 [pkg2 ...]\n"
2840 "\n"
2841 "apt-get is a simple command line interface for downloading and\n"
2842 "installing packages. The most frequently used commands are update\n"
2843 "and install.\n"
2844 "\n"
2845 "Commands:\n"
2846 " update - Retrieve new lists of packages\n"
2847 " upgrade - Perform an upgrade\n"
2848 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
2849 " remove - Remove packages\n"
12bffed7 2850 " autoremove - Remove automatically all unused packages\n"
73fc19d0 2851 " purge - Remove packages and config files\n"
b2e465d6
AL
2852 " source - Download source archives\n"
2853 " build-dep - Configure build-dependencies for source packages\n"
2854 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
2855 " dselect-upgrade - Follow dselect selections\n"
2856 " clean - Erase downloaded archive files\n"
2857 " autoclean - Erase old downloaded archive files\n"
2858 " check - Verify that there are no broken dependencies\n"
d63a1458
JAK
2859 " markauto - Mark the given packages as automatically installed\n"
2860 " unmarkauto - Mark the given packages as manually installed\n"
b2e465d6
AL
2861 "\n"
2862 "Options:\n"
2863 " -h This help text.\n"
2864 " -q Loggable output - no progress indicator\n"
2865 " -qq No output except for errors\n"
2866 " -d Download only - do NOT install or unpack archives\n"
2867 " -s No-act. Perform ordering simulation\n"
2868 " -y Assume Yes to all queries and do not prompt\n"
0748d509 2869 " -f Attempt to correct a system with broken dependencies in place\n"
b2e465d6
AL
2870 " -m Attempt to continue if archives are unlocatable\n"
2871 " -u Show a list of upgraded packages as well\n"
2872 " -b Build the source package after fetching it\n"
ac625538 2873 " -V Show verbose version numbers\n"
b2e465d6 2874 " -c=? Read this configuration file\n"
a2884e32 2875 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
2876 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
2877 "pages for more information and options.\n"
2878 " This APT has Super Cow Powers.\n");
2879 return true;
0a8e3465
AL
2880}
2881 /*}}}*/
d7827aca
AL
2882// SigWinch - Window size change signal handler /*{{{*/
2883// ---------------------------------------------------------------------
2884/* */
2885void SigWinch(int)
2886{
2887 // Riped from GNU ls
2888#ifdef TIOCGWINSZ
2889 struct winsize ws;
2890
2891 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
2892 ScreenWidth = ws.ws_col - 1;
2893#endif
2894}
2895 /*}}}*/
92fcbfc1 2896int main(int argc,const char *argv[]) /*{{{*/
0a8e3465
AL
2897{
2898 CommandLine::Args Args[] = {
2899 {'h',"help","help",0},
04aa15a8 2900 {'v',"version","version",0},
ac625538 2901 {'V',"verbose-versions","APT::Get::Show-Versions",0},
0a8e3465
AL
2902 {'q',"quiet","quiet",CommandLine::IntLevel},
2903 {'q',"silent","quiet",CommandLine::IntLevel},
2904 {'d',"download-only","APT::Get::Download-Only",0},
fb0ee66e
AL
2905 {'b',"compile","APT::Get::Compile",0},
2906 {'b',"build","APT::Get::Compile",0},
d150b09d
AL
2907 {'s',"simulate","APT::Get::Simulate",0},
2908 {'s',"just-print","APT::Get::Simulate",0},
2909 {'s',"recon","APT::Get::Simulate",0},
6df23d2f 2910 {'s',"dry-run","APT::Get::Simulate",0},
d150b09d
AL
2911 {'s',"no-act","APT::Get::Simulate",0},
2912 {'y',"yes","APT::Get::Assume-Yes",0},
0a8e3465
AL
2913 {'y',"assume-yes","APT::Get::Assume-Yes",0},
2914 {'f',"fix-broken","APT::Get::Fix-Broken",0},
2915 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
30e1eab5 2916 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
b2e465d6
AL
2917 {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
2918 {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
2919 {0,"download","APT::Get::Download",0},
30e1eab5 2920 {0,"fix-missing","APT::Get::Fix-Missing",0},
b2e465d6
AL
2921 {0,"ignore-hold","APT::Ignore-Hold",0},
2922 {0,"upgrade","APT::Get::upgrade",0},
6cd9fbd7 2923 {0,"only-upgrade","APT::Get::Only-Upgrade",0},
83d89a9f 2924 {0,"force-yes","APT::Get::force-yes",0},
f7a08e33 2925 {0,"print-uris","APT::Get::Print-URIs",0},
5fafc0ef 2926 {0,"diff-only","APT::Get::Diff-Only",0},
a0895a74 2927 {0,"debian-only","APT::Get::Diff-Only",0},
1979e742
MV
2928 {0,"tar-only","APT::Get::Tar-Only",0},
2929 {0,"dsc-only","APT::Get::Dsc-Only",0},
fc4b5c9f 2930 {0,"purge","APT::Get::Purge",0},
9df71a5b 2931 {0,"list-cleanup","APT::Get::List-Cleanup",0},
d0c59649 2932 {0,"reinstall","APT::Get::ReInstall",0},
d150b09d 2933 {0,"trivial-only","APT::Get::Trivial-Only",0},
b2e465d6
AL
2934 {0,"remove","APT::Get::Remove",0},
2935 {0,"only-source","APT::Get::Only-Source",0},
45430cbf 2936 {0,"arch-only","APT::Get::Arch-Only",0},
f8ac1720 2937 {0,"auto-remove","APT::Get::AutomaticRemove",0},
7db98ffc 2938 {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
e9ae3677 2939 {0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean},
4ef9a929 2940 {0,"fix-policy","APT::Get::Fix-Policy-Broken",0},
0a8e3465
AL
2941 {'c',"config-file",0,CommandLine::ConfigFile},
2942 {'o',"option",0,CommandLine::ArbItem},
2943 {0,0,0,0}};
83d89a9f
AL
2944 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
2945 {"upgrade",&DoUpgrade},
2946 {"install",&DoInstall},
2947 {"remove",&DoInstall},
24401c09 2948 {"purge",&DoInstall},
74a05226 2949 {"autoremove",&DoInstall},
d63a1458
JAK
2950 {"markauto",&DoMarkAuto},
2951 {"unmarkauto",&DoMarkAuto},
83d89a9f
AL
2952 {"dist-upgrade",&DoDistUpgrade},
2953 {"dselect-upgrade",&DoDSelectUpgrade},
b2e465d6 2954 {"build-dep",&DoBuildDep},
83d89a9f 2955 {"clean",&DoClean},
1bc849af 2956 {"autoclean",&DoAutoClean},
83d89a9f 2957 {"check",&DoCheck},
67111687 2958 {"source",&DoSource},
b2e465d6 2959 {"moo",&DoMoo},
67111687 2960 {"help",&ShowHelp},
83d89a9f 2961 {0,0}};
67111687
AL
2962
2963 // Set up gettext support
2964 setlocale(LC_ALL,"");
2965 textdomain(PACKAGE);
2966
0a8e3465
AL
2967 // Parse the command line and initialize the package library
2968 CommandLine CmdL(Args,_config);
b2e465d6
AL
2969 if (pkgInitConfig(*_config) == false ||
2970 CmdL.Parse(argc,argv) == false ||
2971 pkgInitSystem(*_config,_system) == false)
0a8e3465 2972 {
b2e465d6
AL
2973 if (_config->FindB("version") == true)
2974 ShowHelp(CmdL);
2975
0a8e3465
AL
2976 _error->DumpErrors();
2977 return 100;
2978 }
2979
2980 // See if the help should be shown
2981 if (_config->FindB("help") == true ||
04aa15a8 2982 _config->FindB("version") == true ||
0a8e3465 2983 CmdL.FileSize() == 0)
b2e465d6
AL
2984 {
2985 ShowHelp(CmdL);
2986 return 0;
2987 }
55a5a46c
MV
2988
2989 // simulate user-friendly if apt-get has no root privileges
2990 if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true)
2991 {
ecf59bfc
DK
2992 if (_config->FindB("APT::Get::Show-User-Simulation-Note",true) == true)
2993 cout << _("NOTE: This is only a simulation!\n"
2994 " apt-get needs root privileges for real execution.\n"
2995 " Keep also in mind that locking is deactivated,\n"
2996 " so don't depend on the relevance to the real current situation!"
2997 ) << std::endl;
55a5a46c
MV
2998 _config->Set("Debug::NoLocking",true);
2999 }
3000
a9a5908d 3001 // Deal with stdout not being a tty
c340d185 3002 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
a9a5908d 3003 _config->Set("quiet","1");
01b64152 3004
0a8e3465
AL
3005 // Setup the output streams
3006 c0out.rdbuf(cout.rdbuf());
3007 c1out.rdbuf(cout.rdbuf());
3008 c2out.rdbuf(cout.rdbuf());
3009 if (_config->FindI("quiet",0) > 0)
3010 c0out.rdbuf(devnull.rdbuf());
3011 if (_config->FindI("quiet",0) > 1)
3012 c1out.rdbuf(devnull.rdbuf());
d7827aca
AL
3013
3014 // Setup the signals
3015 signal(SIGPIPE,SIG_IGN);
3016 signal(SIGWINCH,SigWinch);
3017 SigWinch(0);
b2e465d6 3018
0a8e3465 3019 // Match the operation
83d89a9f 3020 CmdL.DispatchArg(Cmds);
0a8e3465
AL
3021
3022 // Print any errors or warnings found during parsing
65beb572
DK
3023 bool const Errors = _error->PendingError();
3024 if (_config->FindI("quiet",0) > 0)
0a8e3465 3025 _error->DumpErrors();
65beb572
DK
3026 else
3027 _error->DumpErrors(GlobalError::DEBUG);
3028 return Errors == true ? 100 : 0;
0a8e3465 3029}
92fcbfc1 3030 /*}}}*/