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