cacheset.cc 18 KB

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