apt-get.cc 59 KB

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