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