apt-get.cc 59 KB

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