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 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/>.
24 /****************************************************************************
25 Stat cache code used in unix_convert.
26 *****************************************************************************/
28 static TDB_CONTEXT
*tdb_stat_cache
;
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
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
;
48 size_t original_path_length
;
49 size_t sc_size
= lp_max_stat_cache_size();
54 if (sc_size
&& (tdb_map_size(tdb_stat_cache
) > sc_size
*1024)) {
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'))))
70 * If we are in case insentive mode, we don't need to
71 * store names that need no translation - else, it
75 if(case_sensitive
&& (strcmp(full_orig_name
, orig_translated_path
) == 0))
79 * Remove any trailing '/' characters from the
83 translated_path
= SMB_STRDUP(orig_translated_path
);
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
--;
95 original_path
= SMB_STRDUP(full_orig_name
);
97 original_path
= strdup_upper(full_orig_name
);
100 if (!original_path
) {
101 SAFE_FREE(translated_path
);
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
);
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
));
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
)
165 BOOL sizechanged
= False
;
166 unsigned int num_components
= 0;
167 char *translated_path
;
168 size_t translated_path_length
;
171 if (!lp_stat_cache())
174 namelen
= strlen(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'))))
188 if (conn
->case_sensitive
) {
189 chk_name
= SMB_STRDUP(name
);
191 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
196 chk_name
= strdup_upper(name
);
198 DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
203 * In some language encodings the length changes
204 * if we uppercase. We need to treat this differently
207 if (strlen(chk_name
) != namelen
)
214 data_val
= tdb_fetch_bystring(tdb_stat_cache
, chk_name
);
216 if (data_val
.dptr
!= NULL
&& data_val
.dsize
!= 0) {
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
);
236 * Count the number of times we have done this, we'll
237 * need it when reconstructing the string.
242 if ((*chk_name
== '\0')
243 || ISDOT(chk_name
) || ISDOTDOT(chk_name
)) {
244 DO_PROFILE_INC(statcache_misses
);
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
);
261 SAFE_FREE(data_val
.dptr
);
266 memcpy(name
, translated_path
,
267 MIN(sizeof(pstring
)-1, translated_path_length
));
268 } else if (num_components
== 0) {
269 pstrcpy(name
, translated_path
);
273 sp
= strnrchr_m(name
, '/', num_components
);
275 pstring last_component
;
276 pstrcpy(last_component
, sp
);
277 pstrcpy(name
, translated_path
);
278 pstrcat(name
, last_component
);
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
];
289 pstrcpy(dirpath
, translated_path
);
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
)
302 message_send_all(smbd_messaging_context(),
303 MSG_SMB_STAT_CACHE_DELETE
,
310 /***************************************************************************
312 **************************************************************************/
314 void stat_cache_delete(const char *name
)
316 char *lname
= strdup_upper(name
);
321 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
324 tdb_delete_bystring(tdb_stat_cache
, 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
)
338 for (p
= (const char *)key
->dptr
; *p
!= '\0'; p
++) {
339 n
= ((n
<< 5) + n
) ^ (unsigned int)(*p
);
344 /***************************************************************************
345 Initializes or clears the stat cache.
346 **************************************************************************/
348 BOOL
reset_stat_cache( void )
350 if (!lp_stat_cache())
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
);