[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / smbd / statcache.c
blob1a2b7a8237b2f88352faf4c1e664076fb6bcc3ec
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
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.
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.
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.
23 #include "includes.h"
25 /****************************************************************************
26 Stat cache code used in unix_convert.
27 *****************************************************************************/
29 static TDB_CONTEXT *tdb_stat_cache;
31 /**
32 * Add an entry into the stat cache.
34 * @param full_orig_name The original name as specified by the client
35 * @param orig_translated_path The name on our filesystem.
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.
43 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path, BOOL case_sensitive)
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 size_t sc_size = lp_max_stat_cache_size();
52 if (!lp_stat_cache())
53 return;
55 if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
56 reset_stat_cache();
59 ZERO_STRUCT(data_val);
62 * Don't cache trivial valid directory entries such as . and ..
65 if((*full_orig_name == '\0') || (full_orig_name[0] == '.' &&
66 ((full_orig_name[1] == '\0') ||
67 (full_orig_name[1] == '.' && full_orig_name[2] == '\0'))))
68 return;
71 * If we are in case insentive mode, we don't need to
72 * store names that need no translation - else, it
73 * would be a waste.
76 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
77 return;
80 * Remove any trailing '/' characters from the
81 * translated path.
84 translated_path = SMB_STRDUP(orig_translated_path);
85 if (!translated_path)
86 return;
88 translated_path_length = strlen(translated_path);
90 if(translated_path[translated_path_length-1] == '/') {
91 translated_path[translated_path_length-1] = '\0';
92 translated_path_length--;
95 if(case_sensitive) {
96 original_path = SMB_STRDUP(full_orig_name);
97 } else {
98 original_path = strdup_upper(full_orig_name);
101 if (!original_path) {
102 SAFE_FREE(translated_path);
103 return;
106 original_path_length = strlen(original_path);
108 if(original_path[original_path_length-1] == '/') {
109 original_path[original_path_length-1] = '\0';
110 original_path_length--;
113 if (original_path_length != translated_path_length) {
114 if (original_path_length < translated_path_length) {
115 DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n",
116 original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length));
117 SAFE_FREE(original_path);
118 SAFE_FREE(translated_path);
119 return;
122 /* we only want to index by the first part of original_path,
123 up to the length of translated_path */
125 original_path[translated_path_length] = '\0';
126 original_path_length = translated_path_length;
130 * New entry or replace old entry.
133 data_val.dsize = translated_path_length + 1;
134 data_val.dptr = translated_path;
136 if (tdb_store_bystring(tdb_stat_cache, original_path, data_val, TDB_REPLACE) != 0) {
137 DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n", original_path, translated_path));
138 } else {
139 DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
140 (unsigned long)data_val.dptr, (unsigned int)data_val.dsize, original_path, translated_path));
143 SAFE_FREE(original_path);
144 SAFE_FREE(translated_path);
148 * Look through the stat cache for an entry
150 * @param conn A connection struct to do the stat() with.
151 * @param name The path we are attempting to cache, modified by this routine
152 * to be correct as far as the cache can tell us
153 * @param dirpath The path as far as the stat cache told us.
154 * @param start A pointer into name, for where to 'start' in fixing the rest of the name up.
155 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
157 * @return True if we translated (and did a scuccessful stat on) the entire name.
161 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
162 char **start, SMB_STRUCT_STAT *pst)
164 char *chk_name;
165 size_t namelen;
166 BOOL sizechanged = False;
167 unsigned int num_components = 0;
169 if (!lp_stat_cache())
170 return False;
172 namelen = strlen(name);
174 *start = name;
176 DO_PROFILE_INC(statcache_lookups);
179 * Don't lookup trivial valid directory entries.
181 if((*name == '\0') || (name[0] == '.' &&
182 ((name[1] == '\0') ||
183 (name[1] == '.' && name[1] == '\0'))))
184 return False;
186 if (conn->case_sensitive) {
187 chk_name = SMB_STRDUP(name);
188 if (!chk_name) {
189 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
190 return False;
193 } else {
194 chk_name = strdup_upper(name);
195 if (!chk_name) {
196 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
197 return False;
201 * In some language encodings the length changes
202 * if we uppercase. We need to treat this differently
203 * below.
205 if (strlen(chk_name) != namelen)
206 sizechanged = True;
209 while (1) {
210 TDB_DATA data_val;
211 char *sp;
213 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
214 if(data_val.dptr == NULL || data_val.dsize == 0) {
215 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
217 * Didn't find it - remove last component for next try.
219 sp = strrchr_m(chk_name, '/');
220 if (sp) {
221 *sp = '\0';
223 * Count the number of times we have done this,
224 * we'll need it when reconstructing the string.
226 if (sizechanged)
227 num_components++;
229 } else {
231 * We reached the end of the name - no match.
233 DO_PROFILE_INC(statcache_misses);
234 SAFE_FREE(chk_name);
235 return False;
237 if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
238 || (strcmp(chk_name, "..") == 0)) {
239 DO_PROFILE_INC(statcache_misses);
240 SAFE_FREE(chk_name);
241 return False;
243 } else {
244 BOOL retval;
245 char *translated_path = data_val.dptr;
246 size_t translated_path_length = data_val.dsize - 1;
248 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] -> [%s]\n", chk_name, translated_path ));
249 DO_PROFILE_INC(statcache_hits);
250 if(SMB_VFS_STAT(conn,translated_path, pst) != 0) {
251 /* Discard this entry - it doesn't exist in the filesystem. */
252 tdb_delete_bystring(tdb_stat_cache, chk_name);
253 SAFE_FREE(chk_name);
254 SAFE_FREE(data_val.dptr);
255 return False;
258 if (!sizechanged) {
259 memcpy(name, translated_path, MIN(sizeof(pstring)-1, translated_path_length));
260 } else if (num_components == 0) {
261 pstrcpy(name, translated_path);
262 } else {
263 sp = strnrchr_m(name, '/', num_components);
264 if (sp) {
265 pstring last_component;
266 pstrcpy(last_component, sp);
267 pstrcpy(name, translated_path);
268 pstrcat(name, last_component);
269 } else {
270 pstrcpy(name, translated_path);
274 /* set pointer for 'where to start' on fixing the rest of the name */
275 *start = &name[translated_path_length];
276 if(**start == '/')
277 ++*start;
279 pstrcpy(dirpath, translated_path);
280 retval = (namelen == translated_path_length) ? True : False;
281 SAFE_FREE(chk_name);
282 SAFE_FREE(data_val.dptr);
283 return retval;
288 /***************************************************************************
289 Tell all smbd's to delete an entry.
290 **************************************************************************/
292 void send_stat_cache_delete_message(const char *name)
294 #ifdef DEVELOPER
295 message_send_all(conn_tdb_ctx(),
296 MSG_SMB_STAT_CACHE_DELETE,
297 name,
298 strlen(name)+1,
299 True,
300 NULL);
301 #endif
304 /***************************************************************************
305 Delete an entry.
306 **************************************************************************/
308 void stat_cache_delete(const char *name)
310 char *lname = strdup_upper(name);
312 if (!lname) {
313 return;
315 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
316 lname, name ));
318 tdb_delete_bystring(tdb_stat_cache, lname);
319 SAFE_FREE(lname);
322 /***************************************************************
323 Compute a hash value based on a string key value.
324 The function returns the bucket index number for the hashed key.
325 JRA. Use a djb-algorithm hash for speed.
326 ***************************************************************/
328 unsigned int fast_string_hash(TDB_DATA *key)
330 unsigned int n = 0;
331 const char *p;
332 for (p = key->dptr; *p != '\0'; p++) {
333 n = ((n << 5) + n) ^ (unsigned int)(*p);
335 return n;
338 /***************************************************************************
339 Initializes or clears the stat cache.
340 **************************************************************************/
342 BOOL reset_stat_cache( void )
344 if (!lp_stat_cache())
345 return True;
347 if (tdb_stat_cache) {
348 tdb_close(tdb_stat_cache);
351 /* Create the in-memory tdb using our custom hash function. */
352 tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
353 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
355 if (!tdb_stat_cache)
356 return False;
357 return True;