]> arthur.barton.de Git - netatalk.git/blobdiff - bin/megatron/nad.c
Merge remote branch 'sf/branch-allea' into branch-allea
[netatalk.git] / bin / megatron / nad.c
index cb7ab5a2646a05216a29c6c11c191bbb0e0fda56..9d0b64bf5627612a6bfeb1502d0512b6b8d27f1a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: nad.c,v 1.11 2002-04-29 01:52:50 morgana Exp $
+ * $Id: nad.c,v 1.18 2010-01-27 21:27:53 didg Exp $
  */
 
 #ifdef HAVE_CONFIG_H
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
-#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
 
 #include <atalk/adouble.h>
+#include <atalk/util.h>
+#include <atalk/volinfo.h>
 #include <netatalk/endian.h>
 #include "megatron.h"
 #include "nad.h"
 
+static struct volinfo  vol;
 static char            hexdig[] = "0123456789abcdef";
 
 static char mtou_buf[MAXPATHLEN + 1], utom_buf[MAXPATHLEN + 1];
-static char *mtoupathcap( mpath )
-    char       *mpath;
+static char *mtoupathcap(char *mpath)
 {
     char       *m, *u, *umax;
     int                i = 0;
@@ -64,8 +64,7 @@ static char *mtoupathcap( mpath )
 #define hextoint( c )  ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
 #define islxdigit(x)   (!isupper(x)&&isxdigit(x))
 
-static char *utompathcap( upath )
-    char       *upath;
+static char *utompathcap( char *upath)
 {
     char       *m, *u;
     int h;
@@ -308,6 +307,81 @@ static char *utompathsjis( char *from)
 
     return utom_buf;
  }
+
+static char *utompathiconv(char *upath)
+{
+    char        *m, *u;
+    u_int16_t    flags = CONV_IGNORE | CONV_UNESCAPEHEX;
+    size_t       outlen;
+    static char         mpath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
+
+    m = mpath;
+    outlen = strlen(upath);
+
+#if 0
+    if (vol->v_casefold & AFPVOL_UTOMUPPER)
+        flags |= CONV_TOUPPER;
+    else if (vol->v_casefold & AFPVOL_UTOMLOWER)
+        flags |= CONV_TOLOWER;
+#endif
+
+    u = upath;
+
+    /* convert charsets */
+    if ((size_t)-1 == ( outlen = convert_charset ( vol.v_volcharset, vol.v_maccharset, vol.v_maccharset, u, outlen, mpath, MAXPATHLEN, &flags)) ) {
+        fprintf( stderr, "Conversion from %s to %s for %s failed.", vol.v_volcodepage, vol.v_maccodepage, u);
+        goto utompath_error;
+    }
+
+    if (flags & CONV_REQMANGLE) 
+       goto utompath_error;
+
+    return(m);
+
+utompath_error:
+    return(utompathcap( upath ));
+}
+
+static char *mtoupathiconv(char *mpath)
+{
+    char        *m, *u;
+    size_t       inplen;
+    size_t       outlen;
+    u_int16_t    flags = 0;
+    static char         upath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
+
+    if ( *mpath == '\0' ) {
+        return( "." );
+    }
+
+    /* set conversion flags */
+    if (!(vol.v_flags & AFPVOL_NOHEX))
+        flags |= CONV_ESCAPEHEX;
+    if (!(vol.v_flags & AFPVOL_USEDOTS))
+        flags |= CONV_ESCAPEDOTS;
+
+#if 0
+    if ((vol->v_casefold & AFPVOL_MTOUUPPER))
+        flags |= CONV_TOUPPER;
+    else if ((vol->v_casefold & AFPVOL_MTOULOWER))
+        flags |= CONV_TOLOWER;
+#endif
+
+    m = mpath;
+    u = upath;
+
+    inplen = strlen(m);
+    outlen = MAXPATHLEN;
+
+    if ((size_t)-1 == (outlen = convert_charset ( vol.v_maccharset, vol.v_volcharset, vol.v_maccharset, m, inplen, u, outlen, &flags)) ) {
+        fprintf (stderr, "conversion from %s to %s for %s failed.", vol.v_maccodepage, vol.v_volcodepage, mpath);
+        return(mtoupathcap( upath ));
+    }
+
+    return( upath );
+}
+
+
  
 char * (*_mtoupath) ( char *mpath) = mtoupathcap;
 char * (*_utompath) ( char *upath) = utompathcap;
@@ -333,17 +407,27 @@ void select_charset( int options)
     int                        hexfork[ NUMFORKS ];
 #endif /* HEXOUTPUT */
 
-struct nad_file_data {
+static struct nad_file_data {
     char               macname[ MAXPATHLEN + 1 ];
     char               adpath[ 2 ][ MAXPATHLEN + 1];
     int                        offset[ NUMFORKS ];
     struct adouble     ad;
 } nad;
 
-int nad_open( path, openflags, fh, options )
-    char               *path;
-    int                        openflags, options;
-    struct FHeader     *fh;
+static void initvol(char *path)
+{
+    if (!loadvolinfo(path, &vol)) {
+        vol_load_charsets(&vol);
+        ad_init(&nad.ad, vol.v_adouble, 0);
+        _mtoupath = mtoupathiconv;
+        _utompath = utompathiconv;
+    }
+    else
+        ad_init(&nad.ad, 0, 0);
+}
+
+
+int nad_open( char *path, int openflags, struct FHeader *fh, int options)
 {
     struct stat                st;
     int                        fork;
@@ -355,10 +439,12 @@ int nad_open( path, openflags, fh, options )
  */
     select_charset( options);
     memset(&nad.ad, 0, sizeof(nad.ad));
+
     if ( openflags == O_RDONLY ) {
+       initvol(path);
        strcpy( nad.adpath[0], path );
        strcpy( nad.adpath[1], 
-               ad_path( nad.adpath[0], ADFLAGS_DF|ADFLAGS_HF ));
+               nad.ad.ad_ops->ad_path( nad.adpath[0], ADFLAGS_HF ));
        for ( fork = 0 ; fork < NUMFORKS ; fork++ ) {
            if ( stat( nad.adpath[ fork ], &st ) < 0 ) {
                if ( errno == ENOENT ) {
@@ -382,10 +468,11 @@ int nad_open( path, openflags, fh, options )
        return( nad_header_read( fh ));
 
     } else {
+       initvol (".");
        strcpy( nad.macname, fh->name );
        strcpy( nad.adpath[0], mtoupath( nad.macname ));
        strcpy( nad.adpath[1], 
-               ad_path( nad.adpath[0], ADFLAGS_DF|ADFLAGS_HF ));
+               nad.ad.ad_ops->ad_path( nad.adpath[0], ADFLAGS_HF ));
 #if DEBUG
     fprintf(stderr, "%s\n", nad.macname);
     fprintf(stderr, "%s is adpath[0]\n", nad.adpath[0]);
@@ -405,20 +492,29 @@ int nad_open( path, openflags, fh, options )
     }
 }
 
-int nad_header_read( fh )
-    struct FHeader     *fh;
+int nad_header_read(struct FHeader *fh)
 {
     u_int32_t          temptime;
     struct stat                st;
+    char               *p;
 
+#if 0
     memcpy( nad.macname, ad_entry( &nad.ad, ADEID_NAME ), 
            ad_getentrylen( &nad.ad, ADEID_NAME ));
     nad.macname[ ad_getentrylen( &nad.ad, ADEID_NAME ) ] = '\0';
     strcpy( fh->name, nad.macname );
+#endif
 
     /* just in case there's nothing in macname */
-    if (*fh->name == '\0')
+    if (*fh->name == '\0') {
+      if ( NULL == (p = strrchr(nad.adpath[DATA], '/')) )
+        p = nad.adpath[DATA];
+      else p++;
+#if 0      
       strcpy(fh->name, utompath(nad.adpath[DATA]));
+#endif      
+      strcpy(fh->name, utompath(p));
+    }
 
     if ( stat( nad.adpath[ DATA ], &st ) < 0 ) {
        perror( "stat of datafork failed" );
@@ -507,8 +603,7 @@ int nad_header_read( fh )
 
 }
 
-int nad_header_write( fh )
-    struct FHeader     *fh;
+int nad_header_write(struct FHeader *fh)
 {
     u_int32_t          temptime;
 
@@ -587,19 +682,16 @@ int nad_header_write( fh )
 #endif /* HEXOUTPUT */
 
     nad.offset[ DATA ] = nad.offset[ RESOURCE ] = 0;
-    ad_flush( &nad.ad, ADFLAGS_DF|ADFLAGS_HF );
+    ad_flush( &nad.ad );
 
     return( 0 );
 }
 
-int                    forkeid[] = { ADEID_DFORK, ADEID_RFORK };
+static int             forkeid[] = { ADEID_DFORK, ADEID_RFORK };
 
-int nad_read( fork, forkbuf, bufc )
-    int                        fork;
-    char               *forkbuf;
-    int                        bufc;
+ssize_t nad_read(int fork, char *forkbuf, size_t bufc)
 {
-    int                        cc = 0;
+    ssize_t            cc = 0;
 
 #if DEBUG
     fprintf( stderr, "Entering nad_read\n" );
@@ -619,14 +711,11 @@ int nad_read( fork, forkbuf, bufc )
     return( cc );
 }
 
-int nad_write( fork, forkbuf, bufc )
-    int                        fork;
-    char               *forkbuf;
-    int                        bufc;
+ssize_t nad_write(int fork, char *forkbuf, size_t bufc)
 {
     char               *buf_ptr;
-    int                        writelen;
-    int                        cc = 0;
+    size_t             writelen;
+    ssize_t            cc = 0;
 
 #if DEBUG
     fprintf( stderr, "Entering nad_write\n" );
@@ -654,12 +743,11 @@ int nad_write( fork, forkbuf, bufc )
     return( bufc );
 }
 
-int nad_close( status )
-int                    status;
+int nad_close(int status)
 {
     int                        rv;
     if ( status == KEEP ) {
-       if (( rv = ad_flush( &nad.ad, ADFLAGS_DF|ADFLAGS_HF )) < 0 ) {
+       if (( rv = ad_flush( &nad.ad )) < 0 ) {
            fprintf( stderr, "nad_close rv for flush %d\n", rv );
            return( rv );
        }