2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /****************************************************************************
26 Stat cache code used in unix_convert.
27 *****************************************************************************/
30 * Add an entry into the stat cache.
32 * @param full_orig_name The original name as specified by the client
33 * @param orig_translated_path The name on our filesystem.
35 * @note Only the first strlen(orig_translated_path) characters are stored
36 * into the cache. This means that full_orig_name will be internally
41 void stat_cache_add( const char *full_orig_name
,
42 char *translated_path
,
45 size_t translated_path_length
;
47 size_t original_path_length
;
49 TALLOC_CTX
*ctx
= talloc_tos();
51 if (!lp_stat_cache()) {
56 * Don't cache trivial valid directory entries such as . and ..
59 if ((*full_orig_name
== '\0')
60 || ISDOT(full_orig_name
) || ISDOTDOT(full_orig_name
)) {
65 * If we are in case insentive mode, we don't need to
66 * store names that need no translation - else, it
70 if (case_sensitive
&& (strcmp(full_orig_name
, translated_path
) == 0)) {
75 * Remove any trailing '/' characters from the
79 translated_path_length
= strlen(translated_path
);
81 if(translated_path
[translated_path_length
-1] == '/') {
82 translated_path_length
--;
86 original_path
= talloc_strdup(ctx
,full_orig_name
);
88 original_path
= talloc_strdup_upper(ctx
,full_orig_name
);
95 original_path_length
= strlen(original_path
);
97 if(original_path
[original_path_length
-1] == '/') {
98 original_path
[original_path_length
-1] = '\0';
99 original_path_length
--;
102 if (original_path_length
!= translated_path_length
) {
103 if (original_path_length
< translated_path_length
) {
104 DEBUG(0, ("OOPS - tried to store stat cache entry "
105 "for weird length paths [%s] %lu and [%s] %lu)!\n",
107 (unsigned long)original_path_length
,
109 (unsigned long)translated_path_length
));
110 TALLOC_FREE(original_path
);
114 /* we only want to index by the first part of original_path,
115 up to the length of translated_path */
117 original_path
[translated_path_length
] = '\0';
118 original_path_length
= translated_path_length
;
121 /* Ensure we're null terminated. */
122 saved_char
= translated_path
[translated_path_length
];
123 translated_path
[translated_path_length
] = '\0';
126 * New entry or replace old entry.
130 smbd_memcache(), STAT_CACHE
,
131 data_blob_const(original_path
, original_path_length
),
132 data_blob_const(translated_path
, translated_path_length
+ 1));
134 DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
135 (unsigned long)translated_path
,
136 (unsigned int)translated_path_length
,
140 translated_path
[translated_path_length
] = saved_char
;
141 TALLOC_FREE(original_path
);
145 * Look through the stat cache for an entry
147 * @param conn A connection struct to do the stat() with.
148 * @param posix_paths Whether to lookup using stat() or lstat()
149 * @param name The path we are attempting to cache, modified by this routine
150 * to be correct as far as the cache can tell us. We assume that
151 * it is a talloc'ed string from top of stack, we free it if
153 * @param dirpath The path as far as the stat cache told us. Also talloced
155 * @param start A pointer into name, for where to 'start' in fixing the rest
157 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
159 * @return True if we translated (and did a scuccessful stat on) the entire
164 bool stat_cache_lookup(connection_struct
*conn
,
169 SMB_STRUCT_STAT
*pst
)
173 bool sizechanged
= False
;
174 unsigned int num_components
= 0;
175 char *translated_path
;
176 size_t translated_path_length
;
179 TALLOC_CTX
*ctx
= talloc_tos();
180 struct smb_filename smb_fname
;
184 *pp_start
= *pp_name
;
186 if (!lp_stat_cache()) {
191 namelen
= strlen(name
);
193 DO_PROFILE_INC(statcache_lookups
);
196 * Don't lookup trivial valid directory entries.
198 if ((*name
== '\0') || ISDOT(name
) || ISDOTDOT(name
)) {
202 if (conn
->case_sensitive
) {
203 chk_name
= talloc_strdup(ctx
,name
);
205 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
210 chk_name
= talloc_strdup_upper(ctx
,name
);
212 DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
217 * In some language encodings the length changes
218 * if we uppercase. We need to treat this differently
221 if (strlen(chk_name
) != namelen
) {
229 data_val
= data_blob_null
;
232 smbd_memcache(), STAT_CACHE
,
233 data_blob_const(chk_name
, strlen(chk_name
)),
238 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
241 * Didn't find it - remove last component for next try.
243 if (!(sp
= strrchr_m(chk_name
, '/'))) {
245 * We reached the end of the name - no match.
247 DO_PROFILE_INC(statcache_misses
);
248 TALLOC_FREE(chk_name
);
255 * Count the number of times we have done this, we'll
256 * need it when reconstructing the string.
263 if ((*chk_name
== '\0')
264 || ISDOT(chk_name
) || ISDOTDOT(chk_name
)) {
265 DO_PROFILE_INC(statcache_misses
);
266 TALLOC_FREE(chk_name
);
271 translated_path
= talloc_strdup(ctx
,(char *)data_val
.data
);
272 if (!translated_path
) {
273 smb_panic("talloc failed");
275 translated_path_length
= data_val
.length
- 1;
277 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
278 "-> [%s]\n", chk_name
, translated_path
));
279 DO_PROFILE_INC(statcache_hits
);
281 ZERO_STRUCT(smb_fname
);
282 smb_fname
.base_name
= translated_path
;
285 ret
= SMB_VFS_LSTAT(conn
, &smb_fname
);
287 ret
= SMB_VFS_STAT(conn
, &smb_fname
);
291 /* Discard this entry - it doesn't exist in the filesystem. */
292 memcache_delete(smbd_memcache(), STAT_CACHE
,
293 data_blob_const(chk_name
, strlen(chk_name
)));
294 TALLOC_FREE(chk_name
);
295 TALLOC_FREE(translated_path
);
301 memcpy(*pp_name
, translated_path
,
302 MIN(namelen
, translated_path_length
));
304 if (num_components
== 0) {
305 name
= talloc_strndup(ctx
, translated_path
,
306 translated_path_length
);
310 sp
= strnrchr_m(name
, '/', num_components
);
312 name
= talloc_asprintf(ctx
,"%.*s%s",
313 (int)translated_path_length
,
314 translated_path
, sp
);
316 name
= talloc_strndup(ctx
,
318 translated_path_length
);
323 * TODO: Get us out of here with a real error message
325 smb_panic("talloc failed");
327 TALLOC_FREE(*pp_name
);
332 /* set pointer for 'where to start' on fixing the rest of the name */
333 *pp_start
= &name
[translated_path_length
];
334 if (**pp_start
== '/') {
338 *pp_dirpath
= translated_path
;
339 TALLOC_FREE(chk_name
);
340 return (namelen
== translated_path_length
);
343 /***************************************************************************
344 Tell all smbd's to delete an entry.
345 **************************************************************************/
347 void send_stat_cache_delete_message(const char *name
)
350 message_send_all(smbd_messaging_context(),
351 MSG_SMB_STAT_CACHE_DELETE
,
358 /***************************************************************************
360 **************************************************************************/
362 void stat_cache_delete(const char *name
)
364 char *lname
= talloc_strdup_upper(talloc_tos(), name
);
369 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
372 memcache_delete(smbd_memcache(), STAT_CACHE
,
373 data_blob_const(lname
, talloc_get_size(lname
)-1));
377 /***************************************************************
378 Compute a hash value based on a string key value.
379 The function returns the bucket index number for the hashed key.
380 JRA. Use a djb-algorithm hash for speed.
381 ***************************************************************/
383 unsigned int fast_string_hash(TDB_DATA
*key
)
387 for (p
= (const char *)key
->dptr
; *p
!= '\0'; p
++) {
388 n
= ((n
<< 5) + n
) ^ (unsigned int)(*p
);
393 /***************************************************************************
394 Initializes or clears the stat cache.
395 **************************************************************************/
397 bool reset_stat_cache( void )
399 if (!lp_stat_cache())
402 memcache_flush(smbd_memcache(), STAT_CACHE
);