]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
Fixes
[netatalk.git] / libatalk / adouble / ad_flush.c
index 341df2e617837f0d8ff34e618ad2f1b745b49532..3af59d909c81448bc664f5a777162965eabd0507 100644 (file)
@@ -1,7 +1,6 @@
 /*
- * $Id: ad_flush.c,v 1.10 2009-07-21 13:41:16 didg Exp $
- *
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
+ * Copyright (c) 2010      Frank Lahm
  * All Rights Reserved.
  *
  * Permission to use, copy, modify, and distribute this software and
  * permission. This software is supplied as is without expressed or
  * implied warranties of any kind.
  *
- *     Research Systems Unix Group
- *     The University of Michigan
- *     c/o Mike Clark
- *     535 W. William Street
- *     Ann Arbor, Michigan
- *     +1-313-763-0525
- *     netatalk@itd.umich.edu
+ *  Research Systems Unix Group
+ *  The University of Michigan
+ *  c/o Mike Clark
+ *  535 W. William Street
+ *  Ann Arbor, Michigan
+ *  +1-313-763-0525
+ *  netatalk@itd.umich.edu
  */
 
 #ifdef HAVE_CONFIG_H
 #endif /* HAVE_CONFIG_H */
 
 #include <string.h>
-#include <atalk/adouble.h>
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 
-#include "ad_private.h"
-#if AD_VERSION == AD_VERSION1 
-
-#define EID_DISK(a) (a)
+#include <atalk/adouble.h>
+#include <atalk/ea.h>
+#include <atalk/logger.h>
+#include <atalk/bstrlib.h>
+#include <atalk/bstradd.h>
 
-#else
+#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
+    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])
-#endif
 
-/* rebuild the adouble header 
- * XXX should be in a separate file ?
-*/
+/*
+ * Rebuild any header information that might have changed.
+ */
 int  ad_rebuild_adouble_header(struct adouble *ad)
 {
-    u_int32_t          eid;
-    u_int32_t          temp;
-    
-    u_int16_t          nent;
-    char               *buf, *nentp;
-
-    /*
-     * Rebuild any header information that might have changed.
-     */
+    u_int32_t       eid;
+    u_int32_t       temp;
+    u_int16_t       nent;
+    char        *buf, *nentp;
+    int             len;
+
     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 );
 
-    memcpy(buf, ad->ad_filler, sizeof( ad->ad_filler ));
     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++;
+        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++;
     }
     nent = htons( nent );
     memcpy(nentp, &nent, sizeof( nent ));
-    return ad_getentryoff(ad, ADEID_RFORK);
+
+    switch (ad->ad_flags) {
+    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 len;
 }
 
-/* ------------------- 
+/* -------------------
  * XXX copy only header with same size or comment
  * doesn't work well for adouble with different version.
- * 
-*/
+ *
+ */
 int ad_copy_header(struct adouble *add, struct adouble *ads)
 {
-    u_int32_t          eid;
-    u_int32_t          len;
+    u_int32_t       eid;
+    u_int32_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 ) {
-       continue;
-      }
-
-      len = ads->ad_eid[ eid ].ade_len;
-      if (!len) {
-       continue;
-      }
-      
-      if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
-       continue;      
-      }
-      
-      ad_setentrylen( add, eid, len );
-      memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
-    }
-    add->ad_rlen = ads->ad_rlen;
-    return 0;
-}
+        if ( ads->ad_eid[ eid ].ade_off == 0 ) {
+            continue;
+        }
 
-/* ------------------- */
-int  ad_rebuild_sfm_header(struct adouble *ad)
-{
-    u_int32_t          temp;
-    
-    u_int16_t          attr;
-    char               *buf;
-
-    /*
-     * Rebuild any header information that might have changed.
-     */
-    buf = ad->ad_data;
-    /* FIXME */
-/*    temp = htonl( ad->ad_magic ); */
-    temp = ad->ad_magic;
-    memcpy(buf, &temp, sizeof( temp ));
-    
-/*    temp = htonl( ad->ad_version ); */
-    temp = ad->ad_version;
-    memcpy(buf +4, &temp, sizeof( temp ));
+        if ( add->ad_eid[ eid ].ade_off == 0 ) {
+            continue;
+        }
 
-    /* need to save attrib */
-    if (!ad_getattr(ad, &attr)) {
-        attr &= ~htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN);
+        len = ads->ad_eid[ eid ].ade_len;
+        if (!len) {
+            continue;
+        }
+
+        if (eid != ADEID_COMMENT && add->ad_eid[ eid ].ade_len != len ) {
+            continue;
+        }
 
-        memcpy(buf +48 +4, &attr, sizeof(attr));
-        
+        ad_setentrylen( add, eid, len );
+        memcpy( ad_entry( add, eid ), ad_entry( ads, eid ), len );
     }
-    return AD_SFM_LEN;
+    add->ad_rlen = ads->ad_rlen;
+    return 0;
 }
 
-
-int ad_flush( ad )
-    struct adouble     *ad;
+int ad_flush(struct adouble *ad)
 {
     int len;
 
     if (( ad->ad_md->adf_flags & O_RDWR )) {
-       /* sync our header */
-        if (ad->ad_rlen > 0xffffffff) {
-            ad_setentrylen(ad, ADEID_RFORK, 0xffffffff);
-        }
-        else {
-            ad_setentrylen(ad, ADEID_RFORK, ad->ad_rlen);
+        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->ad_ops->ad_rebuild_header(ad);
 
-        if (adf_pwrite(ad->ad_md, ad->ad_data, len, 0) != len) {
-           if ( errno == 0 ) {
-               errno = EIO;
-           }
-           return( -1 );
-       }
+        switch (ad->ad_flags) {
+        case AD_VERSION2:
+            if (adf_pwrite(ad->ad_md, 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;
+            }
+            break;
+        default:
+            LOG(log_error, logtype_afpd, "ad_flush: unexpected adouble version");
+            return -1;
+        }
     }
 
     return( 0 );
 }
 
-/* use refcounts so that we don't have to re-establish fcntl locks. */
-int ad_close( ad, adflags )
-    struct adouble     *ad;
-    int                        adflags;
+/*!
+ * 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;
-
-    if (( adflags & ADFLAGS_DF ) && ad_data_fileno(ad) != -1 &&
-           !(--ad->ad_data_fork.adf_refcount)) {
-       if ( close( ad_data_fileno(ad) ) < 0 ) {
-           err = -1;
-       }
-       ad_data_fileno(ad) = -1;
-       adf_lock_free(&ad->ad_data_fork);
-    }
+    int err = 0;
+
+    LOG(log_debug, logtype_default, "ad_close(\"%s\", %s)",
+        cfrombstr(ad->ad_fullpath),
+        adflags2logstr(adflags));
 
-    if (!( adflags & ADFLAGS_HF )) {
-        return err;
+    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;
     }
-    
-    /* meta /resource fork */
-
-    if ( ad_meta_fileno(ad) != -1 && !(--ad->ad_md->adf_refcount)) {
-       if ( close( ad_meta_fileno(ad) ) < 0 ) {
-           err = -1;
-       }
-       ad_meta_fileno(ad) = -1;
-       adf_lock_free(ad->ad_md);
+
+    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;
+        }
+        ad_data_fileno(ad) = -1;
+        adf_lock_free(&ad->ad_data_fork);
+        ad->ad_adflags &= ~ADFLAGS_DF;
     }
 
-    if (ad->ad_flags != AD_VERSION1_SFM) {
-       return err;
+    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 ((adflags & ADFLAGS_DIR)) {
-       return err;
+    if (ad->ad_resforkbuf) {
+        free(ad->ad_resforkbuf);
+        ad->ad_resforkbuf = NULL;
+        ad->ad_adflags &= ~ADFLAGS_RF;
     }
 
-    if ( ad_reso_fileno(ad) != -1 && !(--ad->ad_resource_fork.adf_refcount)) {
-       if ( close( ad_reso_fileno(ad) ) < 0 ) {
-           err = -1;
-       }
-       ad_reso_fileno(ad) = -1;
-       adf_lock_free(&ad->ad_resource_fork);
+    if (ad->ad_fullpath) {
+        bdestroy(ad->ad_fullpath);
+        ad->ad_fullpath = NULL;
     }
-    
+
     return err;
 }