]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
libatalk: Fix a couple of issues in ad_openat()
[netatalk.git] / libatalk / adouble / ad_flush.c
index 52f24f17b36dbd84104eddb54f898dda95d8c766..50fd8baef2a2acc7297d0dbb5178490cc456532f 100644 (file)
@@ -37,6 +37,8 @@
 #include <atalk/logger.h>
 #include <atalk/bstrlib.h>
 #include <atalk/bstradd.h>
+#include <atalk/errchk.h>
+#include <atalk/util.h>
 
 #include "ad_lock.h"
 
@@ -48,56 +50,62 @@ static const uint32_t set_eid[] = {
 
 #define EID_DISK(a) (set_eid[a])
 
-int fsetrsrcea(struct adouble *ad, int fd, const char *eaname, const void *value, size_t size, int flags)
+/*
+ * Prepare ad->ad_data buffer from struct adouble for writing on disk
+ */
+int ad_rebuild_adouble_header_v2(struct adouble *ad)
 {
-    if ((ad->ad_maxeafssize == 0) || (ad->ad_maxeafssize >= size)) {
-        LOG(log_debug, logtype_default, "fsetrsrcea(\"%s\"): size: %zu", eaname, size);
-        if (sys_fsetxattr(fd, eaname, value, size, 0) == -1)
-            return -1;
-        return 0;
-    }
+    uint32_t       eid;
+    uint32_t       temp;
+    uint16_t       nent;
+    char        *buf, *nentp;
 
-    /* rsrcfork is larger then maximum EA support by fs so we have to split it */
-    int i;
-    int eas = (size / ad->ad_maxeafssize);
-    size_t remain = size - (eas * ad->ad_maxeafssize);
-    bstring eachunk;
+    LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_v2");
 
-    LOG(log_debug, logtype_default, "fsetrsrcea(\"%s\"): size: %zu, maxea: %zu, eas: %d, remain: %zu",
-        eaname, size, ad->ad_maxeafssize, eas, remain);
+    buf = ad->ad_data;
 
-    for (i = 0; i < eas; i++) {
-        if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
-            return -1;
-        if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), ad->ad_maxeafssize, 0) == -1) {
-            LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
-            bdestroy(eachunk);
-            return -1;
-        }
-        bdestroy(eachunk);
-    }
+    temp = htonl( ad->ad_magic );
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
 
-    if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
-        return -1;
-    if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), remain, 0) == -1) {
-        LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
-        return -1;
+    temp = htonl( ad->ad_version );
+    memcpy(buf, &temp, sizeof( temp ));
+    buf += sizeof( temp );
+
+    buf += sizeof( ad->ad_filler );
+
+    nentp = buf;
+    buf += sizeof( nent );
+    for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
+        if (ad->ad_eid[ eid ].ade_off == 0)
+            continue;
+        temp = htonl( EID_DISK(eid) );
+        memcpy(buf, &temp, sizeof( temp ));
+        buf += sizeof( temp );
+
+        temp = htonl( ad->ad_eid[ eid ].ade_off );
+        memcpy(buf, &temp, sizeof( temp ));
+        buf += sizeof( temp );
+
+        temp = htonl( ad->ad_eid[ eid ].ade_len );
+        memcpy(buf, &temp, sizeof( temp ));
+        buf += sizeof( temp );
+        nent++;
     }
-    bdestroy(eachunk);
+    nent = htons( nent );
+    memcpy(nentp, &nent, sizeof( nent ));
 
-    return 0;
+    return ad_getentryoff(ad, ADEID_RFORK);
 }
 
-/*
- * Rebuild any header information that might have changed.
- */
-int  ad_rebuild_adouble_header(struct adouble *ad)
+int ad_rebuild_adouble_header_ea(struct adouble *ad)
 {
     uint32_t       eid;
     uint32_t       temp;
     uint16_t       nent;
     char        *buf, *nentp;
-    int             len;
+
+    LOG(log_debug, logtype_ad, "ad_rebuild_adouble_header_ea");
 
     buf = ad->ad_data;
 
@@ -114,9 +122,8 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
     nentp = buf;
     buf += sizeof( nent );
     for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
-        if ( ad->ad_eid[ eid ].ade_off == 0 ) {
+        if ((ad->ad_eid[ eid ].ade_off == 0) || (eid == ADEID_RFORK))
             continue;
-        }
         temp = htonl( EID_DISK(eid) );
         memcpy(buf, &temp, sizeof( temp ));
         buf += sizeof( temp );
@@ -133,20 +140,66 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
     nent = htons( nent );
     memcpy(nentp, &nent, sizeof( nent ));
 
-    switch (ad->ad_vers) {
-    case AD_VERSION2:
-        len = ad_getentryoff(ad, ADEID_RFORK);
-        break;
-    case AD_VERSION_EA:
-        len = AD_DATASZ_EA;
-        break;
-    default:
-        LOG(log_error, logtype_afpd, "Unexpected adouble version");
-        len = 0;
-        break;
-    }
+    return AD_DATASZ_EA;
+}
+
+/*!
+ * 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;
+
+    LOG(log_debug, logtype_ad, "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 );
+
+    memcpy(buf, "Netatalk        ", 16);
+    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 );
 
-    return len;
+    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;
 }
 
 /* -------------------
@@ -160,35 +213,44 @@ int ad_copy_header(struct adouble *add, struct adouble *ads)
     uint32_t       len;
 
     for ( eid = 0; eid < ADEID_MAX; eid++ ) {
-        if ( ads->ad_eid[ eid ].ade_off == 0 ) {
-            continue;
-        }
-
-        if ( add->ad_eid[ eid ].ade_off == 0 ) {
+        if ( ads->ad_eid[ eid ].ade_off == 0 || add->ad_eid[ eid ].ade_off == 0 )
             continue;
-        }
 
         len = ads->ad_eid[ eid ].ade_len;
-        if (!len) {
+        if (!len || len != add->ad_eid[ eid ].ade_len)
             continue;
-        }
 
-        if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
+        switch (eid) {
+        case ADEID_COMMENT:
+        case ADEID_PRIVDEV:
+        case ADEID_PRIVINO:
+        case ADEID_PRIVSYN:
+        case ADEID_PRIVID:
             continue;
+        default:
+            ad_setentrylen( add, eid, len );
+            memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
         }
-
-        ad_setentrylen( add, eid, len );
-        memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
     }
     add->ad_rlen = ads->ad_rlen;
+
+    if (((ads->ad_vers == AD_VERSION2) && (add->ad_vers == AD_VERSION_EA))
+        || ((ads->ad_vers == AD_VERSION_EA) && (add->ad_vers == AD_VERSION2))) {
+        cnid_t id;
+        memcpy(&id, ad_entry(add, ADEID_PRIVID), sizeof(cnid_t));
+        id = htonl(id);
+        memcpy(ad_entry(add, ADEID_PRIVID), &id, sizeof(cnid_t));
+    }
     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(%s)", adflags2logstr(ad->ad_adflags));
+    LOG(log_debug, logtype_ad, "ad_flush_hf(%s)", adflags2logstr(ad->ad_adflags));
 
     struct ad_fd *adf;
 
@@ -200,11 +262,12 @@ int ad_flush(struct adouble *ad)
         adf = &ad->ad_data_fork;
         break;
     default:
-        LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
+        LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
         return -1;
     }
 
-    if ((adf->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);
@@ -223,33 +286,90 @@ int ad_flush(struct adouble *ad)
             break;
         case AD_VERSION_EA:
             if (AD_META_OPEN(ad)) {
-                if (sys_fsetxattr(ad_data_fileno(ad), 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->ad_adflags & ADFLAGS_DIR) {
+                    EC_NEG1_LOG( cwd = open(".", O_RDONLY) );
+                    EC_NEG1_LOG( fchdir(ad_data_fileno(ad)) );
+                    EC_ZERO_LOGSTR( sys_lsetxattr(".", AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0),
+                                    "sys_lsetxattr(\"%s\"): %s", fullpathname(".") ,strerror(errno));
+                    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) );
                 }
             }
-#ifndef HAVE_EAFD
-            if (AD_RSRC_OPEN(ad) && (ad->ad_rlen > 0)) {
-                if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
-                    return -1;
-            }
-#endif
             break;
         default:
-            LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
+            LOG(log_error, logtype_ad, "ad_flush: unexpected adouble version");
             return -1;
         }
     }
 
-    return( 0 );
+EC_CLEANUP:
+    if (cwd != -1) {
+        if (fchdir(cwd) != 0) {
+            AFP_PANIC("ad_flush: cant fchdir");
+        }
+        close(cwd);
+    }
+    EC_EXIT;
+}
+
+/* Flush resofork adouble file if any (currently adouble:ea and #ifndef HAVE_EAFD eg Linux) */
+static int ad_flush_rf(struct adouble *ad)
+{
+    ssize_t len;
+    char    adbuf[AD_DATASZ_OSX];
+
+#ifdef HAVE_EAFD
+    return 0;
+#endif
+    if (ad->ad_vers != AD_VERSION_EA)
+        return 0;
+
+    LOG(log_debug, logtype_ad, "ad_flush_rf(%s)", adflags2logstr(ad->ad_adflags));
+
+    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 (adf_pwrite(ad->ad_rfp, adbuf, len, 0) != len) {
+            if (errno == 0)
+                errno = EIO;
+            return -1;
+        }
+    }
+    return 0;
+}
+
+int ad_flush(struct adouble *ad)
+{
+    EC_INIT;
+
+    LOG(log_debug, logtype_ad, "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) {
+    if (ad_data_fileno(ad) == AD_SYMLINK) {
         free(ad->ad_data_fork.adf_syml);
         ad->ad_data_fork.adf_syml = NULL;
     } else {
@@ -270,74 +390,67 @@ int ad_close(struct adouble *ad, int adflags)
     if (ad == NULL)
         return err;
 
-    LOG(log_debug, logtype_default, "ad_close(%s)", adflags2logstr(adflags));
 
-    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);
-    }
+    LOG(log_debug, logtype_ad,
+        "ad_close(%s): BEGIN: {d: %d, m: %d, r: %d} "
+        "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
+        adflags2logstr(adflags),
+        ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
+        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 ((adflags & ADFLAGS_HF)) {
-        switch (ad->ad_vers) {
-        case AD_VERSION2:
-            if ((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_mdp);
-            }
-            break;
+    if (adflags & (ADFLAGS_SETSHRMD | ADFLAGS_CHECK_OF)) {
+        /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling */
+        adflags |= ADFLAGS_DF;
+    }
 
-        case AD_VERSION_EA:
-            if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */ 
-                && !(--ad->ad_data_fork.adf_refcount)) {
-                if (ad_data_closefd(ad) < 0)
-                    err = -1;
-                adf_lock_free(&ad->ad_data_fork);
-            }
-            break;
+    if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF))
+        adflags |= ADFLAGS_HF;
 
-        default:
-            LOG(log_error, logtype_default, "ad_close: unknown AD version");
-            errno = EIO;
-            return -1;
+    if ((adflags & ADFLAGS_DF) && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == AD_SYMLINK)) {        
+        if (ad->ad_data_refcount)
+            if (--ad->ad_data_refcount == 0)
+                adf_lock_free(&ad->ad_data_fork);                
+        if (--ad->ad_data_fork.adf_refcount == 0) {
+            if (ad_data_closefd(ad) < 0)
+                err = -1;
         }
     }
 
-    if ((adflags & ADFLAGS_RF)) {
-        switch (ad->ad_vers) {
-        case AD_VERSION2:
-            /* Do nothing as ADFLAGS_RF == ADFLAGS_HF */
-            break;
+    if ((adflags & ADFLAGS_HF) && (ad_meta_fileno(ad) != -1)) {
+        if (ad->ad_meta_refcount)
+            ad->ad_meta_refcount--;
+        if (!(--ad->ad_mdp->adf_refcount)) {
+            if (close( ad_meta_fileno(ad)) < 0)
+                err = -1;
+            ad_meta_fileno(ad) = -1;
+        }
+    }
 
-        case AD_VERSION_EA:
-#ifndef HAVE_EAFD
-            LOG(log_debug, logtype_default, "ad_close: ad->ad_rlen: %zu", ad->ad_rlen);
-            if (ad->ad_rlen > 0)
-                if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
-                    err = -1;
-#endif
-            if ((ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == -2) /* -2 means symlink */ 
-                && !(--ad->ad_data_fork.adf_refcount)) {
-                if (ad_data_closefd(ad) < 0)
+    if (adflags & ADFLAGS_RF) {
+        if (ad->ad_reso_refcount)
+            if (--ad->ad_reso_refcount == 0)
+                adf_lock_free(ad->ad_rfp);
+        if (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;
-                adf_lock_free(&ad->ad_data_fork);
-                if (ad->ad_resforkbuf)
-                    free(ad->ad_resforkbuf);
-                ad->ad_resforkbuf = NULL;
                 ad->ad_rlen = 0;
+                ad_reso_fileno(ad) = -1;
             }
-            break;
-
-        default:
-            LOG(log_error, logtype_default, "ad_close: unknown AD version");
-            errno = EIO;
-            return -1;
         }
     }
 
+    LOG(log_debug, logtype_ad,
+        "ad_close(%s): END: %d {d: %d, m: %d, r: %d} "
+        "[dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
+        adflags2logstr(adflags), err,
+        ad->ad_data_refcount, ad->ad_meta_refcount, ad->ad_reso_refcount,
+        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;
 }