]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
Merge master
[netatalk.git] / libatalk / adouble / ad_flush.c
index e234ed7b5ab55d15e80523e1625ce76ee00565eb..9c9c3891d522aaeca8259902f24a440c305d8e7b 100644 (file)
 #include <atalk/adouble.h>
 #include <atalk/ea.h>
 #include <atalk/logger.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
 
-#include "ad_private.h"
+#include "ad_lock.h"
+
+static const u_int32_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
+};
+
+#define EID_DISK(a) (set_eid[a])
 
 /*
  * Rebuild any header information that might have changed.
@@ -66,7 +76,7 @@ int  ad_rebuild_adouble_header(struct adouble *ad)
         if ( ad->ad_eid[ eid ].ade_off == 0 ) {
             continue;
         }
-        temp = htonl( DISK_EID(eid) );
+        temp = htonl( EID_DISK(eid) );
         memcpy(buf, &temp, sizeof( temp ));
         buf += sizeof( temp );
 
@@ -155,13 +165,14 @@ int ad_flush(struct adouble *ad)
             }
             break;
         case AD_VERSION_EA:
-            if (sys_fsetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA, 0) != 0) {
-                LOG(log_error, logtype_afpd, "Unexpected adouble version");
+            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;
             }
             break;
         default:
-            LOG(log_error, logtype_afpd, "Unexpected adouble version");
+            LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
             return -1;
         }
     }
@@ -169,38 +180,63 @@ int ad_flush(struct adouble *ad)
     return( 0 );
 }
 
-/* use refcounts so that we don't have to re-establish fcntl locks. */
+/*!
+ * Close a struct adouble freeing all resources
+ *
+ * This close the whole thing, regardless of what you pass in adflags!
+ */
 int ad_close( struct adouble *ad, int adflags)
 {
-    int         err = 0;
+    int err = 0;
+
+    if (ad->ad_inited == AD_CLOSED) {
+        LOG(log_warning, logtype_default, "ad_close: double close");
+        return 0;
+    }
+
+    LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
+        cfrombstr(ad->ad_fullpath),
+        adflags2logstr(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 ((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->ad_data_fork.adf_syml != NULL) {
+    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 = 0;
+            ad->ad_data_fork.adf_syml = NULL;
         } else {
             if ( close( ad_data_fileno(ad) ) < 0 )
                 err = -1;
         }
         ad_data_fileno(ad) = -1;
         adf_lock_free(&ad->ad_data_fork);
+        ad->ad_adflags &= ~ADFLAGS_DF;
     }
 
-    if (!( adflags & ADFLAGS_HF )) {
-        return err;
-    }
-
-    /* meta/resource fork */
-
-    if ( ad_meta_fileno(ad) != -1 && !(--ad->ad_md->adf_refcount)) {
-        if ( close( ad_meta_fileno(ad) ) < 0 ) {
+    if (ad_meta_fileno(ad) != -1) {
+        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_resforkbuf) {
+        free(ad->ad_resforkbuf);
+        ad->ad_resforkbuf = NULL;
+        ad->ad_adflags &= ~ADFLAGS_RF;
+    }
+
+    if (ad->ad_fullpath) {
+        bdestroy(ad->ad_fullpath);
+        ad->ad_fullpath = NULL;
+    }
+
+    ad->ad_inited = AD_CLOSED;
+
     return err;
 }