cacheset.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. _error->PushToStack();
  180. PackageSet pset = FromTask(Cache, str, helper);
  181. if (pset.empty() == true) {
  182. pset = FromRegEx(Cache, str, helper);
  183. if (pset.empty() == true)
  184. pset = helper.canNotFindPackage(Cache, str);
  185. }
  186. if (pset.empty() == false)
  187. _error->RevertToStack();
  188. else
  189. _error->MergeWithStack();
  190. return pset;
  191. }
  192. /*}}}*/
  193. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  194. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  195. pkgCacheFile &Cache, const char **cmdline,
  196. std::list<VersionSet::Modifier> const &mods,
  197. unsigned short const &fallback, CacheSetHelper &helper) {
  198. std::map<unsigned short, VersionSet> versets;
  199. for (const char **I = cmdline; *I != 0; ++I) {
  200. unsigned short modID = fallback;
  201. VersionSet::Version select = VersionSet::NEWEST;
  202. std::string str = *I;
  203. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  204. mod != mods.end(); ++mod) {
  205. if (modID == fallback && mod->ID == fallback)
  206. select = mod->SelectVersion;
  207. size_t const alength = strlen(mod->Alias);
  208. switch(mod->Pos) {
  209. case VersionSet::Modifier::POSTFIX:
  210. if (str.compare(str.length() - alength, alength,
  211. mod->Alias, 0, alength) != 0)
  212. continue;
  213. str.erase(str.length() - alength);
  214. modID = mod->ID;
  215. select = mod->SelectVersion;
  216. break;
  217. case VersionSet::Modifier::PREFIX:
  218. continue;
  219. case VersionSet::Modifier::NONE:
  220. continue;
  221. }
  222. break;
  223. }
  224. versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
  225. }
  226. return versets;
  227. }
  228. /*}}}*/
  229. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  230. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  231. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  232. VersionSet verset;
  233. for (const char **I = cmdline; *I != 0; ++I) {
  234. VersionSet vset = VersionSet::FromString(Cache, *I, fallback, helper);
  235. verset.insert(vset.begin(), vset.end());
  236. }
  237. return verset;
  238. }
  239. /*}}}*/
  240. // FromString - Returns all versions spedcified by a string /*{{{*/
  241. APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
  242. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  243. std::string ver;
  244. bool verIsRel = false;
  245. size_t const vertag = pkg.find_last_of("/=");
  246. if (vertag != string::npos) {
  247. ver = pkg.substr(vertag+1);
  248. verIsRel = (pkg[vertag] == '/');
  249. pkg.erase(vertag);
  250. }
  251. PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), helper);
  252. VersionSet verset;
  253. for (PackageSet::const_iterator P = pkgset.begin();
  254. P != pkgset.end(); ++P) {
  255. if (vertag == string::npos) {
  256. AddSelectedVersion(Cache, verset, P, fallback, helper);
  257. continue;
  258. }
  259. pkgCache::VerIterator V;
  260. if (ver == "installed")
  261. V = getInstalledVer(Cache, P, helper);
  262. else if (ver == "candidate")
  263. V = getCandidateVer(Cache, P, helper);
  264. else {
  265. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  266. pkgVersionMatch::Version));
  267. V = Match.Find(P);
  268. if (V.end() == true) {
  269. if (verIsRel == true)
  270. _error->Error(_("Release '%s' for '%s' was not found"),
  271. ver.c_str(), P.FullName(true).c_str());
  272. else
  273. _error->Error(_("Version '%s' for '%s' was not found"),
  274. ver.c_str(), P.FullName(true).c_str());
  275. continue;
  276. }
  277. }
  278. if (V.end() == true)
  279. continue;
  280. helper.showSelectedVersion(P, V, ver, verIsRel);
  281. verset.insert(V);
  282. }
  283. return verset;
  284. }
  285. /*}}}*/
  286. // AddSelectedVersion - add version from package based on fallback /*{{{*/
  287. void VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  288. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  289. CacheSetHelper &helper) {
  290. pkgCache::VerIterator V;
  291. bool showErrors;
  292. switch(fallback) {
  293. case VersionSet::ALL:
  294. if (P->VersionList != 0)
  295. for (V = P.VersionList(); V.end() != true; ++V)
  296. verset.insert(V);
  297. else
  298. verset.insert(helper.canNotFindAllVer(Cache, P));
  299. break;
  300. case VersionSet::CANDANDINST:
  301. verset.insert(getInstalledVer(Cache, P, helper));
  302. verset.insert(getCandidateVer(Cache, P, helper));
  303. break;
  304. case VersionSet::CANDIDATE:
  305. verset.insert(getCandidateVer(Cache, P, helper));
  306. break;
  307. case VersionSet::INSTALLED:
  308. verset.insert(getInstalledVer(Cache, P, helper));
  309. break;
  310. case VersionSet::CANDINST:
  311. showErrors = helper.showErrors(false);
  312. V = getCandidateVer(Cache, P, helper);
  313. if (V.end() == true)
  314. V = getInstalledVer(Cache, P, helper);
  315. helper.showErrors(showErrors);
  316. if (V.end() == false)
  317. verset.insert(V);
  318. else
  319. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  320. break;
  321. case VersionSet::INSTCAND:
  322. showErrors = helper.showErrors(false);
  323. V = getInstalledVer(Cache, P, helper);
  324. if (V.end() == true)
  325. V = getCandidateVer(Cache, P, helper);
  326. helper.showErrors(showErrors);
  327. if (V.end() == false)
  328. verset.insert(V);
  329. else
  330. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  331. break;
  332. case VersionSet::NEWEST:
  333. if (P->VersionList != 0)
  334. verset.insert(P.VersionList());
  335. else
  336. verset.insert(helper.canNotFindNewestVer(Cache, P));
  337. break;
  338. }
  339. }
  340. /*}}}*/
  341. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  342. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  343. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  344. pkgCache::VerIterator Cand;
  345. if (Cache.IsDepCacheBuilt() == true)
  346. Cand = Cache[Pkg].CandidateVerIter(Cache);
  347. else {
  348. if (unlikely(Cache.BuildPolicy() == false))
  349. return pkgCache::VerIterator(*Cache);
  350. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  351. }
  352. if (Cand.end() == true)
  353. return helper.canNotFindCandidateVer(Cache, Pkg);
  354. return Cand;
  355. }
  356. /*}}}*/
  357. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  358. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  359. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  360. if (Pkg->CurrentVer == 0)
  361. return helper.canNotFindInstalledVer(Cache, Pkg);
  362. return Pkg.CurrentVer();
  363. }
  364. /*}}}*/
  365. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  366. PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
  367. if (ShowError == true)
  368. _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
  369. return PackageSet();
  370. }
  371. /*}}}*/
  372. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  373. PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
  374. if (ShowError == true)
  375. _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
  376. return PackageSet();
  377. }
  378. /*}}}*/
  379. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  380. PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
  381. if (ShowError == true)
  382. _error->Error(_("Unable to locate package %s"), str.c_str());
  383. return PackageSet();
  384. }
  385. /*}}}*/
  386. // canNotFindAllVer /*{{{*/
  387. VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
  388. pkgCache::PkgIterator const &Pkg) {
  389. if (ShowError == true)
  390. _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
  391. return VersionSet();
  392. }
  393. /*}}}*/
  394. // canNotFindInstCandVer /*{{{*/
  395. VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
  396. pkgCache::PkgIterator const &Pkg) {
  397. if (ShowError == true)
  398. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  399. return VersionSet();
  400. }
  401. /*}}}*/
  402. // canNotFindNewestVer /*{{{*/
  403. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  404. pkgCache::PkgIterator const &Pkg) {
  405. if (ShowError == true)
  406. _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  407. return pkgCache::VerIterator(*Cache);
  408. }
  409. /*}}}*/
  410. // canNotFindCandidateVer /*{{{*/
  411. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  412. pkgCache::PkgIterator const &Pkg) {
  413. if (ShowError == true)
  414. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  415. return pkgCache::VerIterator(*Cache);
  416. }
  417. /*}}}*/
  418. // canNotFindInstalledVer /*{{{*/
  419. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  420. pkgCache::PkgIterator const &Pkg) {
  421. if (ShowError == true)
  422. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  423. return pkgCache::VerIterator(*Cache);
  424. }
  425. /*}}}*/
  426. }