cacheset.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  137. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  138. APT::VersionSet::Version const &fallback, std::ostream &out) {
  139. VersionSet verset;
  140. for (const char **I = cmdline; *I != 0; ++I) {
  141. std::string pkg = *I;
  142. std::string ver;
  143. bool verIsRel = false;
  144. size_t const vertag = pkg.find_last_of("/=");
  145. if (vertag != string::npos) {
  146. ver = pkg.substr(vertag+1);
  147. verIsRel = (pkg[vertag] == '/');
  148. pkg.erase(vertag);
  149. }
  150. PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
  151. for (PackageSet::const_iterator P = pkgset.begin();
  152. P != pkgset.end(); ++P) {
  153. if (vertag == string::npos) {
  154. AddSelectedVersion(Cache, verset, P, fallback);
  155. continue;
  156. }
  157. pkgCache::VerIterator V;
  158. if (ver == "installed")
  159. V = getInstalledVer(Cache, P);
  160. else if (ver == "candidate")
  161. V = getCandidateVer(Cache, P);
  162. else {
  163. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  164. pkgVersionMatch::Version));
  165. V = Match.Find(P);
  166. if (V.end() == true) {
  167. if (verIsRel == true)
  168. _error->Error(_("Release '%s' for '%s' was not found"),
  169. ver.c_str(), P.FullName(true).c_str());
  170. else
  171. _error->Error(_("Version '%s' for '%s' was not found"),
  172. ver.c_str(), P.FullName(true).c_str());
  173. continue;
  174. }
  175. }
  176. if (V.end() == true)
  177. continue;
  178. if (ver == V.VerStr())
  179. ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
  180. V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
  181. verset.insert(V);
  182. }
  183. }
  184. return verset;
  185. }
  186. /*}}}*/
  187. // AddSelectedVersion - add version from package based on fallback /*{{{*/
  188. bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  189. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  190. bool const &AllowError) {
  191. pkgCache::VerIterator V;
  192. switch(fallback) {
  193. case VersionSet::ALL:
  194. if (P->VersionList != 0)
  195. for (V = P.VersionList(); V.end() != true; ++V)
  196. verset.insert(V);
  197. else if (AllowError == false)
  198. return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
  199. else
  200. return false;
  201. break;
  202. case VersionSet::CANDANDINST:
  203. verset.insert(getInstalledVer(Cache, P, AllowError));
  204. verset.insert(getCandidateVer(Cache, P, AllowError));
  205. break;
  206. case VersionSet::CANDIDATE:
  207. verset.insert(getCandidateVer(Cache, P, AllowError));
  208. break;
  209. case VersionSet::INSTALLED:
  210. verset.insert(getInstalledVer(Cache, P, AllowError));
  211. break;
  212. case VersionSet::CANDINST:
  213. V = getCandidateVer(Cache, P, true);
  214. if (V.end() == true)
  215. V = getInstalledVer(Cache, P, true);
  216. if (V.end() == false)
  217. verset.insert(V);
  218. else if (AllowError == false)
  219. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  220. else
  221. return false;
  222. break;
  223. case VersionSet::INSTCAND:
  224. V = getInstalledVer(Cache, P, true);
  225. if (V.end() == true)
  226. V = getCandidateVer(Cache, P, true);
  227. if (V.end() == false)
  228. verset.insert(V);
  229. else if (AllowError == false)
  230. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  231. else
  232. return false;
  233. break;
  234. case VersionSet::NEWEST:
  235. if (P->VersionList != 0)
  236. verset.insert(P.VersionList());
  237. else if (AllowError == false)
  238. return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
  239. else
  240. return false;
  241. break;
  242. }
  243. return true;
  244. }
  245. /*}}}*/
  246. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  247. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  248. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  249. if (unlikely(Cache.BuildDepCache() == false))
  250. return pkgCache::VerIterator(*Cache);
  251. pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache);
  252. if (AllowError == false && Cand.end() == true)
  253. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  254. return Cand;
  255. }
  256. /*}}}*/
  257. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  258. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  259. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  260. if (AllowError == false && Pkg->CurrentVer == 0)
  261. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  262. return Pkg.CurrentVer();
  263. }
  264. /*}}}*/
  265. }