r23743: Simplify stat_cache_lookup() logic. For the history of this, see
[ira/wip.git] / source3 / smbd / statcache.c
index 8e22d9687b8d2785810feb9d7fd02bcfbf2ccc3c..92c845ce2e8c59fb50c4ac397d2959ab33dd214f 100644 (file)
@@ -52,7 +52,7 @@ void stat_cache_add( const char *full_orig_name, const char *orig_translated_pat
        if (!lp_stat_cache())
                return;
 
-       if (sc_size && (tdb_stat_cache->map_size > sc_size*1024)) {
+       if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
                reset_stat_cache();
        }
 
@@ -64,7 +64,7 @@ void stat_cache_add( const char *full_orig_name, const char *orig_translated_pat
 
        if((*full_orig_name == '\0') || (full_orig_name[0] == '.' && 
                                ((full_orig_name[1] == '\0') ||
-                                (full_orig_name[1] == '.' && full_orig_name[1] == '\0'))))
+                                (full_orig_name[1] == '.' && full_orig_name[2] == '\0'))))
                return;
 
        /*
@@ -131,13 +131,13 @@ void stat_cache_add( const char *full_orig_name, const char *orig_translated_pat
         */
   
        data_val.dsize = translated_path_length + 1;
-       data_val.dptr = translated_path;
+       data_val.dptr = (uint8 *)translated_path;
 
        if (tdb_store_bystring(tdb_stat_cache, original_path, data_val, TDB_REPLACE) != 0) {
                DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n", original_path, translated_path));
        } else {
-               DEBUG(5,("stat_cache_add: Added entry (%x:size%x) %s -> %s\n",
-                       (unsigned int)data_val.dptr, data_val.dsize, original_path, translated_path));
+               DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
+                       (unsigned long)data_val.dptr, (unsigned int)data_val.dsize, original_path, translated_path));
        }
 
        SAFE_FREE(original_path);
@@ -165,6 +165,9 @@ BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
        size_t namelen;
        BOOL sizechanged = False;
        unsigned int num_components = 0;
+       char *translated_path;
+       size_t translated_path_length;
+       TDB_DATA data_val;
 
        if (!lp_stat_cache())
                return False;
@@ -207,82 +210,120 @@ BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
        }
 
        while (1) {
-               TDB_DATA data_val;
                char *sp;
 
                data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
-               if(data_val.dptr == NULL || data_val.dsize == 0) {
-                       DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
+
+               if (data_val.dptr != NULL && data_val.dsize != 0) {
+                       break;
+               }
+
+               DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
+               /*
+                * Didn't find it - remove last component for next try.
+                */
+               if (!(sp = strrchr_m(chk_name, '/'))) {
                        /*
-                        * Didn't find it - remove last component for next try.
+                        * We reached the end of the name - no match.
                         */
-                       sp = strrchr_m(chk_name, '/');
-                       if (sp) {
-                               *sp = '\0';
-                               /*
-                                * Count the number of times we have done this,
-                                * we'll need it when reconstructing the string.
-                                */
-                               if (sizechanged)
-                                       num_components++;
-
-                       } else {
-                               /*
-                                * We reached the end of the name - no match.
-                                */
-                               DO_PROFILE_INC(statcache_misses);
-                               SAFE_FREE(chk_name);
-                               return False;
-                       }
-                       if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
-                                       || (strcmp(chk_name, "..") == 0)) {
-                               DO_PROFILE_INC(statcache_misses);
-                               SAFE_FREE(chk_name);
-                               return False;
-                       }
-               } else {
-                       BOOL retval;
-                       char *translated_path = data_val.dptr;
-                       size_t translated_path_length = data_val.dsize - 1;
-
-                       DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] -> [%s]\n", chk_name, translated_path ));
-                       DO_PROFILE_INC(statcache_hits);
-                       if(SMB_VFS_STAT(conn,translated_path, pst) != 0) {
-                               /* Discard this entry - it doesn't exist in the filesystem.  */
-                               tdb_delete_bystring(tdb_stat_cache, chk_name);
-                               SAFE_FREE(chk_name);
-                               SAFE_FREE(data_val.dptr);
-                               return False;
-                       }
-
-                       if (!sizechanged) {
-                               memcpy(name, translated_path, MIN(sizeof(pstring)-1, translated_path_length));
-                       } else if (num_components == 0) {
-                               pstrcpy(name, translated_path);
-                       } else {
-                               sp = strnrchr_m(name, '/', num_components);
-                               if (sp) {
-                                       pstring last_component;
-                                       pstrcpy(last_component, sp);
-                                       pstrcpy(name, translated_path);
-                                       pstrcat(name, last_component);
-                               } else {
-                                       pstrcpy(name, translated_path);
-                               }
-                       }
-
-                       /* set pointer for 'where to start' on fixing the rest of the name */
-                       *start = &name[translated_path_length];
-                       if(**start == '/')
-                               ++*start;
-
-                       pstrcpy(dirpath, translated_path);
-                       retval = (namelen == translated_path_length) ? True : False;
+                       DO_PROFILE_INC(statcache_misses);
+                       SAFE_FREE(chk_name);
+                       return False;
+               }
+
+               *sp = '\0';
+
+               /*
+                * Count the number of times we have done this, we'll
+                * need it when reconstructing the string.
+                */
+               if (sizechanged)
+                       num_components++;
+
+               if ((*chk_name == '\0')
+                   || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
+                       DO_PROFILE_INC(statcache_misses);
                        SAFE_FREE(chk_name);
-                       SAFE_FREE(data_val.dptr);
-                       return retval;
+                       return False;
+               }
+       }
+
+       translated_path = (char *)data_val.dptr;
+       translated_path_length = data_val.dsize - 1;
+
+       DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
+                 "-> [%s]\n", chk_name, translated_path ));
+       DO_PROFILE_INC(statcache_hits);
+
+       if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
+               /* Discard this entry - it doesn't exist in the filesystem. */
+               tdb_delete_bystring(tdb_stat_cache, chk_name);
+               SAFE_FREE(chk_name);
+               SAFE_FREE(data_val.dptr);
+               return False;
+       }
+
+       if (!sizechanged) {
+               memcpy(name, translated_path,
+                      MIN(sizeof(pstring)-1, translated_path_length));
+       } else if (num_components == 0) {
+               pstrcpy(name, translated_path);
+       } else {
+               char *sp;
+
+               sp = strnrchr_m(name, '/', num_components);
+               if (sp) {
+                       pstring last_component;
+                       pstrcpy(last_component, sp);
+                       pstrcpy(name, translated_path);
+                       pstrcat(name, last_component);
+               } else {
+                       pstrcpy(name, translated_path);
                }
        }
+
+       /* set pointer for 'where to start' on fixing the rest of the name */
+       *start = &name[translated_path_length];
+       if (**start == '/')
+               ++*start;
+
+       pstrcpy(dirpath, translated_path);
+       SAFE_FREE(chk_name);
+       SAFE_FREE(data_val.dptr);
+       return (namelen == translated_path_length);
+}
+
+/***************************************************************************
+ Tell all smbd's to delete an entry.
+**************************************************************************/
+
+void send_stat_cache_delete_message(const char *name)
+{
+#ifdef DEVELOPER
+       message_send_all(smbd_messaging_context(),
+                       MSG_SMB_STAT_CACHE_DELETE,
+                       name,
+                       strlen(name)+1,
+                       NULL);
+#endif
+}
+
+/***************************************************************************
+ Delete an entry.
+**************************************************************************/
+
+void stat_cache_delete(const char *name)
+{
+       char *lname = strdup_upper(name);
+
+       if (!lname) {
+               return;
+       }
+       DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
+                       lname, name ));
+
+       tdb_delete_bystring(tdb_stat_cache, lname);
+       SAFE_FREE(lname);
 }
 
 /***************************************************************
@@ -291,12 +332,12 @@ BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
  JRA. Use a djb-algorithm hash for speed.
 ***************************************************************/
                                                                                                      
-u32 fast_string_hash(TDB_DATA *key)
+unsigned int fast_string_hash(TDB_DATA *key)
 {
-        u32 n = 0;
+        unsigned int n = 0;
         const char *p;
-        for (p = key->dptr; *p != '\0'; p++) {
-                n = ((n << 5) + n) ^ (u32)(*p);
+        for (p = (const char *)key->dptr; *p != '\0'; p++) {
+                n = ((n << 5) + n) ^ (unsigned int)(*p);
         }
         return n;
 }