cacheset.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  67. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
  68. PackageSet pkgset;
  69. for (const char **I = cmdline + 1; *I != 0; I++) {
  70. PackageSet pset = FromString(Cache, *I, out);
  71. pkgset.insert(pset.begin(), pset.end());
  72. }
  73. return pkgset;
  74. }
  75. /*}}}*/
  76. // FromString - Return all packages matching a specific string /*{{{*/
  77. PackageSet PackageSet::FromString(pkgCacheFile &Cache, const char * const str, std::ostream &out) {
  78. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(str);
  79. if (Grp.end() == false) {
  80. pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
  81. PackageSet pkgset;
  82. pkgset.insert(Pkg);
  83. return pkgset;
  84. }
  85. PackageSet regex = FromRegEx(Cache, str, out);
  86. if (regex.empty() == true)
  87. _error->Warning(_("Unable to locate package %s"), str);
  88. return regex;
  89. }
  90. /*}}}*/
  91. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  92. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  93. APT::VersionSet::Version const &fallback, std::ostream &out) {
  94. VersionSet verset;
  95. for (const char **I = cmdline + 1; *I != 0; I++) {
  96. std::string pkg = *I;
  97. std::string ver;
  98. bool verIsRel = false;
  99. size_t const vertag = pkg.find_last_of("/=");
  100. if (vertag != string::npos) {
  101. ver = pkg.substr(vertag+1);
  102. verIsRel = (pkg[vertag] == '/');
  103. pkg.erase(vertag);
  104. }
  105. PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
  106. for (PackageSet::const_iterator P = pkgset.begin();
  107. P != pkgset.end(); ++P) {
  108. if (vertag != string::npos) {
  109. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  110. pkgVersionMatch::Version));
  111. pkgCache::VerIterator V = Match.Find(P);
  112. if (V.end() == true) {
  113. if (verIsRel == true)
  114. _error->Error(_("Release '%s' for '%s' was not found"),
  115. ver.c_str(), P.FullName(true).c_str());
  116. else
  117. _error->Error(_("Version '%s' for '%s' was not found"),
  118. ver.c_str(), P.FullName(true).c_str());
  119. continue;
  120. }
  121. if (strcmp(ver.c_str(), V.VerStr()) != 0)
  122. ioprintf(out, _("Selected version %s (%s) for %s\n"),
  123. V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
  124. verset.insert(V);
  125. } else {
  126. pkgCache::VerIterator V;
  127. switch(fallback) {
  128. case VersionSet::ALL:
  129. for (V = P.VersionList(); V.end() != true; ++V)
  130. verset.insert(V);
  131. break;
  132. case VersionSet::CANDANDINST:
  133. verset.insert(getInstalledVer(Cache, P));
  134. verset.insert(getCandidateVer(Cache, P));
  135. break;
  136. case VersionSet::CANDIDATE:
  137. verset.insert(getCandidateVer(Cache, P));
  138. break;
  139. case VersionSet::INSTALLED:
  140. verset.insert(getInstalledVer(Cache, P));
  141. break;
  142. case VersionSet::CANDINST:
  143. V = getCandidateVer(Cache, P, true);
  144. if (V.end() == true)
  145. V = getInstalledVer(Cache, P, true);
  146. if (V.end() == false)
  147. verset.insert(V);
  148. else
  149. _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
  150. break;
  151. case VersionSet::INSTCAND:
  152. V = getInstalledVer(Cache, P, true);
  153. if (V.end() == true)
  154. V = getCandidateVer(Cache, P, true);
  155. if (V.end() == false)
  156. verset.insert(V);
  157. else
  158. _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str());
  159. break;
  160. case VersionSet::NEWEST:
  161. if (P->VersionList != 0)
  162. verset.insert(P.VersionList());
  163. else
  164. _error->Error(_("Can't select newest version from package %s as it is purely virtual"), P.FullName(true).c_str());
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. return verset;
  171. }
  172. /*}}}*/
  173. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  174. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  175. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  176. if (unlikely(Cache.BuildDepCache() == false))
  177. return pkgCache::VerIterator(*Cache);
  178. pkgCache::VerIterator Cand = Cache[Pkg].InstVerIter(Cache);
  179. if (AllowError == false && Cand.end() == true)
  180. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  181. return Cand;
  182. }
  183. /*}}}*/
  184. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  185. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  186. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  187. if (AllowError == false && Pkg->CurrentVer == 0)
  188. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  189. return Pkg.CurrentVer();
  190. }
  191. /*}}}*/
  192. }