#include "includes.h"
extern BOOL case_sensitive;
typedef struct {
char *original_path;
char *translated_path;
size_t translated_path_length;
char names[2];
} stat_cache_entry;
#define INIT_STAT_CACHE_SIZE 512
static hash_table stat_cache;
void stat_cache_add( const char *full_orig_name, const char *orig_translated_path)
{
stat_cache_entry *scp;
stat_cache_entry *found_scp;
char *translated_path;
size_t translated_path_length;
char *original_path;
size_t original_path_length;
hash_element *hash_elem;
if (!lp_stat_cache())
return;
if((*full_orig_name == '\0') || (full_orig_name[0] == '.' &&
((full_orig_name[1] == '\0') ||
(full_orig_name[1] == '.' && full_orig_name[1] == '\0'))))
return;
if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
return;
translated_path = strdup(orig_translated_path);
if (!translated_path)
return;
translated_path_length = strlen(translated_path);
if(translated_path[translated_path_length-1] == '/') {
translated_path[translated_path_length-1] = '\0';
translated_path_length--;
}
if(case_sensitive) {
original_path = strdup(full_orig_name);
} else {
original_path = strdup_upper(full_orig_name);
}
if (!original_path) {
SAFE_FREE(translated_path);
return;
}
original_path_length = strlen(original_path);
if(original_path[original_path_length-1] == '/') {
original_path[original_path_length-1] = '\0';
original_path_length--;
}
if (original_path_length != translated_path_length) {
if (original_path_length < translated_path_length) {
DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n",
original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length));
SAFE_FREE(original_path);
SAFE_FREE(translated_path);
return;
}
original_path[translated_path_length] = '\0';
original_path_length = translated_path_length;
}
if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
found_scp = (stat_cache_entry *)(hash_elem->value);
if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
SAFE_FREE(original_path);
SAFE_FREE(translated_path);
return;
}
hash_remove(&stat_cache, hash_elem);
}
if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
+original_path_length
+translated_path_length)) == NULL) {
DEBUG(0,("stat_cache_add: Out of memory !\n"));
SAFE_FREE(original_path);
SAFE_FREE(translated_path);
return;
}
scp->original_path = scp->names;
scp->translated_path = scp->names + original_path_length + 1;
safe_strcpy(scp->original_path, original_path, original_path_length);
safe_strcpy(scp->translated_path, translated_path, translated_path_length);
scp->translated_path_length = translated_path_length;
hash_insert(&stat_cache, (char *)scp, original_path);
SAFE_FREE(original_path);
SAFE_FREE(translated_path);
DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
}
BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
char **start, SMB_STRUCT_STAT *pst)
{
stat_cache_entry *scp;
char *chk_name;
size_t namelen;
hash_element *hash_elem;
char *sp;
BOOL sizechanged = False;
unsigned int num_components = 0;
if (!lp_stat_cache())
return False;
namelen = strlen(name);
*start = name;
DO_PROFILE_INC(statcache_lookups);
if((*name == '\0') || (name[0] == '.' &&
((name[1] == '\0') ||
(name[1] == '.' && name[1] == '\0'))))
return False;
if (case_sensitive) {
chk_name = strdup(name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
return False;
}
} else {
chk_name = strdup_upper(name);
if (!chk_name) {
DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
return False;
}
if (strlen(chk_name) != namelen)
sizechanged = True;
}
while (1) {
hash_elem = hash_lookup(&stat_cache, chk_name);
if(hash_elem == NULL) {
DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
sp = strrchr_m(chk_name, '/');
if (sp) {
*sp = '\0';
if (sizechanged)
num_components++;
} else {
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 {
scp = (stat_cache_entry *)(hash_elem->value);
DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] -> [%s]\n", chk_name, scp->translated_path ));
DO_PROFILE_INC(statcache_hits);
if(SMB_VFS_STAT(conn,scp->translated_path, pst) != 0) {
hash_remove(&stat_cache, hash_elem);
SAFE_FREE(chk_name);
return False;
}
if (!sizechanged) {
memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));
} else if (num_components == 0) {
pstrcpy(name, scp->translated_path);
} else {
sp = strnrchr_m(name, '/', num_components);
if (sp) {
pstring last_component;
pstrcpy(last_component, sp);
pstrcpy(name, scp->translated_path);
pstrcat(name, last_component);
} else {
pstrcpy(name, scp->translated_path);
}
}
*start = &name[scp->translated_path_length];
if(**start == '/')
++*start;
pstrcpy(dirpath, scp->translated_path);
SAFE_FREE(chk_name);
return (namelen == scp->translated_path_length);
}
}
}
BOOL reset_stat_cache( void )
{
static BOOL initialised;
if (!lp_stat_cache())
return True;
if (initialised) {
hash_clear(&stat_cache);
}
initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE,
(compare_function)(strcmp));
return initialised;
}