]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/vfs/unix.c
adouble:ea VFS stuff
[netatalk.git] / libatalk / vfs / unix.c
index 14a0f1647dec29ad31974533de1561c8bc9242c7..659cd41162d95a004eaaefbada06432b03147433 100644 (file)
@@ -23,6 +23,8 @@
 #include <atalk/unix.h>
 #include <atalk/acl.h>
 #include <atalk/compat.h>
+#include <atalk/errchk.h>
+#include <atalk/ea.h>
 
 /* -----------------------------
    a dropbox is a folder where w is set but not r eg:
@@ -150,6 +152,38 @@ int netatalk_unlink(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)
+{
+    EC_INIT;
+    ssize_t cc;
+    size_t  buflen;
+    char   filebuf[NETATALK_DIOSZ_STACK];
+
+    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;
+        }
+    }
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
 /* 
  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
  */
@@ -160,7 +194,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)
@@ -182,29 +216,7 @@ int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
         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)
@@ -225,6 +237,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
  */