]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/dbif.c
Spotlight: SPARQL query optimisations
[netatalk.git] / etc / cnid_dbd / dbif.c
index 646385cbe267d392e955e8261220c49ff80dc933..3dae61cdad2f6ba5561f14708d4c37f39dc5f27c 100644 (file)
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/cdefs.h>
 #include <unistd.h>
+#include <arpa/inet.h>
 
 #include <db.h>
 
 #include <atalk/logger.h>
 #include <atalk/util.h>
+#include <atalk/errchk.h>
+#include <atalk/cnid.h>
 
 #include "db_param.h"
 #include "dbif.h"
  */
 static int dbif_stamp(DBD *dbd, void *buffer, int size)
 {
+    EC_INIT;
     struct stat st;
-    int         rc,cwd;
+    int cwd = -1;
 
     if (size < 8)
-        return -1;
+        EC_FAIL;
 
     /* Remember cwd */
     if ((cwd = open(".", O_RDONLY)) < 0) {
         LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
-        return -1;
+        EC_FAIL;
     }
 
     /* chdir to db_envhome */
     if ((chdir(dbd->db_envhome)) != 0) {
         LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
-        return -1;
+        EC_FAIL;
     }
 
-    if ((rc = stat(dbd->db_table[DBIF_CNID].name, &st)) < 0) {
+    if (stat(dbd->db_table[DBIF_CNID].name, &st) < 0) {
         LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
-        return -1;
+        EC_FAIL;
     }
 
     LOG(log_maxdebug, logtype_cnid,"stamp: %s", asctime(localtime(&st.st_ctime)));
@@ -61,12 +64,15 @@ static int dbif_stamp(DBD *dbd, void *buffer, int size)
     memset(buffer, 0, size);
     memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
 
-    if ((fchdir(cwd)) != 0) {
-        LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
-        return -1;
+EC_CLEANUP:
+    if (cwd != -1) {
+        if (fchdir(cwd) != 0) {
+            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
+            EC_STATUS(-1);
+        }
+        close(cwd);
     }
-
-    return 0;
+    EC_EXIT;
 }
 
 /*!
@@ -83,13 +89,11 @@ static int dbif_stamp(DBD *dbd, void *buffer, int size)
 static int dbif_init_rootinfo(DBD *dbd, int version)
 {
     DBT key, data;
-    uint32_t v;
+    uint32_t uint32;
     char buf[ROOTINFO_DATALEN];
 
     LOG(log_debug, logtype_cnid, "Setting CNID database version to %u", version);
 
-    v = version;
-    v = htonl(v);
 
     memset(&key, 0, sizeof(key));
     memset(&data, 0, sizeof(data));
@@ -99,7 +103,13 @@ static int dbif_init_rootinfo(DBD *dbd, int version)
     data.size = ROOTINFO_DATALEN;
 
     memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
-    memcpy(buf + CNID_DID_OFS, &v, sizeof(v));
+
+    uint32 = htonl(CNID_START - 1);
+    memcpy(buf + CNID_TYPE_OFS, &uint32, sizeof(uint32));
+
+    uint32 = htonl(version);
+    memcpy(buf + CNID_DID_OFS, &uint32, sizeof(uint32));
+
     if (dbif_stamp(dbd, buf + CNID_DEV_OFS, CNID_DEV_LEN) < 0)
         return -1;
 
@@ -715,8 +725,8 @@ int dbif_env_remove(const char *path)
     int ret;
     DBD *dbd;
 
-    LOG(log_debug, logtype_cnid, "Reopening BerkeleyDB environment");
-    
+    LOG(log_debug, logtype_cnid, "Trying to remove BerkeleyDB environment");
+
     if (NULL == (dbd = dbif_init(path, "cnid2.db")))
         return -1;
 
@@ -878,7 +888,7 @@ int dbif_del(DBD *dbd, const int dbi, DBT *key, u_int32_t flags)
                                      flags);
     
     if (ret == DB_NOTFOUND) {
-        LOG(log_info, logtype_cnid, "key not found");
+        LOG(log_debug, logtype_cnid, "key not found");
         return 0;
     }
     if (ret) {