]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_write.c
First working adouble:ea->osx fallback working
[netatalk.git] / libatalk / adouble / ad_write.c
index b0e69ef2206e80056101dae548893c06b70efedb..f5a5c9355b5d57603351f1e3ff64c074c3a8264a 100644 (file)
  * All Rights Reserved.  See COPYRIGHT.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
 #include <sys/param.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <errno.h>
 
 #include <atalk/adouble.h>
+#include <atalk/ea.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
+#include <atalk/logger.h>
+#include <atalk/util.h>
+#include <atalk/errchk.h>
+
+/* XXX: locking has to be checked before each stream of consecutive
+ *      ad_writes to prevent a lock in the middle from causing problems. 
+ */
+
+ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
+{
+    ssize_t            cc;
 
-#ifndef MIN
-#define MIN(a,b)       ((a)<(b)?(a):(b))
+#ifndef  HAVE_PWRITE
+    if ( ad_fd->adf_off != offset ) {
+       if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
+           return -1;
+       }
+       ad_fd->adf_off = offset;
+    }
+    cc = write( ad_fd->adf_fd, buf, count );
+    if ( cc < 0 ) {
+        return -1;
+    }
+    ad_fd->adf_off += cc;
+#else
+   cc = pwrite(ad_fd->adf_fd, buf, count, offset );
 #endif
+    return cc;
+}
 
-/* XXX: this would benefit from pwrite. 
- *      locking has to be checked before each stream of consecutive
- *      ad_writes to prevent a lock in the middle from causing problems. 
- */
-ssize_t ad_write( ad, eid, off, end, buf, buflen )
-    struct adouble     *ad;
-    const u_int32_t    eid;
-    off_t               off;
-    const int          end;
-    const char         *buf;
-    const size_t       buflen;
+/* end is always 0 */
+ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
 {
+    EC_INIT;
     struct stat                st;
     ssize_t            cc;
+    size_t roundup;
+    off_t    r_off;
 
-    if ( eid == ADEID_DFORK ) {
-       if ( end ) {
-           if ( fstat( ad->ad_df.adf_fd, &st ) < 0 ) {
-               return( -1 );
-           }
-           off = st.st_size - off;
-       }
+    if (ad_data_fileno(ad) == -2) {
+        /* It's a symlink */
+        errno = EACCES;
+        return -1;
+    }
 
-       if ( ad->ad_df.adf_off != off ) {
-           if ( lseek( ad->ad_df.adf_fd, (off_t) off, SEEK_SET ) < 0 ) {
-               return( -1 );
-           }
-           ad->ad_df.adf_off = off;
-       }
-       cc = write( ad->ad_df.adf_fd, buf, buflen );
-       if ( cc < 0 ) {
-           return( -1 );
-       }
-       ad->ad_df.adf_off += cc;
-    } else {
-       if ( end ) {
-           off = ad->ad_eid[ eid ].ade_len - off;
-       }
-       cc = ad->ad_eid[eid].ade_off + off;
-
-#ifdef USE_MMAPPED_HEADERS
-       if (eid != ADEID_RFORK) {
-         memcpy(ad->ad_data + cc, buf, buflen);
-         cc = buflen;
-         goto ad_write_done;
-       }         
+    LOG(log_debug, logtype_default, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
+        (uintmax_t)off, buflen, ad->ad_rlen);
+    
+    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 ) {
+        if (end) {
+            if (fstat( ad_reso_fileno(ad), &st ) < 0)
+                return(-1);
+            off = st.st_size - off - ad_getentryoff(ad, eid);
+        }
+#ifdef HAVE_EAFD
+        if (ad->ad_vers == AD_VERSION_EA) {
+            r_off = 0;
+        } else
 #endif
+            r_off = ad_getentryoff(ad, eid) + off;
 
-       if ( ad->ad_hf.adf_off != cc ) {
-         if ( lseek( ad->ad_hf.adf_fd, (off_t) cc, SEEK_SET ) < 0 ) {
-             return( -1 );
-         }
-         ad->ad_hf.adf_off = cc;
-       }
-         
-       if ((cc = write( ad->ad_hf.adf_fd, buf, buflen )) < 0)
-         return( -1 );
-       ad->ad_hf.adf_off += cc;
-       
-#ifndef USE_MMAPPED_HEADERS
-       /* sync up our internal buffer */
-       if (ad->ad_hf.adf_off < ad_getentryoff(ad, ADEID_RFORK))
-         memcpy(ad->ad_data + ad->ad_hf.adf_off, buf,
-                MIN(sizeof(ad->ad_data) - ad->ad_hf.adf_off, cc));
-#else    
-ad_write_done:
-#endif
-         if ( ad->ad_eid[ eid ].ade_len < off + cc ) {
-           ad->ad_eid[ eid ].ade_len = off + cc;
-         }
+        cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
+
+        if ( ad->ad_rlen < off + cc )
+            ad->ad_rlen = off + cc;
+    } else {
+        return -1; /* we don't know how to write if it's not a ressource or data fork */
     }
 
+EC_CLEANUP:
+    if (ret != 0)
+        return ret;
     return( cc );
 }
 
-/* set locks here */
-int ad_rtruncate( ad, size )
-    struct adouble     *ad;
-    const size_t       size;
+/* 
+ * the caller set the locks
+ * ftruncate is undefined when the file length is smaller than 'size'
+ */
+int sys_ftruncate(int fd, off_t length)
 {
-    int err;
 
-    if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0) < 0)
-      return -2;
+#ifndef  HAVE_PWRITE
+off_t           curpos;
+#endif
+int             err;
+struct stat    st;
+char            c = 0;
+
+    if (!ftruncate(fd, length)) {
+        return 0;
+    }
+    /* maybe ftruncate doesn't work if we try to extend the size */
+    err = errno;
+
+#ifndef  HAVE_PWRITE
+    /* we only care about file pointer if we don't use pwrite */
+    if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
+        errno = err;
+        return -1;
+    }
+#endif
+
+    if ( fstat( fd, &st ) < 0 ) {
+        errno = err;
+        return -1;
+    }
+    
+    if (st.st_size > length) {
+        errno = err;
+        return -1;
+    }
+
+    if (lseek(fd, length -1, SEEK_SET) != length -1) {
+        errno = err;
+        return -1;
+    }
 
-    if ( ftruncate( ad->ad_hf.adf_fd,
-           size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
-        err = errno;
-        ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
-       errno = err;
-       return( -1 );
+    if (1 != write( fd, &c, 1 )) {
+        /* return the write errno */
+        return -1;
     }
 
-    ad->ad_eid[ ADEID_RFORK ].ade_len = size;
-    if ( lseek( ad->ad_hf.adf_fd, ad->ad_eid[ADEID_RFORK].ade_off, 
-               SEEK_SET ) < 0 ) {
-        err = errno;
-        ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
-       errno = err;
-       return( -1 );
+#ifndef  HAVE_PWRITE
+    if (curpos != lseek(fd, curpos,  SEEK_SET)) {
+        errno = err;
+        return -1;
     }
+#endif
 
-    ad->ad_hf.adf_off = ad->ad_eid[ADEID_RFORK].ade_off;
-    ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
-    return( 0 );
+    return 0;    
 }
 
-int ad_dtruncate(ad, size)
-    struct adouble     *ad;
-    const size_t       size;
+/* ------------------------ */
+int ad_rtruncate( struct adouble *ad, const off_t size)
 {
-    int err;
+    if (ad->ad_vers != AD_VERSION_EA)
+        if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
+            return -1;
 
-    if (ad_tmplock(ad, ADEID_DFORK, ADLOCK_WR, 0, 0) < 0)
-      return -2;
+    ad->ad_rlen = size;    
 
-    if (ftruncate(ad->ad_df.adf_fd, size) < 0) {
-      err = errno;
-      ad_tmplock(ad, ADEID_DFORK, ADLOCK_CLR, 0, 0);
-      errno = err;
-    } else 
-      ad_tmplock(ad, ADEID_DFORK, ADLOCK_CLR, 0, 0);
+    return 0;
+}
+
+int ad_dtruncate(struct adouble *ad, const off_t size)
+{
+    if (sys_ftruncate(ad_data_fileno(ad), size) < 0)
+        return -1;
 
     return 0;
 }