]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/cmd_dbd_scanvol.c
Remove noadouble option
[netatalk.git] / etc / cnid_dbd / cmd_dbd_scanvol.c
index 576df49fb570bd26009014e9ac8226d75eda5f6d..be38a52f2275baee2fb19a3e84ed81e36858231c 100644 (file)
@@ -34,6 +34,7 @@
 #include <atalk/ea.h>
 #include <atalk/util.h>
 #include <atalk/acl.h>
+#include <atalk/compat.h>
 
 #include "cmd_dbd.h"
 #include "dbif.h"
@@ -292,17 +293,16 @@ static const char *check_special_dirs(const char *name)
 */
 static int check_adfile(const char *fname, const struct stat *st)
 {
-    int ret, adflags;
+    int ret;
+    int adflags = ADFLAGS_HF;
     struct adouble ad;
-    char *adname;
+    const char *adname;
 
     if (dbd_flags & DBD_FLAGS_CLEANUP)
         return 0;
 
     if (S_ISREG(st->st_mode))
-        adflags = 0;
-    else
-        adflags = ADFLAGS_DIR;
+        adflags |= ADFLAGS_DIR;
 
     adname = myvolinfo->ad_path(fname, adflags);
 
@@ -320,9 +320,9 @@ static int check_adfile(const char *fname, const struct stat *st)
             return -1;
 
         /* Create ad file */
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+        ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
 
-        if ((ret = ad_open_metadata( fname, adflags, O_CREAT, &ad)) != 0) {
+        if ((ret = ad_open(&ad, fname, adflags | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666)) != 0) {
             dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
                      cwdbuf, adname, strerror(errno));
 
@@ -332,7 +332,7 @@ static int check_adfile(const char *fname, const struct stat *st)
         /* Set name in ad-file */
         ad_setname(&ad, utompath((char *)fname));
         ad_flush(&ad);
-        ad_close_metadata(&ad);
+        ad_close(&ad, ADFLAGS_HF);
 
         chown(adname, st->st_uid, st->st_gid);
         /* FIXME: should we inherit mode too here ? */
@@ -340,12 +340,12 @@ static int check_adfile(const char *fname, const struct stat *st)
         chmod(adname, st->st_mode);
 #endif
     } else {
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
-        if (ad_open_metadata( fname, adflags, O_RDONLY, &ad) != 0) {
+        ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+        if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
             dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
             return -1;
         }
-        ad_close_metadata(&ad);
+        ad_close(&ad, ADFLAGS_HF);
     }
     return 0;
 }
@@ -374,7 +374,7 @@ static void remove_eafiles(const char *name, struct ea *ea)
     if ((dp = opendir(".")) == NULL) {
         dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
                 cwdbuf, ADv2_DIRNAME, strerror(errno));
-        return;
+        goto exit;
     }
 
     while ((ep = readdir(dp))) {
@@ -388,9 +388,14 @@ static void remove_eafiles(const char *name, struct ea *ea)
         } /* if */
     } /* while */
 
+exit:
     if (dp)
         closedir(dp);
-
+    if ((chdir("..")) != 0) {
+        dbd_log(LOGSTD, "Couldn't chdir to '%s': %s", cwdbuf, strerror(errno));
+        /* we can't proceed */
+        longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
+    }    
 }
 
 /*
@@ -492,9 +497,9 @@ static int check_addir(int volroot)
         }
 
         /* Create ad dir and set name */
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+        ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
 
-        if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad) != 0) {
+        if (ad_open(&ad, ".", ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_CREATE | ADFLAGS_RDWR, 0777) != 0) {
             dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
             return -1;
         }
@@ -505,7 +510,7 @@ static int check_addir(int volroot)
         /* Update name in ad file */
         ad_setname(&ad, mname);
         ad_flush(&ad);
-        ad_close_metadata(&ad);
+        ad_close(&ad, ADFLAGS_HF);
 
         /* Inherit owner/group from "." to ".AppleDouble" and ".Parent" */
         if ((lstat(".", &st)) != 0) {
@@ -689,8 +694,8 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
     /* Get CNID from ad-file if volume is using AFPVOL_CACHE */
     ad_cnid = 0;
     if ( (myvolinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
-        if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
+        ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+        if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
             
             if (dbd_flags & DBD_FLAGS_CLEANUP)
                 return CNID_INVALID;
@@ -706,14 +711,14 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
             ad_flush(&ad);
         }
         else
-            ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, did, stamp);
+            ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
 
         if (ad_cnid == 0)
             dbd_log( LOGSTD, "Bad CNID in adouble file of '%s/%s'", cwdbuf, name);
         else
             dbd_log( LOGDEBUG, "CNID from .AppleDouble file for '%s/%s': %u", cwdbuf, name, ntohl(ad_cnid));
 
-        ad_close_metadata(&ad);
+        ad_close(&ad, ADFLAGS_HF);
     }
 
     /* Get CNID from database */
@@ -750,24 +755,22 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
         /* Everything is fine */
         return db_cnid;
     } else if (ad_cnid && db_cnid && (ad_cnid != db_cnid)) {
-        /* Mismatch ? Delete both from db and re-add data from file */
+        /* Mismatch, overwrite ad file with value from db */
         dbd_log( LOGSTD, "CNID mismatch for '%s/%s', db: %u, ad-file: %u", cwdbuf, name, ntohl(db_cnid), ntohl(ad_cnid));
         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
-            rqst.cnid = db_cnid;
-            ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
-            if (dbif_txn_close(dbd, ret) != 0)
-                return CNID_INVALID;
-
-            rqst.cnid = ad_cnid;
-            ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
-            if (dbif_txn_close(dbd, ret) != 0)
-                return CNID_INVALID;
-
-            ret = dbd_rebuild_add(dbd, &rqst, &rply);
-            if (dbif_txn_close(dbd, ret) != 0)
+            dbd_log(LOGSTD, "Updating AppleDouble file for '%s/%s' with CNID: %u from database",
+                            cwdbuf, name, ntohl(db_cnid));
+            ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+            if (ad_open(&ad, name, adflags | ADFLAGS_HF | ADFLAGS_RDWR) != 0) {
+                dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
+                        cwdbuf, name, strerror(errno));
                 return CNID_INVALID;
+            }
+            ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
+            ad_flush(&ad);
+            ad_close(&ad, ADFLAGS_HF);
         }
-        return ad_cnid;
+        return db_cnid;
     } else if (ad_cnid && (db_cnid == 0)) {
         /* in ad-file but not in db */
         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
@@ -777,7 +780,7 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
 
             rqst.cnid = ad_cnid;
             ret = dbd_resolve(dbd, &rqst, &rply);
-            if (ret == CNID_DBD_RES_OK) {
+            if (rply.result == CNID_DBD_RES_OK) {
                 /* Occupied! Choose another, update ad-file */
                 ret = dbd_add(dbd, &rqst, &rply, 1);
                 if (dbif_txn_close(dbd, ret) != 0)
@@ -790,15 +793,15 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
                     && ( ! (dbd_flags & DBD_FLAGS_SCAN))) {
                     dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
                             cwdbuf, name, ntohl(db_cnid));
-                    ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
-                    if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
+                    ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+                    if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
                         dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
                                 cwdbuf, name, strerror(errno));
                         return CNID_INVALID;
                     }
                     ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
                     ad_flush(&ad);
-                    ad_close_metadata(&ad);
+                    ad_close(&ad, ADFLAGS_HF);
                 }
                 return db_cnid;
             }
@@ -830,15 +833,15 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                 dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
                         cwdbuf, name, ntohl(db_cnid));
-                ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
-                if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
+                ad_init_old(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+                if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
                     dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
                             cwdbuf, name, strerror(errno));
                     return CNID_INVALID;
                 }
                 ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
                 ad_flush(&ad);
-                ad_close_metadata(&ad);
+                ad_close(&ad, ADFLAGS_HF);
             }
         }
         return db_cnid;
@@ -1041,12 +1044,6 @@ static int dbd_readdir(int volroot, cnid_t did)
 
 static int scanvol(struct volinfo *vi, dbd_flags_t flags)
 {
-    /* Dont scanvol on no-appledouble vols */
-    if (vi->v_flags & AFPVOL_NOADOUBLE) {
-        dbd_log( LOGSTD, "Volume without AppleDouble support: skipping volume scanning.");
-        return 0;
-    }
-
     /* Make this stuff accessible from all funcs easily */
     myvolinfo = vi;
     dbd_flags = flags;
@@ -1082,8 +1079,6 @@ static void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
     struct cnid_dbd_rqst rqst;
     struct cnid_dbd_rply rply;
 
-    dbd->db_param.txn_frequency = 0;
-
     /* jump over rootinfo key */
     if ( dbif_idwalk(dbd, &dbd_cnid, 0) != 1)
         return;
@@ -1198,7 +1193,6 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
     db_param.cachesize = 64 * 1024;         /* 64 MB */
     db_param.maxlocks = DEFAULT_MAXLOCKS;
     db_param.maxlockobjs = DEFAULT_MAXLOCKOBJS;
-    db_param.txn_frequency = 1000;          /* close txn every 1000 objects */
     db_param.logfile_autoremove = 1;
 
     /* Make it accessible for all funcs */
@@ -1210,15 +1204,16 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
         return -1;
     }
 
-    /* temporary rebuild db, used with -re rebuild to delete unused CNIDs, not used with -f */
-    if (! nocniddb && !(flags & DBD_FLAGS_FORCE)) {
-        /* Get volume stamp */
-        dbd_getstamp(dbd, &rqst, &rply);
-        if (rply.result != CNID_DBD_RES_OK)
-            goto exit;
-        memcpy(stamp, rply.name, CNID_DEV_LEN);
+    /* Get volume stamp */
+    dbd_getstamp(dbd, &rqst, &rply);
+    if (rply.result != CNID_DBD_RES_OK) {
+        ret = -1;
+        goto exit;
+    }
+    memcpy(stamp, rply.name, CNID_DEV_LEN);
 
-        /* open/create rebuild dbd, copy rootinfo key */
+    /* temporary rebuild db, used with -re rebuild to delete unused CNIDs, not used with -f */
+    if (! nocniddb && (flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE)) {
         tmpdb_path = get_tmpdb_path();
         if (NULL == (dbd_rebuild = dbif_init(tmpdb_path, "cnid2.db"))) {
             ret = -1;
@@ -1256,10 +1251,10 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
 
 exit:
     if (! nocniddb) {
-        if (dbif_txn_close(dbd, 2) != 0)
+        if (dbif_txn_close(dbd, ret == 0 ? 1 : 0) != 0)
             ret = -1;
         if (dbd_rebuild)
-            if (dbif_txn_close(dbd_rebuild, 2) != 0)
+            if (dbif_txn_close(dbd_rebuild, ret == 0 ? 1 : 0) != 0)
                 ret = -1;
         if ((ret == 0) && dbd_rebuild && (flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE))
             /* We can only do this in exclusive mode, otherwise we might delete CNIDs added from