]> arthur.barton.de Git - netatalk.git/commitdiff
Get transaction support to work again against the new DB->associate() and
authorlenneis <lenneis>
Mon, 3 Nov 2003 20:56:59 +0000 (20:56 +0000)
committerlenneis <lenneis>
Mon, 3 Nov 2003 20:56:59 +0000 (20:56 +0000)
DB->open() interfaces of Berkeley DB 4.xx. dbif_stamp() needed to be wrapped
into a transaction as well.

etc/cnid_dbd/dbif.c
etc/cnid_dbd/dbif.h
etc/cnid_dbd/main.c

index 8f04d4ce9e4fbbfa1c6ec3b553515f5c072a6566..f4c349af1d4e02434c63238dae4521631219e59d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: dbif.c,v 1.1.4.5 2003-10-30 10:03:19 bfernhomberg Exp $
+ * $Id: dbif.c,v 1.1.4.6 2003-11-03 20:56:59 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -49,18 +49,77 @@ static struct db_table {
      { "didname.db",     NULL,      0, DB_HASH},
 };
 
-/*
- *  We assume our current directory is already the BDB homedir. Otherwise
- *  opening the databases will not work as expected.
- */
-
 extern int didname(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
 extern int devino(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
 
-static int env_init(struct db_param *dbp)
+/* --------------- */
+static int  db_compat_associate (DB *p, DB *s,
+                   int (*callback)(DB *, const DBT *,const DBT *, DBT *),
+                   u_int32_t flags)
+{
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    return p->associate(p, db_txn, s, callback, flags);
+#else
+#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
+    return p->associate(p,       s, callback, flags);
+#else
+    return 0;
+#endif
+#endif
+}
+
+/* --------------- */
+static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
+{
+    int ret;
+
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    ret = db->open(db, db_txn, file, name, type, DB_CREATE, mode); 
+#else
+    ret = db->open(db,       file, name, type, DB_CREATE, mode); 
+#endif
+
+    if (ret) {
+        LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
+/* --------------- */
+int dbif_stamp(void *buffer, int size)
+{
+    struct stat st;
+    int         rc;
+
+    if (size < 8)
+        return -1;
+
+    if ((rc = stat(db_table[0].name, &st)) < 0) {
+        LOG(log_error, logtype_cnid, "error stating database %s: %s", db_table[0].name, db_strerror(rc));
+        return -1;
+    }
+    memset(buffer, 0, size);
+    memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
+
+    return 0;
+}
+
+/* --------------- */
+/*
+ *  We assume our current directory is already the BDB homedir. Otherwise
+ *  opening the databases will not work as expected. If we use transactions,
+ *  dbif_env_init(), dbif_close() and dbif_stamp() are the only interface
+ *  functions that can be called without a valid transaction handle in db_txn.
+ */
+int dbif_env_init(struct db_param *dbp)
 {
     int ret;
 
+    if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
+        LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
+
 #ifdef CNID_BACKEND_DBD_TXN
     if ((ret = db_env_create(&db_env, 0))) {
         LOG(log_error, logtype_cnid, "error creating DB environment: %s", 
@@ -71,8 +130,7 @@ static int env_init(struct db_param *dbp)
         db_env->set_errfile(db_env, db_errlog); 
     db_env->set_verbose(db_env, DB_VERB_RECOVERY, 1);
     db_env->set_verbose(db_env, DB_VERB_CHKPOINT, 1);
-    if (ret = db_env->open(db_env, ".", DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | 
-                           DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_RECOVER, 0)) {
+    if (ret = db_env->open(db_env, ".", DBOPTIONS | DB_PRIVATE | DB_RECOVER, 0)) {
         LOG(log_error, logtype_cnid, "error opening DB environment: %s", 
             db_strerror(ret));
         db_env->close(db_env, 0);
@@ -83,7 +141,7 @@ static int env_init(struct db_param *dbp)
         fflush(db_errlog);
 
     if (ret = db_env->close(db_env, 0)) {
-        LOG(log_error, logtype_cnid, "error closining DB environment after recovery: %s", 
+        LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s", 
             db_strerror(ret));
         return -1;
     }
@@ -94,7 +152,7 @@ static int env_init(struct db_param *dbp)
         return -1;
     }
     if ((ret = db_env->set_cachesize(db_env, 0, 1024 * dbp->cachesize, 0))) {
-        LOG(log_error, logtype_cnid, "error settining DB environment cachesize to %i: %s",
+        LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
             dbp->cachesize, db_strerror(ret));
         db_env->close(db_env, 0);
         return -1;
@@ -120,79 +178,12 @@ static int env_init(struct db_param *dbp)
     return 0;
 }
 
-/* --------------- */
-static int  db_compat_associate (DB *p, DB *s,
-                   int (*callback)(DB *, const DBT *,const DBT *, DBT *),
-                   u_int32_t flags)
-{
-#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
-    return p->associate(p, NULL, s, callback, flags);
-#else
-#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
-    return p->associate(p,       s, callback, flags);
-#else
-    return 0;
-#endif
-#endif
-}
-
-/* --------------- */
-static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
-{
-    int ret;
-
-#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
-#ifdef CNID_BACKEND_DBD_TXN
-    ret = db->open(db, NULL, file, name, type, DB_CREATE | DB_AUTO_COMMIT, mode); 
-#else 
-    ret = db->open(db, NULL, file, name, type, DB_CREATE                 , mode); 
-#endif
-#else
-    ret = db->open(db,       file, name, type, DB_CREATE                 , mode); 
-#endif
-
-    if (ret) {
-        LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
-        return -1;
-    } else {
-        return 0;
-    }
-}
-
-/* --------------- */
-int dbif_stamp(void *buffer, int size)
-{
-    struct stat st;
-    int         rc;
-
-    if (size < 8)
-        return -1;
-
-    if ((rc = stat(db_table[0].name, &st)) < 0) {
-        LOG(log_error, logtype_cnid, "error stating database %s: %s", db_table[0].name, db_strerror(rc));
-        return -1;
-    }
-    memset(buffer, 0, size);
-    memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
-
-    return 0;
-}
-
 /* --------------- */
 int dbif_open(struct db_param *dbp)
 {
     int ret;
     int i;
 
-    if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
-        LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
-
-    if (env_init(dbp) < 0)
-        return -1;
-
-    /* db_env will point to a valid environment handle from here onwards if
-       transactions are used or to NULL otherwise */
-
     for (i = 0; i != DBIF_DB_CNT; i++) {
         if ((ret = db_create(&(db_table[i].db), db_env, 0))) {
             LOG(log_error, logtype_cnid, "error creating handle for database %s: %s", 
@@ -219,7 +210,7 @@ int dbif_open(struct db_param *dbp)
             return -1;
         if (db_errlog != NULL)
             db_table[i].db->set_errfile(db_table[i].db, db_errlog);
-    }     
+    }
     
     /* TODO: Implement CNID DB versioning info on new databases. */
     /* TODO: Make transaction support a runtime option. */
@@ -233,6 +224,7 @@ int dbif_open(struct db_param *dbp)
         LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
        return -1;
     }
+
     return 0;
 }
 
index 12698a60e7928053d0bcb730c914cc3f33cacbc9..bbeb499cf0f9d3030465246c6d65aa5f86e76987 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: dbif.h,v 1.1.4.4 2003-10-30 10:03:19 bfernhomberg Exp $
+ * $Id: dbif.h,v 1.1.4.5 2003-11-03 20:56:59 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -19,6 +19,7 @@
 #define DBIF_IDX_DIDNAME   2
 
 extern int        dbif_stamp  __P((void *, int));
+extern int        dbif_env_init  __P((struct db_param *));
 extern int        dbif_open  __P((struct db_param *));
 extern int        dbif_close __P((void));
 extern int        dbif_get __P((const int, DBT *, DBT *, u_int32_t));
index 149a837f8211d22bcc7fda1fea80861c3e1f133c..68f284fbbbec8fe4b815f6faa6b4ce3c7605779e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: main.c,v 1.1.4.2 2003-10-30 10:03:19 bfernhomberg Exp $
+ * $Id: main.c,v 1.1.4.3 2003-11-03 20:56:59 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -253,24 +253,46 @@ int main(int argc, char *argv[])
     if ((dbp = db_param_read(dir)) == NULL)
         exit(1);
 
+    if (dbif_env_init(dbp) < 0)
+        exit(2); /* FIXME: same exit code as failure for dbif_open() */  
+    
+#ifdef CNID_BACKEND_DBD_TXN
+    if (dbif_txn_begin() < 0)
+       exit(6);
+#endif
+    
     if (dbif_open(dbp) < 0) {
+#ifdef CNID_BACKEND_DBD_TXN
+       dbif_txn_abort();
+#endif
         dbif_close();
         exit(2);
     }
     if (dbd_stamp() < 0) {
+#ifdef CNID_BACKEND_DBD_TXN
+       dbif_txn_abort();
+#endif
         dbif_close();
         exit(5);
     }
+#ifdef CNID_BACKEND_DBD_TXN
+    if (dbif_txn_commit() < 0)
+       exit(6);
+#endif
     if (comm_init(dbp) < 0) {
         dbif_close();
         exit(3);
     }
     if (loop(dbp) < 0)
         err++;
-    
+
+#ifndef CNID_BACKEND_DBD_TXN
+    /* FIXME: Do we really need to sync before closing the DB? Just closing it
+       should be enough. */
     if (dbif_sync() < 0)
         err++;
-        
+#endif        
+
     if (dbif_close() < 0)
         err++;
 
@@ -279,7 +301,7 @@ int main(int argc, char *argv[])
 #endif
     close(lockfd);
     
-    if (err) 
+    if (err)
         exit(4);
     else {
        if (exit_sig)