database.c 6.2 KB

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