apt-get.cc 58 KB

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