]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/enumerate.c
bugfix: cache handling when inodes are reused.
[netatalk.git] / etc / afpd / enumerate.c
index 3e6eb96edc5ea9e19ddb7d95c015e5e37d390d18..469d1bce2540240c4c34e3286c1063177d623ce8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: enumerate.c,v 1.29 2003-01-07 22:13:39 didg Exp $
+ * $Id: enumerate.c,v 1.38 2003-04-16 06:55:44 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -47,10 +47,8 @@ struct path     *path;
     int                upathlen;
     char        *name;
     char        *upath;
-#ifndef USE_LASTDID
-    struct stat lst, *lstp;
-#endif /* USE_LASTDID */
     struct stat *st;
+    int         deleted;
 
     upath = path->u_name;
     name  = path->m_name;    
@@ -61,42 +59,34 @@ struct path     *path;
         return NULL;
     }
 
-    cdir->d_did = 0;
-
-#ifdef CNID_DB
-    /* add to cnid db */
-    cdir->d_did = cnid_add(vol->v_db, st, dir->d_did, upath,
-                           upathlen, cdir->d_did);
-    /* Fail out if things go bad with CNID. */
-    if (cdir->d_did == CNID_INVALID) {
-        switch (errno) {
-        case CNID_ERR_PARAM:
-            LOG(log_error, logtype_afpd, "adddir: Incorrect parameters passed to cnid_add");
-            return NULL;
-        case CNID_ERR_PATH:
-        case CNID_ERR_DB:
-        case CNID_ERR_MAX:
-            return NULL;
-        }
-    }
-#endif /* CNID_DB */
-
-    if (cdir->d_did == 0) {
-#ifdef USE_LASTDID
-        /* last way of doing DIDs */
-        cdir->d_did = htonl( vol->v_lastdid++ );
-#else /* USE_LASTDID */
-        lstp = lstat(upath, &lst) < 0 ? st : &lst;
-        /* the old way of doing DIDs (default) */
-        cdir->d_did = htonl( CNID(lstp, 0) );
-#endif /* USE_LASTDID */
-    }
+    cdir->d_did = get_id(vol, NULL, st, dir->d_did, upath, upathlen);
+    if (cdir->d_did == 0) 
+        return NULL;
 
     if ((edir = dirinsert( vol, cdir ))) {
+        /* it's not possible with LASTDID
+           for CNID:
+           - someone else have moved the directory.
+           - it's a symlink inside the share.
+           - it's an ID reused, the old directory was deleted but not
+             the cnid record and the server've reused the inode for 
+             the new dir.
+           for HASH (we should get ride of HASH) 
+           - someone else have moved the directory.
+           - it's an ID reused as above
+           - it's a hash duplicate and we are in big trouble
+        */
+        deleted = (edir->d_m_name == NULL);
+        dirfreename(edir);
         edir->d_m_name = cdir->d_m_name;
         edir->d_u_name = cdir->d_u_name;
         free(cdir);
         cdir = edir;
+        if (!cdir->d_parent || (cdir->d_parent == dir && !deleted))
+            return cdir;
+        /* the old was not in the same folder */
+        if (!deleted)
+            dirchildremove(cdir->d_parent, cdir);
     }
 
     /* parent/child directories */
@@ -116,19 +106,20 @@ struct savedir {
     char        *sd_last;
     unsigned int sd_sindex;
 };
-#define SDBUFBRK       1024
+#define SDBUFBRK       2048
 
 static int enumerate_loop(struct dirent *de, char *mname, void *data)
 {
     struct savedir *sd = data; 
     char *start, *end;
-    int  len;
+    int  len,lenm;
     
     end = sd->sd_buf + sd->sd_buflen;
     len = strlen(de->d_name);
     *(sd->sd_last)++ = len;
+    lenm = strlen(mname);
 
-    if ( sd->sd_last + len + 2 > end ) {
+    if ( sd->sd_last + len +lenm + 4 > end ) {
         char *buf;
 
         start = sd->sd_buf;
@@ -146,6 +137,11 @@ static int enumerate_loop(struct dirent *de, char *mname, void *data)
 
     memcpy( sd->sd_last, de->d_name, len + 1 );
     sd->sd_last += len + 1;
+
+    *(sd->sd_last)++ = lenm;
+    memcpy( sd->sd_last, mname, lenm + 1 );
+    sd->sd_last += lenm + 1;
+    
     return 0;
 }
 
@@ -176,9 +172,11 @@ char *check_dirent(const struct vol *vol, char *name)
     /* check for vetoed filenames */
     if (veto_file(vol->v_veto, name))
         return NULL;
+    if (NULL == (m_name = utompath(vol, name, utf8_encoding()))) 
+        return NULL;    
 
     /* now check against too big a file */
-    if (strlen(m_name = utompath(vol, name)) > vol->max_filename)
+    if (strlen(m_name) > vol->max_filename)
         return NULL;
 
     return m_name;
@@ -193,7 +191,7 @@ for_each_dirent(const struct vol *vol, char *name, dir_loop fn, void *data)
     char            *m_name;
     int             ret;
     
-    if (( dp = opendir( name)) == NULL ) {
+    if (NULL == ( dp = opendir( name)) ) {
         return -1;
     }
     ret = 0;
@@ -244,7 +242,7 @@ int     ext;
     memcpy( &vid, ibuf, sizeof( vid ));
     ibuf += sizeof( vid );
 
-    if (( vol = getvolbyvid( vid )) == NULL ) {
+    if (NULL == ( vol = getvolbyvid( vid )) ) {
         *rbuflen = 0;
         return( AFPERR_PARAM );
     }
@@ -252,9 +250,9 @@ int     ext;
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( did );
 
-    if (( dir = dirlookup( vol, did )) == NULL ) {
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
         *rbuflen = 0;
-        return( AFPERR_NODIR );
+        return (afp_errno == AFPERR_NOOBJ)?AFPERR_NODIR:afp_errno;
     }
 
     memcpy( &fbitmap, ibuf, sizeof( fbitmap ));
@@ -309,15 +307,18 @@ int     ext;
     header *=sizeof( u_char );
     
     maxsz = min(maxsz, *rbuflen);
+    o_path = cname( vol, dir, &ibuf );
 
-    if (( o_path = cname( vol, dir, &ibuf )) == NULL) {
-        *rbuflen = 0;
-        return( AFPERR_NODIR );
-    }
+    if (afp_errno == AFPERR_NOOBJ) 
+        afp_errno = AFPERR_NODIR;
 
+    *rbuflen = 0;
+    if (NULL == o_path ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
+    }
     if ( *o_path->m_name != '\0') {
-        *rbuflen = 0;
-        return( AFPERR_BADTYPE );
+        /* it's a file or it's a dir and extendir() was unable to chdir in it */
+        return (path_isadir(o_path))? afp_errno:AFPERR_BADTYPE ;
     }
 
     data = rbuf + 3 * sizeof( u_int16_t );
@@ -330,7 +331,10 @@ int     ext;
      */
     if ( sindex == 1 || curdir->d_did != sd.sd_did || vid != sd.sd_vid ) {
         sd.sd_last = sd.sd_buf;
-        if ( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) {
+        /* if dir was in the cache we don't have the inode */
+        if (( !o_path->st_valid && stat( ".", &o_path->st ) < 0 ) ||
+              (ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) 
+        {
             switch (errno) {
             case EACCES:
                return AFPERR_ACCESS;
@@ -343,19 +347,6 @@ int     ext;
             }
         }
         curdir->ctime  = o_path->st.st_ctime; /* play safe */
-        if ((ret = for_each_dirent(vol, ".", enumerate_loop, (void *)&sd)) < 0) {
-            *rbuflen = 0;
-            switch (errno) {
-            case EACCES:
-               return AFPERR_ACCESS;
-            case ENOTDIR:
-                return AFPERR_BADTYPE;
-            case ENOMEM:
-                return AFPERR_MISC;
-            default:
-                return AFPERR_NODIR;
-            }
-        }
         curdir->offcnt = ret;
         *sd.sd_last = 0;
 
@@ -377,10 +368,11 @@ int     ext;
         len = *(sd.sd_last)++;
         if ( len == 0 ) {
             sd.sd_did = 0;     /* invalidate sd struct to force re-read */
-            *rbuflen = 0;
             return( AFPERR_NOOBJ );
         }
         sd.sd_last += len + 1;
+        len = *(sd.sd_last)++;
+        sd.sd_last += len + 1;
         sd.sd_sindex++;
     }
 
@@ -402,6 +394,8 @@ int     ext;
         if (*sd.sd_last == 0) {
             /* stat() already failed on this one */
             sd.sd_last += len + 1;
+            len = *(sd.sd_last)++;
+            sd.sd_last += len + 1;
             continue;
         }
         s_path.u_name = sd.sd_last;
@@ -417,10 +411,16 @@ int     ext;
              */
             *sd.sd_last = 0;
             sd.sd_last += len + 1;
+            len = *(sd.sd_last)++;
+            sd.sd_last += len + 1;
             curdir->offcnt--;          /* a little lie */
             continue;
         }
 
+        sd.sd_last += len + 1;
+        len = *(sd.sd_last)++;
+        s_path.m_name = sd.sd_last;
+        sd.sd_last += len + 1;
         /*
          * If a fil/dir is not a dir, it's a file. This is slightly
          * inaccurate, since that means /dev/null is a file, /dev/printer
@@ -428,40 +428,31 @@ int     ext;
          */
         if ( S_ISDIR(s_path.st.st_mode)) {
             if ( dbitmap == 0 ) {
-                sd.sd_last += len + 1;
                 continue;
             }
-            dir = curdir->d_child;
-            s_path.m_name = NULL;
-            while (dir) {
-                if ( strcmp( dir->d_u_name, s_path.u_name ) == 0 ) {
-                    break;
-                }
-                dir = (dir == curdir->d_child->d_prev) ? NULL : dir->d_next;
-            }
+            dir = dirsearch_byname(curdir, s_path.u_name);
             if (!dir) {
-                s_path.m_name = utompath(vol, s_path.u_name);
-                if ((dir = adddir( vol, curdir, &s_path)) == NULL) {
-                    *rbuflen = 0;
+                if (s_path.m_name == NULL || (dir = adddir( vol, curdir, &s_path)) == NULL) {
                     return AFPERR_MISC;
                 }
             }
-
-            if (( ret = getdirparams(vol, dbitmap, &s_path, dir,
-                                     data + header , &esz )) != AFP_OK ) {
-                *rbuflen = 0;
+            else {
+                s_path.m_name = NULL;
+            }
+            if (AFP_OK != ( ret = getdirparams(vol, dbitmap, &s_path, dir,
+                                     data + header , &esz ))) {
                 return( ret );
             }
 
         } else {
             if ( fbitmap == 0 ) {
-                sd.sd_last += len + 1;
                 continue;
             }
-            s_path.m_name = utompath(vol, s_path.u_name);
-            if (( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
-                                     data + header , &esz )) != AFP_OK ) {
-                *rbuflen = 0;
+            if (s_path.m_name == NULL ) {
+                return AFPERR_MISC;
+            }
+            if (AFP_OK != ( ret = getfilparams(vol, fbitmap, &s_path, curdir, 
+                                     data + header , &esz )) ) {
                 return( ret );
             }
         }
@@ -480,7 +471,6 @@ int     ext;
          */
         if ( maxsz < sz + esz + header) {
             if (first) { /* maxsz can't hold a single reply */
-                *rbuflen = 0;
                 return AFPERR_PARAM;
             }
             sd.sd_last = start;
@@ -506,11 +496,9 @@ int     ext;
         }
         data += esz;
         actcnt++;
-        sd.sd_last += len + 1;
     }
 
     if ( actcnt == 0 ) {
-        *rbuflen = 0;
         sd.sd_did = 0;         /* invalidate sd struct to force re-read */
         return( AFPERR_NOOBJ );
     }