2 Unix SMB/CIFS implementation.
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.
25 /****************************************************************************
26 Stat cache code used in unix_convert.
27 *****************************************************************************/
29 static TDB_CONTEXT *tdb_stat_cache;
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
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;
49 size_t original_path_length;
50 size_t sc_size = lp_max_stat_cache_size();
55 if (sc_size && (tdb_map_size(tdb_stat_cache) > sc_size*1024)) {
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'))))
71 * If we are in case insentive mode, we don't need to
72 * store names that need no translation - else, it
76 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
80 * Remove any trailing '/' characters from the
84 translated_path = SMB_STRDUP(orig_translated_path);
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--;
96 original_path = SMB_STRDUP(full_orig_name);
98 original_path = strdup_upper(full_orig_name);
101 if (!original_path) {
102 SAFE_FREE(translated_path);
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);
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 = (uint8 *)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));
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)
166 BOOL sizechanged = False;
167 unsigned int num_components = 0;
168 char *translated_path;
169 size_t translated_path_length;
172 if (!lp_stat_cache())
175 namelen = strlen(name);
179 DO_PROFILE_INC(statcache_lookups);
182 * Don't lookup trivial valid directory entries.
184 if((*name == '\0') || (name[0] == '.' &&
185 ((name[1] == '\0') ||
186 (name[1] == '.' && name[1] == '\0'))))
189 if (conn->case_sensitive) {
190 chk_name = SMB_STRDUP(name);
192 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
197 chk_name = strdup_upper(name);
199 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
204 * In some language encodings the length changes
205 * if we uppercase. We need to treat this differently
208 if (strlen(chk_name) != namelen)
215 data_val = tdb_fetch_bystring(tdb_stat_cache, chk_name);
217 if (data_val.dptr != NULL && data_val.dsize != 0) {
221 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n", chk_name ));
223 * Didn't find it - remove last component for next try.
225 if (!(sp = strrchr_m(chk_name, '/'))) {
227 * We reached the end of the name - no match.
229 DO_PROFILE_INC(statcache_misses);
237 * Count the number of times we have done this, we'll
238 * need it when reconstructing the string.
243 if ((*chk_name == '\0')
244 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
245 DO_PROFILE_INC(statcache_misses);
251 translated_path = (char *)data_val.dptr;
252 translated_path_length = data_val.dsize - 1;
254 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
255 "-> [%s]\n", chk_name, translated_path ));
256 DO_PROFILE_INC(statcache_hits);
258 if (SMB_VFS_STAT(conn, translated_path, pst) != 0) {
259 /* Discard this entry - it doesn't exist in the filesystem. */
260 tdb_delete_bystring(tdb_stat_cache, chk_name);
262 SAFE_FREE(data_val.dptr);
267 memcpy(name, translated_path,
268 MIN(sizeof(pstring)-1, translated_path_length));
269 } else if (num_components == 0) {
270 pstrcpy(name, translated_path);
274 sp = strnrchr_m(name, '/', num_components);
276 pstring last_component;
277 pstrcpy(last_component, sp);
278 pstrcpy(name, translated_path);
279 pstrcat(name, last_component);
281 pstrcpy(name, translated_path);
285 /* set pointer for 'where to start' on fixing the rest of the name */
286 *start = &name[translated_path_length];
290 pstrcpy(dirpath, translated_path);
292 SAFE_FREE(data_val.dptr);
293 return (namelen == translated_path_length);
296 /***************************************************************************
297 Tell all smbd's to delete an entry.
298 **************************************************************************/
300 void send_stat_cache_delete_message(const char *name)
303 message_send_all(smbd_messaging_context(),
304 MSG_SMB_STAT_CACHE_DELETE,
311 /***************************************************************************
313 **************************************************************************/
315 void stat_cache_delete(const char *name)
317 char *lname = strdup_upper(name);
322 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
325 tdb_delete_bystring(tdb_stat_cache, lname);
329 /***************************************************************
330 Compute a hash value based on a string key value.
331 The function returns the bucket index number for the hashed key.
332 JRA. Use a djb-algorithm hash for speed.
333 ***************************************************************/
335 unsigned int fast_string_hash(TDB_DATA *key)
339 for (p = (const char *)key->dptr; *p != '\0'; p++) {
340 n = ((n << 5) + n) ^ (unsigned int)(*p);
345 /***************************************************************************
346 Initializes or clears the stat cache.
347 **************************************************************************/
349 BOOL reset_stat_cache( void )
351 if (!lp_stat_cache())
354 if (tdb_stat_cache) {
355 tdb_close(tdb_stat_cache);
358 /* Create the in-memory tdb using our custom hash function. */
359 tdb_stat_cache = tdb_open_ex("statcache", 1031, TDB_INTERNAL,
360 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);