]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/cmd_dbd_scanvol.c
Dont just exit, use longjmp to exit with cleanup
[netatalk.git] / etc / cnid_dbd / cmd_dbd_scanvol.c
index bac1b4fed08528c72ab3ebb4f19812e084e9c839..9af6e585da10c0b1940ed8a0368a6cda30013fe4 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: cmd_dbd_scanvol.c,v 1.7 2009-07-12 09:21:34 franklahm Exp $
+  $Id: cmd_dbd_scanvol.c,v 1.12 2009-12-03 13:33:44 franklahm Exp $
 
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
@@ -208,7 +208,7 @@ static int check_adfile(const char *fname, const struct stat *st)
             return -1;
         }
         /* Missing. Log and create it */
-        dbd_log(LOGSTD, "Missing AppleDoube file '%s/%s'", cwdbuf, adname);
+        dbd_log(LOGSTD, "Missing AppleDouble file '%s/%s'", cwdbuf, adname);
 
         if (dbd_flags & DBD_FLAGS_SCAN)
             /* Scan only requested, dont change anything */
@@ -220,6 +220,7 @@ static int check_adfile(const char *fname, const struct stat *st)
         if ((ret = ad_open_metadata( fname, adflags, O_CREAT, &ad)) != 0) {
             dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
                      cwdbuf, adname, strerror(errno));
+
             return -1;
         }
 
@@ -233,6 +234,13 @@ static int check_adfile(const char *fname, const struct stat *st)
 #if 0
         chmod(adname, st->st_mode);
 #endif
+    } else {
+        ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
+        if (ad_open_metadata( fname, adflags, O_RDONLY, &ad) != 0) {
+            dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
+            return -1;
+        }
+        ad_close_metadata(&ad);
     }
     return 0;
 }
@@ -245,7 +253,8 @@ static int check_addir(int volroot)
     int addir_ok, adpar_ok;
     struct stat st;
     struct adouble ad;
-
+    char *mname;
+    
     /* Check for ad-dir */
     if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
         if (errno != ENOENT) {
@@ -286,7 +295,7 @@ static int check_addir(int volroot)
         }
 
         /* Get basename of cwd from cwdbuf */
-        char *mname = utompath(strrchr(cwdbuf, '/') + 1);
+        utompath(strrchr(cwdbuf, '/') + 1);
 
         /* Update name in ad file */
         ad_setname(&ad, mname);
@@ -378,7 +387,7 @@ static int read_addir(void)
         dbd_log(LOGSTD, "Couldn't chdir back to '%s' from AppleDouble dir: %s",
                 cwdbuf, strerror(errno));
         /* This really is EOT! */
-        exit(1);
+        longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
     }
 
     closedir(dp);
@@ -434,7 +443,7 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
     rqst.namelen = strlen(name);
 
     /* Query the database */
-    ret = cmd_dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
+    ret = dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
     dbif_txn_close(dbd, ret);
     if (rply.result == CNID_DBD_RES_OK) {
         db_cnid = rply.cnid;
@@ -483,7 +492,7 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
         /* Note: the next test will use this new CNID too! */
         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
             /* add to db */
-            ret = cmd_dbd_add(dbd, &rqst, &rply);
+            ret = dbd_add(dbd, &rqst, &rply, 1);
             dbif_txn_close(dbd, ret);
             db_cnid = rply.cnid;
             dbd_log( LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
@@ -589,24 +598,26 @@ static int dbd_readdir(int volroot, cnid_t did)
         if (ADDIR_OK)
             adfile_ok = check_adfile(ep->d_name, &st);
 
-        /* Check CNIDs */
-        cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
-
-        /* Now add this object to our rebuild dbd */
-        if (cnid) {
-            rqst.cnid = rply.cnid;
-            dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
-            if (rply.result != CNID_DBD_RES_OK) {
-                dbd_log( LOGDEBUG, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
-                         cnid, cwdbuf, ep->d_name);
-                exit(EXIT_FAILURE);
+        if ( ! nocniddb) {
+            /* Check CNIDs */
+            cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
+
+            /* Now add this object to our rebuild dbd */
+            if (cnid) {
+                rqst.cnid = rply.cnid;
+                dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
+                if (rply.result != CNID_DBD_RES_OK) {
+                    dbd_log( LOGDEBUG, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
+                             cnid, cwdbuf, ep->d_name);
+                    longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
+                }
             }
         }
 
         /**************************************************************************
           Recursion
         **************************************************************************/
-        if (S_ISDIR(st.st_mode) && cnid) { /* If we have no cnid for it we cant recur */
+        if (S_ISDIR(st.st_mode) && (cnid || nocniddb)) { /* If we have no cnid for it we cant recur */
             strcat(cwdbuf, "/");
             strcat(cwdbuf, ep->d_name);
             dbd_log( LOGDEBUG, "Entering directory: %s", cwdbuf);
@@ -669,7 +680,7 @@ static int scanvol(struct volinfo *vi, dbd_flags_t flags)
 /* 
    Remove all CNIDs from dbd that are not in dbd_rebuild
 */
-void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
+static void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
 {
     int ret, deleted = 0;
     cnid_t dbd_cnid = 0, rebuild_cnid = 0;
@@ -765,19 +776,21 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
         return -1;
     }
 
-    /* Get volume stamp */
-    dbd_getstamp(dbd, &rqst, &rply);
-    if (rply.result != CNID_DBD_RES_OK)
-        goto exit_cleanup;
-    memcpy(stamp, rply.name, CNID_DEV_LEN);
+    if (! nocniddb) {
+        /* Get volume stamp */
+        dbd_getstamp(dbd, &rqst, &rply);
+        if (rply.result != CNID_DBD_RES_OK)
+            goto exit_cleanup;
+        memcpy(stamp, rply.name, CNID_DEV_LEN);
 
-    /* open/create rebuild dbd, copy rootinfo key */
-    if (NULL == (dbd_rebuild = dbif_init(NULL, NULL)))
-        return -1;
-    if (0 != (dbif_open(dbd_rebuild, NULL, 0)))
-        return -1;
-    if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild)))
-        goto exit_cleanup;
+        /* open/create rebuild dbd, copy rootinfo key */
+        if (NULL == (dbd_rebuild = dbif_init(NULL, NULL)))
+            return -1;
+        if (0 != (dbif_open(dbd_rebuild, NULL, 0)))
+            return -1;
+        if (0 != (dbif_copy_rootinfokey(dbd, dbd_rebuild)))
+            goto exit_cleanup;
+    }
 
     if (setjmp(jmp) != 0)
         goto exit_cleanup;      /* Got signal, jump from dbd_readdir */
@@ -786,12 +799,15 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
     if ( (scanvol(volinfo, flags)) != 0)
         return -1;
 
+    if (! nocniddb) {
     /* We can only do this in exclusive mode, otherwise we might delete CNIDs added from
        other clients in between our pass 1 and 2 */
-    if (flags & DBD_FLAGS_EXCL)
-        delete_orphaned_cnids(dbd, dbd_rebuild, flags);
+        if (flags & DBD_FLAGS_EXCL)
+            delete_orphaned_cnids(dbd, dbd_rebuild, flags);
+    }
 
 exit_cleanup:
-    dbif_close(dbd_rebuild);
+    if (! nocniddb)
+        dbif_close(dbd_rebuild);
     return ret;
 }