]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_write.c
Realloc, not malloc
[netatalk.git] / libatalk / adouble / ad_write.c
index db542cb126f8bba07904c415e1f99478c5877ca9..dc6ee839862b7740f046638f32a5d14c831d8fcb 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: ad_write.c,v 1.10 2009-10-13 22:55:37 didg Exp $
- *
  * Copyright (c) 1990,1995 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
@@ -9,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))
@@ -47,42 +48,60 @@ 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;
+
+    if (ad_data_fileno(ad) == -2) {
+        /* It's a symlink */
+        errno = EACCES;
+        return -1;
+    }
     
     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  < r_off + cc ) {
-             ad->ad_rlen = r_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) {
+                size_t roundup = (((off + buflen) / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
+                if ((ad->ad_resforkbuf = realloc(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 );
 }
 
@@ -147,10 +166,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;
@@ -158,8 +177,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;
 }