* Silently refuse to add directory entries for Internet addresses already
[citadel.git] / citadel / database.c
1 /*
2  * $Id$
3  *
4  * GDBM database driver for Citadel/UX
5  *
6  */
7
8 /*
9  * Note that each call to a GDBM function is wrapped in an S_DATABASE critical
10  * section.  This is done because GDBM is not threadsafe.  This is the ONLY
11  * place in the entire Citadel server where any code enters two different
12  * classes of critical sections at the same time; this is why the GDBM calls
13  * are *tightly* wrapped in S_DATABASE.  Opening multiple concurrent critical
14  * sections elsewhere in the code can, and probably will, cause deadlock
15  * conditions to occur.  (Deadlock is bad.  Eliminate.)
16  */
17
18 #ifdef DLL_EXPORT
19 #define IN_LIBCIT
20 #endif
21
22 #include "sysdep.h"
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <ctype.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <gdbm.h>
42 #include "citadel.h"
43 #include "server.h"
44 #include "database.h"
45 #include "sysdep_decls.h"
46
47
48 /*
49  * This array holds one gdbm handle for each Citadel database.
50  */
51 GDBM_FILE gdbms[MAXCDB];
52
53 /*
54  * We also keep these around, for sequential searches (one per session slot)
55  */
56 int max_keys = 0;
57 datum *dtkey;
58
59
60 /*
61  * Reclaim unused space in the databases.  We need to do each one of
62  * these discretely, rather than in a loop.
63  */
64 void defrag_databases(void)
65 {
66
67         /* defrag the message base */
68         lprintf(7, "Defragmenting message base\n");
69         begin_critical_section(S_DATABASE);
70         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
71         end_critical_section(S_DATABASE);
72
73         /* defrag the user file, mailboxes, and user/room relationships */
74         lprintf(7, "Defragmenting user file\n");
75         begin_critical_section(S_USERSUPP);
76         begin_critical_section(S_DATABASE);
77         gdbm_reorganize(gdbms[CDB_USERSUPP]);
78         gdbm_reorganize(gdbms[CDB_VISIT]);
79         end_critical_section(S_DATABASE);
80         end_critical_section(S_USERSUPP);
81
82         /* defrag the room files and message lists */
83         lprintf(7, "Defragmenting room files and message lists\n");
84         begin_critical_section(S_QUICKROOM);
85         begin_critical_section(S_DATABASE);
86         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
87         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
88         end_critical_section(S_DATABASE);
89         end_critical_section(S_QUICKROOM);
90
91         /* defrag the floor table */
92         lprintf(7, "Defragmenting floor table\n");
93         begin_critical_section(S_FLOORTAB);
94         begin_critical_section(S_DATABASE);
95         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
96         end_critical_section(S_DATABASE);
97         end_critical_section(S_FLOORTAB);
98
99         /* defrag the directory */
100         lprintf(7, "Defragmenting the directory\n");
101         begin_critical_section(S_DIRECTORY);
102         gdbm_reorganize(gdbms[CDB_DIRECTORY]);
103         end_critical_section(S_DIRECTORY);
104 }
105
106
107 /*
108  * Open the various gdbm databases we'll be using.  Any database which
109  * does not exist should be created.
110  */
111 void open_databases(void)
112 {
113         lprintf(7, "%s\n", gdbm_version);
114
115         /*
116          * Silently try to create the database subdirectory.  If it's
117          * already there, no problem.
118          */
119         system("exec mkdir data 2>/dev/null");
120
121         /* a critical section is unnecessary, as this function is called before
122            any other threads are created. and it causes problems on BSDI.
123
124            begin_critical_section(S_DATABASE);
125
126          */
127
128         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
129                                        GDBM_WRCREAT, 0600, NULL);
130         if (gdbms[CDB_MSGMAIN] == NULL) {
131                 lprintf(2, "Cannot open msgmain: %s\n",
132                         gdbm_strerror(gdbm_errno));
133                 exit(1);
134         }
135         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
136                                         GDBM_WRCREAT, 0600, NULL);
137         if (gdbms[CDB_USERSUPP] == NULL) {
138                 lprintf(2, "Cannot open usersupp: %s\n",
139                         gdbm_strerror(gdbm_errno));
140                 exit(1);
141         }
142         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
143                                      GDBM_WRCREAT, 0600, NULL);
144         if (gdbms[CDB_VISIT] == NULL) {
145                 lprintf(2, "Cannot open visit file: %s\n",
146                         gdbm_strerror(gdbm_errno));
147                 exit(1);
148         }
149         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
150                                          GDBM_WRCREAT, 0600, NULL);
151         if (gdbms[CDB_QUICKROOM] == NULL) {
152                 lprintf(2, "Cannot open quickroom: %s\n",
153                         gdbm_strerror(gdbm_errno));
154                 exit(1);
155         }
156         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
157                                         GDBM_WRCREAT, 0600, NULL);
158         if (gdbms[CDB_FLOORTAB] == NULL) {
159                 lprintf(2, "Cannot open floortab: %s\n",
160                         gdbm_strerror(gdbm_errno));
161                 exit(1);
162         }
163         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
164                                         GDBM_WRCREAT, 0600, NULL);
165         if (gdbms[CDB_MSGLISTS] == NULL) {
166                 lprintf(2, "Cannot open msglists: %s\n",
167                         gdbm_strerror(gdbm_errno));
168                 exit(1);
169         }
170         gdbms[CDB_DIRECTORY] = gdbm_open("data/directory.gdbm", 0,
171                                         GDBM_WRCREAT, 0600, NULL);
172         if (gdbms[CDB_DIRECTORY] == NULL) {
173                 lprintf(2, "Cannot open directory: %s\n",
174                         gdbm_strerror(gdbm_errno));
175                 exit(1);
176         }
177         /*
178            end_critical_section(S_DATABASE);
179          */
180
181 }
182
183
184 /*
185  * Close all of the gdbm database files we've opened.  This can be done
186  * in a loop, since it's just a bunch of closes.
187  */
188 void close_databases(void)
189 {
190         int a;
191
192         begin_critical_section(S_DATABASE);
193         for (a = 0; a < MAXCDB; ++a) {
194                 lprintf(7, "Closing database %d\n", a);
195                 gdbm_close(gdbms[a]);
196         }
197         end_critical_section(S_DATABASE);
198
199         for (a = 0; a < max_keys; ++a) {
200                 if (dtkey[a].dptr != NULL) {
201                         phree(dtkey[a].dptr);
202                 }
203         }
204
205 }
206
207
208 /*
209  * Store a piece of data.  Returns 0 if the operation was successful.  If a
210  * datum already exists it should be overwritten.
211  */
212 int cdb_store(int cdb,
213               void *key, int keylen,
214               void *data, int datalen)
215 {
216
217         datum dkey, ddata;
218         int retval;
219
220         dkey.dsize = keylen;
221         dkey.dptr = key;
222         ddata.dsize = datalen;
223         ddata.dptr = data;
224
225         begin_critical_section(S_DATABASE);
226         retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
227         end_critical_section(S_DATABASE);
228         if (retval < 0) {
229                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
230                 return (-1);
231         }
232         return (0);
233 }
234
235
236 /*
237  * Delete a piece of data.  Returns 0 if the operation was successful.
238  */
239 int cdb_delete(int cdb, void *key, int keylen)
240 {
241
242         datum dkey;
243         int retval;
244
245         dkey.dsize = keylen;
246         dkey.dptr = key;
247
248         begin_critical_section(S_DATABASE);
249         retval = gdbm_delete(gdbms[cdb], dkey);
250         end_critical_section(S_DATABASE);
251         return (retval);
252
253 }
254
255
256
257
258 /*
259  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
260  * a struct cdbdata which it is the caller's responsibility to free later on
261  * using the cdb_free() routine.
262  */
263 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
264 {
265
266         struct cdbdata *tempcdb;
267         datum dkey, dret;
268
269         dkey.dsize = keylen;
270         dkey.dptr = key;
271
272         begin_critical_section(S_DATABASE);
273         dret = gdbm_fetch(gdbms[cdb], dkey);
274         end_critical_section(S_DATABASE);
275         if (dret.dptr == NULL) {
276                 return NULL;
277         }
278         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
279         if (tempcdb == NULL) {
280                 lprintf(2, "Cannot allocate memory!\n");
281         }
282         tempcdb->len = dret.dsize;
283         tempcdb->ptr = dret.dptr;
284         return (tempcdb);
285 }
286
287
288 /*
289  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
290  * more complex stuff with other database managers in the future).
291  */
292 void cdb_free(struct cdbdata *cdb)
293 {
294         phree(cdb->ptr);
295         phree(cdb);
296 }
297
298 void cdb_close_cursor(cdb)
299 {
300         while (max_keys <= CC->cs_pid) {
301                 ++max_keys;
302                 if (dtkey == NULL) {
303                         dtkey = (datum *)
304                             mallok((sizeof(datum) * max_keys));
305                 } else {
306                         dtkey = (datum *)
307                             reallok(dtkey, (sizeof(datum) * max_keys));
308                 }
309                 dtkey[max_keys - 1].dsize = 0;
310                 dtkey[max_keys - 1].dptr = NULL;
311         }
312
313         if (dtkey[CC->cs_pid].dptr != NULL) {
314                 phree(dtkey[CC->cs_pid].dptr);
315         }
316         dtkey[CC->cs_pid].dptr = NULL;
317         dtkey[CC->cs_pid].dsize = 0;
318 }
319
320
321 /* 
322  * Prepare for a sequential search of an entire database.  (In the gdbm model,
323  * we do this by keeping an array dtkey[] of "the next" key for each session
324  * that is open.  There is guaranteed to be no more than one traversal in
325  * progress per session at any given time.)
326  */
327 void cdb_rewind(int cdb)
328 {
329
330         while (max_keys <= CC->cs_pid) {
331                 ++max_keys;
332                 if (dtkey == NULL) {
333                         dtkey = (datum *)
334                             mallok((sizeof(datum) * max_keys));
335                 } else {
336                         dtkey = (datum *)
337                             reallok(dtkey, (sizeof(datum) * max_keys));
338                 }
339                 dtkey[max_keys - 1].dsize = 0;
340                 dtkey[max_keys - 1].dptr = NULL;
341         }
342
343         if (dtkey[CC->cs_pid].dptr != NULL) {
344                 phree(dtkey[CC->cs_pid].dptr);
345         }
346         begin_critical_section(S_DATABASE);
347         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
348         end_critical_section(S_DATABASE);
349 }
350
351
352 /*
353  * Fetch the next item in a sequential search.  Returns a pointer to a 
354  * cdbdata structure, or NULL if we've hit the end.
355  */
356 struct cdbdata *cdb_next_item(int cdb)
357 {
358         datum dret;
359         struct cdbdata *cdbret;
360         void *ptr = NULL;
361
362
363         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
364                 return NULL;
365         }
366         begin_critical_section(S_DATABASE);
367         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
368         end_critical_section(S_DATABASE);
369         if (dret.dptr == NULL) {        /* bad read */
370                 phree(dtkey[CC->cs_pid].dptr);
371                 return NULL;
372         }
373         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
374         cdbret->len = dret.dsize;
375         cdbret->ptr = dret.dptr;
376
377         ptr = dtkey[CC->cs_pid].dptr;
378         begin_critical_section(S_DATABASE);
379         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
380         end_critical_section(S_DATABASE);
381
382         if (ptr != NULL) {      /* Free the previous key. */
383                 free(ptr);
384         }
385
386         return (cdbret);
387 }
388
389
390 /*
391  * Truncate (delete every record)
392  */
393 void cdb_trunc(int cdb) {
394         datum key;
395
396         begin_critical_section(S_DATABASE);
397         key = gdbm_firstkey ( dbf );
398         while (key = gdbm_firstkey(gdbms[cdb], key.dptr != NULL) {
399                 gdbm_delete(gdbms[cdb], key);
400         }
401         end_critical_section(S_DATABASE);
402 }
403
404
405
406 /*
407  * empty functions because GDBM doesn't have transaction support
408  */
409
410 void cdb_begin_transaction(void) {
411 }
412
413 void cdb_end_transaction(void) {
414 }
415
416 void cdb_allocate_tsd(void) {
417 }
418
419 void cdb_free_tsd(void) {
420 }
421
422 void cdb_check_handles(void) {
423 }