cacheset.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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/versionmatch.h>
  17. #include <apt-pkg/pkgrecords.h>
  18. #include <apt-pkg/policy.h>
  19. #include <apt-pkg/cacheiterators.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <apt-pkg/depcache.h>
  22. #include <apt-pkg/macros.h>
  23. #include <apt-pkg/pkgcache.h>
  24. #include <apt-pkg/fileutl.h>
  25. #include <stddef.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <regex.h>
  29. #include <list>
  30. #include <string>
  31. #include <vector>
  32. #include <apti18n.h>
  33. /*}}}*/
  34. namespace APT {
  35. // FromTask - Return all packages in the cache from a specific task /*{{{*/
  36. bool PackageContainerInterface::FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  37. size_t const archfound = pattern.find_last_of(':');
  38. std::string arch = "native";
  39. if (archfound != std::string::npos) {
  40. arch = pattern.substr(archfound+1);
  41. pattern.erase(archfound);
  42. }
  43. if (pattern[pattern.length() -1] != '^')
  44. return false;
  45. pattern.erase(pattern.length()-1);
  46. if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
  47. return false;
  48. bool const wasEmpty = pci->empty();
  49. if (wasEmpty == true)
  50. pci->setConstructor(CacheSetHelper::TASK);
  51. // get the records
  52. pkgRecords Recs(Cache);
  53. // build regexp for the task
  54. regex_t Pattern;
  55. char S[300];
  56. snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
  57. if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
  58. _error->Error("Failed to compile task regexp");
  59. return false;
  60. }
  61. bool found = false;
  62. for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
  63. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  64. if (Pkg.end() == true)
  65. continue;
  66. pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
  67. if(ver.end() == true)
  68. continue;
  69. pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
  70. const char *start, *end;
  71. parser.GetRec(start,end);
  72. unsigned int const length = end - start;
  73. if (unlikely(length == 0))
  74. continue;
  75. char buf[length];
  76. strncpy(buf, start, length);
  77. buf[length-1] = '\0';
  78. if (regexec(&Pattern, buf, 0, 0, 0) != 0)
  79. continue;
  80. pci->insert(Pkg);
  81. helper.showPackageSelection(Pkg, CacheSetHelper::TASK, pattern);
  82. found = true;
  83. }
  84. regfree(&Pattern);
  85. if (found == false) {
  86. helper.canNotFindPackage(CacheSetHelper::TASK, pci, Cache, pattern);
  87. pci->setConstructor(CacheSetHelper::UNKNOWN);
  88. return false;
  89. }
  90. if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
  91. pci->setConstructor(CacheSetHelper::UNKNOWN);
  92. return true;
  93. }
  94. /*}}}*/
  95. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  96. bool PackageContainerInterface::FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  97. static const char * const isregex = ".?+*|[^$";
  98. if (pattern.find_first_of(isregex) == std::string::npos)
  99. return false;
  100. bool const wasEmpty = pci->empty();
  101. if (wasEmpty == true)
  102. pci->setConstructor(CacheSetHelper::REGEX);
  103. size_t archfound = pattern.find_last_of(':');
  104. std::string arch = "native";
  105. if (archfound != std::string::npos) {
  106. arch = pattern.substr(archfound+1);
  107. if (arch.find_first_of(isregex) == std::string::npos)
  108. pattern.erase(archfound);
  109. else
  110. arch = "native";
  111. }
  112. if (unlikely(Cache.GetPkgCache() == 0))
  113. return false;
  114. APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
  115. bool found = false;
  116. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
  117. if (regexfilter(Grp) == false)
  118. continue;
  119. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  120. if (Pkg.end() == true) {
  121. if (archfound == std::string::npos) {
  122. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  123. for (std::vector<std::string>::const_iterator a = archs.begin();
  124. a != archs.end() && Pkg.end() != true; ++a)
  125. Pkg = Grp.FindPkg(*a);
  126. }
  127. if (Pkg.end() == true)
  128. continue;
  129. }
  130. pci->insert(Pkg);
  131. helper.showPackageSelection(Pkg, CacheSetHelper::REGEX, pattern);
  132. found = true;
  133. }
  134. if (found == false) {
  135. helper.canNotFindPackage(CacheSetHelper::REGEX, pci, Cache, pattern);
  136. pci->setConstructor(CacheSetHelper::UNKNOWN);
  137. return false;
  138. }
  139. if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
  140. pci->setConstructor(CacheSetHelper::UNKNOWN);
  141. return true;
  142. }
  143. /*}}}*/
  144. // FromFnmatch - Returns the package defined by this fnmatch /*{{{*/
  145. bool
  146. PackageContainerInterface::FromFnmatch(PackageContainerInterface * const pci,
  147. pkgCacheFile &Cache,
  148. std::string pattern,
  149. CacheSetHelper &helper)
  150. {
  151. static const char * const isfnmatch = ".?*[]!";
  152. if (pattern.find_first_of(isfnmatch) == std::string::npos)
  153. return false;
  154. bool const wasEmpty = pci->empty();
  155. if (wasEmpty == true)
  156. pci->setConstructor(CacheSetHelper::FNMATCH);
  157. size_t archfound = pattern.find_last_of(':');
  158. std::string arch = "native";
  159. if (archfound != std::string::npos) {
  160. arch = pattern.substr(archfound+1);
  161. if (arch.find_first_of(isfnmatch) == std::string::npos)
  162. pattern.erase(archfound);
  163. else
  164. arch = "native";
  165. }
  166. if (unlikely(Cache.GetPkgCache() == 0))
  167. return false;
  168. APT::CacheFilter::PackageNameMatchesFnmatch filter(pattern);
  169. bool found = false;
  170. for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
  171. if (filter(Grp) == false)
  172. continue;
  173. pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
  174. if (Pkg.end() == true) {
  175. if (archfound == std::string::npos) {
  176. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  177. for (std::vector<std::string>::const_iterator a = archs.begin();
  178. a != archs.end() && Pkg.end() != true; ++a)
  179. Pkg = Grp.FindPkg(*a);
  180. }
  181. if (Pkg.end() == true)
  182. continue;
  183. }
  184. pci->insert(Pkg);
  185. helper.showPackageSelection(Pkg, CacheSetHelper::FNMATCH, pattern);
  186. found = true;
  187. }
  188. if (found == false) {
  189. helper.canNotFindPackage(CacheSetHelper::FNMATCH, pci, Cache, pattern);
  190. pci->setConstructor(CacheSetHelper::UNKNOWN);
  191. return false;
  192. }
  193. if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
  194. pci->setConstructor(CacheSetHelper::UNKNOWN);
  195. return true;
  196. }
  197. /*}}}*/
  198. // FromName - Returns the package defined by this string /*{{{*/
  199. pkgCache::PkgIterator PackageContainerInterface::FromName(pkgCacheFile &Cache,
  200. std::string const &str, CacheSetHelper &helper) {
  201. std::string pkg = str;
  202. size_t archfound = pkg.find_last_of(':');
  203. std::string arch;
  204. if (archfound != std::string::npos) {
  205. arch = pkg.substr(archfound+1);
  206. pkg.erase(archfound);
  207. }
  208. if (Cache.GetPkgCache() == 0)
  209. return pkgCache::PkgIterator(Cache, 0);
  210. pkgCache::PkgIterator Pkg(Cache, 0);
  211. if (arch.empty() == true) {
  212. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  213. if (Grp.end() == false)
  214. Pkg = Grp.FindPreferredPkg();
  215. } else
  216. Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
  217. if (Pkg.end() == true)
  218. return helper.canNotFindPkgName(Cache, str);
  219. return Pkg;
  220. }
  221. /*}}}*/
  222. // FromGroup - Returns the package defined by this string /*{{{*/
  223. bool PackageContainerInterface::FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache,
  224. std::string pkg, CacheSetHelper &helper) {
  225. if (unlikely(Cache.GetPkgCache() == 0))
  226. return false;
  227. size_t const archfound = pkg.find_last_of(':');
  228. std::string arch;
  229. if (archfound != std::string::npos) {
  230. arch = pkg.substr(archfound+1);
  231. pkg.erase(archfound);
  232. if (arch == "all" || arch == "native")
  233. arch = _config->Find("APT::Architecture");
  234. }
  235. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  236. if (Grp.end() == false) {
  237. if (arch.empty() == true) {
  238. pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
  239. if (Pkg.end() == false)
  240. {
  241. pci->insert(Pkg);
  242. return true;
  243. }
  244. } else {
  245. bool found = false;
  246. // for 'linux-any' return the first package matching, for 'linux-*' return all matches
  247. bool const isGlobal = arch.find('*') != std::string::npos;
  248. APT::CacheFilter::PackageArchitectureMatchesSpecification pams(arch);
  249. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg)) {
  250. if (pams(Pkg) == false)
  251. continue;
  252. pci->insert(Pkg);
  253. found = true;
  254. if (isGlobal == false)
  255. break;
  256. }
  257. if (found == true)
  258. return true;
  259. }
  260. }
  261. pkgCache::PkgIterator Pkg = helper.canNotFindPkgName(Cache, pkg);
  262. if (Pkg.end() == true)
  263. return false;
  264. pci->insert(Pkg);
  265. return true;
  266. }
  267. /*}}}*/
  268. // FromString - Return all packages matching a specific string /*{{{*/
  269. bool PackageContainerInterface::FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
  270. bool found = true;
  271. _error->PushToStack();
  272. if (FromGroup(pci, Cache, str, helper) == false &&
  273. FromTask(pci, Cache, str, helper) == false &&
  274. // FIXME: hm, hm, regexp/fnmatch incompatible?
  275. FromFnmatch(pci, Cache, str, helper) == false &&
  276. FromRegEx(pci, Cache, str, helper) == false)
  277. {
  278. helper.canNotFindPackage(CacheSetHelper::PACKAGENAME, pci, Cache, str);
  279. found = false;
  280. }
  281. if (found == true)
  282. _error->RevertToStack();
  283. else
  284. _error->MergeWithStack();
  285. return found;
  286. }
  287. /*}}}*/
  288. // FromCommandLine - Return all packages specified on commandline /*{{{*/
  289. bool PackageContainerInterface::FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  290. bool found = false;
  291. for (const char **I = cmdline; *I != 0; ++I)
  292. found |= PackageContainerInterface::FromString(pci, Cache, *I, helper);
  293. return found;
  294. }
  295. /*}}}*/
  296. // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
  297. bool PackageContainerInterface::FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
  298. pkgCacheFile &Cache, const char * cmdline,
  299. std::list<Modifier> const &mods, CacheSetHelper &helper) {
  300. std::string str = cmdline;
  301. unsigned short fallback = modID;
  302. bool modifierPresent = false;
  303. for (std::list<Modifier>::const_iterator mod = mods.begin();
  304. mod != mods.end(); ++mod) {
  305. size_t const alength = strlen(mod->Alias);
  306. switch(mod->Pos) {
  307. case Modifier::POSTFIX:
  308. if (str.compare(str.length() - alength, alength,
  309. mod->Alias, 0, alength) != 0)
  310. continue;
  311. str.erase(str.length() - alength);
  312. modID = mod->ID;
  313. break;
  314. case Modifier::PREFIX:
  315. continue;
  316. case Modifier::NONE:
  317. continue;
  318. }
  319. modifierPresent = true;
  320. break;
  321. }
  322. if (modifierPresent == true) {
  323. bool const errors = helper.showErrors(false);
  324. pkgCache::PkgIterator Pkg = FromName(Cache, cmdline, helper);
  325. helper.showErrors(errors);
  326. if (Pkg.end() == false) {
  327. pci->insert(Pkg);
  328. modID = fallback;
  329. return true;
  330. }
  331. }
  332. return FromString(pci, Cache, str, helper);
  333. }
  334. /*}}}*/
  335. // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
  336. bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
  337. VersionContainerInterface * const vci,
  338. pkgCacheFile &Cache, const char * cmdline,
  339. std::list<Modifier> const &mods,
  340. CacheSetHelper &helper) {
  341. CacheSetHelper::VerSelector select = CacheSetHelper::NEWEST;
  342. std::string str = cmdline;
  343. if (unlikely(str.empty() == true))
  344. return false;
  345. bool modifierPresent = false;
  346. unsigned short fallback = modID;
  347. for (std::list<Modifier>::const_iterator mod = mods.begin();
  348. mod != mods.end(); ++mod) {
  349. if (modID == fallback && mod->ID == fallback)
  350. select = mod->SelectVersion;
  351. size_t const alength = strlen(mod->Alias);
  352. switch(mod->Pos) {
  353. case Modifier::POSTFIX:
  354. if (str.length() <= alength ||
  355. str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
  356. continue;
  357. str.erase(str.length() - alength);
  358. modID = mod->ID;
  359. select = mod->SelectVersion;
  360. break;
  361. case Modifier::PREFIX:
  362. continue;
  363. case Modifier::NONE:
  364. continue;
  365. }
  366. modifierPresent = true;
  367. break;
  368. }
  369. if (modifierPresent == true) {
  370. bool const errors = helper.showErrors(false);
  371. bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
  372. helper.showErrors(errors);
  373. if (found == true) {
  374. modID = fallback;
  375. return true;
  376. }
  377. }
  378. return FromString(vci, Cache, str, select, helper);
  379. }
  380. /*}}}*/
  381. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  382. bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
  383. pkgCacheFile &Cache, const char **cmdline,
  384. CacheSetHelper::VerSelector const fallback,
  385. CacheSetHelper &helper) {
  386. bool found = false;
  387. for (const char **I = cmdline; *I != 0; ++I)
  388. found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
  389. return found;
  390. }
  391. /*}}}*/
  392. // FromString - Returns all versions spedcified by a string /*{{{*/
  393. bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
  394. pkgCacheFile &Cache, std::string pkg,
  395. CacheSetHelper::VerSelector const fallback,
  396. CacheSetHelper &helper,
  397. bool const onlyFromName) {
  398. PackageSet pkgset;
  399. if(FileExists(pkg))
  400. {
  401. PackageContainerInterface::FromString(&pkgset, Cache, pkg, helper);
  402. if(pkgset.size() == 0)
  403. return false;
  404. return VersionContainerInterface::FromPackage(vci, Cache, pkgset.begin(), fallback, helper);
  405. }
  406. std::string ver;
  407. bool verIsRel = false;
  408. size_t const vertag = pkg.find_last_of("/=");
  409. if (vertag != std::string::npos) {
  410. ver = pkg.substr(vertag+1);
  411. verIsRel = (pkg[vertag] == '/');
  412. pkg.erase(vertag);
  413. }
  414. if (onlyFromName == false)
  415. PackageContainerInterface::FromString(&pkgset, Cache, pkg, helper);
  416. else {
  417. pkgset.insert(PackageContainerInterface::FromName(Cache, pkg, helper));
  418. }
  419. bool errors = true;
  420. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  421. errors = helper.showErrors(false);
  422. bool found = false;
  423. for (PackageSet::const_iterator P = pkgset.begin();
  424. P != pkgset.end(); ++P) {
  425. if (vertag == std::string::npos) {
  426. found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
  427. continue;
  428. }
  429. pkgCache::VerIterator V;
  430. if (ver == "installed")
  431. V = getInstalledVer(Cache, P, helper);
  432. else if (ver == "candidate")
  433. V = getCandidateVer(Cache, P, helper);
  434. else if (ver == "newest") {
  435. if (P->VersionList != 0)
  436. V = P.VersionList();
  437. else
  438. V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
  439. } else {
  440. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  441. pkgVersionMatch::Version));
  442. V = Match.Find(P);
  443. if (V.end() == true) {
  444. if (verIsRel == true)
  445. _error->Error(_("Release '%s' for '%s' was not found"),
  446. ver.c_str(), P.FullName(true).c_str());
  447. else
  448. _error->Error(_("Version '%s' for '%s' was not found"),
  449. ver.c_str(), P.FullName(true).c_str());
  450. continue;
  451. }
  452. }
  453. if (V.end() == true)
  454. continue;
  455. if (verIsRel == true)
  456. helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
  457. else
  458. helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
  459. vci->insert(V);
  460. found = true;
  461. }
  462. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  463. helper.showErrors(errors);
  464. return found;
  465. }
  466. /*}}}*/
  467. // FromPackage - versions from package based on fallback /*{{{*/
  468. bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
  469. pkgCacheFile &Cache,
  470. pkgCache::PkgIterator const &P,
  471. CacheSetHelper::VerSelector const fallback,
  472. CacheSetHelper &helper) {
  473. pkgCache::VerIterator V;
  474. bool showErrors;
  475. bool found = false;
  476. switch(fallback) {
  477. case CacheSetHelper::ALL:
  478. if (P->VersionList != 0)
  479. for (V = P.VersionList(); V.end() != true; ++V)
  480. found |= vci->insert(V);
  481. else
  482. helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
  483. break;
  484. case CacheSetHelper::CANDANDINST:
  485. found |= vci->insert(getInstalledVer(Cache, P, helper));
  486. found |= vci->insert(getCandidateVer(Cache, P, helper));
  487. break;
  488. case CacheSetHelper::CANDIDATE:
  489. found |= vci->insert(getCandidateVer(Cache, P, helper));
  490. break;
  491. case CacheSetHelper::INSTALLED:
  492. found |= vci->insert(getInstalledVer(Cache, P, helper));
  493. break;
  494. case CacheSetHelper::CANDINST:
  495. showErrors = helper.showErrors(false);
  496. V = getCandidateVer(Cache, P, helper);
  497. if (V.end() == true)
  498. V = getInstalledVer(Cache, P, helper);
  499. helper.showErrors(showErrors);
  500. if (V.end() == false)
  501. found |= vci->insert(V);
  502. else
  503. helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
  504. break;
  505. case CacheSetHelper::INSTCAND:
  506. showErrors = helper.showErrors(false);
  507. V = getInstalledVer(Cache, P, helper);
  508. if (V.end() == true)
  509. V = getCandidateVer(Cache, P, helper);
  510. helper.showErrors(showErrors);
  511. if (V.end() == false)
  512. found |= vci->insert(V);
  513. else
  514. helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
  515. break;
  516. case CacheSetHelper::NEWEST:
  517. if (P->VersionList != 0)
  518. found |= vci->insert(P.VersionList());
  519. else
  520. helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
  521. break;
  522. case CacheSetHelper::RELEASE:
  523. case CacheSetHelper::VERSIONNUMBER:
  524. // both make no sense here, so always false
  525. return false;
  526. }
  527. return found;
  528. }
  529. /*}}}*/
  530. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  531. pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
  532. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  533. pkgCache::VerIterator Cand;
  534. if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false) {
  535. if (unlikely(Cache.GetPolicy() == 0))
  536. return pkgCache::VerIterator(Cache);
  537. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  538. } else {
  539. Cand = Cache[Pkg].CandidateVerIter(Cache);
  540. }
  541. if (Cand.end() == true)
  542. return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
  543. return Cand;
  544. }
  545. /*}}}*/
  546. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  547. pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
  548. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  549. if (Pkg->CurrentVer == 0)
  550. return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
  551. return Pkg.CurrentVer();
  552. }
  553. /*}}}*/
  554. // canNotFindPackage - with the given selector and pattern /*{{{*/
  555. void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
  556. PackageContainerInterface * const pci, pkgCacheFile &Cache,
  557. std::string const &pattern) {
  558. switch (select) {
  559. #if __GNUC__ >= 4
  560. #pragma GCC diagnostic push
  561. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  562. #endif
  563. case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
  564. case TASK: canNotFindTask(pci, Cache, pattern); break;
  565. case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
  566. case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
  567. case UNKNOWN: break;
  568. #if __GNUC__ >= 4
  569. #pragma GCC diagnostic pop
  570. #endif
  571. }
  572. }
  573. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  574. void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  575. if (ShowError == true)
  576. _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
  577. }
  578. /*}}}*/
  579. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  580. void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  581. if (ShowError == true)
  582. _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
  583. }
  584. /*}}}*/
  585. // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
  586. void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  587. if (ShowError == true)
  588. _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
  589. }
  590. /*}}}*/
  591. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  592. APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
  593. }
  594. /*}}}*/
  595. /*}}}*/
  596. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  597. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  598. std::string const &str) {
  599. if (ShowError == true)
  600. _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
  601. return pkgCache::PkgIterator(Cache, 0);
  602. }
  603. /*}}}*/
  604. // canNotFindVersion - for package by selector /*{{{*/
  605. void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
  606. {
  607. switch (select) {
  608. #if __GNUC__ >= 4
  609. #pragma GCC diagnostic push
  610. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  611. #endif
  612. case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
  613. case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
  614. case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
  615. case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
  616. case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
  617. case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
  618. #if __GNUC__ >= 4
  619. #pragma GCC diagnostic pop
  620. #endif
  621. case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
  622. case RELEASE:
  623. case VERSIONNUMBER:
  624. // invalid in this branch
  625. break;
  626. }
  627. }
  628. // canNotFindAllVer /*{{{*/
  629. void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
  630. pkgCache::PkgIterator const &Pkg) {
  631. if (ShowError == true)
  632. _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  633. }
  634. /*}}}*/
  635. // canNotFindInstCandVer /*{{{*/
  636. void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  637. pkgCache::PkgIterator const &Pkg) {
  638. canNotGetInstCandVer(Cache, Pkg);
  639. }
  640. /*}}}*/
  641. // canNotFindInstCandVer /*{{{*/
  642. void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  643. pkgCache::PkgIterator const &Pkg) {
  644. canNotGetCandInstVer(Cache, Pkg);
  645. }
  646. /*}}}*/
  647. /*}}}*/
  648. // canNotGetVersion - for package by selector /*{{{*/
  649. pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  650. switch (select) {
  651. #if __GNUC__ >= 4
  652. #pragma GCC diagnostic push
  653. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  654. #endif
  655. case NEWEST: return canNotFindNewestVer(Cache, Pkg);
  656. case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
  657. case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
  658. #if __GNUC__ >= 4
  659. #pragma GCC diagnostic pop
  660. #endif
  661. case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
  662. case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
  663. case ALL:
  664. case CANDANDINST:
  665. case RELEASE:
  666. case VERSIONNUMBER:
  667. // invalid in this branch
  668. return pkgCache::VerIterator(Cache, 0);
  669. }
  670. return pkgCache::VerIterator(Cache, 0);
  671. }
  672. // canNotFindNewestVer /*{{{*/
  673. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  674. pkgCache::PkgIterator const &Pkg) {
  675. if (ShowError == true)
  676. _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  677. return pkgCache::VerIterator(Cache, 0);
  678. }
  679. /*}}}*/
  680. // canNotFindCandidateVer /*{{{*/
  681. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  682. pkgCache::PkgIterator const &Pkg) {
  683. if (ShowError == true)
  684. _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  685. return pkgCache::VerIterator(Cache, 0);
  686. }
  687. /*}}}*/
  688. // canNotFindInstalledVer /*{{{*/
  689. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  690. pkgCache::PkgIterator const &Pkg) {
  691. if (ShowError == true)
  692. _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  693. return pkgCache::VerIterator(Cache, 0);
  694. }
  695. /*}}}*/
  696. // canNotFindInstCandVer /*{{{*/
  697. pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
  698. pkgCache::PkgIterator const &Pkg) {
  699. if (ShowError == true)
  700. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  701. return pkgCache::VerIterator(Cache, 0);
  702. }
  703. /*}}}*/
  704. // canNotFindInstCandVer /*{{{*/
  705. pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
  706. pkgCache::PkgIterator const &Pkg) {
  707. if (ShowError == true)
  708. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  709. return pkgCache::VerIterator(Cache, 0);
  710. }
  711. /*}}}*/
  712. /*}}}*/
  713. // showPackageSelection - by selector and given pattern /*{{{*/
  714. APT_CONST void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
  715. std::string const &pattern) {
  716. switch (select) {
  717. #if __GNUC__ >= 4
  718. #pragma GCC diagnostic push
  719. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  720. #endif
  721. case REGEX: showRegExSelection(pkg, pattern); break;
  722. case TASK: showTaskSelection(pkg, pattern); break;
  723. case FNMATCH: showFnmatchSelection(pkg, pattern); break;
  724. #if __GNUC__ >= 4
  725. #pragma GCC diagnostic pop
  726. #endif
  727. case PACKAGENAME: /* no suprises here */ break;
  728. case UNKNOWN: break;
  729. }
  730. }
  731. // showTaskSelection /*{{{*/
  732. APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
  733. std::string const &/*pattern*/) {
  734. }
  735. /*}}}*/
  736. // showRegExSelection /*{{{*/
  737. APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
  738. std::string const &/*pattern*/) {
  739. }
  740. /*}}}*/
  741. // showFnmatchSelection /*{{{*/
  742. APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
  743. std::string const &/*pattern*/) {
  744. }
  745. /*}}}*/
  746. /*}}}*/
  747. // showVersionSelection /*{{{*/
  748. APT_CONST void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
  749. pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
  750. switch (select) {
  751. #if __GNUC__ >= 4
  752. #pragma GCC diagnostic push
  753. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  754. #endif
  755. case RELEASE:
  756. showSelectedVersion(Pkg, Ver, pattern, true);
  757. break;
  758. case VERSIONNUMBER:
  759. showSelectedVersion(Pkg, Ver, pattern, false);
  760. break;
  761. #if __GNUC__ >= 4
  762. #pragma GCC diagnostic pop
  763. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  764. #endif
  765. case NEWEST:
  766. case CANDIDATE:
  767. case INSTALLED:
  768. case CANDINST:
  769. case INSTCAND:
  770. case ALL:
  771. case CANDANDINST:
  772. // not really suprises, but in fact: just not implemented
  773. break;
  774. }
  775. }
  776. APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
  777. pkgCache::VerIterator const /*Ver*/,
  778. std::string const &/*ver*/,
  779. bool const /*verIsRel*/) {
  780. }
  781. /*}}}*/
  782. }