budb: Only have one build rule for budb_errs.c
[openafs.git] / src / budb / database.c
index aa6216a..94045e2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -10,8 +10,7 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
@@ -21,42 +20,32 @@ RCSID
 #include <sys/types.h>
 #include <afs/stds.h>
 #include <ubik.h>
-#include <afs/auth.h>
 #include <afs/bubasics.h>
 #include "budb_errs.h"
 #include "database.h"
 #include "error_macros.h"
+#include "budb_internal.h"
 #include "afs/audit.h"
-
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
 
 int pollCount;
 struct memoryDB db;            /* really allocate it here */
 
 void
-db_panic(reason)
-     char *reason;
+db_panic(char *reason)
 {
     LogError(0, "db_panic: %s\n", reason);
     BUDB_EXIT(-1);
 }
 
 afs_int32
-InitDB()
+InitDB(void)
 {
     afs_int32 code;
 
     pollCount = 0;
 
     memset(&db, 0, sizeof(db));
-    Lock_Init(&db.lock);
     if ((code = InitDBalloc()) || (code = InitDBhash()))
        return code;
     return 0;
@@ -64,7 +53,7 @@ InitDB()
 
 /* package up seek and write into one procedure for ease of use */
 
-/* dbwrite 
+/* dbwrite
  *     write a portion of the database
  * entry:
  *     pos - offset into the database (disk address). If this is in the
@@ -98,7 +87,9 @@ dbwrite(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
 
   error_exit:
     if (((++pollCount) % 4) == 0) {    /* Poll every 4 reads/writes */
+#ifndef AFS_PTHREAD_ENV
        IOMGR_Poll();
+#endif
        pollCount = 0;
     }
     return code;
@@ -123,14 +114,16 @@ dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
     }
     code = ubik_Read(ut, buff, len);
     if (code) {
-       LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
-                buff, len);
+       LogError(code, "dbread: ubik_Read pos %d, buff %"AFS_PTR_FMT
+                ", len %d\n", pos, buff, len);
        ERROR(code);
     }
 
   error_exit:
     if (((++pollCount) % 4) == 0) {    /* Poll every 4 reads/writes */
+#ifndef AFS_PTHREAD_ENV
        IOMGR_Poll();
+#endif
        pollCount = 0;
     }
     return code;
@@ -156,14 +149,16 @@ cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 le
     }
     code = ubik_Read(ut, buff, len);
     if (code) {
-       LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
-                buff, len);
+       LogError(code, "cdbread: ubik_Read pos 0x%x, buff %"AFS_PTR_FMT
+                ", len %d\n", pos, buff, len);
        ERROR(code);
     }
 
   error_exit:
     if (((++pollCount) % 4) == 0) {    /* Poll every 4 reads/writes */
+#ifndef AFS_PTHREAD_ENV
        IOMGR_Poll();
+#endif
        pollCount = 0;
     }
     return code;
@@ -172,18 +167,22 @@ cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 le
 /* check that the database has been initialized.  Be careful to fail in a safe
    manner, to avoid bogusly reinitializing the db.  */
 
-afs_int32
-CheckInit(ut, db_init)
-     struct ubik_trans *ut;
-     int (*db_init) ();                /* procedure to call if rebuilding DB */
+/**
+ * reads in db cache from ubik.
+ *
+ * @param[in] ut ubik transaction
+ * @param[in] rock  opaque pointer to an int (*) (struct ubik_trans *), which
+ *                  will be called on rebuilding the database (or NULL to not
+ *                  rebuild the db)
+ *
+ * @return operation status
+ *   @retval 0 success
+ */
+static afs_int32
+UpdateCache(struct ubik_trans *ut, void *rock)
 {
-    register afs_int32 code;
-
-    /* Don't read header if not necessary */
-    if (!ubik_CacheUpdate(ut))
-       return 0;
-
-    ObtainWriteLock(&db.lock);
+    int (*db_init) (struct ubik_trans *ut) = rock;
+    afs_int32 code;
 
     db.h.eofPtr = htonl(sizeof(db.h)); /* for sanity check in dbread */
     code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
@@ -208,7 +207,6 @@ CheckInit(ut, db_init)
     ht_Reset(&db.dumpIden);
 
   error_exit:
-    ReleaseWriteLock(&db.lock);
     if (code) {
        if ((code == UEOF) || (code == BUDB_EMPTY)) {
            if (db_init) {
@@ -240,3 +238,10 @@ CheckInit(ut, db_init)
     }
     return code;
 }
+
+afs_int32
+CheckInit(struct ubik_trans *ut,
+         int (*db_init) (struct ubik_trans *ut)) /* call if rebuilding DB */
+{
+    return ubik_CheckCache(ut, UpdateCache, db_init);
+}