cacheset.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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/cachefilter.h>
  12. #include <apt-pkg/cacheset.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/versionmatch.h>
  16. #include <apti18n.h>
  17. #include <vector>
  18. #include <regex.h>
  19. /*}}}*/
  20. namespace APT {
  21. // FromTask - Return all packages in the cache from a specific task /*{{{*/
  22. PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  23. size_t const archfound = pattern.find_last_of(':');
  24. std::string arch = "native";
  25. if (archfound != std::string::npos) {
  26. arch = pattern.substr(archfound+1);
  27. pattern.erase(archfound);
  28. }
  29. if (pattern[pattern.length() -1] != '^')
  30. return APT::PackageSet(TASK);
  31. pattern.erase(pattern.length()-1);
  32. if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
  33. return APT::PackageSet(TASK);
  34. PackageSet pkgset(TASK);
  35. // get the records
  36. pkgRecords Recs(Cache);
  37. // build regexp for the task
  38. regex_t Pattern;
  39. char S[300];
  40. snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
  41. if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
  42. _error->Error("Failed to compile task regexp");
  43. return pkgset;
  44. }
  45. for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
  46. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  47. if (Pkg.end() == true)
  48. continue;
  49. pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
  50. if(ver.end() == true)
  51. continue;
  52. pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
  53. const char *start, *end;
  54. parser.GetRec(start,end);
  55. unsigned int const length = end - start;
  56. char buf[length];
  57. strncpy(buf, start, length);
  58. buf[length-1] = '\0';
  59. if (regexec(&Pattern, buf, 0, 0, 0) != 0)
  60. continue;
  61. pkgset.insert(Pkg);
  62. }
  63. regfree(&Pattern);
  64. if (pkgset.empty() == true)
  65. return helper.canNotFindTask(Cache, pattern);
  66. helper.showTaskSelection(pkgset, pattern);
  67. return pkgset;
  68. }
  69. /*}}}*/
  70. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  71. PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  72. static const char * const isregex = ".?+*|[^$";
  73. if (pattern.find_first_of(isregex) == std::string::npos)
  74. return PackageSet(REGEX);
  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. if (unlikely(Cache.GetPkgCache() == 0))
  85. return PackageSet(REGEX);
  86. APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
  87. PackageSet pkgset(REGEX);
  88. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
  89. if (regexfilter(Grp) == false)
  90. continue;
  91. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  92. if (Pkg.end() == true) {
  93. if (archfound == std::string::npos) {
  94. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  95. for (std::vector<std::string>::const_iterator a = archs.begin();
  96. a != archs.end() && Pkg.end() != true; ++a)
  97. Pkg = Grp.FindPkg(*a);
  98. }
  99. if (Pkg.end() == true)
  100. continue;
  101. }
  102. pkgset.insert(Pkg);
  103. }
  104. if (pkgset.empty() == true)
  105. return helper.canNotFindRegEx(Cache, pattern);
  106. helper.showRegExSelection(pkgset, pattern);
  107. return pkgset;
  108. }
  109. /*}}}*/
  110. // FromName - Returns the package defined by this string /*{{{*/
  111. pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
  112. std::string const &str, CacheSetHelper &helper) {
  113. std::string pkg = str;
  114. size_t archfound = pkg.find_last_of(':');
  115. std::string arch;
  116. if (archfound != std::string::npos) {
  117. arch = pkg.substr(archfound+1);
  118. pkg.erase(archfound);
  119. }
  120. if (Cache.GetPkgCache() == 0)
  121. return pkgCache::PkgIterator(Cache, 0);
  122. pkgCache::PkgIterator Pkg(Cache, 0);
  123. if (arch.empty() == true) {
  124. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  125. if (Grp.end() == false)
  126. Pkg = Grp.FindPreferredPkg();
  127. } else
  128. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  129. if (Pkg.end() == true)
  130. return helper.canNotFindPkgName(Cache, str);
  131. return Pkg;
  132. }
  133. /*}}}*/
  134. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  135. std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
  136. pkgCacheFile &Cache, const char **cmdline,
  137. std::list<PackageSet::Modifier> const &mods,
  138. unsigned short const &fallback, CacheSetHelper &helper) {
  139. std::map<unsigned short, PackageSet> pkgsets;
  140. for (const char **I = cmdline; *I != 0; ++I) {
  141. unsigned short modID = fallback;
  142. std::string str = *I;
  143. bool modifierPresent = false;
  144. for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
  145. mod != mods.end(); ++mod) {
  146. size_t const alength = strlen(mod->Alias);
  147. switch(mod->Pos) {
  148. case PackageSet::Modifier::POSTFIX:
  149. if (str.compare(str.length() - alength, alength,
  150. mod->Alias, 0, alength) != 0)
  151. continue;
  152. str.erase(str.length() - alength);
  153. modID = mod->ID;
  154. break;
  155. case PackageSet::Modifier::PREFIX:
  156. continue;
  157. case PackageSet::Modifier::NONE:
  158. continue;
  159. }
  160. modifierPresent = true;
  161. break;
  162. }
  163. if (modifierPresent == true) {
  164. bool const errors = helper.showErrors(false);
  165. pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
  166. helper.showErrors(errors);
  167. if (Pkg.end() == false) {
  168. pkgsets[fallback].insert(Pkg);
  169. continue;
  170. }
  171. }
  172. pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
  173. }
  174. return pkgsets;
  175. }
  176. /*}}}*/
  177. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  178. PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  179. PackageSet pkgset;
  180. for (const char **I = cmdline; *I != 0; ++I) {
  181. PackageSet pset = FromString(Cache, *I, helper);
  182. pkgset.insert(pset.begin(), pset.end());
  183. }
  184. return pkgset;
  185. }
  186. /*}}}*/
  187. // FromString - Return all packages matching a specific string /*{{{*/
  188. PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
  189. _error->PushToStack();
  190. PackageSet pkgset;
  191. pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
  192. if (Pkg.end() == false)
  193. pkgset.insert(Pkg);
  194. else {
  195. pkgset = FromTask(Cache, str, helper);
  196. if (pkgset.empty() == true) {
  197. pkgset = FromRegEx(Cache, str, helper);
  198. if (pkgset.empty() == true)
  199. pkgset = helper.canNotFindPackage(Cache, str);
  200. }
  201. }
  202. if (pkgset.empty() == false)
  203. _error->RevertToStack();
  204. else
  205. _error->MergeWithStack();
  206. return pkgset;
  207. }
  208. /*}}}*/
  209. // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
  210. std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
  211. pkgCacheFile &Cache, const char **cmdline,
  212. std::list<VersionSet::Modifier> const &mods,
  213. unsigned short const &fallback, CacheSetHelper &helper) {
  214. std::map<unsigned short, VersionSet> versets;
  215. for (const char **I = cmdline; *I != 0; ++I) {
  216. unsigned short modID = fallback;
  217. VersionSet::Version select = VersionSet::NEWEST;
  218. std::string str = *I;
  219. bool modifierPresent = false;
  220. for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
  221. mod != mods.end(); ++mod) {
  222. if (modID == fallback && mod->ID == fallback)
  223. select = mod->SelectVersion;
  224. size_t const alength = strlen(mod->Alias);
  225. switch(mod->Pos) {
  226. case VersionSet::Modifier::POSTFIX:
  227. if (str.compare(str.length() - alength, alength,
  228. mod->Alias, 0, alength) != 0)
  229. continue;
  230. str.erase(str.length() - alength);
  231. modID = mod->ID;
  232. select = mod->SelectVersion;
  233. break;
  234. case VersionSet::Modifier::PREFIX:
  235. continue;
  236. case VersionSet::Modifier::NONE:
  237. continue;
  238. }
  239. modifierPresent = true;
  240. break;
  241. }
  242. if (modifierPresent == true) {
  243. bool const errors = helper.showErrors(false);
  244. VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
  245. helper.showErrors(errors);
  246. if (vset.empty() == false) {
  247. versets[fallback].insert(vset);
  248. continue;
  249. }
  250. }
  251. versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
  252. }
  253. return versets;
  254. }
  255. /*}}}*/
  256. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  257. APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  258. APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
  259. VersionSet verset;
  260. for (const char **I = cmdline; *I != 0; ++I)
  261. verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
  262. return verset;
  263. }
  264. /*}}}*/
  265. // FromString - Returns all versions spedcified by a string /*{{{*/
  266. APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
  267. APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
  268. bool const &onlyFromName) {
  269. std::string ver;
  270. bool verIsRel = false;
  271. size_t const vertag = pkg.find_last_of("/=");
  272. if (vertag != string::npos) {
  273. ver = pkg.substr(vertag+1);
  274. verIsRel = (pkg[vertag] == '/');
  275. pkg.erase(vertag);
  276. }
  277. PackageSet pkgset;
  278. if (onlyFromName == false)
  279. pkgset = PackageSet::FromString(Cache, pkg, helper);
  280. else {
  281. pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
  282. }
  283. VersionSet verset;
  284. bool errors = true;
  285. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  286. errors = helper.showErrors(false);
  287. for (PackageSet::const_iterator P = pkgset.begin();
  288. P != pkgset.end(); ++P) {
  289. if (vertag == string::npos) {
  290. verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
  291. continue;
  292. }
  293. pkgCache::VerIterator V;
  294. if (ver == "installed")
  295. V = getInstalledVer(Cache, P, helper);
  296. else if (ver == "candidate")
  297. V = getCandidateVer(Cache, P, helper);
  298. else {
  299. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  300. pkgVersionMatch::Version));
  301. V = Match.Find(P);
  302. if (V.end() == true) {
  303. if (verIsRel == true)
  304. _error->Error(_("Release '%s' for '%s' was not found"),
  305. ver.c_str(), P.FullName(true).c_str());
  306. else
  307. _error->Error(_("Version '%s' for '%s' was not found"),
  308. ver.c_str(), P.FullName(true).c_str());
  309. continue;
  310. }
  311. }
  312. if (V.end() == true)
  313. continue;
  314. helper.showSelectedVersion(P, V, ver, verIsRel);
  315. verset.insert(V);
  316. }
  317. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  318. helper.showErrors(errors);
  319. return verset;
  320. }
  321. /*}}}*/
  322. // FromPackage - versions from package based on fallback /*{{{*/
  323. VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  324. VersionSet::Version const &fallback, CacheSetHelper &helper) {
  325. VersionSet verset;
  326. pkgCache::VerIterator V;
  327. bool showErrors;
  328. switch(fallback) {
  329. case VersionSet::ALL:
  330. if (P->VersionList != 0)
  331. for (V = P.VersionList(); V.end() != true; ++V)
  332. verset.insert(V);
  333. else
  334. verset.insert(helper.canNotFindAllVer(Cache, P));
  335. break;
  336. case VersionSet::CANDANDINST:
  337. verset.insert(getInstalledVer(Cache, P, helper));
  338. verset.insert(getCandidateVer(Cache, P, helper));
  339. break;
  340. case VersionSet::CANDIDATE:
  341. verset.insert(getCandidateVer(Cache, P, helper));
  342. break;
  343. case VersionSet::INSTALLED:
  344. verset.insert(getInstalledVer(Cache, P, helper));
  345. break;
  346. case VersionSet::CANDINST:
  347. showErrors = helper.showErrors(false);
  348. V = getCandidateVer(Cache, P, helper);
  349. if (V.end() == true)
  350. V = getInstalledVer(Cache, P, helper);
  351. helper.showErrors(showErrors);
  352. if (V.end() == false)
  353. verset.insert(V);
  354. else
  355. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  356. break;
  357. case VersionSet::INSTCAND:
  358. showErrors = helper.showErrors(false);
  359. V = getInstalledVer(Cache, P, helper);
  360. if (V.end() == true)
  361. V = getCandidateVer(Cache, P, helper);
  362. helper.showErrors(showErrors);
  363. if (V.end() == false)
  364. verset.insert(V);
  365. else
  366. verset.insert(helper.canNotFindInstCandVer(Cache, P));
  367. break;
  368. case VersionSet::NEWEST:
  369. if (P->VersionList != 0)
  370. verset.insert(P.VersionList());
  371. else
  372. verset.insert(helper.canNotFindNewestVer(Cache, P));
  373. break;
  374. }
  375. return verset;
  376. }
  377. /*}}}*/
  378. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  379. pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
  380. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  381. pkgCache::VerIterator Cand;
  382. if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false)
  383. {
  384. if (unlikely(Cache.GetPolicy() == 0))
  385. return pkgCache::VerIterator(Cache);
  386. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  387. } else {
  388. Cand = Cache[Pkg].CandidateVerIter(Cache);
  389. }
  390. if (Cand.end() == true)
  391. return helper.canNotFindCandidateVer(Cache, Pkg);
  392. return Cand;
  393. }
  394. /*}}}*/
  395. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  396. pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
  397. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  398. if (Pkg->CurrentVer == 0)
  399. return helper.canNotFindInstalledVer(Cache, Pkg);
  400. return Pkg.CurrentVer();
  401. }
  402. /*}}}*/
  403. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  404. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  405. std::string const &str) {
  406. if (ShowError == true)
  407. _error->Error(_("Unable to locate package %s"), str.c_str());
  408. return pkgCache::PkgIterator(Cache, 0);
  409. }
  410. /*}}}*/
  411. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  412. PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
  413. if (ShowError == true)
  414. _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
  415. return PackageSet();
  416. }
  417. /*}}}*/
  418. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  419. PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
  420. if (ShowError == true)
  421. _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
  422. return PackageSet();
  423. }
  424. /*}}}*/
  425. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  426. PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
  427. return PackageSet();
  428. }
  429. /*}}}*/
  430. // canNotFindAllVer /*{{{*/
  431. VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
  432. pkgCache::PkgIterator const &Pkg) {
  433. if (ShowError == true)
  434. _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
  435. return VersionSet();
  436. }
  437. /*}}}*/
  438. // canNotFindInstCandVer /*{{{*/
  439. VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
  440. pkgCache::PkgIterator const &Pkg) {
  441. if (ShowError == true)
  442. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  443. return VersionSet();
  444. }
  445. /*}}}*/
  446. // canNotFindInstCandVer /*{{{*/
  447. VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache,
  448. pkgCache::PkgIterator const &Pkg) {
  449. if (ShowError == true)
  450. _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  451. return VersionSet();
  452. }
  453. /*}}}*/
  454. // canNotFindNewestVer /*{{{*/
  455. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  456. pkgCache::PkgIterator const &Pkg) {
  457. if (ShowError == true)
  458. _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  459. return pkgCache::VerIterator(Cache, 0);
  460. }
  461. /*}}}*/
  462. // canNotFindCandidateVer /*{{{*/
  463. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  464. pkgCache::PkgIterator const &Pkg) {
  465. if (ShowError == true)
  466. _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  467. return pkgCache::VerIterator(Cache, 0);
  468. }
  469. /*}}}*/
  470. // canNotFindInstalledVer /*{{{*/
  471. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  472. pkgCache::PkgIterator const &Pkg) {
  473. if (ShowError == true)
  474. _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  475. return pkgCache::VerIterator(Cache, 0);
  476. }
  477. /*}}}*/
  478. }