cacheset.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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::PACKAGENAME, 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. PackageSet pkgset;
  402. if(FileExists(pkg)) {
  403. helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
  404. if(pkgset.empty() == true)
  405. return false;
  406. return VersionContainerInterface::FromPackage(vci, Cache, pkgset.begin(), fallback, helper);
  407. }
  408. std::string ver;
  409. bool verIsRel = false;
  410. size_t const vertag = pkg.find_last_of("/=");
  411. if (vertag != std::string::npos) {
  412. ver = pkg.substr(vertag+1);
  413. verIsRel = (pkg[vertag] == '/');
  414. pkg.erase(vertag);
  415. }
  416. if (onlyFromName == false)
  417. helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
  418. else {
  419. helper.PackageFrom(CacheSetHelper::PACKAGENAME, &pkgset, Cache, pkg);
  420. }
  421. bool errors = true;
  422. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  423. errors = helper.showErrors(false);
  424. bool found = false;
  425. for (PackageSet::const_iterator P = pkgset.begin();
  426. P != pkgset.end(); ++P) {
  427. if (vertag == std::string::npos) {
  428. found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
  429. continue;
  430. }
  431. pkgCache::VerIterator V;
  432. if (ver == "installed")
  433. V = getInstalledVer(Cache, P, helper);
  434. else if (ver == "candidate")
  435. V = getCandidateVer(Cache, P, helper);
  436. else if (ver == "newest") {
  437. if (P->VersionList != 0)
  438. V = P.VersionList();
  439. else
  440. V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
  441. } else {
  442. pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
  443. pkgVersionMatch::Version));
  444. V = Match.Find(P);
  445. if (V.end() == true) {
  446. if (verIsRel == true)
  447. _error->Error(_("Release '%s' for '%s' was not found"),
  448. ver.c_str(), P.FullName(true).c_str());
  449. else
  450. _error->Error(_("Version '%s' for '%s' was not found"),
  451. ver.c_str(), P.FullName(true).c_str());
  452. continue;
  453. }
  454. }
  455. if (V.end() == true)
  456. continue;
  457. if (verIsRel == true)
  458. helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
  459. else
  460. helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
  461. vci->insert(V);
  462. found = true;
  463. }
  464. if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
  465. helper.showErrors(errors);
  466. return found;
  467. }
  468. /*}}}*/
  469. // FromPackage - versions from package based on fallback /*{{{*/
  470. bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
  471. pkgCacheFile &Cache,
  472. pkgCache::PkgIterator const &P,
  473. CacheSetHelper::VerSelector const fallback,
  474. CacheSetHelper &helper) {
  475. pkgCache::VerIterator V;
  476. bool showErrors;
  477. bool found = false;
  478. switch(fallback) {
  479. case CacheSetHelper::ALL:
  480. if (P->VersionList != 0)
  481. for (V = P.VersionList(); V.end() != true; ++V)
  482. found |= vci->insert(V);
  483. else
  484. helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
  485. break;
  486. case CacheSetHelper::CANDANDINST:
  487. found |= vci->insert(getInstalledVer(Cache, P, helper));
  488. found |= vci->insert(getCandidateVer(Cache, P, helper));
  489. break;
  490. case CacheSetHelper::CANDIDATE:
  491. found |= vci->insert(getCandidateVer(Cache, P, helper));
  492. break;
  493. case CacheSetHelper::INSTALLED:
  494. found |= vci->insert(getInstalledVer(Cache, P, helper));
  495. break;
  496. case CacheSetHelper::CANDINST:
  497. showErrors = helper.showErrors(false);
  498. V = getCandidateVer(Cache, P, helper);
  499. if (V.end() == true)
  500. V = getInstalledVer(Cache, P, helper);
  501. helper.showErrors(showErrors);
  502. if (V.end() == false)
  503. found |= vci->insert(V);
  504. else
  505. helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
  506. break;
  507. case CacheSetHelper::INSTCAND:
  508. showErrors = helper.showErrors(false);
  509. V = getInstalledVer(Cache, P, helper);
  510. if (V.end() == true)
  511. V = getCandidateVer(Cache, P, helper);
  512. helper.showErrors(showErrors);
  513. if (V.end() == false)
  514. found |= vci->insert(V);
  515. else
  516. helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
  517. break;
  518. case CacheSetHelper::NEWEST:
  519. if (P->VersionList != 0)
  520. found |= vci->insert(P.VersionList());
  521. else
  522. helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
  523. break;
  524. case CacheSetHelper::RELEASE:
  525. case CacheSetHelper::VERSIONNUMBER:
  526. // both make no sense here, so always false
  527. return false;
  528. }
  529. return found;
  530. }
  531. /*}}}*/
  532. // FromDependency - versions satisfying a given dependency /*{{{*/
  533. bool VersionContainerInterface::FromDependency(VersionContainerInterface * const vci,
  534. pkgCacheFile &Cache,
  535. pkgCache::DepIterator const &D,
  536. CacheSetHelper::VerSelector const selector,
  537. CacheSetHelper &helper)
  538. {
  539. bool found = false;
  540. switch(selector) {
  541. case CacheSetHelper::ALL:
  542. {
  543. pkgCache::PkgIterator const T = D.TargetPkg();
  544. for (pkgCache::VerIterator Ver = T.VersionList(); Ver.end() == false; ++Ver)
  545. {
  546. if (D.IsSatisfied(Ver) == true)
  547. {
  548. vci->insert(Ver);
  549. found = true;
  550. }
  551. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  552. {
  553. pkgCache::VerIterator const V = Prv.OwnerVer();
  554. if (unlikely(V.end() == true) || D.IsSatisfied(Prv) == false)
  555. continue;
  556. vci->insert(V);
  557. found = true;
  558. }
  559. }
  560. return found;
  561. }
  562. case CacheSetHelper::CANDANDINST:
  563. {
  564. found = FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
  565. found &= FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
  566. return found;
  567. }
  568. case CacheSetHelper::CANDIDATE:
  569. {
  570. pkgCache::PkgIterator const T = D.TargetPkg();
  571. pkgCache::VerIterator const Cand = Cache[T].CandidateVerIter(Cache);
  572. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  573. {
  574. vci->insert(Cand);
  575. found = true;
  576. }
  577. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  578. {
  579. pkgCache::VerIterator const V = Prv.OwnerVer();
  580. pkgCache::VerIterator const Cand = Cache[Prv.OwnerPkg()].CandidateVerIter(Cache);
  581. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  582. continue;
  583. vci->insert(Cand);
  584. found = true;
  585. }
  586. return found;
  587. }
  588. case CacheSetHelper::INSTALLED:
  589. {
  590. pkgCache::PkgIterator const T = D.TargetPkg();
  591. pkgCache::VerIterator const Cand = T.CurrentVer();
  592. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  593. {
  594. vci->insert(Cand);
  595. found = true;
  596. }
  597. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  598. {
  599. pkgCache::VerIterator const V = Prv.OwnerVer();
  600. pkgCache::VerIterator const Cand = Prv.OwnerPkg().CurrentVer();
  601. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  602. continue;
  603. vci->insert(Cand);
  604. found = true;
  605. }
  606. return found;
  607. }
  608. case CacheSetHelper::CANDINST:
  609. return FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper) ||
  610. FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
  611. case CacheSetHelper::INSTCAND:
  612. return FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper) ||
  613. FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
  614. case CacheSetHelper::NEWEST:
  615. {
  616. pkgCache::PkgIterator const T = D.TargetPkg();
  617. pkgCache::VerIterator const Cand = T.VersionList();
  618. if (Cand.end() == false && D.IsSatisfied(Cand) == true)
  619. {
  620. vci->insert(Cand);
  621. found = true;
  622. }
  623. for (pkgCache::PrvIterator Prv = T.ProvidesList(); Prv.end() != true; ++Prv)
  624. {
  625. pkgCache::VerIterator const V = Prv.OwnerVer();
  626. pkgCache::VerIterator const Cand = Prv.OwnerPkg().VersionList();
  627. if (Cand.end() == true || V != Cand || D.IsSatisfied(Prv) == false)
  628. continue;
  629. vci->insert(Cand);
  630. found = true;
  631. }
  632. return found;
  633. }
  634. case CacheSetHelper::RELEASE:
  635. case CacheSetHelper::VERSIONNUMBER:
  636. // both make no sense here, so always false
  637. return false;
  638. }
  639. return found;
  640. }
  641. /*}}}*/
  642. // getCandidateVer - Returns the candidate version of the given package /*{{{*/
  643. pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
  644. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  645. pkgCache::VerIterator Cand;
  646. if (Cache.IsDepCacheBuilt() == true) {
  647. Cand = Cache[Pkg].CandidateVerIter(Cache);
  648. } else if (unlikely(Cache.GetPolicy() == nullptr)) {
  649. return pkgCache::VerIterator(Cache);
  650. } else {
  651. Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
  652. }
  653. if (Cand.end() == true)
  654. return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
  655. return Cand;
  656. }
  657. /*}}}*/
  658. // getInstalledVer - Returns the installed version of the given package /*{{{*/
  659. pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
  660. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
  661. if (Pkg->CurrentVer == 0)
  662. return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
  663. return Pkg.CurrentVer();
  664. }
  665. /*}}}*/
  666. // canNotFindPackage - with the given selector and pattern /*{{{*/
  667. void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
  668. PackageContainerInterface * const pci, pkgCacheFile &Cache,
  669. std::string const &pattern) {
  670. switch (select) {
  671. APT_IGNORE_DEPRECATED_PUSH
  672. case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
  673. case TASK: canNotFindTask(pci, Cache, pattern); break;
  674. case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
  675. case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
  676. case STRING: canNotFindPackage(pci, Cache, pattern); break;
  677. case UNKNOWN: break;
  678. APT_IGNORE_DEPRECATED_POP
  679. }
  680. }
  681. // canNotFindTask - handle the case no package is found for a task /*{{{*/
  682. void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  683. if (ShowError == true)
  684. _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
  685. }
  686. /*}}}*/
  687. // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
  688. void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  689. if (ShowError == true)
  690. _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
  691. }
  692. /*}}}*/
  693. // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
  694. void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
  695. if (ShowError == true)
  696. _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
  697. }
  698. /*}}}*/
  699. // canNotFindPackage - handle the case no package is found from a string/*{{{*/
  700. APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
  701. }
  702. /*}}}*/
  703. /*}}}*/
  704. // canNotFindPkgName - handle the case no package has this name /*{{{*/
  705. pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
  706. std::string const &str) {
  707. if (ShowError == true)
  708. _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
  709. return pkgCache::PkgIterator(Cache, 0);
  710. }
  711. /*}}}*/
  712. // canNotFindVersion - for package by selector /*{{{*/
  713. void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
  714. {
  715. switch (select) {
  716. APT_IGNORE_DEPRECATED_PUSH
  717. case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
  718. case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
  719. case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
  720. case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
  721. case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
  722. case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
  723. APT_IGNORE_DEPRECATED_POP
  724. case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
  725. case RELEASE:
  726. case VERSIONNUMBER:
  727. // invalid in this branch
  728. break;
  729. }
  730. }
  731. // canNotFindAllVer /*{{{*/
  732. void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
  733. pkgCache::PkgIterator const &Pkg) {
  734. if (ShowError == true)
  735. _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  736. }
  737. /*}}}*/
  738. // canNotFindInstCandVer /*{{{*/
  739. void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  740. pkgCache::PkgIterator const &Pkg) {
  741. canNotGetInstCandVer(Cache, Pkg);
  742. }
  743. /*}}}*/
  744. // canNotFindInstCandVer /*{{{*/
  745. void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
  746. pkgCache::PkgIterator const &Pkg) {
  747. canNotGetCandInstVer(Cache, Pkg);
  748. }
  749. /*}}}*/
  750. /*}}}*/
  751. // canNotGetVersion - for package by selector /*{{{*/
  752. pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  753. switch (select) {
  754. APT_IGNORE_DEPRECATED_PUSH
  755. case NEWEST: return canNotFindNewestVer(Cache, Pkg);
  756. case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
  757. case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
  758. APT_IGNORE_DEPRECATED_POP
  759. case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
  760. case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
  761. case ALL:
  762. case CANDANDINST:
  763. case RELEASE:
  764. case VERSIONNUMBER:
  765. // invalid in this branch
  766. return pkgCache::VerIterator(Cache, 0);
  767. }
  768. return pkgCache::VerIterator(Cache, 0);
  769. }
  770. // canNotFindNewestVer /*{{{*/
  771. pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
  772. pkgCache::PkgIterator const &Pkg) {
  773. if (ShowError == true)
  774. _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
  775. return pkgCache::VerIterator(Cache, 0);
  776. }
  777. /*}}}*/
  778. // canNotFindCandidateVer /*{{{*/
  779. pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
  780. pkgCache::PkgIterator const &Pkg) {
  781. if (ShowError == true)
  782. _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
  783. return pkgCache::VerIterator(Cache, 0);
  784. }
  785. /*}}}*/
  786. // canNotFindInstalledVer /*{{{*/
  787. pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
  788. pkgCache::PkgIterator const &Pkg) {
  789. if (ShowError == true)
  790. _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
  791. return pkgCache::VerIterator(Cache, 0);
  792. }
  793. /*}}}*/
  794. // canNotFindInstCandVer /*{{{*/
  795. pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
  796. pkgCache::PkgIterator const &Pkg) {
  797. if (ShowError == true)
  798. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  799. return pkgCache::VerIterator(Cache, 0);
  800. }
  801. /*}}}*/
  802. // canNotFindInstCandVer /*{{{*/
  803. pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
  804. pkgCache::PkgIterator const &Pkg) {
  805. if (ShowError == true)
  806. _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
  807. return pkgCache::VerIterator(Cache, 0);
  808. }
  809. /*}}}*/
  810. /*}}}*/
  811. // showPackageSelection - by selector and given pattern /*{{{*/
  812. void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
  813. std::string const &pattern) {
  814. switch (select) {
  815. APT_IGNORE_DEPRECATED_PUSH
  816. case REGEX: showRegExSelection(pkg, pattern); break;
  817. case TASK: showTaskSelection(pkg, pattern); break;
  818. case FNMATCH: showFnmatchSelection(pkg, pattern); break;
  819. APT_IGNORE_DEPRECATED_POP
  820. case PACKAGENAME: /* no suprises here */ break;
  821. case STRING: /* handled by the special cases */ break;
  822. case UNKNOWN: break;
  823. }
  824. }
  825. // showTaskSelection /*{{{*/
  826. APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
  827. std::string const &/*pattern*/) {
  828. }
  829. /*}}}*/
  830. // showRegExSelection /*{{{*/
  831. APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
  832. std::string const &/*pattern*/) {
  833. }
  834. /*}}}*/
  835. // showFnmatchSelection /*{{{*/
  836. APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
  837. std::string const &/*pattern*/) {
  838. }
  839. /*}}}*/
  840. /*}}}*/
  841. // showVersionSelection /*{{{*/
  842. void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
  843. pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
  844. switch (select) {
  845. APT_IGNORE_DEPRECATED_PUSH
  846. case RELEASE:
  847. showSelectedVersion(Pkg, Ver, pattern, true);
  848. break;
  849. case VERSIONNUMBER:
  850. showSelectedVersion(Pkg, Ver, pattern, false);
  851. break;
  852. APT_IGNORE_DEPRECATED_POP
  853. case NEWEST:
  854. case CANDIDATE:
  855. case INSTALLED:
  856. case CANDINST:
  857. case INSTCAND:
  858. case ALL:
  859. case CANDANDINST:
  860. // not really suprises, but in fact: just not implemented
  861. break;
  862. }
  863. }
  864. APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
  865. pkgCache::VerIterator const /*Ver*/,
  866. std::string const &/*ver*/,
  867. bool const /*verIsRel*/) {
  868. }
  869. /*}}}*/
  870. CacheSetHelper::CacheSetHelper(bool const ShowError, GlobalError::MsgType ErrorType) :
  871. ShowError(ShowError), ErrorType(ErrorType), d(NULL) {}
  872. CacheSetHelper::~CacheSetHelper() {}
  873. PackageContainerInterface::PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN), d(NULL) {}
  874. PackageContainerInterface::PackageContainerInterface(CacheSetHelper::PkgSelector const by) : ConstructedBy(by), d(NULL) {}
  875. PackageContainerInterface& PackageContainerInterface::operator=(PackageContainerInterface const &other) {
  876. if (this != &other)
  877. this->ConstructedBy = other.ConstructedBy;
  878. return *this;
  879. }
  880. PackageContainerInterface::~PackageContainerInterface() {}
  881. PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {}
  882. PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {}
  883. PackageUniverse::~PackageUniverse() {}
  884. VersionContainerInterface::VersionContainerInterface() : d(NULL) {}
  885. VersionContainerInterface& VersionContainerInterface::operator=(VersionContainerInterface const &) {
  886. return *this;
  887. }
  888. VersionContainerInterface::~VersionContainerInterface() {}
  889. }