cacheset.cc 31 KB

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