]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
always test file open modes.
[netatalk.git] / etc / afpd / file.c
index 826577bb5eb0707c33c60be8cc664046eff7bbd6..26015e3d2d44f473c31387e00f2cb3384507c6a3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.36 2002-01-17 16:19:06 jmarcus Exp $
+ * $Id: file.c,v 1.92.2.2.2.15 2004-02-29 22:18:24 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -11,9 +11,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif /* HAVE_UNISTD_H */
 
 /* STDC check */
 #if STDC_HEADERS
@@ -24,6 +21,7 @@
 #define strrchr index
 #endif /* HAVE_STRCHR */
 char *strchr (), *strrchr ();
+
 #ifndef HAVE_MEMCPY
 #define memcpy(d,s,n) bcopy ((s), (d), (n))
 #define memmove(d,s,n) bcopy ((s), (d), (n))
@@ -31,26 +29,17 @@ char *strchr (), *strrchr ();
 #endif /* STDC_HEADERS */
 
 #include <utime.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
 #include <dirent.h>
-#include <sys/mman.h>
 #include <errno.h>
 
 #include <atalk/logger.h>
-#include <sys/types.h>
-#include <sys/time.h>
 #include <sys/param.h>
-#include <sys/stat.h>
 
-#include <netatalk/endian.h>
 #include <atalk/adouble.h>
+
 #include <atalk/afp.h>
 #include <atalk/util.h>
-#ifdef CNID_DB
 #include <atalk/cnid.h>
-#endif /* CNID_DB */
 #include "directory.h"
 #include "desktop.h"
 #include "volume.h"
@@ -58,16 +47,7 @@ char *strchr (), *strrchr ();
 #include "file.h"
 #include "filedir.h"
 #include "globals.h"
-
-/* check for mtab DID code */
-#ifdef DID_MTAB
-#include "parse_mtab.h"
-#endif /* DID_MTAB */
-
-#ifdef FORCE_UIDGID
-#warning UIDGID
-#include "uid.h"
-#endif /* FORCE_UIDGID */
+#include "unix.h"
 
 /* the format for the finderinfo fields (from IM: Toolbox Essentials):
  * field         bytes        subfield    bytes
@@ -89,50 +69,205 @@ char *strchr (), *strrchr ();
  */
 
 const u_char ufinderi[] = {
-    'T', 'E', 'X', 'T', 'U', 'N', 'I', 'X',
-    0, 0, 0, 0, 0, 0, 0, 0,
-    0, 0, 0, 0, 0, 0, 0, 0,
-    0, 0, 0, 0, 0, 0, 0, 0
-};
+                              'T', 'E', 'X', 'T', 'U', 'N', 'I', 'X',
+                              0, 0, 0, 0, 0, 0, 0, 0,
+                              0, 0, 0, 0, 0, 0, 0, 0,
+                              0, 0, 0, 0, 0, 0, 0, 0
+                          };
+
+/* FIXME mpath : unix or mac name ? (for now it's mac name ) */
+void *get_finderinfo(const char *mpath, struct adouble *adp, void *data)
+{
+    struct extmap      *em;
+    void                *ad_finder;
 
-int getfilparams(struct vol *vol,
+    if (adp && (ad_finder = ad_entry(adp, ADEID_FINDERI))) {
+        memcpy(data, ad_finder, 32);
+    }
+    else {
+        memcpy(data, ufinderi, 32);
+    }
+
+    if ((!adp  || !memcmp(ad_entry(adp, ADEID_FINDERI),ufinderi , 8 )) 
+               && (em = getextmap( mpath ))
+    ) {
+        memcpy(data, em->em_type, sizeof( em->em_type ));
+        memcpy((char *)data + 4, em->em_creator, sizeof(em->em_creator));
+    }
+    return data;
+}
+
+/* ---------------------
+*/
+char *set_name(const struct vol *vol, char *data, cnid_t pid, char *name, cnid_t id, u_int32_t utf8) 
+{
+    u_int32_t   aint;
+    char        *tp = NULL;
+    char        *src = name;
+    aint = strlen( name );
+
+    if (!utf8) {
+        /* want mac name */
+        if (utf8_encoding()) {
+            /* but name is an utf8 mac name */
+            char *u, *m;
+           
+            /* global static variable... */
+            tp = strdup(name);
+            if (!(u = mtoupath(vol, name, pid, 1)) || !(m = utompath(vol, u, id, 0))) {
+               aint = 0;
+            }
+            else {
+                aint = strlen(m);
+                src = m;
+            }
+            
+        }
+        if (aint > MACFILELEN)
+            aint = MACFILELEN;
+        *data++ = aint;
+    }
+    else {
+        u_int16_t temp;
+
+        if (aint > 255)  /* FIXME safeguard, anyway if no ascii char it's game over*/
+           aint = 255;
+
+        utf8 = vol->v_mac?htonl(vol->v_mac->kTextEncoding):0;         /* htonl(utf8) */
+        memcpy(data, &utf8, sizeof(utf8));
+        data += sizeof(utf8);
+        
+        temp = htons(aint);
+        memcpy(data, &temp, sizeof(temp));
+        data += sizeof(temp);
+    }
+
+    memcpy( data, src, aint );
+    data += aint;
+    if (tp) {
+        strcpy(name, tp);
+        free(tp);
+    }
+    return data;
+}
+
+/*
+ * FIXME: PDINFO is UTF8 and doesn't need adp
+*/
+#define PARAM_NEED_ADP(b) ((b) & ((1 << FILPBIT_ATTR)  |\
+                                 (1 << FILPBIT_CDATE) |\
+                                 (1 << FILPBIT_MDATE) |\
+                                 (1 << FILPBIT_BDATE) |\
+                                 (1 << FILPBIT_FINFO) |\
+                                 (1 << FILPBIT_RFLEN) |\
+                                 (1 << FILPBIT_EXTRFLEN) |\
+                                 (1 << FILPBIT_PDINFO)))
+
+/* -------------------------- */
+u_int32_t get_id(struct vol *vol, struct adouble *adp,  const struct stat *st,
+             const cnid_t did, const char *upath, const int len) 
+{
+u_int32_t aint = 0;
+
+#if AD_VERSION > AD_VERSION1
+dev_t  dev;
+ino_t  ino;
+cnid_t a_did;
+char   stamp[ADEDLEN_PRIVSYN];
+    /* look in AD v2 header 
+     * note inode and device are opaques and not in network order
+    */
+    if (adp && sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVDEV)) {
+        memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
+        if ( sizeof(ino_t) == ad_getentrylen(adp,ADEID_PRIVINO)) {
+            memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
+            if (sizeof(stamp) == ad_getentrylen(adp,ADEID_PRIVSYN)) {
+                memcpy(stamp, ad_entry(adp, ADEID_PRIVSYN), sizeof(stamp));
+                if (sizeof(cnid_t) == ad_getentrylen(adp, ADEID_DID)) {
+                    memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
+
+                    if (   ((vol->v_flags & AFPVOL_NODEV) || dev == st->st_dev)
+                           && ino == st->st_ino && a_did == did &&
+                           !memcmp(vol->v_stamp, stamp, sizeof(stamp)) &&
+                          (sizeof(cnid_t) == ad_getentrylen(adp, ADEID_PRIVID)) ) 
+                    { 
+                        memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
+                        return aint;
+                    } 
+                }
+            }
+        }
+    }
+#endif
+    if (vol->v_cdb != NULL) {
+           aint = cnid_add(vol->v_cdb, st, did, upath, len, aint);
+           /* Throw errors if cnid_add fails. */
+           if (aint == CNID_INVALID) {
+            switch (errno) {
+            case CNID_ERR_CLOSE: /* the db is closed */
+                break;
+            case CNID_ERR_PARAM:
+                LOG(log_error, logtype_afpd, "get_id: Incorrect parameters passed to cnid_add");
+                afp_errno = AFPERR_PARAM;
+                return CNID_INVALID;
+            case CNID_ERR_PATH:
+                afp_errno = AFPERR_PARAM;
+                return CNID_INVALID;
+            default:
+                afp_errno = AFPERR_MISC;
+                return CNID_INVALID;
+            }
+        }
+#if AD_VERSION > AD_VERSION1
+        else if (adp && sizeof(dev_t) == ADEDLEN_PRIVDEV && sizeof(ino_t) == ADEDLEN_PRIVINO) {
+            /* update the ressource fork
+             * for a folder adp is always null
+             */
+            ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev, st->st_ino, aint, did, vol->v_stamp);
+            ad_flush(adp, ADFLAGS_HF);
+        }
+#endif    
+    }
+    return aint;
+}
+             
+/* -------------------------- */
+int getmetadata(struct vol *vol,
                  u_int16_t bitmap,
-                 char *path, struct dir *dir, struct stat *st,
-                 char *buf, int *buflen )
+                 struct path *path, struct dir *dir, 
+                 char *buf, int *buflen, struct adouble *adp, int attrbits )
 {
-#ifndef USE_LASTDID
-    struct stat                hst, lst, *lstp;
-#else /* USE_LASTDID */
-    struct stat                hst;
-#endif /* USE_LASTDID */
-    struct adouble     ad, *adp;
-    struct ofork        *of;
-    struct extmap      *em;
-    char               *data, *nameoff = NULL, *upath;
-    int                        bit = 0, isad = 1;
+    char               *data, *l_nameoff = NULL, *upath;
+    char                *utf_nameoff = NULL;
+    int                        bit = 0;
     u_int32_t          aint;
+    cnid_t              id = 0;
     u_int16_t          ashort;
     u_char              achar, fdType[4];
+    u_int32_t           utf8 = 0;
+    struct stat         *st;
+    struct maccess     ma;
 
 #ifdef DEBUG
-    LOG(log_info, logtype_default, "begin getfilparams:");
+    LOG(log_info, logtype_afpd, "begin getmetadata:");
 #endif /* DEBUG */
 
-    upath = mtoupath(vol, path);
-    if ((of = of_findname(vol, curdir, path))) {
-        adp = of->of_ad;
-    } else {
-        memset(&ad, 0, sizeof(ad));
-        adp = &ad;
-    }
-
-    if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
-        isad = 0;
-    } else if ( fstat( ad_hfileno( adp ), &hst ) < 0 ) {
-        LOG(log_error, logtype_default, "getfilparams fstat: %s", strerror(errno) );
-    }
+    upath = path->u_name;
+    st = &path->st;
 
     data = buf;
+
+    if ( ((bitmap & ( (1 << FILPBIT_FINFO)|(1 << FILPBIT_LNAME)|(1 <<FILPBIT_PDINFO) ) ) && !path->m_name)
+         || (bitmap & ( (1 << FILPBIT_LNAME) ) && utf8_encoding()) /* FIXME should be m_name utf8 filename */
+         || (bitmap & (1 << FILPBIT_FNUM))) {
+         
+        id = get_id(vol, adp, st, dir->d_did, upath, strlen(upath));
+        if (id == 0)
+            return afp_errno;
+        if (!path->m_name) {
+            path->m_name = utompath(vol, upath, id, utf8_encoding());
+        }
+    }
     while ( bitmap != 0 ) {
         while (( bitmap & 1 ) == 0 ) {
             bitmap = bitmap>>1;
@@ -141,14 +276,28 @@ int getfilparams(struct vol *vol,
 
         switch ( bit ) {
         case FILPBIT_ATTR :
-            if ( isad ) {
+            if ( adp ) {
                 ad_getattr(adp, &ashort);
             } else if (*upath == '.') {
                 ashort = htons(ATTRBIT_INVISIBLE);
             } else
                 ashort = 0;
+#if 0
+            /* FIXME do we want a visual clue if the file is read only
+             */
+            struct maccess     ma;
+            accessmode( ".", &ma, dir , NULL);
+            if ((ma.ma_user & AR_UWRITE)) {
+               accessmode( upath, &ma, dir , st);
+               if (!(ma.ma_user & AR_UWRITE)) {
+                       attrbits |= ATTRBIT_NOWRITE;
+                }
+            }
+#endif
+            if (attrbits)
+                ashort = htons(ntohs(ashort) | attrbits);
             memcpy(data, &ashort, sizeof( ashort ));
-            data += sizeof( u_short );
+            data += sizeof( ashort );
             break;
 
         case FILPBIT_PDID :
@@ -157,17 +306,16 @@ int getfilparams(struct vol *vol,
             break;
 
         case FILPBIT_CDATE :
-            if (!isad || (ad_getdate(adp, AD_DATE_CREATE, &aint) < 0))
+            if (!adp || (ad_getdate(adp, AD_DATE_CREATE, &aint) < 0))
                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
             memcpy(data, &aint, sizeof( aint ));
             data += sizeof( aint );
             break;
 
         case FILPBIT_MDATE :
-            if ( isad && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) {
-                if ((st->st_mtime > AD_DATE_TO_UNIX(aint)) &&
-                        (hst.st_mtime < st->st_mtime)) {
-                    aint = AD_DATE_FROM_UNIX(st->st_mtime);
+            if ( adp && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) {
+                if ((st->st_mtime > AD_DATE_TO_UNIX(aint))) {
+                   aint = AD_DATE_FROM_UNIX(st->st_mtime);
                 }
             } else {
                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
@@ -177,34 +325,25 @@ int getfilparams(struct vol *vol,
             break;
 
         case FILPBIT_BDATE :
-            if (!isad || (ad_getdate(adp, AD_DATE_BACKUP, &aint) < 0))
+            if (!adp || (ad_getdate(adp, AD_DATE_BACKUP, &aint) < 0))
                 aint = AD_DATE_START;
             memcpy(data, &aint, sizeof( int ));
             data += sizeof( int );
             break;
 
         case FILPBIT_FINFO :
-            if (isad)
-                memcpy(data, ad_entry(adp, ADEID_FINDERI), 32);
-            else {
-                memcpy(data, ufinderi, 32);
+           get_finderinfo(path->m_name, adp, (char *)data);
+            if (!adp) {
                 if (*upath == '.') { /* make it invisible */
                     ashort = htons(FINDERINFO_INVISIBLE);
                     memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
                 }
             }
-
-            if ((!isad || (memcmp(ad_entry(adp, ADEID_FINDERI),
-                                  ufinderi, 8 ) == 0)) &&
-                    (em = getextmap( path ))) {
-                memcpy(data, em->em_type, sizeof( em->em_type ));
-                memcpy(data + 4, em->em_creator, sizeof(em->em_creator));
-            }
             data += 32;
             break;
 
         case FILPBIT_LNAME :
-            nameoff = data;
+            l_nameoff = data;
             data += sizeof( u_int16_t );
             break;
 
@@ -214,97 +353,49 @@ int getfilparams(struct vol *vol,
             break;
 
         case FILPBIT_FNUM :
-            aint = 0;
-#if AD_VERSION > AD_VERSION1
-            /* look in AD v2 header */
-            if (isad)
-                memcpy(&aint, ad_entry(adp, ADEID_DID), sizeof(aint));
-#endif /* AD_VERSION > AD_VERSION1 */
-
-#ifdef CNID_DB
-            aint = cnid_add(vol->v_db, st, dir->d_did, upath,
-                            strlen(upath), aint);
-            /* Throw errors if cnid_add fails. */
-            if (aint > CNID_MAX) {
-                switch (aint) {
-                case CNID_ERR_PARAM:
-                    LOG(log_error, logtype_default, "getfilparams: Incorrect parameters passed to cnid_add");
-                    return(AFPERR_PARAM);
-                case CNID_ERR_PATH:
-                    return(AFPERR_PARAM);
-                case CNID_ERR_DB:
-                case CNID_ERR_MAX:
-                    return(AFPERR_MISC);
-                }
-#endif /* CNID_DB */
-
-                if (aint == 0) {
-                    /*
-                     * What a fucking mess.  First thing:  DID and FNUMs are
-                     * in the same space for purposes of enumerate (and several
-                     * other wierd places).  While we consider this Apple's bug,
-                     * this is the work-around:  In order to maintain constant and
-                     * unique DIDs and FNUMs, we monotonically generate the DIDs
-                     * during the session, and derive the FNUMs from the filesystem.
-                     * Since the DIDs are small, we insure that the FNUMs are fairly
-                     * large by setting thier high bits to the device number.
-                     *
-                     * AFS already does something very similar to this for the
-                     * inode number, so we don't repeat the procedure.
-                     *
-                     * new algorithm:
-                     * due to complaints over did's being non-persistent,
-                     * here's the current hack to provide semi-persistent
-                     * did's:
-                     *      1) we reserve the first bit for file ids.
-                     *      2) the next 7 bits are for the device.
-                     *      3) the remaining 24 bits are for the inode.
-                     *
-                     * both the inode and device information are actually hashes
-                     * that are then truncated to the requisite bit length.
-                     *
-                     * it should be okay to use lstat to deal with symlinks.
-                     */
-#ifdef USE_LASTDID
-                    aint = htonl(( st->st_dev << 16 ) | (st->st_ino & 0x0000ffff));
-#else /* USE_LASTDID */
-                    lstp = lstat(upath, &lst) < 0 ? st : &lst;
-#ifdef DID_MTAB
-                    aint = htonl( afpd_st_cnid ( lstp ) );
-#else /* DID_MTAB */
-                    aint = htonl(CNID(lstp, 1));
-#endif /* DID_MTAB */
-#endif /* USE_LASTDID */
-                }
+            memcpy(data, &id, sizeof( id ));
+            data += sizeof( id );
+            break;
 
-                memcpy(data, &aint, sizeof( aint ));
-                data += sizeof( aint );
-                break;
+        case FILPBIT_DFLEN :
+            if  (st->st_size > 0xffffffff)
+               aint = 0xffffffff;
+            else
+               aint = htonl( st->st_size );
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            break;
 
-            case FILPBIT_DFLEN :
-                aint = htonl( st->st_size );
-                memcpy(data, &aint, sizeof( aint ));
-                data += sizeof( aint );
-                break;
+        case FILPBIT_RFLEN :
+            if ( adp ) {
+                if (adp->ad_rlen > 0xffffffff)
+                    aint = 0xffffffff;
+                else
+                    aint = htonl( adp->ad_rlen);
+            } else {
+                aint = 0;
+            }
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            break;
 
-            case FILPBIT_RFLEN :
-                if ( isad ) {
-                    aint = htonl( ad_getentrylen( adp, ADEID_RFORK ));
-                } else {
-                    aint = 0;
-                }
+            /* Current client needs ProDOS info block for this file.
+               Use simple heuristic and let the Mac "type" string tell
+               us what the PD file code should be.  Everything gets a
+               subtype of 0x0000 unless the original value was hashed
+               to "pXYZ" when we created it.  See IA, Ver 2.
+               <shirsch@adelphia.net> */
+        case FILPBIT_PDINFO :
+            if (afp_version >= 30) { /* UTF8 name */
+                utf8 = kTextEncodingUTF8;
+                utf_nameoff = data;
+                data += sizeof( u_int16_t );
+                aint = 0;
                 memcpy(data, &aint, sizeof( aint ));
                 data += sizeof( aint );
-                break;
-
-                /* Current client needs ProDOS info block for this file.
-                   Use simple heuristic and let the Mac "type" string tell
-                   us what the PD file code should be.  Everything gets a
-                   subtype of 0x0000 unless the original value was hashed
-                   to "pXYZ" when we created it.  See IA, Ver 2.
-                   <shirsch@ibm.net> */
-            case FILPBIT_PDINFO :
-                if ( isad ) {
+            }
+            else {
+                if ( adp ) {
                     memcpy(fdType, ad_entry( adp, ADEID_FINDERI ), 4 );
 
                     if ( memcmp( fdType, "TEXT", 4 ) == 0 ) {
@@ -343,631 +434,856 @@ int getfilparams(struct vol *vol,
                 data += sizeof( ashort );
                 memset(data, 0, sizeof( ashort ));
                 data += sizeof( ashort );
-                break;
-
-            default :
-                if ( isad ) {
-                    ad_close( adp, ADFLAGS_HF );
-                }
-                return( AFPERR_BITMAP );
             }
-            bitmap = bitmap>>1;
-            bit++;
-        }
-        if ( nameoff ) {
-            ashort = htons( data - buf );
-            memcpy(nameoff, &ashort, sizeof( ashort ));
-            if ((aint = strlen( path )) > MACFILELEN)
-                aint = MACFILELEN;
-            *data++ = aint;
-            memcpy(data, path, aint );
-            data += aint;
-        }
-        if ( isad ) {
-            ad_close( adp, ADFLAGS_HF );
-        }
-        *buflen = data - buf;
-
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "end getfilparams:");
-#endif /* DEBUG */
-
-        return( AFP_OK );
-    }
-
-    int afp_createfile(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct stat         st;
-        struct adouble ad, *adp;
-        struct vol             *vol;
-        struct dir             *dir;
-        struct ofork        *of;
-        char           *path, *upath;
-        int                    creatf, did, openf, retvalue = AFP_OK;
-        u_int16_t              vid;
-#ifdef FORCE_UIDGID
-        uidgidset              *uidgid;
-#endif /* FORCE_UIDGID */
+            break;
+        case FILPBIT_EXTDFLEN:
+            aint = htonl(st->st_size >> 32);
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            aint = htonl(st->st_size);
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            break;
+        case FILPBIT_EXTRFLEN:
+            aint = 0;
+            if (adp) 
+                aint = htonl(adp->ad_rlen >> 32);
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            if (adp) 
+                aint = htonl(adp->ad_rlen);
+            memcpy(data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            break;
+        case FILPBIT_UNIXPR :
+            aint = htonl(st->st_uid);
+            memcpy( data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+            aint = htonl(st->st_gid);
+            memcpy( data, &aint, sizeof( aint ));
+            data += sizeof( aint );
 
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_createfile:");
-#endif /* DEBUG */
+            aint = htonl(st->st_mode);
+            memcpy( data, &aint, sizeof( aint ));
+            data += sizeof( aint );
 
-        *rbuflen = 0;
-        ibuf++;
-        creatf = (unsigned char) *ibuf++;
+            accessmode( upath, &ma, dir , st);
 
-        memcpy(&vid, ibuf, sizeof( vid ));
-        ibuf += sizeof( vid );
+            *data++ = ma.ma_user;
+            *data++ = ma.ma_world;
+            *data++ = ma.ma_group;
+            *data++ = ma.ma_owner;
+            break;
 
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM );
+        default :
+            return( AFPERR_BITMAP );
         }
+        bitmap = bitmap>>1;
+        bit++;
+    }
+    if ( l_nameoff ) {
+        ashort = htons( data - buf );
+        memcpy(l_nameoff, &ashort, sizeof( ashort ));
+        data = set_name(vol, data, dir->d_did, path->m_name, id, 0);
+    }
+    if ( utf_nameoff ) {
+        ashort = htons( data - buf );
+        memcpy(utf_nameoff, &ashort, sizeof( ashort ));
+        data = set_name(vol, data, dir->d_did, path->m_name, id, utf8);
+    }
+    *buflen = data - buf;
+    return (AFP_OK);
+}
+                
+/* ----------------------- */
+int getfilparams(struct vol *vol,
+                 u_int16_t bitmap,
+                 struct path *path, struct dir *dir, 
+                 char *buf, int *buflen )
+{
+    struct adouble     ad, *adp;
+    struct ofork        *of;
+    char                   *upath;
+    u_int16_t          attrbits = 0;
+    int                 opened = 0;
+    int rc;    
 
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
-
-        memcpy(&did, ibuf, sizeof( did));
-        ibuf += sizeof( did );
+#ifdef DEBUG
+    LOG(log_info, logtype_default, "begin getfilparams:");
+#endif /* DEBUG */
 
-        if (( dir = dirsearch( vol, did )) == NULL ) {
-            return( AFPERR_NOOBJ );
+    opened = PARAM_NEED_ADP(bitmap);
+    adp = NULL;
+    if (opened) {
+        upath = path->u_name;
+        if ((of = of_findname(path))) {
+            adp = of->of_ad;
+           attrbits = ((of->of_ad->ad_df.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
+           attrbits |= ((of->of_ad->ad_hf.adf_refcount > of->of_ad->ad_df.adf_refcount)? ATTRBIT_ROPEN : 0);
+        } else {
+            ad_init(&ad, vol->v_adouble);
+            adp = &ad;
         }
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_NOOBJ );
-        }
+        if ( ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, adp) < 0 ) {
+             adp = NULL;
+        }
+        else {
+           /* FIXME 
+              we need to check if the file is open by another process.
+              it's slow so we only do it if we have to:
+              - bitmap is requested.
+              - we don't already have the answer!
+           */
+           if ((bitmap & (1 << FILPBIT_ATTR))) {
+                if (!(attrbits & ATTRBIT_ROPEN)) {
+                    attribs | = ad_testlock(adp, ADEID_RFORK, AD_FILELOCK_OPEN_RD) ? ATTRBIT_ROPEN : 0;
+                    attribs | = ad_testlock(adp, ADEID_RFORK, AD_FILELOCK_OPEN_WR) ? ATTRBIT_ROPEN : 0;
+                }
+                if (!(attrbits & ATTRBIT_DOPEN)) {
+                    attribs | = ad_testlock(adp, ADEID_DFORK, AD_FILELOCK_OPEN_RD) ? ATTRBIT_DOPEN : 0;
+                    attribs | = ad_testlock(adp, ADEID_DFORK, AD_FILELOCK_OPEN_WR) ? ATTRBIT_DOPEN : 0;
+                }
+           }
+       }
+    }
+    rc = getmetadata(vol, bitmap, path, dir, buf, buflen, adp, attrbits);
+    if ( adp ) {
+        ad_close( adp, ADFLAGS_HF );
+    }
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "end getfilparams:");
+#endif /* DEBUG */
 
-        if (!wincheck(vol, path))
-            return AFPERR_PARAM;
+    return( rc );
+}
 
-        upath = mtoupath(vol, path);
+/* ----------------------------- */
+int afp_createfile(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct adouble     ad, *adp;
+    struct vol         *vol;
+    struct dir         *dir;
+    struct ofork        *of = NULL;
+    char               *path, *upath;
+    int                        creatf, did, openf, retvalue = AFP_OK;
+    u_int16_t          vid;
+    int                 ret;
+    struct path                *s_path;
+    
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "begin afp_createfile:");
+#endif /* DEBUG */
 
-        if ((vol->v_flags & AFPVOL_NOHEX) && strchr(upath, '/'))
-            return AFPERR_PARAM;
+    *rbuflen = 0;
+    ibuf++;
+    creatf = (unsigned char) *ibuf++;
 
-        if (!validupath(vol, upath))
-            return AFPERR_EXIST;
+    memcpy(&vid, ibuf, sizeof( vid ));
+    ibuf += sizeof( vid );
 
-        /* check for vetoed filenames */
-        if (veto_file(vol->v_veto, upath))
-            return AFPERR_EXIST;
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM );
+    }
 
-        if ((of = of_findname(vol, curdir, path))) {
-            adp = of->of_ad;
-        } else {
-            memset(&ad, 0, sizeof(ad));
-            adp = &ad;
-        }
-        if ( creatf) {
-            /* on a hard create, fail if file exists and is open */
-            if ((stat(upath, &st) == 0) && of)
-                return AFPERR_BUSY;
-            openf = O_RDWR|O_CREAT|O_TRUNC;
-        } else {
-            openf = O_RDWR|O_CREAT|O_EXCL;
-        }
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-#ifdef FORCE_UIDGID
+    memcpy(&did, ibuf, sizeof( did));
+    ibuf += sizeof( did );
 
-        /* preserve current euid, egid */
-        save_uidgid ( uidgid );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno;
+    }
 
-        /* perform all switching of users */
-        set_uidgid ( vol );
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_PARAM);
+    }
 
-#endif /* FORCE_UIDGID */
+    if ( *s_path->m_name == '\0' ) {
+        return( AFPERR_BADTYPE );
+    }
 
-        if ( ad_open( upath, vol_noadouble(vol)|ADFLAGS_DF|ADFLAGS_HF,
-                      openf, 0666, adp) < 0 ) {
-            switch ( errno ) {
-            case EEXIST :
-#ifdef FORCE_UIDGID
-                /* bring everything back to old euid, egid */
-                restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-                return( AFPERR_EXIST );
-            case EACCES :
-#ifdef FORCE_UIDGID
-                /* bring everything back to old euid, egid */
-                restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-                return( AFPERR_ACCESS );
-            case ENOENT:
-                /* on noadouble volumes, just creating the data fork is ok */
-                if (vol_noadouble(vol) && (stat(upath, &st) == 0))
-                    goto createfile_done;
-                /* fallthrough */
-            default :
-#ifdef FORCE_UIDGID
-                /* bring everything back to old euid, egid */
-                restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-                return( AFPERR_PARAM );
-            }
+    upath = s_path->u_name;
+    if (0 != (ret = check_name(vol, upath))) 
+       return  ret;
+    
+    /* if upath is deleted we already in trouble anyway */
+    if ((of = of_findname(s_path))) {
+        adp = of->of_ad;
+    } else {
+        ad_init(&ad, vol->v_adouble);
+        adp = &ad;
+    }
+    if ( creatf) {
+        /* on a hard create, fail if file exists and is open */
+        if (of)
+            return AFPERR_BUSY;
+        openf = O_RDWR|O_CREAT|O_TRUNC;
+    } else {
+       /* on a soft create, if the file is open then ad_open won't fail
+          because open syscall is not called
+       */
+       if (of) {
+               return AFPERR_EXIST;
+       }
+        openf = O_RDWR|O_CREAT|O_EXCL;
+    }
+
+    if ( ad_open( upath, vol_noadouble(vol)|ADFLAGS_DF|ADFLAGS_HF|ADFLAGS_NOHF,
+                  openf, 0666, adp) < 0 ) {
+        switch ( errno ) {
+        case EROFS:
+            return AFPERR_VLOCK;
+        case ENOENT : /* we were already in 'did folder' so chdir() didn't fail */
+            return ( AFPERR_NOOBJ );
+        case EEXIST :
+            return( AFPERR_EXIST );
+        case EACCES :
+            return( AFPERR_ACCESS );
+        default :
+            return( AFPERR_PARAM );
         }
+    }
+    if ( ad_hfileno( adp ) == -1 ) {
+         /* on noadouble volumes, just creating the data fork is ok */
+         if (vol_noadouble(vol)) {
+             ad_close( adp, ADFLAGS_DF );
+             goto createfile_done;
+         }
+         /* FIXME with hard create on an existing file, we already
+          * corrupted the data file.
+          */
+         netatalk_unlink( upath );
+         ad_close( adp, ADFLAGS_DF );
+         return AFPERR_ACCESS;
+    }
 
-        ad_setentrylen( adp, ADEID_NAME, strlen( path ));
-        memcpy(ad_entry( adp, ADEID_NAME ), path,
-               ad_getentrylen( adp, ADEID_NAME ));
-        ad_flush( adp, ADFLAGS_DF|ADFLAGS_HF );
-        ad_close( adp, ADFLAGS_DF|ADFLAGS_HF );
+    path = s_path->m_name;
+    ad_setentrylen( adp, ADEID_NAME, strlen( path ));
+    memcpy(ad_entry( adp, ADEID_NAME ), path,
+           ad_getentrylen( adp, ADEID_NAME ));
+    ad_flush( adp, ADFLAGS_DF|ADFLAGS_HF );
+    ad_close( adp, ADFLAGS_DF|ADFLAGS_HF );
 
 createfile_done:
+    curdir->offcnt++;
 
 #ifdef DROPKLUDGE
-        if (vol->v_flags & AFPVOL_DROPBOX) {
-            retvalue = matchfile2dirperms(upath, vol, did);
-        }
+    if (vol->v_flags & AFPVOL_DROPBOX) {
+        retvalue = matchfile2dirperms(upath, vol, did);
+    }
 #endif /* DROPKLUDGE */
 
-        setvoltime(obj, vol );
+    setvoltime(obj, vol );
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end afp_createfile");
+    LOG(log_info, logtype_afpd, "end afp_createfile");
 #endif /* DEBUG */
 
-#ifdef FORCE_UIDGID
-        /* bring everything back to old euid, egid */
-        restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-
-        return (retvalue);
-    }
+    return (retvalue);
+}
 
-    int afp_setfilparams(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct vol     *vol;
-        struct dir     *dir;
-        char   *path;
-        int            did, rc;
-        u_int16_t      vid, bitmap;
+int afp_setfilparams(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct vol *vol;
+    struct dir *dir;
+    struct path *s_path;
+    int                did, rc;
+    u_int16_t  vid, bitmap;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_setfilparams:");
+    LOG(log_info, logtype_afpd, "begin afp_setfilparams:");
 #endif /* DEBUG */
 
-        *rbuflen = 0;
-        ibuf += 2;
+    *rbuflen = 0;
+    ibuf += 2;
 
-        memcpy(&vid, ibuf, sizeof( vid ));
-        ibuf += sizeof( vid );
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    memcpy(&vid, ibuf, sizeof( vid ));
+    ibuf += sizeof( vid );
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM );
+    }
 
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-        memcpy(&did, ibuf, sizeof( did ));
-        ibuf += sizeof( did );
-        if (( dir = dirsearch( vol, did )) == NULL ) {
-            return( AFPERR_NOOBJ );
-        }
+    memcpy(&did, ibuf, sizeof( did ));
+    ibuf += sizeof( did );
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno; /* was AFPERR_NOOBJ */
+    }
 
-        memcpy(&bitmap, ibuf, sizeof( bitmap ));
-        bitmap = ntohs( bitmap );
-        ibuf += sizeof( bitmap );
+    memcpy(&bitmap, ibuf, sizeof( bitmap ));
+    bitmap = ntohs( bitmap );
+    ibuf += sizeof( bitmap );
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_NOOBJ );
-        }
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_PARAM);
+    }
 
-        if ((u_long)ibuf & 1 ) {
-            ibuf++;
-        }
+    if (path_isadir(s_path)) {
+        return( AFPERR_BADTYPE ); /* it's a directory */
+    }
 
-        if (( rc = setfilparams(vol, path, bitmap, ibuf )) == AFP_OK ) {
-            setvoltime(obj, vol );
-        }
+    if ( s_path->st_errno != 0 ) {
+        return( AFPERR_NOOBJ );
+    }
+
+    if ((u_long)ibuf & 1 ) {
+        ibuf++;
+    }
+
+    if (AFP_OK == ( rc = setfilparams(vol, s_path, bitmap, ibuf )) ) {
+        setvoltime(obj, vol );
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end afp_setfilparams:");
+    LOG(log_info, logtype_afpd, "end afp_setfilparams:");
 #endif /* DEBUG */
 
-        return( rc );
-    }
+    return( rc );
+}
 
+/*
+ * cf AFP3.0.pdf page 252 for change_mdate and change_parent_mdate logic  
+ *
+*/
+extern struct path Cur_Path;
 
-    int setfilparams(struct vol *vol,
-                     char *path, u_int16_t bitmap, char *buf )
-    {
-        struct adouble ad, *adp;
-        struct ofork        *of;
-        struct extmap  *em;
-        int                    bit = 0, isad = 1, err = AFP_OK;
-        char                *upath;
-        u_char              achar, *fdType, xyy[4];
-        u_int16_t              ashort, bshort;
-        u_int32_t              aint;
-        struct utimbuf ut;
+int setfilparams(struct vol *vol,
+                 struct path *path, u_int16_t bitmap, char *buf )
+{
+    struct adouble     ad, *adp;
+    struct ofork        *of;
+    struct extmap      *em;
+    int                        bit = 0, isad = 1, err = AFP_OK;
+    char                *upath;
+    u_char              achar, *fdType, xyy[4];
+    u_int16_t          ashort, bshort;
+    u_int32_t          aint;
+    struct utimbuf     ut;
 
-#ifdef FORCE_UIDGID
-        uidgidset              *uidgid;
+    int                 change_mdate = 0;
+    int                 change_parent_mdate = 0;
+    int                 newdate = 0;
+    struct timeval      tv;
 
-        uidgid = malloc(sizeof(uidgidset));
-#endif /* FORCE_UIDGID */
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin setfilparams:");
+    LOG(log_info, logtype_afpd, "begin setfilparams:");
 #endif /* DEBUG */
 
-        upath = mtoupath(vol, path);
-        if ((of = of_findname(vol, curdir, path))) {
-            adp = of->of_ad;
-        } else {
-            memset(&ad, 0, sizeof(ad));
-            adp = &ad;
+    upath = path->u_name;
+    if ((of = of_findname(path))) {
+        adp = of->of_ad;
+    } else {
+        ad_init(&ad, vol->v_adouble);
+        adp = &ad;
+    }
+
+    if (!vol_unix_priv(vol) && check_access(upath, OPENACC_WR ) < 0) {
+        return AFPERR_ACCESS;
+    }
+
+    if (ad_open( upath, vol_noadouble(vol) | ADFLAGS_HF,
+                 O_RDWR|O_CREAT, 0666, adp) < 0) {
+        /* for some things, we don't need an adouble header */
+        if (bitmap & ~(1<<FILPBIT_MDATE)) {
+            return vol_noadouble(vol) ? AFP_OK : AFPERR_ACCESS;
         }
+        isad = 0;
+    } else if ((ad_get_HF_flags( adp ) & O_CREAT) ) {
+        ad_setentrylen( adp, ADEID_NAME, strlen( path->m_name ));
+        memcpy(ad_entry( adp, ADEID_NAME ), path->m_name,
+               ad_getentrylen( adp, ADEID_NAME ));
+    }
 
-#ifdef FORCE_UIDGID
-        save_uidgid ( uidgid );
-        set_uidgid ( vol );
-#endif /* FORCE_UIDGID */
-
-        if (ad_open( upath, vol_noadouble(vol) | ADFLAGS_HF,
-                     O_RDWR|O_CREAT, 0666, adp) < 0) {
-            /* for some things, we don't need an adouble header */
-            if (bitmap & ~(1<<FILPBIT_MDATE)) {
-#ifdef FORCE_UIDGID
-                restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
-                return vol_noadouble(vol) ? AFP_OK : AFPERR_ACCESS;
-            }
-            isad = 0;
-        } else if ((ad_getoflags( adp, ADFLAGS_HF ) & O_CREAT) ) {
-            ad_setentrylen( adp, ADEID_NAME, strlen( path ));
-            memcpy(ad_entry( adp, ADEID_NAME ), path,
-                   ad_getentrylen( adp, ADEID_NAME ));
+    while ( bitmap != 0 ) {
+        while (( bitmap & 1 ) == 0 ) {
+            bitmap = bitmap>>1;
+            bit++;
         }
 
-        while ( bitmap != 0 ) {
-            while (( bitmap & 1 ) == 0 ) {
-                bitmap = bitmap>>1;
-                bit++;
+        switch(  bit ) {
+        case FILPBIT_ATTR :
+            change_mdate = 1;
+            memcpy(&ashort, buf, sizeof( ashort ));
+            ad_getattr(adp, &bshort);
+            if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
+                bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
+            } else {
+                bshort &= ~ashort;
             }
+            if ((ashort & htons(ATTRBIT_INVISIBLE)))
+                change_parent_mdate = 1;
+            ad_setattr(adp, bshort);
+            buf += sizeof( ashort );
+            break;
 
-            switch(  bit ) {
-            case FILPBIT_ATTR :
-                memcpy(&ashort, buf, sizeof( ashort ));
-                ad_getattr(adp, &bshort);
-                if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
-                    bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
-                } else {
-                    bshort &= ~ashort;
-                }
-                ad_setattr(adp, bshort);
-                buf += sizeof( ashort );
-                break;
-
-            case FILPBIT_CDATE :
-                memcpy(&aint, buf, sizeof(aint));
-                ad_setdate(adp, AD_DATE_CREATE, aint);
-                buf += sizeof( aint );
-                break;
+        case FILPBIT_CDATE :
+            change_mdate = 1;
+            memcpy(&aint, buf, sizeof(aint));
+            ad_setdate(adp, AD_DATE_CREATE, aint);
+            buf += sizeof( aint );
+            break;
 
-            case FILPBIT_MDATE :
-                memcpy(&aint, buf, sizeof( aint ));
-                if (isad)
-                    ad_setdate(adp, AD_DATE_MODIFY, aint);
-                ut.actime = ut.modtime = AD_DATE_TO_UNIX(aint);
-                utime(upath, &ut);
-                buf += sizeof( aint );
-                break;
+        case FILPBIT_MDATE :
+            memcpy(&newdate, buf, sizeof( newdate ));
+            buf += sizeof( newdate );
+            break;
 
-            case FILPBIT_BDATE :
-                memcpy(&aint, buf, sizeof(aint));
-                ad_setdate(adp, AD_DATE_BACKUP, aint);
-                buf += sizeof( aint );
-                break;
+        case FILPBIT_BDATE :
+            change_mdate = 1;
+            memcpy(&aint, buf, sizeof(aint));
+            ad_setdate(adp, AD_DATE_BACKUP, aint);
+            buf += sizeof( aint );
+            break;
 
-            case FILPBIT_FINFO :
-                if ((memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 ) == 0)
-                        && (em = getextmap( path )) &&
-                        (memcmp(buf, em->em_type, sizeof( em->em_type )) == 0) &&
-                        (memcmp(buf + 4, em->em_creator,
-                                sizeof( em->em_creator )) == 0)) {
-                    memcpy(buf, ufinderi, 8 );
-                }
-                memcpy(ad_entry( adp, ADEID_FINDERI ), buf, 32 );
-                buf += 32;
-                break;
+        case FILPBIT_FINFO :
+            change_mdate = 1;
+
+            if (!memcmp( ad_entry( adp, ADEID_FINDERI ), ufinderi, 8 )
+                    && ( 
+                     ((em = getextmap( path->m_name )) &&
+                      !memcmp(buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(buf + 4, em->em_creator,sizeof( em->em_creator)))
+                     || ((em = getdefextmap()) &&
+                      !memcmp(buf, em->em_type, sizeof( em->em_type )) &&
+                      !memcmp(buf + 4, em->em_creator,sizeof( em->em_creator)))
+            )) {
+                memcpy(buf, ufinderi, 8 );
+            }
 
-                /* Client needs to set the ProDOS file info for this file.
-                   Use defined strings for the simple cases, and convert
-                   all else into pXYY per Inside Appletalk.  Always set
-                   the creator as "pdos". <shirsch@ibm.net> */
-            case FILPBIT_PDINFO :
-                achar = *buf;
-                buf += 2;
-                memcpy(&ashort, buf, sizeof( ashort ));
-                ashort = ntohs( ashort );
-                buf += 2;
+            memcpy(ad_entry( adp, ADEID_FINDERI ), buf, 32 );
+            buf += 32;
+            break;
 
-                switch ( (unsigned int) achar )
-                {
-                case 0x04 :
-                    fdType = ( u_char *) "TEXT";
-                    break;
-
-                case 0xff :
-                    fdType = ( u_char *) "PSYS";
-                    break;
-
-                case 0xb3 :
-                    fdType = ( u_char *) "PS16";
-                    break;
-
-                case 0x00 :
-                    fdType = ( u_char *) "BINA";
-                    break;
-
-                default :
-                    xyy[0] = ( u_char ) 'p';
-                    xyy[1] = achar;
-                    xyy[2] = ( u_char ) ( ashort >> 8 ) & 0xff;
-                    xyy[3] = ( u_char ) ashort & 0xff;
-                    fdType = xyy;
-                    break;
-                }
+        case FILPBIT_UNIXPR :
+           /* Skip the UIG/GID, no way to set them from OSX clients */
+            buf += sizeof( aint );
+            buf += sizeof( aint );
 
+            change_mdate = 1;
+            change_parent_mdate = 1;
+            memcpy( &aint, buf, sizeof( aint ));
+            buf += sizeof( aint );
+            aint = ntohl (aint);
+
+            setfilemode(path, aint);
+            break;
+            /* Client needs to set the ProDOS file info for this file.
+               Use a defined string for TEXT to support crlf
+               translations and convert all else into pXYY per Inside
+               Appletalk.  Always set the creator as "pdos".  Changes
+               from original by Marsha Jackson. */
+        case FILPBIT_PDINFO :
+            if (afp_version < 30) { /* else it's UTF8 name */
+                achar = *buf;
+                buf += 2;
+                /* Keep special case to support crlf translations */
+                if ((unsigned int) achar == 0x04) {
+                   fdType = (u_char *)"TEXT";
+                   buf += 2;
+                } else {
+                   xyy[0] = ( u_char ) 'p';
+                   xyy[1] = achar;
+                   xyy[3] = *buf++;
+                   xyy[2] = *buf++;
+                   fdType = xyy;
+               }
                 memcpy(ad_entry( adp, ADEID_FINDERI ), fdType, 4 );
                 memcpy(ad_entry( adp, ADEID_FINDERI ) + 4, "pdos", 4 );
                 break;
-
-
-            default :
-                err = AFPERR_BITMAP;
-                goto setfilparam_done;
             }
-
-            bitmap = bitmap>>1;
-            bit++;
+            /* fallthrough */
+        default :
+            err = AFPERR_BITMAP;
+            goto setfilparam_done;
         }
 
-setfilparam_done:
-        if (isad) {
-            ad_flush( adp, ADFLAGS_HF );
-            ad_close( adp, ADFLAGS_HF );
+        bitmap = bitmap>>1;
+        bit++;
+    }
 
-#ifdef FORCE_UIDGID
-            restore_uidgid ( uidgid );
-#endif /* FORCE_UIDGID */
+setfilparam_done:
+    if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
+       newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
+    }
+    if (newdate) {
+       if (isad)
+          ad_setdate(adp, AD_DATE_MODIFY, newdate);
+       ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
+       utime(upath, &ut);
+    }
 
-        }
+    if (isad) {
+        ad_flush( adp, ADFLAGS_HF );
+        ad_close( adp, ADFLAGS_HF );
 
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "end setfilparams:");
-#endif /* DEBUG */
+    }
 
-        return err;
+    if (change_parent_mdate && gettimeofday(&tv, NULL) == 0) {
+        newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
+        bitmap = 1<<FILPBIT_MDATE;
+        setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
     }
 
-    /*
-     * renamefile and copyfile take the old and new unix pathnames
-     * and the new mac name.
-     * NOTE: if we have to copy a file instead of renaming it, locks
-     *       will break.
-     */
-    int renamefile(src, dst, newname, noadouble )
-    char       *src, *dst, *newname;
-    const int         noadouble;
-    {
-        struct adouble ad;
-        char           adsrc[ MAXPATHLEN + 1];
-        int                    len, rc;
-
-        /*
-         * Note that this is only checking the existance of the data file,
-         * not the header file.  The thinking is that if the data file doesn't
-         * exist, but the header file does, the right thing to do is remove
-         * the data file silently.
-         */
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "end setfilparams:");
+#endif /* DEBUG */
+    return err;
+}
 
-        /* existence check moved to afp_moveandrename */
+/*
+ * renamefile and copyfile take the old and new unix pathnames
+ * and the new mac name.
+ *
+ * src         the source path 
+ * dst         the dest filename in current dir
+ * newname     the dest mac name
+ * adp         adouble struct of src file, if open, or & zeroed one
+ *
+ */
+int renamefile(src, dst, newname, noadouble, adp )
+char   *src, *dst, *newname;
+const int         noadouble;
+struct adouble    *adp;
+{
+    char       adsrc[ MAXPATHLEN + 1];
+    int                len;
+    int                rc;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin renamefile:");
+    LOG(log_info, logtype_afpd, "begin renamefile:");
 #endif /* DEBUG */
 
-        if ( rename( src, dst ) < 0 ) {
-            switch ( errno ) {
-            case ENOENT :
-                return( AFPERR_NOOBJ );
-            case EPERM:
-            case EACCES :
-                return( AFPERR_ACCESS );
-            case EROFS:
-                return AFPERR_VLOCK;
-            case EXDEV :                       /* Cross device move -- try copy */
-                if (( rc = copyfile(src, dst, newname, noadouble )) != AFP_OK ) {
-                    deletefile( dst );
-                    return( rc );
-                }
-                return deletefile( src );
-            default :
-                return( AFPERR_PARAM );
+    if ( unix_rename( src, dst ) < 0 ) {
+        switch ( errno ) {
+        case ENOENT :
+            return( AFPERR_NOOBJ );
+        case EPERM:
+        case EACCES :
+            return( AFPERR_ACCESS );
+        case EROFS:
+            return AFPERR_VLOCK;
+        case EXDEV :                   /* Cross device move -- try copy */
+           /* NOTE: with open file it's an error because after the copy we will 
+            * get two files, it's fixable for our process (eg reopen the new file, get the
+            * locks, and so on. But it doesn't solve the case with a second process
+            */
+           if (adp->ad_df.adf_refcount || adp->ad_hf.adf_refcount) {
+               /* FIXME  warning in syslog so admin'd know there's a conflict ?*/
+               return AFPERR_OLOCK; /* little lie */
+           }
+            if (AFP_OK != ( rc = copyfile(src, dst, newname, noadouble )) ) {
+                /* on error copyfile delete dest */
+                return( rc );
             }
+            return deletefile(NULL, src, 0);
+        default :
+            return( AFPERR_PARAM );
         }
+    }
 
-        strcpy( adsrc, ad_path( src, 0 ));
-        rc = 0;
-rename_retry:
-        if (rename( adsrc, ad_path( dst, 0 )) < 0 ) {
-            struct stat st;
-
-            switch ( errno ) {
-            case ENOENT :
-                /* check for a source appledouble header. if it exists, make
-                 * a dest appledouble directory and do the rename again. */
-                memset(&ad, 0, sizeof(ad));
-                if (rc || stat(adsrc, &st) ||
-                        (ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad) < 0))
-                    return AFP_OK;
-                rc++;
-                ad_close(&ad, ADFLAGS_HF);
-                goto rename_retry;
-            case EPERM:
-            case EACCES :
-                return( AFPERR_ACCESS );
-            case EROFS:
-                return AFPERR_VLOCK;
-            default :
-                return( AFPERR_PARAM );
+    strcpy( adsrc, ad_path( src, 0 ));
+
+    if (unix_rename( adsrc, ad_path( dst, 0 )) < 0 ) {
+        struct stat st;
+        int err;
+        
+        err = errno;        
+       if (errno == ENOENT) {
+           struct adouble    ad;
+
+            if (stat(adsrc, &st)) /* source has no ressource fork, */
+                return AFP_OK;
+            
+            /* We are here  because :
+             * -there's no dest folder. 
+             * -there's no .AppleDouble in the dest folder.
+             * if we use the struct adouble passed in parameter it will not
+             * create .AppleDouble if the file is already opened, so we
+             * use a diff one, it's not a pb,ie it's not the same file, yet.
+             */
+            ad_init(&ad, AD_VERSION); /* FIXME */
+            if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
+               ad_close(&ad, ADFLAGS_HF);
+               if (!unix_rename( adsrc, ad_path( dst, 0 )) ) 
+                   err = 0;
+                else 
+                   err = errno;
             }
-        }
-
-        memset(&ad, 0, sizeof(ad));
-        if ( ad_open( dst, ADFLAGS_HF, O_RDWR, 0666, &ad) < 0 ) {
-            switch ( errno ) {
+            else { /* it's something else, bail out */
+               err = errno;
+           }
+       }
+       /* try to undo the data fork rename,
+        * we know we are on the same device 
+       */
+       if (err) {
+           unix_rename( dst, src ); 
+           /* return the first error */
+           switch ( err) {
             case ENOENT :
-                return( AFPERR_NOOBJ );
+                return AFPERR_NOOBJ;
+            case EPERM:
             case EACCES :
-                return( AFPERR_ACCESS );
+                return AFPERR_ACCESS ;
             case EROFS:
                 return AFPERR_VLOCK;
             default :
-                return( AFPERR_PARAM );
+                return AFPERR_PARAM ;
             }
         }
+    }
 
+    /* don't care if we can't open the newly renamed ressource fork
+     */
+    if ( !ad_open( dst, ADFLAGS_HF, O_RDWR, 0666, adp)) {
         len = strlen( newname );
-        ad_setentrylen( &ad, ADEID_NAME, len );
-        memcpy(ad_entry( &ad, ADEID_NAME ), newname, len );
-        ad_flush( &ad, ADFLAGS_HF );
-        ad_close( &ad, ADFLAGS_HF );
-
+        ad_setentrylen( adp, ADEID_NAME, len );
+        memcpy(ad_entry( adp, ADEID_NAME ), newname, len );
+        ad_flush( adp, ADFLAGS_HF );
+        ad_close( adp, ADFLAGS_HF );
+    }
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end renamefile:");
+    LOG(log_info, logtype_afpd, "end renamefile:");
 #endif /* DEBUG */
 
-        return( AFP_OK );
-    }
+    return( AFP_OK );
+}
+
+int copy_path_name(char *newname, char *ibuf)
+{
+char        type = *ibuf;
+size_t      plen = 0;
+u_int16_t   len16;
+u_int32_t   hint;
 
-    int afp_copyfile(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct vol     *vol;
-        struct dir     *dir;
-        char   *newname, *path, *p;
-        u_int32_t      sdid, ddid;
-        int            plen, err, retvalue = AFP_OK;
-        u_int16_t      svid, dvid;
+    if ( type != 2 && !(afp_version >= 30 && type == 3) ) {
+        return -1;
+    }
+    ibuf++;
+    switch (type) {
+    case 2:
+        if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
+            strncpy( newname, ibuf, plen );
+            newname[ plen ] = '\0';
+            if (strlen(newname) != plen) {
+                /* there's \0 in newname, e.g. it's a pathname not
+                 * only a filename. 
+                */
+               return -1;
+            }
+        }
+        break;
+    case 3:
+        memcpy(&hint, ibuf, sizeof(hint));
+        ibuf += sizeof(hint);
+           
+        memcpy(&len16, ibuf, sizeof(len16));
+        ibuf += sizeof(len16);
+        plen = ntohs(len16);
+        
+        if (plen) {
+            if (plen > AFPOBJ_TMPSIZ) {
+               return -1;
+            }
+            strncpy( newname, ibuf, plen );
+            newname[ plen ] = '\0';
+            if (strlen(newname) != plen) {
+               return -1;
+            }
+        }
+        break;
+    }
+    return plen;
+}
+
+/* -----------------------------------
+*/
+int afp_copyfile(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct vol *vol;
+    struct dir *dir;
+    char       *newname, *p, *upath;
+    struct path *s_path;
+    u_int32_t  sdid, ddid;
+    int         err, retvalue = AFP_OK;
+    u_int16_t  svid, dvid;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_copyfile:");
+    LOG(log_info, logtype_afpd, "begin afp_copyfile:");
 #endif /* DEBUG */
 
-        *rbuflen = 0;
-        ibuf += 2;
-
-        memcpy(&svid, ibuf, sizeof( svid ));
-        ibuf += sizeof( svid );
-        if (( vol = getvolbyvid( svid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
-
-        memcpy(&sdid, ibuf, sizeof( sdid ));
-        ibuf += sizeof( sdid );
-        if (( dir = dirsearch( vol, sdid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    *rbuflen = 0;
+    ibuf += 2;
 
-        memcpy(&dvid, ibuf, sizeof( dvid ));
-        ibuf += sizeof( dvid );
-        memcpy(&ddid, ibuf, sizeof( ddid ));
-        ibuf += sizeof( ddid );
+    memcpy(&svid, ibuf, sizeof( svid ));
+    ibuf += sizeof( svid );
+    if (NULL == ( vol = getvolbyvid( svid )) ) {
+        return( AFPERR_PARAM );
+    }
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_NOOBJ );
-        }
-        if ( *path == '\0' ) {
-            return( AFPERR_BADTYPE );
-        }
+    memcpy(&sdid, ibuf, sizeof( sdid ));
+    ibuf += sizeof( sdid );
+    if (NULL == ( dir = dirlookup( vol, sdid )) ) {
+        return afp_errno;
+    }
 
-        /* don't allow copies when the file is open.
-         * XXX: the spec only calls for read/deny write access.
-         *      however, copyfile doesn't have any of that info,
-         *      and locks need to stay coherent. as a result,
-         *      we just balk if the file is opened already. */
-        if (of_findname(vol, curdir, path))
-            return AFPERR_DENYCONF;
+    memcpy(&dvid, ibuf, sizeof( dvid ));
+    ibuf += sizeof( dvid );
+    memcpy(&ddid, ibuf, sizeof( ddid ));
+    ibuf += sizeof( ddid );
 
-        newname = obj->newtmp;
-        strcpy( newname, path );
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_PARAM);
+    }
+    if ( path_isadir(s_path) ) {
+        return( AFPERR_BADTYPE );
+    }
 
-        p = ctoupath( vol, curdir, newname );
+    /* don't allow copies when the file is open.
+     * XXX: the spec only calls for read/deny write access.
+     *      however, copyfile doesn't have any of that info,
+     *      and locks need to stay coherent. as a result,
+     *      we just balk if the file is opened already. */
 
-        if (( vol = getvolbyvid( dvid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    newname = obj->newtmp;
+    strcpy( newname, s_path->m_name );
 
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
+    if (of_findname(s_path))
+        return AFPERR_DENYCONF;
 
-        if (( dir = dirsearch( vol, ddid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    p = ctoupath( vol, curdir, newname );
+    if (!p) {
+        return AFPERR_PARAM;
+    
+    }
+#ifdef FORCE_UIDGID
+    /* FIXME svid != dvid && dvid's user can't read svid */
+#endif
+    if (NULL == ( vol = getvolbyvid( dvid )) ) {
+        return( AFPERR_PARAM );
+    }
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_NOOBJ );
-        }
-        if ( *path != '\0' ) {
-            return( AFPERR_BADTYPE );
-        }
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-        /* one of the handful of places that knows about the path type */
-        if ( *ibuf++ != 2 ) {
-            return( AFPERR_PARAM );
-        }
-        if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
-            strncpy( newname, ibuf, plen );
-            newname[ plen ] = '\0';
-        }
+    if (NULL == ( dir = dirlookup( vol, ddid )) ) {
+        return afp_errno;
+    }
 
-        if ( (err = copyfile(p, mtoupath(vol, newname ), newname,
-                             vol_noadouble(vol))) < 0 ) {
-            return err;
-        }
+    if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
+    }
+    if ( *s_path->m_name != '\0' ) {
+#if 0
+        return (path_isadir( s_path))? AFPERR_PARAM:AFPERR_BADTYPE ;
+#endif        
+       path_error(s_path, AFPERR_PARAM);
+    }
 
-        setvoltime(obj, vol );
+    /* one of the handful of places that knows about the path type */
+    if (copy_path_name(newname, ibuf) < 0) {
+        return( AFPERR_PARAM );
+    }
+    /* newname is always only a filename so curdir *is* its
+     * parent folder
+    */
+    if (NULL == (upath = mtoupath(vol, newname, curdir->d_did, utf8_encoding()))) {
+        return( AFPERR_PARAM );
+    }
+    if ( (err = copyfile(p, upath , newname, vol_noadouble(vol))) < 0 ) {
+        return err;
+    }
+    curdir->offcnt++;
 
 #ifdef DROPKLUDGE
-        if (vol->v_flags & AFPVOL_DROPBOX) {
-            retvalue=matchfile2dirperms(newname, vol, sdid);
-        }
+    if (vol->v_flags & AFPVOL_DROPBOX) {
+        retvalue=matchfile2dirperms(upath, vol, ddid); /* FIXME sdir or ddid */
+    }
 #endif /* DROPKLUDGE */
 
+    setvoltime(obj, vol );
+
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end afp_copyfile:");
+    LOG(log_info, logtype_afpd, "end afp_copyfile:");
 #endif /* DEBUG */
 
-        return( retvalue );
-    }
+    return( retvalue );
+}
 
 
-    static __inline__ int copy_all(const int dfd, const void *buf,
-                                   size_t buflen)
-    {
-        ssize_t cc;
+static __inline__ int copy_all(const int dfd, const void *buf,
+                               size_t buflen)
+{
+    ssize_t cc;
+
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "begin copy_all:");
+#endif /* DEBUG */
+
+    while (buflen > 0) {
+        if ((cc = write(dfd, buf, buflen)) < 0) {
+            switch (errno) {
+            case EINTR:
+                continue;
+            case EDQUOT:
+            case EFBIG:
+            case ENOSPC:
+                return AFPERR_DFULL;
+            case EROFS:
+                return AFPERR_VLOCK;
+            default:
+                return AFPERR_PARAM;
+            }
+        }
+        buflen -= cc;
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin copy_all:");
+    LOG(log_info, logtype_afpd, "end copy_all:");
 #endif /* DEBUG */
 
-        while (buflen > 0) {
-            if ((cc = write(dfd, buf, buflen)) < 0) {
+    return AFP_OK;
+}
+
+/* -------------------------- */
+static int copy_fd(int dfd, int sfd)
+{
+    ssize_t cc;
+    int     err = AFP_OK;
+    char    filebuf[8192];
+    
+#ifdef SENDFILE_FLAVOR_LINUX
+    off_t   offset = 0;
+    size_t  size;
+    struct stat         st;
+    #define BUF 128*1024*1024
+
+    if (fstat(sfd, &st) == 0) {
+        
+        while (1) {
+            if ( offset >= st.st_size) {
+               return AFP_OK;
+            }
+            size = (st.st_size -offset > BUF)?BUF:st.st_size -offset;
+            if ((cc = sys_sendfile(dfd, sfd, &offset, size)) < 0) {
                 switch (errno) {
-                case EINTR:
-                    continue;
+                case ENOSYS:
+                case EINVAL:  /* there's no guarantee that all fs support sendfile */
+                    goto no_sendfile;
                 case EDQUOT:
                 case EFBIG:
                 case ENOSPC:
@@ -978,767 +1294,778 @@ rename_retry:
                     return AFPERR_PARAM;
                 }
             }
-            buflen -= cc;
         }
+    }
+    no_sendfile:
+    lseek(sfd, offset, SEEK_SET);
+#endif 
+
+    while (1) {
+        if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
+            if (errno == EINTR)
+                continue;
+            err = AFPERR_PARAM;
+            break;
+        }
+
+        if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0))
+            break;
+    }
+    return err;
+}
 
+/* ----------------------------------
+ * if newname is NULL (from directory.c) we don't want to copy ressource fork.
+ * because we are doing it elsewhere.
+ */
+int copyfile(src, dst, newname, noadouble )
+char   *src, *dst, *newname;
+const int   noadouble;
+{
+    struct adouble     ads, add;
+    int                        len, err = AFP_OK;
+    int                 adflags;
+    struct stat         st;
+    
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end copy_all:");
+    LOG(log_info, logtype_afpd, "begin copyfile:");
 #endif /* DEBUG */
 
-        return AFP_OK;
+    ad_init(&ads, 0); /* OK */
+    ad_init(&add, 0); /* FIXME */
+    adflags = ADFLAGS_DF;
+    if (newname) {
+        adflags |= ADFLAGS_HF;
+    }
+
+    if (ad_open(src , adflags | ADFLAGS_NOHF, O_RDONLY, 0, &ads) < 0) {
+        switch ( errno ) {
+        case ENOENT :
+            return( AFPERR_NOOBJ );
+        case EACCES :
+            return( AFPERR_ACCESS );
+        default :
+            return( AFPERR_PARAM );
+        }
+    }
+    if (ad_open(dst , adflags | noadouble, O_RDWR|O_CREAT|O_EXCL, 0666, &add) < 0) {
+        ad_close( &ads, adflags );
+        if (EEXIST != (err = errno)) {
+            deletefile(NULL, dst, 0);
+        }
+        switch ( err ) {
+        case EEXIST :
+            return AFPERR_EXIST;
+        case ENOENT :
+            return( AFPERR_NOOBJ );
+        case EACCES :
+            return( AFPERR_ACCESS );
+        case EROFS:
+            return AFPERR_VLOCK;
+        default :
+            return( AFPERR_PARAM );
+        }
+    }
+    if (ad_hfileno(&ads) == -1 || AFP_OK == (err = copy_fd(ad_hfileno(&add), ad_hfileno(&ads)))){
+        /* copy the data fork */
+       err = copy_fd(ad_dfileno(&add), ad_dfileno(&ads));
+    }
+
+    /* Now, reopen destination file */
+    err = AFP_OK;
+    if (ad_close( &add, adflags ) <0) {
+       deletefile(NULL, dst, 0);
+        return AFPERR_PARAM;  /* FIXME */
+    } else {
+       ad_init(&add, 0);
+       if (ad_open(dst , adflags | noadouble, O_RDWR, 0666, &add) < 0) {
+           ad_close( &ads, adflags );
+           deletefile(NULL, dst, 0);
+           switch ( err ) {
+           case ENOENT :
+               return( AFPERR_NOOBJ );
+           case EACCES :
+               return( AFPERR_ACCESS );
+           case EROFS:
+               return AFPERR_VLOCK;
+           default :
+               return( AFPERR_PARAM );
+           }
+       }
+    }
+
+    if (newname) {
+        len = strlen( newname );
+        ad_setentrylen( &add, ADEID_NAME, len );
+        memcpy(ad_entry( &add, ADEID_NAME ), newname, len );
     }
 
-    /* XXX: this needs to use ad_open and ad_lock. so, we need to
-     * pass in vol and path */
-    int copyfile(src, dst, newname, noadouble )
-    char       *src, *dst, *newname;
-    const int   noadouble;
-    {
-        struct adouble ad;
-        struct stat         st;
-        char           filebuf[8192];
-        int                    sfd, dfd, len, err = AFP_OK;
-        ssize_t             cc;
+    ad_close( &ads, adflags );
+    ad_flush( &add, adflags );
+    if (ad_close( &add, adflags ) <0) {
+       err = errno;
+    }
+    if (err != AFP_OK) {
+        deletefile(NULL, dst, 0);
+        switch ( err ) {
+        case ENOENT :
+            return( AFPERR_NOOBJ );
+        case EACCES :
+            return( AFPERR_ACCESS );
+        default :
+            return( AFPERR_PARAM );
+        }
+    }
+
+    /* set dest modification date to src date */
+    if (!stat(src, &st)) {
+        struct utimbuf ut;
+
+       ut.actime = ut.modtime = st.st_mtime;
+       utime(dst, &ut);
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin copyfile:");
+    LOG(log_info, logtype_afpd, "end copyfile:");
 #endif /* DEBUG */
 
-        if (newname) {
-            if ((sfd = open( ad_path( src, ADFLAGS_HF ), O_RDONLY, 0 )) < 0 ) {
-                switch ( errno ) {
-                case ENOENT :
-                    break; /* just copy the data fork */
-                case EACCES :
-                    return( AFPERR_ACCESS );
-                default :
-                    return( AFPERR_PARAM );
-                }
-            } else {
-                if (( dfd = open( ad_path( dst, ADFLAGS_HF ), O_WRONLY|O_CREAT,
-                                  ad_mode( ad_path( dst, ADFLAGS_HF ), 0666 ))) < 0 ) {
-                    close( sfd );
-                    switch ( errno ) {
-                    case ENOENT :
-                        return( AFPERR_NOOBJ );
-                    case EACCES :
-                        return( AFPERR_ACCESS );
-                    case EROFS:
-                        return AFPERR_VLOCK;
-                    default :
-                        return( AFPERR_PARAM );
-                    }
-                }
+    return( AFP_OK );
+}
 
-                /* copy the file */
-#ifdef SENDFILE_FLAVOR_LINUX
-                if (fstat(sfd, &st) == 0) {
-                    if ((cc = sendfile(dfd, sfd, NULL, st.st_size)) < 0) {
-                        switch (errno) {
-                        case EDQUOT:
-                        case EFBIG:
-                        case ENOSPC:
-                            err = AFPERR_DFULL;
-                            break;
-                        case EROFS:
-                            err = AFPERR_VLOCK;
-                            break;
-                        default:
-                            err = AFPERR_PARAM;
-                        }
-                    }
-                    goto copyheader_done;
-                }
-#endif /* SENDFILE_FLAVOR_LINUX */
-                while (1) {
-                    if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
-                        if (errno == EINTR)
-                            continue;
-                        err = AFPERR_PARAM;
-                        break;
-                    }
 
-                    if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0))
-                        break;
-                }
+/* -----------------------------------
+   vol: not NULL delete cnid entry. then we are in curdir and file is a only filename
+   checkAttrib:   1 check kFPDeleteInhibitBit (deletfile called by afp_delete)
 
-copyheader_done:
-                close(sfd);
-                close(dfd);
-                if (err < 0) {
-                    unlink(ad_path(dst, ADFLAGS_HF));
-                    return err;
-                }
-            }
-        }
+   when deletefile is called we don't have lock on it, file is closed (for us)
+   untrue if called by renamefile
+   
+   ad_open always try to open file RDWR first and ad_lock takes care of
+   WRITE lock on read only file.
+*/
+int deletefile( vol, file, checkAttrib )
+struct vol      *vol;
+char           *file;
+int         checkAttrib;
+{
+    struct adouble     ad;
+    int                        adflags, err = AFP_OK;
 
-        /* data fork copying */
-        if (( sfd = open( src, O_RDONLY, 0 )) < 0 ) {
-            switch ( errno ) {
-            case ENOENT :
-                return( AFPERR_NOOBJ );
-            case EACCES :
-                return( AFPERR_ACCESS );
-            default :
-                return( AFPERR_PARAM );
-            }
-        }
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "begin deletefile:");
+#endif /* DEBUG */
 
-        if (( dfd = open( dst, O_WRONLY|O_CREAT, ad_mode( dst, 0666 ))) < 0 ) {
-            close( sfd );
-            switch ( errno ) {
-            case ENOENT :
-                return( AFPERR_NOOBJ );
-            case EACCES :
-                return( AFPERR_ACCESS );
+    /* try to open both forks at once */
+    adflags = ADFLAGS_DF|ADFLAGS_HF;
+    while(1) {
+        ad_init(&ad, 0);  /* OK */
+        if ( ad_open( file, adflags, O_RDONLY, 0, &ad ) < 0 ) {
+            switch (errno) {
+            case ENOENT:
+                if (adflags == ADFLAGS_DF)
+                    return AFPERR_NOOBJ;
+                   
+                /* that failed. now try to open just the data fork */
+                adflags = ADFLAGS_DF;
+                continue;
+
+            case EACCES:
+                return AFPERR_ACCESS;
             case EROFS:
                 return AFPERR_VLOCK;
-            default :
+            default:
                 return( AFPERR_PARAM );
             }
         }
+        break; /* from the while */
+    }
+    /*
+     * Does kFPDeleteInhibitBit (bit 8) set?
+     */
+    if (checkAttrib && (adflags & ADFLAGS_HF)) {
+        u_int16_t   bshort;
 
-#ifdef SENDFILE_FLAVOR_LINUX
-        if (fstat(sfd, &st) == 0) {
-            if ((cc = sendfile(dfd, sfd, NULL, st.st_size)) < 0) {
-                switch (errno) {
-                case EDQUOT:
-                case EFBIG:
-                case ENOSPC:
-                    err = AFPERR_DFULL;
-                    break;
-                default:
-                    err = AFPERR_PARAM;
-                }
-            }
-            goto copydata_done;
+        ad_getattr(&ad, &bshort);
+        if ((bshort & htons(ATTRBIT_NODELETE))) {
+            ad_close( &ad, adflags );
+            return(AFPERR_OLOCK);
         }
-#endif /* SENDFILE_FLAVOR_LINUX */
-
-        while (1) {
-            if ((cc = read( sfd, filebuf, sizeof( filebuf ))) < 0) {
-                if (errno == EINTR)
-                    continue;
-
-                err = AFPERR_PARAM;
-                break;
-            }
-
-            if (!cc || ((err = copy_all(dfd, filebuf, cc)) < 0)) {
-                break;
-            }
+    }
+    
+    if ((adflags & ADFLAGS_HF) ) {
+        /* FIXME we have a pb here because we want to know if a file is open 
+         * there's a 'priority inversion' if you can't open the ressource fork RW
+         * you can delete it if it's open because you can't get a write lock.
+         * 
+         * ADLOCK_FILELOCK means the whole ressource fork, not only after the 
+         * metadatas
+         *
+         * FIXME it doesn't work for RFORK open read only and fork open without deny mode
+         */
+        if (ad_tmplock(&ad, ADEID_RFORK, ADLOCK_WR |ADLOCK_FILELOCK, 0, 0, 0) < 0 ) {
+            ad_close( &ad, adflags );
+            return( AFPERR_BUSY );
         }
+    }
 
-copydata_done:
-        close(sfd);
-        close(dfd);
-        if (err < 0) {
-            unlink(ad_path(dst, ADFLAGS_HF));
-            unlink(dst);
-            return err;
+    if (ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0, 0 ) < 0) {
+        err = AFPERR_BUSY;
+    }
+    else if (!(err = netatalk_unlink( ad_path( file, ADFLAGS_HF)) ) &&
+             !(err = netatalk_unlink( file )) ) {
+        cnid_t id;
+        if (vol && (id = cnid_get(vol->v_cdb, curdir->d_did, file, strlen(file)))) 
+        {
+            cnid_delete(vol->v_cdb, id);
         }
 
-        if (newname) {
-            memset(&ad, 0, sizeof(ad));
-            if ( ad_open( dst, noadouble | ADFLAGS_HF, O_RDWR|O_CREAT,
-                          0666, &ad) < 0 ) {
-                switch ( errno ) {
-                case ENOENT :
-                    return noadouble ? AFP_OK : AFPERR_NOOBJ;
-                case EACCES :
-                    return( AFPERR_ACCESS );
-                case EROFS:
-                    return AFPERR_VLOCK;
-                default :
-                    return( AFPERR_PARAM );
-                }
-            }
-
-            len = strlen( newname );
-            ad_setentrylen( &ad, ADEID_NAME, len );
-            memcpy(ad_entry( &ad, ADEID_NAME ), newname, len );
-            ad_flush( &ad, ADFLAGS_HF );
-            ad_close( &ad, ADFLAGS_HF );
-        }
+    }
+    ad_close( &ad, adflags );  /* ad_close removes locks if any */
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end copyfile:");
+    LOG(log_info, logtype_afpd, "end deletefile:");
 #endif /* DEBUG */
 
-        return( AFP_OK );
-    }
-
+    return err;
+}
 
-    int deletefile( file )
-    char               *file;
-    {
-        struct adouble ad;
-        int                    adflags, err = AFP_OK;
-        int                    locktype = ADLOCK_WR;
-        int                    openmode = O_RDWR;
+/* ------------------------------------ */
+/* return a file id */
+int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct stat         *st;
+    struct vol         *vol;
+    struct dir         *dir;
+    char               *upath;
+    int                 len;
+    cnid_t             did, id;
+    u_short            vid;
+    struct path         *s_path;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin deletefile:");
+    LOG(log_info, logtype_afpd, "begin afp_createid:");
 #endif /* DEBUG */
 
-        while(1) {
-            /*
-             * If can't open read/write then try again read-only.  If it's open
-             * read-only, we must do a read lock instead of a write lock.
-             */
-            /* try to open both at once */
-            adflags = ADFLAGS_DF|ADFLAGS_HF;
-            memset(&ad, 0, sizeof(ad));
-            if ( ad_open( file, adflags, openmode, 0, &ad ) < 0 ) {
-                switch (errno) {
-                case ENOENT:
-                    adflags = ADFLAGS_DF;
-                    /* that failed. now try to open just the data fork */
-                    memset(&ad, 0, sizeof(ad));
-                    if ( ad_open( file, adflags, openmode, 0, &ad ) < 0 ) {
-                        switch (errno) {
-                        case ENOENT:
-                            return AFPERR_NOOBJ;
-                        case EACCES:
-                            if(openmode == O_RDWR) {
-                                openmode = O_RDONLY;
-                                locktype = ADLOCK_RD;
-                                continue;
-                            } else {
-                                return AFPERR_ACCESS;
-                            }
-                        case EROFS:
-                            return AFPERR_VLOCK;
-                        default:
-                            return AFPERR_PARAM;
-                        }
-                    }
-                    break;
-
-                case EACCES:
-                    if(openmode == O_RDWR) {
-                        openmode = O_RDONLY;
-                        locktype = ADLOCK_RD;
-                        continue;
-                    } else {
-                        return AFPERR_ACCESS;
-                    }
-                case EROFS:
-                    return AFPERR_VLOCK;
-                default:
-                    return( AFPERR_PARAM );
-                }
-            }
-            break;     /* from the while */
-        }
+    *rbuflen = 0;
 
-        if ((adflags & ADFLAGS_HF) &&
-                (ad_tmplock(&ad, ADEID_RFORK, locktype, 0, 0) < 0 )) {
-            ad_close( &ad, adflags );
-            return( AFPERR_BUSY );
-        }
+    ibuf += 2;
 
-        if (ad_tmplock( &ad, ADEID_DFORK, locktype, 0, 0 ) < 0) {
-            err = AFPERR_BUSY;
-            goto delete_unlock;
-        }
+    memcpy(&vid, ibuf, sizeof(vid));
+    ibuf += sizeof(vid);
 
-        if ( unlink( ad_path( file, ADFLAGS_HF )) < 0 ) {
-            switch ( errno ) {
-            case EPERM:
-            case EACCES :
-                err = AFPERR_ACCESS;
-                goto delete_unlock;
-            case EROFS:
-                err = AFPERR_VLOCK;
-                goto delete_unlock;
-            case ENOENT :
-                break;
-            default :
-                err = AFPERR_PARAM;
-                goto delete_unlock;
-            }
-        }
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM);
+    }
 
-        if ( unlink( file ) < 0 ) {
-            switch ( errno ) {
-            case EPERM:
-            case EACCES :
-                err = AFPERR_ACCESS;
-                break;
-            case EROFS:
-                err = AFPERR_VLOCK;
-                break;
-            case ENOENT :
-                break;
-            default :
-                err = AFPERR_PARAM;
-                break;
-            }
-        }
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
+        return AFPERR_NOOP;
+    }
 
-delete_unlock:
-        if (adflags & ADFLAGS_HF)
-            ad_tmplock(&ad, ADEID_RFORK, ADLOCK_CLR, 0, 0);
-        ad_tmplock(&ad, ADEID_DFORK, ADLOCK_CLR, 0, 0);
-        ad_close( &ad, adflags );
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "end deletefile:");
-#endif /* DEBUG */
+    memcpy(&did, ibuf, sizeof( did ));
+    ibuf += sizeof(did);
 
-        return err;
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
+    if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); /* was AFPERR_PARAM */
+    }
 
-#ifdef CNID_DB
-    /* return a file id */
-    int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct stat         st;
-        struct adouble ad;
-        struct vol             *vol;
-        struct dir             *dir;
-        char           *path, *upath;
-        int                 len;
-        cnid_t         did, id;
-        u_short                vid;
+    if ( path_isadir(s_path) ) {
+        return( AFPERR_BADTYPE );
+    }
+
+    upath = s_path->u_name;
+    switch (s_path->st_errno) {
+        case 0:
+             break; /* success */
+        case EPERM:
+        case EACCES:
+            return AFPERR_ACCESS;
+        case ENOENT:
+            return AFPERR_NOOBJ;
+        default:
+            return AFPERR_PARAM;
+    }
+    st = &s_path->st;
+    if ((id = cnid_lookup(vol->v_cdb, st, did, upath, len = strlen(upath)))) {
+        memcpy(rbuf, &id, sizeof(id));
+        *rbuflen = sizeof(id);
+        return AFPERR_EXISTID;
+    }
+
+    if ((id = get_id(vol, NULL, st, did, upath, len)) != CNID_INVALID) {
+        memcpy(rbuf, &id, sizeof(id));
+        *rbuflen = sizeof(id);
+        return AFP_OK;
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_createid:");
+    LOG(log_info, logtype_afpd, "ending afp_createid...:");
 #endif /* DEBUG */
+    return afp_errno;
+}
 
-        *rbuflen = 0;
-        ibuf += 2;
+static int
+reenumerate_id(const struct vol *vol, char *name, cnid_t did)
+{
+    DIR             *dp;
+    struct dirent   *de;
+    int             ret;
+    struct stat     st;
+    cnid_t         aint;
+    struct adouble  ad;
+       
+
+    if (vol->v_cdb == NULL) {
+       return -1;
+    }
+    if (NULL == ( dp = opendir( name)) ) {
+        return -1;
+    }
+    ret = 0;
+    for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
+        if (NULL == check_dirent(vol, de->d_name))
+            continue;
 
-        memcpy(&vid, ibuf, sizeof(vid));
-        ibuf += sizeof(vid);
+        if ( stat(de->d_name, &st)<0 )
+            continue;
+       
+       /* update or add to cnid */
+        aint = cnid_add(vol->v_cdb, &st, did, de->d_name, strlen(de->d_name), 0); /* ignore errors */
 
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM);
+#if AD_VERSION > AD_VERSION1
+        if (aint != CNID_INVALID && !S_ISDIR(st.st_mode)) {
+            ad_init(&ad, 0);  /* OK */
+            if ( ad_open( de->d_name, ADFLAGS_HF, O_RDWR, 0, &ad ) < 0 ) {
+                continue;
+            }
+            else {
+                ad_setid(&ad,(vol->v_flags & AFPVOL_NODEV)?0:st.st_dev, st.st_ino, aint, did, vol->v_stamp);
+                ad_flush(&ad, ADFLAGS_HF);
+                ad_close(&ad, ADFLAGS_HF);
+           }
         }
+#endif /* AD_VERSION > AD_VERSION1 */
 
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
-
-        memcpy(&did, ibuf, sizeof( did ));
-        ibuf += sizeof(did);
+        ret++;
+    }
+    closedir(dp);
+    return ret;
+}
+
+    
+/* ------------------------------
+   resolve a file id */
+int afp_resolveid(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct vol         *vol;
+    struct dir         *dir;
+    char               *upath;
+    struct path         path;
+    int                 err, buflen, retry=0;
+    cnid_t             id, cnid;
+    u_int16_t          vid, bitmap;
 
-        if (( dir = dirsearch( vol, did )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    static char buffer[12 + MAXPATHLEN + 1];
+    int len = 12 + MAXPATHLEN + 1;
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+#ifdef DEBUG
+    LOG(log_info, logtype_afpd, "begin afp_resolveid:");
+#endif /* DEBUG */
 
-        if ( *path == '\0' ) {
-            return( AFPERR_BADTYPE );
-        }
+    *rbuflen = 0;
+    ibuf += 2;
 
-        upath = mtoupath(vol, path);
-        if (stat(upath, &st) < 0) {
-            switch (errno) {
-            case EPERM:
-            case EACCES:
-                return AFPERR_ACCESS;
-            case ENOENT:
-                return AFPERR_NOOBJ;
-            default:
-                return AFPERR_PARAM;
-            }
-        }
+    memcpy(&vid, ibuf, sizeof(vid));
+    ibuf += sizeof(vid);
 
-        if (id = cnid_lookup(vol->v_db, &st, did, upath, len = strlen(upath))) {
-            memcpy(rbuf, &id, sizeof(id));
-            *rbuflen = sizeof(id);
-            return AFPERR_EXISTID;
-        }
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM);
+    }
 
-#if AD_VERSION > AD_VERSION1
-        memset(&ad, 0, sizeof(ad));
-        if (ad_open( upath, ADFLAGS_HF, O_RDONLY, 0, &ad ) >= 0) {
-            memcpy(&id, ad_entry(&ad, ADEID_DID), sizeof(id));
-            ad_close(&ad, ADFLAGS_HF);
-        }
-#endif /* AD_VERSION > AD_VERSION1 */
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
+        return AFPERR_NOOP;
+    }
 
-        if (id = cnid_add(vol->v_db, &st, did, upath, len, id)) {
-            memcpy(rbuf, &id, sizeof(id));
-            *rbuflen = sizeof(id);
-            return AFP_OK;
-        }
+    memcpy(&id, ibuf, sizeof( id ));
+    ibuf += sizeof(id);
+    cnid = id;
 
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "ending afp_createid...:");
-#endif /* DEBUG */
+retry:
+    if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
+        return AFPERR_NOID; /* was AFPERR_BADID, but help older Macs */
+    }
 
+    if (NULL == ( dir = dirlookup( vol, id )) ) {
+        return AFPERR_NOID; /* idem AFPERR_PARAM */
+    }
+    path.u_name = upath;
+    if (movecwd(vol, dir) < 0) {
         switch (errno) {
-        case EROFS:
-            return AFPERR_VLOCK;
-            break;
-        case EPERM:
         case EACCES:
+        case EPERM:
             return AFPERR_ACCESS;
-            break;
+        case ENOENT:
+            return AFPERR_NOID;
         default:
-            LOG(log_error, logtype_default, "afp_createid: cnid_add: %s", strerror(errno));
             return AFPERR_PARAM;
         }
     }
 
-    /* resolve a file id */
-    int afp_resolveid(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct stat         st;
-        struct vol             *vol;
-        struct dir             *dir;
-        char           *upath;
-        int                 err, buflen;
-        cnid_t         id;
-        u_int16_t              vid, bitmap;
-
-#ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_resolveid:");
-#endif /* DEBUG */
-
-        *rbuflen = 0;
-        ibuf += 2;
-
-        memcpy(&vid, ibuf, sizeof(vid));
-        ibuf += sizeof(vid);
-
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM);
-        }
-
-        memcpy(&id, ibuf, sizeof( id ));
-        ibuf += sizeof(id);
-
-        if ((upath = cnid_resolve(vol->v_db, &id)) == NULL) {
-            return AFPERR_BADID;
+    if ( of_stat(&path) < 0 ) {
+       if ( errno == ENOENT && !retry) {
+           /* cnid db is out of sync, reenumerate the directory and updated ids */
+           reenumerate_id(vol, ".", id);
+           id = cnid;
+           retry = 1;
+           goto retry;
         }
-
-        if (( dir = dirsearch( vol, id )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
-
-        if ((movecwd(vol, dir) < 0) || (stat(upath, &st) < 0)) {
-            switch (errno) {
-            case EACCES:
-            case EPERM:
-                return AFPERR_ACCESS;
-            case ENOENT:
-                return AFPERR_NOID;
-            default:
-                return AFPERR_PARAM;
-            }
+        switch (errno) {
+        case EACCES:
+        case EPERM:
+            return AFPERR_ACCESS;
+        case ENOENT:
+            return AFPERR_NOID;
+        default:
+            return AFPERR_PARAM;
         }
+    }
 
-        /* directories are bad */
-        if (S_ISDIR(st.st_mode))
-            return AFPERR_BADTYPE;
-
-        memcpy(&bitmap, ibuf, sizeof(bitmap));
-        bitmap = ntohs( bitmap );
-
-        if ((err = getfilparams(vol, bitmap, utompath(vol, upath), curdir, &st,
-                                rbuf + sizeof(bitmap), &buflen)) != AFP_OK)
-            return err;
+    /* directories are bad */
+    if (S_ISDIR(path.st.st_mode))
+        return AFPERR_BADTYPE;
 
-        *rbuflen = buflen + sizeof(bitmap);
-        memcpy(rbuf, ibuf, sizeof(bitmap));
+    memcpy(&bitmap, ibuf, sizeof(bitmap));
+    bitmap = ntohs( bitmap );
+    if (NULL == (path.m_name = utompath(vol, upath, cnid, utf8_encoding()))) {
+        return AFPERR_NOID;
+    }
+    if (AFP_OK != (err = getfilparams(vol, bitmap, &path , curdir, 
+                            rbuf + sizeof(bitmap), &buflen))) {
+        return err;
+    }
+    *rbuflen = buflen + sizeof(bitmap);
+    memcpy(rbuf, ibuf, sizeof(bitmap));
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end afp_resolveid:");
+    LOG(log_info, logtype_afpd, "end afp_resolveid:");
 #endif /* DEBUG */
 
-        return AFP_OK;
-    }
+    return AFP_OK;
+}
 
-    int afp_deleteid(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct stat         st;
-        struct vol             *vol;
-        struct dir             *dir;
-        char                *upath;
-        int                 err;
-        cnid_t         id;
-        u_short                vid;
+/* ------------------------------ */
+int afp_deleteid(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct stat         st;
+    struct vol         *vol;
+    struct dir         *dir;
+    char                *upath;
+    int                 err;
+    cnid_t             id;
+    cnid_t             fileid;
+    u_short            vid;
+    static char buffer[12 + MAXPATHLEN + 1];
+    int len = 12 + MAXPATHLEN + 1;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_deleteid:");
+    LOG(log_info, logtype_afpd, "begin afp_deleteid:");
 #endif /* DEBUG */
 
-        *rbuflen = 0;
-        ibuf += 2;
+    *rbuflen = 0;
+    ibuf += 2;
 
-        memcpy(&vid, ibuf, sizeof(vid));
-        ibuf += sizeof(vid);
+    memcpy(&vid, ibuf, sizeof(vid));
+    ibuf += sizeof(vid);
 
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM);
-        }
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM);
+    }
 
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
+        return AFPERR_NOOP;
+    }
 
-        memcpy(&id, ibuf, sizeof( id ));
-        ibuf += sizeof(id);
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-        if ((upath = cnid_resolve(vol->v_db, &id)) == NULL) {
-            return AFPERR_NOID;
-        }
+    memcpy(&id, ibuf, sizeof( id ));
+    ibuf += sizeof(id);
+    fileid = id;
 
-        if (( dir = dirsearch( vol, id )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
+        return AFPERR_NOID;
+    }
 
-        err = AFP_OK;
-        if ((movecwd(vol, dir) < 0) || (stat(upath, &st) < 0)) {
-            switch (errno) {
-            case EACCES:
-            case EPERM:
-                return AFPERR_ACCESS;
-            case ENOENT:
-                /* still try to delete the id */
-                err = AFPERR_NOOBJ;
-                break;
-            default:
-                return AFPERR_PARAM;
-            }
-        }
+    if (NULL == ( dir = dirlookup( vol, id )) ) {
+        return( AFPERR_PARAM );
+    }
 
-        /* directories are bad */
-        if (S_ISDIR(st.st_mode))
-            return AFPERR_BADTYPE;
+    err = AFP_OK;
+    if ((movecwd(vol, dir) < 0) || (stat(upath, &st) < 0)) {
+        switch (errno) {
+        case EACCES:
+        case EPERM:
+            return AFPERR_ACCESS;
+        case ENOENT:
+            /* still try to delete the id */
+            err = AFPERR_NOOBJ;
+            break;
+        default:
+            return AFPERR_PARAM;
+        }
+    }
+    else if (S_ISDIR(st.st_mode)) /* directories are bad */
+        return AFPERR_BADTYPE;
 
-        if (cnid_delete(vol->v_db, id)) {
-            switch (errno) {
-            case EROFS:
-                return AFPERR_VLOCK;
-            case EPERM:
-            case EACCES:
-                return AFPERR_ACCESS;
-            default:
-                return AFPERR_PARAM;
-            }
+    if (cnid_delete(vol->v_cdb, fileid)) {
+        switch (errno) {
+        case EROFS:
+            return AFPERR_VLOCK;
+        case EPERM:
+        case EACCES:
+            return AFPERR_ACCESS;
+        default:
+            return AFPERR_PARAM;
         }
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "end afp_deleteid:");
+    LOG(log_info, logtype_afpd, "end afp_deleteid:");
 #endif /* DEBUG */
 
-        return err;
-    }
-#endif /* CNID_DB */
+    return err;
+}
 
 #define APPLETEMP ".AppleTempXXXXXX"
 
-    int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
-    AFPObj      *obj;
-    char       *ibuf, *rbuf;
-    int                ibuflen, *rbuflen;
-    {
-        struct stat         srcst, destst;
-        struct vol             *vol;
-        struct dir             *dir, *sdir;
-        char           *spath, temp[17], *path, *p;
-        char                *supath, *upath;
-        int                 err;
-#ifdef CNID_DB
-        int                 slen, dlen;
-#endif /* CNID_DB */
-        u_int32_t              sid, did;
-        u_int16_t              vid;
+int afp_exchangefiles(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    struct stat         srcst, destst;
+    struct vol         *vol;
+    struct dir         *dir, *sdir;
+    char               *spath, temp[17], *p;
+    char                *supath, *upath;
+    struct path         *path;
+    int                 err;
+    struct adouble     ads;
+    struct adouble     add;
+    struct adouble     *adsp;
+    struct adouble     *addp;
+    struct ofork       *s_of;
+    struct ofork       *d_of;
+    int                 crossdev;
+    
+    int                 slen, dlen;
+    u_int32_t          sid, did;
+    u_int16_t          vid;
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "begin afp_exchangefiles:");
+    LOG(log_info, logtype_afpd, "begin afp_exchangefiles:");
 #endif /* DEBUG */
 
-        *rbuflen = 0;
-        ibuf += 2;
-
-        memcpy(&vid, ibuf, sizeof(vid));
-        ibuf += sizeof(vid);
+    *rbuflen = 0;
+    ibuf += 2;
 
-        if (( vol = getvolbyvid( vid )) == NULL ) {
-            return( AFPERR_PARAM);
-        }
-
-        if (vol->v_flags & AFPVOL_RO)
-            return AFPERR_VLOCK;
+    memcpy(&vid, ibuf, sizeof(vid));
+    ibuf += sizeof(vid);
 
-        /* source and destination dids */
-        memcpy(&sid, ibuf, sizeof(sid));
-        ibuf += sizeof(sid);
-        memcpy(&did, ibuf, sizeof(did));
-        ibuf += sizeof(did);
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
+        return( AFPERR_PARAM);
+    }
 
-        /* source file */
-        if ((dir = dirsearch( vol, sid )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    if (vol->v_flags & AFPVOL_RO)
+        return AFPERR_VLOCK;
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    /* source and destination dids */
+    memcpy(&sid, ibuf, sizeof(sid));
+    ibuf += sizeof(sid);
+    memcpy(&did, ibuf, sizeof(did));
+    ibuf += sizeof(did);
 
-        if ( *path == '\0' ) {
-            return( AFPERR_BADTYPE );
-        }
+    /* source file */
+    if (NULL == (dir = dirlookup( vol, sid )) ) {
+        return afp_errno; /* was AFPERR_PARAM */
+    }
 
-        upath = mtoupath(vol, path);
-        if (stat(upath, &srcst) < 0) {
-            switch (errno) {
-            case ENOENT:
-                return AFPERR_NOID;
-            case EPERM:
-            case EACCES:
-                return AFPERR_ACCESS;
-            default:
-                return AFPERR_PARAM;
-            }
-        }
+    if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
+    }
 
-        /* save some stuff */
-        sdir = curdir;
-        spath = obj->oldtmp;
-        supath = obj->newtmp;
-        strcpy(spath, path);
-        strcpy(supath, upath); /* this is for the cnid changing */
-        p = ctoupath( vol, sdir, spath);
-
-        /* look for the source cnid. if it doesn't exist, don't worry about
-         * it. */
-#ifdef CNID_DB
-        sid = cnid_lookup(vol->v_db, &srcst, sdir->d_did, supath,
-                          slen = strlen(supath));
-#endif /* CNID_DB */
-
-        if (( dir = dirsearch( vol, did )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    if ( path_isadir(path) ) {
+        return( AFPERR_BADTYPE );   /* it's a dir */
+    }
 
-        if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-            return( AFPERR_PARAM );
-        }
+    upath = path->u_name;
+    switch (path->st_errno) {
+        case 0:
+             break;
+        case ENOENT:
+            return AFPERR_NOID;
+        case EPERM:
+        case EACCES:
+            return AFPERR_ACCESS;
+        default:
+            return AFPERR_PARAM;
+    }
+    ad_init(&ads, vol->v_adouble);
+    adsp = &ads;
+    if ((s_of = of_findname(path))) {
+            /* reuse struct adouble so it won't break locks */
+            adsp = s_of->of_ad;
+    }
+    memcpy(&srcst, &path->st, sizeof(struct stat));
+    /* save some stuff */
+    sdir = curdir;
+    spath = obj->oldtmp;
+    supath = obj->newtmp;
+    strcpy(spath, path->m_name);
+    strcpy(supath, upath); /* this is for the cnid changing */
+    p = absupath( vol, sdir, upath);
+    if (!p) {
+        /* pathname too long */
+        return AFPERR_PARAM ;
+    }
 
-        if ( *path == '\0' ) {
-            return( AFPERR_BADTYPE );
-        }
+    /* look for the source cnid. if it doesn't exist, don't worry about
+     * it. */
+    sid = cnid_lookup(vol->v_cdb, &srcst, sdir->d_did, supath,slen = strlen(supath));
 
-        /* FPExchangeFiles is the only call that can return the SameObj
-         * error */
-        if ((curdir == sdir) && strcmp(spath, path) == 0)
-            return AFPERR_SAMEOBJ;
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
+        return afp_errno; /* was AFPERR_PARAM */
+    }
 
-        upath = mtoupath(vol, path);
-        if (stat(upath, &destst) < 0) {
-            switch (errno) {
-            case ENOENT:
-                return AFPERR_NOID;
-            case EPERM:
-            case EACCES:
-                return AFPERR_ACCESS;
-            default:
-                return AFPERR_PARAM;
-            }
-        }
+    if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
+    }
 
-#ifdef CNID_DB
-        /* look for destination id. */
-        did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath,
-                          dlen = strlen(upath));
-#endif /* CNID_DB */
-
-        /* construct a temp name.
-         * NOTE: the temp file will be in the dest file's directory. it
-         * will also be inaccessible from AFP. */
-        memcpy(temp, APPLETEMP, sizeof(APPLETEMP));
-        if (!mktemp(temp))
-            return AFPERR_MISC;
-
-        /* now, quickly rename the file. we error if we can't. */
-        if ((err = renamefile(p, temp, temp, vol_noadouble(vol))) < 0)
-            goto err_exchangefile;
-        of_rename(vol, sdir, spath, curdir, temp);
-
-        /* rename destination to source */
-        if ((err = renamefile(path, p, spath, vol_noadouble(vol))) < 0)
-            goto err_src_to_tmp;
-        of_rename(vol, curdir, path, sdir, spath);
-
-        /* rename temp to destination */
-        if ((err = renamefile(temp, upath, path, vol_noadouble(vol))) < 0)
-            goto err_dest_to_src;
-        of_rename(vol, curdir, temp, curdir, path);
-
-#ifdef CNID_DB
-        /* id's need switching. src -> dest and dest -> src. */
-        if (sid && (cnid_update(vol->v_db, sid, &destst, curdir->d_did,
-                                upath, dlen) < 0)) {
-            switch (errno) {
-            case EPERM:
-            case EACCES:
-                err = AFPERR_ACCESS;
-                break;
-            default:
-                err = AFPERR_PARAM;
-            }
-            goto err_temp_to_dest;
-        }
+    if ( path_isadir(path) ) {
+        return( AFPERR_BADTYPE );
+    }
 
-        if (did && (cnid_update(vol->v_db, did, &srcst, sdir->d_did,
-                                supath, slen) < 0)) {
-            switch (errno) {
-            case EPERM:
-            case EACCES:
-                err = AFPERR_ACCESS;
-                break;
-            default:
-                err = AFPERR_PARAM;
-            }
+    /* FPExchangeFiles is the only call that can return the SameObj
+     * error */
+    if ((curdir == sdir) && strcmp(spath, path->m_name) == 0)
+        return AFPERR_SAMEOBJ;
 
-            if (sid)
-                cnid_update(vol->v_db, sid, &srcst, sdir->d_did, supath, slen);
-            goto err_temp_to_dest;
+    switch (path->st_errno) {
+        case 0:
+             break;
+        case ENOENT:
+            return AFPERR_NOID;
+        case EPERM:
+        case EACCES:
+            return AFPERR_ACCESS;
+        default:
+            return AFPERR_PARAM;
+    }
+    ad_init(&add, vol->v_adouble);
+    addp = &add;
+    if ((d_of = of_findname( path))) {
+            /* reuse struct adouble so it won't break locks */
+            addp = d_of->of_ad;
+    }
+    memcpy(&destst, &path->st, sizeof(struct stat));
+
+    /* they are not on the same device and at least one is open
+    */
+    crossdev = (srcst.st_dev != destst.st_dev);
+    if ((d_of || s_of)  && crossdev)
+        return AFPERR_MISC;
+    
+    upath = path->u_name;
+    /* look for destination id. */
+    did = cnid_lookup(vol->v_cdb, &destst, curdir->d_did, upath, dlen = strlen(upath));
+
+    /* construct a temp name.
+     * NOTE: the temp file will be in the dest file's directory. it
+     * will also be inaccessible from AFP. */
+    memcpy(temp, APPLETEMP, sizeof(APPLETEMP));
+    if (!mktemp(temp))
+        return AFPERR_MISC;
+
+    /* now, quickly rename the file. we error if we can't. */
+    if ((err = renamefile(p, temp, temp, vol_noadouble(vol), adsp)) < 0)
+        goto err_exchangefile;
+    of_rename(vol, s_of, sdir, spath, curdir, temp);
+
+    /* rename destination to source */
+    if ((err = renamefile(upath, p, spath, vol_noadouble(vol), addp)) < 0)
+        goto err_src_to_tmp;
+    of_rename(vol, d_of, curdir, path->m_name, sdir, spath);
+
+    /* rename temp to destination */
+    if ((err = renamefile(temp, upath, path->m_name, vol_noadouble(vol), adsp)) < 0)
+        goto err_dest_to_src;
+    of_rename(vol, s_of, curdir, temp, curdir, path->m_name);
+
+    /* id's need switching. src -> dest and dest -> src. 
+     * we need to re-stat() if it was a cross device copy.
+    */
+    if (sid) {
+       cnid_delete(vol->v_cdb, sid);
+    }
+    if (did) {
+       cnid_delete(vol->v_cdb, did);
+    }
+    if ((did && ( (crossdev && stat( upath, &srcst) < 0) || 
+                cnid_update(vol->v_cdb, did, &srcst, curdir->d_did,upath, dlen) < 0))
+       ||
+       (sid && ( (crossdev && stat(p, &destst) < 0) ||
+                cnid_update(vol->v_cdb, sid, &destst, sdir->d_did,supath, slen) < 0))
+    ) {
+        switch (errno) {
+        case EPERM:
+        case EACCES:
+            err = AFPERR_ACCESS;
+            break;
+        default:
+            err = AFPERR_PARAM;
         }
-#endif /* CNID_DB */
+        goto err_temp_to_dest;
+    }
 
 #ifdef DEBUG
-        LOG(log_info, logtype_default, "ending afp_exchangefiles:");
+    LOG(log_info, logtype_afpd, "ending afp_exchangefiles:");
 #endif /* DEBUG */
 
-        return AFP_OK;
+    return AFP_OK;
 
 
-        /* all this stuff is so that we can unwind a failed operation
-         * properly. */
+    /* all this stuff is so that we can unwind a failed operation
+     * properly. */
 err_temp_to_dest:
-        /* rename dest to temp */
-        renamefile(upath, temp, temp, vol_noadouble(vol));
-        of_rename(vol, curdir, upath, curdir, temp);
+    /* rename dest to temp */
+    renamefile(upath, temp, temp, vol_noadouble(vol), adsp);
+    of_rename(vol, s_of, curdir, upath, curdir, temp);
 
 err_dest_to_src:
-        /* rename source back to dest */
-        renamefile(p, upath, path, vol_noadouble(vol));
-        of_rename(vol, sdir, spath, curdir, path);
+    /* rename source back to dest */
+    renamefile(p, upath, path->m_name, vol_noadouble(vol), addp);
+    of_rename(vol, d_of, sdir, spath, curdir, path->m_name);
 
 err_src_to_tmp:
-        /* rename temp back to source */
-        renamefile(temp, p, spath, vol_noadouble(vol));
-        of_rename(vol, curdir, temp, sdir, spath);
+    /* rename temp back to source */
+    renamefile(temp, p, spath, vol_noadouble(vol), adsp);
+    of_rename(vol, s_of, curdir, temp, sdir, spath);
 
 err_exchangefile:
-        return err;
-    }
+    return err;
+}