]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_flush.c
add a SFM compatible format for adouble
[netatalk.git] / libatalk / adouble / ad_flush.c
index c5d1beb18efae47899db84131678368332d89840..7cef3f7e0ddbfdf1606e7c27870f179d9b16f747 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: ad_flush.c,v 1.4 2002-09-29 17:39:59 didg Exp $
+ * $Id: ad_flush.c,v 1.8 2006-09-29 09:39:16 didg Exp $
  *
  * Copyright (c) 1990,1991 Regents of The University of Michigan.
  * All Rights Reserved.
  */
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif /* HAVE_CONFIG_H */
 
+#include <string.h>
+#include <atalk/adouble.h>
+
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif /* HAVE_UNISTD_H */
-#include <sys/types.h>
-#include <sys/mman.h>
 #include <errno.h>
 
-#include <netatalk/endian.h>
-#include <atalk/adouble.h>
-
 #include "ad_private.h"
+#if AD_VERSION == AD_VERSION1 
+
+#define EID_DISK(a) (a)
+
+#else
+
+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
+};
 
-/* rebuild the header */
-void ad_rebuild_header(struct adouble *ad)
+#define EID_DISK(a) (set_eid[a])
+#endif
+
+/* rebuild the adouble header 
+ * XXX should be in a separate file ?
+*/
+int  ad_rebuild_adouble_header(struct adouble *ad)
 {
     u_int32_t          eid;
     u_int32_t          temp;
@@ -76,7 +83,7 @@ void ad_rebuild_header(struct adouble *ad)
       if ( ad->ad_eid[ eid ].ade_off == 0 ) {
        continue;
       }
-      temp = htonl( eid );
+      temp = htonl( EID_DISK(eid) );
       memcpy(buf, &temp, sizeof( temp ));
       buf += sizeof( temp );
 
@@ -91,48 +98,98 @@ void ad_rebuild_header(struct adouble *ad)
     }
     nent = htons( nent );
     memcpy(nentp, &nent, sizeof( nent ));
+    return ad_getentryoff(ad, ADEID_RFORK);
 }
 
+/* ------------------- 
+ * 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;
+    int                        len;
 
-int ad_flush( ad, adflags )
-    struct adouble     *ad;
-    int                        adflags;
+    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;
+}
+
+/* ------------------- */
+int  ad_rebuild_sfm_header(struct adouble *ad)
 {
-#ifndef USE_MMAPPED_HEADERS
-    int len;
-#endif /* ! USE_MMAPPED_HEADERS */
+    u_int32_t          eid;
+    u_int32_t          temp;
+    
+    u_int16_t          attr;
+    char               *buf, *nentp;
 
-    if (( adflags & ADFLAGS_HF ) && ( ad->ad_hf.adf_flags & O_RDWR )) {
-       /* sync our header */
-        ad_rebuild_header(ad);
+    /*
+     * 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 ));
 
-#ifdef USE_MMAPPED_HEADERS
-       /* now sync it */
-#ifdef MS_SYNC
-       msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK),
-             MS_SYNC | MS_INVALIDATE);
-#else
-       msync(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
-#endif /* MS_SYNC */
+    /* need to save attrib */
+    if (!ad_getattr(ad, &attr)) {
+        attr &= ~htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN);
 
-#else /* USE_MMAPPED_HEADERS */
-       if ( ad->ad_hf.adf_off != 0 ) {
-           if ( lseek( ad->ad_hf.adf_fd, 0L, SEEK_SET ) < 0L ) {
-               return( -1 );
-           }
-           ad->ad_hf.adf_off = 0;
-       }
+        memcpy(buf +48 +4, &attr, sizeof(attr));
+        
+    }
+    return AD_SFM_LEN;
+}
+
+
+int ad_flush( ad )
+    struct adouble     *ad;
+{
+    int len;
 
-       /* now flush it out */
-       len = ad_getentryoff(ad, ADEID_RFORK);
-       if (write( ad->ad_hf.adf_fd, ad->ad_data, len) != 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);
+        }
+        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 );
        }
-       ad->ad_hf.adf_off = len;
-#endif /* USE_MMAPPED_HEADERS */
     }
 
     return( 0 );
@@ -145,27 +202,44 @@ int ad_close( ad, adflags )
 {
     int                        err = 0;
 
-    if (( adflags & ADFLAGS_DF ) && ad->ad_df.adf_fd != -1 &&
-       !(--ad->ad_df.adf_refcount)) {
-       if ( close( ad->ad_df.adf_fd ) < 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->ad_df.adf_fd = -1;
-       adf_lock_free(&ad->ad_df);
+       ad_data_fileno(ad) = -1;
+       adf_lock_free(&ad->ad_data_fork);
     }
 
-    if (( adflags & ADFLAGS_HF ) && ad->ad_hf.adf_fd != -1 &&
-       !(--ad->ad_hf.adf_refcount)) {
-#ifdef USE_MMAPPED_HEADERS
-        if (ad->ad_data != MAP_FAILED)
-         munmap(ad->ad_data, ad_getentryoff(ad, ADEID_RFORK));
-#endif /* USE_MMAPPED_HEADERS */
-       if ( close( ad->ad_hf.adf_fd ) < 0 ) {
+    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 ) {
            err = -1;
        }
-       ad->ad_hf.adf_fd = -1;
-       adf_lock_free(&ad->ad_hf);
+       ad_meta_fileno(ad) = -1;
+       adf_lock_free(ad->ad_md);
     }
 
-    return( err );
+    if (ad->ad_flags != AD_VERSION1_SFM) {
+       return err;
+    }
+
+    if ((adflags & ADFLAGS_DIR)) {
+       return err;
+    }
+
+    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);
+    }
+    
+    return err;
 }