]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
Fix ad_reso_size once more...
[netatalk.git] / libatalk / adouble / ad_flush.c
index 9720be9e33c170c292b0dfe768ee9f343a6e7190..3331074b994ee619d7271e7724ed965cb2f76da6 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <arpa/inet.h>
 
 #include <atalk/adouble.h>
 #include <atalk/ea.h>
 #include <atalk/logger.h>
 #include <atalk/bstrlib.h>
 #include <atalk/bstradd.h>
+#include <atalk/errchk.h>
+#include <atalk/util.h>
 
 #include "ad_lock.h"
 
-static const u_int32_t set_eid[] = {
+static const uint32_t set_eid[] = {
     0,1,2,3,4,5,6,7,8,
     9,10,11,12,13,14,15,
     AD_DEV, AD_INO, AD_SYN, AD_ID
@@ -48,16 +51,18 @@ static const u_int32_t set_eid[] = {
 #define EID_DISK(a) (set_eid[a])
 
 /*
- * Rebuild any header information that might have changed.
+ * Prepare ad->ad_data buffer from struct adouble for writing on disk
  */
-int  ad_rebuild_adouble_header(struct adouble *ad)
+int ad_rebuild_adouble_header(struct adouble *ad)
 {
-    u_int32_t       eid;
-    u_int32_t       temp;
-    u_int16_t       nent;
+    uint32_t       eid;
+    uint32_t       temp;
+    uint16_t       nent;
     char        *buf, *nentp;
     int             len;
 
+    LOG(log_debug, logtype_default, "ad_rebuild_adouble_header");
+
     buf = ad->ad_data;
 
     temp = htonl( ad->ad_magic );
@@ -92,7 +97,7 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
     nent = htons( nent );
     memcpy(nentp, &nent, sizeof( nent ));
 
-    switch (ad->ad_flags) {
+    switch (ad->ad_vers) {
     case AD_VERSION2:
         len = ad_getentryoff(ad, ADEID_RFORK);
         break;
@@ -108,6 +113,65 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
     return len;
 }
 
+/*!
+ * Prepare adbuf buffer from struct adouble for writing on disk
+ */
+static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
+{
+    uint32_t       temp;
+    uint16_t       nent;
+    char           *buf;
+    int            len;
+
+    LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_osx");
+
+    buf = &adbuf[0];
+
+    temp = htonl( ad->ad_magic );
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    temp = htonl( ad->ad_version );
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    buf += sizeof( ad->ad_filler );
+
+    nent = htons(ADEID_NUM_OSX);
+    memcpy(buf, &nent, sizeof( nent ));
+    buf += sizeof( nent );
+
+    /* FinderInfo */
+    temp = htonl(EID_DISK(ADEID_FINDERI));
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    temp = htonl(ADEDOFF_FINDERI_OSX);
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    temp = htonl(ADEDLEN_FINDERI);
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    memcpy(adbuf + ADEDOFF_FINDERI_OSX, ad_entry(ad, ADEID_FINDERI), ADEDLEN_FINDERI);
+
+    /* rfork */
+    temp = htonl( EID_DISK(ADEID_RFORK) );
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    temp = htonl(ADEDOFF_RFORK_OSX);
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    temp = htonl( ad->ad_rlen);
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    return AD_DATASZ_OSX;
+}
+
 /* -------------------
  * XXX copy only header with same size or comment
  * doesn't work well for adouble with different version.
@@ -115,8 +179,8 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
  */
 int ad_copy_header(struct adouble *add, struct adouble *ads)
 {
-    u_int32_t       eid;
-    u_int32_t       len;
+    uint32_t       eid;
+    uint32_t       len;
 
     for ( eid = 0; eid < ADEID_MAX; eid++ ) {
         if ( ads->ad_eid[ eid ].ade_off == 0 ) {
@@ -143,11 +207,30 @@ int ad_copy_header(struct adouble *add, struct adouble *ads)
     return 0;
 }
 
-int ad_flush(struct adouble *ad)
+static int ad_flush_hf(struct adouble *ad)
 {
+    EC_INIT;
     int len;
+    int cwd = -1;
+
+    LOG(log_debug, logtype_default, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
+
+    struct ad_fd *adf;
+
+    switch (ad->ad_vers) {
+    case AD_VERSION2:
+        adf = ad->ad_mdp;
+        break;
+    case AD_VERSION_EA:
+        adf = &ad->ad_data_fork;
+        break;
+    default:
+        LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
+        return -1;
+    }
 
-    if (( ad->ad_md->adf_flags & O_RDWR )) {
+    if ((adf->adf_flags & O_RDWR)
+        || ((ad->ad_adflags & ADFLAGS_DIR) && (ad->ad_adflags & ADFLAGS_RDWR))) {
         if (ad_getentryoff(ad, ADEID_RFORK)) {
             if (ad->ad_rlen > 0xffffffff)
                 ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
@@ -156,19 +239,26 @@ int ad_flush(struct adouble *ad)
         }
         len = ad->ad_ops->ad_rebuild_header(ad);
 
-        switch (ad->ad_flags) {
+        switch (ad->ad_vers) {
         case AD_VERSION2:
-            if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
+            if (adf_pwrite(ad->ad_mdp, ad->ad_data, len, 0) != len) {
                 if (errno == 0)
                     errno = EIO;
                 return( -1 );
             }
             break;
         case AD_VERSION_EA:
-            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;
+            if (AD_META_OPEN(ad)) {
+                if (ad->ad_adflags & ADFLAGS_DIR) {
+                    EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
+                    EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
+                    EC_ZERO_LOG( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
+                    EC_NEG1_LOG( fchdir(cwd) );
+                    EC_NEG1_LOG( close(cwd) );
+                    cwd = -1;
+                } else {
+                    EC_ZERO_LOG( sys_fsetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) );
+                }
             }
             break;
         default:
@@ -177,64 +267,135 @@ int ad_flush(struct adouble *ad)
         }
     }
 
-    return( 0 );
+EC_CLEANUP:
+    if (cwd != -1) {
+        if (fchdir(cwd) != 0) {
+            AFP_PANIC("ad_flush: cant fchdir");
+        }
+        close(cwd);
+    }
+    EC_EXIT;
 }
 
-/*!
- * Close a struct adouble freeing all resources
- *
- * This close the whole thing, regardless of what you pass in adflags!
- * When open forks are using this struct adouble (ad_refcount>0) the close
- * request is ignored.
- */
-int ad_close( struct adouble *ad, int adflags)
+/* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
+static int ad_flush_rf(struct adouble *ad)
 {
-    int err = 0;
+    ssize_t len;
+    char    adbuf[AD_DATASZ_OSX];
 
-    if (ad == NULL)
+#ifdef HAVE_EAFD
+    return 0;
+#endif
+    if (ad->ad_vers != AD_VERSION_EA)
         return 0;
 
-    LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
-        cfrombstr(ad->ad_fullpath),
-        adflags2logstr(adflags));
+    LOG(log_debug, logtype_default, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
 
-    if (ad->ad_refcount) {
-        LOG(log_debug, logtype_default, "ad_close(\"%s\"): adouble in use by fork, not closing",
-            cfrombstr(ad->ad_fullpath));
-        return 0;
-    }
+    if ((ad->ad_rfp->adf_flags & O_RDWR)) {
+        if (ad_getentryoff(ad, ADEID_RFORK)) {
+            if (ad->ad_rlen > 0xffffffff)
+                ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
+            else
+                ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
+        }
+        len = ad_rebuild_adouble_header_osx(ad, &adbuf[0]);
 
-    if (ad_data_fileno(ad) != -1) {
-        if ((ad_data_fileno(ad) == -2) && (ad->ad_data_fork.adf_syml != NULL)) {
-            free(ad->ad_data_fork.adf_syml);
-            ad->ad_data_fork.adf_syml = NULL;
-        } else {
-            if ( close( ad_data_fileno(ad) ) < 0 )
-                err = -1;
+        if (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
+            if (errno == 0)
+                errno = EIO;
+            return -1;
         }
-        ad_data_fileno(ad) = -1;
+    }
+    return 0;
+}
+
+int ad_flush(struct adouble *ad)
+{
+    EC_INIT;
+
+    LOG(log_debug, logtype_default, "ad_flush(%s)", adflags2logstr(ad->ad_adflags));
+
+    if (AD_META_OPEN(ad)) {
+        EC_ZERO( ad_flush_hf(ad) );
+    }
+
+    if (AD_RSRC_OPEN(ad)) {
+        EC_ZERO( ad_flush_rf(ad) );
+    }
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
+static int ad_data_closefd(struct adouble *ad)
+{
+    int ret = 0;
+
+    if (ad_data_fileno(ad) == -2) {
+        free(ad->ad_data_fork.adf_syml);
+        ad->ad_data_fork.adf_syml = NULL;
+    } else {
+        if (close(ad_data_fileno(ad)) < 0)
+            ret = -1;
+    }
+    ad_data_fileno(ad) = -1;
+    return ret;
+}
+
+/*!
+ * Close a struct adouble freeing all resources
+ */
+int ad_close(struct adouble *ad, int adflags)
+{
+    int err = 0;
+
+    if (ad == NULL)
+        return err;
+
+    LOG(log_debug, logtype_default,
+        "ad_close(%s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
+        adflags2logstr(adflags),
+        ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
+        ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
+        ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
+
+    if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF))
+        adflags |= ADFLAGS_HF;
+
+    if ((adflags & ADFLAGS_DF)
+        && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */
+        && --ad->ad_data_fork.adf_refcount == 0) {
+        if (ad_data_closefd(ad) < 0)
+            err = -1;
         adf_lock_free(&ad->ad_data_fork);
-        ad->ad_adflags &= ~ADFLAGS_DF;
     }
 
-    if (ad_meta_fileno(ad) != -1) {
-        if ( close( ad_meta_fileno(ad) ) < 0 )
+    if ((adflags & ADFLAGS_HF)
+        && (ad_meta_fileno(ad) != -1) && !(--ad->ad_mdp->adf_refcount)) {
+        if (close( ad_meta_fileno(ad)) < 0)
             err = -1;
         ad_meta_fileno(ad) = -1;
-        adf_lock_free(ad->ad_md);
-        ad->ad_adflags &= ~ADFLAGS_HF;
+        if (ad->ad_vers == AD_VERSION2)
+            adf_lock_free(ad->ad_mdp);
     }
 
-    if (ad->ad_resforkbuf) {
-        free(ad->ad_resforkbuf);
-        ad->ad_resforkbuf = NULL;
-        ad->ad_adflags &= ~ADFLAGS_RF;
+    if ((adflags & ADFLAGS_RF) && (ad->ad_vers == AD_VERSION_EA)) {
+        if ((ad_reso_fileno(ad) != -1)
+            && !(--ad->ad_rfp->adf_refcount)) {
+            if (close(ad->ad_rfp->adf_fd) < 0)
+                err = -1;
+            ad->ad_rlen = 0;
+            ad_reso_fileno(ad) = -1;
+            adf_lock_free(ad->ad_rfp);
+        }
     }
 
-    if (ad->ad_fullpath) {
-        bdestroy(ad->ad_fullpath);
-        ad->ad_fullpath = NULL;
-    }
+    LOG(log_debug, logtype_default,
+        "ad_close(%s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
+        adflags2logstr(adflags), err,
+        ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
+        ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
+        ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
 
     return err;
 }