Defined user/room relationships
[citadel.git] / citadel / database.c
1 /*
2  * This file contains a set of abstractions that allow Citadel to plug into any
3  * record manager or database system for its data store.
4  */
5
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <pthread.h>
15 #include <gdbm.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include "database.h"
19 #include "sysdep_decls.h"
20
21
22 /*
23  * This array holds one gdbm handle for each Citadel database.
24  */
25 GDBM_FILE gdbms[MAXCDB];
26
27 /*
28  * We also keep these around, for sequential searches... (one per 
29  * session.  Maybe there's a better way?)
30  */
31 #define MAXKEYS 256
32 datum dtkey[MAXKEYS];
33
34
35 /*
36  * Reclaim unused space in the databases.  We need to do each one of
37  * these discretely, rather than in a loop.
38  */
39 void defrag_databases(void) {
40
41         /* defrag the message base */
42         begin_critical_section(S_MSGMAIN);
43         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
44         end_critical_section(S_MSGMAIN);
45
46         /* defrag the user file, mailboxes, and user/room relationships */
47         begin_critical_section(S_USERSUPP);
48         gdbm_reorganize(gdbms[CDB_USERSUPP]);
49         gdbm_reorganize(gdbms[CDB_MAILBOXES]);
50         gdbm_reorganize(gdbms[CDB_VISIT]);
51         end_critical_section(S_USERSUPP);
52
53         /* defrag the room files and message lists */
54         begin_critical_section(S_QUICKROOM);
55         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
56         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
57         end_critical_section(S_QUICKROOM);
58
59         /* defrag the floor table */
60         begin_critical_section(S_FLOORTAB);
61         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
62         end_critical_section(S_FLOORTAB);
63         }
64
65
66 /*
67  * Open the various gdbm databases we'll be using.  Any database which
68  * does not exist should be created.
69  */
70 void open_databases(void) {
71         int a;
72
73         /*
74          * Silently try to create the database subdirectory.  If it's
75          * already there, no problem.
76          */
77         system("exec mkdir data 2>/dev/null");
78
79         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
80                 GDBM_WRCREAT, 0600, NULL);
81         if (gdbms[CDB_MSGMAIN] == NULL) {
82                 lprintf(2, "Cannot open msgmain: %s\n",
83                         gdbm_strerror(gdbm_errno));
84                 }
85
86         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
87                 GDBM_WRCREAT, 0600, NULL);
88         if (gdbms[CDB_USERSUPP] == NULL) {
89                 lprintf(2, "Cannot open usersupp: %s\n",
90                         gdbm_strerror(gdbm_errno));
91                 }
92
93         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
94                 GDBM_WRCREAT, 0600, NULL);
95         if (gdbms[CDB_VISIT] == NULL) {
96                 lprintf(2, "Cannot open visit file: %s\n",
97                         gdbm_strerror(gdbm_errno));
98                 }
99
100         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
101                 GDBM_WRCREAT, 0600, NULL);
102         if (gdbms[CDB_QUICKROOM] == NULL) {
103                 lprintf(2, "Cannot open quickroom: %s\n",
104                         gdbm_strerror(gdbm_errno));
105                 }
106
107         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
108                 GDBM_WRCREAT, 0600, NULL);
109         if (gdbms[CDB_FLOORTAB] == NULL) {
110                 lprintf(2, "Cannot open floortab: %s\n",
111                         gdbm_strerror(gdbm_errno));
112                 }
113
114         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
115                 GDBM_WRCREAT, 0600, NULL);
116         if (gdbms[CDB_MSGLISTS] == NULL) {
117                 lprintf(2, "Cannot open msglists: %s\n",
118                         gdbm_strerror(gdbm_errno));
119                 }
120
121         gdbms[CDB_MAILBOXES] = gdbm_open("data/mailboxes.gdbm", 0,
122                 GDBM_WRCREAT, 0600, NULL);
123         if (gdbms[CDB_MAILBOXES] == NULL) {
124                 lprintf(2, "Cannot open mailboxes: %s\n",
125                         gdbm_strerror(gdbm_errno));
126                 }
127
128         for (a=0; a<MAXKEYS; ++a) {
129                 dtkey[a].dsize = 0;
130                 dtkey[a].dptr = NULL;
131                 }
132
133         }
134
135
136 /*
137  * Close all of the gdbm database files we've opened.  This can be done
138  * in a loop, since it's just a bunch of closes.
139  */
140 void close_databases(void) {
141         int a;
142
143         defrag_databases();
144         for (a=0; a<MAXCDB; ++a) {
145                 lprintf(7, "Closing database %d\n", a);
146                 gdbm_close(gdbms[a]);
147                 }
148
149         for (a=0; a<MAXKEYS; ++a) {
150                 if (dtkey[a].dptr != NULL) {
151                         free(dtkey[a].dptr);
152                         }
153                 }
154
155         }
156
157
158 /*
159  * Store a piece of data.  Returns 0 if the operation was successful.  If a
160  * datum already exists it should be overwritten.
161  */
162 int cdb_store(int cdb,
163                 void *key, int keylen,
164                 void *data, int datalen) {
165
166         datum dkey, ddata;
167
168         dkey.dsize = keylen;
169         dkey.dptr = key;
170         ddata.dsize = datalen;
171         ddata.dptr = data;
172
173         if ( gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE) < 0 ) {
174                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
175                 return(-1);
176                 }
177
178         return(0);
179         }
180
181
182 /*
183  * Delete a piece of data.  Returns 0 if the operation was successful.
184  */
185 int cdb_delete(int cdb, void *key, int keylen) {
186
187         datum dkey;
188
189         dkey.dsize = keylen;
190         dkey.dptr = key;
191
192         return(gdbm_delete(gdbms[cdb], dkey));
193
194         }
195
196
197
198
199 /*
200  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
201  * a struct cdbdata which it is the caller's responsibility to free later on
202  * using the cdb_free() routine.
203  */
204 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
205         
206         struct cdbdata *tempcdb;
207         datum dkey, dret;
208         
209         dkey.dsize = keylen;
210         dkey.dptr = key;
211
212         dret = gdbm_fetch(gdbms[cdb], dkey);
213         if (dret.dptr == NULL) {
214                 return NULL;
215                 }
216
217         tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
218         if (tempcdb == NULL) {
219                 lprintf(2, "Cannot allocate memory!\n");
220                 }
221
222         tempcdb->len = dret.dsize;
223         tempcdb->ptr = dret.dptr;
224         return(tempcdb);
225         }
226
227
228 /*
229  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
230  * more complex stuff with other database managers in the future).
231  */
232 void cdb_free(struct cdbdata *cdb) {
233         free(cdb->ptr);
234         free(cdb);
235         }
236
237
238 /* 
239  * Prepare for a sequential search of an entire database.  (In the gdbm model,
240  * we do this by keeping an array dtkey[] of "the next" key for each session
241  * that is open.  There is guaranteed to be no more than one traversal in
242  * progress per session at any given time.)
243  */
244 void cdb_rewind(int cdb) {
245
246         if (dtkey[CC->cs_pid].dptr != NULL) {
247                 free(dtkey[CC->cs_pid].dptr);
248                 }
249
250         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
251         }
252
253
254 /*
255  * Fetch the next item in a sequential search.  Returns a pointer to a 
256  * cdbdata structure, or NULL if we've hit the end.
257  */
258 struct cdbdata *cdb_next_item(int cdb) {
259         datum dret;
260         struct cdbdata *cdbret;
261
262         
263         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
264                 return NULL;
265                 }
266
267         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
268         if (dret.dptr == NULL) {        /* bad read */
269                 free(dtkey[CC->cs_pid].dptr);
270                 return NULL;
271                 }
272
273         cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
274         cdbret->len = dret.dsize;
275         cdbret->ptr = dret.dptr;
276
277         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
278         return(cdbret);
279         }