]> arthur.barton.de Git - netatalk.git/commitdiff
CNID fixes
authorFrank Lahm <franklahm@googlemail.com>
Fri, 8 Oct 2010 20:40:23 +0000 (22:40 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Fri, 8 Oct 2010 20:40:23 +0000 (22:40 +0200)
bin/ad/ad.h
bin/ad/ad_cp.c
bin/ad/ad_ls.c
bin/ad/ad_util.c
libatalk/vfs/vfs.c

index e47c384aec0af68d5e97a667a0da920565ca64d6..cb574d600ecd25f4067d88e0019ebe15e2bf66d9 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <atalk/ftw.h>
 #include <atalk/volinfo.h>
+#include <atalk/cnid.h>
 
 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
 
@@ -42,21 +43,19 @@ enum logtype {STD, DBG};
 
 typedef struct {
     struct volinfo volinfo;
-//    char *basename;
+    struct vol     volume;
+    char           db_stamp[ADEDLEN_PRIVSYN];
 } afpvol_t;
 
 extern int log_verbose;             /* Logging flag */
 extern void _log(enum logtype lt, char *fmt, ...);
 
-extern struct volinfo svolinfo, dvolinfo;
-extern struct vol svolume, dvolume;
-
 extern int ad_ls(int argc, char **argv);
 extern int ad_cp(int argc, char **argv);
 
 /* ad_util.c */
-extern int newvol(const char *path, afpvol_t *vol);
-extern void freevol(afpvol_t *vol);
+extern int openvol(const char *path, afpvol_t *vol);
+extern void closevol(afpvol_t *vol);
 extern cnid_t cnid_for_path(const struct volinfo *vi, const struct vol *vol, const char *path);
 extern char *utompath(const struct volinfo *volinfo, char *upath);
 
@@ -76,12 +75,5 @@ typedef struct {
 
 extern PATH_T to;
 extern int fflag, iflag, lflag, nflag, pflag, vflag;
-extern volatile sig_atomic_t info;
-
-extern int ftw_copy_file(const struct FTW *, const char *, const struct stat *, int);
-extern int copy_link(const struct FTW *, const char *, const struct stat *, int);
-extern int setfile(const struct stat *, int);
-extern int preserve_dir_acls(const struct stat *, char *, char *);
-extern int preserve_fd_acls(int, int);
 
 #endif /* AD_H */
index 6a717bba7c016a9692a0dbf16de51311c2d7e04b..7a79fbf8ecac6d770c40253f35c53963eacbe527 100644 (file)
@@ -70,7 +70,7 @@
 #include <atalk/bstrlib.h>
 #include <atalk/bstradd.h>
 #include <atalk/queue.h>
-
 #include "ad.h"
 
 #define STRIP_TRAILING_SLASH(p) {                                   \
 static char emptystring[] = "";
 
 PATH_T to = { to.p_path, emptystring, "" };
+enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
 
 int fflag, iflag, lflag, nflag, pflag, vflag;
 mode_t mask;
-struct volinfo svolinfo, dvolinfo;
-struct vol svolume, dvolume;
-cnid_t did = 0; /* current dir CNID */
 
+cnid_t pdid, did; /* current dir CNID and parent did*/
+
+static afpvol_t svolume, dvolume;
 static enum op type;
 static int Rflag;
 volatile sig_atomic_t sigint;
 static int badcp, rval;
 static int ftw_options = FTW_MOUNT | FTW_PHYS | FTW_ACTIONRETVAL;
-static q_t *cnidq;              /* CNID dir stack */
-
-enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
 
 static char           *netatalk_dirs[] = {
     ".AppleDouble",
@@ -104,8 +102,14 @@ static char           *netatalk_dirs[] = {
     NULL
 };
 
+/* Forward declarations */
 static int copy(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf);
 static void siginfo(int _U_);
+static int ftw_copy_file(const struct FTW *, const char *, const struct stat *, int);
+static int ftw_copy_link(const struct FTW *, const char *, const struct stat *, int);
+static int setfile(const struct stat *, int);
+static int preserve_dir_acls(const struct stat *, char *, char *);
+static int preserve_fd_acls(int, int);
 
 /*
   Check for netatalk special folders e.g. ".AppleDB" or ".AppleDesktop"
@@ -122,6 +126,10 @@ static const char *check_netatalk_dirs(const char *name)
     return NULL;
 }
 
+static void upfunc(void)
+{
+    did = pdid;
+}
 
 static void usage_cp(void)
 {
@@ -132,17 +140,6 @@ static void usage_cp(void)
     exit(EXIT_FAILURE);
 }
 
-static void upfunc(void)
-{
-    if (cnidq) {
-        cnid_t *cnid = dequeue(cnidq);
-        if (cnid) {
-            did = *cnid;
-            free(cnid);
-        }
-    }
-}
-
 int ad_cp(int argc, char *argv[])
 {
     struct stat to_stat, tmp_stat;
@@ -284,68 +281,17 @@ int ad_cp(int argc, char *argv[])
 #endif
 
     /* Load .volinfo file for destination*/
-    if (loadvolinfo(to.p_path, &dvolinfo) == 0) {
-        if (vol_load_charsets(&dvolinfo) == -1)
-            ERROR("Error loading charsets!");
-        /* Sanity checks to ensure we can touch this volume */
-        if (dvolinfo.v_vfs_ea != AFPVOL_EA_SYS)
-            ERROR("Unsupported Extended Attributes option: %u", dvolinfo.v_vfs_ea);
-
-        /* initialize sufficient struct vol and initialize CNID connection */
-        dvolume.v_adouble = AD_VERSION2;
-        dvolume.v_vfs_ea = AFPVOL_EA_SYS;
-        initvol_vfs(&dvolume);
-        int flags = 0;
-        if ((dvolinfo.v_flags & AFPVOL_NODEV))
-            flags |= CNID_FLAG_NODEV;
-
-        if ((dvolume.v_cdb = cnid_open(dvolinfo.v_dbpath,
-                                       0000,
-                                       "dbd",
-                                       flags,
-                                       dvolinfo.v_dbd_host,
-                                       dvolinfo.v_dbd_port)) == NULL)
-            ERROR("Cant initialize CNID database connection for %s", dvolinfo.v_path);
-
-        /* setup a list for storing the CNID stack of dirs in destination path */
-        if ((cnidq = queue_init()) == NULL)
-            ERROR("Cant initialize CNID stack");
-    }
+    openvol(to.p_path, &dvolume);
 
     for (int i = 0; argv[i] != NULL; i++) { 
         /* Load .volinfo file for source */
-        if (loadvolinfo(to.p_path, &svolinfo) == 0) {
-            if (vol_load_charsets(&svolinfo) == -1)
-                ERROR("Error loading charsets!");
-            /* Sanity checks to ensure we can touch this volume */
-            if (svolinfo.v_vfs_ea != AFPVOL_EA_SYS)
-                ERROR("Unsupported Extended Attributes option: %u", svolinfo.v_vfs_ea);
-
-            /* initialize sufficient struct vol and initialize CNID connection */
-            svolume.v_adouble = AD_VERSION2;
-            svolume.v_vfs_ea = AFPVOL_EA_SYS;
-            initvol_vfs(&svolume);
-            int flags = 0;
-            if ((svolinfo.v_flags & AFPVOL_NODEV))
-                flags |= CNID_FLAG_NODEV;
-
-            if ((svolume.v_cdb = cnid_open(svolinfo.v_dbpath,
-                                           0000,
-                                           "dbd",
-                                           flags,
-                                           svolinfo.v_dbd_host,
-                                           svolinfo.v_dbd_port)) == NULL)
-                ERROR("Cant initialize CNID database connection for %s", svolinfo.v_path);
-        }
+        openvol(to.p_path, &svolume);
 
         if (nftw(argv[i], copy, upfunc, 20, ftw_options) == -1) {
             ERROR("%s: %s", argv[i], strerror(errno));
             exit(EXIT_FAILURE);
         }
 
-        if (svolume.v_cdb)
-            cnid_close(svolume.v_cdb);
-        svolume.v_cdb = NULL;
 
     }
     return rval;
@@ -369,7 +315,7 @@ static int copy(const char *path,
         dir++;
     if (check_netatalk_dirs(dir) != NULL) {
         SLOG("Skipping Netatalk dir %s", path);
-        return FTW_SKIP_SIBLINGS;
+        return FTW_SKIP_SUBTREE;
     }
 
     /*
@@ -449,7 +395,7 @@ static int copy(const char *path,
 
     switch (statp->st_mode & S_IFMT) {
     case S_IFLNK:
-        if (copy_link(ftw, path, statp, !dne))
+        if (ftw_copy_link(ftw, path, statp, !dne))
             badcp = rval = 1;
         break;
     case S_IFDIR:
@@ -475,8 +421,8 @@ static int copy(const char *path,
         }
 
         /* Create ad dir and copy ".Parent" */
-        if (svolinfo.v_path && svolinfo.v_adouble == AD_VERSION2 &&
-            dvolinfo.v_path && dvolinfo.v_adouble == AD_VERSION2) {
+        if (svolume.volinfo.v_path && svolume.volinfo.v_adouble == AD_VERSION2 &&
+            dvolume.volinfo.v_path && dvolume.volinfo.v_adouble == AD_VERSION2) {
             /* Create ".AppleDouble" dir */
             mode_t omask = umask(0);
             bstring addir = bfromcstr(to.p_path);
@@ -492,11 +438,29 @@ static int copy(const char *path,
                 badcp = rval = 1;
                 break;
             }
-            umask(omask);
 
             /* Get CNID of Parent and add new childir to CNID database */
-            did = cnid_for_path(&dvolinfo, &dvolume, to.p_path);
+            pdid = did;
+            did = cnid_for_path(&dvolume.volinfo, &dvolume.volume, to.p_path);
             SLOG("got CNID: %u for path: %s", ntohl(did), to.p_path);
+
+            struct adouble ad;
+            struct stat st;
+            if (stat(to.p_path, &st) != 0) {
+                badcp = rval = 1;
+                break;
+            }
+            ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
+            if (ad_open_metadata(to.p_path, ADFLAGS_DIR, O_RDWR, &ad) != 0) {
+                ERROR("Error opening adouble for: %s", to.p_path);
+            }
+            ad_setid( &ad, st.st_dev, st.st_ino, did, pdid, dvolume.db_stamp);
+            ad_flush(&ad);
+            ad_close_metadata(&ad);
+
+            bdestroy(addir);
+            bdestroy(sdir);
+            umask(omask);
         }
 
         if (pflag) {
@@ -522,6 +486,29 @@ static int copy(const char *path,
     default:
         if (ftw_copy_file(ftw, path, statp, dne))
             badcp = rval = 1;
+
+        SLOG("file: %s", to.p_path);
+
+        if (svolume.volinfo.v_path && svolume.volinfo.v_adouble == AD_VERSION2 &&
+            dvolume.volinfo.v_path && dvolume.volinfo.v_adouble == AD_VERSION2) {
+
+            SLOG("ad for file: %s", to.p_path);
+
+            if (dvolume.volume.vfs->vfs_copyfile(&dvolume.volume, -1, path, to.p_path))
+                badcp = rval = 1;
+            /* Get CNID of Parent and add new childir to CNID database */
+            cnid_t cnid = cnid_for_path(&dvolume.volinfo, &dvolume.volume, to.p_path);
+            SLOG("got CNID: %u for path: %s", ntohl(cnid), to.p_path);
+
+            struct adouble ad;
+            ad_init(&ad, dvolume.volinfo.v_adouble, dvolume.volinfo.v_ad_options);
+            if (ad_open_metadata(to.p_path, 0, O_RDWR, &ad) != 0) {
+                ERROR("Error opening adouble for: %s", to.p_path);
+            }
+            ad_setid( &ad, statp->st_dev, statp->st_ino, cnid, did, dvolume.db_stamp);
+            ad_flush(&ad);
+            ad_close_metadata(&ad);
+        }
         break;
     }
     if (vflag && !badcp)
@@ -534,3 +521,416 @@ static void siginfo(int sig _U_)
 {
     sigint = 1;
 }
+
+/* Memory strategy threshold, in pages: if physmem is larger then this, use a large buffer */
+#define PHYSPAGES_THRESHOLD (32*1024)
+
+/* Maximum buffer size in bytes - do not allow it to grow larger than this */
+#define BUFSIZE_MAX (2*1024*1024)
+
+/* Small (default) buffer size in bytes. It's inefficient for this to be smaller than MAXPHYS */
+#define MAXPHYS (64 * 1024)
+#define BUFSIZE_SMALL (MAXPHYS)
+
+static int ftw_copy_file(const struct FTW *entp,
+                         const char *spath,
+                         const struct stat *sp,
+                         int dne)
+{
+    static char *buf = NULL;
+    static size_t bufsize;
+    ssize_t wcount;
+    size_t wresid;
+    off_t wtotal;
+    int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
+    char *bufp;
+    char *p;
+
+    if ((from_fd = open(spath, O_RDONLY, 0)) == -1) {
+        SLOG("%s: %s", spath, strerror(errno));
+        return (1);
+    }
+
+    /*
+     * If the file exists and we're interactive, verify with the user.
+     * If the file DNE, set the mode to be the from file, minus setuid
+     * bits, modified by the umask; arguably wrong, but it makes copying
+     * executables work right and it's been that way forever.  (The
+     * other choice is 666 or'ed with the execute bits on the from file
+     * modified by the umask.)
+     */
+    if (!dne) {
+#define YESNO "(y/n [n]) "
+        if (nflag) {
+            if (vflag)
+                printf("%s not overwritten\n", to.p_path);
+            (void)close(from_fd);
+            return (0);
+        } else if (iflag) {
+            (void)fprintf(stderr, "overwrite %s? %s", 
+                          to.p_path, YESNO);
+            checkch = ch = getchar();
+            while (ch != '\n' && ch != EOF)
+                ch = getchar();
+            if (checkch != 'y' && checkch != 'Y') {
+                (void)close(from_fd);
+                (void)fprintf(stderr, "not overwritten\n");
+                return (1);
+            }
+        }
+        
+        if (fflag) {
+            /* remove existing destination file name, 
+             * create a new file  */
+            (void)unlink(to.p_path);
+            (void)dvolume.volume.vfs->vfs_deletefile(&dvolume.volume, -1, to.p_path);
+            if (!lflag)
+                to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
+                             sp->st_mode & ~(S_ISUID | S_ISGID));
+        } else {
+            if (!lflag)
+                /* overwrite existing destination file name */
+                to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
+        }
+    } else {
+        if (!lflag)
+            to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
+                         sp->st_mode & ~(S_ISUID | S_ISGID));
+    }
+    
+    if (to_fd == -1) {
+        SLOG("%s: %s", to.p_path, strerror(errno));
+        (void)close(from_fd);
+        return (1);
+    }
+
+    rval = 0;
+
+    if (!lflag) {
+        /*
+         * Mmap and write if less than 8M (the limit is so we don't totally
+         * trash memory on big files.  This is really a minor hack, but it
+         * wins some CPU back.
+         * Some filesystems, such as smbnetfs, don't support mmap,
+         * so this is a best-effort attempt.
+         */
+
+        if (S_ISREG(sp->st_mode) && sp->st_size > 0 &&
+            sp->st_size <= 8 * 1024 * 1024 &&
+            (p = mmap(NULL, (size_t)sp->st_size, PROT_READ,
+                      MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
+            wtotal = 0;
+            for (bufp = p, wresid = sp->st_size; ;
+                 bufp += wcount, wresid -= (size_t)wcount) {
+                wcount = write(to_fd, bufp, wresid);
+                if (wcount <= 0)
+                    break;
+                wtotal += wcount;
+                if (wcount >= (ssize_t)wresid)
+                    break;
+            }
+            if (wcount != (ssize_t)wresid) {
+                SLOG("%s: %s", to.p_path, strerror(errno));
+                rval = 1;
+            }
+            /* Some systems don't unmap on close(2). */
+            if (munmap(p, sp->st_size) < 0) {
+                SLOG("%s: %s", spath, strerror(errno));
+                rval = 1;
+            }
+        } else {
+            if (buf == NULL) {
+                /*
+                 * Note that buf and bufsize are static. If
+                 * malloc() fails, it will fail at the start
+                 * and not copy only some files. 
+                 */ 
+                if (sysconf(_SC_PHYS_PAGES) > 
+                    PHYSPAGES_THRESHOLD)
+                    bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
+                else
+                    bufsize = BUFSIZE_SMALL;
+                buf = malloc(bufsize);
+                if (buf == NULL)
+                    ERROR("Not enough memory");
+
+            }
+            wtotal = 0;
+            while ((rcount = read(from_fd, buf, bufsize)) > 0) {
+                for (bufp = buf, wresid = rcount; ;
+                     bufp += wcount, wresid -= wcount) {
+                    wcount = write(to_fd, bufp, wresid);
+                    if (wcount <= 0)
+                        break;
+                    wtotal += wcount;
+                    if (wcount >= (ssize_t)wresid)
+                        break;
+                }
+                if (wcount != (ssize_t)wresid) {
+                    SLOG("%s: %s", to.p_path, strerror(errno));
+                    rval = 1;
+                    break;
+                }
+            }
+            if (rcount < 0) {
+                SLOG("%s: %s", spath, strerror(errno));
+                rval = 1;
+            }
+        }
+    } else {
+        if (link(spath, to.p_path)) {
+            SLOG("%s", to.p_path);
+            rval = 1;
+        }
+    }
+    
+    /*
+     * Don't remove the target even after an error.  The target might
+     * not be a regular file, or its attributes might be important,
+     * or its contents might be irreplaceable.  It would only be safe
+     * to remove it if we created it and its length is 0.
+     */
+
+    if (!lflag) {
+        if (pflag && setfile(sp, to_fd))
+            rval = 1;
+        if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
+            rval = 1;
+        if (close(to_fd)) {
+            SLOG("%s: %s", to.p_path, strerror(errno));
+            rval = 1;
+        }
+    }
+
+    (void)close(from_fd);
+
+    return (rval);
+}
+
+static int ftw_copy_link(const struct FTW *p,
+                         const char *spath,
+                         const struct stat *sstp,
+                         int exists)
+{
+    int len;
+    char llink[PATH_MAX];
+
+    if ((len = readlink(spath, llink, sizeof(llink) - 1)) == -1) {
+        SLOG("readlink: %s: %s", spath, strerror(errno));
+        return (1);
+    }
+    llink[len] = '\0';
+    if (exists && unlink(to.p_path)) {
+        SLOG("unlink: %s: %s", to.p_path, strerror(errno));
+        return (1);
+    }
+    if (symlink(llink, to.p_path)) {
+        SLOG("symlink: %s: %s", llink, strerror(errno));
+        return (1);
+    }
+    return (pflag ? setfile(sstp, -1) : 0);
+}
+
+static int setfile(const struct stat *fs, int fd)
+{
+    static struct timeval tv[2];
+    struct stat ts;
+    int rval, gotstat, islink, fdval;
+    mode_t mode;
+
+    rval = 0;
+    fdval = fd != -1;
+    islink = !fdval && S_ISLNK(fs->st_mode);
+    mode = fs->st_mode & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
+
+    TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atim);
+    TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtim);
+    if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
+        SLOG("%sutimes: %s", islink ? "l" : "", to.p_path);
+        rval = 1;
+    }
+    if (fdval ? fstat(fd, &ts) :
+        (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
+        gotstat = 0;
+    else {
+        gotstat = 1;
+        ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
+            S_IRWXU | S_IRWXG | S_IRWXO;
+    }
+    /*
+     * Changing the ownership probably won't succeed, unless we're root
+     * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
+     * the mode; current BSD behavior is to remove all setuid bits on
+     * chown.  If chown fails, lose setuid/setgid bits.
+     */
+    if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
+        if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
+            (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
+             chown(to.p_path, fs->st_uid, fs->st_gid))) {
+            if (errno != EPERM) {
+                SLOG("chown: %s: %s", to.p_path, strerror(errno));
+                rval = 1;
+            }
+            mode &= ~(S_ISUID | S_ISGID);
+        }
+
+    if (!gotstat || mode != ts.st_mode)
+        if (fdval ? fchmod(fd, mode) : chmod(to.p_path, mode)) {
+            SLOG("chmod: %s: %s", to.p_path, strerror(errno));
+            rval = 1;
+        }
+
+#ifdef HAVE_ST_FLAGS
+    if (!gotstat || fs->st_flags != ts.st_flags)
+        if (fdval ?
+            fchflags(fd, fs->st_flags) :
+            (islink ? lchflags(to.p_path, fs->st_flags) :
+             chflags(to.p_path, fs->st_flags))) {
+            SLOG("chflags: %s: %s", to.p_path, strerror(errno));
+            rval = 1;
+        }
+#endif
+
+    return (rval);
+}
+
+static int preserve_fd_acls(int source_fd, int dest_fd)
+{
+#if 0
+    acl_t acl;
+    acl_type_t acl_type;
+    int acl_supported = 0, ret, trivial;
+
+    ret = fpathconf(source_fd, _PC_ACL_NFS4);
+    if (ret > 0 ) {
+        acl_supported = 1;
+        acl_type = ACL_TYPE_NFS4;
+    } else if (ret < 0 && errno != EINVAL) {
+        warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
+        return (1);
+    }
+    if (acl_supported == 0) {
+        ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
+        if (ret > 0 ) {
+            acl_supported = 1;
+            acl_type = ACL_TYPE_ACCESS;
+        } else if (ret < 0 && errno != EINVAL) {
+            warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
+                 to.p_path);
+            return (1);
+        }
+    }
+    if (acl_supported == 0)
+        return (0);
+
+    acl = acl_get_fd_np(source_fd, acl_type);
+    if (acl == NULL) {
+        warn("failed to get acl entries while setting %s", to.p_path);
+        return (1);
+    }
+    if (acl_is_trivial_np(acl, &trivial)) {
+        warn("acl_is_trivial() failed for %s", to.p_path);
+        acl_free(acl);
+        return (1);
+    }
+    if (trivial) {
+        acl_free(acl);
+        return (0);
+    }
+    if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
+        warn("failed to set acl entries for %s", to.p_path);
+        acl_free(acl);
+        return (1);
+    }
+    acl_free(acl);
+#endif
+    return (0);
+}
+
+static int preserve_dir_acls(const struct stat *fs, char *source_dir, char *dest_dir)
+{
+#if 0
+    acl_t (*aclgetf)(const char *, acl_type_t);
+    int (*aclsetf)(const char *, acl_type_t, acl_t);
+    struct acl *aclp;
+    acl_t acl;
+    acl_type_t acl_type;
+    int acl_supported = 0, ret, trivial;
+
+    ret = pathconf(source_dir, _PC_ACL_NFS4);
+    if (ret > 0) {
+        acl_supported = 1;
+        acl_type = ACL_TYPE_NFS4;
+    } else if (ret < 0 && errno != EINVAL) {
+        warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
+        return (1);
+    }
+    if (acl_supported == 0) {
+        ret = pathconf(source_dir, _PC_ACL_EXTENDED);
+        if (ret > 0) {
+            acl_supported = 1;
+            acl_type = ACL_TYPE_ACCESS;
+        } else if (ret < 0 && errno != EINVAL) {
+            warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
+                 source_dir);
+            return (1);
+        }
+    }
+    if (acl_supported == 0)
+        return (0);
+
+    /*
+     * If the file is a link we will not follow it
+     */
+    if (S_ISLNK(fs->st_mode)) {
+        aclgetf = acl_get_link_np;
+        aclsetf = acl_set_link_np;
+    } else {
+        aclgetf = acl_get_file;
+        aclsetf = acl_set_file;
+    }
+    if (acl_type == ACL_TYPE_ACCESS) {
+        /*
+         * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
+         * size ACL will be returned. So it is not safe to simply
+         * check the pointer to see if the default ACL is present.
+         */
+        acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
+        if (acl == NULL) {
+            warn("failed to get default acl entries on %s",
+                 source_dir);
+            return (1);
+        }
+        aclp = &acl->ats_acl;
+        if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
+                                          ACL_TYPE_DEFAULT, acl) < 0) {
+            warn("failed to set default acl entries on %s",
+                 dest_dir);
+            acl_free(acl);
+            return (1);
+        }
+        acl_free(acl);
+    }
+    acl = aclgetf(source_dir, acl_type);
+    if (acl == NULL) {
+        warn("failed to get acl entries on %s", source_dir);
+        return (1);
+    }
+    if (acl_is_trivial_np(acl, &trivial)) {
+        warn("acl_is_trivial() failed on %s", source_dir);
+        acl_free(acl);
+        return (1);
+    }
+    if (trivial) {
+        acl_free(acl);
+        return (0);
+    }
+    if (aclsetf(dest_dir, acl_type, acl) < 0) {
+        warn("failed to set acl entries on %s", dest_dir);
+        acl_free(acl);
+        return (1);
+    }
+    acl_free(acl);
+#endif
+    return (0);
+}
index 87834d8605706d61ed2847bb0bb45239cae4af2d..02c4fc663a79799f219e0cc894b88e1e1799e1c3 100644 (file)
@@ -578,9 +578,9 @@ int ad_ls(int argc, char **argv)
     }
 
     if ((argc - optind) == 0) {
-        newvol(".", &vol);
+        openvol(".", &vol);
         ad_ls_r(".", &vol);
-        freevol(&vol);
+        closevol(&vol);
     }
     else {
         int havefile = 0;
@@ -598,9 +598,9 @@ int ad_ls(int argc, char **argv)
             first = 1;
             recursion = 0;
 
-            newvol(argv[optind], &vol);
+            openvol(argv[optind], &vol);
             ad_ls_r(argv[optind], &vol);
-            freevol(&vol);
+            closevol(&vol);
         next:
             optind++;
         }
@@ -620,9 +620,9 @@ int ad_ls(int argc, char **argv)
             first = 1;
             recursion = 0;
 
-            newvol(argv[optind], &vol);
+            openvol(argv[optind], &vol);
             ad_ls_r(argv[optind], &vol);
-            freevol(&vol);
+            closevol(&vol);
 
         next2:
             optind++;
index 25a980b8437a59215ab6110af7ceef77e704838a..81a27b518811e81cc595b3b444f1ef8c8ad7c834 100644 (file)
 #include <atalk/errchk.h>
 #include "ad.h"
 
-#define cp_pct(x, y)((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
-
-/* Memory strategy threshold, in pages: if physmem is larger then this, use a 
- * large buffer */
-#define PHYSPAGES_THRESHOLD (32*1024)
-
-/* Maximum buffer size in bytes - do not allow it to grow larger than this */
-#define BUFSIZE_MAX (2*1024*1024)
-
-/* Small (default) buffer size in bytes. It's inefficient for this to be
- * smaller than MAXPHYS */
-#define MAXPHYS (64 * 1024)
-#define BUFSIZE_SMALL (MAXPHYS)
-
 int log_verbose;             /* Logging flag */
 
 void _log(enum logtype lt, char *fmt, ...)
@@ -89,37 +75,63 @@ void _log(enum logtype lt, char *fmt, ...)
     }
 }
 
-int newvol(const char *path, afpvol_t *vol)
+/*!
+ * Load volinfo and initialize struct vol
+ *
+ * @param path   (r)  path to evaluate
+ * @param vol    (rw) structure to initialize 
+*
+ * @returns 0 on success, exits on error
+ */
+int openvol(const char *path, afpvol_t *vol)
 {
-    //    char *pathdup;
-    
+    int flags = 0;
+
     memset(vol, 0, sizeof(afpvol_t));
 
-    //    pathdup = strdup(path);
-    //    vol->dirname = strdup(dirname(pathdup));
-    //    free(pathdup);
-    
-    //    pathdup = strdup(path);
-    //    vol->basename = strdup(basename(pathdup));
-    //    free(pathdup);
+    /* try to find a .AppleDesktop/.volinfo */
+    if (loadvolinfo((char *)path, &vol->volinfo) == 0) {
+
+        if (vol_load_charsets(&vol->volinfo) == -1)
+            ERROR("Error loading charsets!");
+
+        /* Sanity checks to ensure we can touch this volume */
+        if (vol->volinfo.v_adouble != AD_VERSION2)
+            ERROR("Unsupported adouble versions: %u", vol->volinfo.v_adouble);
 
-    loadvolinfo((char *)path, &vol->volinfo);
+        if (vol->volinfo.v_vfs_ea != AFPVOL_EA_SYS)
+            ERROR("Unsupported Extended Attributes option: %u", vol->volinfo.v_vfs_ea);
+
+        /* initialize sufficient struct vol for VFS initialisation */
+        vol->volume.v_adouble = AD_VERSION2;
+        vol->volume.v_vfs_ea = AFPVOL_EA_SYS;
+        initvol_vfs(&vol->volume);
+
+        if ((vol->volinfo.v_flags & AFPVOL_NODEV))
+            flags |= CNID_FLAG_NODEV;
+
+        if ((vol->volume.v_cdb = cnid_open(vol->volinfo.v_dbpath,
+                                           0000,
+                                           "dbd",
+                                           flags,
+                                           vol->volinfo.v_dbd_host,
+                                           vol->volinfo.v_dbd_port)) == NULL)
+            ERROR("Cant initialize CNID database connection for %s", vol->volinfo.v_path);
+
+        cnid_getstamp(vol->volume.v_cdb,
+                      vol->db_stamp,
+                      sizeof(vol->db_stamp));
+    }
 
     return 0;
 }
 
-void freevol(afpvol_t *vol)
+void closevol(afpvol_t *vol)
 {
-#if 0
-    if (vol->dirname) {
-        free(vol->dirname);
-        vol->dirname = NULL;
-    }
-    if (vol->basename) {
-        free(vol->basename);
-        vol->basename = NULL;
-    }
-#endif
+    if (vol->volume.v_cdb)
+        cnid_close(vol->volume.v_cdb);
+
+    memset(vol, 0, sizeof(afpvol_t));
 }
 
 /*
@@ -278,404 +290,4 @@ EC_CLEANUP:
     return cnid;
 }
 
-int ftw_copy_file(const struct FTW *entp,
-              const char *spath,
-              const struct stat *sp,
-              int dne)
-{
-    static char *buf = NULL;
-    static size_t bufsize;
-    ssize_t wcount;
-    size_t wresid;
-    off_t wtotal;
-    int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
-    char *bufp;
-    char *p;
-
-    if ((from_fd = open(spath, O_RDONLY, 0)) == -1) {
-        SLOG("%s: %s", spath, strerror(errno));
-        return (1);
-    }
-
-    /*
-     * If the file exists and we're interactive, verify with the user.
-     * If the file DNE, set the mode to be the from file, minus setuid
-     * bits, modified by the umask; arguably wrong, but it makes copying
-     * executables work right and it's been that way forever.  (The
-     * other choice is 666 or'ed with the execute bits on the from file
-     * modified by the umask.)
-     */
-    if (!dne) {
-#define YESNO "(y/n [n]) "
-        if (nflag) {
-            if (vflag)
-                printf("%s not overwritten\n", to.p_path);
-            (void)close(from_fd);
-            return (0);
-        } else if (iflag) {
-            (void)fprintf(stderr, "overwrite %s? %s", 
-                          to.p_path, YESNO);
-            checkch = ch = getchar();
-            while (ch != '\n' && ch != EOF)
-                ch = getchar();
-            if (checkch != 'y' && checkch != 'Y') {
-                (void)close(from_fd);
-                (void)fprintf(stderr, "not overwritten\n");
-                return (1);
-            }
-        }
-        
-        if (fflag) {
-            /* remove existing destination file name, 
-             * create a new file  */
-            (void)unlink(to.p_path);
-            if (!lflag)
-                to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
-                             sp->st_mode & ~(S_ISUID | S_ISGID));
-        } else {
-            if (!lflag)
-                /* overwrite existing destination file name */
-                to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
-        }
-    } else {
-        if (!lflag)
-            to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
-                         sp->st_mode & ~(S_ISUID | S_ISGID));
-    }
-    
-    if (to_fd == -1) {
-        SLOG("%s: %s", to.p_path, strerror(errno));
-        (void)close(from_fd);
-        return (1);
-    }
-
-    rval = 0;
-
-    if (!lflag) {
-        /*
-         * Mmap and write if less than 8M (the limit is so we don't totally
-         * trash memory on big files.  This is really a minor hack, but it
-         * wins some CPU back.
-         * Some filesystems, such as smbnetfs, don't support mmap,
-         * so this is a best-effort attempt.
-         */
-
-        if (S_ISREG(sp->st_mode) && sp->st_size > 0 &&
-            sp->st_size <= 8 * 1024 * 1024 &&
-            (p = mmap(NULL, (size_t)sp->st_size, PROT_READ,
-                      MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
-            wtotal = 0;
-            for (bufp = p, wresid = sp->st_size; ;
-                 bufp += wcount, wresid -= (size_t)wcount) {
-                wcount = write(to_fd, bufp, wresid);
-                if (wcount <= 0)
-                    break;
-                wtotal += wcount;
-                if (wcount >= (ssize_t)wresid)
-                    break;
-            }
-            if (wcount != (ssize_t)wresid) {
-                SLOG("%s: %s", to.p_path, strerror(errno));
-                rval = 1;
-            }
-            /* Some systems don't unmap on close(2). */
-            if (munmap(p, sp->st_size) < 0) {
-                SLOG("%s: %s", spath, strerror(errno));
-                rval = 1;
-            }
-        } else {
-            if (buf == NULL) {
-                /*
-                 * Note that buf and bufsize are static. If
-                 * malloc() fails, it will fail at the start
-                 * and not copy only some files. 
-                 */ 
-                if (sysconf(_SC_PHYS_PAGES) > 
-                    PHYSPAGES_THRESHOLD)
-                    bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
-                else
-                    bufsize = BUFSIZE_SMALL;
-                buf = malloc(bufsize);
-                if (buf == NULL)
-                    ERROR("Not enough memory");
-
-            }
-            wtotal = 0;
-            while ((rcount = read(from_fd, buf, bufsize)) > 0) {
-                for (bufp = buf, wresid = rcount; ;
-                     bufp += wcount, wresid -= wcount) {
-                    wcount = write(to_fd, bufp, wresid);
-                    if (wcount <= 0)
-                        break;
-                    wtotal += wcount;
-                    if (wcount >= (ssize_t)wresid)
-                        break;
-                }
-                if (wcount != (ssize_t)wresid) {
-                    SLOG("%s: %s", to.p_path, strerror(errno));
-                    rval = 1;
-                    break;
-                }
-            }
-            if (rcount < 0) {
-                SLOG("%s: %s", spath, strerror(errno));
-                rval = 1;
-            }
-        }
-    } else {
-        if (link(spath, to.p_path)) {
-            SLOG("%s", to.p_path);
-            rval = 1;
-        }
-    }
-    
-    /*
-     * Don't remove the target even after an error.  The target might
-     * not be a regular file, or its attributes might be important,
-     * or its contents might be irreplaceable.  It would only be safe
-     * to remove it if we created it and its length is 0.
-     */
-
-    if (!lflag) {
-        if (pflag && setfile(sp, to_fd))
-            rval = 1;
-        if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
-            rval = 1;
-        if (close(to_fd)) {
-            SLOG("%s: %s", to.p_path, strerror(errno));
-            rval = 1;
-        }
-    }
-
-    (void)close(from_fd);
-
-    return (rval);
-}
-
-int copy_link(const struct FTW *p,
-              const char *spath,
-              const struct stat *sstp,
-              int exists)
-{
-    int len;
-    char llink[PATH_MAX];
-
-    if ((len = readlink(spath, llink, sizeof(llink) - 1)) == -1) {
-        SLOG("readlink: %s: %s", spath, strerror(errno));
-        return (1);
-    }
-    llink[len] = '\0';
-    if (exists && unlink(to.p_path)) {
-        SLOG("unlink: %s: %s", to.p_path, strerror(errno));
-        return (1);
-    }
-    if (symlink(llink, to.p_path)) {
-        SLOG("symlink: %s: %s", llink, strerror(errno));
-        return (1);
-    }
-    return (pflag ? setfile(sstp, -1) : 0);
-}
-
-int setfile(const struct stat *fs, int fd)
-{
-    static struct timeval tv[2];
-    struct stat ts;
-    int rval, gotstat, islink, fdval;
-    mode_t mode;
-
-    rval = 0;
-    fdval = fd != -1;
-    islink = !fdval && S_ISLNK(fs->st_mode);
-    mode = fs->st_mode & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO);
-
-    TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atim);
-    TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtim);
-    if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
-        SLOG("%sutimes: %s", islink ? "l" : "", to.p_path);
-        rval = 1;
-    }
-    if (fdval ? fstat(fd, &ts) :
-        (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
-        gotstat = 0;
-    else {
-        gotstat = 1;
-        ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
-            S_IRWXU | S_IRWXG | S_IRWXO;
-    }
-    /*
-     * Changing the ownership probably won't succeed, unless we're root
-     * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
-     * the mode; current BSD behavior is to remove all setuid bits on
-     * chown.  If chown fails, lose setuid/setgid bits.
-     */
-    if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
-        if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
-            (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
-             chown(to.p_path, fs->st_uid, fs->st_gid))) {
-            if (errno != EPERM) {
-                SLOG("chown: %s: %s", to.p_path, strerror(errno));
-                rval = 1;
-            }
-            mode &= ~(S_ISUID | S_ISGID);
-        }
-
-    if (!gotstat || mode != ts.st_mode)
-        if (fdval ? fchmod(fd, mode) : chmod(to.p_path, mode)) {
-            SLOG("chmod: %s: %s", to.p_path, strerror(errno));
-            rval = 1;
-        }
-
-#ifdef HAVE_ST_FLAGS
-    if (!gotstat || fs->st_flags != ts.st_flags)
-        if (fdval ?
-            fchflags(fd, fs->st_flags) :
-            (islink ? lchflags(to.p_path, fs->st_flags) :
-             chflags(to.p_path, fs->st_flags))) {
-            SLOG("chflags: %s: %s", to.p_path, strerror(errno));
-            rval = 1;
-        }
-#endif
-
-    return (rval);
-}
-
-int preserve_fd_acls(int source_fd, int dest_fd)
-{
-#if 0
-    acl_t acl;
-    acl_type_t acl_type;
-    int acl_supported = 0, ret, trivial;
-
-    ret = fpathconf(source_fd, _PC_ACL_NFS4);
-    if (ret > 0 ) {
-        acl_supported = 1;
-        acl_type = ACL_TYPE_NFS4;
-    } else if (ret < 0 && errno != EINVAL) {
-        warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
-        return (1);
-    }
-    if (acl_supported == 0) {
-        ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
-        if (ret > 0 ) {
-            acl_supported = 1;
-            acl_type = ACL_TYPE_ACCESS;
-        } else if (ret < 0 && errno != EINVAL) {
-            warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
-                 to.p_path);
-            return (1);
-        }
-    }
-    if (acl_supported == 0)
-        return (0);
-
-    acl = acl_get_fd_np(source_fd, acl_type);
-    if (acl == NULL) {
-        warn("failed to get acl entries while setting %s", to.p_path);
-        return (1);
-    }
-    if (acl_is_trivial_np(acl, &trivial)) {
-        warn("acl_is_trivial() failed for %s", to.p_path);
-        acl_free(acl);
-        return (1);
-    }
-    if (trivial) {
-        acl_free(acl);
-        return (0);
-    }
-    if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
-        warn("failed to set acl entries for %s", to.p_path);
-        acl_free(acl);
-        return (1);
-    }
-    acl_free(acl);
-#endif
-    return (0);
-}
 
-int preserve_dir_acls(const struct stat *fs, char *source_dir, char *dest_dir)
-{
-#if 0
-    acl_t (*aclgetf)(const char *, acl_type_t);
-    int (*aclsetf)(const char *, acl_type_t, acl_t);
-    struct acl *aclp;
-    acl_t acl;
-    acl_type_t acl_type;
-    int acl_supported = 0, ret, trivial;
-
-    ret = pathconf(source_dir, _PC_ACL_NFS4);
-    if (ret > 0) {
-        acl_supported = 1;
-        acl_type = ACL_TYPE_NFS4;
-    } else if (ret < 0 && errno != EINVAL) {
-        warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
-        return (1);
-    }
-    if (acl_supported == 0) {
-        ret = pathconf(source_dir, _PC_ACL_EXTENDED);
-        if (ret > 0) {
-            acl_supported = 1;
-            acl_type = ACL_TYPE_ACCESS;
-        } else if (ret < 0 && errno != EINVAL) {
-            warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
-                 source_dir);
-            return (1);
-        }
-    }
-    if (acl_supported == 0)
-        return (0);
-
-    /*
-     * If the file is a link we will not follow it
-     */
-    if (S_ISLNK(fs->st_mode)) {
-        aclgetf = acl_get_link_np;
-        aclsetf = acl_set_link_np;
-    } else {
-        aclgetf = acl_get_file;
-        aclsetf = acl_set_file;
-    }
-    if (acl_type == ACL_TYPE_ACCESS) {
-        /*
-         * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
-         * size ACL will be returned. So it is not safe to simply
-         * check the pointer to see if the default ACL is present.
-         */
-        acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
-        if (acl == NULL) {
-            warn("failed to get default acl entries on %s",
-                 source_dir);
-            return (1);
-        }
-        aclp = &acl->ats_acl;
-        if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
-                                          ACL_TYPE_DEFAULT, acl) < 0) {
-            warn("failed to set default acl entries on %s",
-                 dest_dir);
-            acl_free(acl);
-            return (1);
-        }
-        acl_free(acl);
-    }
-    acl = aclgetf(source_dir, acl_type);
-    if (acl == NULL) {
-        warn("failed to get acl entries on %s", source_dir);
-        return (1);
-    }
-    if (acl_is_trivial_np(acl, &trivial)) {
-        warn("acl_is_trivial() failed on %s", source_dir);
-        acl_free(acl);
-        return (1);
-    }
-    if (trivial) {
-        acl_free(acl);
-        return (0);
-    }
-    if (aclsetf(dest_dir, acl_type, acl) < 0) {
-        warn("failed to set acl entries on %s", dest_dir);
-        acl_free(acl);
-        return (1);
-    }
-    acl_free(acl);
-#endif
-    return (0);
-}
index 43d37b675cece951c8b8c65f07f79d23ea28a2e8..d833e1569f779b17bb39f21a6ba72ae2de5c843a 100644 (file)
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <libgen.h>
 
 #include <atalk/afp.h>    
 #include <atalk/adouble.h>
@@ -321,6 +322,44 @@ static int RF_renamefile_adouble(VFS_FUNC_ARGS_RENAMEFILE)
        return 0;
 }
 
+static int RF_copyfile_adouble(VFS_FUNC_ARGS_COPYFILE)
+/* const struct vol *vol, int sfd, const char *src, const char *dst */
+{
+    EC_INIT;
+    bstring s = NULL, d = NULL;
+    const char *name;
+    char *dir = NULL;
+
+    /* get basename */
+    EC_NULL(name = strchr(src, '/'));
+    name++;
+
+    /* build src path to AppleDouble file*/
+    EC_NULL(dir = dirname(strdup(src)));
+    EC_NULL(s = bfromcstr(dir));
+    EC_ZERO(bcatcstr(s, "/.AppleDouble/"));
+    EC_ZERO(bcatcstr(s, name));
+    free(dirname);
+    dir = NULL;
+
+    /* build dst path to AppleDouble file*/
+    EC_NULL(dir = dirname(strdup(dst)));
+    EC_NULL(d = bfromcstr(dir));
+    EC_ZERO(bcatcstr(d, "/.AppleDouble/"));
+    EC_ZERO(bcatcstr(d, name));
+
+    free(dirname);
+    dir = NULL;
+
+    EC_ZERO(copy_file(sfd, src, dst, 0666));
+
+EC_CLEANUP:
+    bdestroy(s);
+    bdestroy(d);
+    if (dir) free(dir);
+    EC_EXIT;
+}
+
 #ifdef HAVE_SOLARIS_ACLS
 static int RF_solaris_acl(VFS_FUNC_ARGS_ACL)
 {
@@ -947,7 +986,7 @@ static struct vfs_ops netatalk_adouble = {
     /* vfs_setdirowner:   */ RF_setdirowner_adouble,
     /* vfs_deletefile:    */ RF_deletefile_adouble,
     /* vfs_renamefile:    */ RF_renamefile_adouble,
-    /* vfs_copyfile:      */ NULL,
+    /* vfs_copyfile:      */ RF_copyfile_adouble,
     NULL
 };