apt-get.cc 58 KB

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