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