r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / smbd / statcache.c
blob51e8c0417a3fa6ed0452d67d51eabc5d8150002d
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 /****************************************************************************
25 Stat cache code used in unix_convert.
26 *****************************************************************************/
28 static TDB_CONTEXT *tdb_stat_cache;
30 /**
31 * Add an entry into the stat cache.
33 * @param full_orig_name The original name as specified by the client
34 * @param orig_translated_path The name on our filesystem.
36 * @note Only the first strlen(orig_translated_path) characters are stored
37 * into the cache. This means that full_orig_name will be internally
38 * truncated.
42 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path, BOOL case_sensitive)
44 char *translated_path;
45 size_t translated_path_length;
46 TDB_DATA data_val;
47 char *original_path;
48 size_t original_path_length;
49 size_t sc_size = lp_max_stat_cache_size();
51 if (!lp_stat_cache())
52 return;
54 if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
55 reset_stat_cache();
58 ZERO_STRUCT(data_val);
61 * Don't cache trivial valid directory entries such as . and ..
64 if((*full_orig_name == '\0') || (full_orig_name[0] == '.' &&
65 ((full_orig_name[1] == '\0') ||
66 (full_orig_name[1] == '.' && full_orig_name[2] == '\0'))))
67 return;
70 * If we are in case insentive mode, we don't need to
71 * store names that need no translation - else, it
72 * would be a waste.
75 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
76 return;
79 * Remove any trailing '/' characters from the
80 * translated path.
83 translated_path = SMB_STRDUP(orig_translated_path);
84 if (!translated_path)
85 return;
87 translated_path_length = strlen(translated_path);
89 if(translated_path[translated_path_length-1] == '/') {
90 translated_path[translated_path_length-1] = '\0';
91 translated_path_length--;
94 if(case_sensitive) {
95 original_path = SMB_STRDUP(full_orig_name);
96 } else {
97 original_path = strdup_upper(full_orig_name);
100 if (!original_path) {
101 SAFE_FREE(translated_path);
102 return;
105 original_path_length = strlen(original_path);
107 if(original_path[original_path_length-1] == '/') {
108 original_path[original_path_length-1] = '\0';
109 original_path_length--;
112 if (original_path_length != translated_path_length) {
113 if (original_path_length < translated_path_length) {
114 DEBUG(0, ("OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)!\n",
115 original_path, (unsigned long)original_path_length, translated_path, (unsigned long)translated_path_length));
116 SAFE_FREE(original_path);
117 SAFE_FREE(translated_path);
118 return;
121 /* we only want to index by the first part of original_path,
122 up to the length of translated_path */
124 original_path[translated_path_length] = '\0';
125 original_path_length = translated_path_length;
129 * New entry or replace old entry.
132 data_val.dsize = translated_path_length + 1;
133 data_val.dptr = (uint8 *)translated_path;
135 if (tdb_store_bystring(tdb_stat_cache, original_path, data_val, TDB_REPLACE) != 0) {
136 DEBUG(0,("stat_cache_add: Error storing entry %s -> %s\n", original_path, translated_path));
137 } else {
138 DEBUG(5,("stat_cache_add: Added entry (%lx:size%x) %s -> %s\n",
139 (unsigned long)data_val.dptr, (unsigned int)data_val.dsize, original_path, translated_path));
142 SAFE_FREE(original_path);
143 SAFE_FREE(translated_path);
147 * Look through the stat cache for an entry
149 * @param conn A connection struct to do the stat() with.
150 * @param name The path we are attempting to cache, modified by this routine
151 * to be correct as far as the cache can tell us
152 * @param dirpath The path as far as the stat cache told us.
153 * @param start A pointer into name, for where to 'start' in fixing the rest of the name up.
154 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
156 * @return True if we translated (and did a scuccessful stat on) the entire name.
160 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
161 char **start, SMB_STRUCT_STAT *pst)
163 char *chk_name;
164 size_t namelen;
165 BOOL sizechanged = False;
166 unsigned int num_components = 0;
167 char *translated_path;
168 size_t translated_path_length;
169 TDB_DATA data_val;
171 if (!lp_stat_cache())
172 return False;
174 namelen = strlen(name);
176 *start = name;
178 DO_PROFILE_INC(statcache_lookups);
181 * Don't lookup trivial valid directory entries.
183 if((*name == '\0') || (name[0] == '.' &&
184 ((name[1] == '\0') ||
185 (name[1] == '.' && name[1] == '\0'))))
186 return False;
188 if (conn->case_sensitive) {
189 chk_name = SMB_STRDUP(name);
190 if (!chk_name) {
191 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
192 return False;
195 } else {
196 chk_name = strdup_upper(name);
197 if (!chk_name) {
198 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
199 return False;
203 * In some language encodings the length changes
204 * if we uppercase. We need to treat this differently
205 * below.
207 if (strlen(chk_name) != namelen)
208 sizechanged = True;
211 while (1) {
212 char *sp;
214 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
216 if (data_val.dptr != NULL && data_val.dsize != 0) {
217 break;
220 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
222 * Didn't find it - remove last component for next try.
224 if (!(sp = strrchr_m(chk_name, '/'))) {
226 * We reached the end of the name - no match.
228 DO_PROFILE_INC(statcache_misses);
229 SAFE_FREE(chk_name);
230 return False;
233 *sp = '\0';
236 * Count the number of times we have done this, we'll
237 * need it when reconstructing the string.
239 if (sizechanged)
240 num_components++;
242 if ((*chk_name == '\0')
243 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
244 DO_PROFILE_INC(statcache_misses);
245 SAFE_FREE(chk_name);
246 return False;
250 translated_path = (char *)data_val.dptr;
251 translated_path_length = data_val.dsize - 1;
253 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
254 "-> [%s]\n", chk_name, translated_path ));
255 DO_PROFILE_INC(statcache_hits);
257 if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
258 /* Discard this entry - it doesn't exist in the filesystem. */
259 tdb_delete_bystring(tdb_stat_cache, chk_name);
260 SAFE_FREE(chk_name);
261 SAFE_FREE(data_val.dptr);
262 return False;
265 if (!sizechanged) {
266 memcpy(name, translated_path,
267 MIN(sizeof(pstring)-1, translated_path_length));
268 } else if (num_components == 0) {
269 pstrcpy(name, translated_path);
270 } else {
271 char *sp;
273 sp = strnrchr_m(name, '/', num_components);
274 if (sp) {
275 pstring last_component;
276 pstrcpy(last_component, sp);
277 pstrcpy(name, translated_path);
278 pstrcat(name, last_component);
279 } else {
280 pstrcpy(name, translated_path);
284 /* set pointer for 'where to start' on fixing the rest of the name */
285 *start = &name[translated_path_length];
286 if (**start == '/')
287 ++*start;
289 pstrcpy(dirpath, translated_path);
290 SAFE_FREE(chk_name);
291 SAFE_FREE(data_val.dptr);
292 return (namelen == translated_path_length);
295 /***************************************************************************
296 Tell all smbd's to delete an entry.
297 **************************************************************************/
299 void send_stat_cache_delete_message(const char *name)
301 #ifdef DEVELOPER
302 message_send_all(smbd_messaging_context(),
303 MSG_SMB_STAT_CACHE_DELETE,
304 name,
305 strlen(name)+1,
306 NULL);
307 #endif
310 /***************************************************************************
311 Delete an entry.
312 **************************************************************************/
314 void stat_cache_delete(const char *name)
316 char *lname = strdup_upper(name);
318 if (!lname) {
319 return;
321 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
322 lname, name ));
324 tdb_delete_bystring(tdb_stat_cache, lname);
325 SAFE_FREE(lname);
328 /***************************************************************
329 Compute a hash value based on a string key value.
330 The function returns the bucket index number for the hashed key.
331 JRA. Use a djb-algorithm hash for speed.
332 ***************************************************************/
334 unsigned int fast_string_hash(TDB_DATA *key)
336 unsigned int n = 0;
337 const char *p;
338 for (p = (const char *)key->dptr; *p != '\0'; p++) {
339 n = ((n << 5) + n) ^ (unsigned int)(*p);
341 return n;
344 /***************************************************************************
345 Initializes or clears the stat cache.
346 **************************************************************************/
348 BOOL reset_stat_cache( void )
350 if (!lp_stat_cache())
351 return True;
353 if (tdb_stat_cache) {
354 tdb_close(tdb_stat_cache);
357 /* Create the in-memory tdb using our custom hash function. */
358 tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
359 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
361 if (!tdb_stat_cache)
362 return False;
363 return True;