]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/cmd_dbd_scanvol.c
Set cachesize for rebuild db
[netatalk.git] / etc / cnid_dbd / cmd_dbd_scanvol.c
index 5da4527bcc5666fd8a6f5e8c0eda1bb643415b84..53a81ae3c16b674b2c9f06ce92e52f4b318630ce 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  $Id: cmd_dbd_scanvol.c,v 1.5 2009-05-23 06:28:27 franklahm Exp $
+  $Id: cmd_dbd_scanvol.c,v 1.15 2009-12-21 06:41:09 franklahm Exp $
 
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
 #include <string.h>
 #include <errno.h>
 #include <setjmp.h>
+
 #include <atalk/adouble.h>
 #include <atalk/unicode.h>
 #include <atalk/volinfo.h>
 #include <atalk/cnid_dbd_private.h>
+#include <atalk/volume.h>
+#include <atalk/ea.h>
+#include <atalk/util.h>
+
 #include "cmd_dbd.h"
 #include "dbif.h"
 #include "db_param.h"
@@ -57,6 +62,8 @@ static char           *netatalk_dirs[] = {
 static struct cnid_dbd_rqst rqst;
 static struct cnid_dbd_rply rply;
 static jmp_buf jmp;
+static struct vol volume; /* fake it for ea_open */
+static char pname[MAXPATHLEN] = "../";
 
 /*
   Taken form afpd/desktop.c
@@ -208,7 +215,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 +227,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,10 +241,115 @@ 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;
 }
 
+/* 
+   Remove all files with file::EA* from adouble dir
+*/
+static void remove_eafiles(const char *name, struct ea *ea)
+{
+    DIR *dp = NULL;
+    struct dirent *ep;
+    char eaname[MAXPATHLEN];
+
+    strlcpy(eaname, name, sizeof(eaname));
+    if (strlcat(eaname, "::EA", sizeof(eaname)) >= sizeof(eaname)) {
+        dbd_log(LOGSTD, "name too long: '%s/%s/%s'", cwdbuf, ADv2_DIRNAME, name);
+        return;
+    }
+
+    if ((chdir(ADv2_DIRNAME)) != 0) {
+        dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
+                cwdbuf, ADv2_DIRNAME, strerror(errno));
+        return;
+    }
+
+    if ((dp = opendir(".")) == NULL) {
+        dbd_log(LOGSTD, "Couldn't open the directory '%s/%s': %s",
+                cwdbuf, ADv2_DIRNAME, strerror(errno));
+        return;
+    }
+
+    while ((ep = readdir(dp))) {
+        if (strstr(ep->d_name, eaname) != NULL) {
+            dbd_log(LOGSTD, "Removing EA file: '%s/%s/%s'",
+                    cwdbuf, ADv2_DIRNAME, ep->d_name);
+            if ((unlink(ep->d_name)) != 0) {
+                dbd_log(LOGSTD, "Error unlinking EA file '%s/%s/%s': %s",
+                        cwdbuf, ADv2_DIRNAME, ep->d_name, strerror(errno));
+            }
+        } /* if */
+    } /* while */
+
+    if (dp)
+        closedir(dp);
+
+}
+
+/*
+  Check Extended Attributes files
+*/
+static int check_eafiles(const char *fname)
+{
+    unsigned int  count = 0;
+    int ret = 0, remove;
+    struct ea ea;
+    struct stat st;
+    char *eaname;
+
+    if ((ret = ea_open(&volume, fname, EA_RDWR, &ea)) != 0) {
+        if (errno == ENOENT)
+            return 0;
+        dbd_log(LOGSTD, "Error calling ea_open for file: %s/%s, removing EA files",
+                cwdbuf, fname);
+        if ( ! (dbd_flags & DBD_FLAGS_SCAN))
+            remove_eafiles(fname, &ea);
+        return -1;
+    }
+
+    /* Check all EAs */
+    while (count < ea.ea_count) {        
+        dbd_log(LOGDEBUG, "EA: %s", (*ea.ea_entries)[count].ea_name);
+        remove = 0;
+
+        eaname = ea_path(&ea, (*ea.ea_entries)[count].ea_name, 0);
+
+        if (stat(eaname, &st) != 0) {
+            if (errno == ENOENT)
+                dbd_log(LOGSTD, "Missing EA: %s/%s", cwdbuf, eaname);
+            else
+                dbd_log(LOGSTD, "Bogus EA: %s/%s", cwdbuf, eaname);
+            remove = 1;
+        } else if (st.st_size != (*ea.ea_entries)[count].ea_size) {
+            dbd_log(LOGSTD, "Bogus EA: %s/%s, removing it...", cwdbuf, eaname);
+            remove = 1;
+            if ((unlink(eaname)) != 0)
+                dbd_log(LOGSTD, "Error removing EA file '%s/%s': %s",
+                        cwdbuf, eaname, strerror(errno));
+        }
+
+        if (remove) {
+            /* Be CAREFUL here! This should do what ea_delentry does. ea_close relies on it !*/
+            free((*ea.ea_entries)[count].ea_name);
+            (*ea.ea_entries)[count].ea_name = NULL;
+        }
+
+        count++;
+    } /* while */
+
+    ea_close(&ea);
+    return ret;
+}
+
 /*
   Check for .AppleDouble folder and .Parent, create if missing
 */
@@ -245,6 +358,7 @@ static int check_addir(int volroot)
     int addir_ok, adpar_ok;
     struct stat st;
     struct adouble ad;
+    char *mname = NULL;
 
     /* Check for ad-dir */
     if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
@@ -286,7 +400,7 @@ static int check_addir(int volroot)
         }
 
         /* Get basename of cwd from cwdbuf */
-        char *mname = utompath(strrchr(cwdbuf, '/') + 1);
+        mname = utompath(strrchr(cwdbuf, '/') + 1);
 
         /* Update name in ad file */
         ad_setname(&ad, mname);
@@ -305,7 +419,67 @@ static int check_addir(int volroot)
     return 0;
 }
 
-/* 
+/*
+  Check if file cotains "::EA" and if it does check if its correspondig data fork exists.
+  Returns:
+  0 = name is not an EA file
+  1 = name is an EA file and no problem was found
+  -1 = name is an EA file and data fork is gone
+ */
+static int check_eafile_in_adouble(const char *name)
+{
+    int ret = 0;
+    char *namep, *namedup = NULL;
+
+    /* Check if this is an AFPVOL_EA_AD vol */
+    if (volinfo->v_vfs_ea == AFPVOL_EA_AD) {
+        /* Does the filename contain "::EA" ? */
+        namedup = strdup(name);
+        if ((namep = strstr(namedup, "::EA")) == NULL) {
+            ret = 0;
+            goto ea_check_done;
+        } else {
+            /* File contains "::EA" so it's an EA file. Check for data file  */
+
+            /* Get string before "::EA" from EA filename */
+            namep[0] = 0;
+            strlcpy(pname + 3, namedup, sizeof(pname)); /* Prepends "../" */
+
+            if ((access( pname, F_OK)) == 0) {
+                ret = 1;
+                goto ea_check_done;
+            } else {
+                ret = -1;
+                if (errno != ENOENT) {
+                    dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
+                            cwdbuf, name, strerror(errno));
+                    goto ea_check_done;
+                }
+
+                /* Orphaned EA file*/
+                dbd_log(LOGSTD, "Orphaned Extended Attribute file '%s/%s/%s'",
+                        cwdbuf, ADv2_DIRNAME, name);
+
+                if (dbd_flags & DBD_FLAGS_SCAN)
+                    /* Scan only requested, dont change anything */
+                    goto ea_check_done;
+
+                if ((unlink(name)) != 0) {
+                    dbd_log(LOGSTD, "Error unlinking orphaned Extended Attribute file '%s/%s/%s'",
+                            cwdbuf, ADv2_DIRNAME, name);
+                }
+            } /* if (access) */
+        } /* if strstr */
+    } /* if AFPVOL_EA_AD */
+
+ea_check_done:
+    if (namedup)
+        free(namedup);
+
+    return ret;
+}
+
+/*
   Check files and dirs inside .AppleDouble folder:
   - remove orphaned files
   - bail on dirs
@@ -315,7 +489,6 @@ static int read_addir(void)
     DIR *dp;
     struct dirent *ep;
     struct stat st;
-    static char fname[MAXPATHLEN] = "../";
 
     if ((chdir(ADv2_DIRNAME)) != 0) {
         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
@@ -350,12 +523,16 @@ static int read_addir(void)
             continue;
         }
 
+        /* Check if for orphaned and corrupt Extended Attributes file */
+        if (check_eafile_in_adouble(ep->d_name) != 0)
+            continue;
+
         /* Check for data file */
-        strcpy(fname+3, ep->d_name);
-        if ((access( fname, F_OK)) != 0) {
+        strcpy(pname + 3, ep->d_name);
+        if ((access( pname, F_OK)) != 0) {
             if (errno != ENOENT) {
                 dbd_log(LOGSTD, "Access error for file '%s/%s': %s",
-                        cwdbuf, ep->d_name, strerror(errno));
+                        cwdbuf, pname, strerror(errno));
                 continue;
             }
             /* Orphaned ad-file*/
@@ -378,7 +555,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);
@@ -386,9 +563,9 @@ static int read_addir(void)
     return 0;
 }
 
-/* 
-   Check CNID for a file/dir, both from db and from ad-file.
-   For detailed specs see intro.
+/*
+  Check CNID for a file/dir, both from db and from ad-file.
+  For detailed specs see intro.
 */
 static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok, int adflags)
 {
@@ -412,7 +589,8 @@ 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, did, stamp);
+
         if (ad_cnid == 0)
             dbd_log( LOGSTD, "Incorrect CNID data in .AppleDouble data for '%s/%s' (bad stamp?)", cwdbuf, name);
 
@@ -425,6 +603,7 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
     memset(&rqst, 0, sizeof(struct cnid_dbd_rqst));
     memset(&rply, 0, sizeof(struct cnid_dbd_rply));
     rqst.did = did;
+    rqst.cnid = ad_cnid;
     if ( ! (volinfo->v_flags & AFPVOL_NODEV))
         rqst.dev = st->st_dev;
     rqst.ino = st->st_ino;
@@ -433,12 +612,13 @@ 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;
     } else if (rply.result == CNID_DBD_RES_NOTFOUND) {
-        dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
+        if ( ! (dbd_flags & DBD_FLAGS_FORCE))
+            dbd_log( LOGSTD, "No CNID for '%s/%s' in database", cwdbuf, name);
         db_cnid = 0;
     } else {
         dbd_log( LOGSTD, "Fatal error resolving '%s/%s'", cwdbuf, name);
@@ -454,11 +634,11 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
         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);
+            ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
             dbif_txn_close(dbd, ret);
 
             rqst.cnid = ad_cnid;
-            ret = dbd_delete(dbd, &rqst, &rply);
+            ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
             dbif_txn_close(dbd, ret);
 
             ret = dbd_rebuild_add(dbd, &rqst, &rply);
@@ -468,9 +648,9 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
     } else if (ad_cnid && (db_cnid == 0)) {
         /* in ad-file but not in db */
         if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
-            dbd_log( LOGSTD, "CNID rebuild add for '%s/%s', adding with CNID from ad-file: %u", cwdbuf, name, ntohl(ad_cnid));
+            dbd_log( LOGDEBUG, "CNID rebuild add for '%s/%s', adding with CNID from ad-file: %u", cwdbuf, name, ntohl(ad_cnid));
             rqst.cnid = ad_cnid;
-            ret = dbd_delete(dbd, &rqst, &rply);
+            ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
             dbif_txn_close(dbd, ret);
             ret = dbd_rebuild_add(dbd, &rqst, &rply);
             dbif_txn_close(dbd, ret);
@@ -481,13 +661,13 @@ 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));
         }
     }
-    
+
     if ((ad_cnid == 0) && db_cnid) {
         /* in db but zeroID in ad-file, write it to ad-file if AFPVOL_CACHE */
         if ((volinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
@@ -587,24 +767,30 @@ 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() */
+                }
             }
         }
 
+        /* Check EA files */
+        if (volinfo->v_vfs_ea == AFPVOL_EA_AD)
+            check_eafiles(ep->d_name);
+
         /**************************************************************************
           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);
@@ -648,6 +834,11 @@ static int scanvol(struct volinfo *vi, dbd_flags_t flags)
     volinfo = vi;
     dbd_flags = flags;
 
+    /* Init a fake struct vol with just enough so we can call ea_open and friends */
+    volume.v_adouble = AD_VERSION2;
+    volume.v_vfs_ea = volinfo->v_vfs_ea;
+    initvol_vfs(&volume);
+
     /* Run with umask 0 */
     umask(0);
 
@@ -664,10 +855,10 @@ static int scanvol(struct volinfo *vi, dbd_flags_t flags)
     return 0;
 }
 
-/* 
-   Remove all CNIDs from dbd that are not in dbd_rebuild
+/*
+  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;
@@ -707,7 +898,7 @@ void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
                     dbd_log(LOGSTD, "Orphaned CNID in database: %u", dbd_cnid);
                     if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                         rqst.cnid = htonl(dbd_cnid);
-                        ret = dbd_delete(dbd, &rqst, &rply);
+                        ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
                         dbif_txn_close(dbd, ret);
                         deleted++;
                     }
@@ -723,7 +914,7 @@ void delete_orphaned_cnids(DBD *dbd, DBD *dbd_rebuild, dbd_flags_t flags)
             dbd_log(LOGSTD, "Orphaned CNID in database: %u.", dbd_cnid);
             if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                 rqst.cnid = htonl(dbd_cnid);
-                ret = dbd_delete(dbd, &rqst, &rply);
+                ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
                 dbif_txn_close(dbd, ret);
                 deleted++;
             }
@@ -753,6 +944,10 @@ cleanup:
 int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
 {
     int ret = 0;
+    struct db_param db_param = { 0 };
+
+    /* Set cachesize for in-memory rebuild db */
+    db_param.cachesize = 128 * 1024 * 1024; /* 128 MB */
 
     /* Make it accessible for all funcs */
     dbd = dbd_ref;
@@ -763,19 +958,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, &db_param, 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 */
@@ -784,12 +981,15 @@ int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
     if ( (scanvol(volinfo, flags)) != 0)
         return -1;
 
-    /* We can only do this in excluse 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 (! 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);
+    }
 
 exit_cleanup:
-    dbif_close(dbd_rebuild);
+    if (! nocniddb)
+        dbif_close(dbd_rebuild);
     return ret;
 }