test changelogs locally instead of depending on an online service
[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 {
067cc369
DK
836 for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
837 s != start.end(); ++s)
838 Cache->GetDepCache()->SetCandidateVersion(s->first);
839
2c085486
DK
840 bool Success = true;
841 std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
842 for (std::list<std::pair<pkgCache::VerIterator, std::string> >::const_iterator s = start.begin();
843 s != start.end(); ++s)
844 {
845 Changed.push_back(std::make_pair(s->first, pkgCache::VerIterator(*Cache)));
846 // We continue here even if it failed to enhance the ShowBroken output
847 Success &= Cache->GetDepCache()->SetCandidateRelease(s->first, s->second, Changed);
848 }
849 for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
850 c != Changed.end(); ++c)
851 {
852 if (c->second.end() == true)
853 ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
854 c->first.VerStr(), c->first.RelStr().c_str(), c->first.ParentPkg().FullName(true).c_str());
855 else if (c->first.ParentPkg()->Group != c->second.ParentPkg()->Group)
856 {
857 pkgCache::VerIterator V = (*Cache)[c->first.ParentPkg()].CandidateVerIter(*Cache);
858 ioprintf(out, _("Selected version '%s' (%s) for '%s' because of '%s'\n"), V.VerStr(),
859 V.RelStr().c_str(), V.ParentPkg().FullName(true).c_str(), c->second.ParentPkg().FullName(true).c_str());
860 }
861 }
862 return Success;
863 }
864
6806db8a
DK
865 void doAutoInstall() {
866 for (APT::PackageSet::const_iterator P = doAutoInstallLater.begin();
867 P != doAutoInstallLater.end(); ++P) {
868 pkgDepCache::StateCache &State = (*Cache)[P];
869 if (State.InstBroken() == false && State.InstPolicyBroken() == false)
870 continue;
871 Cache->GetDepCache()->MarkInstall(P, true);
872 }
873 doAutoInstallLater.clear();
874 }
21d4c9f1
DK
875};
876 /*}}}*/
877// TryToRemove - Mark a package for removal /*{{{*/
878struct TryToRemove {
879 pkgCacheFile* Cache;
880 pkgProblemResolver* Fix;
881 bool FixBroken;
6cb1583a 882 bool PurgePkgs;
21d4c9f1
DK
883 unsigned long AutoMarkChanged;
884
6cb1583a
DK
885 TryToRemove(pkgCacheFile &Cache, pkgProblemResolver &PM) : Cache(&Cache), Fix(&PM),
886 PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
21d4c9f1
DK
887
888 void operator() (pkgCache::VerIterator const &Ver)
889 {
890 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
891
892 Fix->Clear(Pkg);
893 Fix->Protect(Pkg);
894 Fix->Remove(Pkg);
895
6cb1583a
DK
896 if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
897 (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
bea41712 898 {
21d4c9f1 899 ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.FullName(true).c_str());
bea41712
DK
900 // MarkInstall refuses to install packages on hold
901 Pkg->SelectedState = pkgCache::State::Hold;
902 }
21d4c9f1 903 else
6cb1583a 904 Cache->GetDepCache()->MarkDelete(Pkg, PurgePkgs);
21d4c9f1
DK
905 }
906};
907 /*}}}*/
1089ca89 908// CacheFile::NameComp - QSort compare by name /*{{{*/
0a8e3465
AL
909// ---------------------------------------------------------------------
910/* */
1089ca89
AL
911pkgCache *CacheFile::SortCache = 0;
912int CacheFile::NameComp(const void *a,const void *b)
0a8e3465 913{
8508b1df
AL
914 if (*(pkgCache::Package **)a == 0 || *(pkgCache::Package **)b == 0)
915 return *(pkgCache::Package **)a - *(pkgCache::Package **)b;
0a8e3465 916
1089ca89
AL
917 const pkgCache::Package &A = **(pkgCache::Package **)a;
918 const pkgCache::Package &B = **(pkgCache::Package **)b;
919
920 return strcmp(SortCache->StrP + A.Name,SortCache->StrP + B.Name);
921}
922 /*}}}*/
923// CacheFile::Sort - Sort by name /*{{{*/
924// ---------------------------------------------------------------------
925/* */
926void CacheFile::Sort()
927{
928 delete [] List;
929 List = new pkgCache::Package *[Cache->Head().PackageCount];
930 memset(List,0,sizeof(*List)*Cache->Head().PackageCount);
931 pkgCache::PkgIterator I = Cache->PkgBegin();
932 for (;I.end() != true; I++)
933 List[I->ID] = I;
934
935 SortCache = *this;
936 qsort(List,Cache->Head().PackageCount,sizeof(*List),NameComp);
937}
0a8e3465 938 /*}}}*/
b2e465d6 939// CacheFile::CheckDeps - Open the cache file /*{{{*/
0a8e3465
AL
940// ---------------------------------------------------------------------
941/* This routine generates the caches and then opens the dependency cache
942 and verifies that the system is OK. */
2d11135a 943bool CacheFile::CheckDeps(bool AllowBroken)
0a8e3465 944{
4ef9a929
MV
945 bool FixBroken = _config->FindB("APT::Get::Fix-Broken",false);
946
d38b7b3d
AL
947 if (_error->PendingError() == true)
948 return false;
0a8e3465 949
0a8e3465 950 // Check that the system is OK
b2e465d6 951 if (DCache->DelCount() != 0 || DCache->InstCount() != 0)
db0db9fe 952 return _error->Error("Internal error, non-zero counts");
0a8e3465
AL
953
954 // Apply corrections for half-installed packages
b2e465d6 955 if (pkgApplyStatus(*DCache) == false)
0a8e3465
AL
956 return false;
957
4ef9a929
MV
958 if (_config->FindB("APT::Get::Fix-Policy-Broken",false) == true)
959 {
960 FixBroken = true;
961 if ((DCache->PolicyBrokenCount() > 0))
962 {
963 // upgrade all policy-broken packages with ForceImportantDeps=True
964 for (pkgCache::PkgIterator I = Cache->PkgBegin(); !I.end(); I++)
965 if ((*DCache)[I].NowPolicyBroken() == true)
7610bb3d 966 DCache->MarkInstall(I,true,0, false, true);
4ef9a929
MV
967 }
968 }
969
0a8e3465 970 // Nothing is broken
b2e465d6 971 if (DCache->BrokenCount() == 0 || AllowBroken == true)
0a8e3465
AL
972 return true;
973
974 // Attempt to fix broken things
4ef9a929 975 if (FixBroken == true)
0a8e3465 976 {
b2e465d6
AL
977 c1out << _("Correcting dependencies...") << flush;
978 if (pkgFixBroken(*DCache) == false || DCache->BrokenCount() != 0)
0a8e3465 979 {
b2e465d6 980 c1out << _(" failed.") << endl;
421c8d10 981 ShowBroken(c1out,*this,true);
0a8e3465 982
b2e465d6 983 return _error->Error(_("Unable to correct dependencies"));
0a8e3465 984 }
b2e465d6
AL
985 if (pkgMinimizeUpgrade(*DCache) == false)
986 return _error->Error(_("Unable to minimize the upgrade set"));
0a8e3465 987
b2e465d6 988 c1out << _(" Done") << endl;
0a8e3465
AL
989 }
990 else
991 {
b5647402 992 c1out << _("You might want to run 'apt-get -f install' to correct these.") << endl;
421c8d10 993 ShowBroken(c1out,*this,true);
0a8e3465 994
b2e465d6 995 return _error->Error(_("Unmet dependencies. Try using -f."));
0a8e3465
AL
996 }
997
998 return true;
999}
92fcbfc1
DK
1000 /*}}}*/
1001// CheckAuth - check if each download comes form a trusted source /*{{{*/
1002// ---------------------------------------------------------------------
1003/* */
7db98ffc
MZ
1004static bool CheckAuth(pkgAcquire& Fetcher)
1005{
1006 string UntrustedList;
1007 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd(); ++I)
1008 {
1009 if (!(*I)->IsTrusted())
1010 {
1011 UntrustedList += string((*I)->ShortDesc()) + " ";
1012 }
1013 }
1014
1015 if (UntrustedList == "")
1016 {
1017 return true;
1018 }
1019
1020 ShowList(c2out,_("WARNING: The following packages cannot be authenticated!"),UntrustedList,"");
1021
1022 if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
1023 {
2a7e07c7 1024 c2out << _("Authentication warning overridden.\n");
7db98ffc
MZ
1025 return true;
1026 }
1027
1028 if (_config->FindI("quiet",0) < 2
1029 && _config->FindB("APT::Get::Assume-Yes",false) == false)
1030 {
db0db9fe 1031 c2out << _("Install these packages without verification [y/N]? ") << flush;
7db98ffc
MZ
1032 if (!YnPrompt(false))
1033 return _error->Error(_("Some packages could not be authenticated"));
1034
1035 return true;
1036 }
1037 else if (_config->FindB("APT::Get::Force-Yes",false) == true)
1038 {
1039 return true;
1040 }
1041
1042 return _error->Error(_("There are problems and -y was used without --force-yes"));
1043}
0a8e3465 1044 /*}}}*/
0a8e3465
AL
1045// InstallPackages - Actually download and install the packages /*{{{*/
1046// ---------------------------------------------------------------------
1047/* This displays the informative messages describing what is going to
1048 happen and then calls the download routines */
a3eaf954 1049bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true,
80fbda96 1050 bool Safety = true)
0a8e3465 1051{
fc4b5c9f
AL
1052 if (_config->FindB("APT::Get::Purge",false) == true)
1053 {
1054 pkgCache::PkgIterator I = Cache->PkgBegin();
1055 for (; I.end() == false; I++)
d556d1a1
AL
1056 {
1057 if (I.Purge() == false && Cache[I].Mode == pkgDepCache::ModeDelete)
1058 Cache->MarkDelete(I,true);
1059 }
fc4b5c9f
AL
1060 }
1061
83d89a9f 1062 bool Fail = false;
6f86c974 1063 bool Essential = false;
83d89a9f 1064
a6568219 1065 // Show all the various warning indicators
0a8e3465
AL
1066 ShowDel(c1out,Cache);
1067 ShowNew(c1out,Cache);
1068 if (ShwKept == true)
1069 ShowKept(c1out,Cache);
7a215bee 1070 Fail |= !ShowHold(c1out,Cache);
906fbf88 1071 if (_config->FindB("APT::Get::Show-Upgraded",true) == true)
0a8e3465 1072 ShowUpgraded(c1out,Cache);
b2e465d6 1073 Fail |= !ShowDowngraded(c1out,Cache);
5d1d0738
AL
1074 if (_config->FindB("APT::Get::Download-Only",false) == false)
1075 Essential = !ShowEssential(c1out,Cache);
6f86c974 1076 Fail |= Essential;
0a8e3465 1077 Stats(c1out,Cache);
7db98ffc 1078
0a8e3465 1079 // Sanity check
d38b7b3d 1080 if (Cache->BrokenCount() != 0)
0a8e3465 1081 {
421c8d10 1082 ShowBroken(c1out,Cache,false);
2a7e07c7 1083 return _error->Error(_("Internal error, InstallPackages was called with broken packages!"));
0a8e3465
AL
1084 }
1085
d0c59649 1086 if (Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
d38b7b3d 1087 Cache->BadCount() == 0)
c60d151b 1088 return true;
03e39e59 1089
d150b09d 1090 // No remove flag
b2e465d6 1091 if (Cache->DelCount() != 0 && _config->FindB("APT::Get::Remove",true) == false)
db0db9fe 1092 return _error->Error(_("Packages need to be removed but remove is disabled."));
d150b09d 1093
03e39e59
AL
1094 // Run the simulator ..
1095 if (_config->FindB("APT::Get::Simulate") == true)
1096 {
1097 pkgSimulate PM(Cache);
2a7e07c7
MV
1098 int status_fd = _config->FindI("APT::Status-Fd",-1);
1099 pkgPackageManager::OrderResult Res = PM.DoInstall(status_fd);
281daf46
AL
1100 if (Res == pkgPackageManager::Failed)
1101 return false;
1102 if (Res != pkgPackageManager::Completed)
2a7e07c7 1103 return _error->Error(_("Internal error, Ordering didn't finish"));
281daf46 1104 return true;
03e39e59
AL
1105 }
1106
1107 // Create the text record parser
1108 pkgRecords Recs(Cache);
83d89a9f
AL
1109 if (_error->PendingError() == true)
1110 return false;
1cd1c398 1111
03e39e59 1112 // Create the download object
1cd1c398 1113 pkgAcquire Fetcher;
03e39e59 1114 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
a722b2c5
DK
1115 if (_config->FindB("APT::Get::Print-URIs", false) == true)
1116 {
1117 // force a hashsum for compatibility reasons
1118 _config->CndSet("Acquire::ForceHash", "md5sum");
a722b2c5
DK
1119 }
1120 else if (Fetcher.Setup(&Stat, _config->FindDir("Dir::Cache::Archives")) == false)
1cd1c398 1121 return false;
03e39e59
AL
1122
1123 // Read the source list
1bb8cd67
DK
1124 if (Cache.BuildSourceList() == false)
1125 return false;
1126 pkgSourceList *List = Cache.GetSourceList();
03e39e59
AL
1127
1128 // Create the package manager and prepare to download
b2e465d6 1129 SPtr<pkgPackageManager> PM= _system->CreatePM(Cache);
1bb8cd67 1130 if (PM->GetArchives(&Fetcher,List,&Recs) == false ||
424c3bc0 1131 _error->PendingError() == true)
03e39e59
AL
1132 return false;
1133
7a1b1f8b 1134 // Display statistics
3a882565
DK
1135 unsigned long long FetchBytes = Fetcher.FetchNeeded();
1136 unsigned long long FetchPBytes = Fetcher.PartialPresent();
1137 unsigned long long DebBytes = Fetcher.TotalNeeded();
d38b7b3d
AL
1138 if (DebBytes != Cache->DebSize())
1139 {
1140 c0out << DebBytes << ',' << Cache->DebSize() << endl;
2a7e07c7 1141 c0out << _("How odd.. The sizes didn't match, email apt@packages.debian.org") << endl;
d38b7b3d 1142 }
138d4b3d 1143
7a1b1f8b 1144 // Number of bytes
a6568219 1145 if (DebBytes != FetchBytes)
4d8d8112
DK
1146 //TRANSLATOR: The required space between number and unit is already included
1147 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
ac7fd99c 1148 ioprintf(c1out,_("Need to get %sB/%sB of archives.\n"),
b2e465d6 1149 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
813603a0 1150 else if (DebBytes != 0)
4d8d8112
DK
1151 //TRANSLATOR: The required space between number and unit is already included
1152 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
ac7fd99c 1153 ioprintf(c1out,_("Need to get %sB of archives.\n"),
b2e465d6
AL
1154 SizeToStr(DebBytes).c_str());
1155
10bb1f5f
AL
1156 // Size delta
1157 if (Cache->UsrSize() >= 0)
4d8d8112
DK
1158 //TRANSLATOR: The required space between number and unit is already included
1159 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
813603a0 1160 ioprintf(c1out,_("After this operation, %sB of additional disk space will be used.\n"),
b2e465d6 1161 SizeToStr(Cache->UsrSize()).c_str());
10bb1f5f 1162 else
4d8d8112
DK
1163 //TRANSLATOR: The required space between number and unit is already included
1164 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
813603a0 1165 ioprintf(c1out,_("After this operation, %sB disk space will be freed.\n"),
b2e465d6 1166 SizeToStr(-1*Cache->UsrSize()).c_str());
10bb1f5f
AL
1167
1168 if (_error->PendingError() == true)
1169 return false;
31a0531d 1170
01b64152
AL
1171 /* Check for enough free space, but only if we are actually going to
1172 download */
18e20d63
AL
1173 if (_config->FindB("APT::Get::Print-URIs") == false &&
1174 _config->FindB("APT::Get::Download",true) == true)
01b64152
AL
1175 {
1176 struct statvfs Buf;
1177 string OutputDir = _config->FindDir("Dir::Cache::Archives");
c1ce032a
DK
1178 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
1179 if (errno == EOVERFLOW)
1180 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
1181 OutputDir.c_str());
1182 else
1183 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
1184 OutputDir.c_str());
1185 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
885d204b
OS
1186 {
1187 struct statfs Stat;
f64196e8
DK
1188 if (statfs(OutputDir.c_str(),&Stat) != 0
1189#if HAVE_STRUCT_STATFS_F_TYPE
1190 || unsigned(Stat.f_type) != RAMFS_MAGIC
1191#endif
1192 )
885d204b
OS
1193 return _error->Error(_("You don't have enough free space in %s."),
1194 OutputDir.c_str());
1195 }
01b64152
AL
1196 }
1197
83d89a9f 1198 // Fail safe check
0c95c765
AL
1199 if (_config->FindI("quiet",0) >= 2 ||
1200 _config->FindB("APT::Get::Assume-Yes",false) == true)
83d89a9f
AL
1201 {
1202 if (Fail == true && _config->FindB("APT::Get::Force-Yes",false) == false)
b2e465d6 1203 return _error->Error(_("There are problems and -y was used without --force-yes"));
83d89a9f 1204 }
83d89a9f 1205
80fbda96 1206 if (Essential == true && Safety == true)
6f86c974 1207 {
d150b09d 1208 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
b2e465d6 1209 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
d150b09d 1210
b2e465d6
AL
1211 const char *Prompt = _("Yes, do as I say!");
1212 ioprintf(c2out,
080bf1be 1213 _("You are about to do something potentially harmful.\n"
b2e465d6
AL
1214 "To continue type in the phrase '%s'\n"
1215 " ?] "),Prompt);
1216 c2out << flush;
1217 if (AnalPrompt(Prompt) == false)
6f86c974 1218 {
b2e465d6 1219 c2out << _("Abort.") << endl;
a6568219 1220 exit(1);
6f86c974
AL
1221 }
1222 }
1223 else
d150b09d 1224 {
6f86c974 1225 // Prompt to continue
38262e68 1226 if (Ask == true || Fail == true)
6f86c974 1227 {
d150b09d 1228 if (_config->FindB("APT::Get::Trivial-Only",false) == true)
b2e465d6 1229 return _error->Error(_("Trivial Only specified but this is not a trivial operation."));
d150b09d 1230
0c95c765 1231 if (_config->FindI("quiet",0) < 2 &&
6f86c974 1232 _config->FindB("APT::Get::Assume-Yes",false) == false)
0c95c765 1233 {
db0db9fe 1234 c2out << _("Do you want to continue [Y/n]? ") << flush;
6f86c974 1235
0c95c765
AL
1236 if (YnPrompt() == false)
1237 {
b2e465d6 1238 c2out << _("Abort.") << endl;
0c95c765
AL
1239 exit(1);
1240 }
1241 }
6f86c974
AL
1242 }
1243 }
1244
36375005 1245 // Just print out the uris an exit if the --print-uris flag was used
f7a08e33
AL
1246 if (_config->FindB("APT::Get::Print-URIs") == true)
1247 {
1248 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1249 for (; I != Fetcher.UriEnd(); I++)
1250 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 1251 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
f7a08e33
AL
1252 return true;
1253 }
b2e465d6 1254
7db98ffc
MZ
1255 if (!CheckAuth(Fetcher))
1256 return false;
1257
b2e465d6
AL
1258 /* Unlock the dpkg lock if we are not going to be doing an install
1259 after. */
1260 if (_config->FindB("APT::Get::Download-Only",false) == true)
1261 _system->UnLock();
83d89a9f 1262
03e39e59 1263 // Run it
281daf46 1264 while (1)
30e1eab5 1265 {
a3eaf954 1266 bool Transient = false;
b2e465d6 1267 if (_config->FindB("APT::Get::Download",true) == false)
a3eaf954 1268 {
076d01b0 1269 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
a3eaf954
AL
1270 {
1271 if ((*I)->Local == true)
1272 {
1273 I++;
1274 continue;
1275 }
1276
1277 // Close the item and check if it was found in cache
1278 (*I)->Finished();
1279 if ((*I)->Complete == false)
1280 Transient = true;
1281
1282 // Clear it out of the fetch list
1283 delete *I;
1284 I = Fetcher.ItemsBegin();
1285 }
1286 }
1287
1288 if (Fetcher.Run() == pkgAcquire::Failed)
1289 return false;
30e1eab5 1290
281daf46
AL
1291 // Print out errors
1292 bool Failed = false;
076d01b0 1293 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
f01fe790 1294 {
281daf46
AL
1295 if ((*I)->Status == pkgAcquire::Item::StatDone &&
1296 (*I)->Complete == true)
1297 continue;
1298
281daf46
AL
1299 if ((*I)->Status == pkgAcquire::Item::StatIdle)
1300 {
1301 Transient = true;
1302 // Failed = true;
1303 continue;
1304 }
a3eaf954 1305
b2e465d6
AL
1306 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
1307 (*I)->ErrorText.c_str());
f01fe790 1308 Failed = true;
f01fe790 1309 }
5ec427c2 1310
a3eaf954 1311 /* If we are in no download mode and missing files and there were
5ec427c2
AL
1312 'failures' then the user must specify -m. Furthermore, there
1313 is no such thing as a transient error in no-download mode! */
a3eaf954 1314 if (Transient == true &&
b2e465d6 1315 _config->FindB("APT::Get::Download",true) == false)
5ec427c2
AL
1316 {
1317 Transient = false;
1318 Failed = true;
1319 }
f01fe790 1320
281daf46
AL
1321 if (_config->FindB("APT::Get::Download-Only",false) == true)
1322 {
1323 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
b2e465d6
AL
1324 return _error->Error(_("Some files failed to download"));
1325 c1out << _("Download complete and in download only mode") << endl;
281daf46
AL
1326 return true;
1327 }
1328
8195ae46 1329 if (Failed == true && _config->FindB("APT::Get::Fix-Missing",false) == false)
f01fe790 1330 {
b2e465d6 1331 return _error->Error(_("Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?"));
f01fe790
AL
1332 }
1333
281daf46 1334 if (Transient == true && Failed == true)
b2e465d6 1335 return _error->Error(_("--fix-missing and media swapping is not currently supported"));
281daf46
AL
1336
1337 // Try to deal with missing package files
b2e465d6 1338 if (Failed == true && PM->FixMissing() == false)
281daf46 1339 {
b2e465d6 1340 cerr << _("Unable to correct missing packages.") << endl;
db0db9fe 1341 return _error->Error(_("Aborting install."));
281daf46 1342 }
afb1e2e3 1343
b2e465d6 1344 _system->UnLock();
2a7e07c7
MV
1345 int status_fd = _config->FindI("APT::Status-Fd",-1);
1346 pkgPackageManager::OrderResult Res = PM->DoInstall(status_fd);
281daf46
AL
1347 if (Res == pkgPackageManager::Failed || _error->PendingError() == true)
1348 return false;
1349 if (Res == pkgPackageManager::Completed)
642ebc1a 1350 break;
281daf46
AL
1351
1352 // Reload the fetcher object and loop again for media swapping
1353 Fetcher.Shutdown();
1bb8cd67 1354 if (PM->GetArchives(&Fetcher,List,&Recs) == false)
281daf46 1355 return false;
b2e465d6
AL
1356
1357 _system->Lock();
642ebc1a
DK
1358 }
1359
1360 std::set<std::string> const disappearedPkgs = PM->GetDisappearedPackages();
1361 if (disappearedPkgs.empty() == true)
1362 return true;
1363
1364 string disappear;
1365 for (std::set<std::string>::const_iterator d = disappearedPkgs.begin();
1366 d != disappearedPkgs.end(); ++d)
1367 disappear.append(*d).append(" ");
1368
1369 ShowList(c1out, P_("The following package disappeared from your system as\n"
1370 "all files have been overwritten by other packages:",
1371 "The following packages disappeared from your system as\n"
1372 "all files have been overwritten by other packages:", disappearedPkgs.size()), disappear, "");
1373 c0out << _("Note: This is done automatic and on purpose by dpkg.") << std::endl;
1374
1375 return true;
0a8e3465
AL
1376}
1377 /*}}}*/
b8ad5512 1378// TryToInstallBuildDep - Try to install a single package /*{{{*/
c373c37a
AL
1379// ---------------------------------------------------------------------
1380/* This used to be inlined in DoInstall, but with the advent of regex package
1381 name matching it was split out.. */
21d4c9f1 1382bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
c373c37a 1383 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
70e706ad 1384 bool AllowFail = true)
c373c37a 1385{
21d4c9f1 1386 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
c373c37a 1387 {
21d4c9f1
DK
1388 CacheSetHelperAPTGet helper(c1out);
1389 helper.showErrors(AllowFail == false);
1390 pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
1391 if (Ver.end() == false)
1392 Pkg = Ver.ParentPkg();
1393 else if (helper.showVirtualPackageErrors(Cache) == false)
1394 return AllowFail;
c373c37a 1395 }
61d6a8de 1396
c373c37a
AL
1397 if (Remove == true)
1398 {
21d4c9f1
DK
1399 TryToRemove RemoveAction(Cache, Fix);
1400 RemoveAction(Pkg.VersionList());
1401 } else if (Cache[Pkg].CandidateVer != 0) {
1402 TryToInstall InstallAction(Cache, Fix, BrokenFix);
1403 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
6806db8a 1404 InstallAction.doAutoInstall();
21d4c9f1
DK
1405 } else
1406 return AllowFail;
60681f93 1407
b2e465d6
AL
1408 return true;
1409}
1410 /*}}}*/
1411// FindSrc - Find a source record /*{{{*/
1412// ---------------------------------------------------------------------
1413/* */
1414pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
1415 pkgSrcRecords &SrcRecs,string &Src,
1416 pkgDepCache &Cache)
1417{
b2e465d6 1418 string VerTag;
fb3dc579 1419 string DefRel = _config->Find("APT::Default-Release");
b2e465d6 1420 string TmpSrc = Name;
aca056a9 1421
fb3dc579
MV
1422 // extract the version/release from the pkgname
1423 const size_t found = TmpSrc.find_last_of("/=");
1424 if (found != string::npos) {
1425 if (TmpSrc[found] == '/')
1426 DefRel = TmpSrc.substr(found+1);
1427 else
1428 VerTag = TmpSrc.substr(found+1);
ebf6c42d
DK
1429 TmpSrc = TmpSrc.substr(0,found);
1430 }
aca056a9 1431
fb3dc579
MV
1432 /* Lookup the version of the package we would install if we were to
1433 install a version and determine the source package name, then look
1434 in the archive for a source package of the same name. */
1435 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
1436 const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
1437 if (MatchSrcOnly == false && Pkg.end() == false)
aca056a9 1438 {
fb3dc579 1439 if(VerTag.empty() == false || DefRel.empty() == false)
aca056a9 1440 {
e84adb76 1441 bool fuzzy = false;
fb3dc579
MV
1442 // we have a default release, try to locate the pkg. we do it like
1443 // this because GetCandidateVer() will not "downgrade", that means
1444 // "apt-get source -t stable apt" won't work on a unstable system
e84adb76 1445 for (pkgCache::VerIterator Ver = Pkg.VersionList();; Ver++)
aca056a9 1446 {
e84adb76
DK
1447 // try first only exact matches, later fuzzy matches
1448 if (Ver.end() == true)
1449 {
1450 if (fuzzy == true)
1451 break;
1452 fuzzy = true;
1453 Ver = Pkg.VersionList();
259f688a
MV
1454 // exit right away from the Pkg.VersionList() loop if we
1455 // don't have any versions
1456 if (Ver.end() == true)
1457 break;
e84adb76
DK
1458 }
1459 // We match against a concrete version (or a part of this version)
1460 if (VerTag.empty() == false &&
1461 (fuzzy == true || Cache.VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
1462 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
1463 continue;
1464
fb3dc579
MV
1465 for (pkgCache::VerFileIterator VF = Ver.FileList();
1466 VF.end() == false; VF++)
aca056a9 1467 {
fb3dc579
MV
1468 /* If this is the status file, and the current version is not the
1469 version in the status file (ie it is not installed, or somesuch)
1470 then it is not a candidate for installation, ever. This weeds
1471 out bogus entries that may be due to config-file states, or
1472 other. */
1473 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
1474 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
1475 continue;
1476
fb3dc579
MV
1477 // or we match against a release
1478 if(VerTag.empty() == false ||
1479 (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
1480 (VF.File().Codename() != 0 && VF.File().Codename() == DefRel))
1481 {
1482 pkgRecords::Parser &Parse = Recs.Lookup(VF);
1483 Src = Parse.SourcePkg();
61690a7e
MV
1484 // no SourcePkg name, so it is the "binary" name
1485 if (Src.empty() == true)
1486 Src = TmpSrc;
e84adb76
DK
1487 // the Version we have is possibly fuzzy or includes binUploads,
1488 // so we use the Version of the SourcePkg (empty if same as package)
1489 VerTag = Parse.SourceVer();
61690a7e
MV
1490 if (VerTag.empty() == true)
1491 VerTag = Ver.VerStr();
fb3dc579
MV
1492 break;
1493 }
aca056a9 1494 }
61690a7e
MV
1495 if (Src.empty() == false)
1496 break;
aca056a9 1497 }
fb3dc579
MV
1498 if (Src.empty() == true)
1499 {
61690a7e 1500 // Sources files have no codename information
ddff663f
MV
1501 if (VerTag.empty() == true && DefRel.empty() == false)
1502 {
1503 _error->Error(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
1504 return 0;
1505 }
fb3dc579 1506 }
026f60e2 1507 }
61690a7e 1508 if (Src.empty() == true)
b2e465d6 1509 {
61690a7e
MV
1510 // if we don't have found a fitting package yet so we will
1511 // choose a good candidate and proceed with that.
1512 // Maybe we will find a source later on with the right VerTag
ce6162be 1513 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
fb3dc579 1514 if (Ver.end() == false)
b2e465d6
AL
1515 {
1516 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1517 Src = Parse.SourcePkg();
61690a7e
MV
1518 if (VerTag.empty() == true)
1519 VerTag = Parse.SourceVer();
b2e465d6 1520 }
fb3dc579
MV
1521 }
1522 }
1523
1524 if (Src.empty() == true)
1525 Src = TmpSrc;
1526 else
1527 {
1528 /* if we have a source pkg name, make sure to only search
1529 for srcpkg names, otherwise apt gets confused if there
1530 is a binary package "pkg1" and a source package "pkg1"
1531 with the same name but that comes from different packages */
1532 MatchSrcOnly = true;
1533 if (Src != TmpSrc)
1534 {
1535 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
1536 }
b2e465d6 1537 }
89ad8e7c 1538
b2e465d6
AL
1539 // The best hit
1540 pkgSrcRecords::Parser *Last = 0;
1541 unsigned long Offset = 0;
1542 string Version;
89ad8e7c 1543
b2e465d6
AL
1544 /* Iterate over all of the hits, which includes the resulting
1545 binary packages in the search */
1546 pkgSrcRecords::Parser *Parse;
fb3dc579 1547 while (true)
b2e465d6 1548 {
fb3dc579
MV
1549 SrcRecs.Restart();
1550 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
b2e465d6 1551 {
fb3dc579
MV
1552 const string Ver = Parse->Version();
1553
1554 // Ignore all versions which doesn't fit
e84adb76
DK
1555 if (VerTag.empty() == false &&
1556 Cache.VS().CmpVersion(VerTag, Ver) != 0) // exact match
b2e465d6 1557 continue;
fb3dc579
MV
1558
1559 // Newer version or an exact match? Save the hit
1560 if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
1561 Last = Parse;
1562 Offset = Parse->Offset();
1563 Version = Ver;
1564 }
1565
1566 // was the version check above an exact match? If so, we don't need to look further
1567 if (VerTag.empty() == false && VerTag.size() == Ver.size())
1568 break;
b2e465d6 1569 }
fb3dc579
MV
1570 if (Last != 0 || VerTag.empty() == true)
1571 break;
1572 //if (VerTag.empty() == false && Last == 0)
ddff663f
MV
1573 _error->Error(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
1574 return 0;
b2e465d6 1575 }
fb3dc579 1576
ce6162be 1577 if (Last == 0 || Last->Jump(Offset) == false)
b2e465d6 1578 return 0;
fb3dc579 1579
b2e465d6
AL
1580 return Last;
1581}
1582 /*}}}*/
0a8e3465
AL
1583// DoUpdate - Update the package lists /*{{{*/
1584// ---------------------------------------------------------------------
1585/* */
b2e465d6 1586bool DoUpdate(CommandLine &CmdL)
0a8e3465 1587{
b2e465d6
AL
1588 if (CmdL.FileSize() != 1)
1589 return _error->Error(_("The update command takes no arguments"));
1bb8cd67
DK
1590
1591 CacheFile Cache;
1592
0919e3f9 1593 // Get the source list
1bb8cd67 1594 if (Cache.BuildSourceList() == false)
0919e3f9 1595 return false;
1bb8cd67 1596 pkgSourceList *List = Cache.GetSourceList();
0919e3f9 1597
89b70b5a 1598 // Create the progress
0919e3f9 1599 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
89b70b5a 1600
f0863b21
AL
1601 // Just print out the uris an exit if the --print-uris flag was used
1602 if (_config->FindB("APT::Get::Print-URIs") == true)
1603 {
a722b2c5
DK
1604 // force a hashsum for compatibility reasons
1605 _config->CndSet("Acquire::ForceHash", "md5sum");
1606
89b70b5a 1607 // get a fetcher
1cd1c398
DK
1608 pkgAcquire Fetcher;
1609 if (Fetcher.Setup(&Stat) == false)
1610 return false;
89b70b5a 1611
7db98ffc
MZ
1612 // Populate it with the source selection and get all Indexes
1613 // (GetAll=true)
1bb8cd67 1614 if (List->GetIndexes(&Fetcher,true) == false)
7db98ffc
MZ
1615 return false;
1616
f0863b21
AL
1617 pkgAcquire::UriIterator I = Fetcher.UriBegin();
1618 for (; I != Fetcher.UriEnd(); I++)
1619 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 1620 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
f0863b21
AL
1621 return true;
1622 }
7db98ffc 1623
89b70b5a 1624 // do the work
e88d983a 1625 if (_config->FindB("APT::Get::Download",true) == true)
1bb8cd67 1626 ListUpdate(Stat, *List);
e88d983a 1627
89b70b5a 1628 // Rebuild the cache.
0077d829 1629 if (Cache.BuildCaches() == false)
0919e3f9
AL
1630 return false;
1631
1632 return true;
afb1e2e3
MV
1633}
1634 /*}}}*/
1635// DoAutomaticRemove - Remove all automatic unused packages /*{{{*/
1636// ---------------------------------------------------------------------
1637/* Remove unused automatic packages */
1638bool DoAutomaticRemove(CacheFile &Cache)
1639{
9d2938d4 1640 bool Debug = _config->FindI("Debug::pkgAutoRemove",false);
b255ff1a 1641 bool doAutoRemove = _config->FindB("APT::Get::AutomaticRemove", false);
7898bd97 1642 bool hideAutoRemove = _config->FindB("APT::Get::HideAutoRemove");
afb1e2e3 1643
03dbbc98 1644 pkgDepCache::ActionGroup group(*Cache);
9d2938d4 1645 if(Debug)
120365ce 1646 std::cout << "DoAutomaticRemove()" << std::endl;
afb1e2e3 1647
03dbbc98
DK
1648 if (doAutoRemove == true &&
1649 _config->FindB("APT::Get::Remove",true) == false)
afb1e2e3 1650 {
3d0de656
MV
1651 c1out << _("We are not supposed to delete stuff, can't start "
1652 "AutoRemover") << std::endl;
03dbbc98 1653 return false;
3d0de656 1654 }
74a05226 1655
03dbbc98
DK
1656 bool purgePkgs = _config->FindB("APT::Get::Purge", false);
1657 bool smallList = (hideAutoRemove == false &&
df6c9723 1658 strcasecmp(_config->Find("APT::Get::HideAutoRemove","").c_str(),"small") == 0);
03dbbc98 1659
9d2938d4 1660 string autoremovelist, autoremoveversions;
03dbbc98 1661 unsigned long autoRemoveCount = 0;
9d2938d4
MV
1662 // look over the cache to see what can be removed
1663 for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); ! Pkg.end(); ++Pkg)
afb1e2e3 1664 {
9d2938d4
MV
1665 if (Cache[Pkg].Garbage)
1666 {
1667 if(Pkg.CurrentVer() != 0 || Cache[Pkg].Install())
1668 if(Debug)
75ce2062 1669 std::cout << "We could delete %s" << Pkg.FullName(true).c_str() << std::endl;
03dbbc98 1670
3d0de656 1671 if (doAutoRemove)
9d2938d4
MV
1672 {
1673 if(Pkg.CurrentVer() != 0 &&
1674 Pkg->CurrentState != pkgCache::State::ConfigFiles)
03dbbc98 1675 Cache->MarkDelete(Pkg, purgePkgs);
9d2938d4 1676 else
74a05226 1677 Cache->MarkKeep(Pkg, false, false);
9d2938d4 1678 }
03dbbc98
DK
1679 else
1680 {
a8dfff90
DK
1681 // if the package is a new install and already garbage we don't need to
1682 // install it in the first place, so nuke it instead of show it
1683 if (Cache[Pkg].Install() == true && Pkg.CurrentVer() == 0)
1684 Cache->MarkDelete(Pkg, false);
03dbbc98 1685 // only show stuff in the list that is not yet marked for removal
df6c9723 1686 else if(hideAutoRemove == false && Cache[Pkg].Delete() == false)
03dbbc98 1687 {
f0f2f956 1688 ++autoRemoveCount;
03dbbc98 1689 // we don't need to fill the strings if we don't need them
f0f2f956 1690 if (smallList == false)
03dbbc98 1691 {
75ce2062 1692 autoremovelist += Pkg.FullName(true) + " ";
03dbbc98
DK
1693 autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
1694 }
1695 }
1696 }
9d2938d4 1697 }
afb1e2e3 1698 }
a8dfff90
DK
1699
1700 // Now see if we had destroyed anything (if we had done anything)
1701 if (Cache->BrokenCount() != 0)
1702 {
1703 c1out << _("Hmm, seems like the AutoRemover destroyed something which really\n"
1704 "shouldn't happen. Please file a bug report against apt.") << endl;
1705 c1out << endl;
1706 c1out << _("The following information may help to resolve the situation:") << endl;
1707 c1out << endl;
1708 ShowBroken(c1out,Cache,false);
1709
1710 return _error->Error(_("Internal Error, AutoRemover broke stuff"));
1711 }
1712
03dbbc98
DK
1713 // if we don't remove them, we should show them!
1714 if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
1715 {
1716 if (smallList == false)
d204fc7a 1717 ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
f0f2f956
DK
1718 "The following packages were automatically installed and are no longer required:",
1719 autoRemoveCount), autoremovelist, autoremoveversions);
03dbbc98 1720 else
f0f2f956
DK
1721 ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
1722 "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
9d2938d4 1723 c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
03dbbc98 1724 }
afb1e2e3 1725 return true;
db1e7193 1726}
92fcbfc1 1727 /*}}}*/
db1e7193
MV
1728// DoUpgrade - Upgrade all packages /*{{{*/
1729// ---------------------------------------------------------------------
1730/* Upgrade all packages without installing new packages or erasing old
1731 packages */
1732bool DoUpgrade(CommandLine &CmdL)
1733{
1734 CacheFile Cache;
1735 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
1736 return false;
1737
1738 // Do the upgrade
1739 if (pkgAllUpgrade(Cache) == false)
1740 {
1741 ShowBroken(c1out,Cache,false);
1742 return _error->Error(_("Internal error, AllUpgrade broke stuff"));
1743 }
1744
1745 return InstallPackages(Cache,true);
afb1e2e3
MV
1746}
1747 /*}}}*/
0a8e3465
AL
1748// DoInstall - Install packages from the command line /*{{{*/
1749// ---------------------------------------------------------------------
1750/* Install named packages */
1751bool DoInstall(CommandLine &CmdL)
1752{
1753 CacheFile Cache;
c37b9502
AL
1754 if (Cache.OpenForInstall() == false ||
1755 Cache.CheckDeps(CmdL.FileSize() != 1) == false)
0a8e3465
AL
1756 return false;
1757
7c57fe64
AL
1758 // Enter the special broken fixing mode if the user specified arguments
1759 bool BrokenFix = false;
1760 if (Cache->BrokenCount() != 0)
1761 BrokenFix = true;
1762
0a8e3465 1763 pkgProblemResolver Fix(Cache);
31367812 1764
e67c0834
DK
1765 static const unsigned short MOD_REMOVE = 1;
1766 static const unsigned short MOD_INSTALL = 2;
1767
1768 unsigned short fallback = MOD_INSTALL;
303a1703 1769 if (strcasecmp(CmdL.FileList[0],"remove") == 0)
e67c0834 1770 fallback = MOD_REMOVE;
e47c7d16
MV
1771 else if (strcasecmp(CmdL.FileList[0], "purge") == 0)
1772 {
1773 _config->Set("APT::Get::Purge", true);
e67c0834 1774 fallback = MOD_REMOVE;
e47c7d16 1775 }
74a05226 1776 else if (strcasecmp(CmdL.FileList[0], "autoremove") == 0)
0a8e3465 1777 {
54668e4e 1778 _config->Set("APT::Get::AutomaticRemove", "true");
e67c0834 1779 fallback = MOD_REMOVE;
54668e4e 1780 }
70e706ad
DK
1781
1782 std::list<APT::VersionSet::Modifier> mods;
e67c0834 1783 mods.push_back(APT::VersionSet::Modifier(MOD_INSTALL, "+",
b8ad5512 1784 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::CANDIDATE));
e67c0834 1785 mods.push_back(APT::VersionSet::Modifier(MOD_REMOVE, "-",
b8ad5512 1786 APT::VersionSet::Modifier::POSTFIX, APT::VersionSet::NEWEST));
70e706ad
DK
1787 CacheSetHelperAPTGet helper(c0out);
1788 std::map<unsigned short, APT::VersionSet> verset = APT::VersionSet::GroupedFromCommandLine(Cache,
1789 CmdL.FileList + 1, mods, fallback, helper);
31367812 1790
70e706ad 1791 if (_error->PendingError() == true)
b8ad5512
DK
1792 {
1793 helper.showVirtualPackageErrors(Cache);
70e706ad 1794 return false;
b8ad5512 1795 }
31367812 1796
bea41712 1797 unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 };
e67c0834 1798
b8ad5512
DK
1799 TryToInstall InstallAction(Cache, Fix, BrokenFix);
1800 TryToRemove RemoveAction(Cache, Fix);
1801
70e706ad
DK
1802 // new scope for the ActionGroup
1803 {
1804 pkgDepCache::ActionGroup group(Cache);
b8ad5512 1805
e67c0834 1806 for (unsigned short i = 0; order[i] != 0; ++i)
31367812 1807 {
6806db8a 1808 if (order[i] == MOD_INSTALL) {
b8ad5512 1809 InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
2c085486 1810 InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
6806db8a
DK
1811 InstallAction.doAutoInstall();
1812 }
e67c0834 1813 else if (order[i] == MOD_REMOVE)
b8ad5512 1814 RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
54668e4e 1815 }
0a8e3465 1816
31367812
DK
1817 if (_error->PendingError() == true)
1818 return false;
1819
54668e4e
MV
1820 /* If we are in the Broken fixing mode we do not attempt to fix the
1821 problems. This is if the user invoked install without -f and gave
1822 packages */
1823 if (BrokenFix == true && Cache->BrokenCount() != 0)
1824 {
b5647402 1825 c1out << _("You might want to run 'apt-get -f install' to correct these:") << endl;
54668e4e 1826 ShowBroken(c1out,Cache,false);
7c57fe64 1827
54668e4e
MV
1828 return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
1829 }
7c57fe64 1830
54668e4e
MV
1831 // Call the scored problem resolver
1832 Fix.InstallProtect();
1833 if (Fix.Resolve(true) == false)
1834 _error->Discard();
0a8e3465 1835
54668e4e
MV
1836 // Now we check the state of the packages,
1837 if (Cache->BrokenCount() != 0)
303a1703 1838 {
b2e465d6 1839 c1out <<
54668e4e
MV
1840 _("Some packages could not be installed. This may mean that you have\n"
1841 "requested an impossible situation or if you are using the unstable\n"
1842 "distribution that some required packages have not yet been created\n"
1843 "or been moved out of Incoming.") << endl;
ecd414ef 1844 /*
54668e4e
MV
1845 if (Packages == 1)
1846 {
1847 c1out << endl;
1848 c1out <<
1849 _("Since you only requested a single operation it is extremely likely that\n"
1850 "the package is simply not installable and a bug report against\n"
1851 "that package should be filed.") << endl;
1852 }
ecd414ef 1853 */
303a1703 1854
54668e4e
MV
1855 c1out << _("The following information may help to resolve the situation:") << endl;
1856 c1out << endl;
1857 ShowBroken(c1out,Cache,false);
1858 return _error->Error(_("Broken packages"));
1859 }
120365ce 1860 }
5a68ea79
MV
1861 if (!DoAutomaticRemove(Cache))
1862 return false;
afb1e2e3 1863
0a8e3465
AL
1864 /* Print out a list of packages that are going to be installed extra
1865 to what the user asked */
e67c0834 1866 if (Cache->InstCount() != verset[MOD_INSTALL].size())
0a8e3465
AL
1867 {
1868 string List;
ac625538 1869 string VersionsList;
1089ca89 1870 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
0a8e3465 1871 {
1089ca89 1872 pkgCache::PkgIterator I(Cache,Cache.List[J]);
0a8e3465
AL
1873 if ((*Cache)[I].Install() == false)
1874 continue;
6a2512be
DK
1875 pkgCache::VerIterator Cand = Cache[I].CandidateVerIter(Cache);
1876 if (Cand.Pseudo() == true)
1877 continue;
0a8e3465 1878
6a2512be
DK
1879 if (verset[MOD_INSTALL].find(Cand) != verset[MOD_INSTALL].end())
1880 continue;
1881
1882 List += I.FullName(true) + " ";
1883 VersionsList += string(Cache[I].CandVersion) + "\n";
0a8e3465
AL
1884 }
1885
ac625538 1886 ShowList(c1out,_("The following extra packages will be installed:"),List,VersionsList);
0a8e3465
AL
1887 }
1888
a7e41689
AL
1889 /* Print out a list of suggested and recommended packages */
1890 {
1891 string SuggestsList, RecommendsList, List;
72122b62 1892 string SuggestsVersions, RecommendsVersions;
a7e41689
AL
1893 for (unsigned J = 0; J < Cache->Head().PackageCount; J++)
1894 {
29f37db8 1895 pkgCache::PkgIterator Pkg(Cache,Cache.List[J]);
a7e41689
AL
1896
1897 /* Just look at the ones we want to install */
29f37db8 1898 if ((*Cache)[Pkg].Install() == false)
a7e41689
AL
1899 continue;
1900
29f37db8
MV
1901 // get the recommends/suggests for the candidate ver
1902 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
1903 for (pkgCache::DepIterator D = CV.DependsList(); D.end() == false; )
1904 {
1905 pkgCache::DepIterator Start;
1906 pkgCache::DepIterator End;
1907 D.GlobOr(Start,End); // advances D
1908
cd33a786
MV
1909 // FIXME: we really should display a or-group as a or-group to the user
1910 // the problem is that ShowList is incapable of doing this
29f37db8
MV
1911 string RecommendsOrList,RecommendsOrVersions;
1912 string SuggestsOrList,SuggestsOrVersions;
1913 bool foundInstalledInOrGroup = false;
1914 for(;;)
1915 {
1916 /* Skip if package is installed already, or is about to be */
75ce2062 1917 string target = Start.TargetPkg().FullName(true) + " ";
2d847a59
DK
1918 pkgCache::PkgIterator const TarPkg = Start.TargetPkg();
1919 if (TarPkg->SelectedState == pkgCache::State::Install ||
d1aa9162 1920 TarPkg->SelectedState == pkgCache::State::Hold ||
2d847a59 1921 Cache[Start.TargetPkg()].Install())
29f37db8
MV
1922 {
1923 foundInstalledInOrGroup=true;
1924 break;
1925 }
1926
1927 /* Skip if we already saw it */
1928 if (int(SuggestsList.find(target)) != -1 || int(RecommendsList.find(target)) != -1)
1929 {
1930 foundInstalledInOrGroup=true;
1931 break;
1932 }
1933
1934 // this is a dep on a virtual pkg, check if any package that provides it
1935 // should be installed
1936 if(Start.TargetPkg().ProvidesList() != 0)
1937 {
1938 pkgCache::PrvIterator I = Start.TargetPkg().ProvidesList();
1939 for (; I.end() == false; I++)
1940 {
1941 pkgCache::PkgIterator Pkg = I.OwnerPkg();
1942 if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer() &&
1943 Pkg.CurrentVer() != 0)
1944 foundInstalledInOrGroup=true;
1945 }
1946 }
1947
1948 if (Start->Type == pkgCache::Dep::Suggests)
1949 {
1950 SuggestsOrList += target;
1951 SuggestsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
1952 }
1953
1954 if (Start->Type == pkgCache::Dep::Recommends)
1955 {
1956 RecommendsOrList += target;
1957 RecommendsOrVersions += string(Cache[Start.TargetPkg()].CandVersion) + "\n";
1958 }
1959
1960 if (Start >= End)
1961 break;
1962 Start++;
1963 }
1964
1965 if(foundInstalledInOrGroup == false)
1966 {
1967 RecommendsList += RecommendsOrList;
1968 RecommendsVersions += RecommendsOrVersions;
1969 SuggestsList += SuggestsOrList;
1970 SuggestsVersions += SuggestsOrVersions;
1971 }
1972
1973 }
a7e41689 1974 }
29f37db8 1975
72122b62
AL
1976 ShowList(c1out,_("Suggested packages:"),SuggestsList,SuggestsVersions);
1977 ShowList(c1out,_("Recommended packages:"),RecommendsList,RecommendsVersions);
a7e41689
AL
1978
1979 }
1980
e4b74e4b
MV
1981 // if nothing changed in the cache, but only the automark information
1982 // we write the StateFile here, otherwise it will be written in
1983 // cache.commit()
b8ad5512 1984 if (InstallAction.AutoMarkChanged > 0 &&
e4b74e4b 1985 Cache->DelCount() == 0 && Cache->InstCount() == 0 &&
9964a721
MV
1986 Cache->BadCount() == 0 &&
1987 _config->FindB("APT::Get::Simulate",false) == false)
e4b74e4b
MV
1988 Cache->writeStateFile(NULL);
1989
03e39e59 1990 // See if we need to prompt
70e706ad 1991 // FIXME: check if really the packages in the set are going to be installed
e67c0834 1992 if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0)
2c3bc8bb 1993 return InstallPackages(Cache,false,false);
13e8426f 1994
03e39e59 1995 return InstallPackages(Cache,false);
0a8e3465 1996}
d63a1458 1997
d63a1458
JAK
1998/* mark packages as automatically/manually installed. */
1999bool DoMarkAuto(CommandLine &CmdL)
2000{
2001 bool Action = true;
2002 int AutoMarkChanged = 0;
2003 OpTextProgress progress;
2004 CacheFile Cache;
2005 if (Cache.Open() == false)
2006 return false;
2007
2008 if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
2009 Action = true;
2010 else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
2011 Action = false;
2012
2013 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
2014 {
2015 const char *S = *I;
2016 // Locate the package
2017 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
2018 if (Pkg.end() == true) {
2019 return _error->Error(_("Couldn't find package %s"),S);
2020 }
2021 else
2022 {
2023 if (!Action)
2024 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
2025 else
2026 ioprintf(c1out,_("%s set to automatically installed.\n"),
2027 Pkg.Name());
2028
2029 Cache->MarkAuto(Pkg,Action);
2030 AutoMarkChanged++;
2031 }
2032 }
2033 if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
2034 return Cache->writeStateFile(NULL);
2035 return false;
2036}
0a8e3465
AL
2037 /*}}}*/
2038// DoDistUpgrade - Automatic smart upgrader /*{{{*/
2039// ---------------------------------------------------------------------
2040/* Intelligent upgrader that will install and remove packages at will */
2041bool DoDistUpgrade(CommandLine &CmdL)
2042{
2043 CacheFile Cache;
c37b9502 2044 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
0a8e3465
AL
2045 return false;
2046
db0db9fe 2047 c0out << _("Calculating upgrade... ") << flush;
0a8e3465
AL
2048 if (pkgDistUpgrade(*Cache) == false)
2049 {
b2e465d6 2050 c0out << _("Failed") << endl;
421c8d10 2051 ShowBroken(c1out,Cache,false);
0a8e3465
AL
2052 return false;
2053 }
2054
b2e465d6 2055 c0out << _("Done") << endl;
0a8e3465
AL
2056
2057 return InstallPackages(Cache,true);
2058}
2059 /*}}}*/
2060// DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
2061// ---------------------------------------------------------------------
2062/* Follows dselect's selections */
2063bool DoDSelectUpgrade(CommandLine &CmdL)
2064{
2065 CacheFile Cache;
c37b9502 2066 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
0a8e3465
AL
2067 return false;
2068
a4decc40
MV
2069 pkgDepCache::ActionGroup group(Cache);
2070
0a8e3465
AL
2071 // Install everything with the install flag set
2072 pkgCache::PkgIterator I = Cache->PkgBegin();
2073 for (;I.end() != true; I++)
2074 {
2075 /* Install the package only if it is a new install, the autoupgrader
2076 will deal with the rest */
2077 if (I->SelectedState == pkgCache::State::Install)
2078 Cache->MarkInstall(I,false);
2079 }
2080
2081 /* Now install their deps too, if we do this above then order of
2082 the status file is significant for | groups */
2083 for (I = Cache->PkgBegin();I.end() != true; I++)
2084 {
2085 /* Install the package only if it is a new install, the autoupgrader
2086 will deal with the rest */
2087 if (I->SelectedState == pkgCache::State::Install)
2f45c76a 2088 Cache->MarkInstall(I,true);
0a8e3465
AL
2089 }
2090
2091 // Apply erasures now, they override everything else.
2092 for (I = Cache->PkgBegin();I.end() != true; I++)
2093 {
2094 // Remove packages
2095 if (I->SelectedState == pkgCache::State::DeInstall ||
2096 I->SelectedState == pkgCache::State::Purge)
d556d1a1 2097 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
0a8e3465
AL
2098 }
2099
2f45c76a
AL
2100 /* Resolve any problems that dselect created, allupgrade cannot handle
2101 such things. We do so quite agressively too.. */
2102 if (Cache->BrokenCount() != 0)
2103 {
2104 pkgProblemResolver Fix(Cache);
2105
2106 // Hold back held packages.
b2e465d6 2107 if (_config->FindB("APT::Ignore-Hold",false) == false)
2f45c76a
AL
2108 {
2109 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; I++)
2110 {
2111 if (I->SelectedState == pkgCache::State::Hold)
2112 {
2113 Fix.Protect(I);
2114 Cache->MarkKeep(I);
2115 }
2116 }
2117 }
2118
2119 if (Fix.Resolve() == false)
2120 {
421c8d10 2121 ShowBroken(c1out,Cache,false);
2a7e07c7 2122 return _error->Error(_("Internal error, problem resolver broke stuff"));
2f45c76a
AL
2123 }
2124 }
2125
2126 // Now upgrade everything
0a8e3465
AL
2127 if (pkgAllUpgrade(Cache) == false)
2128 {
421c8d10 2129 ShowBroken(c1out,Cache,false);
2a7e07c7 2130 return _error->Error(_("Internal error, problem resolver broke stuff"));
0a8e3465
AL
2131 }
2132
2133 return InstallPackages(Cache,false);
2134}
2135 /*}}}*/
2136// DoClean - Remove download archives /*{{{*/
2137// ---------------------------------------------------------------------
2138/* */
2139bool DoClean(CommandLine &CmdL)
2140{
8b067c22
AL
2141 if (_config->FindB("APT::Get::Simulate") == true)
2142 {
2143 cout << "Del " << _config->FindDir("Dir::Cache::archives") << "* " <<
2144 _config->FindDir("Dir::Cache::archives") << "partial/*" << endl;
2145 return true;
2146 }
2147
1b6d659c
AL
2148 // Lock the archive directory
2149 FileFd Lock;
2150 if (_config->FindB("Debug::NoLocking",false) == false)
2151 {
2152 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2153 if (_error->PendingError() == true)
b2e465d6 2154 return _error->Error(_("Unable to lock the download directory"));
1b6d659c
AL
2155 }
2156
7a1b1f8b
AL
2157 pkgAcquire Fetcher;
2158 Fetcher.Clean(_config->FindDir("Dir::Cache::archives"));
2159 Fetcher.Clean(_config->FindDir("Dir::Cache::archives") + "partial/");
0a8e3465
AL
2160 return true;
2161}
2162 /*}}}*/
1bc849af
AL
2163// DoAutoClean - Smartly remove downloaded archives /*{{{*/
2164// ---------------------------------------------------------------------
2165/* This is similar to clean but it only purges things that cannot be
2166 downloaded, that is old versions of cached packages. */
65a1e968
AL
2167class LogCleaner : public pkgArchiveCleaner
2168{
2169 protected:
2170 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
2171 {
4cc8bab0 2172 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
c1e78ee5
AL
2173
2174 if (_config->FindB("APT::Get::Simulate") == false)
2175 unlink(File);
65a1e968
AL
2176 };
2177};
2178
1bc849af
AL
2179bool DoAutoClean(CommandLine &CmdL)
2180{
1b6d659c
AL
2181 // Lock the archive directory
2182 FileFd Lock;
2183 if (_config->FindB("Debug::NoLocking",false) == false)
2184 {
2185 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2186 if (_error->PendingError() == true)
b2e465d6 2187 return _error->Error(_("Unable to lock the download directory"));
1b6d659c
AL
2188 }
2189
1bc849af 2190 CacheFile Cache;
2d11135a 2191 if (Cache.Open() == false)
1bc849af
AL
2192 return false;
2193
65a1e968 2194 LogCleaner Cleaner;
1bc849af
AL
2195
2196 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
2197 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
2198}
2199 /*}}}*/
0e3e112e
MV
2200// DoDownload - download a binary /*{{{*/
2201// ---------------------------------------------------------------------
2202bool DoDownload(CommandLine &CmdL)
2203{
2204 CacheFile Cache;
2205 if (Cache.ReadOnlyOpen() == false)
2206 return false;
2207
2208 APT::CacheSetHelper helper(c0out);
2209 APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
2210 CmdL.FileList + 1, APT::VersionSet::CANDIDATE, helper);
2211 pkgAcquire Fetcher;
2212 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
2213 Fetcher.Setup(&Stat);
2214
2215 if (verset.empty() == true)
2216 return false;
2217
0e3e112e
MV
2218 pkgRecords Recs(Cache);
2219 pkgSourceList *SrcList = Cache.GetSourceList();
2220 for (APT::VersionSet::const_iterator Ver = verset.begin();
2221 Ver != verset.end();
2222 ++Ver)
2223 {
2224 string descr;
2225 // get the right version
2226 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
2227 pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
2228 pkgCache::VerFileIterator Vf = Ver.FileList();
2229 if (Vf.end() == true)
2230 return _error->Error("Can not find VerFile");
2231 pkgCache::PkgFileIterator F = Vf.File();
2232 pkgIndexFile *index;
2233 if(SrcList->FindIndex(F, index) == false)
2234 return _error->Error("FindIndex failed");
2235 string uri = index->ArchiveURI(rec.FileName());
2236 strprintf(descr, _("Downloading %s %s"), Pkg.Name(), Ver.VerStr());
2237 // get the most appropriate hash
2238 HashString hash;
2239 if (rec.SHA256Hash() != "")
2240 hash = HashString("sha256", rec.SHA256Hash());
2241 else if (rec.SHA1Hash() != "")
2242 hash = HashString("sha1", rec.SHA1Hash());
2243 else if (rec.MD5Hash() != "")
2244 hash = HashString("md5", rec.MD5Hash());
2245 // get the file
2246 new pkgAcqFile(&Fetcher, uri, hash.toStr(), (*Ver)->Size, descr, Pkg.Name(), ".");
0e3e112e 2247 }
3b1c02f7 2248 bool result = (Fetcher.Run() == pkgAcquire::Continue);
0e3e112e
MV
2249
2250 return result;
2251}
2252 /*}}}*/
0a8e3465
AL
2253// DoCheck - Perform the check operation /*{{{*/
2254// ---------------------------------------------------------------------
2255/* Opening automatically checks the system, this command is mostly used
2256 for debugging */
2257bool DoCheck(CommandLine &CmdL)
2258{
2259 CacheFile Cache;
2260 Cache.Open();
2d11135a 2261 Cache.CheckDeps();
0a8e3465
AL
2262
2263 return true;
2264}
2265 /*}}}*/
36375005
AL
2266// DoSource - Fetch a source archive /*{{{*/
2267// ---------------------------------------------------------------------
2d11135a 2268/* Fetch souce packages */
fb0ee66e
AL
2269struct DscFile
2270{
2271 string Package;
2272 string Version;
2273 string Dsc;
2274};
2275
36375005
AL
2276bool DoSource(CommandLine &CmdL)
2277{
2278 CacheFile Cache;
2d11135a 2279 if (Cache.Open(false) == false)
36375005
AL
2280 return false;
2281
2d11135a 2282 if (CmdL.FileSize() <= 1)
b2e465d6 2283 return _error->Error(_("Must specify at least one package to fetch source for"));
2d11135a 2284
36375005 2285 // Read the source list
1bb8cd67
DK
2286 if (Cache.BuildSourceList() == false)
2287 return false;
2288 pkgSourceList *List = Cache.GetSourceList();
36375005
AL
2289
2290 // Create the text record parsers
2291 pkgRecords Recs(Cache);
1bb8cd67 2292 pkgSrcRecords SrcRecs(*List);
36375005
AL
2293 if (_error->PendingError() == true)
2294 return false;
2295
2296 // Create the download object
2297 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1cd1c398
DK
2298 pkgAcquire Fetcher;
2299 if (Fetcher.Setup(&Stat) == false)
2300 return false;
fb0ee66e
AL
2301
2302 DscFile *Dsc = new DscFile[CmdL.FileSize()];
36375005 2303
092ae175
MV
2304 // insert all downloaded uris into this set to avoid downloading them
2305 // twice
2306 set<string> queued;
8545b536
DK
2307
2308 // Diff only mode only fetches .diff files
2309 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
2310 // Tar only mode only fetches .tar files
2311 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
2312 // Dsc only mode only fetches .dsc files
2313 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
2314
36375005 2315 // Load the requestd sources into the fetcher
fb0ee66e
AL
2316 unsigned J = 0;
2317 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
36375005
AL
2318 {
2319 string Src;
b2e465d6 2320 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
36375005
AL
2321
2322 if (Last == 0)
b2e465d6 2323 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
36375005 2324
3238423f
MV
2325 string srec = Last->AsStr();
2326 string::size_type pos = srec.find("\nVcs-");
774a6687 2327 while (pos != string::npos)
3238423f
MV
2328 {
2329 pos += strlen("\nVcs-");
2330 string vcs = srec.substr(pos,srec.find(":",pos)-pos);
774a6687
MV
2331 if(vcs == "Browser")
2332 {
2333 pos = srec.find("\nVcs-", pos);
2334 continue;
2335 }
3238423f
MV
2336 pos += vcs.length()+2;
2337 string::size_type epos = srec.find("\n", pos);
2338 string uri = srec.substr(pos,epos-pos).c_str();
b799e134 2339 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
3238423f 2340 "the '%s' version control system at:\n"
8756297c 2341 "%s\n"),
3238423f
MV
2342 Src.c_str(), vcs.c_str(), uri.c_str());
2343 if(vcs == "Bzr")
927677f0
MV
2344 ioprintf(c1out,_("Please use:\n"
2345 "bzr get %s\n"
3d513bbd 2346 "to retrieve the latest (possibly unreleased) "
b799e134 2347 "updates to the package.\n"),
3238423f 2348 uri.c_str());
b799e134 2349 break;
3238423f
MV
2350 }
2351
36375005
AL
2352 // Back track
2353 vector<pkgSrcRecords::File> Lst;
b2e465d6 2354 if (Last->Files(Lst) == false)
36375005
AL
2355 return false;
2356
2357 // Load them into the fetcher
2358 for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
2359 I != Lst.end(); I++)
2360 {
2361 // Try to guess what sort of file it is we are getting.
b2e465d6 2362 if (I->Type == "dsc")
fb0ee66e 2363 {
fb0ee66e
AL
2364 Dsc[J].Package = Last->Package();
2365 Dsc[J].Version = Last->Version();
2366 Dsc[J].Dsc = flNotDir(I->Path);
2367 }
092ae175 2368
8545b536
DK
2369 // Handle the only options so that multiple can be used at once
2370 if (diffOnly == true || tarOnly == true || dscOnly == true)
2371 {
2372 if ((diffOnly == true && I->Type == "diff") ||
2373 (tarOnly == true && I->Type == "tar") ||
2374 (dscOnly == true && I->Type == "dsc"))
2375 ; // Fine, we want this file downloaded
2376 else
2377 continue;
2378 }
1979e742 2379
092ae175
MV
2380 // don't download the same uri twice (should this be moved to
2381 // the fetcher interface itself?)
2382 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
2383 continue;
2384 queued.insert(Last->Index().ArchiveURI(I->Path));
2385
2386 // check if we have a file with that md5 sum already localy
2387 if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
2388 {
2389 FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
2390 MD5Summation sum;
2391 sum.AddFD(Fd.Fd(), Fd.Size());
2392 Fd.Close();
2393 if((string)sum.Result() == I->MD5Hash)
2394 {
443cb67c 2395 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
092ae175
MV
2396 flNotDir(I->Path).c_str());
2397 continue;
2398 }
2399 }
2400
b2e465d6
AL
2401 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
2402 I->MD5Hash,I->Size,
2403 Last->Index().SourceInfo(*Last,*I),Src);
36375005
AL
2404 }
2405 }
2406
2407 // Display statistics
3a882565
DK
2408 unsigned long long FetchBytes = Fetcher.FetchNeeded();
2409 unsigned long long FetchPBytes = Fetcher.PartialPresent();
2410 unsigned long long DebBytes = Fetcher.TotalNeeded();
36375005
AL
2411
2412 // Check for enough free space
f332b62b 2413 struct statvfs Buf;
36375005 2414 string OutputDir = ".";
c1ce032a
DK
2415 if (statvfs(OutputDir.c_str(),&Buf) != 0) {
2416 if (errno == EOVERFLOW)
2417 return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
2418 OutputDir.c_str());
2419 else
2420 return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
2421 OutputDir.c_str());
2422 } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
885d204b
OS
2423 {
2424 struct statfs Stat;
f64196e8
DK
2425 if (statfs(OutputDir.c_str(),&Stat) != 0
2426#if HAVE_STRUCT_STATFS_F_TYPE
2427 || unsigned(Stat.f_type) != RAMFS_MAGIC
2428#endif
2429 )
885d204b
OS
2430 return _error->Error(_("You don't have enough free space in %s"),
2431 OutputDir.c_str());
2432 }
36375005
AL
2433
2434 // Number of bytes
36375005 2435 if (DebBytes != FetchBytes)
4d8d8112
DK
2436 //TRANSLATOR: The required space between number and unit is already included
2437 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
2438 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
2439 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
36375005 2440 else
4d8d8112
DK
2441 //TRANSLATOR: The required space between number and unit is already included
2442 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
2443 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
2444 SizeToStr(DebBytes).c_str());
2445
2c0c53b3
AL
2446 if (_config->FindB("APT::Get::Simulate",false) == true)
2447 {
2448 for (unsigned I = 0; I != J; I++)
db0db9fe 2449 ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
3a4477a4 2450 delete[] Dsc;
2c0c53b3
AL
2451 return true;
2452 }
2453
36375005
AL
2454 // Just print out the uris an exit if the --print-uris flag was used
2455 if (_config->FindB("APT::Get::Print-URIs") == true)
2456 {
2457 pkgAcquire::UriIterator I = Fetcher.UriBegin();
2458 for (; I != Fetcher.UriEnd(); I++)
2459 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 2460 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
3a4477a4 2461 delete[] Dsc;
36375005
AL
2462 return true;
2463 }
2464
2465 // Run it
024d1123 2466 if (Fetcher.Run() == pkgAcquire::Failed)
36375005
AL
2467 return false;
2468
2469 // Print error messages
fb0ee66e 2470 bool Failed = false;
076d01b0 2471 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
36375005
AL
2472 {
2473 if ((*I)->Status == pkgAcquire::Item::StatDone &&
2474 (*I)->Complete == true)
2475 continue;
2476
b2e465d6
AL
2477 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
2478 (*I)->ErrorText.c_str());
fb0ee66e 2479 Failed = true;
36375005 2480 }
fb0ee66e 2481 if (Failed == true)
b2e465d6 2482 return _error->Error(_("Failed to fetch some archives."));
fb0ee66e
AL
2483
2484 if (_config->FindB("APT::Get::Download-only",false) == true)
b2e465d6
AL
2485 {
2486 c1out << _("Download complete and in download only mode") << endl;
3a4477a4 2487 delete[] Dsc;
fb0ee66e 2488 return true;
b2e465d6
AL
2489 }
2490
fb0ee66e 2491 // Unpack the sources
54676e1a
AL
2492 pid_t Process = ExecFork();
2493
2494 if (Process == 0)
fb0ee66e 2495 {
827d04d3 2496 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
54676e1a 2497 for (unsigned I = 0; I != J; I++)
fb0ee66e 2498 {
b2e465d6 2499 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
fb0ee66e 2500
17c0e8e1
AL
2501 // Diff only mode only fetches .diff files
2502 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
a3eaf954
AL
2503 _config->FindB("APT::Get::Tar-Only",false) == true ||
2504 Dsc[I].Dsc.empty() == true)
17c0e8e1 2505 continue;
a3eaf954 2506
54676e1a
AL
2507 // See if the package is already unpacked
2508 struct stat Stat;
827d04d3 2509 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
54676e1a
AL
2510 S_ISDIR(Stat.st_mode) != 0)
2511 {
b2e465d6
AL
2512 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
2513 Dir.c_str());
54676e1a
AL
2514 }
2515 else
2516 {
2517 // Call dpkg-source
2518 char S[500];
2519 snprintf(S,sizeof(S),"%s -x %s",
2520 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
2521 Dsc[I].Dsc.c_str());
2522 if (system(S) != 0)
2523 {
b2e465d6 2524 fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
14cd494a 2525 fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
54676e1a
AL
2526 _exit(1);
2527 }
2528 }
2529
2530 // Try to compile it with dpkg-buildpackage
2531 if (_config->FindB("APT::Get::Compile",false) == true)
2532 {
2533 // Call dpkg-buildpackage
2534 char S[500];
2535 snprintf(S,sizeof(S),"cd %s && %s %s",
2536 Dir.c_str(),
2537 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
2538 _config->Find("DPkg::Build-Options","-b -uc").c_str());
2539
2540 if (system(S) != 0)
2541 {
b2e465d6 2542 fprintf(stderr,_("Build command '%s' failed.\n"),S);
54676e1a
AL
2543 _exit(1);
2544 }
2545 }
2546 }
2547
2548 _exit(0);
2549 }
3a4477a4
DK
2550 delete[] Dsc;
2551
54676e1a
AL
2552 // Wait for the subprocess
2553 int Status = 0;
2554 while (waitpid(Process,&Status,0) != Process)
2555 {
2556 if (errno == EINTR)
2557 continue;
2558 return _error->Errno("waitpid","Couldn't wait for subprocess");
2559 }
2560
2561 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
b2e465d6
AL
2562 return _error->Error(_("Child process failed"));
2563
2564 return true;
2565}
2566 /*}}}*/
2567// DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
2568// ---------------------------------------------------------------------
2569/* This function will look at the build depends list of the given source
2570 package and install the necessary packages to make it true, or fail. */
2571bool DoBuildDep(CommandLine &CmdL)
2572{
2573 CacheFile Cache;
2574 if (Cache.Open(true) == false)
2575 return false;
2576
2577 if (CmdL.FileSize() <= 1)
2578 return _error->Error(_("Must specify at least one package to check builddeps for"));
2579
2580 // Read the source list
1bb8cd67
DK
2581 if (Cache.BuildSourceList() == false)
2582 return false;
2583 pkgSourceList *List = Cache.GetSourceList();
54676e1a 2584
b2e465d6
AL
2585 // Create the text record parsers
2586 pkgRecords Recs(Cache);
1bb8cd67 2587 pkgSrcRecords SrcRecs(*List);
b2e465d6
AL
2588 if (_error->PendingError() == true)
2589 return false;
2590
2591 // Create the download object
2592 AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
1cd1c398
DK
2593 pkgAcquire Fetcher;
2594 if (Fetcher.Setup(&Stat) == false)
2595 return false;
b2e465d6
AL
2596
2597 unsigned J = 0;
086bb6d7 2598 bool const StripMultiArch = APT::Configuration::getArchitectures().size() <= 1;
b2e465d6
AL
2599 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
2600 {
2601 string Src;
2602 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
2603 if (Last == 0)
2604 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
2605
2606 // Process the build-dependencies
2607 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
086bb6d7 2608 if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
b2e465d6
AL
2609 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
2610
7d6f9f8f
AL
2611 // Also ensure that build-essential packages are present
2612 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
2613 if (Opts)
2614 Opts = Opts->Child;
2615 for (; Opts; Opts = Opts->Next)
2616 {
2617 if (Opts->Value.empty() == true)
2618 continue;
2619
2620 pkgSrcRecords::Parser::BuildDepRec rec;
2621 rec.Package = Opts->Value;
2622 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
2623 rec.Op = 0;
58d76831 2624 BuildDeps.push_back(rec);
7d6f9f8f
AL
2625 }
2626
b2e465d6
AL
2627 if (BuildDeps.size() == 0)
2628 {
2629 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
2630 continue;
2631 }
2632
2633 // Install the requested packages
b2e465d6
AL
2634 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
2635 pkgProblemResolver Fix(Cache);
58d76831 2636 bool skipAlternatives = false; // skip remaining alternatives in an or group
b2e465d6
AL
2637 for (D = BuildDeps.begin(); D != BuildDeps.end(); D++)
2638 {
58d76831
AL
2639 bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
2640
2641 if (skipAlternatives == true)
2642 {
2643 if (!hasAlternatives)
2644 skipAlternatives = false; // end of or group
2645 continue;
2646 }
2647
aa2d22be
AL
2648 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
2649 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
d3fc0061 2650 {
aa2d22be
AL
2651 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
2652 // Build-conflicts on unknown packages are silently ignored
2653 if (Pkg.end() == true)
2654 continue;
2655
2656 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
2657
2658 /*
2659 * Remove if we have an installed version that satisfies the
2660 * version criteria
2661 */
2662 if (IV.end() == false &&
2663 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
b8ad5512 2664 TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
d3fc0061 2665 }
aa2d22be
AL
2666 else // BuildDep || BuildDepIndep
2667 {
2668 pkgCache::PkgIterator Pkg = Cache->FindPkg((*D).Package);
58d76831
AL
2669 if (_config->FindB("Debug::BuildDeps",false) == true)
2670 cout << "Looking for " << (*D).Package << "...\n";
2671
aa2d22be
AL
2672 if (Pkg.end() == true)
2673 {
58d76831
AL
2674 if (_config->FindB("Debug::BuildDeps",false) == true)
2675 cout << " (not found)" << (*D).Package << endl;
2676
2677 if (hasAlternatives)
2678 continue;
2679
2680 return _error->Error(_("%s dependency for %s cannot be satisfied "
2681 "because the package %s cannot be found"),
2682 Last->BuildDepType((*D).Type),Src.c_str(),
2683 (*D).Package.c_str());
aa2d22be
AL
2684 }
2685
2686 /*
2687 * if there are alternatives, we've already picked one, so skip
2688 * the rest
2689 *
2690 * TODO: this means that if there's a build-dep on A|B and B is
2691 * installed, we'll still try to install A; more importantly,
2692 * if A is currently broken, we cannot go back and try B. To fix
2693 * this would require we do a Resolve cycle for each package we
2694 * add to the install list. Ugh
2695 */
aa2d22be 2696
cfa5659c
AL
2697 /*
2698 * If this is a virtual package, we need to check the list of
2699 * packages that provide it and see if any of those are
2700 * installed
2701 */
2702 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
a8a0fdcf
AL
2703 for (; Prv.end() != true; Prv++)
2704 {
58d76831 2705 if (_config->FindB("Debug::BuildDeps",false) == true)
75ce2062 2706 cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
58d76831 2707
cfa5659c
AL
2708 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
2709 break;
cb99271c 2710 }
e5002e30
AL
2711
2712 // Get installed version and version we are going to install
2713 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
e5002e30 2714
58d76831
AL
2715 if ((*D).Version[0] != '\0') {
2716 // Versioned dependency
cb99271c
AL
2717
2718 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
2719
2720 for (; CV.end() != true; CV++)
2721 {
2722 if (Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2723 break;
2724 }
2725 if (CV.end() == true)
085bedac 2726 {
34e88622
AL
2727 if (hasAlternatives)
2728 {
2729 continue;
2730 }
2731 else
2732 {
cb99271c
AL
2733 return _error->Error(_("%s dependency for %s cannot be satisfied "
2734 "because no available versions of package %s "
2735 "can satisfy version requirements"),
2736 Last->BuildDepType((*D).Type),Src.c_str(),
2737 (*D).Package.c_str());
34e88622 2738 }
085bedac 2739 }
e5002e30 2740 }
58d76831
AL
2741 else
2742 {
2743 // Only consider virtual packages if there is no versioned dependency
2744 if (Prv.end() == false)
2745 {
2746 if (_config->FindB("Debug::BuildDeps",false) == true)
75ce2062 2747 cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
58d76831
AL
2748 skipAlternatives = hasAlternatives;
2749 continue;
2750 }
2751 }
cfa5659c 2752
58d76831
AL
2753 if (IV.end() == false)
2754 {
2755 if (_config->FindB("Debug::BuildDeps",false) == true)
2756 cout << " Is installed\n";
2757
2758 if (Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
2759 {
2760 skipAlternatives = hasAlternatives;
2761 continue;
2762 }
2763
2764 if (_config->FindB("Debug::BuildDeps",false) == true)
2765 cout << " ...but the installed version doesn't meet the version requirement\n";
2766
2767 if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
2768 {
2769 return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
2770 Last->BuildDepType((*D).Type),
2771 Src.c_str(),
75ce2062 2772 Pkg.FullName(true).c_str());
58d76831
AL
2773 }
2774 }
2775
2776
2777 if (_config->FindB("Debug::BuildDeps",false) == true)
2778 cout << " Trying to install " << (*D).Package << endl;
2779
b8ad5512 2780 if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false) == true)
58d76831
AL
2781 {
2782 // We successfully installed something; skip remaining alternatives
2783 skipAlternatives = hasAlternatives;
d59228b0 2784 if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
496a05c6 2785 Cache->MarkAuto(Pkg, true);
58d76831
AL
2786 continue;
2787 }
2788 else if (hasAlternatives)
2789 {
2790 if (_config->FindB("Debug::BuildDeps",false) == true)
2791 cout << " Unsatisfiable, trying alternatives\n";
2792 continue;
2793 }
2794 else
2795 {
2796 return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
2797 Last->BuildDepType((*D).Type),
2798 Src.c_str(),
2799 (*D).Package.c_str());
2800 }
b2e465d6
AL
2801 }
2802 }
2803
2804 Fix.InstallProtect();
2805 if (Fix.Resolve(true) == false)
2806 _error->Discard();
2807
2808 // Now we check the state of the packages,
2809 if (Cache->BrokenCount() != 0)
0dae8ac5
DK
2810 {
2811 ShowBroken(cout, Cache, false);
2812 return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
2813 }
b2e465d6
AL
2814 }
2815
2816 if (InstallPackages(Cache, false, true) == false)
2817 return _error->Error(_("Failed to process build dependencies"));
36375005
AL
2818 return true;
2819}
2820 /*}}}*/
a53b07bb
MV
2821// GetChangelogPath - return a path pointing to a changelog file or dir /*{{{*/
2822// ---------------------------------------------------------------------
2823/* This returns a "path" string for the changelog url construction.
2824 * Please note that its not complete, it either needs a "/changelog"
2825 * appended (for the packages.debian.org/changelogs site) or a
2826 * ".changelog" (for third party sites that store the changelog in the
2827 * pool/ next to the deb itself)
2828 * Example return: "pool/main/a/apt/apt_0.8.8ubuntu3"
2829 */
2830string GetChangelogPath(CacheFile &Cache,
2831 pkgCache::PkgIterator Pkg,
2832 pkgCache::VerIterator Ver)
2833{
2834 string path;
2835
2836 pkgRecords Recs(Cache);
2837 pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
2838 string srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
c5d6a22c
MV
2839 string ver = Ver.VerStr();
2840 // if there is a source version it always wins
2841 if (rec.SourceVer() != "")
2842 ver = rec.SourceVer();
a53b07bb 2843 path = flNotFile(rec.FileName());
c5d6a22c 2844 path += srcpkg + "_" + StripEpoch(ver);
a53b07bb
MV
2845 return path;
2846}
2847 /*}}}*/
cdb9307c
MV
2848// GuessThirdPartyChangelogUri - return url /*{{{*/
2849// ---------------------------------------------------------------------
a53b07bb
MV
2850/* Contruct a changelog file path for third party sites that do not use
2851 * packages.debian.org/changelogs
2852 * This simply uses the ArchiveURI() of the source pkg and looks for
2853 * a .changelog file there, Example for "mediabuntu":
2854 * apt-get changelog mplayer-doc:
2855 * http://packages.medibuntu.org/pool/non-free/m/mplayer/mplayer_1.0~rc4~try1.dsfg1-1ubuntu1+medibuntu1.changelog
2856 */
cdb9307c
MV
2857bool GuessThirdPartyChangelogUri(CacheFile &Cache,
2858 pkgCache::PkgIterator Pkg,
2859 pkgCache::VerIterator Ver,
2860 string &out_uri)
2861{
cdb9307c 2862 // get the binary deb server path
cdb9307c
MV
2863 pkgCache::VerFileIterator Vf = Ver.FileList();
2864 if (Vf.end() == true)
2865 return false;
2866 pkgCache::PkgFileIterator F = Vf.File();
2867 pkgIndexFile *index;
a53b07bb 2868 pkgSourceList *SrcList = Cache.GetSourceList();
cdb9307c
MV
2869 if(SrcList->FindIndex(F, index) == false)
2870 return false;
a53b07bb 2871
cdb9307c 2872 // get archive uri for the binary deb
a53b07bb
MV
2873 string path_without_dot_changelog = GetChangelogPath(Cache, Pkg, Ver);
2874 out_uri = index->ArchiveURI(path_without_dot_changelog + ".changelog");
cdb9307c
MV
2875
2876 // now strip away the filename and add srcpkg_srcver.changelog
cdb9307c
MV
2877 return true;
2878}
a4c40430
MV
2879// DownloadChangelog - Download the changelog /*{{{*/
2880// ---------------------------------------------------------------------
a53b07bb
MV
2881bool DownloadChangelog(CacheFile &CacheFile, pkgAcquire &Fetcher,
2882 pkgCache::VerIterator Ver, string targetfile)
2883/* Download a changelog file for the given package version to
2884 * targetfile. This will first try the server from Apt::Changelogs::Server
2885 * (http://packages.debian.org/changelogs by default) and if that gives
2886 * a 404 tries to get it from the archive directly (see
2887 * GuessThirdPartyChangelogUri for details how)
2888 */
a4c40430 2889{
a53b07bb 2890 string path;
a4c40430 2891 string descr;
c2991635 2892 string server;
a53b07bb 2893 string changelog_uri;
a4c40430
MV
2894
2895 // data structures we need
a53b07bb 2896 pkgCache::PkgIterator Pkg = Ver.ParentPkg();
a4c40430 2897
a53b07bb 2898 // make the server root configurable
c2991635 2899 server = _config->Find("Apt::Changelogs::Server",
a53b07bb
MV
2900 "http://packages.debian.org/changelogs");
2901 path = GetChangelogPath(CacheFile, Pkg, Ver);
2902 strprintf(changelog_uri, "%s/%s/changelog", server.c_str(), path.c_str());
88573174 2903 strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), changelog_uri.c_str());
a53b07bb 2904 // queue it
88573174 2905 new pkgAcqFile(&Fetcher, changelog_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
d786352d 2906
cdb9307c
MV
2907 // try downloading it, if that fails, they third-party-changelogs location
2908 // FIXME: res is "Continue" even if I get a 404?!?
a4c40430 2909 int res = Fetcher.Run();
cdb9307c
MV
2910 if (!FileExists(targetfile))
2911 {
2912 string third_party_uri;
a53b07bb 2913 if (GuessThirdPartyChangelogUri(CacheFile, Pkg, Ver, third_party_uri))
cdb9307c 2914 {
88573174
MV
2915 strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), third_party_uri.c_str());
2916 new pkgAcqFile(&Fetcher, third_party_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
cdb9307c
MV
2917 res = Fetcher.Run();
2918 }
2919 }
4a6fe09c 2920
a4c40430 2921 if (FileExists(targetfile))
18ae8b29 2922 return true;
a4c40430
MV
2923
2924 // error
18ae8b29 2925 return _error->Error("changelog download failed");
a4c40430
MV
2926}
2927 /*}}}*/
2928// DisplayFileInPager - Display File with pager /*{{{*/
2929void DisplayFileInPager(string filename)
2930{
2931 pid_t Process = ExecFork();
2932 if (Process == 0)
2933 {
2934 const char *Args[3];
2935 Args[0] = "/usr/bin/sensible-pager";
2936 Args[1] = filename.c_str();
2937 Args[2] = 0;
2938 execvp(Args[0],(char **)Args);
2939 exit(100);
2940 }
2941
2942 // Wait for the subprocess
2943 ExecWait(Process, "sensible-pager", false);
2944}
2945 /*}}}*/
2946// DoChangelog - Get changelog from the command line /*{{{*/
2947// ---------------------------------------------------------------------
2948bool DoChangelog(CommandLine &CmdL)
2949{
2950 CacheFile Cache;
2951 if (Cache.ReadOnlyOpen() == false)
2952 return false;
2953
2954 APT::CacheSetHelper helper(c0out);
2955 APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
2956 CmdL.FileList + 1, APT::VersionSet::CANDIDATE, helper);
2957 pkgAcquire Fetcher;
8cc74fb1
MV
2958 AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
2959 Fetcher.Setup(&Stat);
a4c40430
MV
2960
2961 if (verset.empty() == true)
2962 return false;
64b4c03e 2963 char *tmpdir = mkdtemp(strdup("/tmp/apt-changelog-XXXXXX"));
18ae8b29
MV
2964 if (tmpdir == NULL) {
2965 return _error->Errno("mkdtemp", "mkdtemp failed");
2966 }
2967
a4c40430
MV
2968 for (APT::VersionSet::const_iterator Ver = verset.begin();
2969 Ver != verset.end();
2970 ++Ver)
2971 {
18ae8b29
MV
2972 string changelogfile = string(tmpdir) + "changelog";
2973 if (DownloadChangelog(Cache, Fetcher, Ver, changelogfile))
a4c40430 2974 DisplayFileInPager(changelogfile);
18ae8b29
MV
2975 // cleanup temp file
2976 unlink(changelogfile.c_str());
a4c40430 2977 }
18ae8b29
MV
2978 // clenaup tmp dir
2979 rmdir(tmpdir);
2980 free(tmpdir);
4a6fe09c 2981 return true;
a4c40430
MV
2982}
2983 /*}}}*/
b2e465d6
AL
2984// DoMoo - Never Ask, Never Tell /*{{{*/
2985// ---------------------------------------------------------------------
2986/* */
2987bool DoMoo(CommandLine &CmdL)
2988{
2989 cout <<
2990 " (__) \n"
2991 " (oo) \n"
2992 " /------\\/ \n"
2993 " / | || \n"
2994 " * /\\---/\\ \n"
2995 " ~~ ~~ \n"
2996 "....\"Have you mooed today?\"...\n";
2997
2998 return true;
2999}
3000 /*}}}*/
0a8e3465
AL
3001// ShowHelp - Show a help screen /*{{{*/
3002// ---------------------------------------------------------------------
3003/* */
212ad54a 3004bool ShowHelp(CommandLine &CmdL)
0a8e3465 3005{
5b28c804
OS
3006 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
3007 COMMON_ARCH,__DATE__,__TIME__);
b2e465d6 3008
04aa15a8 3009 if (_config->FindB("version") == true)
b2e465d6 3010 {
db0db9fe 3011 cout << _("Supported modules:") << endl;
b2e465d6
AL
3012
3013 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
3014 {
3015 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
3016 if (_system != 0 && _system->VS == VS)
3017 cout << '*';
3018 else
3019 cout << ' ';
3020 cout << "Ver: " << VS->Label << endl;
3021
3022 /* Print out all the packaging systems that will work with
3023 this VS */
3024 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
3025 {
3026 pkgSystem *Sys = pkgSystem::GlobalList[J];
3027 if (_system == Sys)
3028 cout << '*';
3029 else
3030 cout << ' ';
3031 if (Sys->VS->TestCompatibility(*VS) == true)
3032 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
3033 }
3034 }
3035
3036 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
3037 {
3038 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
3039 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
3040 }
3041
3042 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
3043 {
3044 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
3045 cout << " Idx: " << Type->Label << endl;
3046 }
3047
3048 return true;
3049 }
3050
3051 cout <<
3052 _("Usage: apt-get [options] command\n"
3053 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
3054 " apt-get [options] source pkg1 [pkg2 ...]\n"
3055 "\n"
3056 "apt-get is a simple command line interface for downloading and\n"
3057 "installing packages. The most frequently used commands are update\n"
3058 "and install.\n"
3059 "\n"
3060 "Commands:\n"
3061 " update - Retrieve new lists of packages\n"
3062 " upgrade - Perform an upgrade\n"
3063 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
3064 " remove - Remove packages\n"
12bffed7 3065 " autoremove - Remove automatically all unused packages\n"
73fc19d0 3066 " purge - Remove packages and config files\n"
b2e465d6
AL
3067 " source - Download source archives\n"
3068 " build-dep - Configure build-dependencies for source packages\n"
3069 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
3070 " dselect-upgrade - Follow dselect selections\n"
3071 " clean - Erase downloaded archive files\n"
3072 " autoclean - Erase old downloaded archive files\n"
3073 " check - Verify that there are no broken dependencies\n"
d63a1458
JAK
3074 " markauto - Mark the given packages as automatically installed\n"
3075 " unmarkauto - Mark the given packages as manually installed\n"
5f967f2d
MV
3076 " changelog - Download and display the changelog for the given package\n"
3077 " download - Download the binary package into the current directory\n"
b2e465d6
AL
3078 "\n"
3079 "Options:\n"
3080 " -h This help text.\n"
3081 " -q Loggable output - no progress indicator\n"
3082 " -qq No output except for errors\n"
3083 " -d Download only - do NOT install or unpack archives\n"
3084 " -s No-act. Perform ordering simulation\n"
3085 " -y Assume Yes to all queries and do not prompt\n"
0748d509 3086 " -f Attempt to correct a system with broken dependencies in place\n"
b2e465d6
AL
3087 " -m Attempt to continue if archives are unlocatable\n"
3088 " -u Show a list of upgraded packages as well\n"
3089 " -b Build the source package after fetching it\n"
ac625538 3090 " -V Show verbose version numbers\n"
b2e465d6 3091 " -c=? Read this configuration file\n"
a2884e32 3092 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
3093 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
3094 "pages for more information and options.\n"
3095 " This APT has Super Cow Powers.\n");
3096 return true;
0a8e3465
AL
3097}
3098 /*}}}*/
d7827aca
AL
3099// SigWinch - Window size change signal handler /*{{{*/
3100// ---------------------------------------------------------------------
3101/* */
3102void SigWinch(int)
3103{
3104 // Riped from GNU ls
3105#ifdef TIOCGWINSZ
3106 struct winsize ws;
3107
3108 if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
3109 ScreenWidth = ws.ws_col - 1;
3110#endif
3111}
3112 /*}}}*/
92fcbfc1 3113int main(int argc,const char *argv[]) /*{{{*/
0a8e3465
AL
3114{
3115 CommandLine::Args Args[] = {
3116 {'h',"help","help",0},
04aa15a8 3117 {'v',"version","version",0},
ac625538 3118 {'V',"verbose-versions","APT::Get::Show-Versions",0},
0a8e3465
AL
3119 {'q',"quiet","quiet",CommandLine::IntLevel},
3120 {'q',"silent","quiet",CommandLine::IntLevel},
3121 {'d',"download-only","APT::Get::Download-Only",0},
fb0ee66e
AL
3122 {'b',"compile","APT::Get::Compile",0},
3123 {'b',"build","APT::Get::Compile",0},
d150b09d
AL
3124 {'s',"simulate","APT::Get::Simulate",0},
3125 {'s',"just-print","APT::Get::Simulate",0},
3126 {'s',"recon","APT::Get::Simulate",0},
6df23d2f 3127 {'s',"dry-run","APT::Get::Simulate",0},
d150b09d
AL
3128 {'s',"no-act","APT::Get::Simulate",0},
3129 {'y',"yes","APT::Get::Assume-Yes",0},
0a8e3465
AL
3130 {'y',"assume-yes","APT::Get::Assume-Yes",0},
3131 {'f',"fix-broken","APT::Get::Fix-Broken",0},
3132 {'u',"show-upgraded","APT::Get::Show-Upgraded",0},
30e1eab5 3133 {'m',"ignore-missing","APT::Get::Fix-Missing",0},
b2e465d6
AL
3134 {'t',"target-release","APT::Default-Release",CommandLine::HasArg},
3135 {'t',"default-release","APT::Default-Release",CommandLine::HasArg},
3136 {0,"download","APT::Get::Download",0},
30e1eab5 3137 {0,"fix-missing","APT::Get::Fix-Missing",0},
b2e465d6
AL
3138 {0,"ignore-hold","APT::Ignore-Hold",0},
3139 {0,"upgrade","APT::Get::upgrade",0},
6cd9fbd7 3140 {0,"only-upgrade","APT::Get::Only-Upgrade",0},
83d89a9f 3141 {0,"force-yes","APT::Get::force-yes",0},
f7a08e33 3142 {0,"print-uris","APT::Get::Print-URIs",0},
5fafc0ef 3143 {0,"diff-only","APT::Get::Diff-Only",0},
a0895a74 3144 {0,"debian-only","APT::Get::Diff-Only",0},
1979e742
MV
3145 {0,"tar-only","APT::Get::Tar-Only",0},
3146 {0,"dsc-only","APT::Get::Dsc-Only",0},
fc4b5c9f 3147 {0,"purge","APT::Get::Purge",0},
9df71a5b 3148 {0,"list-cleanup","APT::Get::List-Cleanup",0},
d0c59649 3149 {0,"reinstall","APT::Get::ReInstall",0},
d150b09d 3150 {0,"trivial-only","APT::Get::Trivial-Only",0},
b2e465d6
AL
3151 {0,"remove","APT::Get::Remove",0},
3152 {0,"only-source","APT::Get::Only-Source",0},
45430cbf 3153 {0,"arch-only","APT::Get::Arch-Only",0},
f8ac1720 3154 {0,"auto-remove","APT::Get::AutomaticRemove",0},
7db98ffc 3155 {0,"allow-unauthenticated","APT::Get::AllowUnauthenticated",0},
e9ae3677 3156 {0,"install-recommends","APT::Install-Recommends",CommandLine::Boolean},
4ef9a929 3157 {0,"fix-policy","APT::Get::Fix-Policy-Broken",0},
0a8e3465
AL
3158 {'c',"config-file",0,CommandLine::ConfigFile},
3159 {'o',"option",0,CommandLine::ArbItem},
3160 {0,0,0,0}};
83d89a9f
AL
3161 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
3162 {"upgrade",&DoUpgrade},
3163 {"install",&DoInstall},
3164 {"remove",&DoInstall},
24401c09 3165 {"purge",&DoInstall},
74a05226 3166 {"autoremove",&DoInstall},
d63a1458
JAK
3167 {"markauto",&DoMarkAuto},
3168 {"unmarkauto",&DoMarkAuto},
83d89a9f
AL
3169 {"dist-upgrade",&DoDistUpgrade},
3170 {"dselect-upgrade",&DoDSelectUpgrade},
b2e465d6 3171 {"build-dep",&DoBuildDep},
83d89a9f 3172 {"clean",&DoClean},
1bc849af 3173 {"autoclean",&DoAutoClean},
83d89a9f 3174 {"check",&DoCheck},
67111687 3175 {"source",&DoSource},
459b5f5d 3176 {"download",&DoDownload},
a4c40430 3177 {"changelog",&DoChangelog},
b2e465d6 3178 {"moo",&DoMoo},
67111687 3179 {"help",&ShowHelp},
83d89a9f 3180 {0,0}};
67111687
AL
3181
3182 // Set up gettext support
3183 setlocale(LC_ALL,"");
3184 textdomain(PACKAGE);
3185
0a8e3465
AL
3186 // Parse the command line and initialize the package library
3187 CommandLine CmdL(Args,_config);
b2e465d6
AL
3188 if (pkgInitConfig(*_config) == false ||
3189 CmdL.Parse(argc,argv) == false ||
3190 pkgInitSystem(*_config,_system) == false)
0a8e3465 3191 {
b2e465d6
AL
3192 if (_config->FindB("version") == true)
3193 ShowHelp(CmdL);
3194
0a8e3465
AL
3195 _error->DumpErrors();
3196 return 100;
3197 }
3198
3199 // See if the help should be shown
3200 if (_config->FindB("help") == true ||
04aa15a8 3201 _config->FindB("version") == true ||
0a8e3465 3202 CmdL.FileSize() == 0)
b2e465d6
AL
3203 {
3204 ShowHelp(CmdL);
3205 return 0;
3206 }
55a5a46c
MV
3207
3208 // simulate user-friendly if apt-get has no root privileges
3209 if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true)
3210 {
ecf59bfc
DK
3211 if (_config->FindB("APT::Get::Show-User-Simulation-Note",true) == true)
3212 cout << _("NOTE: This is only a simulation!\n"
3213 " apt-get needs root privileges for real execution.\n"
3214 " Keep also in mind that locking is deactivated,\n"
3215 " so don't depend on the relevance to the real current situation!"
3216 ) << std::endl;
55a5a46c
MV
3217 _config->Set("Debug::NoLocking",true);
3218 }
3219
a9a5908d 3220 // Deal with stdout not being a tty
c340d185 3221 if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
a9a5908d 3222 _config->Set("quiet","1");
01b64152 3223
0a8e3465
AL
3224 // Setup the output streams
3225 c0out.rdbuf(cout.rdbuf());
3226 c1out.rdbuf(cout.rdbuf());
3227 c2out.rdbuf(cout.rdbuf());
3228 if (_config->FindI("quiet",0) > 0)
3229 c0out.rdbuf(devnull.rdbuf());
3230 if (_config->FindI("quiet",0) > 1)
3231 c1out.rdbuf(devnull.rdbuf());
d7827aca
AL
3232
3233 // Setup the signals
3234 signal(SIGPIPE,SIG_IGN);
3235 signal(SIGWINCH,SigWinch);
3236 SigWinch(0);
b2e465d6 3237
0a8e3465 3238 // Match the operation
83d89a9f 3239 CmdL.DispatchArg(Cmds);
0a8e3465
AL
3240
3241 // Print any errors or warnings found during parsing
65beb572
DK
3242 bool const Errors = _error->PendingError();
3243 if (_config->FindI("quiet",0) > 0)
0a8e3465 3244 _error->DumpErrors();
65beb572
DK
3245 else
3246 _error->DumpErrors(GlobalError::DEBUG);
3247 return Errors == true ? 100 : 0;
0a8e3465 3248}
92fcbfc1 3249 /*}}}*/