]> git.saurik.com Git - apt.git/blame - cmdline/apt-get.cc
implement reverse_iterators for cachesets
[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
1e3f4083 18 dist-upgrade - Powerful upgrader designed to handle the issues with
5ec427c2
AL
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 /*{{{*/
ea542140
DK
28#include <config.h>
29
453b82a3
DK
30#include <apt-pkg/acquire-item.h>
31#include <apt-pkg/algorithms.h>
086bb6d7 32#include <apt-pkg/aptconfiguration.h>
453b82a3
DK
33#include <apt-pkg/cachefile.h>
34#include <apt-pkg/cacheset.h>
35#include <apt-pkg/clean.h>
0a8e3465 36#include <apt-pkg/cmndline.h>
453b82a3 37#include <apt-pkg/debmetaindex.h>
0a8e3465 38#include <apt-pkg/depcache.h>
453b82a3 39#include <apt-pkg/error.h>
472ff00e 40#include <apt-pkg/fileutl.h>
453b82a3
DK
41#include <apt-pkg/indexfile.h>
42#include <apt-pkg/indexrecords.h>
43#include <apt-pkg/init.h>
092ae175 44#include <apt-pkg/md5.h>
453b82a3 45#include <apt-pkg/metaindex.h>
472ff00e 46#include <apt-pkg/pkgrecords.h>
453b82a3
DK
47#include <apt-pkg/pkgsystem.h>
48#include <apt-pkg/progress.h>
49#include <apt-pkg/sourcelist.h>
50#include <apt-pkg/srcrecords.h>
51#include <apt-pkg/strutl.h>
52#include <apt-pkg/version.h>
53#include <apt-pkg/acquire.h>
54#include <apt-pkg/configuration.h>
55#include <apt-pkg/macros.h>
56#include <apt-pkg/pkgcache.h>
57#include <apt-pkg/cacheiterators.h>
82e369c4 58#include <apt-pkg/upgrade.h>
ffee1c2b 59
453b82a3
DK
60#include <apt-private/acqprogress.h>
61#include <apt-private/private-cacheset.h>
62#include <apt-private/private-cachefile.h>
63#include <apt-private/private-cmndline.h>
866893a6 64#include <apt-private/private-download.h>
b9179170 65#include <apt-private/private-install.h>
453b82a3
DK
66#include <apt-private/private-main.h>
67#include <apt-private/private-moo.h>
b9179170 68#include <apt-private/private-output.h>
b9179170 69#include <apt-private/private-update.h>
453b82a3 70#include <apt-private/private-upgrade.h>
cfacba52 71#include <apt-private/private-utils.h>
b9179170 72
453b82a3
DK
73#include <errno.h>
74#include <signal.h>
75#include <stddef.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
a3f1d60c 79#include <sys/ioctl.h>
1bc849af 80#include <sys/stat.h>
b9179170 81#include <sys/wait.h>
453b82a3 82#include <unistd.h>
03aa0847
DK
83#include <pwd.h>
84#include <grp.h>
85
453b82a3
DK
86#include <algorithm>
87#include <fstream>
88#include <iostream>
8881b11e 89#include <sstream>
453b82a3
DK
90#include <set>
91#include <string>
92#include <vector>
5ec427c2 93
b9179170
MV
94#include <apti18n.h>
95 /*}}}*/
afb1e2e3 96
b9179170 97using namespace std;
642ebc1a 98
b8ad5512 99// TryToInstallBuildDep - Try to install a single package /*{{{*/
c373c37a
AL
100// ---------------------------------------------------------------------
101/* This used to be inlined in DoInstall, but with the advent of regex package
102 name matching it was split out.. */
c3ccac92 103static bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
c373c37a 104 pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
70e706ad 105 bool AllowFail = true)
c373c37a 106{
21d4c9f1 107 if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0)
c373c37a 108 {
21d4c9f1 109 CacheSetHelperAPTGet helper(c1out);
4caf8231 110 helper.showErrors(false);
21d4c9f1
DK
111 pkgCache::VerIterator Ver = helper.canNotFindNewestVer(Cache, Pkg);
112 if (Ver.end() == false)
113 Pkg = Ver.ParentPkg();
114 else if (helper.showVirtualPackageErrors(Cache) == false)
115 return AllowFail;
c373c37a 116 }
61d6a8de 117
ac321486
DK
118 if (_config->FindB("Debug::BuildDeps",false) == true)
119 {
120 if (Remove == true)
121 cout << " Trying to remove " << Pkg << endl;
122 else
123 cout << " Trying to install " << Pkg << endl;
124 }
125
c373c37a
AL
126 if (Remove == true)
127 {
71d73928 128 TryToRemove RemoveAction(Cache, &Fix);
21d4c9f1
DK
129 RemoveAction(Pkg.VersionList());
130 } else if (Cache[Pkg].CandidateVer != 0) {
71d73928 131 TryToInstall InstallAction(Cache, &Fix, BrokenFix);
21d4c9f1 132 InstallAction(Cache[Pkg].CandidateVerIter(Cache));
6806db8a 133 InstallAction.doAutoInstall();
21d4c9f1
DK
134 } else
135 return AllowFail;
60681f93 136
b2e465d6
AL
137 return true;
138}
139 /*}}}*/
5ad0096a
DK
140// GetReleaseFileForSourceRecord - Return Suite for the given srcrecord /*{{{*/
141static pkgCache::RlsFileIterator GetReleaseFileForSourceRecord(CacheFile &CacheFile,
142 pkgSourceList *SrcList, pkgSrcRecords::Parser *Parse)
7014e148
MV
143{
144 // try to find release
145 const pkgIndexFile& CurrentIndexFile = Parse->Index();
146
147 for (pkgSourceList::const_iterator S = SrcList->begin();
148 S != SrcList->end(); ++S)
149 {
150 vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
151 for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
152 IF != Indexes->end(); ++IF)
153 {
154 if (&CurrentIndexFile == (*IF))
5ad0096a 155 return (*S)->FindInCache(CacheFile, false);
7014e148
MV
156 }
157 }
5ad0096a 158 return pkgCache::RlsFileIterator(CacheFile);
7014e148
MV
159}
160 /*}}}*/
b2e465d6
AL
161// FindSrc - Find a source record /*{{{*/
162// ---------------------------------------------------------------------
163/* */
765190e4 164#if APT_PKG_ABI >= 413
a221efc3 165static pkgSrcRecords::Parser *FindSrc(const char *Name,
765190e4
DK
166#else
167static pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
168#endif
b2e465d6 169 pkgSrcRecords &SrcRecs,string &Src,
7014e148 170 CacheFile &CacheFile)
b2e465d6 171{
adf379e7
MV
172 string VerTag, UserRequestedVerTag;
173 string ArchTag = "";
7014e148 174 string RelTag = _config->Find("APT::Default-Release");
b2e465d6 175 string TmpSrc = Name;
7014e148 176 pkgDepCache *Cache = CacheFile.GetDepCache();
aca056a9 177
adf379e7
MV
178 // extract release
179 size_t found = TmpSrc.find_last_of("/");
180 if (found != string::npos)
181 {
182 RelTag = TmpSrc.substr(found+1);
183 TmpSrc = TmpSrc.substr(0,found);
184 }
185 // extract the version
186 found = TmpSrc.find_last_of("=");
187 if (found != string::npos)
188 {
189 VerTag = UserRequestedVerTag = TmpSrc.substr(found+1);
190 TmpSrc = TmpSrc.substr(0,found);
191 }
192 // extract arch
193 found = TmpSrc.find_last_of(":");
194 if (found != string::npos)
195 {
196 ArchTag = TmpSrc.substr(found+1);
ebf6c42d
DK
197 TmpSrc = TmpSrc.substr(0,found);
198 }
aca056a9 199
fb3dc579
MV
200 /* Lookup the version of the package we would install if we were to
201 install a version and determine the source package name, then look
202 in the archive for a source package of the same name. */
203 bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
adf379e7
MV
204 pkgCache::PkgIterator Pkg;
205 if (ArchTag != "")
206 Pkg = Cache->FindPkg(TmpSrc, ArchTag);
207 else
208 Pkg = Cache->FindPkg(TmpSrc);
209
210 // if we can't find a package but the user qualified with a arch,
211 // error out here
212 if (Pkg.end() && ArchTag != "")
213 {
214 Src = Name;
215 _error->Error(_("Can not find a package for architecture '%s'"),
216 ArchTag.c_str());
217 return 0;
218 }
219
fb3dc579 220 if (MatchSrcOnly == false && Pkg.end() == false)
aca056a9 221 {
adf379e7 222 if(VerTag != "" || RelTag != "" || ArchTag != "")
aca056a9 223 {
e84adb76 224 bool fuzzy = false;
fb3dc579
MV
225 // we have a default release, try to locate the pkg. we do it like
226 // this because GetCandidateVer() will not "downgrade", that means
227 // "apt-get source -t stable apt" won't work on a unstable system
f7f0d6c7 228 for (pkgCache::VerIterator Ver = Pkg.VersionList();; ++Ver)
aca056a9 229 {
e84adb76
DK
230 // try first only exact matches, later fuzzy matches
231 if (Ver.end() == true)
232 {
233 if (fuzzy == true)
234 break;
235 fuzzy = true;
236 Ver = Pkg.VersionList();
259f688a
MV
237 // exit right away from the Pkg.VersionList() loop if we
238 // don't have any versions
239 if (Ver.end() == true)
240 break;
e84adb76 241 }
adf379e7 242
37f45a57
MV
243 // ignore arches that are not for us
244 if (ArchTag != "" && Ver.Arch() != ArchTag)
245 continue;
246
adf379e7
MV
247 // pick highest version for the arch unless the user wants
248 // something else
249 if (ArchTag != "" && VerTag == "" && RelTag == "")
adf379e7
MV
250 if(Cache->VS().CmpVersion(VerTag, Ver.VerStr()) < 0)
251 VerTag = Ver.VerStr();
adf379e7 252
e84adb76
DK
253 // We match against a concrete version (or a part of this version)
254 if (VerTag.empty() == false &&
7014e148 255 (fuzzy == true || Cache->VS().CmpVersion(VerTag, Ver.VerStr()) != 0) && // exact match
e84adb76
DK
256 (fuzzy == false || strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)) // fuzzy match
257 continue;
258
fb3dc579 259 for (pkgCache::VerFileIterator VF = Ver.FileList();
f7f0d6c7 260 VF.end() == false; ++VF)
aca056a9 261 {
fb3dc579
MV
262 /* If this is the status file, and the current version is not the
263 version in the status file (ie it is not installed, or somesuch)
264 then it is not a candidate for installation, ever. This weeds
265 out bogus entries that may be due to config-file states, or
266 other. */
267 if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
268 pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
269 continue;
270
fb3dc579
MV
271 // or we match against a release
272 if(VerTag.empty() == false ||
7014e148
MV
273 (VF.File().Archive() != 0 && VF.File().Archive() == RelTag) ||
274 (VF.File().Codename() != 0 && VF.File().Codename() == RelTag))
fb3dc579 275 {
e84adb76 276 // the Version we have is possibly fuzzy or includes binUploads,
765190e4
DK
277 // so we use the Version of the SourcePkg (empty if same as package)
278#if APT_PKG_ABI >= 413
279 Src = Ver.SourcePkgName();
a221efc3 280 VerTag = Ver.SourceVerStr();
765190e4
DK
281#else
282 pkgRecords::Parser &Parse = Recs.Lookup(VF);
283 Src = Parse.SourcePkg();
284 // no SourcePkg name, so it is the "binary" name
285 if (Src.empty() == true)
286 Src = TmpSrc;
287 VerTag = Parse.SourceVer();
288 if (VerTag.empty() == true)
289 VerTag = Ver.VerStr();
290#endif
fb3dc579
MV
291 break;
292 }
aca056a9 293 }
61690a7e
MV
294 if (Src.empty() == false)
295 break;
aca056a9 296 }
026f60e2 297 }
adf379e7 298
37f45a57
MV
299 if (Src == "" && ArchTag != "")
300 {
c9829e07
MV
301 if (VerTag != "")
302 _error->Error(_("Can not find a package '%s' with version '%s'"),
303 Pkg.FullName().c_str(), VerTag.c_str());
304 if (RelTag != "")
305 _error->Error(_("Can not find a package '%s' with release '%s'"),
306 Pkg.FullName().c_str(), RelTag.c_str());
37f45a57
MV
307 Src = Name;
308 return 0;
309 }
310
311
61690a7e 312 if (Src.empty() == true)
b2e465d6 313 {
61690a7e
MV
314 // if we don't have found a fitting package yet so we will
315 // choose a good candidate and proceed with that.
316 // Maybe we will find a source later on with the right VerTag
7014e148
MV
317 // or RelTag
318 pkgCache::VerIterator Ver = Cache->GetCandidateVer(Pkg);
fb3dc579 319 if (Ver.end() == false)
b2e465d6 320 {
765190e4 321#if APT_PKG_ABI >= 413
a221efc3
DK
322 if (strcmp(Ver.SourcePkgName(),Ver.ParentPkg().Name()) != 0)
323 Src = Ver.SourcePkgName();
324 if (VerTag.empty() == true && strcmp(Ver.SourceVerStr(),Ver.VerStr()) != 0)
325 VerTag = Ver.SourceVerStr();
765190e4
DK
326#else
327 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
328 Src = Parse.SourcePkg();
329 if (VerTag.empty() == true)
330 VerTag = Parse.SourceVer();
331#endif
b2e465d6 332 }
fb3dc579
MV
333 }
334 }
335
336 if (Src.empty() == true)
7014e148 337 {
fb3dc579 338 Src = TmpSrc;
7014e148 339 }
fb3dc579
MV
340 else
341 {
342 /* if we have a source pkg name, make sure to only search
343 for srcpkg names, otherwise apt gets confused if there
344 is a binary package "pkg1" and a source package "pkg1"
345 with the same name but that comes from different packages */
346 MatchSrcOnly = true;
347 if (Src != TmpSrc)
348 {
349 ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
350 }
b2e465d6 351 }
89ad8e7c 352
b2e465d6
AL
353 // The best hit
354 pkgSrcRecords::Parser *Last = 0;
355 unsigned long Offset = 0;
356 string Version;
7014e148 357 pkgSourceList *SrcList = CacheFile.GetSourceList();
89ad8e7c 358
b2e465d6
AL
359 /* Iterate over all of the hits, which includes the resulting
360 binary packages in the search */
361 pkgSrcRecords::Parser *Parse;
fb3dc579 362 while (true)
b2e465d6 363 {
fb3dc579
MV
364 SrcRecs.Restart();
365 while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0)
b2e465d6 366 {
fb3dc579
MV
367 const string Ver = Parse->Version();
368
7014e148 369 // See if we need to look for a specific release tag
65dcff55 370 if (RelTag != "" && UserRequestedVerTag == "")
7014e148 371 {
5ad0096a
DK
372 pkgCache::RlsFileIterator const Rls = GetReleaseFileForSourceRecord(CacheFile, SrcList, Parse);
373 if (Rls.end() == false)
7014e148 374 {
5ad0096a
DK
375 if ((Rls->Archive != 0 && RelTag == Rls.Archive()) ||
376 (Rls->Codename != 0 && RelTag == Rls.Codename()))
377 {
378 Last = Parse;
379 Offset = Parse->Offset();
380 Version = Ver;
381 }
7014e148
MV
382 }
383 }
384
fb3dc579 385 // Ignore all versions which doesn't fit
e84adb76 386 if (VerTag.empty() == false &&
7014e148 387 Cache->VS().CmpVersion(VerTag, Ver) != 0) // exact match
b2e465d6 388 continue;
fb3dc579
MV
389
390 // Newer version or an exact match? Save the hit
7014e148 391 if (Last == 0 || Cache->VS().CmpVersion(Version,Ver) < 0) {
fb3dc579
MV
392 Last = Parse;
393 Offset = Parse->Offset();
394 Version = Ver;
395 }
396
7014e148
MV
397 // was the version check above an exact match?
398 // If so, we don't need to look further
399 if (VerTag.empty() == false && (VerTag == Ver))
fb3dc579 400 break;
b2e465d6 401 }
65dcff55 402 if (UserRequestedVerTag == "" && Version != "" && RelTag != "")
bfa7bfc8 403 ioprintf(c1out, "Selected version '%s' (%s) for %s\n",
651ae5ce
MV
404 Version.c_str(), RelTag.c_str(), Src.c_str());
405
fb3dc579
MV
406 if (Last != 0 || VerTag.empty() == true)
407 break;
017d3f3b 408 _error->Error(_("Can not find version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
ddff663f 409 return 0;
b2e465d6 410 }
fb3dc579 411
ce6162be 412 if (Last == 0 || Last->Jump(Offset) == false)
b2e465d6 413 return 0;
fb3dc579 414
b2e465d6
AL
415 return Last;
416}
417 /*}}}*/
182a6a55 418/* mark packages as automatically/manually installed. {{{*/
c3ccac92 419static bool DoMarkAuto(CommandLine &CmdL)
d63a1458
JAK
420{
421 bool Action = true;
422 int AutoMarkChanged = 0;
423 OpTextProgress progress;
424 CacheFile Cache;
425 if (Cache.Open() == false)
426 return false;
427
428 if (strcasecmp(CmdL.FileList[0],"markauto") == 0)
429 Action = true;
430 else if (strcasecmp(CmdL.FileList[0],"unmarkauto") == 0)
431 Action = false;
432
433 for (const char **I = CmdL.FileList + 1; *I != 0; I++)
434 {
435 const char *S = *I;
436 // Locate the package
437 pkgCache::PkgIterator Pkg = Cache->FindPkg(S);
438 if (Pkg.end() == true) {
439 return _error->Error(_("Couldn't find package %s"),S);
440 }
441 else
442 {
443 if (!Action)
444 ioprintf(c1out,_("%s set to manually installed.\n"), Pkg.Name());
445 else
446 ioprintf(c1out,_("%s set to automatically installed.\n"),
447 Pkg.Name());
448
449 Cache->MarkAuto(Pkg,Action);
450 AutoMarkChanged++;
451 }
452 }
182a6a55
DK
453
454 _error->Notice(_("This command is deprecated. Please use 'apt-mark auto' and 'apt-mark manual' instead."));
455
d63a1458
JAK
456 if (AutoMarkChanged && ! _config->FindB("APT::Get::Simulate",false))
457 return Cache->writeStateFile(NULL);
458 return false;
459}
0a8e3465 460 /*}}}*/
0a8e3465
AL
461// DoDSelectUpgrade - Do an upgrade by following dselects selections /*{{{*/
462// ---------------------------------------------------------------------
463/* Follows dselect's selections */
65512241 464static bool DoDSelectUpgrade(CommandLine &)
0a8e3465
AL
465{
466 CacheFile Cache;
c37b9502 467 if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false)
0a8e3465
AL
468 return false;
469
a4decc40
MV
470 pkgDepCache::ActionGroup group(Cache);
471
0a8e3465
AL
472 // Install everything with the install flag set
473 pkgCache::PkgIterator I = Cache->PkgBegin();
f7f0d6c7 474 for (;I.end() != true; ++I)
0a8e3465
AL
475 {
476 /* Install the package only if it is a new install, the autoupgrader
477 will deal with the rest */
478 if (I->SelectedState == pkgCache::State::Install)
479 Cache->MarkInstall(I,false);
480 }
481
482 /* Now install their deps too, if we do this above then order of
483 the status file is significant for | groups */
f7f0d6c7 484 for (I = Cache->PkgBegin();I.end() != true; ++I)
0a8e3465
AL
485 {
486 /* Install the package only if it is a new install, the autoupgrader
487 will deal with the rest */
488 if (I->SelectedState == pkgCache::State::Install)
2f45c76a 489 Cache->MarkInstall(I,true);
0a8e3465
AL
490 }
491
492 // Apply erasures now, they override everything else.
f7f0d6c7 493 for (I = Cache->PkgBegin();I.end() != true; ++I)
0a8e3465
AL
494 {
495 // Remove packages
496 if (I->SelectedState == pkgCache::State::DeInstall ||
497 I->SelectedState == pkgCache::State::Purge)
d556d1a1 498 Cache->MarkDelete(I,I->SelectedState == pkgCache::State::Purge);
0a8e3465
AL
499 }
500
2f45c76a 501 /* Resolve any problems that dselect created, allupgrade cannot handle
1e3f4083 502 such things. We do so quite aggressively too.. */
2f45c76a
AL
503 if (Cache->BrokenCount() != 0)
504 {
505 pkgProblemResolver Fix(Cache);
506
507 // Hold back held packages.
b2e465d6 508 if (_config->FindB("APT::Ignore-Hold",false) == false)
2f45c76a 509 {
f7f0d6c7 510 for (pkgCache::PkgIterator I = Cache->PkgBegin(); I.end() == false; ++I)
2f45c76a
AL
511 {
512 if (I->SelectedState == pkgCache::State::Hold)
513 {
514 Fix.Protect(I);
515 Cache->MarkKeep(I);
516 }
517 }
518 }
519
520 if (Fix.Resolve() == false)
521 {
421c8d10 522 ShowBroken(c1out,Cache,false);
2a7e07c7 523 return _error->Error(_("Internal error, problem resolver broke stuff"));
2f45c76a
AL
524 }
525 }
526
527 // Now upgrade everything
67caa2e6 528 if (APT::Upgrade::Upgrade(Cache, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
0a8e3465 529 {
421c8d10 530 ShowBroken(c1out,Cache,false);
2a7e07c7 531 return _error->Error(_("Internal error, problem resolver broke stuff"));
0a8e3465
AL
532 }
533
534 return InstallPackages(Cache,false);
535}
536 /*}}}*/
537// DoClean - Remove download archives /*{{{*/
538// ---------------------------------------------------------------------
539/* */
65512241 540static bool DoClean(CommandLine &)
0a8e3465 541{
657ecd4a 542 std::string const archivedir = _config->FindDir("Dir::Cache::archives");
20e6965a 543 std::string const listsdir = _config->FindDir("Dir::state::lists");
657ecd4a 544
8b067c22
AL
545 if (_config->FindB("APT::Get::Simulate") == true)
546 {
20e6965a
DK
547 std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
548 std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
657ecd4a 549 cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< endl
20e6965a 550 << "Del " << listsdir << "partial/*" << endl
657ecd4a 551 << "Del " << pkgcache << " " << srcpkgcache << endl;
8b067c22
AL
552 return true;
553 }
20e6965a 554
7a1b1f8b 555 pkgAcquire Fetcher;
7e04a6bf 556 Fetcher.GetLock(archivedir);
657ecd4a
DK
557 Fetcher.Clean(archivedir);
558 Fetcher.Clean(archivedir + "partial/");
559
7e04a6bf 560 Fetcher.GetLock(listsdir);
20e6965a
DK
561 Fetcher.Clean(listsdir + "partial/");
562
8de79b68 563 pkgCacheFile::RemoveCaches();
657ecd4a 564
0a8e3465
AL
565 return true;
566}
567 /*}}}*/
1bc849af
AL
568// DoAutoClean - Smartly remove downloaded archives /*{{{*/
569// ---------------------------------------------------------------------
570/* This is similar to clean but it only purges things that cannot be
571 downloaded, that is old versions of cached packages. */
65a1e968
AL
572class LogCleaner : public pkgArchiveCleaner
573{
574 protected:
575 virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
576 {
4cc8bab0 577 c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
c1e78ee5
AL
578
579 if (_config->FindB("APT::Get::Simulate") == false)
580 unlink(File);
65a1e968
AL
581 };
582};
583
65512241 584static bool DoAutoClean(CommandLine &)
1bc849af 585{
1b6d659c
AL
586 // Lock the archive directory
587 FileFd Lock;
588 if (_config->FindB("Debug::NoLocking",false) == false)
589 {
de24f8ce
MV
590 int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
591 if (lock_fd < 0)
b2e465d6 592 return _error->Error(_("Unable to lock the download directory"));
de24f8ce 593 Lock.Fd(lock_fd);
1b6d659c
AL
594 }
595
1bc849af 596 CacheFile Cache;
2d11135a 597 if (Cache.Open() == false)
1bc849af
AL
598 return false;
599
65a1e968 600 LogCleaner Cleaner;
1bc849af
AL
601
602 return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
603 Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
604}
605 /*}}}*/
0e3e112e
MV
606// DoDownload - download a binary /*{{{*/
607// ---------------------------------------------------------------------
c3ccac92 608static bool DoDownload(CommandLine &CmdL)
0e3e112e
MV
609{
610 CacheFile Cache;
611 if (Cache.ReadOnlyOpen() == false)
612 return false;
d57f6084 613
353c135e 614 APT::CacheSetHelper helper;
8f3594c3 615 APT::VersionSet verset = APT::VersionSet::FromCommandLine(Cache,
43acd019 616 CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
0e3e112e
MV
617
618 if (verset.empty() == true)
619 return false;
620
dfad5bee 621 AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
04a54261 622 pkgAcquire Fetcher(&Stat);
42d41ddb 623
0e3e112e
MV
624 pkgRecords Recs(Cache);
625 pkgSourceList *SrcList = Cache.GetSourceList();
567785b9 626
d57f6084
DK
627 // reuse the usual acquire methods for deb files, but don't drop them into
628 // the usual directories - keep everything in the current directory
629 std::vector<std::string> storefile(verset.size());
630 std::string const cwd = SafeGetCWD();
631 _config->Set("Dir::Cache::Archives", cwd);
632 int i = 0;
8f3594c3 633 for (APT::VersionSet::const_iterator Ver = verset.begin();
d57f6084 634 Ver != verset.end(); ++Ver, ++i)
0e3e112e 635 {
d57f6084 636 pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
03aa0847
DK
637 if (storefile[i].empty())
638 continue;
d57f6084
DK
639 std::string const filename = cwd + flNotDir(storefile[i]);
640 storefile[i].assign(filename);
641 I->DestFile.assign(filename);
0e3e112e
MV
642 }
643
42d41ddb
DK
644 // Just print out the uris and exit if the --print-uris flag was used
645 if (_config->FindB("APT::Get::Print-URIs") == true)
646 {
647 pkgAcquire::UriIterator I = Fetcher.UriBegin();
f7f0d6c7 648 for (; I != Fetcher.UriEnd(); ++I)
b0f4b486 649 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
42d41ddb
DK
650 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
651 return true;
652 }
653
460601d5
DK
654 // Disable drop-privs if "_apt" can not write to the target dir
655 CheckDropPrivsMustBeDisabled(Fetcher);
656
d57f6084
DK
657 if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
658 return false;
659
660 bool Failed = false;
661 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
662 return false;
663
664 // copy files in local sources to the current directory
665 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
6ae22905
MV
666 {
667 std::string const filename = cwd + flNotDir((*I)->DestFile);
668 if ((*I)->Local == true &&
669 filename != (*I)->DestFile &&
670 (*I)->Status == pkgAcquire::Item::StatDone)
d57f6084 671 {
d57f6084
DK
672 std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
673 std::ofstream dst(filename.c_str(), std::ios::binary);
674 dst << src.rdbuf();
675 }
6ae22905 676 }
d57f6084 677 return Failed == false;
0e3e112e
MV
678}
679 /*}}}*/
0a8e3465
AL
680// DoCheck - Perform the check operation /*{{{*/
681// ---------------------------------------------------------------------
682/* Opening automatically checks the system, this command is mostly used
683 for debugging */
65512241 684static bool DoCheck(CommandLine &)
0a8e3465
AL
685{
686 CacheFile Cache;
687 Cache.Open();
2d11135a 688 Cache.CheckDeps();
0a8e3465
AL
689
690 return true;
691}
692 /*}}}*/
36375005
AL
693// DoSource - Fetch a source archive /*{{{*/
694// ---------------------------------------------------------------------
2d11135a 695/* Fetch souce packages */
fb0ee66e
AL
696struct DscFile
697{
698 string Package;
699 string Version;
700 string Dsc;
701};
702
c3ccac92 703static bool DoSource(CommandLine &CmdL)
36375005
AL
704{
705 CacheFile Cache;
2d11135a 706 if (Cache.Open(false) == false)
36375005
AL
707 return false;
708
2d11135a 709 if (CmdL.FileSize() <= 1)
b2e465d6 710 return _error->Error(_("Must specify at least one package to fetch source for"));
2d11135a 711
36375005 712 // Read the source list
1bb8cd67
DK
713 if (Cache.BuildSourceList() == false)
714 return false;
715 pkgSourceList *List = Cache.GetSourceList();
36375005
AL
716
717 // Create the text record parsers
765190e4
DK
718#if APT_PKG_ABI < 413
719 pkgRecords Recs(Cache);
720#endif
1bb8cd67 721 pkgSrcRecords SrcRecs(*List);
36375005
AL
722 if (_error->PendingError() == true)
723 return false;
724
725 // Create the download object
dfad5bee 726 AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
04a54261 727 pkgAcquire Fetcher(&Stat);
fb0ee66e 728
500dc070 729 SPtrArray<DscFile> Dsc = new DscFile[CmdL.FileSize()];
36375005 730
092ae175
MV
731 // insert all downloaded uris into this set to avoid downloading them
732 // twice
733 set<string> queued;
8545b536
DK
734
735 // Diff only mode only fetches .diff files
736 bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
737 // Tar only mode only fetches .tar files
738 bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
739 // Dsc only mode only fetches .dsc files
740 bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
741
36375005 742 // Load the requestd sources into the fetcher
fb0ee66e 743 unsigned J = 0;
a3f1d60c 744 std::string UntrustedList;
fb0ee66e 745 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
36375005
AL
746 {
747 string Src;
765190e4 748#if APT_PKG_ABI >= 413
a221efc3 749 pkgSrcRecords::Parser *Last = FindSrc(*I,SrcRecs,Src,Cache);
765190e4
DK
750#else
751 pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,Cache);
752#endif
6070a346 753 if (Last == 0) {
b2e465d6 754 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
6070a346 755 }
a3f1d60c
MV
756
757 if (Last->Index().IsTrusted() == false)
758 UntrustedList += Src + " ";
36375005 759
3238423f
MV
760 string srec = Last->AsStr();
761 string::size_type pos = srec.find("\nVcs-");
774a6687 762 while (pos != string::npos)
3238423f
MV
763 {
764 pos += strlen("\nVcs-");
765 string vcs = srec.substr(pos,srec.find(":",pos)-pos);
774a6687
MV
766 if(vcs == "Browser")
767 {
768 pos = srec.find("\nVcs-", pos);
769 continue;
770 }
3238423f
MV
771 pos += vcs.length()+2;
772 string::size_type epos = srec.find("\n", pos);
773 string uri = srec.substr(pos,epos-pos).c_str();
b799e134 774 ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
3238423f 775 "the '%s' version control system at:\n"
8756297c 776 "%s\n"),
3238423f
MV
777 Src.c_str(), vcs.c_str(), uri.c_str());
778 if(vcs == "Bzr")
927677f0 779 ioprintf(c1out,_("Please use:\n"
f3e3a2d4 780 "bzr branch %s\n"
3d513bbd 781 "to retrieve the latest (possibly unreleased) "
b799e134 782 "updates to the package.\n"),
3238423f 783 uri.c_str());
b799e134 784 break;
3238423f
MV
785 }
786
36375005 787 // Back track
3a2b39ee
DK
788 vector<pkgSrcRecords::File2> Lst;
789 if (Last->Files2(Lst) == false) {
36375005 790 return false;
6070a346 791 }
36375005
AL
792
793 // Load them into the fetcher
3a2b39ee 794 for (vector<pkgSrcRecords::File2>::const_iterator I = Lst.begin();
f7f0d6c7 795 I != Lst.end(); ++I)
36375005
AL
796 {
797 // Try to guess what sort of file it is we are getting.
b2e465d6 798 if (I->Type == "dsc")
fb0ee66e 799 {
fb0ee66e
AL
800 Dsc[J].Package = Last->Package();
801 Dsc[J].Version = Last->Version();
802 Dsc[J].Dsc = flNotDir(I->Path);
803 }
092ae175 804
8545b536
DK
805 // Handle the only options so that multiple can be used at once
806 if (diffOnly == true || tarOnly == true || dscOnly == true)
807 {
808 if ((diffOnly == true && I->Type == "diff") ||
809 (tarOnly == true && I->Type == "tar") ||
810 (dscOnly == true && I->Type == "dsc"))
811 ; // Fine, we want this file downloaded
812 else
813 continue;
814 }
1979e742 815
092ae175
MV
816 // don't download the same uri twice (should this be moved to
817 // the fetcher interface itself?)
818 if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
819 continue;
820 queued.insert(Last->Index().ArchiveURI(I->Path));
cb6a2b3e 821
092ae175 822 // check if we have a file with that md5 sum already localy
1262d358
DK
823 std::string localFile = flNotDir(I->Path);
824 if (FileExists(localFile) == true)
825 if(I->Hashes.VerifyFile(localFile) == true)
092ae175 826 {
443cb67c 827 ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
1262d358 828 localFile.c_str());
092ae175
MV
829 continue;
830 }
1262d358
DK
831
832 // see if we have a hash (Acquire::ForceHash is the only way to have none)
b3501edb 833 if (I->Hashes.usable() == false && _config->FindB("APT::Get::AllowUnauthenticated",false) == false)
1262d358
DK
834 {
835 ioprintf(c1out, "Skipping download of file '%s' as requested hashsum is not available for authentication\n",
836 localFile.c_str());
837 continue;
092ae175
MV
838 }
839
b2e465d6 840 new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
d61960d9 841 I->Hashes, I->FileSize, Last->Index().SourceInfo(*Last,*I), Src);
36375005
AL
842 }
843 }
a3f1d60c 844
36375005 845 // Display statistics
3a882565
DK
846 unsigned long long FetchBytes = Fetcher.FetchNeeded();
847 unsigned long long FetchPBytes = Fetcher.PartialPresent();
848 unsigned long long DebBytes = Fetcher.TotalNeeded();
36375005 849
9c81f8de
DK
850 if (CheckFreeSpaceBeforeDownload(".", (FetchBytes - FetchPBytes)) == false)
851 return false;
852
36375005 853 // Number of bytes
36375005 854 if (DebBytes != FetchBytes)
4d8d8112
DK
855 //TRANSLATOR: The required space between number and unit is already included
856 // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
857 ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
858 SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
36375005 859 else
4d8d8112
DK
860 //TRANSLATOR: The required space between number and unit is already included
861 // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
b2e465d6
AL
862 ioprintf(c1out,_("Need to get %sB of source archives.\n"),
863 SizeToStr(DebBytes).c_str());
864
2c0c53b3
AL
865 if (_config->FindB("APT::Get::Simulate",false) == true)
866 {
867 for (unsigned I = 0; I != J; I++)
db0db9fe 868 ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
2c0c53b3
AL
869 return true;
870 }
460601d5 871
36375005
AL
872 // Just print out the uris an exit if the --print-uris flag was used
873 if (_config->FindB("APT::Get::Print-URIs") == true)
874 {
875 pkgAcquire::UriIterator I = Fetcher.UriBegin();
f7f0d6c7 876 for (; I != Fetcher.UriEnd(); ++I)
36375005 877 cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
495e5cb2 878 I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
36375005
AL
879 return true;
880 }
36375005 881
460601d5
DK
882 // Disable drop-privs if "_apt" can not write to the target dir
883 CheckDropPrivsMustBeDisabled(Fetcher);
884
885 // check authentication status of the source as well
886 if (UntrustedList != "" && !AuthPrompt(UntrustedList, false))
887 return false;
888
866893a6 889 // Run it
fb0ee66e 890 bool Failed = false;
866893a6 891 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
6070a346 892 {
b2e465d6 893 return _error->Error(_("Failed to fetch some archives."));
6070a346
DK
894 }
895
fb0ee66e 896 if (_config->FindB("APT::Get::Download-only",false) == true)
b2e465d6
AL
897 {
898 c1out << _("Download complete and in download only mode") << endl;
fb0ee66e 899 return true;
b2e465d6
AL
900 }
901
fb0ee66e 902 // Unpack the sources
54676e1a
AL
903 pid_t Process = ExecFork();
904
905 if (Process == 0)
fb0ee66e 906 {
827d04d3 907 bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
f7f0d6c7 908 for (unsigned I = 0; I != J; ++I)
fb0ee66e 909 {
b2e465d6 910 string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
fb0ee66e 911
17c0e8e1
AL
912 // Diff only mode only fetches .diff files
913 if (_config->FindB("APT::Get::Diff-Only",false) == true ||
a3eaf954
AL
914 _config->FindB("APT::Get::Tar-Only",false) == true ||
915 Dsc[I].Dsc.empty() == true)
17c0e8e1 916 continue;
a3eaf954 917
54676e1a
AL
918 // See if the package is already unpacked
919 struct stat Stat;
827d04d3 920 if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
54676e1a
AL
921 S_ISDIR(Stat.st_mode) != 0)
922 {
b2e465d6
AL
923 ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
924 Dir.c_str());
54676e1a
AL
925 }
926 else
927 {
928 // Call dpkg-source
c53cf70d
DK
929 std::string const sourceopts = _config->Find("DPkg::Source-Options", "-x");
930 std::string S;
931 strprintf(S, "%s %s %s",
54676e1a 932 _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
c53cf70d
DK
933 sourceopts.c_str(), Dsc[I].Dsc.c_str());
934 if (system(S.c_str()) != 0)
54676e1a 935 {
c53cf70d
DK
936 fprintf(stderr, _("Unpack command '%s' failed.\n"), S.c_str());
937 fprintf(stderr, _("Check if the 'dpkg-dev' package is installed.\n"));
54676e1a 938 _exit(1);
c53cf70d 939 }
54676e1a 940 }
c53cf70d 941
54676e1a
AL
942 // Try to compile it with dpkg-buildpackage
943 if (_config->FindB("APT::Get::Compile",false) == true)
944 {
234675b7
DK
945 string buildopts = _config->Find("APT::Get::Host-Architecture");
946 if (buildopts.empty() == false)
c5102538 947 buildopts = "-a" + buildopts + " ";
ce7f128c
DK
948
949 // get all active build profiles
950 std::string const profiles = APT::Configuration::getBuildProfilesString();
951 if (profiles.empty() == false)
952 buildopts.append(" -P").append(profiles).append(" ");
953
234675b7
DK
954 buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
955
54676e1a 956 // Call dpkg-buildpackage
c53cf70d
DK
957 std::string S;
958 strprintf(S, "cd %s && %s %s",
54676e1a
AL
959 Dir.c_str(),
960 _config->Find("Dir::Bin::dpkg-buildpackage","dpkg-buildpackage").c_str(),
234675b7 961 buildopts.c_str());
c53cf70d
DK
962
963 if (system(S.c_str()) != 0)
54676e1a 964 {
c53cf70d 965 fprintf(stderr, _("Build command '%s' failed.\n"), S.c_str());
54676e1a 966 _exit(1);
c53cf70d
DK
967 }
968 }
54676e1a 969 }
c53cf70d 970
54676e1a
AL
971 _exit(0);
972 }
3a4477a4 973
54676e1a
AL
974 // Wait for the subprocess
975 int Status = 0;
976 while (waitpid(Process,&Status,0) != Process)
977 {
978 if (errno == EINTR)
979 continue;
980 return _error->Errno("waitpid","Couldn't wait for subprocess");
981 }
982
983 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
b2e465d6
AL
984 return _error->Error(_("Child process failed"));
985
986 return true;
987}
988 /*}}}*/
989// DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
990// ---------------------------------------------------------------------
991/* This function will look at the build depends list of the given source
992 package and install the necessary packages to make it true, or fail. */
c3ccac92 993static bool DoBuildDep(CommandLine &CmdL)
b2e465d6
AL
994{
995 CacheFile Cache;
35180212
JAK
996
997 _config->Set("APT::Install-Recommends", false);
998
b2e465d6
AL
999 if (Cache.Open(true) == false)
1000 return false;
1001
1002 if (CmdL.FileSize() <= 1)
1003 return _error->Error(_("Must specify at least one package to check builddeps for"));
1004
1005 // Read the source list
1bb8cd67
DK
1006 if (Cache.BuildSourceList() == false)
1007 return false;
1008 pkgSourceList *List = Cache.GetSourceList();
54676e1a 1009
b2e465d6 1010 // Create the text record parsers
1bb8cd67 1011 pkgSrcRecords SrcRecs(*List);
b2e465d6
AL
1012 if (_error->PendingError() == true)
1013 return false;
1014
234675b7
DK
1015 bool StripMultiArch;
1016 string hostArch = _config->Find("APT::Get::Host-Architecture");
1017 if (hostArch.empty() == false)
1018 {
1019 std::vector<std::string> archs = APT::Configuration::getArchitectures();
1020 if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
1021 return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
1022 StripMultiArch = false;
1023 }
1024 else
1025 StripMultiArch = true;
1026
b2e465d6
AL
1027 unsigned J = 0;
1028 for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
1029 {
1030 string Src;
feab34c5 1031 pkgSrcRecords::Parser *Last = 0;
3d8232bf 1032 SPtr<pkgSrcRecords::Parser> LastOwner;
feab34c5 1033
a5bb5e1e
MV
1034 // an unpacked debian source tree
1035 using APT::String::Startswith;
1036 if ((Startswith(*I, "./") || Startswith(*I, "/")) &&
1037 DirectoryExists(*I))
77da39b9 1038 {
a5bb5e1e 1039 ioprintf(c1out, _("Note, using directory '%s' to get the build dependencies\n"), *I);
77da39b9 1040 // FIXME: how can we make this more elegant?
463c8d80 1041 std::string TypeName = "Debian control file";
77da39b9
MV
1042 pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
1043 if(Type != NULL)
3d8232bf 1044 LastOwner = Last = Type->CreateSrcPkgParser(*I);
77da39b9 1045 }
a49e7948 1046 // if its a local file (e.g. .dsc) use this
77da39b9 1047 else if (FileExists(*I))
feab34c5 1048 {
a5bb5e1e
MV
1049 ioprintf(c1out, _("Note, using file '%s' to get the build dependencies\n"), *I);
1050
a49e7948 1051 // see if we can get a parser for this pkgIndexFile type
463c8d80 1052 string TypeName = "Debian " + flExtension(*I) + " file";
a49e7948
MV
1053 pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(TypeName.c_str());
1054 if(Type != NULL)
3d8232bf 1055 LastOwner = Last = Type->CreateSrcPkgParser(*I);
feab34c5 1056 } else {
a49e7948 1057 // normal case, search the cache for the source file
765190e4
DK
1058#if APT_PKG_ABI >= 413
1059 Last = FindSrc(*I,SrcRecs,Src,Cache);
1060#else
1061 Last = FindSrc(*I,Recs,SrcRecs,Src,Cache);
1062#endif
feab34c5 1063 }
a49e7948 1064
b2e465d6
AL
1065 if (Last == 0)
1066 return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
1067
1068 // Process the build-dependencies
1069 vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
65f99834
DK
1070 // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
1071 if (hostArch.empty() == false)
1072 {
1073 std::string nativeArch = _config->Find("APT::Architecture");
1074 _config->Set("APT::Architecture", hostArch);
1075 bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
1076 _config->Set("APT::Architecture", nativeArch);
1077 if (Success == false)
1078 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
1079 }
1080 else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
1081 return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
feab34c5 1082
7d6f9f8f
AL
1083 // Also ensure that build-essential packages are present
1084 Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
1085 if (Opts)
1086 Opts = Opts->Child;
1087 for (; Opts; Opts = Opts->Next)
1088 {
1089 if (Opts->Value.empty() == true)
1090 continue;
1091
1092 pkgSrcRecords::Parser::BuildDepRec rec;
1093 rec.Package = Opts->Value;
1094 rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
1095 rec.Op = 0;
58d76831 1096 BuildDeps.push_back(rec);
7d6f9f8f
AL
1097 }
1098
f7f0d6c7 1099 if (BuildDeps.empty() == true)
b2e465d6
AL
1100 {
1101 ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
1102 continue;
1103 }
234675b7 1104
b2e465d6 1105 // Install the requested packages
b2e465d6
AL
1106 vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
1107 pkgProblemResolver Fix(Cache);
58d76831 1108 bool skipAlternatives = false; // skip remaining alternatives in an or group
f7f0d6c7 1109 for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
b2e465d6 1110 {
58d76831
AL
1111 bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
1112
1113 if (skipAlternatives == true)
1114 {
3fdb6f83
DK
1115 /*
1116 * if there are alternatives, we've already picked one, so skip
1117 * the rest
1118 *
1119 * TODO: this means that if there's a build-dep on A|B and B is
1120 * installed, we'll still try to install A; more importantly,
1121 * if A is currently broken, we cannot go back and try B. To fix
1122 * this would require we do a Resolve cycle for each package we
1123 * add to the install list. Ugh
1124 */
58d76831
AL
1125 if (!hasAlternatives)
1126 skipAlternatives = false; // end of or group
1127 continue;
1128 }
1129
aa2d22be
AL
1130 if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
1131 (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
d3fc0061 1132 {
e3a86238 1133 pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
aa2d22be 1134 // Build-conflicts on unknown packages are silently ignored
e3a86238 1135 if (Grp.end() == true)
aa2d22be
AL
1136 continue;
1137
e3a86238
DK
1138 for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
1139 {
1140 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
1141 /*
1142 * Remove if we have an installed version that satisfies the
1143 * version criteria
1144 */
1145 if (IV.end() == false &&
1146 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
1147 TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
1148 }
d3fc0061 1149 }
aa2d22be
AL
1150 else // BuildDep || BuildDepIndep
1151 {
58d76831
AL
1152 if (_config->FindB("Debug::BuildDeps",false) == true)
1153 cout << "Looking for " << (*D).Package << "...\n";
1154
234675b7
DK
1155 pkgCache::PkgIterator Pkg;
1156
1157 // Cross-Building?
c307a4f0 1158 if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
234675b7
DK
1159 {
1160 size_t const colon = D->Package.find(":");
666faa35
DK
1161 if (colon != string::npos)
1162 {
1163 if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
1164 Pkg = Cache->FindPkg(D->Package.substr(0,colon));
1165 else
1166 Pkg = Cache->FindPkg(D->Package);
1167 }
234675b7 1168 else
666faa35 1169 Pkg = Cache->FindPkg(D->Package, hostArch);
234675b7 1170
234675b7 1171 // a bad version either is invalid or doesn't satify dependency
d5dea0be
DK
1172 #define BADVER(Ver) (Ver.end() == true || \
1173 (D->Version.empty() == false && \
1174 Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
234675b7 1175
d5dea0be 1176 APT::VersionList verlist;
234675b7
DK
1177 if (Pkg.end() == false)
1178 {
d5dea0be
DK
1179 pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
1180 if (BADVER(Ver) == false)
1181 verlist.insert(Ver);
1182 Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
1183 if (BADVER(Ver) == false)
1184 verlist.insert(Ver);
234675b7 1185 }
d5dea0be 1186 if (verlist.empty() == true)
234675b7 1187 {
666faa35
DK
1188 pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
1189 if (BuildPkg.end() == false && Pkg != BuildPkg)
234675b7 1190 {
666faa35 1191 pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
d5dea0be
DK
1192 if (BADVER(Ver) == false)
1193 verlist.insert(Ver);
666faa35 1194 Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
d5dea0be
DK
1195 if (BADVER(Ver) == false)
1196 verlist.insert(Ver);
234675b7
DK
1197 }
1198 }
d5dea0be
DK
1199 #undef BADVER
1200
1201 string forbidden;
1202 // We need to decide if host or build arch, so find a version we can look at
1203 APT::VersionList::const_iterator Ver = verlist.begin();
1204 for (; Ver != verlist.end(); ++Ver)
234675b7 1205 {
d5dea0be 1206 forbidden.clear();
737c7a7b
SL
1207 if (Ver->MultiArch == pkgCache::Version::None || Ver->MultiArch == pkgCache::Version::All)
1208 {
1209 if (colon == string::npos)
737c7a7b 1210 Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
a9a370d9
TG
1211 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
1212 forbidden = "Multi-Arch: none";
666faa35
DK
1213 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
1214 Pkg = Ver.ParentPkg().Group().FindPkg("native");
737c7a7b 1215 }
234675b7
DK
1216 else if (Ver->MultiArch == pkgCache::Version::Same)
1217 {
737c7a7b 1218 if (colon == string::npos)
234675b7
DK
1219 Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
1220 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
1221 forbidden = "Multi-Arch: same";
666faa35
DK
1222 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
1223 Pkg = Ver.ParentPkg().Group().FindPkg("native");
234675b7 1224 }
2a2a7ef4 1225 else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
234675b7 1226 {
666faa35
DK
1227 if (colon == string::npos)
1228 Pkg = Ver.ParentPkg().Group().FindPkg("native");
1229 else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
1230 strcmp(D->Package.c_str() + colon, ":native") == 0)
234675b7
DK
1231 forbidden = "Multi-Arch: foreign";
1232 }
2a2a7ef4 1233 else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
234675b7
DK
1234 {
1235 if (colon == string::npos)
1236 Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
1237 else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
1238 {
1239 // prefer any installed over preferred non-installed architectures
1240 pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
1241 // we don't check for version here as we are better of with upgrading than remove and install
1242 for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
1243 if (Pkg.CurrentVer().end() == false)
1244 break;
1245 if (Pkg.end() == true)
1246 Pkg = Grp.FindPreferredPkg(true);
1247 }
666faa35
DK
1248 else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
1249 Pkg = Ver.ParentPkg().Group().FindPkg("native");
234675b7 1250 }
d5dea0be 1251
234675b7
DK
1252 if (forbidden.empty() == false)
1253 {
1254 if (_config->FindB("Debug::BuildDeps",false) == true)
d5dea0be
DK
1255 cout << D->Package.substr(colon, string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << endl;
1256 continue;
1257 }
1258
1259 //we found a good version
1260 break;
1261 }
1262 if (Ver == verlist.end())
1263 {
1264 if (_config->FindB("Debug::BuildDeps",false) == true)
1265 cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
1266
1267 if (forbidden.empty() == false)
1268 {
234675b7
DK
1269 if (hasAlternatives)
1270 continue;
1271 return _error->Error(_("%s dependency for %s can't be satisfied "
1272 "because %s is not allowed on '%s' packages"),
1273 Last->BuildDepType(D->Type), Src.c_str(),
a9a370d9 1274 D->Package.c_str(), forbidden.c_str());
234675b7
DK
1275 }
1276 }
234675b7
DK
1277 }
1278 else
1279 Pkg = Cache->FindPkg(D->Package);
1280
727d8712 1281 if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
aa2d22be 1282 {
58d76831
AL
1283 if (_config->FindB("Debug::BuildDeps",false) == true)
1284 cout << " (not found)" << (*D).Package << endl;
1285
1286 if (hasAlternatives)
1287 continue;
1288
1289 return _error->Error(_("%s dependency for %s cannot be satisfied "
1290 "because the package %s cannot be found"),
1291 Last->BuildDepType((*D).Type),Src.c_str(),
1292 (*D).Package.c_str());
aa2d22be
AL
1293 }
1294
e5002e30 1295 pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
91bee655
DK
1296 if (IV.end() == false)
1297 {
1298 if (_config->FindB("Debug::BuildDeps",false) == true)
1299 cout << " Is installed\n";
e5002e30 1300
91bee655
DK
1301 if (D->Version.empty() == true ||
1302 Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
1303 {
1304 skipAlternatives = hasAlternatives;
1305 continue;
1306 }
cfa5659c 1307
91bee655
DK
1308 if (_config->FindB("Debug::BuildDeps",false) == true)
1309 cout << " ...but the installed version doesn't meet the version requirement\n";
58d76831 1310
91bee655
DK
1311 if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
1312 return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
1313 Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
1314 }
58d76831 1315
fe027879
DK
1316 // Only consider virtual packages if there is no versioned dependency
1317 if ((*D).Version.empty() == true)
1318 {
1319 /*
1320 * If this is a virtual package, we need to check the list of
1321 * packages that provide it and see if any of those are
1322 * installed
1323 */
1324 pkgCache::PrvIterator Prv = Pkg.ProvidesList();
f7f0d6c7 1325 for (; Prv.end() != true; ++Prv)
fe027879
DK
1326 {
1327 if (_config->FindB("Debug::BuildDeps",false) == true)
1328 cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
58d76831 1329
fe027879
DK
1330 if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
1331 break;
1332 }
58d76831 1333
fe027879
DK
1334 if (Prv.end() == false)
1335 {
1336 if (_config->FindB("Debug::BuildDeps",false) == true)
1337 cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
1338 skipAlternatives = hasAlternatives;
1339 continue;
1340 }
1341 }
2baf02ca
DK
1342 else // versioned dependency
1343 {
1344 pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
1345 if (CV.end() == true ||
1346 Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
1347 {
1348 if (hasAlternatives)
1349 continue;
1350 else if (CV.end() == false)
1351 return _error->Error(_("%s dependency for %s cannot be satisfied "
1352 "because candidate version of package %s "
1353 "can't satisfy version requirements"),
1354 Last->BuildDepType(D->Type), Src.c_str(),
1355 D->Package.c_str());
1356 else
1357 return _error->Error(_("%s dependency for %s cannot be satisfied "
1358 "because package %s has no candidate version"),
1359 Last->BuildDepType(D->Type), Src.c_str(),
1360 D->Package.c_str());
1361 }
1362 }
58d76831 1363
f1f874bd 1364 if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
58d76831
AL
1365 {
1366 // We successfully installed something; skip remaining alternatives
1367 skipAlternatives = hasAlternatives;
d59228b0 1368 if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
496a05c6 1369 Cache->MarkAuto(Pkg, true);
58d76831
AL
1370 continue;
1371 }
1372 else if (hasAlternatives)
1373 {
1374 if (_config->FindB("Debug::BuildDeps",false) == true)
1375 cout << " Unsatisfiable, trying alternatives\n";
1376 continue;
1377 }
1378 else
1379 {
1380 return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
1381 Last->BuildDepType((*D).Type),
1382 Src.c_str(),
1383 (*D).Package.c_str());
1384 }
b2e465d6
AL
1385 }
1386 }
f3c736f9 1387
b2e465d6
AL
1388 if (Fix.Resolve(true) == false)
1389 _error->Discard();
1390
1391 // Now we check the state of the packages,
1392 if (Cache->BrokenCount() != 0)
0dae8ac5
DK
1393 {
1394 ShowBroken(cout, Cache, false);
1395 return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
1396 }
b2e465d6
AL
1397 }
1398
1399 if (InstallPackages(Cache, false, true) == false)
1400 return _error->Error(_("Failed to process build dependencies"));
36375005
AL
1401 return true;
1402}
1403 /*}}}*/
a4c40430 1404// DoChangelog - Get changelog from the command line /*{{{*/
c3ccac92 1405static bool DoChangelog(CommandLine &CmdL)
a4c40430
MV
1406{
1407 CacheFile Cache;
1408 if (Cache.ReadOnlyOpen() == false)
1409 return false;
d56e2917 1410
353c135e 1411 APT::CacheSetHelper helper;
c4cca791 1412 APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
43acd019 1413 CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
72dd5bec
DK
1414 if (verset.empty() == true)
1415 return false;
a4c40430 1416 pkgAcquire Fetcher;
d56e2917
DK
1417 AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
1418 Fetcher.SetLog(&Stat);
fcb144b9 1419
d56e2917
DK
1420 bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
1421 bool const printOnly = _config->FindB("APT::Get::Print-URIs", false);
1422
1423 for (APT::VersionList::const_iterator Ver = verset.begin();
1424 Ver != verset.end();
1425 ++Ver)
a98b6615 1426 {
d56e2917
DK
1427 if (printOnly)
1428 new pkgAcqChangelog(&Fetcher, Ver, "/dev/null");
1429 else if (downOnly)
1430 new pkgAcqChangelog(&Fetcher, Ver, ".");
1431 else
1432 new pkgAcqChangelog(&Fetcher, Ver);
a98b6615 1433 }
fcb144b9 1434
d56e2917
DK
1435 if (printOnly == false)
1436 {
1437 // Disable drop-privs if "_apt" can not write to the target dir
1438 CheckDropPrivsMustBeDisabled(Fetcher);
1439 if (_error->PendingError() == true)
1440 return false;
a4c40430 1441
d56e2917
DK
1442 bool Failed = false;
1443 if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
1444 return false;
1445 }
72dd5bec 1446
d56e2917 1447 if (downOnly == false || printOnly == true)
72dd5bec 1448 {
d56e2917
DK
1449 bool Failed = false;
1450 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
03aa0847 1451 {
d56e2917 1452 if (printOnly)
03aa0847 1453 {
d56e2917
DK
1454 if ((*I)->ErrorText.empty() == false)
1455 {
1456 Failed = true;
1457 _error->Error("%s", (*I)->ErrorText.c_str());
1458 }
1459 else
1460 cout << '\'' << (*I)->DescURI() << "' " << flNotDir((*I)->DestFile) << std::endl;
03aa0847 1461 }
d56e2917
DK
1462 else
1463 DisplayFileInPager((*I)->DestFile);
03aa0847 1464 }
d56e2917 1465 return Failed == false;
18ae8b29 1466 }
72dd5bec 1467
4a6fe09c 1468 return true;
a4c40430
MV
1469}
1470 /*}}}*/
8881b11e
DK
1471// DoFiles - Lists all IndexTargets /*{{{*/
1472static std::string format_key(std::string key)
1473{
1474 // deb822 is case-insensitive, but the human eye prefers candy
1475 std::transform(key.begin(), key.end(), key.begin(), ::tolower);
1476 key[0] = ::toupper(key[0]);
1477 size_t found = key.find("_uri");
1478 if (found != std::string::npos)
1479 key.replace(found, 4, "-URI");
1480 while ((found = key.find('_')) != std::string::npos)
1481 {
1482 key[found] = '-';
1483 key[found + 1] = ::toupper(key[found + 1]);
1484 }
1485 return key;
1486}
1487static bool DoFiles(CommandLine &CmdL)
1488{
1489 pkgCacheFile CacheFile;
1490 pkgSourceList *SrcList = CacheFile.GetSourceList();
1491
1492 if (SrcList == NULL)
1493 return false;
1494
1495 std::string const Format = _config->Find("APT::Get::Files::Format");
3fd89e62 1496 bool const ReleaseInfo = _config->FindB("APT::Get::Files::ReleaseInfo", true);
8881b11e
DK
1497 bool Filtered = CmdL.FileSize() > 1;
1498 for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
1499 {
1500 std::vector<IndexTarget> const targets = (*S)->GetIndexTargets();
1501 std::map<std::string, string> AddOptions;
3fd89e62
DK
1502 if (ReleaseInfo)
1503 {
1504 AddOptions.insert(std::make_pair("TRUSTED", ((*S)->IsTrusted() ? "yes" : "no")));
1505 pkgCache &Cache = *CacheFile.GetPkgCache();
1506 pkgCache::RlsFileIterator const RlsFile = (*S)->FindInCache(Cache, false);
1507 if (RlsFile.end())
1508 continue;
1509#define APT_RELEASE(X,Y) if (RlsFile.Y() != NULL) AddOptions.insert(std::make_pair(X, RlsFile.Y()))
1510 APT_RELEASE("CODENAME", Codename);
1511 APT_RELEASE("SUITE", Archive);
1512 APT_RELEASE("VERSION", Version);
1513 APT_RELEASE("ORIGIN", Origin);
1514 APT_RELEASE("LABEL", Label);
1515#undef APT_RELEASE
1516 }
8881b11e
DK
1517
1518 for (std::vector<IndexTarget>::const_iterator T = targets.begin(); T != targets.end(); ++T)
1519 {
3fd89e62
DK
1520 std::string filename = T->Option(ReleaseInfo ? IndexTarget::EXISTING_FILENAME : IndexTarget::FILENAME);
1521 if (filename.empty())
1522 continue;
1523
8881b11e
DK
1524 std::ostringstream stanza;
1525 if (Filtered || Format.empty())
1526 {
1527 stanza << "MetaKey: " << T->MetaKey << "\n"
1528 << "ShortDesc: " << T->ShortDesc << "\n"
1529 << "Description: " << T->Description << "\n"
1530 << "URI: " << T->URI << "\n"
3fd89e62 1531 << "Filename: " << filename << "\n"
8881b11e
DK
1532 << "Optional: " << (T->IsOptional ? "yes" : "no") << "\n";
1533 for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
1534 stanza << format_key(O->first) << ": " << O->second << "\n";
1535 for (std::map<std::string,std::string>::const_iterator O = T->Options.begin(); O != T->Options.end(); ++O)
1536 stanza << format_key(O->first) << ": " << O->second << "\n";
1537 stanza << "\n";
1538
1539 if (Filtered)
1540 {
1541 // that is a bit crude, but good enough for now
1542 bool found = true;
1543 std::string haystack = std::string("\n") + stanza.str() + "\n";
1544 std::transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower);
1545 size_t const filesize = CmdL.FileSize() - 1;
1546 for (size_t i = 0; i != filesize; ++i)
1547 {
1548 std::string needle = std::string("\n") + CmdL.FileList[i + 1] + "\n";
1549 std::transform(needle.begin(), needle.end(), needle.begin(), ::tolower);
1550 if (haystack.find(needle) != std::string::npos)
1551 continue;
1552 found = false;
1553 break;
1554 }
1555 if (found == false)
1556 continue;
1557 }
1558 }
1559
1560 if (Format.empty())
1561 cout << stanza.str();
1562 else
1563 {
3fd89e62
DK
1564 std::string out = SubstVar(Format, "$(FILENAME)", filename);
1565 out = T->Format(out);
8881b11e
DK
1566 for (std::map<std::string,std::string>::const_iterator O = AddOptions.begin(); O != AddOptions.end(); ++O)
1567 out = SubstVar(out, std::string("$(") + O->first + ")", O->second);
1568 cout << out << std::endl;
1569 }
1570 }
1571 }
1572
1573 return true;
1574}
1575 /*}}}*/
0a8e3465
AL
1576// ShowHelp - Show a help screen /*{{{*/
1577// ---------------------------------------------------------------------
1578/* */
65512241 1579static bool ShowHelp(CommandLine &)
0a8e3465 1580{
249aec3b
JB
1581 ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
1582
04aa15a8 1583 if (_config->FindB("version") == true)
b2e465d6 1584 {
db0db9fe 1585 cout << _("Supported modules:") << endl;
249aec3b 1586
b2e465d6
AL
1587 for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
1588 {
1589 pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
1590 if (_system != 0 && _system->VS == VS)
1591 cout << '*';
1592 else
1593 cout << ' ';
1594 cout << "Ver: " << VS->Label << endl;
249aec3b 1595
b2e465d6
AL
1596 /* Print out all the packaging systems that will work with
1597 this VS */
1598 for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
1599 {
1600 pkgSystem *Sys = pkgSystem::GlobalList[J];
1601 if (_system == Sys)
1602 cout << '*';
1603 else
1604 cout << ' ';
1605 if (Sys->VS->TestCompatibility(*VS) == true)
1606 cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
1607 }
1608 }
1609
1610 for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
1611 {
1612 pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
1613 cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
1614 }
1615
1616 for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
1617 {
1618 pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
1619 cout << " Idx: " << Type->Label << endl;
1620 }
1621
1622 return true;
1623 }
1624
1625 cout <<
1626 _("Usage: apt-get [options] command\n"
1627 " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
1628 " apt-get [options] source pkg1 [pkg2 ...]\n"
1629 "\n"
1630 "apt-get is a simple command line interface for downloading and\n"
1631 "installing packages. The most frequently used commands are update\n"
1632 "and install.\n"
1633 "\n"
1634 "Commands:\n"
1635 " update - Retrieve new lists of packages\n"
1636 " upgrade - Perform an upgrade\n"
1637 " install - Install new packages (pkg is libc6 not libc6.deb)\n"
1638 " remove - Remove packages\n"
12bffed7 1639 " autoremove - Remove automatically all unused packages\n"
73fc19d0 1640 " purge - Remove packages and config files\n"
b2e465d6
AL
1641 " source - Download source archives\n"
1642 " build-dep - Configure build-dependencies for source packages\n"
1643 " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
1644 " dselect-upgrade - Follow dselect selections\n"
1645 " clean - Erase downloaded archive files\n"
1646 " autoclean - Erase old downloaded archive files\n"
1647 " check - Verify that there are no broken dependencies\n"
5f967f2d
MV
1648 " changelog - Download and display the changelog for the given package\n"
1649 " download - Download the binary package into the current directory\n"
b2e465d6
AL
1650 "\n"
1651 "Options:\n"
1652 " -h This help text.\n"
1653 " -q Loggable output - no progress indicator\n"
1654 " -qq No output except for errors\n"
1655 " -d Download only - do NOT install or unpack archives\n"
1656 " -s No-act. Perform ordering simulation\n"
1657 " -y Assume Yes to all queries and do not prompt\n"
0748d509 1658 " -f Attempt to correct a system with broken dependencies in place\n"
b2e465d6
AL
1659 " -m Attempt to continue if archives are unlocatable\n"
1660 " -u Show a list of upgraded packages as well\n"
1661 " -b Build the source package after fetching it\n"
ac625538 1662 " -V Show verbose version numbers\n"
b2e465d6 1663 " -c=? Read this configuration file\n"
a2884e32 1664 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
1665 "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
1666 "pages for more information and options.\n"
1667 " This APT has Super Cow Powers.\n");
1668 return true;
d7827aca
AL
1669}
1670 /*}}}*/
92fcbfc1 1671int main(int argc,const char *argv[]) /*{{{*/
0a8e3465 1672{
83d89a9f
AL
1673 CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
1674 {"upgrade",&DoUpgrade},
1675 {"install",&DoInstall},
1676 {"remove",&DoInstall},
24401c09 1677 {"purge",&DoInstall},
74a05226 1678 {"autoremove",&DoInstall},
d63a1458
JAK
1679 {"markauto",&DoMarkAuto},
1680 {"unmarkauto",&DoMarkAuto},
83d89a9f
AL
1681 {"dist-upgrade",&DoDistUpgrade},
1682 {"dselect-upgrade",&DoDSelectUpgrade},
b2e465d6 1683 {"build-dep",&DoBuildDep},
83d89a9f 1684 {"clean",&DoClean},
1bc849af 1685 {"autoclean",&DoAutoClean},
83d89a9f 1686 {"check",&DoCheck},
67111687 1687 {"source",&DoSource},
459b5f5d 1688 {"download",&DoDownload},
a4c40430 1689 {"changelog",&DoChangelog},
8881b11e 1690 {"files",&DoFiles},
b2e465d6 1691 {"moo",&DoMoo},
67111687 1692 {"help",&ShowHelp},
83d89a9f 1693 {0,0}};
67111687 1694
b9179170
MV
1695 std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", CommandLine::GetCommand(Cmds, argc, argv));
1696
67111687
AL
1697 // Set up gettext support
1698 setlocale(LC_ALL,"");
1699 textdomain(PACKAGE);
1700
0a8e3465 1701 // Parse the command line and initialize the package library
ad7e0941
DK
1702 CommandLine CmdL;
1703 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
55a5a46c 1704
b9179170
MV
1705 // see if we are in simulate mode
1706 CheckSimulateMode(CmdL);
55a5a46c 1707
3163087b
MV
1708 // Init the signals
1709 InitSignals();
1710
0a8e3465 1711 // Setup the output streams
b9179170 1712 InitOutput();
d7827aca 1713
0a8e3465 1714 // Match the operation
83d89a9f 1715 CmdL.DispatchArg(Cmds);
0a8e3465
AL
1716
1717 // Print any errors or warnings found during parsing
65beb572
DK
1718 bool const Errors = _error->PendingError();
1719 if (_config->FindI("quiet",0) > 0)
0a8e3465 1720 _error->DumpErrors();
65beb572
DK
1721 else
1722 _error->DumpErrors(GlobalError::DEBUG);
1723 return Errors == true ? 100 : 0;
0a8e3465 1724}
92fcbfc1 1725 /*}}}*/