]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/cmd_dbd_scanvol.c
Fix bogus CNIDs in existing meta EA
[netatalk.git] / etc / cnid_dbd / cmd_dbd_scanvol.c
index 3061c2fb16cb1db93a0c7f0bbbb7f889bf6f4e60..90293a48a0d1b8a25639d3f9fd32dff90103e4f1 100644 (file)
@@ -157,85 +157,6 @@ static char *mtoupath(char *mpath)
     return( upath );
 }
 
-#if 0
-/* 
-   Check if "name" is pointing to
-   a) an object inside the current volume (return 0)
-   b) an object outside the current volume (return 1)
-   Then stats the pointed to object and if it is a dir ors ADFLAGS_DIR to *adflags
-   Return -1 on any serious error.
- */
-static int check_symlink(const char *name, int *adflags)
-{
-    int cwd;
-    ssize_t len;
-    char pathbuf[MAXPATHLEN + 1];
-    char *sep;
-    struct stat st;
-
-    if ((len = readlink(name, pathbuf, MAXPATHLEN)) == -1) {
-        dbd_log(LOGSTD, "Error reading link info for '%s/%s': %s", 
-                cwdbuf, name, strerror(errno));
-        return -1;
-    }
-    pathbuf[len] = 0;
-
-    if ((stat(pathbuf, &st)) != 0) {
-        dbd_log(LOGSTD, "stat error '%s': %s", pathbuf, strerror(errno));
-    }
-
-    /* Remember cwd */
-    if ((cwd = open(".", O_RDONLY)) < 0) {
-        dbd_log(LOGSTD, "error opening cwd '%s': %s", cwdbuf, strerror(errno));
-        return -1;
-    }
-
-    if (S_ISDIR(st.st_mode)) {
-        *adflags |= ADFLAGS_DIR;
-    } else { /* file */
-        /* get basename from path */
-        if ((sep = strrchr(pathbuf, '/')) == NULL)
-            /* just a file at the same level */
-            return 0;
-        sep = 0; /* pathbuf now contains the directory*/
-    }
-
-    /* fchdir() to pathbuf so we can easily get its path with getcwd() */
-    if ((chdir(pathbuf)) != 0) {
-        dbd_log(LOGSTD, "Cant chdir to '%s': %s", pathbuf, strerror(errno));
-        return -1;
-    }
-
-    if ((getcwd(pathbuf, MAXPATHLEN)) == NULL) {
-        dbd_log(LOGSTD, "Cant get symlink'ed dir '%s/%s': %s", cwdbuf, pathbuf, strerror(errno));
-        if ((fchdir(cwd)) != 0)
-            /* We're foobared */
-            longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
-        return -1;
-    }
-
-    if ((fchdir(cwd)) != 0)
-        /* We're foobared */
-        longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
-
-    /*
-      We now have the symlink target dir as absoulte path in pathbuf
-      and can compare it with the currents volume path
-    */
-    int i = 0;
-    while (myvolinfo->v_path[i]) {
-        if ((pathbuf[i] == 0) || (myvolinfo->v_path[i] != pathbuf[i])) {
-            dbd_log( LOGDEBUG, "extra-share symlink '%s/%s', following", cwdbuf, name);
-            return 1;
-        }
-        i++;
-    }
-
-    dbd_log( LOGDEBUG, "intra-share symlink '%s/%s', not following", cwdbuf, name);
-    return 0;
-}
-#endif
-
 /*
   Check for wrong encoding e.g. "." at the beginning is not CAP encoded (:2e) although volume is default !AFPVOL_USEDOTS.
   We do it by roundtripiping from volcharset to UTF8-MAC and back and then compare the result.
@@ -298,13 +219,16 @@ static int check_adfile(const char *fname, const struct stat *st)
     struct adouble ad;
     const char *adname;
 
+    if (volume.v_adouble == AD_VERSION_EA)
+        return 0;
+
     if (dbd_flags & DBD_FLAGS_CLEANUP)
         return 0;
 
     if (S_ISREG(st->st_mode))
         adflags |= ADFLAGS_DIR;
 
-    adname = myvolinfo->ad_path(fname, adflags);
+    adname = volume.ad_path(fname, adflags);
 
     if ((ret = access( adname, F_OK)) != 0) {
         if (errno != ENOENT) {
@@ -320,7 +244,7 @@ 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(&ad, &volume);
 
         if ((ret = ad_open(&ad, fname, adflags | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666)) != 0) {
             dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
@@ -340,7 +264,7 @@ 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);
+        ad_init(&ad, &volume);
         if (ad_open(&ad, fname, adflags | ADFLAGS_RDONLY) != 0) {
             dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
             return -1;
@@ -465,6 +389,9 @@ static int check_addir(int volroot)
     if (dbd_flags & DBD_FLAGS_CLEANUP)
         return 0;
 
+    if (volume.v_adouble == AD_VERSION_EA)
+        return 0;
+
     /* Check for ad-dir */
     if ( (addir_ok = access(ADv2_DIRNAME, F_OK)) != 0) {
         if (errno != ENOENT) {
@@ -475,10 +402,10 @@ static int check_addir(int volroot)
     }
 
     /* Check for ".Parent" */
-    if ( (adpar_ok = access(myvolinfo->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
+    if ( (adpar_ok = access(volume.ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
         if (errno != ENOENT) {
             dbd_log(LOGSTD, "Access error on '%s/%s': %s",
-                    cwdbuf, myvolinfo->ad_path(".", ADFLAGS_DIR), strerror(errno));
+                    cwdbuf, volume.ad_path(".", ADFLAGS_DIR), strerror(errno));
             return -1;
         }
         dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
@@ -497,7 +424,7 @@ static int check_addir(int volroot)
         }
 
         /* Create ad dir and set name */
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+        ad_init(&ad, &volume);
 
         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));
@@ -518,7 +445,7 @@ static int check_addir(int volroot)
             return -1;
         }
         chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
-        chown(myvolinfo->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
+        chown(volume.ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
     }
 
     return 0;
@@ -595,6 +522,9 @@ static int read_addir(void)
     struct dirent *ep;
     struct stat st;
 
+    if (volume.v_adouble == AD_VERSION_EA)
+        return 0;
+
     if ((chdir(ADv2_DIRNAME)) != 0) {
         dbd_log(LOGSTD, "Couldn't chdir to '%s/%s': %s",
                 cwdbuf, ADv2_DIRNAME, strerror(errno));
@@ -611,6 +541,7 @@ static int read_addir(void)
         /* Check if its "." or ".." */
         if (DIR_DOT_OR_DOTDOT(ep->d_name))
             continue;
+
         /* Skip ".Parent" */
         if (STRCMP(ep->d_name, ==, ".Parent"))
             continue;
@@ -674,12 +605,14 @@ static int read_addir(void)
 
   @return Correct CNID of object or CNID_INVALID (ie 0) on error
 */
-static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok, int adflags)
+static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok)
 {
-    int ret;
+    int ret, adflags = ADFLAGS_HF;
     cnid_t db_cnid, ad_cnid;
     struct adouble ad;
 
+    adflags = ADFLAGS_HF | (S_ISDIR(st->st_mode) ? ADFLAGS_DIR : 0);
+
     /* Force checkout every X items */
     static int cnidcount = 0;
     cnidcount++;
@@ -691,34 +624,37 @@ 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 */
+    /* Get CNID from ad-file */
     ad_cnid = 0;
-    if ( (myvolinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
-        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+    if (ADFILE_OK) {
+        ad_init(&ad, &volume);
         if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
             
             if (dbd_flags & DBD_FLAGS_CLEANUP)
                 return CNID_INVALID;
 
-            dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
-            return CNID_INVALID;
-        }
-
-        if (dbd_flags & DBD_FLAGS_FORCE) {
-            ad_cnid = ad_forcegetid(&ad);
-            /* This ensures the changed stamp is written */
-            ad_setid( &ad, st->st_dev, st->st_ino, ad_cnid, did, stamp);
-            ad_flush(&ad);
-        }
-        else
-            ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
+            if (volume.v_adouble != AD_VERSION_EA) {
+                dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
+                return CNID_INVALID;
+            }
+            dbd_log( LOGDEBUG, "File without meta EA: \"%s/%s\"", cwdbuf, name);
+            adfile_ok = 1;
+        } else {
 
-        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));
+            if (dbd_flags & DBD_FLAGS_FORCE) {
+                ad_cnid = ad_forcegetid(&ad);
+                /* This ensures the changed stamp is written */
+                ad_setid( &ad, st->st_dev, st->st_ino, ad_cnid, did, stamp);
+                ad_flush(&ad);
+            } else
+                ad_cnid = ad_getid(&ad, st->st_dev, st->st_ino, 0, stamp);
 
-        ad_close(&ad, ADFLAGS_HF);
+            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(&ad, ADFLAGS_HF);
+        }
     }
 
     /* Get CNID from database */
@@ -760,7 +696,7 @@ 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, "Updating AppleDouble file for '%s/%s' with CNID: %u from database",
                             cwdbuf, name, ntohl(db_cnid));
-            ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
+            ad_init(&ad, &volume);
             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));
@@ -788,12 +724,10 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
                 db_cnid = rply.cnid;
                 dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
 
-                if ((myvolinfo->v_flags & AFPVOL_CACHE)
-                    && ADFILE_OK
-                    && ( ! (dbd_flags & DBD_FLAGS_SCAN))) {
+                if (ADFILE_OK && ( ! (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);
+                    ad_init(&ad, &volume);
                     if (ad_open(&ad, name, adflags | ADFLAGS_RDWR) != 0) {
                         dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
                                 cwdbuf, name, strerror(errno));
@@ -828,21 +762,19 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
     }
 
     if ((ad_cnid == 0) && db_cnid) {
-        /* in db but zeroID in ad-file, write it to ad-file if AFPVOL_CACHE */
-        if ((myvolinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
-            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(&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(&ad, ADFLAGS_HF);
+        /* in db but zeroID in ad-file, write it to ad-file */
+        if (ADFILE_OK && ! (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, &volume);
+            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(&ad, ADFLAGS_HF);
         }
         return db_cnid;
     }
@@ -858,7 +790,7 @@ static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfi
 */
 static int dbd_readdir(int volroot, cnid_t did)
 {
-    int cwd, ret = 0, adflags, adfile_ok, addir_ok, encoding_ok;
+    int cwd, ret = 0, adfile_ok, addir_ok, encoding_ok;
     cnid_t cnid = 0;
     const char *name;
     DIR *dp;
@@ -872,7 +804,7 @@ static int dbd_readdir(int volroot, cnid_t did)
             return -1;
 
     /* Check AppleDouble files in AppleDouble folder, but only if it exists or could be created */
-    if (ADDIR_OK)
+    if (volume.v_adouble == AD_VERSION2 && ADDIR_OK)
         if ((read_addir()) != 0)
             if ( ! (dbd_flags & DBD_FLAGS_SCAN))
                 /* Fatal on rebuild run, continue if only scanning ! */
@@ -919,20 +851,10 @@ static int dbd_readdir(int volroot, cnid_t did)
         
         switch (st.st_mode & S_IFMT) {
         case S_IFREG:
-            adflags = 0;
-            break;
         case S_IFDIR:
-            adflags = ADFLAGS_DIR;
             break;
         case S_IFLNK:
             dbd_log(LOGDEBUG, "Ignoring symlink %s/%s", cwdbuf, ep->d_name);
-#if 0
-            ret = check_symlink(ep->d_name, &adflags);
-            if (ret == 1)
-                break;
-            if (ret == -1)
-                dbd_log(LOGSTD, "Error checking symlink %s/%s", cwdbuf, ep->d_name);
-#endif
             continue;
         default:
             dbd_log(LOGSTD, "Bad filetype: %s/%s", cwdbuf, ep->d_name);
@@ -978,7 +900,7 @@ static int dbd_readdir(int volroot, cnid_t did)
 
         if ( ! nocniddb) {
             /* Check CNIDs */
-            cnid = check_cnid(ep->d_name, did, &st, adfile_ok, adflags);
+            cnid = check_cnid(ep->d_name, did, &st, adfile_ok);
 
             /* Now add this object to our rebuild dbd */
             if (cnid && dbd_rebuild) {
@@ -1044,18 +966,14 @@ 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;
 
-    /* Init a fake struct vol with just enough so we can call ea_open and friends */
-    volume.v_adouble = AD_VERSION2;
+    /* Init a fake struct vol so that we can call ad_init(.., &volume) and initvol_vfs(&volume) */
+    volume.v_adouble = vi->v_adouble;
+    volume.v_ad_options = vi->v_flags;
+    volume.ad_path = vi->ad_path;
     volume.v_vfs_ea = myvolinfo->v_vfs_ea;
     initvol_vfs(&volume);