cacheset.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Simple wrapper around a std::set to provide a similar interface to
  5. a set of cache structures as to the complete set of all structures
  6. in the pkgCache. Currently only Package is supported.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <apt-pkg/aptconfiguration.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/versionmatch.h>
  14. #include <apti18n.h>
  15. #include "cacheset.h"
  16. #include <vector>
  17. #include <regex.h>
  18. /*}}}*/
  19. namespace APT {
  20. // FromTask - Return all packages in the cache from a specific task /*{{{*/
  21. PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  22. size_t const archfound = pattern.find_last_of(':');
  23. std::string arch = "native";
  24. if (archfound != std::string::npos) {
  25. arch = pattern.substr(archfound+1);
  26. pattern.erase(archfound);
  27. }
  28. if (pattern[pattern.length() -1] != '^')
  29. return APT::PackageSet(TASK);
  30. pattern.erase(pattern.length()-1);
  31. if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
  32. return APT::PackageSet(TASK);
  33. PackageSet pkgset(TASK);
  34. // get the records
  35. pkgRecords Recs(Cache);
  36. // build regexp for the task
  37. regex_t Pattern;
  38. char S[300];
  39. snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
  40. if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
  41. _error->Error("Failed to compile task regexp");
  42. return pkgset;
  43. }
  44. for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
  45. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  46. if (Pkg.end() == true)
  47. continue;
  48. pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
  49. if(ver.end() == true)
  50. continue;
  51. pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
  52. const char *start, *end;
  53. parser.GetRec(start,end);
  54. unsigned int const length = end - start;
  55. char buf[length];
  56. strncpy(buf, start, length);
  57. buf[length-1] = '\0';
  58. if (regexec(&Pattern, buf, 0, 0, 0) != 0)
  59. continue;
  60. pkgset.insert(Pkg);
  61. }
  62. regfree(&Pattern);
  63. if (pkgset.empty() == true)
  64. return helper.canNotFindTask(Cache, pattern);
  65. helper.showTaskSelection(pkgset, pattern);
  66. return pkgset;
  67. }
  68. /*}}}*/
  69. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  70. PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  71. static const char * const isregex = ".?+*|[^$";
  72. if (pattern.find_first_of(isregex) == std::string::npos)
  73. return PackageSet(REGEX);
  74. size_t archfound = pattern.find_last_of(':');
  75. std::string arch = "native";
  76. if (archfound != std::string::npos) {
  77. arch = pattern.substr(archfound+1);
  78. if (arch.find_first_of(isregex) == std::string::npos)
  79. pattern.erase(archfound);
  80. else
  81. arch = "native";
  82. }
  83. regex_t Pattern;
  84. int Res;
  85. if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
  86. char Error[300];
  87. regerror(Res, &Pattern, Error, sizeof(Error));
  88. _error->Error(_("Regex compilation error - %s"), Error);
  89. return PackageSet(REGEX);
  90. }
  91. if (unlikely(Cache.GetPkgCache() == 0))
  92. return PackageSet(REGEX);
  93. PackageSet pkgset(REGEX);
  94. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
  95. {
  96. if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
  97. continue;
  98. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  99. if (Pkg.end() == true) {
  100. if (archfound == std::string::npos) {
  101. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  102. for (std::vector<std::string>::const_iterator a = archs.begin();
  103. a != archs.end() && Pkg.end() != true; ++a)
  104. Pkg = Grp.FindPkg(*a);
  105. }
  106. if (Pkg.end() == true)
  107. continue;
  108. }
  109. pkgset.insert(Pkg);
  110. }
  111. regfree(&Pattern);
  112. if (pkgset.empty() == true)
  113. return helper.canNotFindRegEx(Cache, pattern);
  114. helper.showRegExSelection(pkgset, pattern);
  115. return pkgset;
  116. }
  117. /*}}}*/
  118. // FromName - Returns the package defined by this string /*{{{*/
  119. pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
  120. std::string const &str, CacheSetHelper &helper) {
  121. std::string pkg = str;
  122. size_t archfound = pkg.find_last_of(':');
  123. std::string arch;
  124. if (archfound != std::string::npos) {
  125. arch = pkg.substr(archfound+1);
  126. pkg.erase(archfound);
  127. }
  128. if (Cache.GetPkgCache() == 0)
  129. return pkgCache::PkgIterator(Cache, 0);
  130. pkgCache::PkgIterator Pkg(Cache, 0);
  131. if (arch.empty() == true) {
  132. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  133. if (Grp.end() == false)
  134. Pkg = Grp.FindPreferredPkg();
  135. } else
  136. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  137. if (Pkg.end() == true)
  138. return helper.canNotFindPkgName(Cache, str);
  139. return Pkg;
  140. }
  141. /*}}}*/
  142. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  143. std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
  144. pkgCacheFile &Cache, const char **cmdline,
  145. std::list<PackageSet::Modifier> const &mods,
  146. unsigned short const &fallback, CacheSetHelper &helper) {
  147. std::map<unsigned short, PackageSet> pkgsets;
  148. for (const char **I = cmdline; *I != 0; ++I) {
  149. unsigned short modID = fallback;
  150. std::string str = *I;
  151. bool modifierPresent = false;
  152. for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
  153. mod != mods.end(); ++mod) {
  154. size_t const alength = strlen(mod->Alias);
  155. switch(mod->Pos) {
  156. case PackageSet::Modifier::POSTFIX:
  157. if (str.compare(str.length() - alength, alength,
  158. mod->Alias, 0, alength) != 0)
  159. continue;
  160. str.erase(str.length() - alength);
  161. modID = mod->ID;
  162. break;
  163. case PackageSet::Modifier::PREFIX:
  164. continue;
  165. case PackageSet::Modifier::NONE:
  166. continue;
  167. }
  168. modifierPresent = true;
  169. break;
  170. }
  171. if (modifierPresent == true) {
  172. bool const errors = helper.showErrors(false);
  173. pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
  174. helper.showErrors(errors);
  175. if (Pkg.end() == false) {
  176. pkgsets[fallback].insert(Pkg);
  177. continue;
  178. }
  179. }
  180. pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
  181. }
  182. return pkgsets;
  183. }
  184. /*}}}*/
  185. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  186. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  187. PackageSet pkgset;
  188. for (const char **I = cmdline; *I != 0; ++I) {
  189. PackageSet pset = FromString(Cache, *I, helper);
  190. pkgset.insert(pset.begin(), pset.end());
  191. }
  192. return pkgset;
  193. }
  194. /*}}}*/
  195. // FromString - Return all packages matching a specific string /*{{{*/
  196. PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
  197. _error->PushToStack();
  198. PackageSet pkgset;
  199. pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
  200. if (Pkg.end() == false)
  201. pkgset.insert(Pkg);
  202. else {
  203. pkgset = FromTask(Cache, str, helper);
  204. if (pkgset.empty() == true) {
  205. pkgset = FromRegEx(Cache, str, helper);
  206. if (pkgset.empty() == true)
  207. pkgset = helper.canNotFindPackage(Cache, str);
  208. }
  209. }
  210. if (pkgset.empty() == false)
  211. _error->RevertToStack();
  212. else
  213. _error->MergeWithStack();
  214. return pkgset;
  215. }
  216. /*}}}*/
  217. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  218. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  219. pkgCacheFile &Cache, const char **cmdline,
  220. std::list<VersionSet::Modifier> const &mods,
  221. unsigned short const &fallback, CacheSetHelper &helper) {
  222. std::map<unsigned short, VersionSet> versets;
  223. for (const char **I = cmdline; *I != 0; ++I) {
  224. unsigned short modID = fallback;
  225. VersionSet::Version select = VersionSet::NEWEST;
  226. std::string str = *I;
  227. bool modifierPresent = false;
  228. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  229. mod != mods.end(); ++mod) {
  230. if (modID == fallback && mod->ID == fallback)
  231. select = mod->SelectVersion;
  232. size_t const alength = strlen(mod->Alias);
  233. switch(mod->Pos) {
  234. case VersionSet::Modifier::POSTFIX:
  235. if (str.compare(str.length() - alength, alength,
  236. mod->Alias, 0, alength) != 0)
  237. continue;
  238. str.erase(str.length() - alength);
  239. modID = mod->ID;
  240. select = mod->SelectVersion;
  241. break;
  242. case VersionSet::Modifier::PREFIX:
  243. continue;
  244. case VersionSet::Modifier::NONE:
  245. continue;
  246. }
  247. modifierPresent = true;
  248. break;
  249. }
  250. if (modifierPresent == true) {
  251. bool const errors = helper.showErrors(false);
  252. VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
  253. helper.showErrors(errors);
  254. if (vset.empty() == false) {
  255. versets[fallback].insert(vset);
  256. continue;
  257. }
  258. }
  259. versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
  260. }
  261. return versets;
  262. }
  263. /*}}}*/
  264. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  265. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  266. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  267. VersionSet verset;
  268. for (const char **I = cmdline; *I != 0; ++I)
  269. verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
  270. return verset;
  271. }
  272. /*}}}*/
  273. // FromString - Returns all versions spedcified by a string /*{{{*/
  274. APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
  275. APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
  276. bool const &onlyFromName) {
  277. std::string ver;
  278. bool verIsRel = false;
  279. size_t const vertag = pkg.find_last_of("/=");
  280. if (vertag != string::npos) {
  281. ver = pkg.substr(vertag+1);
  282. verIsRel = (pkg[vertag] == '/');
  283. pkg.erase(vertag);
  284. }
  285. PackageSet pkgset;
  286. if (onlyFromName == false)
  287. pkgset = PackageSet::FromString(Cache, pkg, helper);
  288. else {
  289. pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
  290. }
  291. VersionSet verset;
  292. bool errors = true;
  293. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  294. errors = helper.showErrors(false);
  295. for (PackageSet::const_iterator P = pkgset.begin();
  296. P != pkgset.end(); ++P) {
  297. if (vertag == string::npos) {
  298. verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
  299. continue;
  300. }
  301. pkgCache::VerIterator V;
  302. if (ver == "installed")
  303. V = getInstalledVer(Cache, P, helper);
  304. else if (ver == "candidate")
  305. V = getCandidateVer(Cache, P, helper);
  306. else {
  307. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  308. pkgVersionMatch::Version));
  309. V = Match.Find(P);
  310. if (V.end() == true) {
  311. if (verIsRel == true)
  312. _error->Error(_("Release '%s' for '%s' was not found"),
  313. ver.c_str(), P.FullName(true).c_str());
  314. else
  315. _error->Error(_("Version '%s' for '%s' was not found"),
  316. ver.c_str(), P.FullName(true).c_str());
  317. continue;
  318. }
  319. }
  320. if (V.end() == true)
  321. continue;
  322. helper.showSelectedVersion(P, V, ver, verIsRel);
  323. verset.insert(V);
  324. }
  325. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  326. helper.showErrors(errors);
  327. return verset;
  328. }
  329. /*}}}*/
  330. // FromPackage - versions from package based on fallback /*{{{*/
  331. VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  332. VersionSet::Version const &fallback, CacheSetHelper &helper) {
  333. VersionSet verset;
  334. pkgCache::VerIterator V;
  335. bool showErrors;
  336. switch(fallback) {
  337. case VersionSet::ALL:
  338. if (P->VersionList != 0)
  339. for (V = P.VersionList(); V.end() != true; ++V)
  340. verset.insert(V);
  341. else
  342. verset.insert(helper.canNotFindAllVer(Cache, P));
  343. break;
  344. case VersionSet::CANDANDINST:
  345. verset.insert(getInstalledVer(Cache, P, helper));
  346. verset.insert(getCandidateVer(Cache, P, helper));
  347. break;
  348. case VersionSet::CANDIDATE:
  349. verset.insert(getCandidateVer(Cache, P, helper));
  350. break;
  351. case VersionSet::INSTALLED:
  352. verset.insert(getInstalledVer(Cache, P, helper));
  353. break;
  354. case VersionSet::CANDINST:
  355. showErrors = helper.showErrors(false);
  356. V = getCandidateVer(Cache, P, helper);
  357. if (V.end() == true)
  358. V = getInstalledVer(Cache, P, helper);
  359. helper.showErrors(showErrors);
  360. if (V.end() == false)
  361. verset.insert(V);
  362. else
  363. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  364. break;
  365. case VersionSet::INSTCAND:
  366. showErrors = helper.showErrors(false);
  367. V = getInstalledVer(Cache, P, helper);
  368. if (V.end() == true)
  369. V = getCandidateVer(Cache, P, helper);
  370. helper.showErrors(showErrors);
  371. if (V.end() == false)
  372. verset.insert(V);
  373. else
  374. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  375. break;
  376. case VersionSet::NEWEST:
  377. if (P->VersionList != 0)
  378. verset.insert(P.VersionList());
  379. else
  380. verset.insert(helper.canNotFindNewestVer(Cache, P));
  381. break;
  382. }
  383. return verset;
  384. }
  385. /*}}}*/
  386. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  387. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  388. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  389. pkgCache::VerIterator Cand;
  390. if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false)
  391. {
  392. if (unlikely(Cache.GetPolicy() == 0))
  393. return pkgCache::VerIterator(Cache);
  394. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  395. } else {
  396. Cand = Cache[Pkg].CandidateVerIter(Cache);
  397. }
  398. if (Cand.end() == true)
  399. return helper.canNotFindCandidateVer(Cache, Pkg);
  400. return Cand;
  401. }
  402. /*}}}*/
  403. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  404. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  405. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  406. if (Pkg->CurrentVer == 0)
  407. return helper.canNotFindInstalledVer(Cache, Pkg);
  408. return Pkg.CurrentVer();
  409. }
  410. /*}}}*/
  411. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  412. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  413. std::string const &str) {
  414. if (ShowError == true)
  415. _error->Error(_("Unable to locate package %s"), str.c_str());
  416. return pkgCache::PkgIterator(Cache, 0);
  417. }
  418. /*}}}*/
  419. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  420. PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
  421. if (ShowError == true)
  422. _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
  423. return PackageSet();
  424. }
  425. /*}}}*/
  426. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  427. PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
  428. if (ShowError == true)
  429. _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
  430. return PackageSet();
  431. }
  432. /*}}}*/
  433. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  434. PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
  435. return PackageSet();
  436. }
  437. /*}}}*/
  438. // canNotFindAllVer /*{{{*/
  439. VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
  440. pkgCache::PkgIterator const &Pkg) {
  441. if (ShowError == true)
  442. _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
  443. return VersionSet();
  444. }
  445. /*}}}*/
  446. // canNotFindInstCandVer /*{{{*/
  447. VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
  448. pkgCache::PkgIterator const &Pkg) {
  449. if (ShowError == true)
  450. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  451. return VersionSet();
  452. }
  453. /*}}}*/
  454. // canNotFindInstCandVer /*{{{*/
  455. VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache,
  456. pkgCache::PkgIterator const &Pkg) {
  457. if (ShowError == true)
  458. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  459. return VersionSet();
  460. }
  461. /*}}}*/
  462. // canNotFindNewestVer /*{{{*/
  463. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  464. pkgCache::PkgIterator const &Pkg) {
  465. if (ShowError == true)
  466. _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  467. return pkgCache::VerIterator(Cache, 0);
  468. }
  469. /*}}}*/
  470. // canNotFindCandidateVer /*{{{*/
  471. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  472. pkgCache::PkgIterator const &Pkg) {
  473. if (ShowError == true)
  474. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  475. return pkgCache::VerIterator(Cache, 0);
  476. }
  477. /*}}}*/
  478. // canNotFindInstalledVer /*{{{*/
  479. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  480. pkgCache::PkgIterator const &Pkg) {
  481. if (ShowError == true)
  482. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  483. return pkgCache::VerIterator(Cache, 0);
  484. }
  485. /*}}}*/
  486. }