database.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. pigp->color= white;
  66. pigp->trigaw.head = pigp->trigaw.tail = NULL;
  67. pigp->othertrigaw_head = NULL;
  68. pigp->trigpend_head = NULL;
  69. blankpackageperfile(&pigp->installed);
  70. blankpackageperfile(&pigp->available);
  71. }
  72. void blankpackageperfile(struct pkginfoperfile *pifp) {
  73. pifp->essential= 0;
  74. pifp->depends= NULL;
  75. pifp->depended= NULL;
  76. pifp->description= pifp->maintainer= pifp->source= pifp->installedsize= pifp->bugs= pifp->origin= NULL;
  77. pifp->architecture= NULL;
  78. blankversion(&pifp->version);
  79. pifp->conffiles= NULL;
  80. pifp->arbs= NULL;
  81. pifp->valid= 1;
  82. }
  83. static int nes(const char *s) { return s && *s; }
  84. int informative(struct pkginfo *pkg, struct pkginfoperfile *info) {
  85. /* Used by dselect and dpkg query options as an aid to decide
  86. * whether to display things, and by dump to decide whether to write them
  87. * out.
  88. */
  89. if (info == &pkg->installed &&
  90. (pkg->want != want_unknown ||
  91. pkg->eflag != eflagv_ok ||
  92. pkg->status != stat_notinstalled ||
  93. informativeversion(&pkg->configversion)))
  94. /* We ignore Section and Priority, as these tend to hang around. */
  95. return 1;
  96. if (!info->valid) return 0;
  97. if (info->depends ||
  98. nes(info->description) ||
  99. nes(info->maintainer) ||
  100. nes(info->origin) ||
  101. nes(info->bugs) ||
  102. nes(info->installedsize) ||
  103. nes(info->source) ||
  104. informativeversion(&info->version) ||
  105. info->conffiles ||
  106. info->arbs) return 1;
  107. return 0;
  108. }
  109. struct pkginfo *findpackage(const char *inname) {
  110. struct pkginfo **pointerp, *newpkg;
  111. char *name = m_strdup(inname), *p;
  112. p= name;
  113. while(*p) { *p= tolower(*p); p++; }
  114. pointerp= bins + (hash(name) % (BINS));
  115. while (*pointerp && strcasecmp((*pointerp)->name,name))
  116. pointerp= &(*pointerp)->next;
  117. if (*pointerp) { free(name); return *pointerp; }
  118. newpkg= nfmalloc(sizeof(struct pkginfo));
  119. blankpackage(newpkg);
  120. newpkg->name= nfstrsave(name);
  121. newpkg->next= NULL;
  122. *pointerp= newpkg;
  123. npackages++;
  124. free(name);
  125. return newpkg;
  126. }
  127. int countpackages(void) {
  128. return npackages;
  129. }
  130. struct pkgiterator {
  131. struct pkginfo *pigp;
  132. int nbinn;
  133. };
  134. struct pkgiterator *iterpkgstart(void) {
  135. struct pkgiterator *i;
  136. i= m_malloc(sizeof(struct pkgiterator));
  137. i->pigp= NULL;
  138. i->nbinn= 0;
  139. return i;
  140. }
  141. struct pkginfo *iterpkgnext(struct pkgiterator *i) {
  142. struct pkginfo *r;
  143. while (!i->pigp) {
  144. if (i->nbinn >= BINS) return NULL;
  145. i->pigp= bins[i->nbinn++];
  146. }
  147. r= i->pigp; i->pigp= r->next; return r;
  148. }
  149. void iterpkgend(struct pkgiterator *i) {
  150. free(i);
  151. }
  152. void resetpackages(void) {
  153. int i;
  154. nffreeall();
  155. npackages= 0;
  156. for (i=0; i<BINS; i++) bins[i]= NULL;
  157. }
  158. void hashreport(FILE *file) {
  159. int i, c;
  160. struct pkginfo *pkg;
  161. int *freq;
  162. freq= m_malloc(sizeof(int)*npackages+1);
  163. for (i=0; i<=npackages; i++) freq[i]= 0;
  164. for (i=0; i<BINS; i++) {
  165. for (c=0, pkg= bins[i]; pkg; c++, pkg= pkg->next);
  166. fprintf(file,"bin %5d has %7d\n",i,c);
  167. freq[c]++;
  168. }
  169. for (i=npackages; i>0 && freq[i]==0; i--);
  170. while (i>=0) { fprintf(file,_("size %7d occurs %5d times\n"),i,freq[i]); i--; }
  171. if (ferror(file)) ohshite(_("failed write during hashreport"));
  172. free(freq);
  173. }
  174. /*
  175. * Test dataset package names were:
  176. *
  177. * agetty bash bc bdflush biff bin86 binutil binutils bison bsdutils
  178. * byacc chfn cron dc dictionaries diff dlltools dpkg e2fsprogs ed
  179. * elisp19 elm emacs emacs-nox emacs-x emacs19 file fileutils find
  180. * flex fsprogs gas gawk gcc gcc1 gcc2 gdb ghostview ghstview glibcdoc
  181. * gnuplot grep groff gs gs_both gs_svga gs_x gsfonts gxditviw gzip
  182. * hello hostname idanish ifrench igerman indent inewsinn info inn
  183. * ispell kbd kern1148 language ldso less libc libgr libgrdev librl
  184. * lilo linuxsrc login lout lpr m4 mailx make man manpages more mount
  185. * mtools ncurses netbase netpbm netstd patch perl4 perl5 procps
  186. * psutils rcs rdev sed sendmail seyon shar shellutils smail svgalib
  187. * syslogd sysvinit tar tcpdump tcsh tex texidoc texinfo textutils
  188. * time timezone trn unzip uuencode wenglish wu-ftpd x8514 xaxe xbase
  189. * xbdm2 xcomp xcoral xdevel xfig xfnt100 xfnt75 xfntbig xfntscl
  190. * xgames xherc xmach32 xmach8 xmono xnet xs3 xsvga xtexstuff xv
  191. * xvga16 xxgdb zip
  192. */