1/* 2 Unix SMB/CIFS implementation. 3 stat cache code 4 Copyright (C) Andrew Tridgell 1992-2000 5 Copyright (C) Jeremy Allison 1999-2004 6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21*/ 22 23#include "includes.h" 24 25/**************************************************************************** 26 Stat cache code used in unix_convert. 27*****************************************************************************/ 28 29static TDB_CONTEXT *tdb_stat_cache; 30 31/** 32 * Add an entry into the stat cache. 33 * 34 * @param full_orig_name The original name as specified by the client 35 * @param orig_translated_path The name on our filesystem. 36 * 37 * @note Only the first strlen(orig_translated_path) characters are stored 38 * into the cache. This means that full_orig_name will be internally 39 * truncated. 40 * 41 */ 42 43void stat_cache_add( const char *full_orig_name, const char *orig_translated_path, BOOL case_sensitive) 44{ 45 char *translated_path; 46 size_t translated_path_length; 47 TDB_DATA data_val; 48 char *original_path; 49 size_t original_path_length; 50 51 if (!lp_stat_cache()) 52 return; 53 54 ZERO_STRUCT(data_val); 55 56 /* 57 * Don't cache trivial valid directory entries such as . and .. 58 */ 59 60 if((*full_orig_name == '\0') || (full_orig_name[0] == '.' && 61 ((full_orig_name[1] == '\0') || 62 (full_orig_name[1] == '.' && full_orig_name[1] == '\0')))) 63 return; 64 65 /* 66 * If we are in case insentive mode, we don't need to 67 * store names that need no translation - else, it 68 * would be a waste. 69 */ 70 71 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0)) 72 return; 73 74 /* 75 * Remove any trailing '/' characters from the 76 * translated path. 77 */ 78 79 translated_path = SMB_STRDUP(orig_translated_path); 80 if (!translated_path) 81 return; 82 83 translated_path_length = strlen(translated_path); 84 85 if(translated_path[translated_path_length-1] == '/') { 86 translated_path[translated_path_length-1] = '\0'; 87 translated_path_length--; 88 } 89 90 if(case_sensitive) { 91 original_path = SMB_STRDUP(full_orig_name); 92 } else { 93 original_path = strdup_upper(full_orig_name); 94 } 95 96 if (!original_path) { 97 SAFE_FREE(translated_path); 98 return; 99 } 100 101 original_path_length = strlen(original_path); 102 103 if(original_path[original_path_length-1] == '/') { 104 original_path[original_path_length-1] = '\0'; 105 original_path_length--; 106 } 107 108 if (original_path_length != translated_path_length) { 109 if (original_path_length < translated_path_length) { 110 DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n", 111 original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length)); 112 SAFE_FREE(original_path); 113 SAFE_FREE(translated_path); 114 return; 115 } 116 117 /* we only want to index by the first part of original_path, 118 up to the length of translated_path */ 119 120 original_path[translated_path_length] = '\0'; 121 original_path_length = translated_path_length; 122 } 123 124 /* 125 * New entry or replace old entry. 126 */ 127 128 data_val.dsize = translated_path_length + 1; 129 data_val.dptr = translated_path; 130 131 if (tdb_store_bystring(tdb_stat_cache, original_path, data_val, TDB_REPLACE) != 0) { 132 DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n", original_path, translated_path)); 133 } else { 134 DEBUG(5,("stat_cache_add: Added entry (%x:size%x) %s -> %s\n", 135 (unsigned int)data_val.dptr, data_val.dsize, original_path, translated_path)); 136 } 137 138 SAFE_FREE(original_path); 139 SAFE_FREE(translated_path); 140} 141 142/** 143 * Look through the stat cache for an entry 144 * 145 * @param conn A connection struct to do the stat() with. 146 * @param name The path we are attempting to cache, modified by this routine 147 * to be correct as far as the cache can tell us 148 * @param dirpath The path as far as the stat cache told us. 149 * @param start A pointer into name, for where to 'start' in fixing the rest of the name up. 150 * @param psd A stat buffer, NOT from the cache, but just a side-effect. 151 * 152 * @return True if we translated (and did a scuccessful stat on) the entire name. 153 * 154 */ 155 156BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath, 157 char **start, SMB_STRUCT_STAT *pst) 158{ 159 char *chk_name; 160 size_t namelen; 161 BOOL sizechanged = False; 162 unsigned int num_components = 0; 163 164 if (!lp_stat_cache()) 165 return False; 166 167 namelen = strlen(name); 168 169 *start = name; 170 171 DO_PROFILE_INC(statcache_lookups); 172 173 /* 174 * Don't lookup trivial valid directory entries. 175 */ 176 if((*name == '\0') || (name[0] == '.' && 177 ((name[1] == '\0') || 178 (name[1] == '.' && name[1] == '\0')))) 179 return False; 180 181 if (conn->case_sensitive) { 182 chk_name = SMB_STRDUP(name); 183 if (!chk_name) { 184 DEBUG(0, ("stat_cache_lookup: strdup failed!\n")); 185 return False; 186 } 187 188 } else { 189 chk_name = strdup_upper(name); 190 if (!chk_name) { 191 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n")); 192 return False; 193 } 194 195 /* 196 * In some language encodings the length changes 197 * if we uppercase. We need to treat this differently 198 * below. 199 */ 200 if (strlen(chk_name) != namelen) 201 sizechanged = True; 202 } 203 204 while (1) { 205 TDB_DATA data_val; 206 char *sp; 207 208 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name); 209 if(data_val.dptr == NULL || data_val.dsize == 0) { 210 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name )); 211 /* 212 * Didn't find it - remove last component for next try. 213 */ 214 sp = strrchr_m(chk_name, '/'); 215 if (sp) { 216 *sp = '\0'; 217 /* 218 * Count the number of times we have done this, 219 * we'll need it when reconstructing the string. 220 */ 221 if (sizechanged) 222 num_components++; 223 224 } else { 225 /* 226 * We reached the end of the name - no match. 227 */ 228 DO_PROFILE_INC(statcache_misses); 229 SAFE_FREE(chk_name); 230 return False; 231 } 232 if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0) 233 || (strcmp(chk_name, "..") == 0)) { 234 DO_PROFILE_INC(statcache_misses); 235 SAFE_FREE(chk_name); 236 return False; 237 } 238 } else { 239 BOOL retval; 240 char *translated_path = data_val.dptr; 241 size_t translated_path_length = data_val.dsize - 1; 242 243 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] -> [%s]\n", chk_name, translated_path )); 244 DO_PROFILE_INC(statcache_hits); 245 if(SMB_VFS_STAT(conn,translated_path, pst) != 0) { 246 /* Discard this entry - it doesn't exist in the filesystem. */ 247 tdb_delete_bystring(tdb_stat_cache, chk_name); 248 SAFE_FREE(chk_name); 249 SAFE_FREE(data_val.dptr); 250 return False; 251 } 252 253 if (!sizechanged) { 254 memcpy(name, translated_path, MIN(sizeof(pstring)-1, translated_path_length)); 255 } else if (num_components == 0) { 256 pstrcpy(name, translated_path); 257 } else { 258 sp = strnrchr_m(name, '/', num_components); 259 if (sp) { 260 pstring last_component; 261 pstrcpy(last_component, sp); 262 pstrcpy(name, translated_path); 263 pstrcat(name, last_component); 264 } else { 265 pstrcpy(name, translated_path); 266 } 267 } 268 269 /* set pointer for 'where to start' on fixing the rest of the name */ 270 *start = &name[translated_path_length]; 271 if(**start == '/') 272 ++*start; 273 274 pstrcpy(dirpath, translated_path); 275 retval = (namelen == translated_path_length) ? True : False; 276 SAFE_FREE(chk_name); 277 SAFE_FREE(data_val.dptr); 278 return retval; 279 } 280 } 281} 282 283/*************************************************************** 284 Compute a hash value based on a string key value. 285 The function returns the bucket index number for the hashed key. 286 JRA. Use a djb-algorithm hash for speed. 287***************************************************************/ 288 289u32 fast_string_hash(TDB_DATA *key) 290{ 291 u32 n = 0; 292 const char *p; 293 for (p = key->dptr; *p != '\0'; p++) { 294 n = ((n << 5) + n) ^ (u32)(*p); 295 } 296 return n; 297} 298 299/*************************************************************************** 300 Initializes or clears the stat cache. 301**************************************************************************/ 302 303BOOL reset_stat_cache( void ) 304{ 305 if (!lp_stat_cache()) 306 return True; 307 308 if (tdb_stat_cache) { 309 tdb_close(tdb_stat_cache); 310 } 311 312 /* Create the in-memory tdb using our custom hash function. */ 313 tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL, 314 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash); 315 316 if (!tdb_stat_cache) 317 return False; 318 return True; 319} 320