]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
Fix a bug where Mac OS X ZIP archives can't be extracted on Netatalk shares
[netatalk.git] / libatalk / adouble / ad_flush.c
index ad15f61bf362a12b713035e10c085af114aa3ea4..91f8058b6f0b2620c790e1e5347036686702e263 100644 (file)
@@ -53,7 +53,7 @@ static const uint32_t set_eid[] = {
 /*
  * 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_v2(struct adouble *ad)
 {
     uint32_t       eid;
     uint32_t       temp;
@@ -61,7 +61,7 @@ int ad_rebuild_adouble_header(struct adouble *ad)
     char        *buf, *nentp;
     int             len;
 
-    LOG(log_debug, logtype_default, "ad_rebuild_adouble_header");
+    LOG(log_debug, logtype_default, "ad_rebuild_adouble_header_v2");
 
     buf = ad->ad_data;
 
@@ -78,9 +78,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)
             continue;
-        }
         temp = htonl( EID_DISK(eid) );
         memcpy(buf, &temp, sizeof( temp ));
         buf += sizeof( temp );
@@ -97,20 +96,53 @@ 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_getentryoff(ad, ADEID_RFORK);
+}
+
+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_default, "ad_rebuild_adouble_header_ea");
+
+    buf = ad->ad_data;
+
+    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 );
+
+    nentp = buf;
+    buf += sizeof( nent );
+    for ( eid = 0, nent = 0; eid < ADEID_MAX; eid++ ) {
+        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 );
+
+        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++;
     }
+    nent = htons( nent );
+    memcpy(nentp, &nent, sizeof( nent ));
 
-    return len;
+    return AD_DATASZ_EA;
 }
 
 /*!
@@ -135,6 +167,7 @@ static int ad_rebuild_adouble_header_osx(struct adouble *ad, char *adbuf)
     memcpy(buf, &temp, sizeof( temp ));
     buf += sizeof( temp );
 
+    memcpy(buf, "Netatalk        ", 16);
     buf += sizeof( ad->ad_filler );
 
     nent = htons(ADEID_NUM_OSX);
@@ -183,27 +216,34 @@ 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 ) {
+        if ( ads->ad_eid[ eid ].ade_off == 0 || add->ad_eid[ eid ].ade_off == 0 )
             continue;
-        }
-
-        if ( 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;
 }
 
@@ -331,7 +371,7 @@ 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 {
@@ -352,46 +392,66 @@ int ad_close(struct adouble *ad, int adflags)
     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)]",
+        "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_SETSHRMD | ADFLAGS_CHECK_OF)) {
+        /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling */
+        adflags |= ADFLAGS_DF;
+    }
+
     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);
+    if ((adflags & ADFLAGS_DF) && (ad_data_fileno(ad) >= 0 || ad_data_fileno(ad) == AD_SYMLINK)) {        
+        if (ad->ad_data_refcount)
+            ad->ad_data_refcount--;
+        if (--ad->ad_data_fork.adf_refcount == 0) {
+            if (ad_data_closefd(ad) < 0)
+                err = -1;
+            adf_lock_free(&ad->ad_data_fork);
+        }
     }
 
-    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;
-        if (ad->ad_vers == AD_VERSION2)
-            adf_lock_free(ad->ad_mdp);
+    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;
+            if (ad->ad_vers == AD_VERSION2)
+                adf_lock_free(ad->ad_mdp);
+        }
     }
 
-    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;
+    if (adflags & ADFLAGS_RF) {
+        if (ad->ad_reso_refcount)
+            ad->ad_reso_refcount--;
+        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;
+                ad->ad_rlen = 0;
+                ad_reso_fileno(ad) = -1;
+                adf_lock_free(ad->ad_rfp);
+            }
         }
     }
 
     LOG(log_debug, logtype_default,
-        "ad_close(%s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
+        "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);