database.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * dpkg-db.h - Low level package database routines (hash tables, etc.)
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <dpkg-i18n.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <dpkg.h>
  28. #include <dpkg-db.h>
  29. #define BINS 8191
  30. /* This must always be a prime for optimal performance.
  31. * With 4093 buckets, we glean a 20% speedup, for 8191 buckets
  32. * we get 23%. The nominal increase in memory usage is a mere
  33. * sizeof(void*)*8063 (I.E. less than 32KB on 32bit systems)
  34. */
  35. static struct pkginfo *bins[BINS];
  36. static int npackages;
  37. #define FNV_offset_basis 2166136261ul
  38. #define FNV_mixing_prime 16777619ul
  39. /* Fowler/Noll/Vo -- simple string hash.
  40. * For more info, see http://www.isthe.com/chongo/tech/comp/fnv/index.html
  41. * */
  42. static unsigned int hash(const char *name) {
  43. register unsigned int h = FNV_offset_basis;
  44. register unsigned int p = FNV_mixing_prime;
  45. while( *name ) {
  46. h *= p;
  47. h ^= *name++;
  48. }
  49. return h;
  50. }
  51. void blankversion(struct versionrevision *version) {
  52. version->epoch= 0;
  53. version->version= version->revision= NULL;
  54. }
  55. void blankpackage(struct pkginfo *pigp) {
  56. pigp->name= NULL;
  57. pigp->status= stat_notinstalled;
  58. pigp->eflag= eflagv_ok;
  59. pigp->want= want_unknown;
  60. pigp->priority= pri_unknown;
  61. pigp->otherpriority = NULL;
  62. pigp->section= NULL;
  63. blankversion(&pigp->configversion);
  64. pigp->files= NULL;
  65. pigp->installed.valid= 0;
  66. pigp->available.valid= 0;
  67. pigp->clientdata= NULL;
  68. pigp->color= white;
  69. pigp->trigaw.head = pigp->trigaw.tail = NULL;
  70. pigp->othertrigaw_head = NULL;
  71. pigp->trigpend_head = NULL;
  72. blankpackageperfile(&pigp->installed);
  73. blankpackageperfile(&pigp->available);
  74. }
  75. void blankpackageperfile(struct pkginfoperfile *pifp) {
  76. pifp->essential= 0;
  77. pifp->depends= NULL;
  78. pifp->depended= NULL;
  79. pifp->description= pifp->maintainer= pifp->source= pifp->installedsize= pifp->bugs= pifp->origin= NULL;
  80. pifp->architecture= NULL;
  81. blankversion(&pifp->version);
  82. pifp->conffiles= NULL;
  83. pifp->arbs= NULL;
  84. pifp->valid= 1;
  85. }
  86. static int nes(const char *s) { return s && *s; }
  87. int informative(struct pkginfo *pkg, struct pkginfoperfile *info) {
  88. /* Used by dselect and dpkg query options as an aid to decide
  89. * whether to display things, and by dump to decide whether to write them
  90. * out.
  91. */
  92. if (info == &pkg->installed &&
  93. (pkg->want != want_unknown ||
  94. pkg->eflag != eflagv_ok ||
  95. pkg->status != stat_notinstalled ||
  96. informativeversion(&pkg->configversion)))
  97. /* We ignore Section and Priority, as these tend to hang around. */
  98. return 1;
  99. if (!info->valid) return 0;
  100. if (info->depends ||
  101. nes(info->description) ||
  102. nes(info->maintainer) ||
  103. nes(info->origin) ||
  104. nes(info->bugs) ||
  105. nes(info->installedsize) ||
  106. nes(info->source) ||
  107. informativeversion(&info->version) ||
  108. info->conffiles ||
  109. info->arbs) return 1;
  110. return 0;
  111. }
  112. struct pkginfo *findpackage(const char *inname) {
  113. struct pkginfo **pointerp, *newpkg;
  114. char *name = m_strdup(inname), *p;
  115. p= name;
  116. while(*p) { *p= tolower(*p); p++; }
  117. pointerp= bins + (hash(name) % (BINS));
  118. while (*pointerp && strcasecmp((*pointerp)->name,name))
  119. pointerp= &(*pointerp)->next;
  120. if (*pointerp) { free(name); return *pointerp; }
  121. newpkg= nfmalloc(sizeof(struct pkginfo));
  122. blankpackage(newpkg);
  123. newpkg->name= nfstrsave(name);
  124. newpkg->next= NULL;
  125. *pointerp= newpkg;
  126. npackages++;
  127. free(name);
  128. return newpkg;
  129. }
  130. int countpackages(void) {
  131. return npackages;
  132. }
  133. struct pkgiterator {
  134. struct pkginfo *pigp;
  135. int nbinn;
  136. };
  137. struct pkgiterator *iterpkgstart(void) {
  138. struct pkgiterator *i;
  139. i= m_malloc(sizeof(struct pkgiterator));
  140. i->pigp= NULL;
  141. i->nbinn= 0;
  142. return i;
  143. }
  144. struct pkginfo *iterpkgnext(struct pkgiterator *i) {
  145. struct pkginfo *r;
  146. while (!i->pigp) {
  147. if (i->nbinn >= BINS) return NULL;
  148. i->pigp= bins[i->nbinn++];
  149. }
  150. r= i->pigp; i->pigp= r->next; return r;
  151. }
  152. void iterpkgend(struct pkgiterator *i) {
  153. free(i);
  154. }
  155. void resetpackages(void) {
  156. int i;
  157. nffreeall();
  158. npackages= 0;
  159. for (i=0; i<BINS; i++) bins[i]= NULL;
  160. }
  161. void hashreport(FILE *file) {
  162. int i, c;
  163. struct pkginfo *pkg;
  164. int *freq;
  165. freq= m_malloc(sizeof(int)*npackages+1);
  166. for (i=0; i<=npackages; i++) freq[i]= 0;
  167. for (i=0; i<BINS; i++) {
  168. for (c=0, pkg= bins[i]; pkg; c++, pkg= pkg->next);
  169. fprintf(file,"bin %5d has %7d\n",i,c);
  170. freq[c]++;
  171. }
  172. for (i=npackages; i>0 && freq[i]==0; i--);
  173. while (i>=0) { fprintf(file,_("size %7d occurs %5d times\n"),i,freq[i]); i--; }
  174. if (ferror(file)) ohshite(_("failed write during hashreport"));
  175. free(freq);
  176. }
  177. /*
  178. * Test dataset package names were:
  179. *
  180. * agetty bash bc bdflush biff bin86 binutil binutils bison bsdutils
  181. * byacc chfn cron dc dictionaries diff dlltools dpkg e2fsprogs ed
  182. * elisp19 elm emacs emacs-nox emacs-x emacs19 file fileutils find
  183. * flex fsprogs gas gawk gcc gcc1 gcc2 gdb ghostview ghstview glibcdoc
  184. * gnuplot grep groff gs gs_both gs_svga gs_x gsfonts gxditviw gzip
  185. * hello hostname idanish ifrench igerman indent inewsinn info inn
  186. * ispell kbd kern1148 language ldso less libc libgr libgrdev librl
  187. * lilo linuxsrc login lout lpr m4 mailx make man manpages more mount
  188. * mtools ncurses netbase netpbm netstd patch perl4 perl5 procps
  189. * psutils rcs rdev sed sendmail seyon shar shellutils smail svgalib
  190. * syslogd sysvinit tar tcpdump tcsh tex texidoc texinfo textutils
  191. * time timezone trn unzip uuencode wenglish wu-ftpd x8514 xaxe xbase
  192. * xbdm2 xcomp xcoral xdevel xfig xfnt100 xfnt75 xfntbig xfntscl
  193. * xgames xherc xmach32 xmach8 xmono xnet xs3 xsvga xtexstuff xv
  194. * xvga16 xxgdb zip
  195. */