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