]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/cnid/cnid_open.c
Change all references to db3/DB3 to BDB. We now support Berkeley DB
[netatalk.git] / libatalk / cnid / cnid_open.c
index 7f24b51d478bdcb5aa6806ee12bd091bd5d800b3..674343efe1cb9c4ad06cb5d223b116ef4fcf2137 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: cnid_open.c,v 1.39 2002-05-29 18:02:59 jmarcus Exp $
+ * $Id: cnid_open.c,v 1.49 2003-01-04 21:41:49 jmarcus Exp $
  *
  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
  * All Rights Reserved. See COPYRIGHT.
 #define DBVERSION1       0x00000001U
 #define DBVERSION        DBVERSION1
 
-#if DB_VERSION_MINOR > 1
+#ifdef CNID_DB_CDB
+#define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
+#else /* !CNID_DB_CDB */
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
 DB_INIT_LOG | DB_INIT_TXN)
 #else /* DB_VERSION_MINOR < 1 */
@@ -99,14 +102,17 @@ DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
 DB_INIT_LOG | DB_INIT_TXN)
 #endif /* DB_VERSION_MINOR */
+#endif /* CNID_DB_CDB */
 
+#ifndef CNID_DB_CDB
 /* Let's try and use the youngest lock detector if present.
- * If we can't do that, then let DB3 use its default deadlock detector. */
+ * If we can't do that, then let BDB use its default deadlock detector. */
 #if defined DB_LOCK_YOUNGEST
 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
 #else /* DB_LOCK_YOUNGEST */
 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
 #endif /* DB_LOCK_YOUNGEST */
+#endif /* CNID_DB_CDB */
 
 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
 * databases. */
@@ -123,7 +129,7 @@ static __inline__ int compare_did(const DBT *a, const DBT *b)
 
 /* sort did's and then names. this is for unix paths.
  * i.e., did/unixname lookups. */
-#if DB_VERSION_MINOR > 1
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
 static int compare_unix(DB *db, const DBT *a, const DBT *b)
 #else /* DB_VERSION_MINOR < 1 */
 static int compare_unix(const DBT *a, const DBT *b)
@@ -149,7 +155,7 @@ static int compare_unix(const DBT *a, const DBT *b)
  * did/macname, and did/shortname. i think did/longname needs a
  * unicode table to work. also, we can't use strdiacasecmp as that
  * returns a match if a < b. */
-#if DB_VERSION_MINOR > 1
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
 static int compare_mac(DB *db, const DBT *a, const DBT *b)
 #else /* DB_VERSION_MINOR < 1 */
 static int compare_mac(const DBT *a, const DBT *b)
@@ -173,23 +179,25 @@ static int compare_mac(const DBT *a, const DBT *b)
 
 
 /* for unicode names -- right now it's the same as compare_mac. */
-#if DB_VERSION_MINOR > 1
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
 #else /* DB_VERSION_MINOR < 1 */
 static int compare_unicode(const DBT *a, const DBT *b)
 #endif /* DB_VERSION_MINOR */
 {
-#if DB_VERSION_MINOR > 1
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
     return compare_mac(db,a,b);
 #else /* DB_VERSION_MINOR < 1 */
     return compare_mac(a,b);
 #endif /* DB_VERSION_MINOR */
 }
 
-void *cnid_open(const char *dir) {
-    struct stat st, rsb, lsb, csb;
+void *cnid_open(const char *dir, mode_t mask) {
+    struct stat st;
+#ifndef CNID_DB_CDB
     struct flock lock;
-    char path[MAXPATHLEN + 1];
+#endif /* CNID_DB_CDB */
+char path[MAXPATHLEN + 1];
     CNID_private *db;
     DBT key, data;
     DB_TXN *tid;
@@ -219,15 +227,15 @@ void *cnid_open(const char *dir) {
         len++;
     }
 
-    lock.l_type = F_WRLCK;
-    lock.l_whence = SEEK_SET;
-
     strcpy(path + len, DBHOME);
-    if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
+    if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
         goto fail_adouble;
     }
 
+#ifndef CNID_DB_CDB
+    lock.l_type = F_WRLCK;
+    lock.l_whence = SEEK_SET;
     /* Make sure cnid.lock goes in .AppleDB. */
     strcat(path, "/");
     len++;
@@ -239,7 +247,7 @@ void *cnid_open(const char *dir) {
      * to the sahe directory. */
     strcat(path, DBLOCKFILE);
     strcpy(db->lock_file, path);
-    if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
+    if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
         lock.l_start = 0;
         lock.l_len = 1;
         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
@@ -254,6 +262,7 @@ void *cnid_open(const char *dir) {
     else {
         LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
     }
+#endif /* CNID_DB_CDB */
 
     path[len + DBHOMELEN] = '\0';
     open_flag = DB_CREATE;
@@ -266,13 +275,16 @@ void *cnid_open(const char *dir) {
         goto fail_lock;
     }
 
+#ifndef CNID_DB_CDB
     /* Setup internal deadlock detection. */
     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
         LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
         goto fail_lock;
     }
+#endif /* CNID_DB_CDB */
 
-#if DB_VERSION_MINOR > 1
+#ifndef CNID_DB_CDB
+#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
 #if 0
     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
@@ -281,9 +293,10 @@ void *cnid_open(const char *dir) {
     }
 #endif
 #endif /* DB_VERSION_MINOR > 1 */
+#endif /* CNID_DB_CDB */
 
     /* Open the database environment. */
-    if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) != 0) {
+    if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
         if (rc == DB_RUNRECOVERY) {
             /* This is the mother of all errors.  We _must_ fail here. */
             LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
@@ -293,10 +306,10 @@ void *cnid_open(const char *dir) {
         /* We can't get a full transactional environment, so multi-access
          * is out of the question.  Let's assume a read-only environment,
          * and try to at least get a shared memory pool. */
-        if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
+        if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
              * open the environment with no flags. */
-            if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
+            if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
                     path, db_strerror(rc));
                 goto fail_lock;
@@ -315,8 +328,13 @@ void *cnid_open(const char *dir) {
     }
 
     /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_didname->open(db->db_didname, NULL, DBDIDNAME, NULL,
+                                   DB_HASH, open_flag | DB_AUTO_COMMIT, 0666 & ~mask))) {
+#else
     if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
-                                   DB_HASH, open_flag, 0666))) {
+                                   DB_HASH, open_flag, 0666 & ~mask))) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
             db_strerror(rc));
         goto fail_appinit;
@@ -329,6 +347,24 @@ void *cnid_open(const char *dir) {
     key.data = DBVERSION_KEY;
     key.size = DBVERSION_KEYLEN;
 
+#ifdef CNID_DB_CDB
+    if ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) != 0) {
+        int ret;
+        {
+            u_int32_t version = htonl(DBVERSION);
+
+            data.data = &version;
+            data.size = sizeof(version);
+        }
+        if ((ret = db->db_didname->put(db->db_didname, NULL, &key, &data,
+                                       DB_NOOVERWRITE))) {
+            LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
+                db_strerror(ret));
+            db->db_didname->close(db->db_didname, 0);
+            goto fail_appinit;
+        }
+    }
+#else /* CNID_DB_CDB */
 dbversion_retry:
     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
         LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
@@ -368,7 +404,7 @@ dbversion_retry:
                 }
                 else if (ret == DB_RUNRECOVERY) {
                     /* At this point, we don't care if the transaction aborts
-                     * successfully or not. */
+                    * successfully or not. */
                     txn_abort(tid);
                     LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
                         db_strerror(ret));
@@ -392,6 +428,7 @@ dbversion_retry:
         db->db_didname->close(db->db_didname, 0);
         goto fail_appinit;
     }
+#endif /* CNID_DB_CDB */
 
     /* TODO In the future we might check for version number here. */
 #if 0
@@ -411,7 +448,11 @@ dbversion_retry:
     }
 
     db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
-    if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_macname->open(db->db_macname, NULL, DBMACNAME, NULL, DB_BTREE, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
             db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
@@ -428,7 +469,11 @@ dbversion_retry:
     }
 
     db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
-    if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_shortname->open(db->db_shortname, NULL, DBSHORTNAME, NULL, DB_BTREE, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
             db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
@@ -447,7 +492,11 @@ dbversion_retry:
     }
 
     db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
-    if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_longname->open(db->db_longname, NULL, DBLONGNAME, NULL, DB_BTREE, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
             db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
@@ -470,7 +519,11 @@ dbversion_retry:
         goto fail_appinit;
     }
 
-    if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_devino->open(db->db_devino, NULL, DBDEVINO, NULL, DB_HASH, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
             db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
@@ -497,7 +550,11 @@ dbversion_retry:
     }
 
 
-    if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_cnid->open(db->db_cnid, NULL, DBCNID, NULL, DB_HASH, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
             db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
@@ -506,6 +563,7 @@ dbversion_retry:
         db->db_shortname->close(db->db_shortname, 0);
         db->db_longname->close(db->db_longname, 0);
 #endif /* EXTENDED_DB */
+        db->db_devino->close(db->db_devino, 0);
         goto fail_appinit;
     }
 
@@ -524,7 +582,11 @@ dbversion_retry:
         goto fail_appinit;
     }
 
-    if ((rc = db->db_mangle->open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666)) != 0) {
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
+    if ((rc = db->db_mangle->open(db->db_mangle, NULL, DBMANGLE, NULL, DB_HASH, open_flag | DB_AUTO_COMMIT, 0666 & ~mask)) != 0) {
+#else
+    if ((rc = db->db_mangle->open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666 & ~mask)) != 0) {
+#endif /* DB_VERSION_MAJOR >= 4 */
         LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
         db->db_didname->close(db->db_didname, 0);
         db->db_devino->close(db->db_devino, 0);
@@ -538,7 +600,7 @@ dbversion_retry:
     }
 #endif /* FILE_MANGLING */
 
-    /* Print out the version of DB3 we're linked against. */
+    /* Print out the version of BDB we're linked against. */
     LOG(log_info, logtype_default, "CNID DB initialized using %s",
         db_version(NULL, NULL, NULL));
 
@@ -549,10 +611,12 @@ fail_appinit:
     db->dbenv->close(db->dbenv, 0);
 
 fail_lock:
+#ifndef CNID_DB_CDB
     if (db->lockfd > -1) {
         close(db->lockfd);
         (void)remove(db->lock_file);
     }
+#endif /* CNID_DB_CDB */
 
 fail_adouble: