cacheset.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. size_t const archfound = pkg.find_last_of(':');
  231. std::string arch;
  232. if (archfound != std::string::npos) {
  233. arch = pkg.substr(archfound+1);
  234. pkg.erase(archfound);
  235. if (arch == "all" || arch == "native")
  236. arch = _config->Find("APT::Architecture");
  237. }
  238. pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
  239. if (Grp.end() == false) {
  240. if (arch.empty() == true) {
  241. pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
  242. if (Pkg.end() == false)
  243. {
  244. pci->insert(Pkg);
  245. return true;
  246. }
  247. } else {
  248. bool found = false;
  249. // for 'linux-any' return the first package matching, for 'linux-*' return all matches
  250. bool const isGlobal = arch.find('*') != std::string::npos;
  251. APT::CacheFilter::PackageArchitectureMatchesSpecification pams(arch);
  252. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg)) {
  253. if (pams(Pkg) == false)
  254. continue;
  255. pci->insert(Pkg);
  256. found = true;
  257. if (isGlobal == false)
  258. break;
  259. }
  260. if (found == true)
  261. return true;
  262. }
  263. }
  264. pkgCache::PkgIterator Pkg = canNotFindPkgName(Cache, pkg);
  265. if (Pkg.end() == true)
  266. return false;
  267. pci->insert(Pkg);
  268. return true;
  269. }
  270. /*}}}*/
  271. // PackageFromString - Return all packages matching a specific string /*{{{*/
  272. bool CacheSetHelper::PackageFromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str) {
  273. bool found = true;
  274. _error->PushToStack();
  275. if (PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str) == false &&
  276. PackageFrom(CacheSetHelper::TASK, pci, Cache, str) == false &&
  277. // FIXME: hm, hm, regexp/fnmatch incompatible?
  278. PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, str) == false &&
  279. PackageFrom(CacheSetHelper::REGEX, pci, Cache, str) == false)
  280. {
  281. canNotFindPackage(CacheSetHelper::PACKAGENAME, pci, Cache, str);
  282. found = false;
  283. }
  284. if (found == true)
  285. _error->RevertToStack();
  286. else
  287. _error->MergeWithStack();
  288. return found;
  289. }
  290. /*}}}*/
  291. // PackageFromCommandLine - Return all packages specified on commandline /*{{{*/
  292. bool CacheSetHelper::PackageFromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline) {
  293. bool found = false;
  294. for (const char **I = cmdline; *I != 0; ++I)
  295. found |= PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, *I);
  296. return found;
  297. }
  298. /*}}}*/
  299. // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
  300. bool CacheSetHelper::PackageFromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
  301. pkgCacheFile &Cache, const char * cmdline,
  302. std::list<PkgModifier> const &mods) {
  303. std::string str = cmdline;
  304. unsigned short fallback = modID;
  305. bool modifierPresent = false;
  306. for (std::list<PkgModifier>::const_iterator mod = mods.begin();
  307. mod != mods.end(); ++mod) {
  308. size_t const alength = strlen(mod->Alias);
  309. switch(mod->Pos) {
  310. case PkgModifier::POSTFIX:
  311. if (str.compare(str.length() - alength, alength,
  312. mod->Alias, 0, alength) != 0)
  313. continue;
  314. str.erase(str.length() - alength);
  315. modID = mod->ID;
  316. break;
  317. case PkgModifier::PREFIX:
  318. continue;
  319. case PkgModifier::NONE:
  320. continue;
  321. }
  322. modifierPresent = true;
  323. break;
  324. }
  325. if (modifierPresent == true) {
  326. bool const errors = showErrors(false);
  327. bool const found = PackageFrom(PACKAGENAME, pci, Cache, cmdline);
  328. showErrors(errors);
  329. if (found == true) {
  330. modID = fallback;
  331. return true;
  332. }
  333. }
  334. return PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str);
  335. }
  336. /*}}}*/
  337. // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
  338. bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
  339. VersionContainerInterface * const vci,
  340. pkgCacheFile &Cache, const char * cmdline,
  341. std::list<Modifier> const &mods,
  342. CacheSetHelper &helper) {
  343. CacheSetHelper::VerSelector select = CacheSetHelper::NEWEST;
  344. std::string str = cmdline;
  345. if (unlikely(str.empty() == true))
  346. return false;
  347. bool modifierPresent = false;
  348. unsigned short fallback = modID;
  349. for (std::list<Modifier>::const_iterator mod = mods.begin();
  350. mod != mods.end(); ++mod) {
  351. if (modID == fallback && mod->ID == fallback)
  352. select = mod->SelectVersion;
  353. size_t const alength = strlen(mod->Alias);
  354. switch(mod->Pos) {
  355. case Modifier::POSTFIX:
  356. if (str.length() <= alength ||
  357. str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
  358. continue;
  359. str.erase(str.length() - alength);
  360. modID = mod->ID;
  361. select = mod->SelectVersion;
  362. break;
  363. case Modifier::PREFIX:
  364. continue;
  365. case Modifier::NONE:
  366. continue;
  367. }
  368. modifierPresent = true;
  369. break;
  370. }
  371. if (modifierPresent == true) {
  372. bool const errors = helper.showErrors(false);
  373. bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
  374. helper.showErrors(errors);
  375. if (found == true) {
  376. modID = fallback;
  377. return true;
  378. }
  379. }
  380. return FromString(vci, Cache, str, select, helper);
  381. }
  382. /*}}}*/
  383. // FromCommandLine - Return all versions specified on commandline /*{{{*/
  384. bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
  385. pkgCacheFile &Cache, const char **cmdline,
  386. CacheSetHelper::VerSelector const fallback,
  387. CacheSetHelper &helper) {
  388. bool found = false;
  389. for (const char **I = cmdline; *I != 0; ++I)
  390. found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
  391. return found;
  392. }
  393. /*}}}*/
  394. // FromString - Returns all versions spedcified by a string /*{{{*/
  395. bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
  396. pkgCacheFile &Cache, std::string pkg,
  397. CacheSetHelper::VerSelector const fallback,
  398. CacheSetHelper &helper,
  399. bool const onlyFromName) {
  400. PackageSet pkgset;
  401. if(FileExists(pkg)) {
  402. helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
  403. if(pkgset.empty() == true)
  404. return false;
  405. return VersionContainerInterface::FromPackage(vci, Cache, pkgset.begin(), fallback, helper);
  406. }
  407. std::string ver;
  408. bool verIsRel = false;
  409. size_t const vertag = pkg.find_last_of("/=");
  410. if (vertag != std::string::npos) {
  411. ver = pkg.substr(vertag+1);
  412. verIsRel = (pkg[vertag] == '/');
  413. pkg.erase(vertag);
  414. }
  415. if (onlyFromName == false)
  416. helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
  417. else {
  418. helper.PackageFrom(CacheSetHelper::PACKAGENAME, &pkgset, Cache, pkg);
  419. }
  420. bool errors = true;
  421. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  422. errors = helper.showErrors(false);
  423. bool found = false;
  424. for (PackageSet::const_iterator P = pkgset.begin();
  425. P != pkgset.end(); ++P) {
  426. if (vertag == std::string::npos) {
  427. found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
  428. continue;
  429. }
  430. pkgCache::VerIterator V;
  431. if (ver == "installed")
  432. V = getInstalledVer(Cache, P, helper);
  433. else if (ver == "candidate")
  434. V = getCandidateVer(Cache, P, helper);
  435. else if (ver == "newest") {
  436. if (P->VersionList != 0)
  437. V = P.VersionList();
  438. else
  439. V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
  440. } else {
  441. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  442. pkgVersionMatch::Version));
  443. V = Match.Find(P);
  444. if (V.end() == true) {
  445. if (verIsRel == true)
  446. _error->Error(_("Release '%s' for '%s' was not found"),
  447. ver.c_str(), P.FullName(true).c_str());
  448. else
  449. _error->Error(_("Version '%s' for '%s' was not found"),
  450. ver.c_str(), P.FullName(true).c_str());
  451. continue;
  452. }
  453. }
  454. if (V.end() == true)
  455. continue;
  456. if (verIsRel == true)
  457. helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
  458. else
  459. helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
  460. vci->insert(V);
  461. found = true;
  462. }
  463. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  464. helper.showErrors(errors);
  465. return found;
  466. }
  467. /*}}}*/
  468. // FromPackage - versions from package based on fallback /*{{{*/
  469. bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
  470. pkgCacheFile &Cache,
  471. pkgCache::PkgIterator const &P,
  472. CacheSetHelper::VerSelector const fallback,
  473. CacheSetHelper &helper) {
  474. pkgCache::VerIterator V;
  475. bool showErrors;
  476. bool found = false;
  477. switch(fallback) {
  478. case CacheSetHelper::ALL:
  479. if (P->VersionList != 0)
  480. for (V = P.VersionList(); V.end() != true; ++V)
  481. found |= vci->insert(V);
  482. else
  483. helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
  484. break;
  485. case CacheSetHelper::CANDANDINST:
  486. found |= vci->insert(getInstalledVer(Cache, P, helper));
  487. found |= vci->insert(getCandidateVer(Cache, P, helper));
  488. break;
  489. case CacheSetHelper::CANDIDATE:
  490. found |= vci->insert(getCandidateVer(Cache, P, helper));
  491. break;
  492. case CacheSetHelper::INSTALLED:
  493. found |= vci->insert(getInstalledVer(Cache, P, helper));
  494. break;
  495. case CacheSetHelper::CANDINST:
  496. showErrors = helper.showErrors(false);
  497. V = getCandidateVer(Cache, P, helper);
  498. if (V.end() == true)
  499. V = getInstalledVer(Cache, P, helper);
  500. helper.showErrors(showErrors);
  501. if (V.end() == false)
  502. found |= vci->insert(V);
  503. else
  504. helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
  505. break;
  506. case CacheSetHelper::INSTCAND:
  507. showErrors = helper.showErrors(false);
  508. V = getInstalledVer(Cache, P, helper);
  509. if (V.end() == true)
  510. V = getCandidateVer(Cache, P, helper);
  511. helper.showErrors(showErrors);
  512. if (V.end() == false)
  513. found |= vci->insert(V);
  514. else
  515. helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
  516. break;
  517. case CacheSetHelper::NEWEST:
  518. if (P->VersionList != 0)
  519. found |= vci->insert(P.VersionList());
  520. else
  521. helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
  522. break;
  523. case CacheSetHelper::RELEASE:
  524. case CacheSetHelper::VERSIONNUMBER:
  525. // both make no sense here, so always false
  526. return false;
  527. }
  528. return found;
  529. }
  530. /*}}}*/
  531. // FromDependency - versions satisfying a given dependency /*{{{*/
  532. bool VersionContainerInterface::FromDependency(VersionContainerInterface * const vci,
  533. pkgCacheFile &Cache,
  534. pkgCache::DepIterator const &D,
  535. CacheSetHelper::VerSelector const selector,
  536. CacheSetHelper &helper)
  537. {
  538. bool found = false;
  539. switch(selector) {
  540. case CacheSetHelper::ALL:
  541. {
  542. pkgCache::PkgIterator const T = D.TargetPkg();
  543. for (pkgCache::VerIterator Ver = T.VersionList(); Ver.end() == false; ++Ver)
  544. {
  545. if (D.IsSatisfied(Ver) == true)
  546. {
  547. vci->insert(Ver);
  548. found = true;
  549. }
  550. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  551. {
  552. pkgCache::VerIterator const V = Prv.OwnerVer();
  553. if (unlikely(V.end() == true) || D.IsSatisfied(Prv) == false)
  554. continue;
  555. vci->insert(V);
  556. found = true;
  557. }
  558. }
  559. return found;
  560. }
  561. case CacheSetHelper::CANDANDINST:
  562. {
  563. found = FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
  564. found &= FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
  565. return found;
  566. }
  567. case CacheSetHelper::CANDIDATE:
  568. {
  569. pkgCache::PkgIterator const T = D.TargetPkg();
  570. pkgCache::VerIterator const Cand = Cache[T].CandidateVerIter(Cache);
  571. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  572. {
  573. vci->insert(Cand);
  574. found = true;
  575. }
  576. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  577. {
  578. pkgCache::VerIterator const V = Prv.OwnerVer();
  579. pkgCache::VerIterator const Cand = Cache[Prv.OwnerPkg()].CandidateVerIter(Cache);
  580. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  581. continue;
  582. vci->insert(Cand);
  583. found = true;
  584. }
  585. return found;
  586. }
  587. case CacheSetHelper::INSTALLED:
  588. {
  589. pkgCache::PkgIterator const T = D.TargetPkg();
  590. pkgCache::VerIterator const Cand = T.CurrentVer();
  591. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  592. {
  593. vci->insert(Cand);
  594. found = true;
  595. }
  596. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  597. {
  598. pkgCache::VerIterator const V = Prv.OwnerVer();
  599. pkgCache::VerIterator const Cand = Prv.OwnerPkg().CurrentVer();
  600. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  601. continue;
  602. vci->insert(Cand);
  603. found = true;
  604. }
  605. return found;
  606. }
  607. case CacheSetHelper::CANDINST:
  608. return FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper) ||
  609. FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
  610. case CacheSetHelper::INSTCAND:
  611. return FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper) ||
  612. FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
  613. case CacheSetHelper::NEWEST:
  614. {
  615. pkgCache::PkgIterator const T = D.TargetPkg();
  616. pkgCache::VerIterator const Cand = T.VersionList();
  617. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  618. {
  619. vci->insert(Cand);
  620. found = true;
  621. }
  622. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  623. {
  624. pkgCache::VerIterator const V = Prv.OwnerVer();
  625. pkgCache::VerIterator const Cand = Prv.OwnerPkg().VersionList();
  626. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  627. continue;
  628. vci->insert(Cand);
  629. found = true;
  630. }
  631. return found;
  632. }
  633. case CacheSetHelper::RELEASE:
  634. case CacheSetHelper::VERSIONNUMBER:
  635. // both make no sense here, so always false
  636. return false;
  637. }
  638. return found;
  639. }
  640. /*}}}*/
  641. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  642. pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
  643. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  644. pkgCache::VerIterator Cand;
  645. if (Cache.IsDepCacheBuilt() == true) {
  646. Cand = Cache[Pkg].CandidateVerIter(Cache);
  647. } else if (unlikely(Cache.GetPolicy() == nullptr)) {
  648. return pkgCache::VerIterator(Cache);
  649. } else {
  650. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  651. }
  652. if (Cand.end() == true)
  653. return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
  654. return Cand;
  655. }
  656. /*}}}*/
  657. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  658. pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
  659. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  660. if (Pkg->CurrentVer == 0)
  661. return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
  662. return Pkg.CurrentVer();
  663. }
  664. /*}}}*/
  665. // canNotFindPackage - with the given selector and pattern /*{{{*/
  666. void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
  667. PackageContainerInterface * const pci, pkgCacheFile &Cache,
  668. std::string const &pattern) {
  669. switch (select) {
  670. APT_IGNORE_DEPRECATED_PUSH
  671. case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
  672. case TASK: canNotFindTask(pci, Cache, pattern); break;
  673. case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
  674. case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
  675. case STRING: canNotFindPackage(pci, Cache, pattern); break;
  676. case UNKNOWN: break;
  677. APT_IGNORE_DEPRECATED_POP
  678. }
  679. }
  680. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  681. void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  682. if (ShowError == true)
  683. _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
  684. }
  685. /*}}}*/
  686. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  687. void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  688. if (ShowError == true)
  689. _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
  690. }
  691. /*}}}*/
  692. // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
  693. void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  694. if (ShowError == true)
  695. _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
  696. }
  697. /*}}}*/
  698. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  699. APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
  700. }
  701. /*}}}*/
  702. /*}}}*/
  703. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  704. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  705. std::string const &str) {
  706. if (ShowError == true)
  707. _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
  708. return pkgCache::PkgIterator(Cache, 0);
  709. }
  710. /*}}}*/
  711. // canNotFindVersion - for package by selector /*{{{*/
  712. void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
  713. {
  714. switch (select) {
  715. APT_IGNORE_DEPRECATED_PUSH
  716. case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
  717. case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
  718. case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
  719. case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
  720. case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
  721. case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
  722. APT_IGNORE_DEPRECATED_POP
  723. case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
  724. case RELEASE:
  725. case VERSIONNUMBER:
  726. // invalid in this branch
  727. break;
  728. }
  729. }
  730. // canNotFindAllVer /*{{{*/
  731. void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
  732. pkgCache::PkgIterator const &Pkg) {
  733. if (ShowError == true)
  734. _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  735. }
  736. /*}}}*/
  737. // canNotFindInstCandVer /*{{{*/
  738. void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  739. pkgCache::PkgIterator const &Pkg) {
  740. canNotGetInstCandVer(Cache, Pkg);
  741. }
  742. /*}}}*/
  743. // canNotFindInstCandVer /*{{{*/
  744. void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  745. pkgCache::PkgIterator const &Pkg) {
  746. canNotGetCandInstVer(Cache, Pkg);
  747. }
  748. /*}}}*/
  749. /*}}}*/
  750. // canNotGetVersion - for package by selector /*{{{*/
  751. pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  752. switch (select) {
  753. APT_IGNORE_DEPRECATED_PUSH
  754. case NEWEST: return canNotFindNewestVer(Cache, Pkg);
  755. case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
  756. case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
  757. APT_IGNORE_DEPRECATED_POP
  758. case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
  759. case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
  760. case ALL:
  761. case CANDANDINST:
  762. case RELEASE:
  763. case VERSIONNUMBER:
  764. // invalid in this branch
  765. return pkgCache::VerIterator(Cache, 0);
  766. }
  767. return pkgCache::VerIterator(Cache, 0);
  768. }
  769. // canNotFindNewestVer /*{{{*/
  770. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  771. pkgCache::PkgIterator const &Pkg) {
  772. if (ShowError == true)
  773. _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  774. return pkgCache::VerIterator(Cache, 0);
  775. }
  776. /*}}}*/
  777. // canNotFindCandidateVer /*{{{*/
  778. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  779. pkgCache::PkgIterator const &Pkg) {
  780. if (ShowError == true)
  781. _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  782. return pkgCache::VerIterator(Cache, 0);
  783. }
  784. /*}}}*/
  785. // canNotFindInstalledVer /*{{{*/
  786. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  787. pkgCache::PkgIterator const &Pkg) {
  788. if (ShowError == true)
  789. _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  790. return pkgCache::VerIterator(Cache, 0);
  791. }
  792. /*}}}*/
  793. // canNotFindInstCandVer /*{{{*/
  794. pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
  795. pkgCache::PkgIterator const &Pkg) {
  796. if (ShowError == true)
  797. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  798. return pkgCache::VerIterator(Cache, 0);
  799. }
  800. /*}}}*/
  801. // canNotFindInstCandVer /*{{{*/
  802. pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
  803. pkgCache::PkgIterator const &Pkg) {
  804. if (ShowError == true)
  805. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  806. return pkgCache::VerIterator(Cache, 0);
  807. }
  808. /*}}}*/
  809. /*}}}*/
  810. // showPackageSelection - by selector and given pattern /*{{{*/
  811. void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
  812. std::string const &pattern) {
  813. switch (select) {
  814. APT_IGNORE_DEPRECATED_PUSH
  815. case REGEX: showRegExSelection(pkg, pattern); break;
  816. case TASK: showTaskSelection(pkg, pattern); break;
  817. case FNMATCH: showFnmatchSelection(pkg, pattern); break;
  818. APT_IGNORE_DEPRECATED_POP
  819. case PACKAGENAME: /* no suprises here */ break;
  820. case STRING: /* handled by the special cases */ break;
  821. case UNKNOWN: break;
  822. }
  823. }
  824. // showTaskSelection /*{{{*/
  825. APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
  826. std::string const &/*pattern*/) {
  827. }
  828. /*}}}*/
  829. // showRegExSelection /*{{{*/
  830. APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
  831. std::string const &/*pattern*/) {
  832. }
  833. /*}}}*/
  834. // showFnmatchSelection /*{{{*/
  835. APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
  836. std::string const &/*pattern*/) {
  837. }
  838. /*}}}*/
  839. /*}}}*/
  840. // showVersionSelection /*{{{*/
  841. void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
  842. pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
  843. switch (select) {
  844. APT_IGNORE_DEPRECATED_PUSH
  845. case RELEASE:
  846. showSelectedVersion(Pkg, Ver, pattern, true);
  847. break;
  848. case VERSIONNUMBER:
  849. showSelectedVersion(Pkg, Ver, pattern, false);
  850. break;
  851. APT_IGNORE_DEPRECATED_POP
  852. case NEWEST:
  853. case CANDIDATE:
  854. case INSTALLED:
  855. case CANDINST:
  856. case INSTCAND:
  857. case ALL:
  858. case CANDANDINST:
  859. // not really suprises, but in fact: just not implemented
  860. break;
  861. }
  862. }
  863. APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
  864. pkgCache::VerIterator const /*Ver*/,
  865. std::string const &/*ver*/,
  866. bool const /*verIsRel*/) {
  867. }
  868. /*}}}*/
  869. CacheSetHelper::CacheSetHelper(bool const ShowError, GlobalError::MsgType ErrorType) :
  870. ShowError(ShowError), ErrorType(ErrorType), d(NULL) {}
  871. CacheSetHelper::~CacheSetHelper() {}
  872. PackageContainerInterface::PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN), d(NULL) {}
  873. PackageContainerInterface::PackageContainerInterface(CacheSetHelper::PkgSelector const by) : ConstructedBy(by), d(NULL) {}
  874. PackageContainerInterface& PackageContainerInterface::operator=(PackageContainerInterface const &other) {
  875. if (this != &other)
  876. this->ConstructedBy = other.ConstructedBy;
  877. return *this;
  878. }
  879. PackageContainerInterface::~PackageContainerInterface() {}
  880. PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {}
  881. PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {}
  882. PackageUniverse::~PackageUniverse() {}
  883. VersionContainerInterface::VersionContainerInterface() : d(NULL) {}
  884. VersionContainerInterface& VersionContainerInterface::operator=(VersionContainerInterface const &) {
  885. return *this;
  886. }
  887. VersionContainerInterface::~VersionContainerInterface() {}
  888. }