* Removed the transaction stuff (but left the log in). It wasn't working.
[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 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <gdbm.h>
27 #include "citadel.h"
28 #include "server.h"
29 #include "database.h"
30 #include "sysdep_decls.h"
31
32
33 /*
34  * This array holds one gdbm handle for each Citadel database.
35  */
36 GDBM_FILE gdbms[MAXCDB];
37
38 /*
39  * We also keep these around, for sequential searches (one per session slot)
40  */
41 int max_keys = 0;
42 datum *dtkey;
43
44
45 /*
46  * Reclaim unused space in the databases.  We need to do each one of
47  * these discretely, rather than in a loop.
48  */
49 void defrag_databases(void)
50 {
51
52         /* defrag the message base */
53         lprintf(7, "Defragmenting message base\n");
54         begin_critical_section(S_MSGMAIN);
55         begin_critical_section(S_DATABASE);
56         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
57         end_critical_section(S_DATABASE);
58         end_critical_section(S_MSGMAIN);
59
60         /* defrag the user file, mailboxes, and user/room relationships */
61         lprintf(7, "Defragmenting user file\n");
62         begin_critical_section(S_USERSUPP);
63         begin_critical_section(S_DATABASE);
64         gdbm_reorganize(gdbms[CDB_USERSUPP]);
65         gdbm_reorganize(gdbms[CDB_VISIT]);
66         end_critical_section(S_DATABASE);
67         end_critical_section(S_USERSUPP);
68
69         /* defrag the room files and message lists */
70         lprintf(7, "Defragmenting room files and message lists\n");
71         begin_critical_section(S_QUICKROOM);
72         begin_critical_section(S_DATABASE);
73         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
74         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
75         end_critical_section(S_DATABASE);
76         end_critical_section(S_QUICKROOM);
77
78         /* defrag the floor table */
79         lprintf(7, "Defragmenting floor table\n");
80         begin_critical_section(S_FLOORTAB);
81         begin_critical_section(S_DATABASE);
82         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
83         end_critical_section(S_DATABASE);
84         end_critical_section(S_FLOORTAB);
85 }
86
87
88 /*
89  * Open the various gdbm databases we'll be using.  Any database which
90  * does not exist should be created.
91  */
92 void open_databases(void)
93 {
94         lprintf(7, "%s\n", gdbm_version);
95
96         /*
97          * Silently try to create the database subdirectory.  If it's
98          * already there, no problem.
99          */
100         system("exec mkdir data 2>/dev/null");
101
102         /* a critical section is unnecessary, as this function is called before
103            any other threads are created. and it causes problems on BSDI.
104
105            begin_critical_section(S_DATABASE);
106
107          */
108
109         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
110                                        GDBM_WRCREAT, 0600, NULL);
111         if (gdbms[CDB_MSGMAIN] == NULL) {
112                 lprintf(2, "Cannot open msgmain: %s\n",
113                         gdbm_strerror(gdbm_errno));
114                 exit(1);
115         }
116         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
117                                         GDBM_WRCREAT, 0600, NULL);
118         if (gdbms[CDB_USERSUPP] == NULL) {
119                 lprintf(2, "Cannot open usersupp: %s\n",
120                         gdbm_strerror(gdbm_errno));
121                 exit(1);
122         }
123         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
124                                      GDBM_WRCREAT, 0600, NULL);
125         if (gdbms[CDB_VISIT] == NULL) {
126                 lprintf(2, "Cannot open visit file: %s\n",
127                         gdbm_strerror(gdbm_errno));
128                 exit(1);
129         }
130         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
131                                          GDBM_WRCREAT, 0600, NULL);
132         if (gdbms[CDB_QUICKROOM] == NULL) {
133                 lprintf(2, "Cannot open quickroom: %s\n",
134                         gdbm_strerror(gdbm_errno));
135                 exit(1);
136         }
137         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
138                                         GDBM_WRCREAT, 0600, NULL);
139         if (gdbms[CDB_FLOORTAB] == NULL) {
140                 lprintf(2, "Cannot open floortab: %s\n",
141                         gdbm_strerror(gdbm_errno));
142                 exit(1);
143         }
144         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
145                                         GDBM_WRCREAT, 0600, NULL);
146         if (gdbms[CDB_MSGLISTS] == NULL) {
147                 lprintf(2, "Cannot open msglists: %s\n",
148                         gdbm_strerror(gdbm_errno));
149                 exit(1);
150         }
151         /*
152            end_critical_section(S_DATABASE);
153          */
154
155 }
156
157
158 /*
159  * Close all of the gdbm database files we've opened.  This can be done
160  * in a loop, since it's just a bunch of closes.
161  */
162 void close_databases(void)
163 {
164         int a;
165
166         begin_critical_section(S_DATABASE);
167         for (a = 0; a < MAXCDB; ++a) {
168                 lprintf(7, "Closing database %d\n", a);
169                 gdbm_close(gdbms[a]);
170         }
171         end_critical_section(S_DATABASE);
172
173         for (a = 0; a < max_keys; ++a) {
174                 if (dtkey[a].dptr != NULL) {
175                         phree(dtkey[a].dptr);
176                 }
177         }
178
179 }
180
181
182 /*
183  * Store a piece of data.  Returns 0 if the operation was successful.  If a
184  * datum already exists it should be overwritten.
185  */
186 int cdb_store(int cdb,
187               void *key, int keylen,
188               void *data, int datalen)
189 {
190
191         datum dkey, ddata;
192         int retval;
193
194         dkey.dsize = keylen;
195         dkey.dptr = key;
196         ddata.dsize = datalen;
197         ddata.dptr = data;
198
199         begin_critical_section(S_DATABASE);
200         retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
201         end_critical_section(S_DATABASE);
202         if (retval < 0) {
203                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
204                 return (-1);
205         }
206         return (0);
207 }
208
209
210 /*
211  * Delete a piece of data.  Returns 0 if the operation was successful.
212  */
213 int cdb_delete(int cdb, void *key, int keylen)
214 {
215
216         datum dkey;
217         int retval;
218
219         dkey.dsize = keylen;
220         dkey.dptr = key;
221
222         begin_critical_section(S_DATABASE);
223         retval = gdbm_delete(gdbms[cdb], dkey);
224         end_critical_section(S_DATABASE);
225         return (retval);
226
227 }
228
229
230
231
232 /*
233  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
234  * a struct cdbdata which it is the caller's responsibility to free later on
235  * using the cdb_free() routine.
236  */
237 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
238 {
239
240         struct cdbdata *tempcdb;
241         datum dkey, dret;
242
243         dkey.dsize = keylen;
244         dkey.dptr = key;
245
246         begin_critical_section(S_DATABASE);
247         dret = gdbm_fetch(gdbms[cdb], dkey);
248         end_critical_section(S_DATABASE);
249         if (dret.dptr == NULL) {
250                 return NULL;
251         }
252         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
253         if (tempcdb == NULL) {
254                 lprintf(2, "Cannot allocate memory!\n");
255         }
256         tempcdb->len = dret.dsize;
257         tempcdb->ptr = dret.dptr;
258         return (tempcdb);
259 }
260
261
262 /*
263  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
264  * more complex stuff with other database managers in the future).
265  */
266 void cdb_free(struct cdbdata *cdb)
267 {
268         phree(cdb->ptr);
269         phree(cdb);
270 }
271
272
273 /* 
274  * Prepare for a sequential search of an entire database.  (In the gdbm model,
275  * we do this by keeping an array dtkey[] of "the next" key for each session
276  * that is open.  There is guaranteed to be no more than one traversal in
277  * progress per session at any given time.)
278  */
279 void cdb_rewind(int cdb)
280 {
281
282         while (max_keys <= CC->cs_pid) {
283                 ++max_keys;
284                 if (dtkey == NULL) {
285                         dtkey = (datum *)
286                             mallok((sizeof(datum) * max_keys));
287                 } else {
288                         dtkey = (datum *)
289                             reallok(dtkey, (sizeof(datum) * max_keys));
290                 }
291                 dtkey[max_keys - 1].dsize = 0;
292                 dtkey[max_keys - 1].dptr = NULL;
293         }
294
295         if (dtkey[CC->cs_pid].dptr != NULL) {
296                 phree(dtkey[CC->cs_pid].dptr);
297         }
298         begin_critical_section(S_DATABASE);
299         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
300         end_critical_section(S_DATABASE);
301 }
302
303
304 /*
305  * Fetch the next item in a sequential search.  Returns a pointer to a 
306  * cdbdata structure, or NULL if we've hit the end.
307  */
308 struct cdbdata *cdb_next_item(int cdb)
309 {
310         datum dret;
311         struct cdbdata *cdbret;
312         void *ptr = NULL;
313
314
315         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
316                 return NULL;
317         }
318         begin_critical_section(S_DATABASE);
319         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
320         end_critical_section(S_DATABASE);
321         if (dret.dptr == NULL) {        /* bad read */
322                 phree(dtkey[CC->cs_pid].dptr);
323                 return NULL;
324         }
325         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
326         cdbret->len = dret.dsize;
327         cdbret->ptr = dret.dptr;
328
329         ptr = dtkey[CC->cs_pid].dptr;
330         begin_critical_section(S_DATABASE);
331         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
332         end_critical_section(S_DATABASE);
333
334         if (ptr != NULL) {      /* Free the previous key. */
335                 free(ptr);
336         }
337
338         return (cdbret);
339 }
340