database.c 6.3 KB

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