pkg-db.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * pkg-db.c - low level package database routines (hash tables, etc.)
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. #include <dpkg/arch.h>
  33. /* This must always be a prime for optimal performance.
  34. * With 4093 buckets, we glean a 20% speedup, for 8191 buckets
  35. * we get 23%. The nominal increase in memory usage is a mere
  36. * sizeof(void *) * 8063 (i.e. less than 32 KiB on 32bit systems). */
  37. #define BINS 8191
  38. static struct pkgset *bins[BINS];
  39. static int npkg, nset;
  40. #define FNV_offset_basis 2166136261ul
  41. #define FNV_mixing_prime 16777619ul
  42. /**
  43. * Fowler/Noll/Vo -- simple string hash.
  44. *
  45. * For more info, see <http://www.isthe.com/chongo/tech/comp/fnv/index.html>.
  46. */
  47. static unsigned int hash(const char *name) {
  48. register unsigned int h = FNV_offset_basis;
  49. register unsigned int p = FNV_mixing_prime;
  50. while( *name ) {
  51. h *= p;
  52. h ^= *name++;
  53. }
  54. return h;
  55. }
  56. /**
  57. * Return the package set with the given name.
  58. *
  59. * If the package already exists in the internal database, then it returns
  60. * the existing structure. Otherwise it allocates a new one and will return
  61. * it. The actual name associated to the package set is a lowercase version
  62. * of the name given in parameter.
  63. *
  64. * A package set (struct pkgset) can be composed of multiple package instances
  65. * (struct pkginfo) where each instance is distinguished by its architecture
  66. * (as recorded in pkg.installed.arch and pkg.available.arch).
  67. *
  68. * @param inname Name of the package set.
  69. *
  70. * @return The package set.
  71. */
  72. struct pkgset *
  73. pkg_db_find_set(const char *inname)
  74. {
  75. struct pkgset **setp, *new_set;
  76. char *name = m_strdup(inname), *p;
  77. p= name;
  78. while(*p) { *p= tolower(*p); p++; }
  79. setp = bins + (hash(name) % (BINS));
  80. while (*setp && strcasecmp((*setp)->name, name))
  81. setp = &(*setp)->next;
  82. if (*setp) {
  83. free(name);
  84. return *setp;
  85. }
  86. new_set = nfmalloc(sizeof(struct pkgset));
  87. pkgset_blank(new_set);
  88. new_set->name = nfstrsave(name);
  89. new_set->next = NULL;
  90. *setp = new_set;
  91. nset++;
  92. npkg++;
  93. free(name);
  94. return new_set;
  95. }
  96. /**
  97. * Return the singleton package instance from a package set.
  98. *
  99. * This means, if none are installed either an instance with native or
  100. * all arch or the first if none found, the single installed instance,
  101. * or NULL if more than one instance is installed.
  102. *
  103. * @param set The package set to use.
  104. *
  105. * @return The singleton package instance.
  106. */
  107. struct pkginfo *
  108. pkg_db_get_singleton(struct pkgset *set)
  109. {
  110. struct pkginfo *pkg;
  111. switch (pkgset_installed_instances(set)) {
  112. case 0:
  113. /* Pick an available candidate. */
  114. for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
  115. const struct dpkg_arch *arch = pkg->available.arch;
  116. if (arch->type == arch_native || arch->type == arch_all)
  117. return pkg;
  118. }
  119. /* Or failing that, the first entry. */
  120. return &set->pkg;
  121. case 1:
  122. for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
  123. if (pkg->status > stat_notinstalled)
  124. return pkg;
  125. }
  126. internerr("pkgset '%s' should have one installed instance", set->name);
  127. default:
  128. return NULL;
  129. }
  130. }
  131. /**
  132. * Return the singleton package instance with the given name.
  133. *
  134. * @param name The package name.
  135. *
  136. * @return The package instance.
  137. */
  138. struct pkginfo *
  139. pkg_db_find_singleton(const char *name)
  140. {
  141. struct pkgset *set;
  142. struct pkginfo *pkg;
  143. set = pkg_db_find_set(name);
  144. pkg = pkg_db_get_singleton(set);
  145. if (pkg == NULL)
  146. ohshit(_("ambiguous package name '%s' with more "
  147. "than one installed instance"), set->name);
  148. return pkg;
  149. }
  150. /**
  151. * Return the package instance in a set with the given architecture.
  152. *
  153. * It traverse the various instances to find out whether there's one
  154. * matching the given architecture. If found, it returns it. Otherwise it
  155. * allocates a new instance and registers it in the package set before
  156. * returning it.
  157. *
  158. * @param set The package set to use.
  159. * @param arch The requested architecture.
  160. *
  161. * @return The package instance.
  162. */
  163. struct pkginfo *
  164. pkg_db_get_pkg(struct pkgset *set, const struct dpkg_arch *arch)
  165. {
  166. struct pkginfo *pkg, **pkgp;
  167. assert(arch);
  168. assert(arch->type != arch_none);
  169. pkg = &set->pkg;
  170. /* If there's a single unused slot, let's use that. */
  171. if (pkg->installed.arch->type == arch_none && pkg->arch_next == NULL) {
  172. pkg->installed.arch = arch;
  173. pkg->available.arch = arch;
  174. return pkg;
  175. }
  176. /* Match the slot with the most appropriate architecture. The installed
  177. * architecture always has preference over the available one, as there's
  178. * a small time window on cross-grades, where they might differ. */
  179. for (pkgp = &pkg; *pkgp; pkgp = &(*pkgp)->arch_next) {
  180. if ((*pkgp)->installed.arch == arch)
  181. return *pkgp;
  182. }
  183. /* Need to create a new instance for the wanted architecture. */
  184. pkg = nfmalloc(sizeof(struct pkginfo));
  185. pkg_blank(pkg);
  186. pkg->set = set;
  187. pkg->arch_next = NULL;
  188. pkg->installed.arch = arch;
  189. pkg->available.arch = arch;
  190. *pkgp = pkg;
  191. npkg++;
  192. return pkg;
  193. }
  194. /**
  195. * Return the package instance with the given name and architecture.
  196. *
  197. * @param name The package name.
  198. * @param arch The requested architecture.
  199. *
  200. * @return The package instance.
  201. */
  202. struct pkginfo *
  203. pkg_db_find_pkg(const char *name, const struct dpkg_arch *arch)
  204. {
  205. struct pkgset *set;
  206. struct pkginfo *pkg;
  207. set = pkg_db_find_set(name);
  208. pkg = pkg_db_get_pkg(set, arch);
  209. return pkg;
  210. }
  211. /**
  212. * Return the number of package sets available in the database.
  213. *
  214. * @return The number of package sets.
  215. */
  216. int
  217. pkg_db_count_set(void)
  218. {
  219. return nset;
  220. }
  221. /**
  222. * Return the number of package instances available in the database.
  223. *
  224. * @return The number of package instances.
  225. */
  226. int
  227. pkg_db_count_pkg(void)
  228. {
  229. return npkg;
  230. }
  231. struct pkgiterator {
  232. struct pkginfo *pkg;
  233. int nbinn;
  234. };
  235. /**
  236. * Create a new package iterator.
  237. *
  238. * It can iterate either over package sets or over package instances.
  239. *
  240. * @return The iterator.
  241. */
  242. struct pkgiterator *
  243. pkg_db_iter_new(void)
  244. {
  245. struct pkgiterator *iter;
  246. iter = m_malloc(sizeof(struct pkgiterator));
  247. iter->pkg = NULL;
  248. iter->nbinn = 0;
  249. return iter;
  250. }
  251. /**
  252. * Return the next package set in the database.
  253. *
  254. * If no further package set is available, it will return NULL.
  255. *
  256. * @name iter The iterator.
  257. *
  258. * @return A package set.
  259. */
  260. struct pkgset *
  261. pkg_db_iter_next_set(struct pkgiterator *iter)
  262. {
  263. struct pkgset *set;
  264. while (!iter->pkg) {
  265. if (iter->nbinn >= BINS)
  266. return NULL;
  267. if (bins[iter->nbinn])
  268. iter->pkg = &bins[iter->nbinn]->pkg;
  269. iter->nbinn++;
  270. }
  271. set = iter->pkg->set;
  272. if (set->next)
  273. iter->pkg = &set->next->pkg;
  274. else
  275. iter->pkg = NULL;
  276. return set;
  277. }
  278. /**
  279. * Return the next package instance in the database.
  280. *
  281. * If no further package instance is available, it will return NULL. Note
  282. * that it will return all instances of a given package set in sequential
  283. * order. The first instance for a given package set will always correspond
  284. * to the native architecture even if that package is not installed or
  285. * available.
  286. *
  287. * @name iter The iterator.
  288. *
  289. * @return A package instance.
  290. */
  291. struct pkginfo *
  292. pkg_db_iter_next_pkg(struct pkgiterator *iter)
  293. {
  294. struct pkginfo *r;
  295. while (!iter->pkg) {
  296. if (iter->nbinn >= BINS)
  297. return NULL;
  298. if (bins[iter->nbinn])
  299. iter->pkg = &bins[iter->nbinn]->pkg;
  300. iter->nbinn++;
  301. }
  302. r = iter->pkg;
  303. if (r->arch_next)
  304. iter->pkg = r->arch_next;
  305. else if (r->set->next)
  306. iter->pkg = &r->set->next->pkg;
  307. else
  308. iter->pkg = NULL;
  309. return r;
  310. }
  311. /**
  312. * Free the package database iterator.
  313. *
  314. * @name iter The iterator.
  315. */
  316. void
  317. pkg_db_iter_free(struct pkgiterator *iter)
  318. {
  319. free(iter);
  320. }
  321. void
  322. pkg_db_reset(void)
  323. {
  324. int i;
  325. dpkg_arch_reset_list();
  326. nffreeall();
  327. nset = 0;
  328. npkg = 0;
  329. for (i=0; i<BINS; i++) bins[i]= NULL;
  330. }
  331. void
  332. pkg_db_report(FILE *file)
  333. {
  334. int i, c;
  335. struct pkgset *pkg;
  336. int *freq;
  337. freq = m_malloc(sizeof(int) * nset + 1);
  338. for (i = 0; i <= nset; i++)
  339. freq[i] = 0;
  340. for (i=0; i<BINS; i++) {
  341. for (c=0, pkg= bins[i]; pkg; c++, pkg= pkg->next);
  342. fprintf(file,"bin %5d has %7d\n",i,c);
  343. freq[c]++;
  344. }
  345. for (i = nset; i > 0 && freq[i] == 0; i--);
  346. while (i >= 0) {
  347. fprintf(file, "size %7d occurs %5d times\n", i, freq[i]);
  348. i--;
  349. }
  350. m_output(file, "<hash report>");
  351. free(freq);
  352. }