]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/vfs/unix.c
Merge 2-2
[netatalk.git] / libatalk / vfs / unix.c
index 229f1b7a216e25309c3c581c5c9fcc6e3a1ca6e5..1c0556d95e2eebc2e9baeccc34227a112ceeed8a 100644 (file)
 #include <atalk/logger.h>
 #include <atalk/unix.h>
 #include <atalk/acl.h>
-
-/* -----------------------------
-   a dropbox is a folder where w is set but not r eg:
-   rwx-wx-wx or rwx-wx--
-   rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
-*/
-int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
-{
-    int retval = 0;
-
-#ifdef DROPKLUDGE
-    /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
-    if ((dropbox & AFPVOL_DROPBOX)) {
-        int uid;
-
-        if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
-             ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
-        {
-            uid=geteuid();
-            if ( seteuid(0) < 0) {
-                LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
-            }
-            if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
-                LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
-            } else {
-                LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
-            }
-            seteuid(uid);
-            return retval;
-        }
-    }
-#endif /* DROPKLUDGE */
-
-    /*
-     *  Ignore EPERM errors:  We may be dealing with a directory that is
-     *  group writable, in which case chmod will fail.
-     */
-    if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
-         !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
-    {
-        LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
-        retval = -1;
-    }
-
-    return retval;
-}
+#include <atalk/compat.h>
+#include <atalk/errchk.h>
+#include <atalk/ea.h>
 
 /* ------------------------- */
 int dir_rx_set(mode_t mode)
@@ -92,7 +49,7 @@ int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
     
     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
 
-    if ( chmod( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
+    if ( chmod_acl( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
         return -1;
     }
     return 0;
@@ -168,24 +125,41 @@ int netatalk_unlink(const char *name)
     return AFP_OK;
 }
 
-char *fullpathname(const char *name)
+/**************************************************************************
+ * *at semnatics support functions (like openat, renameat standard funcs)
+ **************************************************************************/
+
+/* Copy all file data from one file fd to another */
+int copy_file_fd(int sfd, int dfd)
 {
-    static char wd[ MAXPATHLEN + 1];
+    EC_INIT;
+    ssize_t cc;
+    size_t  buflen;
+    char   filebuf[NETATALK_DIOSZ_STACK];
 
-    if ( getcwd( wd , MAXPATHLEN) ) {
-        strlcat(wd, "/", MAXPATHLEN);
-        strlcat(wd, name, MAXPATHLEN);
-    }
-    else {
-        strlcpy(wd, name, MAXPATHLEN);
-    }
-    return wd;
-}
+    while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
+        if (cc < 0) {
+            if (errno == EINTR)
+                continue;
+            LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
+            EC_FAIL;
+        }
 
+        buflen = cc;
+        while (buflen > 0) {
+            if ((cc = write(dfd, filebuf, buflen)) < 0) {
+                if (errno == EINTR)
+                    continue;
+                LOG(log_error, logtype_afpd, "copy_file_fd: %s", strerror(errno));
+                EC_FAIL;
+            }
+            buflen -= cc;
+        }
+    }
 
-/**************************************************************************
- * *at semnatics support functions (like openat, renameat standard funcs)
- **************************************************************************/
+EC_CLEANUP:
+    EC_EXIT;
+}
 
 /* 
  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
@@ -197,7 +171,7 @@ int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
     int    dfd = -1;
     ssize_t cc;
     size_t  buflen;
-    char   filebuf[8192];
+    char   filebuf[NETATALK_DIOSZ_STACK];
 
 #ifdef HAVE_ATFUNCS
     if (dirfd == -1)
@@ -207,41 +181,19 @@ int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
     sfd = open(src, O_RDONLY);
 #endif
     if (sfd < 0) {
-        LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
+        LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
             src, dst, src, strerror(errno));
         return -1;
     }
 
     if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
-        LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
+        LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
             src, dst, dst, strerror(errno));
         ret = -1;
         goto exit;
     }
 
-    while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
-        if (cc < 0) {
-            if (errno == EINTR)
-                continue;
-            LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
-                src, dst, src, strerror(errno));
-            ret = -1;
-            goto exit;
-        }
-
-        buflen = cc;
-        while (buflen > 0) {
-            if ((cc = write(dfd, filebuf, buflen)) < 0) {
-                if (errno == EINTR)
-                    continue;
-                LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
-                    src, dst, dst, strerror(errno));
-                ret = -1;
-                goto exit;
-            }
-            buflen -= cc;
-        }
-    }
+    ret = copy_file_fd(sfd, dfd);
 
 exit:
     if (sfd != -1)
@@ -262,6 +214,43 @@ exit:
     return ret;
 }
 
+/*!
+ * Copy an EA from one file to another
+ *
+ * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
+ */
+int copy_ea(const char *ea, int dirfd, const char *src, const char *dst, mode_t mode)
+{
+    EC_INIT;
+    int    sfd = -1;
+    int    dfd = -1;
+    size_t easize;
+    char   *eabuf = NULL;
+
+#ifdef HAVE_ATFUNCS
+    if (dirfd == -1)
+        dirfd = AT_FDCWD;
+    EC_NEG1_LOG( sfd = openat(dirfd, src, O_RDONLY) );
+#else
+    EC_NEG1_LOG( sfd = open(src, O_RDONLY) );
+#endif
+    EC_NEG1_LOG( dfd = open(dst, O_WRONLY, mode) );
+
+    if ((easize = sys_fgetxattr(sfd, ea, NULL, 0)) > 0) {
+        EC_NULL_LOG( eabuf = malloc(easize));
+        EC_NEG1_LOG( easize = sys_fgetxattr(sfd, ea, eabuf, easize) );
+        EC_NEG1_LOG( easize = sys_fsetxattr(dfd, ea, eabuf, easize, 0) );
+    }
+
+EC_CLEANUP:
+    if (sfd != -1)
+        close(sfd);
+    if (dfd != -1)
+        close(dfd);
+    free(eabuf);
+    EC_EXIT;
+}
+
 /* 
  * at wrapper for netatalk_unlink
  */