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