]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
Fixes
[netatalk.git] / libatalk / adouble / ad_flush.c
index 6dcaf07266e5d809dcaf4e4da442a86eb77abecb..3af59d909c81448bc664f5a777162965eabd0507 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,
@@ -163,7 +165,7 @@ 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) {
+            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;
@@ -178,17 +180,31 @@ 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!
+ * When open forks are using this struct adouble (ad_refcount>0) the close
+ * request is ignored.
+ */
 int ad_close( struct adouble *ad, int adflags)
 {
-    int         err = 0;
+    int err = 0;
+
+    LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
+        cfrombstr(ad->ad_fullpath),
+        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->ad_data_fork.adf_syml != NULL) {
+    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_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;
@@ -198,9 +214,7 @@ int ad_close( struct adouble *ad, int adflags)
         ad->ad_adflags &= ~ADFLAGS_DF;
     }
 
-    if ((adflags & ADFLAGS_HF)
-        && (ad_meta_fileno(ad) != -1)
-        && (--ad->ad_md->adf_refcount == 0)) {
+    if (ad_meta_fileno(ad) != -1) {
         if ( close( ad_meta_fileno(ad) ) < 0 )
             err = -1;
         ad_meta_fileno(ad) = -1;
@@ -208,20 +222,15 @@ int ad_close( struct adouble *ad, int adflags)
         ad->ad_adflags &= ~ADFLAGS_HF;
     }
 
-    if ((adflags & ADFLAGS_RF)
-        && (--ad->ad_resource_fork.adf_refcount == 0)
-        && (ad->ad_resforkbuf)) {
+    if (ad->ad_resforkbuf) {
         free(ad->ad_resforkbuf);
         ad->ad_resforkbuf = NULL;
         ad->ad_adflags &= ~ADFLAGS_RF;
     }
 
-    /* If both header and ressource are not open anymore, deallocate fullpath */
-    if (!(adflags & (ADFLAGS_HF | ADFLAGS_RF))) {
-        if (ad->ad_fullpath) {
-            bdestroy(ad->ad_fullpath);
-            ad->ad_fullpath = NULL;
-        }
+    if (ad->ad_fullpath) {
+        bdestroy(ad->ad_fullpath);
+        ad->ad_fullpath = NULL;
     }
 
     return err;