cacheset.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/cacheset.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/versionmatch.h>
  15. #include <apti18n.h>
  16. #include <vector>
  17. #include <regex.h>
  18. /*}}}*/
  19. namespace APT {
  20. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  21. PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) {
  22. PackageSet pkgset;
  23. std::string arch = "native";
  24. static const char * const isregex = ".?+*|[^$";
  25. if (pattern.find_first_of(isregex) == std::string::npos)
  26. return pkgset;
  27. size_t archfound = pattern.find_last_of(':');
  28. if (archfound != std::string::npos) {
  29. arch = pattern.substr(archfound+1);
  30. if (arch.find_first_of(isregex) == std::string::npos)
  31. pattern.erase(archfound);
  32. else
  33. arch = "native";
  34. }
  35. regex_t Pattern;
  36. int Res;
  37. if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
  38. char Error[300];
  39. regerror(Res, &Pattern, Error, sizeof(Error));
  40. _error->Error(_("Regex compilation error - %s"), Error);
  41. return pkgset;
  42. }
  43. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
  44. {
  45. if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
  46. continue;
  47. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  48. if (Pkg.end() == true) {
  49. if (archfound == std::string::npos) {
  50. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  51. for (std::vector<std::string>::const_iterator a = archs.begin();
  52. a != archs.end() && Pkg.end() != true; ++a)
  53. Pkg = Grp.FindPkg(*a);
  54. }
  55. if (Pkg.end() == true)
  56. continue;
  57. }
  58. ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
  59. Pkg.FullName(true).c_str(), pattern.c_str());
  60. pkgset.insert(Pkg);
  61. }
  62. regfree(&Pattern);
  63. return pkgset;
  64. }
  65. /*}}}*/
  66. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  67. std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
  68. pkgCacheFile &Cache, const char **cmdline,
  69. std::list<PackageSet::Modifier> const &mods,
  70. unsigned short const &fallback, std::ostream &out) {
  71. std::map<unsigned short, PackageSet> pkgsets;
  72. for (const char **I = cmdline; *I != 0; ++I) {
  73. unsigned short modID = fallback;
  74. std::string str = *I;
  75. for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
  76. mod != mods.end(); ++mod) {
  77. size_t const alength = strlen(mod->Alias);
  78. switch(mod->Pos) {
  79. case PackageSet::Modifier::POSTFIX:
  80. if (str.compare(str.length() - alength, alength,
  81. mod->Alias, 0, alength) != 0)
  82. continue;
  83. str.erase(str.length() - alength);
  84. modID = mod->ID;
  85. break;
  86. case PackageSet::Modifier::PREFIX:
  87. continue;
  88. case PackageSet::Modifier::NONE:
  89. continue;
  90. }
  91. break;
  92. }
  93. PackageSet pset = PackageSet::FromString(Cache, str, out);
  94. pkgsets[modID].insert(pset.begin(), pset.end());
  95. }
  96. return pkgsets;
  97. }
  98. /*}}}*/
  99. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  100. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
  101. PackageSet pkgset;
  102. for (const char **I = cmdline; *I != 0; ++I) {
  103. PackageSet pset = FromString(Cache, *I, out);
  104. pkgset.insert(pset.begin(), pset.end());
  105. }
  106. return pkgset;
  107. }
  108. /*}}}*/
  109. // FromString - Return all packages matching a specific string /*{{{*/
  110. PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) {
  111. std::string pkg = str;
  112. size_t archfound = pkg.find_last_of(':');
  113. std::string arch;
  114. if (archfound != std::string::npos) {
  115. arch = pkg.substr(archfound+1);
  116. pkg.erase(archfound);
  117. }
  118. pkgCache::PkgIterator Pkg;
  119. if (arch.empty() == true) {
  120. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  121. if (Grp.end() == false)
  122. Pkg = Grp.FindPreferredPkg();
  123. } else
  124. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  125. if (Pkg.end() == false) {
  126. PackageSet pkgset;
  127. pkgset.insert(Pkg);
  128. return pkgset;
  129. }
  130. PackageSet regex = FromRegEx(Cache, str, out);
  131. if (regex.empty() == true)
  132. _error->Warning(_("Unable to locate package %s"), str.c_str());
  133. return regex;
  134. }
  135. /*}}}*/
  136. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  137. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  138. pkgCacheFile &Cache, const char **cmdline,
  139. std::list<VersionSet::Modifier> const &mods,
  140. unsigned short const &fallback, std::ostream &out) {
  141. std::map<unsigned short, VersionSet> versets;
  142. for (const char **I = cmdline; *I != 0; ++I) {
  143. unsigned short modID = fallback;
  144. VersionSet::Version select = VersionSet::NEWEST;
  145. std::string str = *I;
  146. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  147. mod != mods.end(); ++mod) {
  148. if (modID == fallback && mod->ID == fallback)
  149. select = mod->SelectVersion;
  150. size_t const alength = strlen(mod->Alias);
  151. switch(mod->Pos) {
  152. case VersionSet::Modifier::POSTFIX:
  153. if (str.compare(str.length() - alength, alength,
  154. mod->Alias, 0, alength) != 0)
  155. continue;
  156. str.erase(str.length() - alength);
  157. modID = mod->ID;
  158. select = mod->SelectVersion;
  159. break;
  160. case VersionSet::Modifier::PREFIX:
  161. continue;
  162. case VersionSet::Modifier::NONE:
  163. continue;
  164. }
  165. break;
  166. }
  167. VersionSet vset = VersionSet::FromString(Cache, str, select , out);
  168. versets[modID].insert(vset.begin(), vset.end());
  169. }
  170. return versets;
  171. }
  172. /*}}}*/
  173. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  174. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  175. APT::VersionSet::Version const &fallback, std::ostream &out) {
  176. VersionSet verset;
  177. for (const char **I = cmdline; *I != 0; ++I) {
  178. VersionSet vset = VersionSet::FromString(Cache, *I, fallback, out);
  179. verset.insert(vset.begin(), vset.end());
  180. }
  181. return verset;
  182. }
  183. /*}}}*/
  184. // FromString - Returns all versions spedcified by a string /*{{{*/
  185. APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
  186. APT::VersionSet::Version const &fallback, std::ostream &out) {
  187. std::string ver;
  188. bool verIsRel = false;
  189. size_t const vertag = pkg.find_last_of("/=");
  190. if (vertag != string::npos) {
  191. ver = pkg.substr(vertag+1);
  192. verIsRel = (pkg[vertag] == '/');
  193. pkg.erase(vertag);
  194. }
  195. PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
  196. VersionSet verset;
  197. for (PackageSet::const_iterator P = pkgset.begin();
  198. P != pkgset.end(); ++P) {
  199. if (vertag == string::npos) {
  200. AddSelectedVersion(Cache, verset, P, fallback);
  201. continue;
  202. }
  203. pkgCache::VerIterator V;
  204. if (ver == "installed")
  205. V = getInstalledVer(Cache, P);
  206. else if (ver == "candidate")
  207. V = getCandidateVer(Cache, P);
  208. else {
  209. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  210. pkgVersionMatch::Version));
  211. V = Match.Find(P);
  212. if (V.end() == true) {
  213. if (verIsRel == true)
  214. _error->Error(_("Release '%s' for '%s' was not found"),
  215. ver.c_str(), P.FullName(true).c_str());
  216. else
  217. _error->Error(_("Version '%s' for '%s' was not found"),
  218. ver.c_str(), P.FullName(true).c_str());
  219. continue;
  220. }
  221. }
  222. if (V.end() == true)
  223. continue;
  224. if (ver == V.VerStr())
  225. ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
  226. V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
  227. verset.insert(V);
  228. }
  229. return verset;
  230. }
  231. /*}}}*/
  232. // AddSelectedVersion - add version from package based on fallback /*{{{*/
  233. bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  234. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  235. bool const &AllowError) {
  236. pkgCache::VerIterator V;
  237. switch(fallback) {
  238. case VersionSet::ALL:
  239. if (P->VersionList != 0)
  240. for (V = P.VersionList(); V.end() != true; ++V)
  241. verset.insert(V);
  242. else if (AllowError == false)
  243. return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
  244. else
  245. return false;
  246. break;
  247. case VersionSet::CANDANDINST:
  248. verset.insert(getInstalledVer(Cache, P, AllowError));
  249. verset.insert(getCandidateVer(Cache, P, AllowError));
  250. break;
  251. case VersionSet::CANDIDATE:
  252. verset.insert(getCandidateVer(Cache, P, AllowError));
  253. break;
  254. case VersionSet::INSTALLED:
  255. verset.insert(getInstalledVer(Cache, P, AllowError));
  256. break;
  257. case VersionSet::CANDINST:
  258. V = getCandidateVer(Cache, P, true);
  259. if (V.end() == true)
  260. V = getInstalledVer(Cache, P, true);
  261. if (V.end() == false)
  262. verset.insert(V);
  263. else if (AllowError == false)
  264. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  265. else
  266. return false;
  267. break;
  268. case VersionSet::INSTCAND:
  269. V = getInstalledVer(Cache, P, true);
  270. if (V.end() == true)
  271. V = getCandidateVer(Cache, P, true);
  272. if (V.end() == false)
  273. verset.insert(V);
  274. else if (AllowError == false)
  275. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  276. else
  277. return false;
  278. break;
  279. case VersionSet::NEWEST:
  280. if (P->VersionList != 0)
  281. verset.insert(P.VersionList());
  282. else if (AllowError == false)
  283. return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
  284. else
  285. return false;
  286. break;
  287. }
  288. return true;
  289. }
  290. /*}}}*/
  291. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  292. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  293. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  294. pkgCache::VerIterator Cand;
  295. if (Cache.IsDepCacheBuilt() == true)
  296. Cand = Cache[Pkg].CandidateVerIter(Cache);
  297. else {
  298. if (unlikely(Cache.BuildPolicy() == false))
  299. return pkgCache::VerIterator(*Cache);
  300. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  301. }
  302. if (AllowError == false && Cand.end() == true)
  303. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  304. return Cand;
  305. }
  306. /*}}}*/
  307. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  308. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  309. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  310. if (AllowError == false && Pkg->CurrentVer == 0)
  311. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  312. return Pkg.CurrentVer();
  313. }
  314. /*}}}*/
  315. }