apt-get.cc 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. _error->Error(_("Can not find a package '%s' with version '%s' and "
  292. "release '%s'"), Pkg.FullName().c_str(),
  293. VerTag.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. pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
  307. Src = Parse.SourcePkg();
  308. if (VerTag.empty() == true)
  309. VerTag = Parse.SourceVer();
  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. 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. bool DoDSelectUpgrade(CommandLine &CmdL)
  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 agressively 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 (pkgAllUpgrade(Cache) == 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. bool DoClean(CommandLine &CmdL)
  491. {
  492. std::string const archivedir = _config->FindDir("Dir::Cache::archives");
  493. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  494. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  495. if (_config->FindB("APT::Get::Simulate") == true)
  496. {
  497. cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< endl
  498. << "Del " << pkgcache << " " << srcpkgcache << endl;
  499. return true;
  500. }
  501. // Lock the archive directory
  502. FileFd Lock;
  503. if (_config->FindB("Debug::NoLocking",false) == false)
  504. {
  505. int lock_fd = GetLock(archivedir + "lock");
  506. if (lock_fd < 0)
  507. return _error->Error(_("Unable to lock the download directory"));
  508. Lock.Fd(lock_fd);
  509. }
  510. pkgAcquire Fetcher;
  511. Fetcher.Clean(archivedir);
  512. Fetcher.Clean(archivedir + "partial/");
  513. pkgCacheFile::RemoveCaches();
  514. return true;
  515. }
  516. /*}}}*/
  517. // DoAutoClean - Smartly remove downloaded archives /*{{{*/
  518. // ---------------------------------------------------------------------
  519. /* This is similar to clean but it only purges things that cannot be
  520. downloaded, that is old versions of cached packages. */
  521. class LogCleaner : public pkgArchiveCleaner
  522. {
  523. protected:
  524. virtual void Erase(const char *File,string Pkg,string Ver,struct stat &St)
  525. {
  526. c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << endl;
  527. if (_config->FindB("APT::Get::Simulate") == false)
  528. unlink(File);
  529. };
  530. };
  531. bool DoAutoClean(CommandLine &CmdL)
  532. {
  533. // Lock the archive directory
  534. FileFd Lock;
  535. if (_config->FindB("Debug::NoLocking",false) == false)
  536. {
  537. int lock_fd = GetLock(_config->FindDir("Dir::Cache::Archives") + "lock");
  538. if (lock_fd < 0)
  539. return _error->Error(_("Unable to lock the download directory"));
  540. Lock.Fd(lock_fd);
  541. }
  542. CacheFile Cache;
  543. if (Cache.Open() == false)
  544. return false;
  545. LogCleaner Cleaner;
  546. return Cleaner.Go(_config->FindDir("Dir::Cache::archives"),*Cache) &&
  547. Cleaner.Go(_config->FindDir("Dir::Cache::archives") + "partial/",*Cache);
  548. }
  549. /*}}}*/
  550. // DoDownload - download a binary /*{{{*/
  551. // ---------------------------------------------------------------------
  552. bool DoDownload(CommandLine &CmdL)
  553. {
  554. CacheFile Cache;
  555. if (Cache.ReadOnlyOpen() == false)
  556. return false;
  557. APT::CacheSetHelper helper(c0out);
  558. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  559. CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
  560. if (verset.empty() == true)
  561. return false;
  562. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet", 0));
  563. pkgAcquire Fetcher;
  564. if (Fetcher.Setup(&Stat) == false)
  565. return false;
  566. pkgRecords Recs(Cache);
  567. pkgSourceList *SrcList = Cache.GetSourceList();
  568. // reuse the usual acquire methods for deb files, but don't drop them into
  569. // the usual directories - keep everything in the current directory
  570. std::vector<std::string> storefile(verset.size());
  571. std::string const cwd = SafeGetCWD();
  572. _config->Set("Dir::Cache::Archives", cwd);
  573. int i = 0;
  574. for (APT::VersionList::const_iterator Ver = verset.begin();
  575. Ver != verset.end(); ++Ver, ++i)
  576. {
  577. pkgAcquire::Item *I = new pkgAcqArchive(&Fetcher, SrcList, &Recs, *Ver, storefile[i]);
  578. std::string const filename = cwd + flNotDir(storefile[i]);
  579. storefile[i].assign(filename);
  580. I->DestFile.assign(filename);
  581. }
  582. // Just print out the uris and exit if the --print-uris flag was used
  583. if (_config->FindB("APT::Get::Print-URIs") == true)
  584. {
  585. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  586. for (; I != Fetcher.UriEnd(); ++I)
  587. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  588. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  589. return true;
  590. }
  591. if (_error->PendingError() == true || CheckAuth(Fetcher, false) == false)
  592. return false;
  593. bool Failed = false;
  594. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false)
  595. return false;
  596. // copy files in local sources to the current directory
  597. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); ++I)
  598. if ((*I)->Local == true && (*I)->Status == pkgAcquire::Item::StatDone)
  599. {
  600. std::string const filename = cwd + flNotDir((*I)->DestFile);
  601. std::ifstream src((*I)->DestFile.c_str(), std::ios::binary);
  602. std::ofstream dst(filename.c_str(), std::ios::binary);
  603. dst << src.rdbuf();
  604. }
  605. return Failed == false;
  606. }
  607. /*}}}*/
  608. // DoCheck - Perform the check operation /*{{{*/
  609. // ---------------------------------------------------------------------
  610. /* Opening automatically checks the system, this command is mostly used
  611. for debugging */
  612. bool DoCheck(CommandLine &CmdL)
  613. {
  614. CacheFile Cache;
  615. Cache.Open();
  616. Cache.CheckDeps();
  617. return true;
  618. }
  619. /*}}}*/
  620. // DoSource - Fetch a source archive /*{{{*/
  621. // ---------------------------------------------------------------------
  622. /* Fetch souce packages */
  623. struct DscFile
  624. {
  625. string Package;
  626. string Version;
  627. string Dsc;
  628. };
  629. bool DoSource(CommandLine &CmdL)
  630. {
  631. CacheFile Cache;
  632. if (Cache.Open(false) == false)
  633. return false;
  634. if (CmdL.FileSize() <= 1)
  635. return _error->Error(_("Must specify at least one package to fetch source for"));
  636. // Read the source list
  637. if (Cache.BuildSourceList() == false)
  638. return false;
  639. pkgSourceList *List = Cache.GetSourceList();
  640. // Create the text record parsers
  641. pkgRecords Recs(Cache);
  642. pkgSrcRecords SrcRecs(*List);
  643. if (_error->PendingError() == true)
  644. return false;
  645. // Create the download object
  646. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  647. pkgAcquire Fetcher;
  648. Fetcher.SetLog(&Stat);
  649. DscFile *Dsc = new DscFile[CmdL.FileSize()];
  650. // insert all downloaded uris into this set to avoid downloading them
  651. // twice
  652. set<string> queued;
  653. // Diff only mode only fetches .diff files
  654. bool const diffOnly = _config->FindB("APT::Get::Diff-Only", false);
  655. // Tar only mode only fetches .tar files
  656. bool const tarOnly = _config->FindB("APT::Get::Tar-Only", false);
  657. // Dsc only mode only fetches .dsc files
  658. bool const dscOnly = _config->FindB("APT::Get::Dsc-Only", false);
  659. // Load the requestd sources into the fetcher
  660. unsigned J = 0;
  661. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  662. {
  663. string Src;
  664. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,Cache);
  665. if (Last == 0) {
  666. delete[] Dsc;
  667. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  668. }
  669. string srec = Last->AsStr();
  670. string::size_type pos = srec.find("\nVcs-");
  671. while (pos != string::npos)
  672. {
  673. pos += strlen("\nVcs-");
  674. string vcs = srec.substr(pos,srec.find(":",pos)-pos);
  675. if(vcs == "Browser")
  676. {
  677. pos = srec.find("\nVcs-", pos);
  678. continue;
  679. }
  680. pos += vcs.length()+2;
  681. string::size_type epos = srec.find("\n", pos);
  682. string uri = srec.substr(pos,epos-pos).c_str();
  683. ioprintf(c1out, _("NOTICE: '%s' packaging is maintained in "
  684. "the '%s' version control system at:\n"
  685. "%s\n"),
  686. Src.c_str(), vcs.c_str(), uri.c_str());
  687. if(vcs == "Bzr")
  688. ioprintf(c1out,_("Please use:\n"
  689. "bzr branch %s\n"
  690. "to retrieve the latest (possibly unreleased) "
  691. "updates to the package.\n"),
  692. uri.c_str());
  693. break;
  694. }
  695. // Back track
  696. vector<pkgSrcRecords::File> Lst;
  697. if (Last->Files(Lst) == false) {
  698. delete[] Dsc;
  699. return false;
  700. }
  701. // Load them into the fetcher
  702. for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
  703. I != Lst.end(); ++I)
  704. {
  705. // Try to guess what sort of file it is we are getting.
  706. if (I->Type == "dsc")
  707. {
  708. Dsc[J].Package = Last->Package();
  709. Dsc[J].Version = Last->Version();
  710. Dsc[J].Dsc = flNotDir(I->Path);
  711. }
  712. // Handle the only options so that multiple can be used at once
  713. if (diffOnly == true || tarOnly == true || dscOnly == true)
  714. {
  715. if ((diffOnly == true && I->Type == "diff") ||
  716. (tarOnly == true && I->Type == "tar") ||
  717. (dscOnly == true && I->Type == "dsc"))
  718. ; // Fine, we want this file downloaded
  719. else
  720. continue;
  721. }
  722. // don't download the same uri twice (should this be moved to
  723. // the fetcher interface itself?)
  724. if(queued.find(Last->Index().ArchiveURI(I->Path)) != queued.end())
  725. continue;
  726. queued.insert(Last->Index().ArchiveURI(I->Path));
  727. // check if we have a file with that md5 sum already localy
  728. if(!I->MD5Hash.empty() && FileExists(flNotDir(I->Path)))
  729. {
  730. FileFd Fd(flNotDir(I->Path), FileFd::ReadOnly);
  731. MD5Summation sum;
  732. sum.AddFD(Fd.Fd(), Fd.Size());
  733. Fd.Close();
  734. if((string)sum.Result() == I->MD5Hash)
  735. {
  736. ioprintf(c1out,_("Skipping already downloaded file '%s'\n"),
  737. flNotDir(I->Path).c_str());
  738. continue;
  739. }
  740. }
  741. new pkgAcqFile(&Fetcher,Last->Index().ArchiveURI(I->Path),
  742. I->MD5Hash,I->Size,
  743. Last->Index().SourceInfo(*Last,*I),Src);
  744. }
  745. }
  746. // Display statistics
  747. unsigned long long FetchBytes = Fetcher.FetchNeeded();
  748. unsigned long long FetchPBytes = Fetcher.PartialPresent();
  749. unsigned long long DebBytes = Fetcher.TotalNeeded();
  750. // Check for enough free space
  751. struct statvfs Buf;
  752. string OutputDir = ".";
  753. if (statvfs(OutputDir.c_str(),&Buf) != 0) {
  754. delete[] Dsc;
  755. if (errno == EOVERFLOW)
  756. return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
  757. OutputDir.c_str());
  758. else
  759. return _error->Errno("statvfs",_("Couldn't determine free space in %s"),
  760. OutputDir.c_str());
  761. } else if (unsigned(Buf.f_bfree) < (FetchBytes - FetchPBytes)/Buf.f_bsize)
  762. {
  763. struct statfs Stat;
  764. if (statfs(OutputDir.c_str(),&Stat) != 0
  765. #if HAVE_STRUCT_STATFS_F_TYPE
  766. || unsigned(Stat.f_type) != RAMFS_MAGIC
  767. #endif
  768. ) {
  769. delete[] Dsc;
  770. return _error->Error(_("You don't have enough free space in %s"),
  771. OutputDir.c_str());
  772. }
  773. }
  774. // Number of bytes
  775. if (DebBytes != FetchBytes)
  776. //TRANSLATOR: The required space between number and unit is already included
  777. // in the replacement strings, so %sB will be correctly translate in e.g. 1,5 MB
  778. ioprintf(c1out,_("Need to get %sB/%sB of source archives.\n"),
  779. SizeToStr(FetchBytes).c_str(),SizeToStr(DebBytes).c_str());
  780. else
  781. //TRANSLATOR: The required space between number and unit is already included
  782. // in the replacement string, so %sB will be correctly translate in e.g. 1,5 MB
  783. ioprintf(c1out,_("Need to get %sB of source archives.\n"),
  784. SizeToStr(DebBytes).c_str());
  785. if (_config->FindB("APT::Get::Simulate",false) == true)
  786. {
  787. for (unsigned I = 0; I != J; I++)
  788. ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
  789. delete[] Dsc;
  790. return true;
  791. }
  792. // Just print out the uris an exit if the --print-uris flag was used
  793. if (_config->FindB("APT::Get::Print-URIs") == true)
  794. {
  795. pkgAcquire::UriIterator I = Fetcher.UriBegin();
  796. for (; I != Fetcher.UriEnd(); ++I)
  797. cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' <<
  798. I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
  799. delete[] Dsc;
  800. return true;
  801. }
  802. // Run it
  803. bool Failed = false;
  804. if (AcquireRun(Fetcher, 0, &Failed, NULL) == false || Failed == true)
  805. {
  806. delete[] Dsc;
  807. return _error->Error(_("Failed to fetch some archives."));
  808. }
  809. if (_config->FindB("APT::Get::Download-only",false) == true)
  810. {
  811. c1out << _("Download complete and in download only mode") << endl;
  812. delete[] Dsc;
  813. return true;
  814. }
  815. // Unpack the sources
  816. pid_t Process = ExecFork();
  817. if (Process == 0)
  818. {
  819. bool const fixBroken = _config->FindB("APT::Get::Fix-Broken", false);
  820. for (unsigned I = 0; I != J; ++I)
  821. {
  822. string Dir = Dsc[I].Package + '-' + Cache->VS().UpstreamVersion(Dsc[I].Version.c_str());
  823. // Diff only mode only fetches .diff files
  824. if (_config->FindB("APT::Get::Diff-Only",false) == true ||
  825. _config->FindB("APT::Get::Tar-Only",false) == true ||
  826. Dsc[I].Dsc.empty() == true)
  827. continue;
  828. // See if the package is already unpacked
  829. struct stat Stat;
  830. if (fixBroken == false && stat(Dir.c_str(),&Stat) == 0 &&
  831. S_ISDIR(Stat.st_mode) != 0)
  832. {
  833. ioprintf(c0out ,_("Skipping unpack of already unpacked source in %s\n"),
  834. Dir.c_str());
  835. }
  836. else
  837. {
  838. // Call dpkg-source
  839. char S[500];
  840. snprintf(S,sizeof(S),"%s -x %s",
  841. _config->Find("Dir::Bin::dpkg-source","dpkg-source").c_str(),
  842. Dsc[I].Dsc.c_str());
  843. if (system(S) != 0)
  844. {
  845. fprintf(stderr,_("Unpack command '%s' failed.\n"),S);
  846. fprintf(stderr,_("Check if the 'dpkg-dev' package is installed.\n"));
  847. _exit(1);
  848. }
  849. }
  850. // Try to compile it with dpkg-buildpackage
  851. if (_config->FindB("APT::Get::Compile",false) == true)
  852. {
  853. string buildopts = _config->Find("APT::Get::Host-Architecture");
  854. if (buildopts.empty() == false)
  855. buildopts = "-a" + buildopts + " ";
  856. buildopts.append(_config->Find("DPkg::Build-Options","-b -uc"));
  857. // Call dpkg-buildpackage
  858. char S[500];
  859. snprintf(S,sizeof(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) != 0)
  864. {
  865. fprintf(stderr,_("Build command '%s' failed.\n"),S);
  866. _exit(1);
  867. }
  868. }
  869. }
  870. _exit(0);
  871. }
  872. delete[] Dsc;
  873. // Wait for the subprocess
  874. int Status = 0;
  875. while (waitpid(Process,&Status,0) != Process)
  876. {
  877. if (errno == EINTR)
  878. continue;
  879. return _error->Errno("waitpid","Couldn't wait for subprocess");
  880. }
  881. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  882. return _error->Error(_("Child process failed"));
  883. return true;
  884. }
  885. /*}}}*/
  886. // DoBuildDep - Install/removes packages to satisfy build dependencies /*{{{*/
  887. // ---------------------------------------------------------------------
  888. /* This function will look at the build depends list of the given source
  889. package and install the necessary packages to make it true, or fail. */
  890. bool DoBuildDep(CommandLine &CmdL)
  891. {
  892. CacheFile Cache;
  893. _config->Set("APT::Install-Recommends", false);
  894. if (Cache.Open(true) == false)
  895. return false;
  896. if (CmdL.FileSize() <= 1)
  897. return _error->Error(_("Must specify at least one package to check builddeps for"));
  898. // Read the source list
  899. if (Cache.BuildSourceList() == false)
  900. return false;
  901. pkgSourceList *List = Cache.GetSourceList();
  902. // Create the text record parsers
  903. pkgRecords Recs(Cache);
  904. pkgSrcRecords SrcRecs(*List);
  905. if (_error->PendingError() == true)
  906. return false;
  907. // Create the download object
  908. AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
  909. pkgAcquire Fetcher;
  910. if (Fetcher.Setup(&Stat) == false)
  911. return false;
  912. bool StripMultiArch;
  913. string hostArch = _config->Find("APT::Get::Host-Architecture");
  914. if (hostArch.empty() == false)
  915. {
  916. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  917. if (std::find(archs.begin(), archs.end(), hostArch) == archs.end())
  918. return _error->Error(_("No architecture information available for %s. See apt.conf(5) APT::Architectures for setup"), hostArch.c_str());
  919. StripMultiArch = false;
  920. }
  921. else
  922. StripMultiArch = true;
  923. unsigned J = 0;
  924. for (const char **I = CmdL.FileList + 1; *I != 0; I++, J++)
  925. {
  926. string Src;
  927. pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,Cache);
  928. if (Last == 0)
  929. return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
  930. // Process the build-dependencies
  931. vector<pkgSrcRecords::Parser::BuildDepRec> BuildDeps;
  932. // FIXME: Can't specify architecture to use for [wildcard] matching, so switch default arch temporary
  933. if (hostArch.empty() == false)
  934. {
  935. std::string nativeArch = _config->Find("APT::Architecture");
  936. _config->Set("APT::Architecture", hostArch);
  937. bool Success = Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch);
  938. _config->Set("APT::Architecture", nativeArch);
  939. if (Success == false)
  940. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  941. }
  942. else if (Last->BuildDepends(BuildDeps, _config->FindB("APT::Get::Arch-Only", false), StripMultiArch) == false)
  943. return _error->Error(_("Unable to get build-dependency information for %s"),Src.c_str());
  944. // Also ensure that build-essential packages are present
  945. Configuration::Item const *Opts = _config->Tree("APT::Build-Essential");
  946. if (Opts)
  947. Opts = Opts->Child;
  948. for (; Opts; Opts = Opts->Next)
  949. {
  950. if (Opts->Value.empty() == true)
  951. continue;
  952. pkgSrcRecords::Parser::BuildDepRec rec;
  953. rec.Package = Opts->Value;
  954. rec.Type = pkgSrcRecords::Parser::BuildDependIndep;
  955. rec.Op = 0;
  956. BuildDeps.push_back(rec);
  957. }
  958. if (BuildDeps.empty() == true)
  959. {
  960. ioprintf(c1out,_("%s has no build depends.\n"),Src.c_str());
  961. continue;
  962. }
  963. // Install the requested packages
  964. vector <pkgSrcRecords::Parser::BuildDepRec>::iterator D;
  965. pkgProblemResolver Fix(Cache);
  966. bool skipAlternatives = false; // skip remaining alternatives in an or group
  967. for (D = BuildDeps.begin(); D != BuildDeps.end(); ++D)
  968. {
  969. bool hasAlternatives = (((*D).Op & pkgCache::Dep::Or) == pkgCache::Dep::Or);
  970. if (skipAlternatives == true)
  971. {
  972. /*
  973. * if there are alternatives, we've already picked one, so skip
  974. * the rest
  975. *
  976. * TODO: this means that if there's a build-dep on A|B and B is
  977. * installed, we'll still try to install A; more importantly,
  978. * if A is currently broken, we cannot go back and try B. To fix
  979. * this would require we do a Resolve cycle for each package we
  980. * add to the install list. Ugh
  981. */
  982. if (!hasAlternatives)
  983. skipAlternatives = false; // end of or group
  984. continue;
  985. }
  986. if ((*D).Type == pkgSrcRecords::Parser::BuildConflict ||
  987. (*D).Type == pkgSrcRecords::Parser::BuildConflictIndep)
  988. {
  989. pkgCache::GrpIterator Grp = Cache->FindGrp((*D).Package);
  990. // Build-conflicts on unknown packages are silently ignored
  991. if (Grp.end() == true)
  992. continue;
  993. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  994. {
  995. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  996. /*
  997. * Remove if we have an installed version that satisfies the
  998. * version criteria
  999. */
  1000. if (IV.end() == false &&
  1001. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1002. TryToInstallBuildDep(Pkg,Cache,Fix,true,false);
  1003. }
  1004. }
  1005. else // BuildDep || BuildDepIndep
  1006. {
  1007. if (_config->FindB("Debug::BuildDeps",false) == true)
  1008. cout << "Looking for " << (*D).Package << "...\n";
  1009. pkgCache::PkgIterator Pkg;
  1010. // Cross-Building?
  1011. if (StripMultiArch == false && D->Type != pkgSrcRecords::Parser::BuildDependIndep)
  1012. {
  1013. size_t const colon = D->Package.find(":");
  1014. if (colon != string::npos)
  1015. {
  1016. if (strcmp(D->Package.c_str() + colon, ":any") == 0 || strcmp(D->Package.c_str() + colon, ":native") == 0)
  1017. Pkg = Cache->FindPkg(D->Package.substr(0,colon));
  1018. else
  1019. Pkg = Cache->FindPkg(D->Package);
  1020. }
  1021. else
  1022. Pkg = Cache->FindPkg(D->Package, hostArch);
  1023. // a bad version either is invalid or doesn't satify dependency
  1024. #define BADVER(Ver) (Ver.end() == true || \
  1025. (D->Version.empty() == false && \
  1026. Cache->VS().CheckDep(Ver.VerStr(),D->Op,D->Version.c_str()) == false))
  1027. APT::VersionList verlist;
  1028. if (Pkg.end() == false)
  1029. {
  1030. pkgCache::VerIterator Ver = (*Cache)[Pkg].InstVerIter(*Cache);
  1031. if (BADVER(Ver) == false)
  1032. verlist.insert(Ver);
  1033. Ver = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1034. if (BADVER(Ver) == false)
  1035. verlist.insert(Ver);
  1036. }
  1037. if (verlist.empty() == true)
  1038. {
  1039. pkgCache::PkgIterator BuildPkg = Cache->FindPkg(D->Package, "native");
  1040. if (BuildPkg.end() == false && Pkg != BuildPkg)
  1041. {
  1042. pkgCache::VerIterator Ver = (*Cache)[BuildPkg].InstVerIter(*Cache);
  1043. if (BADVER(Ver) == false)
  1044. verlist.insert(Ver);
  1045. Ver = (*Cache)[BuildPkg].CandidateVerIter(*Cache);
  1046. if (BADVER(Ver) == false)
  1047. verlist.insert(Ver);
  1048. }
  1049. }
  1050. #undef BADVER
  1051. string forbidden;
  1052. // We need to decide if host or build arch, so find a version we can look at
  1053. APT::VersionList::const_iterator Ver = verlist.begin();
  1054. for (; Ver != verlist.end(); ++Ver)
  1055. {
  1056. forbidden.clear();
  1057. if (Ver->MultiArch == pkgCache::Version::None || Ver->MultiArch == pkgCache::Version::All)
  1058. {
  1059. if (colon == string::npos)
  1060. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1061. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1062. forbidden = "Multi-Arch: none";
  1063. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1064. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1065. }
  1066. else if (Ver->MultiArch == pkgCache::Version::Same)
  1067. {
  1068. if (colon == string::npos)
  1069. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1070. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1071. forbidden = "Multi-Arch: same";
  1072. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1073. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1074. }
  1075. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  1076. {
  1077. if (colon == string::npos)
  1078. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1079. else if (strcmp(D->Package.c_str() + colon, ":any") == 0 ||
  1080. strcmp(D->Package.c_str() + colon, ":native") == 0)
  1081. forbidden = "Multi-Arch: foreign";
  1082. }
  1083. else if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  1084. {
  1085. if (colon == string::npos)
  1086. Pkg = Ver.ParentPkg().Group().FindPkg(hostArch);
  1087. else if (strcmp(D->Package.c_str() + colon, ":any") == 0)
  1088. {
  1089. // prefer any installed over preferred non-installed architectures
  1090. pkgCache::GrpIterator Grp = Ver.ParentPkg().Group();
  1091. // we don't check for version here as we are better of with upgrading than remove and install
  1092. for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
  1093. if (Pkg.CurrentVer().end() == false)
  1094. break;
  1095. if (Pkg.end() == true)
  1096. Pkg = Grp.FindPreferredPkg(true);
  1097. }
  1098. else if (strcmp(D->Package.c_str() + colon, ":native") == 0)
  1099. Pkg = Ver.ParentPkg().Group().FindPkg("native");
  1100. }
  1101. if (forbidden.empty() == false)
  1102. {
  1103. if (_config->FindB("Debug::BuildDeps",false) == true)
  1104. cout << D->Package.substr(colon, string::npos) << " is not allowed from " << forbidden << " package " << (*D).Package << " (" << Ver.VerStr() << ")" << endl;
  1105. continue;
  1106. }
  1107. //we found a good version
  1108. break;
  1109. }
  1110. if (Ver == verlist.end())
  1111. {
  1112. if (_config->FindB("Debug::BuildDeps",false) == true)
  1113. cout << " No multiarch info as we have no satisfying installed nor candidate for " << D->Package << " on build or host arch" << endl;
  1114. if (forbidden.empty() == false)
  1115. {
  1116. if (hasAlternatives)
  1117. continue;
  1118. return _error->Error(_("%s dependency for %s can't be satisfied "
  1119. "because %s is not allowed on '%s' packages"),
  1120. Last->BuildDepType(D->Type), Src.c_str(),
  1121. D->Package.c_str(), forbidden.c_str());
  1122. }
  1123. }
  1124. }
  1125. else
  1126. Pkg = Cache->FindPkg(D->Package);
  1127. if (Pkg.end() == true || (Pkg->VersionList == 0 && Pkg->ProvidesList == 0))
  1128. {
  1129. if (_config->FindB("Debug::BuildDeps",false) == true)
  1130. cout << " (not found)" << (*D).Package << endl;
  1131. if (hasAlternatives)
  1132. continue;
  1133. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1134. "because the package %s cannot be found"),
  1135. Last->BuildDepType((*D).Type),Src.c_str(),
  1136. (*D).Package.c_str());
  1137. }
  1138. pkgCache::VerIterator IV = (*Cache)[Pkg].InstVerIter(*Cache);
  1139. if (IV.end() == false)
  1140. {
  1141. if (_config->FindB("Debug::BuildDeps",false) == true)
  1142. cout << " Is installed\n";
  1143. if (D->Version.empty() == true ||
  1144. Cache->VS().CheckDep(IV.VerStr(),(*D).Op,(*D).Version.c_str()) == true)
  1145. {
  1146. skipAlternatives = hasAlternatives;
  1147. continue;
  1148. }
  1149. if (_config->FindB("Debug::BuildDeps",false) == true)
  1150. cout << " ...but the installed version doesn't meet the version requirement\n";
  1151. if (((*D).Op & pkgCache::Dep::LessEq) == pkgCache::Dep::LessEq)
  1152. return _error->Error(_("Failed to satisfy %s dependency for %s: Installed package %s is too new"),
  1153. Last->BuildDepType((*D).Type), Src.c_str(), Pkg.FullName(true).c_str());
  1154. }
  1155. // Only consider virtual packages if there is no versioned dependency
  1156. if ((*D).Version.empty() == true)
  1157. {
  1158. /*
  1159. * If this is a virtual package, we need to check the list of
  1160. * packages that provide it and see if any of those are
  1161. * installed
  1162. */
  1163. pkgCache::PrvIterator Prv = Pkg.ProvidesList();
  1164. for (; Prv.end() != true; ++Prv)
  1165. {
  1166. if (_config->FindB("Debug::BuildDeps",false) == true)
  1167. cout << " Checking provider " << Prv.OwnerPkg().FullName() << endl;
  1168. if ((*Cache)[Prv.OwnerPkg()].InstVerIter(*Cache).end() == false)
  1169. break;
  1170. }
  1171. if (Prv.end() == false)
  1172. {
  1173. if (_config->FindB("Debug::BuildDeps",false) == true)
  1174. cout << " Is provided by installed package " << Prv.OwnerPkg().FullName() << endl;
  1175. skipAlternatives = hasAlternatives;
  1176. continue;
  1177. }
  1178. }
  1179. else // versioned dependency
  1180. {
  1181. pkgCache::VerIterator CV = (*Cache)[Pkg].CandidateVerIter(*Cache);
  1182. if (CV.end() == true ||
  1183. Cache->VS().CheckDep(CV.VerStr(),(*D).Op,(*D).Version.c_str()) == false)
  1184. {
  1185. if (hasAlternatives)
  1186. continue;
  1187. else if (CV.end() == false)
  1188. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1189. "because candidate version of package %s "
  1190. "can't satisfy version requirements"),
  1191. Last->BuildDepType(D->Type), Src.c_str(),
  1192. D->Package.c_str());
  1193. else
  1194. return _error->Error(_("%s dependency for %s cannot be satisfied "
  1195. "because package %s has no candidate version"),
  1196. Last->BuildDepType(D->Type), Src.c_str(),
  1197. D->Package.c_str());
  1198. }
  1199. }
  1200. if (TryToInstallBuildDep(Pkg,Cache,Fix,false,false,false) == true)
  1201. {
  1202. // We successfully installed something; skip remaining alternatives
  1203. skipAlternatives = hasAlternatives;
  1204. if(_config->FindB("APT::Get::Build-Dep-Automatic", false) == true)
  1205. Cache->MarkAuto(Pkg, true);
  1206. continue;
  1207. }
  1208. else if (hasAlternatives)
  1209. {
  1210. if (_config->FindB("Debug::BuildDeps",false) == true)
  1211. cout << " Unsatisfiable, trying alternatives\n";
  1212. continue;
  1213. }
  1214. else
  1215. {
  1216. return _error->Error(_("Failed to satisfy %s dependency for %s: %s"),
  1217. Last->BuildDepType((*D).Type),
  1218. Src.c_str(),
  1219. (*D).Package.c_str());
  1220. }
  1221. }
  1222. }
  1223. if (Fix.Resolve(true) == false)
  1224. _error->Discard();
  1225. // Now we check the state of the packages,
  1226. if (Cache->BrokenCount() != 0)
  1227. {
  1228. ShowBroken(cout, Cache, false);
  1229. return _error->Error(_("Build-dependencies for %s could not be satisfied."),*I);
  1230. }
  1231. }
  1232. if (InstallPackages(Cache, false, true) == false)
  1233. return _error->Error(_("Failed to process build dependencies"));
  1234. return true;
  1235. }
  1236. /*}}}*/
  1237. // GetChangelogPath - return a path pointing to a changelog file or dir /*{{{*/
  1238. // ---------------------------------------------------------------------
  1239. /* This returns a "path" string for the changelog url construction.
  1240. * Please note that its not complete, it either needs a "/changelog"
  1241. * appended (for the packages.debian.org/changelogs site) or a
  1242. * ".changelog" (for third party sites that store the changelog in the
  1243. * pool/ next to the deb itself)
  1244. * Example return: "pool/main/a/apt/apt_0.8.8ubuntu3"
  1245. */
  1246. string GetChangelogPath(CacheFile &Cache,
  1247. pkgCache::PkgIterator Pkg,
  1248. pkgCache::VerIterator Ver)
  1249. {
  1250. string path;
  1251. pkgRecords Recs(Cache);
  1252. pkgRecords::Parser &rec=Recs.Lookup(Ver.FileList());
  1253. string srcpkg = rec.SourcePkg().empty() ? Pkg.Name() : rec.SourcePkg();
  1254. string ver = Ver.VerStr();
  1255. // if there is a source version it always wins
  1256. if (rec.SourceVer() != "")
  1257. ver = rec.SourceVer();
  1258. path = flNotFile(rec.FileName());
  1259. path += srcpkg + "_" + StripEpoch(ver);
  1260. return path;
  1261. }
  1262. /*}}}*/
  1263. // GuessThirdPartyChangelogUri - return url /*{{{*/
  1264. // ---------------------------------------------------------------------
  1265. /* Contruct a changelog file path for third party sites that do not use
  1266. * packages.debian.org/changelogs
  1267. * This simply uses the ArchiveURI() of the source pkg and looks for
  1268. * a .changelog file there, Example for "mediabuntu":
  1269. * apt-get changelog mplayer-doc:
  1270. * http://packages.medibuntu.org/pool/non-free/m/mplayer/mplayer_1.0~rc4~try1.dsfg1-1ubuntu1+medibuntu1.changelog
  1271. */
  1272. bool GuessThirdPartyChangelogUri(CacheFile &Cache,
  1273. pkgCache::PkgIterator Pkg,
  1274. pkgCache::VerIterator Ver,
  1275. string &out_uri)
  1276. {
  1277. // get the binary deb server path
  1278. pkgCache::VerFileIterator Vf = Ver.FileList();
  1279. if (Vf.end() == true)
  1280. return false;
  1281. pkgCache::PkgFileIterator F = Vf.File();
  1282. pkgIndexFile *index;
  1283. pkgSourceList *SrcList = Cache.GetSourceList();
  1284. if(SrcList->FindIndex(F, index) == false)
  1285. return false;
  1286. // get archive uri for the binary deb
  1287. string path_without_dot_changelog = GetChangelogPath(Cache, Pkg, Ver);
  1288. out_uri = index->ArchiveURI(path_without_dot_changelog + ".changelog");
  1289. // now strip away the filename and add srcpkg_srcver.changelog
  1290. return true;
  1291. }
  1292. /*}}}*/
  1293. // DownloadChangelog - Download the changelog /*{{{*/
  1294. // ---------------------------------------------------------------------
  1295. bool DownloadChangelog(CacheFile &CacheFile, pkgAcquire &Fetcher,
  1296. pkgCache::VerIterator Ver, string targetfile)
  1297. /* Download a changelog file for the given package version to
  1298. * targetfile. This will first try the server from Apt::Changelogs::Server
  1299. * (http://packages.debian.org/changelogs by default) and if that gives
  1300. * a 404 tries to get it from the archive directly (see
  1301. * GuessThirdPartyChangelogUri for details how)
  1302. */
  1303. {
  1304. string path;
  1305. string descr;
  1306. string server;
  1307. string changelog_uri;
  1308. // data structures we need
  1309. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  1310. // make the server root configurable
  1311. server = _config->Find("Apt::Changelogs::Server",
  1312. "http://packages.debian.org/changelogs");
  1313. path = GetChangelogPath(CacheFile, Pkg, Ver);
  1314. strprintf(changelog_uri, "%s/%s/changelog", server.c_str(), path.c_str());
  1315. if (_config->FindB("APT::Get::Print-URIs", false) == true)
  1316. {
  1317. std::cout << '\'' << changelog_uri << '\'' << std::endl;
  1318. return true;
  1319. }
  1320. strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), changelog_uri.c_str());
  1321. // queue it
  1322. new pkgAcqFile(&Fetcher, changelog_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
  1323. // try downloading it, if that fails, try third-party-changelogs location
  1324. // FIXME: Fetcher.Run() is "Continue" even if I get a 404?!?
  1325. Fetcher.Run();
  1326. if (!FileExists(targetfile))
  1327. {
  1328. string third_party_uri;
  1329. if (GuessThirdPartyChangelogUri(CacheFile, Pkg, Ver, third_party_uri))
  1330. {
  1331. strprintf(descr, _("Changelog for %s (%s)"), Pkg.Name(), third_party_uri.c_str());
  1332. new pkgAcqFile(&Fetcher, third_party_uri, "", 0, descr, Pkg.Name(), "ignored", targetfile);
  1333. Fetcher.Run();
  1334. }
  1335. }
  1336. if (FileExists(targetfile))
  1337. return true;
  1338. // error
  1339. return _error->Error("changelog download failed");
  1340. }
  1341. /*}}}*/
  1342. // DoChangelog - Get changelog from the command line /*{{{*/
  1343. // ---------------------------------------------------------------------
  1344. bool DoChangelog(CommandLine &CmdL)
  1345. {
  1346. CacheFile Cache;
  1347. if (Cache.ReadOnlyOpen() == false)
  1348. return false;
  1349. APT::CacheSetHelper helper(c0out);
  1350. APT::VersionList verset = APT::VersionList::FromCommandLine(Cache,
  1351. CmdL.FileList + 1, APT::VersionList::CANDIDATE, helper);
  1352. if (verset.empty() == true)
  1353. return false;
  1354. pkgAcquire Fetcher;
  1355. if (_config->FindB("APT::Get::Print-URIs", false) == true)
  1356. {
  1357. bool Success = true;
  1358. for (APT::VersionList::const_iterator Ver = verset.begin();
  1359. Ver != verset.end(); ++Ver)
  1360. Success &= DownloadChangelog(Cache, Fetcher, Ver, "");
  1361. return Success;
  1362. }
  1363. AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
  1364. Fetcher.Setup(&Stat);
  1365. bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
  1366. char tmpname[100];
  1367. char* tmpdir = NULL;
  1368. if (downOnly == false)
  1369. {
  1370. const char* const tmpDir = getenv("TMPDIR");
  1371. if (tmpDir != NULL && *tmpDir != '\0')
  1372. snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", tmpDir);
  1373. else
  1374. strncpy(tmpname, "/tmp/apt-changelog-XXXXXX", sizeof(tmpname));
  1375. tmpdir = mkdtemp(tmpname);
  1376. if (tmpdir == NULL)
  1377. return _error->Errno("mkdtemp", "mkdtemp failed");
  1378. }
  1379. for (APT::VersionList::const_iterator Ver = verset.begin();
  1380. Ver != verset.end();
  1381. ++Ver)
  1382. {
  1383. string changelogfile;
  1384. if (downOnly == false)
  1385. changelogfile.append(tmpname).append("changelog");
  1386. else
  1387. changelogfile.append(Ver.ParentPkg().Name()).append(".changelog");
  1388. if (DownloadChangelog(Cache, Fetcher, Ver, changelogfile) && downOnly == false)
  1389. {
  1390. DisplayFileInPager(changelogfile);
  1391. // cleanup temp file
  1392. unlink(changelogfile.c_str());
  1393. }
  1394. }
  1395. // clenaup tmp dir
  1396. if (tmpdir != NULL)
  1397. rmdir(tmpdir);
  1398. return true;
  1399. }
  1400. /*}}}*/
  1401. // ShowHelp - Show a help screen /*{{{*/
  1402. // ---------------------------------------------------------------------
  1403. /* */
  1404. bool ShowHelp(CommandLine &CmdL)
  1405. {
  1406. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  1407. COMMON_ARCH,__DATE__,__TIME__);
  1408. if (_config->FindB("version") == true)
  1409. {
  1410. cout << _("Supported modules:") << endl;
  1411. for (unsigned I = 0; I != pkgVersioningSystem::GlobalListLen; I++)
  1412. {
  1413. pkgVersioningSystem *VS = pkgVersioningSystem::GlobalList[I];
  1414. if (_system != 0 && _system->VS == VS)
  1415. cout << '*';
  1416. else
  1417. cout << ' ';
  1418. cout << "Ver: " << VS->Label << endl;
  1419. /* Print out all the packaging systems that will work with
  1420. this VS */
  1421. for (unsigned J = 0; J != pkgSystem::GlobalListLen; J++)
  1422. {
  1423. pkgSystem *Sys = pkgSystem::GlobalList[J];
  1424. if (_system == Sys)
  1425. cout << '*';
  1426. else
  1427. cout << ' ';
  1428. if (Sys->VS->TestCompatibility(*VS) == true)
  1429. cout << "Pkg: " << Sys->Label << " (Priority " << Sys->Score(*_config) << ")" << endl;
  1430. }
  1431. }
  1432. for (unsigned I = 0; I != pkgSourceList::Type::GlobalListLen; I++)
  1433. {
  1434. pkgSourceList::Type *Type = pkgSourceList::Type::GlobalList[I];
  1435. cout << " S.L: '" << Type->Name << "' " << Type->Label << endl;
  1436. }
  1437. for (unsigned I = 0; I != pkgIndexFile::Type::GlobalListLen; I++)
  1438. {
  1439. pkgIndexFile::Type *Type = pkgIndexFile::Type::GlobalList[I];
  1440. cout << " Idx: " << Type->Label << endl;
  1441. }
  1442. return true;
  1443. }
  1444. cout <<
  1445. _("Usage: apt-get [options] command\n"
  1446. " apt-get [options] install|remove pkg1 [pkg2 ...]\n"
  1447. " apt-get [options] source pkg1 [pkg2 ...]\n"
  1448. "\n"
  1449. "apt-get is a simple command line interface for downloading and\n"
  1450. "installing packages. The most frequently used commands are update\n"
  1451. "and install.\n"
  1452. "\n"
  1453. "Commands:\n"
  1454. " update - Retrieve new lists of packages\n"
  1455. " upgrade - Perform an upgrade\n"
  1456. " install - Install new packages (pkg is libc6 not libc6.deb)\n"
  1457. " remove - Remove packages\n"
  1458. " autoremove - Remove automatically all unused packages\n"
  1459. " purge - Remove packages and config files\n"
  1460. " source - Download source archives\n"
  1461. " build-dep - Configure build-dependencies for source packages\n"
  1462. " dist-upgrade - Distribution upgrade, see apt-get(8)\n"
  1463. " dselect-upgrade - Follow dselect selections\n"
  1464. " clean - Erase downloaded archive files\n"
  1465. " autoclean - Erase old downloaded archive files\n"
  1466. " check - Verify that there are no broken dependencies\n"
  1467. " changelog - Download and display the changelog for the given package\n"
  1468. " download - Download the binary package into the current directory\n"
  1469. "\n"
  1470. "Options:\n"
  1471. " -h This help text.\n"
  1472. " -q Loggable output - no progress indicator\n"
  1473. " -qq No output except for errors\n"
  1474. " -d Download only - do NOT install or unpack archives\n"
  1475. " -s No-act. Perform ordering simulation\n"
  1476. " -y Assume Yes to all queries and do not prompt\n"
  1477. " -f Attempt to correct a system with broken dependencies in place\n"
  1478. " -m Attempt to continue if archives are unlocatable\n"
  1479. " -u Show a list of upgraded packages as well\n"
  1480. " -b Build the source package after fetching it\n"
  1481. " -V Show verbose version numbers\n"
  1482. " -c=? Read this configuration file\n"
  1483. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  1484. "See the apt-get(8), sources.list(5) and apt.conf(5) manual\n"
  1485. "pages for more information and options.\n"
  1486. " This APT has Super Cow Powers.\n");
  1487. return true;
  1488. }
  1489. /*}}}*/
  1490. // SigWinch - Window size change signal handler /*{{{*/
  1491. // ---------------------------------------------------------------------
  1492. /* */
  1493. void SigWinch(int)
  1494. {
  1495. // Riped from GNU ls
  1496. #ifdef TIOCGWINSZ
  1497. struct winsize ws;
  1498. if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
  1499. ScreenWidth = ws.ws_col - 1;
  1500. #endif
  1501. }
  1502. /*}}}*/
  1503. bool DoUpgrade(CommandLine &CmdL) /*{{{*/
  1504. {
  1505. if (_config->FindB("APT::Get::Upgrade-Allow-New", false) == true)
  1506. return DoUpgradeWithAllowNewPackages(CmdL);
  1507. else
  1508. return DoUpgradeNoNewPackages(CmdL);
  1509. }
  1510. /*}}}*/
  1511. int main(int argc,const char *argv[]) /*{{{*/
  1512. {
  1513. CommandLine::Dispatch Cmds[] = {{"update",&DoUpdate},
  1514. {"upgrade",&DoUpgrade},
  1515. {"install",&DoInstall},
  1516. {"remove",&DoInstall},
  1517. {"purge",&DoInstall},
  1518. {"autoremove",&DoInstall},
  1519. {"markauto",&DoMarkAuto},
  1520. {"unmarkauto",&DoMarkAuto},
  1521. {"dist-upgrade",&DoDistUpgrade},
  1522. {"dselect-upgrade",&DoDSelectUpgrade},
  1523. {"build-dep",&DoBuildDep},
  1524. {"clean",&DoClean},
  1525. {"autoclean",&DoAutoClean},
  1526. {"check",&DoCheck},
  1527. {"source",&DoSource},
  1528. {"download",&DoDownload},
  1529. {"changelog",&DoChangelog},
  1530. {"moo",&DoMoo},
  1531. {"help",&ShowHelp},
  1532. {0,0}};
  1533. std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", CommandLine::GetCommand(Cmds, argc, argv));
  1534. // Set up gettext support
  1535. setlocale(LC_ALL,"");
  1536. textdomain(PACKAGE);
  1537. // Parse the command line and initialize the package library
  1538. CommandLine CmdL(Args.data(),_config);
  1539. if (pkgInitConfig(*_config) == false ||
  1540. CmdL.Parse(argc,argv) == false ||
  1541. pkgInitSystem(*_config,_system) == false)
  1542. {
  1543. if (_config->FindB("version") == true)
  1544. ShowHelp(CmdL);
  1545. _error->DumpErrors();
  1546. return 100;
  1547. }
  1548. // See if the help should be shown
  1549. if (_config->FindB("help") == true ||
  1550. _config->FindB("version") == true ||
  1551. CmdL.FileSize() == 0)
  1552. {
  1553. ShowHelp(CmdL);
  1554. return 0;
  1555. }
  1556. // see if we are in simulate mode
  1557. CheckSimulateMode(CmdL);
  1558. // Deal with stdout not being a tty
  1559. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  1560. _config->Set("quiet","1");
  1561. // Setup the output streams
  1562. InitOutput();
  1563. // Setup the signals
  1564. signal(SIGPIPE,SIG_IGN);
  1565. signal(SIGWINCH,SigWinch);
  1566. SigWinch(0);
  1567. // Match the operation
  1568. CmdL.DispatchArg(Cmds);
  1569. // Print any errors or warnings found during parsing
  1570. bool const Errors = _error->PendingError();
  1571. if (_config->FindI("quiet",0) > 0)
  1572. _error->DumpErrors();
  1573. else
  1574. _error->DumpErrors(GlobalError::DEBUG);
  1575. return Errors == true ? 100 : 0;
  1576. }
  1577. /*}}}*/