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_stat_cache
->map_size
> 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
= 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;
169 if (!lp_stat_cache())
172 namelen
= strlen(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'))))
186 if (conn
->case_sensitive
) {
187 chk_name
= SMB_STRDUP(name
);
189 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
194 chk_name
= strdup_upper(name
);
196 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
201 * In some language encodings the length changes
202 * if we uppercase. We need to treat this differently
205 if (strlen(chk_name
) != namelen
)
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
, '/');
223 * Count the number of times we have done this,
224 * we'll need it when reconstructing the string.
231 * We reached the end of the name - no match.
233 DO_PROFILE_INC(statcache_misses
);
237 if((*chk_name
== '\0') || (strcmp(chk_name
, ".") == 0)
238 || (strcmp(chk_name
, "..") == 0)) {
239 DO_PROFILE_INC(statcache_misses
);
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
);
254 SAFE_FREE(data_val
.dptr
);
259 memcpy(name
, translated_path
, MIN(sizeof(pstring
)-1, translated_path_length
));
260 } else if (num_components
== 0) {
261 pstrcpy(name
, translated_path
);
263 sp
= strnrchr_m(name
, '/', num_components
);
265 pstring last_component
;
266 pstrcpy(last_component
, sp
);
267 pstrcpy(name
, translated_path
);
268 pstrcat(name
, last_component
);
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
];
279 pstrcpy(dirpath
, translated_path
);
280 retval
= (namelen
== translated_path_length
) ? True
: False
;
282 SAFE_FREE(data_val
.dptr
);
288 /***************************************************************
289 Compute a hash value based on a string key value.
290 The function returns the bucket index number for the hashed key.
291 JRA. Use a djb-algorithm hash for speed.
292 ***************************************************************/
294 u32
fast_string_hash(TDB_DATA
*key
)
298 for (p
= key
->dptr
; *p
!= '\0'; p
++) {
299 n
= ((n
<< 5) + n
) ^ (u32
)(*p
);
304 /***************************************************************************
305 Initializes or clears the stat cache.
306 **************************************************************************/
308 BOOL
reset_stat_cache( void )
310 if (!lp_stat_cache())
313 if (tdb_stat_cache
) {
314 tdb_close(tdb_stat_cache
);
317 /* Create the in-memory tdb using our custom hash function. */
318 tdb_stat_cache
= tdb_open_ex("statcache", 1031, TDB_INTERNAL
,
319 (O_RDWR
|O_CREAT
), 0644, NULL
, fast_string_hash
);