]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
always test file open modes.
[netatalk.git] / etc / afpd / file.c
index f88976bf51915e0105f1fd241b74614175b9447b..26015e3d2d44f473c31387e00f2cb3384507c6a3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.87 2003-03-09 19:55:34 didg 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,6 +47,7 @@ char *strchr (), *strrchr ();
 #include "file.h"
 #include "filedir.h"
 #include "globals.h"
+#include "unix.h"
 
 /* the format for the finderinfo fields (from IM: Toolbox Essentials):
  * field         bytes        subfield    bytes
@@ -89,9 +79,10 @@ const u_char ufinderi[] = {
 void *get_finderinfo(const char *mpath, struct adouble *adp, void *data)
 {
     struct extmap      *em;
+    void                *ad_finder;
 
-    if (adp) {
-        memcpy(data, ad_entry(adp, ADEID_FINDERI), 32);
+    if (adp && (ad_finder = ad_entry(adp, ADEID_FINDERI))) {
+        memcpy(data, ad_finder, 32);
     }
     else {
         memcpy(data, ufinderi, 32);
@@ -101,14 +92,14 @@ void *get_finderinfo(const char *mpath, struct adouble *adp, void *data)
                && (em = getextmap( mpath ))
     ) {
         memcpy(data, em->em_type, sizeof( em->em_type ));
-        memcpy(data + 4, em->em_creator, sizeof(em->em_creator));
+        memcpy((char *)data + 4, em->em_creator, sizeof(em->em_creator));
     }
     return data;
 }
 
 /* ---------------------
 */
-char *set_name(const struct vol *vol, char *data, char *name, u_int32_t utf8) 
+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;
@@ -123,7 +114,7 @@ char *set_name(const struct vol *vol, char *data, char *name, u_int32_t utf8)
            
             /* global static variable... */
             tp = strdup(name);
-            if (!(u = mtoupath(vol, name, 1)) || !(m = utompath(vol, u, 0))) {
+            if (!(u = mtoupath(vol, name, pid, 1)) || !(m = utompath(vol, u, id, 0))) {
                aint = 0;
             }
             else {
@@ -142,7 +133,7 @@ char *set_name(const struct vol *vol, char *data, char *name, u_int32_t utf8)
         if (aint > 255)  /* FIXME safeguard, anyway if no ascii char it's game over*/
            aint = 255;
 
-        utf8 = htonl(utf8);
+        utf8 = vol->v_mac?htonl(vol->v_mac->kTextEncoding):0;         /* htonl(utf8) */
         memcpy(data, &utf8, sizeof(utf8));
         data += sizeof(utf8);
         
@@ -178,72 +169,64 @@ u_int32_t get_id(struct vol *vol, struct adouble *adp,  const struct stat *st,
 {
 u_int32_t aint = 0;
 
-#ifdef CNID_DB
-
-    aint = cnid_add(vol->v_db, 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
+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 /* CNID_DB */
-
-    if (aint == 0) {
-        /*
-         * 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
-        if ( S_ISDIR(st->st_mode)) {
-            aint = htonl( vol->v_lastdid++ );
-        }
-        else
-        {
-            aint = htonl(( st->st_dev << 16 ) | (st->st_ino & 0x0000ffff));
+#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;
+            }
         }
-#else /* USE_LASTDID */
-        {
-            struct stat        lst;
-            const struct stat *lstp;
-
-            lstp = lstat(upath, &lst) < 0 ? st : &lst;
-            aint = htonl(CNID(lstp, 1));
+#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 /* USE_LASTDID */
+#endif    
     }
     return aint;
 }
@@ -258,10 +241,13 @@ int getmetadata(struct vol *vol,
     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_afpd, "begin getmetadata:");
 #endif /* DEBUG */
@@ -270,6 +256,18 @@ int getmetadata(struct vol *vol,
     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;
@@ -355,11 +353,8 @@ int getmetadata(struct vol *vol,
             break;
 
         case FILPBIT_FNUM :
-            aint = get_id(vol, adp, st, dir->d_did, upath, strlen(upath));
-            if (aint == 0)
-                return afp_errno;
-            memcpy(data, &aint, sizeof( aint ));
-            data += sizeof( aint );
+            memcpy(data, &id, sizeof( id ));
+            data += sizeof( id );
             break;
 
         case FILPBIT_DFLEN :
@@ -460,6 +455,26 @@ int getmetadata(struct vol *vol,
             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 );
+
+            aint = htonl(st->st_mode);
+            memcpy( data, &aint, sizeof( aint ));
+            data += sizeof( aint );
+
+            accessmode( upath, &ma, dir , st);
+
+            *data++ = ma.ma_user;
+            *data++ = ma.ma_world;
+            *data++ = ma.ma_group;
+            *data++ = ma.ma_owner;
+            break;
+
         default :
             return( AFPERR_BITMAP );
         }
@@ -469,12 +484,12 @@ int getmetadata(struct vol *vol,
     if ( l_nameoff ) {
         ashort = htons( data - buf );
         memcpy(l_nameoff, &ashort, sizeof( ashort ));
-        data = set_name(vol, data, path->m_name, 0);
+        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, path->m_name, utf8);
+        data = set_name(vol, data, dir->d_did, path->m_name, id, utf8);
     }
     *buflen = data - buf;
     return (AFP_OK);
@@ -506,7 +521,7 @@ int getfilparams(struct vol *vol,
            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 {
-            memset(&ad, 0, sizeof(ad));
+            ad_init(&ad, vol->v_adouble);
             adp = &ad;
         }
 
@@ -522,8 +537,12 @@ int getfilparams(struct vol *vol,
            */
            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;
                 }
            }
        }
@@ -581,7 +600,7 @@ int         ibuflen, *rbuflen;
     }
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno;
+        return get_afp_errno(AFPERR_PARAM);
     }
 
     if ( *s_path->m_name == '\0' ) {
@@ -596,7 +615,7 @@ int         ibuflen, *rbuflen;
     if ((of = of_findname(s_path))) {
         adp = of->of_ad;
     } else {
-        memset(&ad, 0, sizeof(ad));
+        ad_init(&ad, vol->v_adouble);
         adp = &ad;
     }
     if ( creatf) {
@@ -617,6 +636,10 @@ int                ibuflen, *rbuflen;
     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 :
@@ -627,8 +650,10 @@ int                ibuflen, *rbuflen;
     }
     if ( ad_hfileno( adp ) == -1 ) {
          /* on noadouble volumes, just creating the data fork is ok */
-         if (vol_noadouble(vol))
+         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.
           */
@@ -700,13 +725,17 @@ int               ibuflen, *rbuflen;
     ibuf += sizeof( bitmap );
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno;
+        return get_afp_errno(AFPERR_PARAM);
     }
 
     if (path_isadir(s_path)) {
         return( AFPERR_BADTYPE ); /* it's a directory */
     }
 
+    if ( s_path->st_errno != 0 ) {
+        return( AFPERR_NOOBJ );
+    }
+
     if ((u_long)ibuf & 1 ) {
         ibuf++;
     }
@@ -755,11 +784,11 @@ int setfilparams(struct vol *vol,
     if ((of = of_findname(path))) {
         adp = of->of_ad;
     } else {
-        memset(&ad, 0, sizeof(ad));
+        ad_init(&ad, vol->v_adouble);
         adp = &ad;
     }
 
-    if (check_access(upath, OPENACC_WR ) < 0) {
+    if (!vol_unix_priv(vol) && check_access(upath, OPENACC_WR ) < 0) {
         return AFPERR_ACCESS;
     }
 
@@ -836,6 +865,19 @@ int setfilparams(struct vol *vol,
             buf += 32;
             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
@@ -970,7 +1012,7 @@ struct adouble    *adp;
              * 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.
              */
-            memset(&ad, 0, sizeof(ad));
+            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 )) ) 
@@ -1049,7 +1091,11 @@ u_int32_t   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) {
@@ -1101,7 +1147,7 @@ int               ibuflen, *rbuflen;
     ibuf += sizeof( ddid );
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno;
+        return get_afp_errno(AFPERR_PARAM);
     }
     if ( path_isadir(s_path) ) {
         return( AFPERR_BADTYPE );
@@ -1139,18 +1185,23 @@ int             ibuflen, *rbuflen;
     }
 
     if (( s_path = cname( vol, dir, &ibuf )) == NULL ) {
-        return afp_errno;
+        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);
     }
 
     /* one of the handful of places that knows about the path type */
     if (copy_path_name(newname, ibuf) < 0) {
         return( AFPERR_PARAM );
     }
-
-    if (NULL == (upath = mtoupath(vol, newname, utf8_encoding()))) {
+    /* 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 ) {
@@ -1214,30 +1265,40 @@ 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) {
-        if ((cc = sendfile(dfd, sfd, NULL, st.st_size)) < 0) {
-            switch (errno) {
-            case EINVAL:  /* there's no guarantee that all fs support sendfile */
-                break;
-            case EDQUOT:
-            case EFBIG:
-            case ENOSPC:
-                return AFPERR_DFULL;
-            case EROFS:
-                return AFPERR_VLOCK;
-            default:
-                return AFPERR_PARAM;
+        
+        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 ENOSYS:
+                case EINVAL:  /* there's no guarantee that all fs support sendfile */
+                    goto no_sendfile;
+                case EDQUOT:
+                case EFBIG:
+                case ENOSPC:
+                    return AFPERR_DFULL;
+                case EROFS:
+                    return AFPERR_VLOCK;
+                default:
+                    return AFPERR_PARAM;
+                }
             }
-        }
-        else {
-           return AFP_OK;
         }
     }
-#endif /* SENDFILE_FLAVOR_LINUX */
+    no_sendfile:
+    lseek(sfd, offset, SEEK_SET);
+#endif 
 
     while (1) {
         if ((cc = read(sfd, filebuf, sizeof(filebuf))) < 0) {
@@ -1264,13 +1325,14 @@ const int   noadouble;
     struct adouble     ads, add;
     int                        len, err = AFP_OK;
     int                 adflags;
+    struct stat         st;
     
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin copyfile:");
 #endif /* DEBUG */
 
-    memset(&ads, 0, sizeof(ads));
-    memset(&add, 0, sizeof(add));
+    ad_init(&ads, 0); /* OK */
+    ad_init(&add, 0); /* FIXME */
     adflags = ADFLAGS_DF;
     if (newname) {
         adflags |= ADFLAGS_HF;
@@ -1309,6 +1371,29 @@ const int   noadouble;
        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 );
@@ -1332,6 +1417,14 @@ const int   noadouble;
         }
     }
 
+    /* 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_afpd, "end copyfile:");
 #endif /* DEBUG */
@@ -1365,7 +1458,7 @@ int         checkAttrib;
     /* try to open both forks at once */
     adflags = ADFLAGS_DF|ADFLAGS_HF;
     while(1) {
-        memset(&ad, 0, sizeof(ad));
+        ad_init(&ad, 0);  /* OK */
         if ( ad_open( file, adflags, O_RDONLY, 0, &ad ) < 0 ) {
             switch (errno) {
             case ENOENT:
@@ -1420,13 +1513,11 @@ int         checkAttrib;
     }
     else if (!(err = netatalk_unlink( ad_path( file, ADFLAGS_HF)) ) &&
              !(err = netatalk_unlink( file )) ) {
-#ifdef CNID_DB /* get rid of entry */
         cnid_t id;
-        if (vol && (id = cnid_get(vol->v_db, curdir->d_did, file, strlen(file)))) 
+        if (vol && (id = cnid_get(vol->v_cdb, curdir->d_did, file, strlen(file)))) 
         {
-            cnid_delete(vol->v_db, id);
+            cnid_delete(vol->v_cdb, id);
         }
-#endif /* CNID_DB */
 
     }
     ad_close( &ad, adflags );  /* ad_close removes locks if any */
@@ -1439,7 +1530,6 @@ int         checkAttrib;
 }
 
 /* ------------------------------------ */
-#ifdef CNID_DB
 /* return a file id */
 int afp_createid(obj, ibuf, ibuflen, rbuf, rbuflen )
 AFPObj      *obj;
@@ -1470,7 +1560,7 @@ int               ibuflen, *rbuflen;
         return( AFPERR_PARAM);
     }
 
-    if (vol->v_db == NULL) {
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
         return AFPERR_NOOP;
     }
 
@@ -1481,11 +1571,11 @@ int             ibuflen, *rbuflen;
     ibuf += sizeof(did);
 
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        return( AFPERR_PARAM );
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno; /* was AFPERR_PARAM */
+        return get_afp_errno(AFPERR_NOOBJ); /* was AFPERR_PARAM */
     }
 
     if ( path_isadir(s_path) ) {
@@ -1505,7 +1595,7 @@ int               ibuflen, *rbuflen;
             return AFPERR_PARAM;
     }
     st = &s_path->st;
-    if ((id = cnid_lookup(vol->v_db, st, did, upath, len = strlen(upath)))) {
+    if ((id = cnid_lookup(vol->v_cdb, st, did, upath, len = strlen(upath)))) {
         memcpy(rbuf, &id, sizeof(id));
         *rbuflen = sizeof(id);
         return AFPERR_EXISTID;
@@ -1523,6 +1613,55 @@ int              ibuflen, *rbuflen;
     return afp_errno;
 }
 
+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;
+
+        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 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 */
+
+        ret++;
+    }
+    closedir(dp);
+    return ret;
+}
+
+    
 /* ------------------------------
    resolve a file id */
 int afp_resolveid(obj, ibuf, ibuflen, rbuf, rbuflen )
@@ -1534,8 +1673,8 @@ int               ibuflen, *rbuflen;
     struct dir         *dir;
     char               *upath;
     struct path         path;
-    int                 err, buflen;
-    cnid_t             id;
+    int                 err, buflen, retry=0;
+    cnid_t             id, cnid;
     u_int16_t          vid, bitmap;
 
     static char buffer[12 + MAXPATHLEN + 1];
@@ -1555,14 +1694,16 @@ int             ibuflen, *rbuflen;
         return( AFPERR_PARAM);
     }
 
-    if (vol->v_db == NULL) {
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
         return AFPERR_NOOP;
     }
 
     memcpy(&id, ibuf, sizeof( id ));
     ibuf += sizeof(id);
+    cnid = id;
 
-    if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, len)) ) {
+retry:
+    if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
         return AFPERR_NOID; /* was AFPERR_BADID, but help older Macs */
     }
 
@@ -1570,7 +1711,26 @@ int              ibuflen, *rbuflen;
         return AFPERR_NOID; /* idem AFPERR_PARAM */
     }
     path.u_name = upath;
-    if (movecwd(vol, dir) < 0 || of_stat(&path) < 0) {
+    if (movecwd(vol, dir) < 0) {
+        switch (errno) {
+        case EACCES:
+        case EPERM:
+            return AFPERR_ACCESS;
+        case ENOENT:
+            return AFPERR_NOID;
+        default:
+            return AFPERR_PARAM;
+        }
+    }
+
+    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;
+        }
         switch (errno) {
         case EACCES:
         case EPERM:
@@ -1581,13 +1741,14 @@ int             ibuflen, *rbuflen;
             return AFPERR_PARAM;
         }
     }
+
     /* directories are bad */
     if (S_ISDIR(path.st.st_mode))
         return AFPERR_BADTYPE;
 
     memcpy(&bitmap, ibuf, sizeof(bitmap));
     bitmap = ntohs( bitmap );
-    if (NULL == (path.m_name = utompath(vol, upath, utf8_encoding()))) {
+    if (NULL == (path.m_name = utompath(vol, upath, cnid, utf8_encoding()))) {
         return AFPERR_NOID;
     }
     if (AFP_OK != (err = getfilparams(vol, bitmap, &path , curdir, 
@@ -1635,7 +1796,7 @@ int               ibuflen, *rbuflen;
         return( AFPERR_PARAM);
     }
 
-    if (vol->v_db == NULL) {
+    if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
         return AFPERR_NOOP;
     }
 
@@ -1646,7 +1807,7 @@ int               ibuflen, *rbuflen;
     ibuf += sizeof(id);
     fileid = id;
 
-    if (NULL == (upath = cnid_resolve(vol->v_db, &id, buffer, len)) ) {
+    if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
         return AFPERR_NOID;
     }
 
@@ -1671,7 +1832,7 @@ int               ibuflen, *rbuflen;
     else if (S_ISDIR(st.st_mode)) /* directories are bad */
         return AFPERR_BADTYPE;
 
-    if (cnid_delete(vol->v_db, fileid)) {
+    if (cnid_delete(vol->v_cdb, fileid)) {
         switch (errno) {
         case EROFS:
             return AFPERR_VLOCK;
@@ -1689,7 +1850,6 @@ int               ibuflen, *rbuflen;
 
     return err;
 }
-#endif /* CNID_DB */
 
 #define APPLETEMP ".AppleTempXXXXXX"
 
@@ -1713,9 +1873,7 @@ int               ibuflen, *rbuflen;
     struct ofork       *d_of;
     int                 crossdev;
     
-#ifdef CNID_DB
     int                 slen, dlen;
-#endif /* CNID_DB */
     u_int32_t          sid, did;
     u_int16_t          vid;
 
@@ -1744,11 +1902,11 @@ int             ibuflen, *rbuflen;
 
     /* source file */
     if (NULL == (dir = dirlookup( vol, sid )) ) {
-        return( AFPERR_PARAM );
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno; /* was AFPERR_PARAM */
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     if ( path_isadir(path) ) {
@@ -1767,7 +1925,7 @@ int               ibuflen, *rbuflen;
         default:
             return AFPERR_PARAM;
     }
-    memset(&ads, 0, sizeof(ads));
+    ad_init(&ads, vol->v_adouble);
     adsp = &ads;
     if ((s_of = of_findname(path))) {
             /* reuse struct adouble so it won't break locks */
@@ -1788,17 +1946,14 @@ int             ibuflen, *rbuflen;
 
     /* 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 */
+    sid = cnid_lookup(vol->v_cdb, &srcst, sdir->d_did, supath,slen = strlen(supath));
 
     if (NULL == ( dir = dirlookup( vol, did )) ) {
-        return( AFPERR_PARAM );
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
     if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
-        return( AFPERR_PARAM );
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     if ( path_isadir(path) ) {
@@ -1821,7 +1976,7 @@ int               ibuflen, *rbuflen;
         default:
             return AFPERR_PARAM;
     }
-    memset(&add, 0, sizeof(add));
+    ad_init(&add, vol->v_adouble);
     addp = &add;
     if ((d_of = of_findname( path))) {
             /* reuse struct adouble so it won't break locks */
@@ -1836,11 +1991,8 @@ int              ibuflen, *rbuflen;
         return AFPERR_MISC;
     
     upath = path->u_name;
-#ifdef CNID_DB
     /* look for destination id. */
-    did = cnid_lookup(vol->v_db, &destst, curdir->d_did, upath,
-                      dlen = strlen(upath));
-#endif /* CNID_DB */
+    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
@@ -1864,21 +2016,20 @@ int             ibuflen, *rbuflen;
         goto err_dest_to_src;
     of_rename(vol, s_of, curdir, temp, curdir, path->m_name);
 
-#ifdef CNID_DB
     /* 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_db, sid);
+       cnid_delete(vol->v_cdb, sid);
     }
     if (did) {
-       cnid_delete(vol->v_db, did);
+       cnid_delete(vol->v_cdb, did);
     }
     if ((did && ( (crossdev && stat( upath, &srcst) < 0) || 
-                cnid_update(vol->v_db, did, &srcst, curdir->d_did,upath, dlen) < 0))
+                cnid_update(vol->v_cdb, did, &srcst, curdir->d_did,upath, dlen) < 0))
        ||
        (sid && ( (crossdev && stat(p, &destst) < 0) ||
-                cnid_update(vol->v_db, sid, &destst, sdir->d_did,supath, slen) < 0))
+                cnid_update(vol->v_cdb, sid, &destst, sdir->d_did,supath, slen) < 0))
     ) {
         switch (errno) {
         case EPERM:
@@ -1890,7 +2041,6 @@ int               ibuflen, *rbuflen;
         }
         goto err_temp_to_dest;
     }
-#endif /* CNID_DB */
 
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "ending afp_exchangefiles:");
@@ -1901,9 +2051,7 @@ int               ibuflen, *rbuflen;
 
     /* all this stuff is so that we can unwind a failed operation
      * properly. */
-#ifdef CNID_DB
 err_temp_to_dest:
-#endif
     /* rename dest to temp */
     renamefile(upath, temp, temp, vol_noadouble(vol), adsp);
     of_rename(vol, s_of, curdir, upath, curdir, temp);