apt-get.cc 58 KB

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