* Fixed a small bug in the GDBM backend (deprecated, but the bug was very
[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         /* defrag the use table */
106         lprintf(7, "Defragmenting the use table\n");
107         gdbm_reorganize(gdbms[CDB_USETABLE]);
108 }
109
110
111 /*
112  * Open the various gdbm databases we'll be using.  Any database which
113  * does not exist should be created.
114  */
115 void open_databases(void)
116 {
117         lprintf(7, "%s\n", gdbm_version);
118
119         /*
120          * Silently try to create the database subdirectory.  If it's
121          * already there, no problem.
122          */
123         system("exec mkdir data 2>/dev/null");
124
125         /* a critical section is unnecessary, as this function is called before
126            any other threads are created. and it causes problems on BSDI.
127
128            begin_critical_section(S_DATABASE);
129
130          */
131
132         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
133                                        GDBM_WRCREAT, 0600, NULL);
134         if (gdbms[CDB_MSGMAIN] == NULL) {
135                 lprintf(2, "Cannot open msgmain: %s\n",
136                         gdbm_strerror(gdbm_errno));
137                 exit(1);
138         }
139         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
140                                         GDBM_WRCREAT, 0600, NULL);
141         if (gdbms[CDB_USERSUPP] == NULL) {
142                 lprintf(2, "Cannot open usersupp: %s\n",
143                         gdbm_strerror(gdbm_errno));
144                 exit(1);
145         }
146         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
147                                      GDBM_WRCREAT, 0600, NULL);
148         if (gdbms[CDB_VISIT] == NULL) {
149                 lprintf(2, "Cannot open visit file: %s\n",
150                         gdbm_strerror(gdbm_errno));
151                 exit(1);
152         }
153         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
154                                          GDBM_WRCREAT, 0600, NULL);
155         if (gdbms[CDB_QUICKROOM] == NULL) {
156                 lprintf(2, "Cannot open quickroom: %s\n",
157                         gdbm_strerror(gdbm_errno));
158                 exit(1);
159         }
160         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
161                                         GDBM_WRCREAT, 0600, NULL);
162         if (gdbms[CDB_FLOORTAB] == NULL) {
163                 lprintf(2, "Cannot open floortab: %s\n",
164                         gdbm_strerror(gdbm_errno));
165                 exit(1);
166         }
167         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
168                                         GDBM_WRCREAT, 0600, NULL);
169         if (gdbms[CDB_MSGLISTS] == NULL) {
170                 lprintf(2, "Cannot open msglists: %s\n",
171                         gdbm_strerror(gdbm_errno));
172                 exit(1);
173         }
174         gdbms[CDB_DIRECTORY] = gdbm_open("data/directory.gdbm", 0,
175                                         GDBM_WRCREAT, 0600, NULL);
176         if (gdbms[CDB_DIRECTORY] == NULL) {
177                 lprintf(2, "Cannot open directory: %s\n",
178                         gdbm_strerror(gdbm_errno));
179                 exit(1);
180         }
181         gdbms[CDB_USETABLE] = gdbm_open("data/usetable.gdbm", 0,
182                                         GDBM_WRCREAT, 0600, NULL);
183         if (gdbms[CDB_USETABLE] == NULL) {
184                 lprintf(2, "Cannot open use table: %s\n",
185                         gdbm_strerror(gdbm_errno));
186                 exit(1);
187         }
188
189         /*
190            end_critical_section(S_DATABASE);
191          */
192
193 }
194
195
196 /*
197  * Close all of the gdbm database files we've opened.  This can be done
198  * in a loop, since it's just a bunch of closes.
199  */
200 void close_databases(void)
201 {
202         int a;
203
204         begin_critical_section(S_DATABASE);
205         for (a = 0; a < MAXCDB; ++a) {
206                 lprintf(7, "Closing database %d\n", a);
207                 gdbm_close(gdbms[a]);
208         }
209         end_critical_section(S_DATABASE);
210
211         for (a = 0; a < max_keys; ++a) {
212                 if (dtkey[a].dptr != NULL) {
213                         phree(dtkey[a].dptr);
214                 }
215         }
216
217 }
218
219
220 /*
221  * Store a piece of data.  Returns 0 if the operation was successful.  If a
222  * datum already exists it should be overwritten.
223  */
224 int cdb_store(int cdb,
225               void *key, int keylen,
226               void *data, int datalen)
227 {
228
229         datum dkey, ddata;
230         int retval;
231
232         dkey.dsize = keylen;
233         dkey.dptr = key;
234         ddata.dsize = datalen;
235         ddata.dptr = data;
236
237         begin_critical_section(S_DATABASE);
238         retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
239         end_critical_section(S_DATABASE);
240         if (retval < 0) {
241                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
242                 return (-1);
243         }
244         return (0);
245 }
246
247
248 /*
249  * Delete a piece of data.  Returns 0 if the operation was successful.
250  */
251 int cdb_delete(int cdb, void *key, int keylen)
252 {
253
254         datum dkey;
255         int retval;
256
257         dkey.dsize = keylen;
258         dkey.dptr = key;
259
260         begin_critical_section(S_DATABASE);
261         retval = gdbm_delete(gdbms[cdb], dkey);
262         end_critical_section(S_DATABASE);
263         return (retval);
264
265 }
266
267
268
269
270 /*
271  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
272  * a struct cdbdata which it is the caller's responsibility to free later on
273  * using the cdb_free() routine.
274  */
275 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
276 {
277
278         struct cdbdata *tempcdb;
279         datum dkey, dret;
280
281         dkey.dsize = keylen;
282         dkey.dptr = key;
283
284         begin_critical_section(S_DATABASE);
285         dret = gdbm_fetch(gdbms[cdb], dkey);
286         end_critical_section(S_DATABASE);
287         if (dret.dptr == NULL) {
288                 return NULL;
289         }
290         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
291         if (tempcdb == NULL) {
292                 lprintf(2, "Cannot allocate memory!\n");
293         }
294         tempcdb->len = dret.dsize;
295         tempcdb->ptr = dret.dptr;
296         return (tempcdb);
297 }
298
299
300 /*
301  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
302  * more complex stuff with other database managers in the future).
303  */
304 void cdb_free(struct cdbdata *cdb)
305 {
306         phree(cdb->ptr);
307         phree(cdb);
308 }
309
310 void cdb_close_cursor(cdb)
311 {
312         while (max_keys <= CC->cs_pid) {
313                 ++max_keys;
314                 if (dtkey == NULL) {
315                         dtkey = (datum *)
316                             mallok((sizeof(datum) * max_keys));
317                 } else {
318                         dtkey = (datum *)
319                             reallok(dtkey, (sizeof(datum) * max_keys));
320                 }
321                 dtkey[max_keys - 1].dsize = 0;
322                 dtkey[max_keys - 1].dptr = NULL;
323         }
324
325         if (dtkey[CC->cs_pid].dptr != NULL) {
326                 phree(dtkey[CC->cs_pid].dptr);
327         }
328         dtkey[CC->cs_pid].dptr = NULL;
329         dtkey[CC->cs_pid].dsize = 0;
330 }
331
332
333 /* 
334  * Prepare for a sequential search of an entire database.  (In the gdbm model,
335  * we do this by keeping an array dtkey[] of "the next" key for each session
336  * that is open.  There is guaranteed to be no more than one traversal in
337  * progress per session at any given time.)
338  */
339 void cdb_rewind(int cdb)
340 {
341
342         while (max_keys <= CC->cs_pid) {
343                 ++max_keys;
344                 if (dtkey == NULL) {
345                         dtkey = (datum *)
346                             mallok((sizeof(datum) * max_keys));
347                 } else {
348                         dtkey = (datum *)
349                             reallok(dtkey, (sizeof(datum) * max_keys));
350                 }
351                 dtkey[max_keys - 1].dsize = 0;
352                 dtkey[max_keys - 1].dptr = NULL;
353         }
354
355         if (dtkey[CC->cs_pid].dptr != NULL) {
356                 phree(dtkey[CC->cs_pid].dptr);
357         }
358         begin_critical_section(S_DATABASE);
359         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
360         end_critical_section(S_DATABASE);
361 }
362
363
364 /*
365  * Fetch the next item in a sequential search.  Returns a pointer to a 
366  * cdbdata structure, or NULL if we've hit the end.
367  */
368 struct cdbdata *cdb_next_item(int cdb)
369 {
370         datum dret;
371         struct cdbdata *cdbret;
372         void *ptr = NULL;
373
374
375         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
376                 return NULL;
377         }
378         begin_critical_section(S_DATABASE);
379         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
380         end_critical_section(S_DATABASE);
381         if (dret.dptr == NULL) {        /* bad read */
382                 phree(dtkey[CC->cs_pid].dptr);
383                 return NULL;
384         }
385         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
386         cdbret->len = dret.dsize;
387         cdbret->ptr = dret.dptr;
388
389         ptr = dtkey[CC->cs_pid].dptr;
390         begin_critical_section(S_DATABASE);
391         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
392         end_critical_section(S_DATABASE);
393
394         if (ptr != NULL) {      /* Free the previous key. */
395                 free(ptr);
396         }
397
398         return (cdbret);
399 }
400
401
402 /*
403  * Truncate (delete every record)
404  */
405 void cdb_trunc(int cdb) {
406         datum key;
407
408         begin_critical_section(S_DATABASE);
409         key = gdbm_firstkey (gdbms[cdb]);
410         while (key = gdbm_firstkey(gdbms[cdb], key.dptr) {
411                 gdbm_delete(gdbms[cdb], key);
412         }
413         end_critical_section(S_DATABASE);
414 }
415
416
417
418 /*
419  * empty functions because GDBM doesn't have transaction support
420  */
421
422 void cdb_begin_transaction(void) {
423 }
424
425 void cdb_end_transaction(void) {
426 }
427
428 void cdb_allocate_tsd(void) {
429 }
430
431 void cdb_free_tsd(void) {
432 }
433
434 void cdb_check_handles(void) {
435 }