cacheset.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. unsigned short fallback = modID;
  190. bool modifierPresent = false;
  191. for (std::list<Modifier>::const_iterator mod = mods.begin();
  192. mod != mods.end(); ++mod) {
  193. size_t const alength = strlen(mod->Alias);
  194. switch(mod->Pos) {
  195. case Modifier::POSTFIX:
  196. if (str.compare(str.length() - alength, alength,
  197. mod->Alias, 0, alength) != 0)
  198. continue;
  199. str.erase(str.length() - alength);
  200. modID = mod->ID;
  201. break;
  202. case Modifier::PREFIX:
  203. continue;
  204. case Modifier::NONE:
  205. continue;
  206. }
  207. modifierPresent = true;
  208. break;
  209. }
  210. if (modifierPresent == true) {
  211. bool const errors = helper.showErrors(false);
  212. pkgCache::PkgIterator Pkg = FromName(Cache, cmdline, helper);
  213. helper.showErrors(errors);
  214. if (Pkg.end() == false) {
  215. pci->insert(Pkg);
  216. modID = fallback;
  217. return true;
  218. }
  219. }
  220. return FromString(pci, Cache, str, helper);
  221. }
  222. /*}}}*/
  223. // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
  224. bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
  225. VersionContainerInterface * const vci,
  226. pkgCacheFile &Cache, const char * cmdline,
  227. std::list<Modifier> const &mods,
  228. CacheSetHelper &helper) {
  229. Version select = NEWEST;
  230. std::string str = cmdline;
  231. bool modifierPresent = false;
  232. unsigned short fallback = modID;
  233. for (std::list<Modifier>::const_iterator mod = mods.begin();
  234. mod != mods.end(); ++mod) {
  235. if (modID == fallback && mod->ID == fallback)
  236. select = mod->SelectVersion;
  237. size_t const alength = strlen(mod->Alias);
  238. switch(mod->Pos) {
  239. case Modifier::POSTFIX:
  240. if (str.compare(str.length() - alength, alength,
  241. mod->Alias, 0, alength) != 0)
  242. continue;
  243. str.erase(str.length() - alength);
  244. modID = mod->ID;
  245. select = mod->SelectVersion;
  246. break;
  247. case Modifier::PREFIX:
  248. continue;
  249. case Modifier::NONE:
  250. continue;
  251. }
  252. modifierPresent = true;
  253. break;
  254. }
  255. if (modifierPresent == true) {
  256. bool const errors = helper.showErrors(false);
  257. bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
  258. helper.showErrors(errors);
  259. if (found == true) {
  260. modID = fallback;
  261. return true;
  262. }
  263. }
  264. return FromString(vci, Cache, str, select, helper);
  265. }
  266. /*}}}*/
  267. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  268. bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
  269. pkgCacheFile &Cache, const char **cmdline,
  270. Version const &fallback, CacheSetHelper &helper) {
  271. bool found = false;
  272. for (const char **I = cmdline; *I != 0; ++I)
  273. found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
  274. return found;
  275. }
  276. /*}}}*/
  277. // FromString - Returns all versions spedcified by a string /*{{{*/
  278. bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
  279. pkgCacheFile &Cache, std::string pkg,
  280. Version const &fallback, CacheSetHelper &helper,
  281. bool const onlyFromName) {
  282. std::string ver;
  283. bool verIsRel = false;
  284. size_t const vertag = pkg.find_last_of("/=");
  285. if (vertag != std::string::npos) {
  286. ver = pkg.substr(vertag+1);
  287. verIsRel = (pkg[vertag] == '/');
  288. pkg.erase(vertag);
  289. }
  290. PackageSet pkgset;
  291. if (onlyFromName == false)
  292. PackageContainerInterface::FromString(&pkgset, Cache, pkg, helper);
  293. else {
  294. pkgset.insert(PackageContainerInterface::FromName(Cache, pkg, helper));
  295. }
  296. bool errors = true;
  297. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  298. errors = helper.showErrors(false);
  299. bool found = false;
  300. for (PackageSet::const_iterator P = pkgset.begin();
  301. P != pkgset.end(); ++P) {
  302. if (vertag == std::string::npos) {
  303. found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
  304. continue;
  305. }
  306. pkgCache::VerIterator V;
  307. if (ver == "installed")
  308. V = getInstalledVer(Cache, P, helper);
  309. else if (ver == "candidate")
  310. V = getCandidateVer(Cache, P, helper);
  311. else if (ver == "newest") {
  312. if (P->VersionList != 0)
  313. V = P.VersionList();
  314. else
  315. V = helper.canNotFindNewestVer(Cache, P);
  316. } else {
  317. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  318. pkgVersionMatch::Version));
  319. V = Match.Find(P);
  320. if (V.end() == true) {
  321. if (verIsRel == true)
  322. _error->Error(_("Release '%s' for '%s' was not found"),
  323. ver.c_str(), P.FullName(true).c_str());
  324. else
  325. _error->Error(_("Version '%s' for '%s' was not found"),
  326. ver.c_str(), P.FullName(true).c_str());
  327. continue;
  328. }
  329. }
  330. if (V.end() == true)
  331. continue;
  332. helper.showSelectedVersion(P, V, ver, verIsRel);
  333. vci->insert(V);
  334. found = true;
  335. }
  336. if (pkgset.getConstructor() != PackageSet::UNKNOWN)
  337. helper.showErrors(errors);
  338. return found;
  339. }
  340. /*}}}*/
  341. // FromPackage - versions from package based on fallback /*{{{*/
  342. bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
  343. pkgCacheFile &Cache,
  344. pkgCache::PkgIterator const &P,
  345. Version const &fallback,
  346. CacheSetHelper &helper) {
  347. pkgCache::VerIterator V;
  348. bool showErrors;
  349. bool found = false;
  350. switch(fallback) {
  351. case ALL:
  352. if (P->VersionList != 0)
  353. for (V = P.VersionList(); V.end() != true; ++V)
  354. found |= vci->insert(V);
  355. else
  356. helper.canNotFindAllVer(vci, Cache, P);
  357. break;
  358. case CANDANDINST:
  359. found |= vci->insert(getInstalledVer(Cache, P, helper));
  360. found |= vci->insert(getCandidateVer(Cache, P, helper));
  361. break;
  362. case CANDIDATE:
  363. found |= vci->insert(getCandidateVer(Cache, P, helper));
  364. break;
  365. case INSTALLED:
  366. found |= vci->insert(getInstalledVer(Cache, P, helper));
  367. break;
  368. case CANDINST:
  369. showErrors = helper.showErrors(false);
  370. V = getCandidateVer(Cache, P, helper);
  371. if (V.end() == true)
  372. V = getInstalledVer(Cache, P, helper);
  373. helper.showErrors(showErrors);
  374. if (V.end() == false)
  375. found |= vci->insert(V);
  376. else
  377. helper.canNotFindInstCandVer(vci, Cache, P);
  378. break;
  379. case INSTCAND:
  380. showErrors = helper.showErrors(false);
  381. V = getInstalledVer(Cache, P, helper);
  382. if (V.end() == true)
  383. V = getCandidateVer(Cache, P, helper);
  384. helper.showErrors(showErrors);
  385. if (V.end() == false)
  386. found |= vci->insert(V);
  387. else
  388. helper.canNotFindInstCandVer(vci, Cache, P);
  389. break;
  390. case NEWEST:
  391. if (P->VersionList != 0)
  392. found |= vci->insert(P.VersionList());
  393. else
  394. helper.canNotFindNewestVer(Cache, P);
  395. break;
  396. }
  397. return found;
  398. }
  399. /*}}}*/
  400. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  401. pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
  402. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  403. pkgCache::VerIterator Cand;
  404. if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false) {
  405. if (unlikely(Cache.GetPolicy() == 0))
  406. return pkgCache::VerIterator(Cache);
  407. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  408. } else {
  409. Cand = Cache[Pkg].CandidateVerIter(Cache);
  410. }
  411. if (Cand.end() == true)
  412. return helper.canNotFindCandidateVer(Cache, Pkg);
  413. return Cand;
  414. }
  415. /*}}}*/
  416. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  417. pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
  418. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  419. if (Pkg->CurrentVer == 0)
  420. return helper.canNotFindInstalledVer(Cache, Pkg);
  421. return Pkg.CurrentVer();
  422. }
  423. /*}}}*/
  424. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  425. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  426. std::string const &str) {
  427. if (ShowError == true)
  428. _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
  429. return pkgCache::PkgIterator(Cache, 0);
  430. }
  431. /*}}}*/
  432. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  433. void CacheSetHelper::canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
  434. if (ShowError == true)
  435. _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
  436. }
  437. /*}}}*/
  438. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  439. void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
  440. if (ShowError == true)
  441. _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
  442. }
  443. /*}}}*/
  444. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  445. void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str) {
  446. }
  447. /*}}}*/
  448. // canNotFindAllVer /*{{{*/
  449. void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  450. pkgCache::PkgIterator const &Pkg) {
  451. if (ShowError == true)
  452. _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  453. }
  454. /*}}}*/
  455. // canNotFindInstCandVer /*{{{*/
  456. void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  457. pkgCache::PkgIterator const &Pkg) {
  458. if (ShowError == true)
  459. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  460. }
  461. /*}}}*/
  462. // canNotFindInstCandVer /*{{{*/
  463. void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  464. pkgCache::PkgIterator const &Pkg) {
  465. if (ShowError == true)
  466. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  467. }
  468. /*}}}*/
  469. // canNotFindNewestVer /*{{{*/
  470. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  471. pkgCache::PkgIterator const &Pkg) {
  472. if (ShowError == true)
  473. _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  474. return pkgCache::VerIterator(Cache, 0);
  475. }
  476. /*}}}*/
  477. // canNotFindCandidateVer /*{{{*/
  478. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  479. pkgCache::PkgIterator const &Pkg) {
  480. if (ShowError == true)
  481. _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  482. return pkgCache::VerIterator(Cache, 0);
  483. }
  484. /*}}}*/
  485. // canNotFindInstalledVer /*{{{*/
  486. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  487. pkgCache::PkgIterator const &Pkg) {
  488. if (ShowError == true)
  489. _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  490. return pkgCache::VerIterator(Cache, 0);
  491. }
  492. /*}}}*/
  493. // showTaskSelection /*{{{*/
  494. void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &pkg,
  495. std::string const &pattern) {
  496. }
  497. /*}}}*/
  498. // showRegExSelection /*{{{*/
  499. void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &pkg,
  500. std::string const &pattern) {
  501. }
  502. /*}}}*/
  503. // showSelectedVersion /*{{{*/
  504. void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &Pkg,
  505. pkgCache::VerIterator const Ver,
  506. std::string const &ver,
  507. bool const verIsRel) {
  508. }
  509. /*}}}*/
  510. }