You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cacheset.cc 32 KiB

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