File: dhash.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (203 lines) | stat: -rw-r--r-- 4,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright  1998-2000 Harold Tay.
 * Copyright  2000- Patrik Rdman.
 */

/*
 * Disk based persistent hash table, like gdbm.
 *
 * This is specifically for the news system.  The keys are message
 * ids, the values are {newsgroup, serial number} tuples.
 *
 * The table is static in size.  There a 3 files: ".table" is the hash
 * table proper.  It contains an array of chain "pointers".  The
 * other 2 files are handled by allo_*().  ".chain" is an arena of
 * chain structures; ".newsgroup" contains all newsgroups.
 *
 * Supported opertions are insert, delete, and search.
 *
 * Requires allocate.c to allocate space on a disk file (".chain")
 * for data.
 */

static const char rcsid[] = "$Id$";

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/time.h>
#include "config.h"
#include "allocate.h" /* Save ids and chains */
#include "dhash.h"
#include "newsgroup.h" /* Save newsgroup names */
#include <out.h>
#include <nap.h>

extern char dh_tablefile[];
extern char dh_chainfile[];
extern char dh_groupfile[];
extern struct table *dh_table;
extern unsigned int dhhash(char *);
extern struct chain *dhlocatechain(char *);
extern int dh_isreadonly;
extern int dhlock(void);
extern int dhunlock(void);

static char *inttochar2 (int integer)
{
   static unsigned char buf[2];

   buf[0] = (integer & 0xffff) >> 8;
   buf[1] = integer & 0xff;
   return ((char *) buf);
}

static char *inttochar3 (unsigned int integer)
{
   static unsigned char buf[3];

   integer >>= 2; /* last 2 bits will be 0 */
   buf[0] = (integer & 0xffffff) >> 16;
   buf[1] = (integer & 0xffff) >> 8;
   buf[2] = integer & 0xff;
   return ((char *) buf);
}

int dh_insert (struct data *dp)
{
   struct chain *chp;
   unsigned int h;
   int off;
   int len;

   if (dh_isreadonly)
   {
      errno = EPERM;
      return (-1);
   }
   /* Search first; if found, cannot insert. */
   if (-1 == dhlock())
      return (-1);
   chp = dhlocatechain(dp->messageid);
   while (chp)
   {
      unsigned index;

      if (0 == strcmp(dp->messageid, chp->messageid))
      {
         dhunlock();
         errno = EEXIST;
         return (-1);
      }
      index = (unsigned) chp->next;
      if (!index)
         break;
      chp = allo_deref((unsigned) chp->next);
      if (NULL == chp)
      {
         LOG("dh_insert:bad deref in chain for \"<%s>\" during search", dp->messageid);
         break;
      }
   }

   len = sizeof (struct chain) + strlen(dp->messageid) + 1 - sizeof (chp->messageid);
   off = allo_make(len);
   chp = allo_deref(off);
   if (NULL == chp)
   {
      LOG("dh_insert:allo_make gave bad allo_deref");
      dhunlock();
      return (-1);
   }
   chp->serial = dp->serial;
   strcpy(chp->messageid, dp->messageid);
   {
      int ident;

      ident = ng_ident(dp->newsgroup);
      if (-1 == ident)
         if (-1 == (ident = ng_addgroup(dp->newsgroup)))
            return (-1);
      memcpy(chp->newsgroup, inttochar2(ident), 2);
   }

   /*
    * If someone searches before
    * dh_table->next[h] is updated, they just will miss the new one.
    * Readers don't need to lock, not even LOCK_SH.  The worst that
    * can come of this is, someone else has just inserted the same
    * object, and we have two of them instead of one.
    */

   h = dhhash(dp->messageid);

   {
      unsigned char *x = dh_table->next + h * 3;

      chp->next = char3toint(x);
      memcpy(x, inttochar3(off), 3);
   }
   dhunlock();
   return (0);
}

int dh_delete (struct data *dp)
{
   struct chain *oldchp = NULL;
   struct chain *chp;

   if (dh_isreadonly)
   {
      errno = EPERM;
      return (-1);
   }

   if (-1 == dhlock())
      return (-1);
   chp = dhlocatechain(dp->messageid);
   if (NULL == chp)
      goto fail;

   while (strcmp(chp->messageid, dp->messageid))
   {
      unsigned int index = chp->next;

      if (!index)
         goto fail;
      oldchp = chp;
      chp = allo_deref(index);
      if (NULL == chp)
      {
         LOG2("dh_delete:dereferencing bad offset");
         goto fail;
      }
   }

   *chp->messageid = '\0';
   if (oldchp)
      oldchp->next = chp->next;
   else
   {
      unsigned int i = chp->next;
      unsigned h = dhhash(dp->messageid);

      memcpy(dh_table->next + h * 3, inttochar3(i), 3);
   }
   allo_free(allo_ref(chp), sizeof (struct chain)
             + strlen(dp->messageid) + 1 - sizeof (chp->messageid));

   dhunlock();
   return (0);

fail:
   dhunlock();
   return (-1); /* Not found */
}