2 * libdpkg - Debian packaging suite library routines
3 * pkg-db.c - low level package database routines (hash tables, etc.)
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
7 * Copyright © 2011 Linaro Limited
8 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
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.
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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 #include <dpkg/i18n.h>
32 #include <dpkg/c-ctype.h>
33 #include <dpkg/dpkg.h>
34 #include <dpkg/dpkg-db.h>
35 #include <dpkg/string.h>
36 #include <dpkg/arch.h>
38 /* This must always be a prime for optimal performance.
39 * With 4093 buckets, we glean a 20% speedup, for 8191 buckets
40 * we get 23%. The nominal increase in memory usage is a mere
41 * sizeof(void *) * 8191 (i.e. less than 32 KiB on 32bit systems). */
44 static struct pkgset *bins[BINS];
45 static int npkg, nset;
48 * Return the package set with the given name.
50 * If the package already exists in the internal database, then it returns
51 * the existing structure. Otherwise it allocates a new one and will return
52 * it. The actual name associated to the package set is a lowercase version
53 * of the name given in parameter.
55 * A package set (struct pkgset) can be composed of multiple package instances
56 * (struct pkginfo) where each instance is distinguished by its architecture
57 * (as recorded in pkg.installed.arch and pkg.available.arch).
59 * @param inname Name of the package set.
61 * @return The package set.
64 pkg_db_find_set(const char *inname)
66 struct pkgset **setp, *new_set;
67 char *name = m_strdup(inname), *p;
75 setp = bins + (str_fnv_hash(name) % (BINS));
76 while (*setp && strcasecmp((*setp)->name, name))
77 setp = &(*setp)->next;
83 new_set = nfmalloc(sizeof(struct pkgset));
84 pkgset_blank(new_set);
85 new_set->name = nfstrsave(name);
97 * Return the singleton package instance from a package set.
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.
103 * @param set The package set to use.
105 * @return The singleton package instance.
108 pkg_db_get_singleton(struct pkgset *set)
112 switch (pkgset_installed_instances(set)) {
114 /* Pick an available candidate. */
115 for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
116 const struct dpkg_arch *arch = pkg->available.arch;
118 if (arch->type == DPKG_ARCH_NATIVE || arch->type == DPKG_ARCH_ALL)
121 /* Or failing that, the first entry. */
124 for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
125 if (pkg->status > PKG_STAT_NOTINSTALLED)
128 internerr("pkgset '%s' should have one installed instance", set->name);
135 * Return the singleton package instance with the given name.
137 * @param name The package name.
139 * @return The package instance.
142 pkg_db_find_singleton(const char *name)
147 set = pkg_db_find_set(name);
148 pkg = pkg_db_get_singleton(set);
150 ohshit(_("ambiguous package name '%s' with more "
151 "than one installed instance"), set->name);
157 * Return the package instance in a set with the given architecture.
159 * It traverse the various instances to find out whether there's one
160 * matching the given architecture. If found, it returns it. Otherwise it
161 * allocates a new instance and registers it in the package set before
164 * @param set The package set to use.
165 * @param arch The requested architecture.
167 * @return The package instance.
170 pkg_db_get_pkg(struct pkgset *set, const struct dpkg_arch *arch)
172 struct pkginfo *pkg, **pkgp;
175 assert(arch->type != DPKG_ARCH_NONE);
179 /* If there's a single unused slot, let's use that. */
180 if (pkg->installed.arch->type == DPKG_ARCH_NONE && pkg->arch_next == NULL) {
181 pkg->installed.arch = arch;
182 pkg->available.arch = arch;
186 /* Match the slot with the most appropriate architecture. The installed
187 * architecture always has preference over the available one, as there's
188 * a small time window on cross-grades, where they might differ. */
189 for (pkgp = &pkg; *pkgp; pkgp = &(*pkgp)->arch_next) {
190 if ((*pkgp)->installed.arch == arch)
194 /* Need to create a new instance for the wanted architecture. */
195 pkg = nfmalloc(sizeof(struct pkginfo));
198 pkg->arch_next = NULL;
199 pkg->installed.arch = arch;
200 pkg->available.arch = arch;
208 * Return the package instance with the given name and architecture.
210 * @param name The package name.
211 * @param arch The requested architecture.
213 * @return The package instance.
216 pkg_db_find_pkg(const char *name, const struct dpkg_arch *arch)
221 set = pkg_db_find_set(name);
222 pkg = pkg_db_get_pkg(set, arch);
228 * Return the number of package sets available in the database.
230 * @return The number of package sets.
233 pkg_db_count_set(void)
239 * Return the number of package instances available in the database.
241 * @return The number of package instances.
244 pkg_db_count_pkg(void)
255 * Create a new package iterator.
257 * It can iterate either over package sets or over package instances.
259 * @return The iterator.
262 pkg_db_iter_new(void)
264 struct pkgiterator *iter;
266 iter = m_malloc(sizeof(struct pkgiterator));
274 * Return the next package set in the database.
276 * If no further package set is available, it will return NULL.
278 * @name iter The iterator.
280 * @return A package set.
283 pkg_db_iter_next_set(struct pkgiterator *iter)
288 if (iter->nbinn >= BINS)
290 if (bins[iter->nbinn])
291 iter->pkg = &bins[iter->nbinn]->pkg;
295 set = iter->pkg->set;
297 iter->pkg = &set->next->pkg;
305 * Return the next package instance in the database.
307 * If no further package instance is available, it will return NULL. Note
308 * that it will return all instances of a given package set in sequential
309 * order. The first instance for a given package set will always correspond
310 * to the native architecture even if that package is not installed or
313 * @name iter The iterator.
315 * @return A package instance.
318 pkg_db_iter_next_pkg(struct pkgiterator *iter)
323 if (iter->nbinn >= BINS)
325 if (bins[iter->nbinn])
326 iter->pkg = &bins[iter->nbinn]->pkg;
332 iter->pkg = pkg->arch_next;
333 else if (pkg->set->next)
334 iter->pkg = &pkg->set->next->pkg;
342 * Free the package database iterator.
344 * @name iter The iterator.
347 pkg_db_iter_free(struct pkgiterator *iter)
357 dpkg_arch_reset_list();
361 for (i=0; i<BINS; i++) bins[i]= NULL;
365 pkg_db_report(FILE *file)
371 freq = m_malloc(sizeof(int) * nset + 1);
372 for (i = 0; i <= nset; i++)
374 for (i=0; i<BINS; i++) {
375 for (c=0, pkg= bins[i]; pkg; c++, pkg= pkg->next);
376 fprintf(file,"bin %5d has %7d\n",i,c);
379 for (i = nset; i > 0 && freq[i] == 0; i--);
381 fprintf(file, "size %7d occurs %5d times\n", i, freq[i]);
385 m_output(file, "<hash report>");