]> arthur.barton.de Git - netatalk.git/commitdiff
Reading/writing rfork
authorFrank Lahm <franklahm@googlemail.com>
Sat, 18 Dec 2010 18:23:48 +0000 (19:23 +0100)
committerFrank Lahm <franklahm@googlemail.com>
Sat, 18 Dec 2010 18:23:48 +0000 (19:23 +0100)
include/atalk/adouble.h
libatalk/adouble/ad_flush.c
libatalk/adouble/ad_open.c
libatalk/adouble/ad_read.c
libatalk/adouble/ad_write.c

index ec7f389350a21e5209e111b431cc69501e1a8c4f..20ce9a221e3fd0d73c097af165e2e0543dec682d 100644 (file)
 #define AD_DATASZ   AD_DATASZ_EA
 #endif
 
+#define RFORK_EA_ALLOCSIZE (128*1024) /* 128k */
+
 typedef u_int32_t cnid_t;
 
 struct ad_entry {
index 4afda4d5a7ffd27e8bad86362cedb4525dd3da9d..8d1e1578c35137bac92f5d5178697418066c65f6 100644 (file)
@@ -34,6 +34,8 @@
 #include <atalk/adouble.h>
 #include <atalk/ea.h>
 #include <atalk/logger.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
 
 #include "ad_lock.h"
 
@@ -163,7 +165,7 @@ int ad_flush(struct adouble *ad)
             }
             break;
         case AD_VERSION_EA:
-            if (sys_fsetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
+            if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
                 LOG(log_error, logtype_afpd, "ad_flush: sys_fsetxattr error: %s",
                     strerror(errno));
                 return -1;
index 93fb035473649c831db09e06dfb27073ae9730a4..660caea1781848fa6c9d64742d0a70ff065e8ed4 100644 (file)
@@ -710,7 +710,6 @@ static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struc
     return ret;
 }
 
-#define RFORK_EA_ALLOCSIZE (128*1024) /* 128k */
 /*!
  * Open EA with resfork, only for AD_VERSION_EA, a nullop otherwise
  */
@@ -744,7 +743,7 @@ static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struc
     ad->ad_resforkbufsize = roundup;
 
     /* Read the EA into the buffer */
-    if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_resforkbuf, ad->ad_rlen) == -1)
+    if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen) == -1)
         return -1;
 
     if (ret != 0) {
index 17c2361faa99a775fef26c000bfb1ff82a8b2675..911138aa5946934c76e1690e48296a0120309b64 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: ad_read.c,v 1.10 2010-02-10 14:05:37 franklahm Exp $
- *
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
  * All Rights Reserved.
  *
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#include <atalk/adouble.h>
 #include <string.h>
 #include <sys/param.h>
 
+#include <atalk/adouble.h>
+#include <atalk/ea.h>
+
 #ifndef MIN
 #define MIN(a,b)    ((a)<(b)?(a):(b))
 #endif /* ! MIN */
@@ -77,31 +77,36 @@ ssize_t ad_read( struct adouble *ad, const u_int32_t eid, off_t off, char *buf,
             cc = adf_pread(&ad->ad_data_fork, buf, buflen, off);
         }
     } else {
-        off_t r_off;
-
-        if ( ad_reso_fileno( ad ) == -1 ) {
-            /* resource fork is not open ( cf etc/afp/fork.c) */
-            return 0;
-        }
-        r_off = ad_getentryoff(ad, eid) + off;
-
-        if (( cc = adf_pread( &ad->ad_resource_fork, buf, buflen, r_off )) < 0 ) {
-            return( -1 );
-        }
-        /*
-         * We've just read in bytes from the disk that we read earlier
-         * into ad_data. If we're going to write this buffer out later,
-         * we need to update ad_data.
-         * FIXME : always false?
-         */
-        if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
-            if ( ad->ad_resource_fork.adf_flags & O_RDWR ) {
-                memcpy(buf, ad->ad_data + r_off,
-                       MIN(sizeof( ad->ad_data ) - r_off, cc));
-            } else {
-                memcpy(ad->ad_data + r_off, buf,
-                       MIN(sizeof( ad->ad_data ) - r_off, cc));
+        if (ad->ad_flags != AD_VERSION_EA) {
+            off_t r_off;
+            if ( ad_reso_fileno( ad ) == -1 )
+                /* resource fork is not open ( cf etc/afp/fork.c) */
+                return 0;
+            r_off = ad_getentryoff(ad, eid) + off;
+            if (( cc = adf_pread( &ad->ad_resource_fork, buf, buflen, r_off )) < 0 )
+                return( -1 );
+            /*
+             * We've just read in bytes from the disk that we read earlier
+             * into ad_data. If we're going to write this buffer out later,
+             * we need to update ad_data.
+             * FIXME : always false?
+             */
+            if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
+                if ( ad->ad_resource_fork.adf_flags & O_RDWR ) {
+                    memcpy(buf, ad->ad_data + r_off,
+                           MIN(sizeof( ad->ad_data ) - r_off, cc));
+                } else {
+                    memcpy(ad->ad_data + r_off, buf,
+                           MIN(sizeof( ad->ad_data ) - r_off, cc));
+                }
+            }
+        } else { /* AD_VERSION_EA */
+            if ((off + buflen) > ad->ad_rlen) {
+                errno = ERANGE;
+                return -1;
             }
+            memcpy(buf, ad->ad_resforkbuf + off, buflen);
+            cc = buflen;
         }
     }
 
index 3031f6390ffa6dc485ba49dc10c55feebd849516..06a9e3c5c405e2298caaf05887c6577acc4a3edd 100644 (file)
@@ -7,12 +7,15 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#include <atalk/adouble.h>
-
+#include <stdlib.h>
 #include <string.h>
 #include <sys/param.h>
 #include <errno.h>
 
+#include <atalk/adouble.h>
+#include <atalk/ea.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
 
 #ifndef MIN
 #define MIN(a,b)       ((a)<(b)?(a):(b))
@@ -45,7 +48,7 @@ ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t off
 }
 
 /* end is always 0 */
-ssize_t ad_write(struct adouble *ad, const u_int32_t eid, off_t off, const int end, const char *buf, const size_t buflen)
+ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
 {
     struct stat                st;
     ssize_t            cc;
@@ -57,36 +60,49 @@ ssize_t ad_write(struct adouble *ad, const u_int32_t eid, off_t off, const int e
     }
     
     if ( eid == ADEID_DFORK ) {
-       if ( end ) {
-           if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
-               return( -1 );
-           }
-           off = st.st_size - off;
-       }
-       cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
-    } else if ( eid == ADEID_RFORK ) {
-        off_t    r_off;
-
-       if ( end ) {
-           if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
-               return( -1 );
-           }
-           off = st.st_size - off -ad_getentryoff(ad, eid);
-       }
-       r_off = ad_getentryoff(ad, eid) + off;
-       cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
-
-       /* sync up our internal buffer  FIXME always false? */
-       if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
-           memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
+        if ( end ) {
+            if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
+                return( -1 );
+            }
+            off = st.st_size - off;
         }
-        if ( ad->ad_rlen < off + cc ) {
-             ad->ad_rlen = off + cc;
+        cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
+    } else if ( eid == ADEID_RFORK ) {
+        if (ad->ad_flags != AD_VERSION_EA) {
+            off_t    r_off;
+            if ( end ) {
+                if ( fstat( ad_data_fileno(ad), &st ) < 0 )
+                    return( -1 );
+                off = st.st_size - off -ad_getentryoff(ad, eid);
+            }
+            r_off = ad_getentryoff(ad, eid) + off;
+            cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
+
+            /* sync up our internal buffer  FIXME always false? */
+            if (r_off < ad_getentryoff(ad, ADEID_RFORK))
+                memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
+            if ( ad->ad_rlen < off + cc )
+                ad->ad_rlen = off + cc;
+        } else { /* AD_VERSION_EA */
+            if ((off + buflen) > ad->ad_resforkbufsize) {
+                free(ad->ad_resforkbuf);
+                size_t roundup = (((off + buflen) / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
+                if ((ad->ad_resforkbuf = malloc(roundup)) == NULL)
+                    return -1;
+                ad->ad_resforkbufsize = roundup;
+            }
+            memcpy(ad->ad_resforkbuf + off, buf, buflen);
+            if ((off + buflen) > ad->ad_rlen)
+                ad->ad_rlen = off + buflen;
+            
+            if (sys_lsetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
+                return -1;
+            cc = buflen;
         }
-    }
-    else {
+    } else {
         return -1; /* we don't know how to write if it's not a ressource or data fork */
     }
+
     return( cc );
 }
 
@@ -151,10 +167,10 @@ char            c = 0;
 /* ------------------------ */
 int ad_rtruncate( struct adouble *ad, const off_t size)
 {
-    if ( sys_ftruncate( ad_reso_fileno(ad),
-           size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
-       return -1;
-    }
+    if (ad->ad_flags != AD_VERSION_EA)
+        if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
+            return -1;
+
     ad->ad_rlen = size;    
 
     return 0;
@@ -162,8 +178,8 @@ int ad_rtruncate( struct adouble *ad, const off_t size)
 
 int ad_dtruncate(struct adouble *ad, const off_t size)
 {
-    if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
-      return -1;
-    }
+    if (sys_ftruncate(ad_data_fileno(ad), size) < 0)
+        return -1;
+
     return 0;
 }