cacheset.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, std::ostream &out) {
  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. pkgset.insert(Pkg);
  60. }
  61. if (pkgset.empty() == true)
  62. _error->Error(_("Couldn't find task %s"), pattern.c_str());
  63. regfree(&Pattern);
  64. return pkgset;
  65. }
  66. /*}}}*/
  67. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  68. PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) {
  69. PackageSet pkgset;
  70. static const char * const isregex = ".?+*|[^$";
  71. if (pattern.find_first_of(isregex) == std::string::npos)
  72. return pkgset;
  73. size_t archfound = pattern.find_last_of(':');
  74. std::string arch = "native";
  75. if (archfound != std::string::npos) {
  76. arch = pattern.substr(archfound+1);
  77. if (arch.find_first_of(isregex) == std::string::npos)
  78. pattern.erase(archfound);
  79. else
  80. arch = "native";
  81. }
  82. regex_t Pattern;
  83. int Res;
  84. if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
  85. char Error[300];
  86. regerror(Res, &Pattern, Error, sizeof(Error));
  87. _error->Error(_("Regex compilation error - %s"), Error);
  88. return pkgset;
  89. }
  90. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
  91. {
  92. if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
  93. continue;
  94. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  95. if (Pkg.end() == true) {
  96. if (archfound == std::string::npos) {
  97. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  98. for (std::vector<std::string>::const_iterator a = archs.begin();
  99. a != archs.end() && Pkg.end() != true; ++a)
  100. Pkg = Grp.FindPkg(*a);
  101. }
  102. if (Pkg.end() == true)
  103. continue;
  104. }
  105. ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
  106. Pkg.FullName(true).c_str(), pattern.c_str());
  107. pkgset.insert(Pkg);
  108. }
  109. regfree(&Pattern);
  110. return pkgset;
  111. }
  112. /*}}}*/
  113. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  114. std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
  115. pkgCacheFile &Cache, const char **cmdline,
  116. std::list<PackageSet::Modifier> const &mods,
  117. unsigned short const &fallback, std::ostream &out) {
  118. std::map<unsigned short, PackageSet> pkgsets;
  119. for (const char **I = cmdline; *I != 0; ++I) {
  120. unsigned short modID = fallback;
  121. std::string str = *I;
  122. for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
  123. mod != mods.end(); ++mod) {
  124. size_t const alength = strlen(mod->Alias);
  125. switch(mod->Pos) {
  126. case PackageSet::Modifier::POSTFIX:
  127. if (str.compare(str.length() - alength, alength,
  128. mod->Alias, 0, alength) != 0)
  129. continue;
  130. str.erase(str.length() - alength);
  131. modID = mod->ID;
  132. break;
  133. case PackageSet::Modifier::PREFIX:
  134. continue;
  135. case PackageSet::Modifier::NONE:
  136. continue;
  137. }
  138. break;
  139. }
  140. PackageSet pset = PackageSet::FromString(Cache, str, out);
  141. pkgsets[modID].insert(pset.begin(), pset.end());
  142. }
  143. return pkgsets;
  144. }
  145. /*}}}*/
  146. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  147. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
  148. PackageSet pkgset;
  149. for (const char **I = cmdline; *I != 0; ++I) {
  150. PackageSet pset = FromString(Cache, *I, out);
  151. pkgset.insert(pset.begin(), pset.end());
  152. }
  153. return pkgset;
  154. }
  155. /*}}}*/
  156. // FromString - Return all packages matching a specific string /*{{{*/
  157. PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) {
  158. std::string pkg = str;
  159. size_t archfound = pkg.find_last_of(':');
  160. std::string arch;
  161. if (archfound != std::string::npos) {
  162. arch = pkg.substr(archfound+1);
  163. pkg.erase(archfound);
  164. }
  165. pkgCache::PkgIterator Pkg;
  166. if (arch.empty() == true) {
  167. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  168. if (Grp.end() == false)
  169. Pkg = Grp.FindPreferredPkg();
  170. } else
  171. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  172. if (Pkg.end() == false) {
  173. PackageSet pkgset;
  174. pkgset.insert(Pkg);
  175. return pkgset;
  176. }
  177. PackageSet pset = FromTask(Cache, str, out);
  178. if (pset.empty() == false)
  179. return pset;
  180. pset = FromRegEx(Cache, str, out);
  181. if (pset.empty() == false)
  182. return pset;
  183. _error->Warning(_("Unable to locate package %s"), str.c_str());
  184. return pset;
  185. }
  186. /*}}}*/
  187. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  188. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  189. pkgCacheFile &Cache, const char **cmdline,
  190. std::list<VersionSet::Modifier> const &mods,
  191. unsigned short const &fallback, std::ostream &out) {
  192. std::map<unsigned short, VersionSet> versets;
  193. for (const char **I = cmdline; *I != 0; ++I) {
  194. unsigned short modID = fallback;
  195. VersionSet::Version select = VersionSet::NEWEST;
  196. std::string str = *I;
  197. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  198. mod != mods.end(); ++mod) {
  199. if (modID == fallback && mod->ID == fallback)
  200. select = mod->SelectVersion;
  201. size_t const alength = strlen(mod->Alias);
  202. switch(mod->Pos) {
  203. case VersionSet::Modifier::POSTFIX:
  204. if (str.compare(str.length() - alength, alength,
  205. mod->Alias, 0, alength) != 0)
  206. continue;
  207. str.erase(str.length() - alength);
  208. modID = mod->ID;
  209. select = mod->SelectVersion;
  210. break;
  211. case VersionSet::Modifier::PREFIX:
  212. continue;
  213. case VersionSet::Modifier::NONE:
  214. continue;
  215. }
  216. break;
  217. }
  218. VersionSet vset = VersionSet::FromString(Cache, str, select , out);
  219. versets[modID].insert(vset.begin(), vset.end());
  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, std::ostream &out) {
  227. VersionSet verset;
  228. for (const char **I = cmdline; *I != 0; ++I) {
  229. VersionSet vset = VersionSet::FromString(Cache, *I, fallback, out);
  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, std::ostream &out) {
  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(), out);
  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);
  252. continue;
  253. }
  254. pkgCache::VerIterator V;
  255. if (ver == "installed")
  256. V = getInstalledVer(Cache, P);
  257. else if (ver == "candidate")
  258. V = getCandidateVer(Cache, P);
  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. if (ver != V.VerStr())
  276. ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
  277. V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
  278. verset.insert(V);
  279. }
  280. return verset;
  281. }
  282. /*}}}*/
  283. // AddSelectedVersion - add version from package based on fallback /*{{{*/
  284. bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  285. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  286. bool const &AllowError) {
  287. pkgCache::VerIterator V;
  288. switch(fallback) {
  289. case VersionSet::ALL:
  290. if (P->VersionList != 0)
  291. for (V = P.VersionList(); V.end() != true; ++V)
  292. verset.insert(V);
  293. else if (AllowError == false)
  294. return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
  295. else
  296. return false;
  297. break;
  298. case VersionSet::CANDANDINST:
  299. verset.insert(getInstalledVer(Cache, P, AllowError));
  300. verset.insert(getCandidateVer(Cache, P, AllowError));
  301. break;
  302. case VersionSet::CANDIDATE:
  303. verset.insert(getCandidateVer(Cache, P, AllowError));
  304. break;
  305. case VersionSet::INSTALLED:
  306. verset.insert(getInstalledVer(Cache, P, AllowError));
  307. break;
  308. case VersionSet::CANDINST:
  309. V = getCandidateVer(Cache, P, true);
  310. if (V.end() == true)
  311. V = getInstalledVer(Cache, P, true);
  312. if (V.end() == false)
  313. verset.insert(V);
  314. else if (AllowError == false)
  315. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  316. else
  317. return false;
  318. break;
  319. case VersionSet::INSTCAND:
  320. V = getInstalledVer(Cache, P, true);
  321. if (V.end() == true)
  322. V = getCandidateVer(Cache, P, true);
  323. if (V.end() == false)
  324. verset.insert(V);
  325. else if (AllowError == false)
  326. return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
  327. else
  328. return false;
  329. break;
  330. case VersionSet::NEWEST:
  331. if (P->VersionList != 0)
  332. verset.insert(P.VersionList());
  333. else if (AllowError == false)
  334. return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
  335. else
  336. return false;
  337. break;
  338. }
  339. return true;
  340. }
  341. /*}}}*/
  342. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  343. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  344. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  345. pkgCache::VerIterator Cand;
  346. if (Cache.IsDepCacheBuilt() == true)
  347. Cand = Cache[Pkg].CandidateVerIter(Cache);
  348. else {
  349. if (unlikely(Cache.BuildPolicy() == false))
  350. return pkgCache::VerIterator(*Cache);
  351. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  352. }
  353. if (AllowError == false && Cand.end() == true)
  354. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  355. return Cand;
  356. }
  357. /*}}}*/
  358. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  359. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  360. pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
  361. if (AllowError == false && Pkg->CurrentVer == 0)
  362. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  363. return Pkg.CurrentVer();
  364. }
  365. /*}}}*/
  366. }