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.
19 #include "sysdep_decls.h"
23 * This array holds one gdbm handle for each Citadel database.
25 GDBM_FILE gdbms[MAXCDB];
28 * We also keep these around, for sequential searches... (one per
29 * session. Maybe there's a better way?)
36 * Reclaim unused space in the databases. We need to do each one of
37 * these discretely, rather than in a loop.
39 void defrag_databases(void) {
41 /* defrag the message base */
42 begin_critical_section(S_MSGMAIN);
43 gdbm_reorganize(gdbms[CDB_MSGMAIN]);
44 end_critical_section(S_MSGMAIN);
46 /* defrag the user file and mailboxes */
47 begin_critical_section(S_USERSUPP);
48 gdbm_reorganize(gdbms[CDB_USERSUPP]);
49 gdbm_reorganize(gdbms[CDB_MAILBOXES]);
50 end_critical_section(S_USERSUPP);
52 /* defrag the room files and message lists */
53 begin_critical_section(S_QUICKROOM);
54 gdbm_reorganize(gdbms[CDB_QUICKROOM]);
55 gdbm_reorganize(gdbms[CDB_MSGLISTS]);
56 end_critical_section(S_QUICKROOM);
58 /* defrag the floor table */
59 begin_critical_section(S_FLOORTAB);
60 gdbm_reorganize(gdbms[CDB_FLOORTAB]);
61 end_critical_section(S_FLOORTAB);
66 * Open the various gdbm databases we'll be using. Any database which
67 * does not exist should be created.
69 void open_databases(void) {
72 gdbms[CDB_MSGMAIN] = gdbm_open("msgmain.gdbm", 8192,
73 GDBM_WRCREAT, 0600, NULL);
74 if (gdbms[CDB_MSGMAIN] == NULL) {
75 lprintf(2, "Cannot open msgmain: %s\n",
76 gdbm_strerror(gdbm_errno));
79 gdbms[CDB_USERSUPP] = gdbm_open("usersupp.gdbm", 0,
80 GDBM_WRCREAT, 0600, NULL);
81 if (gdbms[CDB_USERSUPP] == NULL) {
82 lprintf(2, "Cannot open usersupp: %s\n",
83 gdbm_strerror(gdbm_errno));
86 gdbms[CDB_QUICKROOM] = gdbm_open("quickroom.gdbm", 0,
87 GDBM_WRCREAT, 0600, NULL);
88 if (gdbms[CDB_QUICKROOM] == NULL) {
89 lprintf(2, "Cannot open quickroom: %s\n",
90 gdbm_strerror(gdbm_errno));
93 gdbms[CDB_FLOORTAB] = gdbm_open("floortab.gdbm", 0,
94 GDBM_WRCREAT, 0600, NULL);
95 if (gdbms[CDB_FLOORTAB] == NULL) {
96 lprintf(2, "Cannot open floortab: %s\n",
97 gdbm_strerror(gdbm_errno));
100 gdbms[CDB_MSGLISTS] = gdbm_open("msglists.gdbm", 0,
101 GDBM_WRCREAT, 0600, NULL);
102 if (gdbms[CDB_MSGLISTS] == NULL) {
103 lprintf(2, "Cannot open msglists: %s\n",
104 gdbm_strerror(gdbm_errno));
107 gdbms[CDB_MAILBOXES] = gdbm_open("mailboxes.gdbm", 0,
108 GDBM_WRCREAT, 0600, NULL);
109 if (gdbms[CDB_MAILBOXES] == NULL) {
110 lprintf(2, "Cannot open mailboxes: %s\n",
111 gdbm_strerror(gdbm_errno));
114 for (a=0; a<MAXKEYS; ++a) {
116 dtkey[a].dptr = NULL;
123 * Close all of the gdbm database files we've opened. This can be done
124 * in a loop, since it's just a bunch of closes.
126 void close_databases(void) {
130 for (a=0; a<MAXCDB; ++a) {
131 lprintf(7, "Closing database %d\n", a);
132 gdbm_close(gdbms[a]);
135 for (a=0; a<MAXKEYS; ++a) {
136 if (dtkey[a].dptr != NULL) {
145 * Store a piece of data. Returns 0 if the operation was successful. If a
146 * datum already exists it should be overwritten.
148 int cdb_store(int cdb,
149 void *key, int keylen,
150 void *data, int datalen) {
156 ddata.dsize = datalen;
159 if ( gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE) < 0 ) {
160 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
169 * Delete a piece of data. Returns 0 if the operation was successful.
171 int cdb_delete(int cdb, void *key, int keylen) {
178 return(gdbm_delete(gdbms[cdb], dkey));
186 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
187 * a struct cdbdata which it is the caller's responsibility to free later on
188 * using the cdb_free() routine.
190 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
192 struct cdbdata *tempcdb;
198 dret = gdbm_fetch(gdbms[cdb], dkey);
199 if (dret.dptr == NULL) {
203 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
204 if (tempcdb == NULL) {
205 lprintf(2, "Cannot allocate memory!\n");
208 tempcdb->len = dret.dsize;
209 tempcdb->ptr = dret.dptr;
215 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
216 * more complex stuff with other database managers in the future).
218 void cdb_free(struct cdbdata *cdb) {
225 * Prepare for a sequential search of an entire database. (In the gdbm model,
226 * we do this by keeping an array dtkey[] of "the next" key for each session
227 * that is open. There is guaranteed to be no more than one traversal in
228 * progress per session at any given time.)
230 void cdb_rewind(int cdb) {
232 if (dtkey[CC->cs_pid].dptr != NULL) {
233 free(dtkey[CC->cs_pid].dptr);
236 dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
241 * Fetch the next item in a sequential search. Returns a pointer to a
242 * cdbdata structure, or NULL if we've hit the end.
244 struct cdbdata *cdb_next_item(int cdb) {
246 struct cdbdata *cdbret;
249 if (dtkey[CC->cs_pid].dptr == NULL) { /* end of file */
253 dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
254 if (dret.dptr == NULL) { /* bad read */
255 free(dtkey[CC->cs_pid].dptr);
259 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
260 cdbret->len = dret.dsize;
261 cdbret->ptr = dret.dptr;
263 dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);