2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2000
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 extern BOOL case_sensitive;
27 /****************************************************************************
28 Stat cache code used in unix_convert.
29 *****************************************************************************/
33 char *translated_path;
34 size_t translated_path_length;
35 char names[2]; /* This is extended via malloc... */
38 #define INIT_STAT_CACHE_SIZE 512
39 static hash_table stat_cache;
42 * Add an entry into the stat cache.
44 * @param full_orig_name The original name as specified by the client
45 * @param orig_translated_path The name on our filesystem.
47 * @note Only the first strlen(orig_translated_path) characters are stored
48 * into the cache. This means that full_orig_name will be internally
53 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path)
55 stat_cache_entry *scp;
56 stat_cache_entry *found_scp;
57 char *translated_path;
58 size_t translated_path_length;
61 size_t original_path_length;
63 hash_element *hash_elem;
69 * Don't cache trivial valid directory entries.
72 if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
73 (strcmp(full_orig_name, "..") == 0))
77 * If we are in case insentive mode, we don't need to
78 * store names that need no translation - else, it
82 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
86 * Remove any trailing '/' characters from the
90 translated_path = strdup(orig_translated_path);
94 translated_path_length = strlen(translated_path);
96 if(translated_path[translated_path_length-1] == '/') {
97 translated_path[translated_path_length-1] = '\0';
98 translated_path_length--;
101 original_path = strdup(full_orig_name);
102 if (!original_path) {
103 SAFE_FREE(translated_path);
107 original_path_length = strlen(original_path);
109 if(original_path[original_path_length-1] == '/') {
110 original_path[original_path_length-1] = '\0';
111 original_path_length--;
115 strupper_m(original_path);
117 if (original_path_length != translated_path_length) {
118 if (original_path_length < translated_path_length) {
119 DEBUG(0, ("OOPS - tried to store stat cache entry for werid length paths [%s] %u and [%s] %u)!\n",
120 original_path, original_path_length, translated_path, translated_path_length));
121 SAFE_FREE(original_path);
122 SAFE_FREE(translated_path);
126 /* we only want to store the first part of original_path,
127 up to the length of translated_path */
129 original_path[translated_path_length] = '\0';
130 original_path_length = translated_path_length;
134 * Check this name doesn't exist in the cache before we
138 if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
139 found_scp = (stat_cache_entry *)(hash_elem->value);
140 if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
141 /* already in hash table */
142 SAFE_FREE(original_path);
143 SAFE_FREE(translated_path);
146 /* hash collision - remove before we re-add */
147 hash_remove(&stat_cache, hash_elem);
154 if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
155 +original_path_length
156 +translated_path_length)) == NULL) {
157 DEBUG(0,("stat_cache_add: Out of memory !\n"));
158 SAFE_FREE(original_path);
159 SAFE_FREE(translated_path);
163 scp->original_path = scp->names;
164 scp->translated_path = scp->names + original_path_length + 1;
165 safe_strcpy(scp->original_path, original_path, original_path_length);
166 safe_strcpy(scp->translated_path, translated_path, translated_path_length);
167 scp->translated_path_length = translated_path_length;
169 hash_insert(&stat_cache, (char *)scp, original_path);
171 SAFE_FREE(original_path);
172 SAFE_FREE(translated_path);
174 DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
178 * Look through the stat cache for an entry
180 * The hash-table's internals will promote it to the top if found.
182 * @param conn A connection struct to do the stat() with.
183 * @param name The path we are attempting to cache, modified by this routine
184 * to be correct as far as the cache can tell us
185 * @param dirpath The path as far as the stat cache told us.
186 * @param start A pointer into name, for where to 'start' in fixing the rest of the name up.
187 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
189 * @return True if we translated (and did a scuccessful stat on) the entire name.
193 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
194 char **start, SMB_STRUCT_STAT *pst)
196 stat_cache_entry *scp;
199 hash_element *hash_elem;
201 BOOL sizechanged = False;
202 unsigned int num_components = 0;
204 if (!lp_stat_cache())
207 namelen = strlen(name);
211 DO_PROFILE_INC(statcache_lookups);
214 * Don't lookup trivial valid directory entries.
216 if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
217 DO_PROFILE_INC(statcache_misses);
221 pstrcpy(chk_name, name);
223 if(!case_sensitive) {
224 strupper_m( chk_name );
226 * In some language encodings the length changes
227 * if we uppercase. We need to treat this differently
230 if (strlen(chk_name) != namelen)
235 hash_elem = hash_lookup(&stat_cache, chk_name);
236 if(hash_elem == NULL) {
238 * Didn't find it - remove last component for next try.
240 sp = strrchr_m(chk_name, '/');
244 * Count the number of times we have done this,
245 * we'll need it when reconstructing the string.
252 * We reached the end of the name - no match.
254 DO_PROFILE_INC(statcache_misses);
257 if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
258 || (strcmp(chk_name, "..") == 0)) {
259 DO_PROFILE_INC(statcache_misses);
263 scp = (stat_cache_entry *)(hash_elem->value);
264 DO_PROFILE_INC(statcache_hits);
265 if(SMB_VFS_STAT(conn,scp->translated_path, pst) != 0) {
266 /* Discard this entry - it doesn't exist in the filesystem. */
267 hash_remove(&stat_cache, hash_elem);
272 memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));
273 } else if (num_components == 0) {
274 pstrcpy(name, scp->translated_path);
276 sp = strnrchr_m(name, '/', num_components);
278 pstring last_component;
279 pstrcpy(last_component, sp);
280 pstrcpy(name, scp->translated_path);
281 pstrcat(name, last_component);
283 pstrcpy(name, scp->translated_path);
287 /* set pointer for 'where to start' on fixing the rest of the name */
288 *start = &name[scp->translated_path_length];
292 pstrcpy(dirpath, scp->translated_path);
293 return (namelen == scp->translated_path_length);
298 /*************************************************************************** **
299 * Initializes or clears the stat cache.
304 * ************************************************************************** **
306 BOOL reset_stat_cache( void )
308 static BOOL initialised;
309 if (!lp_stat_cache())
313 hash_clear(&stat_cache);
316 initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE,
317 (compare_function)(strcmp));