]> code.citadel.org Git - citadel.git/blobdiff - citadel/database.c
* Applied a patch sent in by Clint Adams <schizo@debian.org> to handle
[citadel.git] / citadel / database.c
index d562e07bcdfc7179eb7220262f54eb9d59b2e05d..934cd69e13c315596d89eb61988ce25004c7a6cd 100644 (file)
  * conditions to occur.  (Deadlock is bad.  Eliminate.)
  */
 
+#ifdef DLL_EXPORT
+#define IN_LIBCIT
+#endif
+
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
@@ -80,6 +95,16 @@ void defrag_databases(void)
        gdbm_reorganize(gdbms[CDB_FLOORTAB]);
        end_critical_section(S_DATABASE);
        end_critical_section(S_FLOORTAB);
+
+       /* defrag the directory */
+       lprintf(7, "Defragmenting the directory\n");
+       begin_critical_section(S_DIRECTORY);
+       gdbm_reorganize(gdbms[CDB_DIRECTORY]);
+       end_critical_section(S_DIRECTORY);
+
+       /* defrag the use table */
+       lprintf(7, "Defragmenting the use table\n");
+       gdbm_reorganize(gdbms[CDB_USETABLE]);
 }
 
 
@@ -146,6 +171,21 @@ void open_databases(void)
                        gdbm_strerror(gdbm_errno));
                exit(1);
        }
+       gdbms[CDB_DIRECTORY] = gdbm_open("data/directory.gdbm", 0,
+                                       GDBM_WRCREAT, 0600, NULL);
+       if (gdbms[CDB_DIRECTORY] == NULL) {
+               lprintf(2, "Cannot open directory: %s\n",
+                       gdbm_strerror(gdbm_errno));
+               exit(1);
+       }
+       gdbms[CDB_USETABLE] = gdbm_open("data/usetable.gdbm", 0,
+                                       GDBM_WRCREAT, 0600, NULL);
+       if (gdbms[CDB_USETABLE] == NULL) {
+               lprintf(2, "Cannot open use table: %s\n",
+                       gdbm_strerror(gdbm_errno));
+               exit(1);
+       }
+
        /*
           end_critical_section(S_DATABASE);
         */
@@ -267,6 +307,28 @@ void cdb_free(struct cdbdata *cdb)
        phree(cdb);
 }
 
+void cdb_close_cursor(cdb)
+{
+        while (max_keys <= CC->cs_pid) {
+                ++max_keys;
+                if (dtkey == NULL) {
+                        dtkey = (datum *)
+                            mallok((sizeof(datum) * max_keys));
+                } else {
+                        dtkey = (datum *)
+                            reallok(dtkey, (sizeof(datum) * max_keys));
+                }
+                dtkey[max_keys - 1].dsize = 0;
+                dtkey[max_keys - 1].dptr = NULL;
+        }
+
+        if (dtkey[CC->cs_pid].dptr != NULL) {
+                phree(dtkey[CC->cs_pid].dptr);
+        }
+       dtkey[CC->cs_pid].dptr = NULL;
+       dtkey[CC->cs_pid].dsize = 0;
+}
+
 
 /* 
  * Prepare for a sequential search of an entire database.  (In the gdbm model,
@@ -337,6 +399,22 @@ struct cdbdata *cdb_next_item(int cdb)
 }
 
 
+/*
+ * Truncate (delete every record)
+ */
+void cdb_trunc(int cdb) {
+       datum key;
+
+       begin_critical_section(S_DATABASE);
+       key = gdbm_firstkey (gdbms[cdb]);
+       while (key = gdbm_firstkey(gdbms[cdb], key.dptr) {
+               gdbm_delete(gdbms[cdb], key);
+       }
+       end_critical_section(S_DATABASE);
+}
+
+
+
 /*
  * empty functions because GDBM doesn't have transaction support
  */
@@ -353,5 +431,5 @@ void cdb_allocate_tsd(void) {
 void cdb_free_tsd(void) {
 }
 
-void cdb_release_handles(void) {
+void cdb_check_handles(void) {
 }