cacheset.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. PackageSet pkgset;
  23. if (Cache.BuildCaches() == false || Cache.BuildDepCache() == false)
  24. return pkgset;
  25. size_t archfound = pattern.find_last_of(':');
  26. std::string arch = "native";
  27. if (archfound != std::string::npos) {
  28. arch = pattern.substr(archfound+1);
  29. pattern.erase(archfound);
  30. }
  31. if (pattern[pattern.length() -1] != '^')
  32. return pkgset;
  33. pattern.erase(pattern.length()-1);
  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. PackageSet pkgset;
  72. static const char * const isregex = ".?+*|[^$";
  73. if (pattern.find_first_of(isregex) == std::string::npos)
  74. return pkgset;
  75. size_t archfound = pattern.find_last_of(':');
  76. std::string arch = "native";
  77. if (archfound != std::string::npos) {
  78. arch = pattern.substr(archfound+1);
  79. if (arch.find_first_of(isregex) == std::string::npos)
  80. pattern.erase(archfound);
  81. else
  82. arch = "native";
  83. }
  84. regex_t Pattern;
  85. int Res;
  86. if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
  87. char Error[300];
  88. regerror(Res, &Pattern, Error, sizeof(Error));
  89. _error->Error(_("Regex compilation error - %s"), Error);
  90. return pkgset;
  91. }
  92. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
  93. {
  94. if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
  95. continue;
  96. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  97. if (Pkg.end() == true) {
  98. if (archfound == std::string::npos) {
  99. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  100. for (std::vector<std::string>::const_iterator a = archs.begin();
  101. a != archs.end() && Pkg.end() != true; ++a)
  102. Pkg = Grp.FindPkg(*a);
  103. }
  104. if (Pkg.end() == true)
  105. continue;
  106. }
  107. pkgset.insert(Pkg);
  108. }
  109. regfree(&Pattern);
  110. if (pkgset.empty() == true)
  111. return helper.canNotFindRegEx(Cache, pattern);
  112. helper.showRegExSelection(pkgset, pattern);
  113. return pkgset;
  114. }
  115. /*}}}*/
  116. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  117. std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
  118. pkgCacheFile &Cache, const char **cmdline,
  119. std::list<PackageSet::Modifier> const &mods,
  120. unsigned short const &fallback, CacheSetHelper &helper) {
  121. std::map<unsigned short, PackageSet> pkgsets;
  122. for (const char **I = cmdline; *I != 0; ++I) {
  123. unsigned short modID = fallback;
  124. std::string str = *I;
  125. for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
  126. mod != mods.end(); ++mod) {
  127. size_t const alength = strlen(mod->Alias);
  128. switch(mod->Pos) {
  129. case PackageSet::Modifier::POSTFIX:
  130. if (str.compare(str.length() - alength, alength,
  131. mod->Alias, 0, alength) != 0)
  132. continue;
  133. str.erase(str.length() - alength);
  134. modID = mod->ID;
  135. break;
  136. case PackageSet::Modifier::PREFIX:
  137. continue;
  138. case PackageSet::Modifier::NONE:
  139. continue;
  140. }
  141. break;
  142. }
  143. pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
  144. }
  145. return pkgsets;
  146. }
  147. /*}}}*/
  148. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  149. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  150. PackageSet pkgset;
  151. for (const char **I = cmdline; *I != 0; ++I) {
  152. PackageSet pset = FromString(Cache, *I, helper);
  153. pkgset.insert(pset.begin(), pset.end());
  154. }
  155. return pkgset;
  156. }
  157. /*}}}*/
  158. // FromString - Return all packages matching a specific string /*{{{*/
  159. PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
  160. std::string pkg = str;
  161. size_t archfound = pkg.find_last_of(':');
  162. std::string arch;
  163. if (archfound != std::string::npos) {
  164. arch = pkg.substr(archfound+1);
  165. pkg.erase(archfound);
  166. }
  167. pkgCache::PkgIterator Pkg;
  168. if (arch.empty() == true) {
  169. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  170. if (Grp.end() == false)
  171. Pkg = Grp.FindPreferredPkg();
  172. } else
  173. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  174. if (Pkg.end() == false) {
  175. PackageSet pkgset;
  176. pkgset.insert(Pkg);
  177. return pkgset;
  178. }
  179. PackageSet pset = FromTask(Cache, str, helper);
  180. if (pset.empty() == false)
  181. return pset;
  182. pset = FromRegEx(Cache, str, helper);
  183. if (pset.empty() == false)
  184. return pset;
  185. return helper.canNotFindPackage(Cache, str);
  186. }
  187. /*}}}*/
  188. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  189. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  190. pkgCacheFile &Cache, const char **cmdline,
  191. std::list<VersionSet::Modifier> const &mods,
  192. unsigned short const &fallback, CacheSetHelper &helper) {
  193. std::map<unsigned short, VersionSet> versets;
  194. for (const char **I = cmdline; *I != 0; ++I) {
  195. unsigned short modID = fallback;
  196. VersionSet::Version select = VersionSet::NEWEST;
  197. std::string str = *I;
  198. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  199. mod != mods.end(); ++mod) {
  200. if (modID == fallback && mod->ID == fallback)
  201. select = mod->SelectVersion;
  202. size_t const alength = strlen(mod->Alias);
  203. switch(mod->Pos) {
  204. case VersionSet::Modifier::POSTFIX:
  205. if (str.compare(str.length() - alength, alength,
  206. mod->Alias, 0, alength) != 0)
  207. continue;
  208. str.erase(str.length() - alength);
  209. modID = mod->ID;
  210. select = mod->SelectVersion;
  211. break;
  212. case VersionSet::Modifier::PREFIX:
  213. continue;
  214. case VersionSet::Modifier::NONE:
  215. continue;
  216. }
  217. break;
  218. }
  219. versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
  220. }
  221. return versets;
  222. }
  223. /*}}}*/
  224. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  225. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  226. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  227. VersionSet verset;
  228. for (const char **I = cmdline; *I != 0; ++I) {
  229. VersionSet vset = VersionSet::FromString(Cache, *I, fallback, helper);
  230. verset.insert(vset.begin(), vset.end());
  231. }
  232. return verset;
  233. }
  234. /*}}}*/
  235. // FromString - Returns all versions spedcified by a string /*{{{*/
  236. APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
  237. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  238. std::string ver;
  239. bool verIsRel = false;
  240. size_t const vertag = pkg.find_last_of("/=");
  241. if (vertag != string::npos) {
  242. ver = pkg.substr(vertag+1);
  243. verIsRel = (pkg[vertag] == '/');
  244. pkg.erase(vertag);
  245. }
  246. PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), helper);
  247. VersionSet verset;
  248. for (PackageSet::const_iterator P = pkgset.begin();
  249. P != pkgset.end(); ++P) {
  250. if (vertag == string::npos) {
  251. AddSelectedVersion(Cache, verset, P, fallback, helper);
  252. continue;
  253. }
  254. pkgCache::VerIterator V;
  255. if (ver == "installed")
  256. V = getInstalledVer(Cache, P, helper);
  257. else if (ver == "candidate")
  258. V = getCandidateVer(Cache, P, helper);
  259. else {
  260. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  261. pkgVersionMatch::Version));
  262. V = Match.Find(P);
  263. if (V.end() == true) {
  264. if (verIsRel == true)
  265. _error->Error(_("Release '%s' for '%s' was not found"),
  266. ver.c_str(), P.FullName(true).c_str());
  267. else
  268. _error->Error(_("Version '%s' for '%s' was not found"),
  269. ver.c_str(), P.FullName(true).c_str());
  270. continue;
  271. }
  272. }
  273. if (V.end() == true)
  274. continue;
  275. helper.showSelectedVersion(P, V, ver, verIsRel);
  276. verset.insert(V);
  277. }
  278. return verset;
  279. }
  280. /*}}}*/
  281. // AddSelectedVersion - add version from package based on fallback /*{{{*/
  282. void VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  283. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  284. CacheSetHelper &helper) {
  285. pkgCache::VerIterator V;
  286. bool showErrors;
  287. switch(fallback) {
  288. case VersionSet::ALL:
  289. if (P->VersionList != 0)
  290. for (V = P.VersionList(); V.end() != true; ++V)
  291. verset.insert(V);
  292. else
  293. verset.insert(helper.canNotFindAllVer(Cache, P));
  294. break;
  295. case VersionSet::CANDANDINST:
  296. verset.insert(getInstalledVer(Cache, P, helper));
  297. verset.insert(getCandidateVer(Cache, P, helper));
  298. break;
  299. case VersionSet::CANDIDATE:
  300. verset.insert(getCandidateVer(Cache, P, helper));
  301. break;
  302. case VersionSet::INSTALLED:
  303. verset.insert(getInstalledVer(Cache, P, helper));
  304. break;
  305. case VersionSet::CANDINST:
  306. showErrors = helper.showErrors(false);
  307. V = getCandidateVer(Cache, P, helper);
  308. if (V.end() == true)
  309. V = getInstalledVer(Cache, P, helper);
  310. helper.showErrors(showErrors);
  311. if (V.end() == false)
  312. verset.insert(V);
  313. else
  314. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  315. break;
  316. case VersionSet::INSTCAND:
  317. showErrors = helper.showErrors(false);
  318. V = getInstalledVer(Cache, P, helper);
  319. if (V.end() == true)
  320. V = getCandidateVer(Cache, P, helper);
  321. helper.showErrors(showErrors);
  322. if (V.end() == false)
  323. verset.insert(V);
  324. else
  325. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  326. break;
  327. case VersionSet::NEWEST:
  328. if (P->VersionList != 0)
  329. verset.insert(P.VersionList());
  330. else
  331. verset.insert(helper.canNotFindNewestVer(Cache, P));
  332. break;
  333. }
  334. }
  335. /*}}}*/
  336. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  337. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  338. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  339. pkgCache::VerIterator Cand;
  340. if (Cache.IsDepCacheBuilt() == true)
  341. Cand = Cache[Pkg].CandidateVerIter(Cache);
  342. else {
  343. if (unlikely(Cache.BuildPolicy() == false))
  344. return pkgCache::VerIterator(*Cache);
  345. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  346. }
  347. if (Cand.end() == true)
  348. return helper.canNotFindCandidateVer(Cache, Pkg);
  349. return Cand;
  350. }
  351. /*}}}*/
  352. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  353. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  354. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  355. if (Pkg->CurrentVer == 0)
  356. return helper.canNotFindInstalledVer(Cache, Pkg);
  357. return Pkg.CurrentVer();
  358. }
  359. /*}}}*/
  360. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  361. PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
  362. if (ShowError == true)
  363. _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
  364. return PackageSet();
  365. }
  366. /*}}}*/
  367. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  368. PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
  369. if (ShowError == true)
  370. _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
  371. return PackageSet();
  372. }
  373. /*}}}*/
  374. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  375. PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
  376. if (ShowError == true)
  377. _error->Error(_("Unable to locate package %s"), str.c_str());
  378. return PackageSet();
  379. }
  380. /*}}}*/
  381. // canNotFindAllVer /*{{{*/
  382. VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
  383. pkgCache::PkgIterator const &Pkg) {
  384. if (ShowError == true)
  385. _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
  386. return VersionSet();
  387. }
  388. /*}}}*/
  389. // canNotFindInstCandVer /*{{{*/
  390. VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
  391. pkgCache::PkgIterator const &Pkg) {
  392. if (ShowError == true)
  393. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  394. return VersionSet();
  395. }
  396. /*}}}*/
  397. // canNotFindNewestVer /*{{{*/
  398. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  399. pkgCache::PkgIterator const &Pkg) {
  400. if (ShowError == true)
  401. _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  402. return pkgCache::VerIterator(*Cache);
  403. }
  404. /*}}}*/
  405. // canNotFindCandidateVer /*{{{*/
  406. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  407. pkgCache::PkgIterator const &Pkg) {
  408. if (ShowError == true)
  409. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  410. return pkgCache::VerIterator(*Cache);
  411. }
  412. /*}}}*/
  413. // canNotFindInstalledVer /*{{{*/
  414. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  415. pkgCache::PkgIterator const &Pkg) {
  416. if (ShowError == true)
  417. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  418. return pkgCache::VerIterator(*Cache);
  419. }
  420. /*}}}*/
  421. }