apt-get.cc 58 KB

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