2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2000
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;
28 /****************************************************************************
29 Stat cache code used in unix_convert.
30 *****************************************************************************/
34 char *translated_path;
35 size_t translated_path_length;
36 char names[2]; /* This is extended via malloc... */
39 #define INIT_STAT_CACHE_SIZE 512
40 static hash_table stat_cache;
42 /****************************************************************************
43 Add an entry into the stat cache.
44 *****************************************************************************/
46 void stat_cache_add( char *full_orig_name, char *orig_translated_path)
48 stat_cache_entry *scp;
49 stat_cache_entry *found_scp;
50 char *translated_path;
51 size_t translated_path_length;
54 size_t original_path_length;
56 hash_element *hash_elem;
58 if (!lp_stat_cache()) return;
61 * Don't cache trivial valid directory entries.
63 if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
64 (strcmp(full_orig_name, "..") == 0))
68 * If we are in case insentive mode, we don't need to
69 * store names that need no translation - else, it
73 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
77 * Remove any trailing '/' characters from the
81 translated_path = strdup(orig_translated_path);
85 translated_path_length = strlen(translated_path);
87 if(translated_path[translated_path_length-1] == '/') {
88 translated_path[translated_path_length-1] = '\0';
89 translated_path_length--;
92 original_path = strdup(full_orig_name);
96 original_path_length = strlen(original_path);
99 strupper(original_path);
103 * We will only replace namelen characters
105 * StrnCpy always null terminates.
108 smbStrnCpy(orig_name, full_orig_name, namelen);
110 strupper( orig_name );
114 * Check this name doesn't exist in the cache before we
118 if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
119 found_scp = (stat_cache_entry *)(hash_elem->value);
120 if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
121 /* already in hash table */
124 /* hash collision - remove before we re-add */
125 hash_remove(&stat_cache, hash_elem);
132 if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
133 +original_path_length
134 +translated_path_length)) == NULL) {
135 DEBUG(0,("stat_cache_add: Out of memory !\n"));
139 scp->original_path = scp->names;
140 scp->translated_path = scp->names + original_path_length + 1;
141 safe_strcpy(scp->original_path, original_path, original_path_length);
142 safe_strcpy(scp->translated_path, translated_path, translated_path_length);
143 scp->translated_path_length = translated_path_length;
145 hash_insert(&stat_cache, (char *)scp, original_path);
147 DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
151 * Look through the stat cache for an entry
153 * The hash-table's internals will promote it to the top if found.
155 * @param conn A connection struct to do the stat() with.
156 * @param name The path we are attempting to cache, modified by this routine
157 * to be correct as far as the cache can tell us
158 * @param dirpath The path as far as the stat cache told us.
159 * @param start A pointer into name, for where to 'start' in fixing the rest of the name up.
160 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
162 * @return True if we translated (and did a scuccessful stat on) the entire name.
166 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
167 char **start, SMB_STRUCT_STAT *pst)
169 stat_cache_entry *scp;
172 hash_element *hash_elem;
175 if (!lp_stat_cache())
178 namelen = strlen(name);
182 DO_PROFILE_INC(statcache_lookups);
185 * Don't lookup trivial valid directory entries.
187 if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
188 DO_PROFILE_INC(statcache_misses);
192 pstrcpy(chk_name, name);
194 strupper( chk_name );
197 hash_elem = hash_lookup(&stat_cache, chk_name);
198 if(hash_elem == NULL) {
200 * Didn't find it - remove last component for next try.
202 sp = strrchr_m(chk_name, '/');
207 * We reached the end of the name - no match.
209 DO_PROFILE_INC(statcache_misses);
212 if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
213 || (strcmp(chk_name, "..") == 0)) {
214 DO_PROFILE_INC(statcache_misses);
218 scp = (stat_cache_entry *)(hash_elem->value);
219 DO_PROFILE_INC(statcache_hits);
220 if(vfs_stat(conn,scp->translated_path, pst) != 0) {
221 /* Discard this entry - it doesn't exist in the filesystem. */
222 hash_remove(&stat_cache, hash_elem);
225 memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));
227 /* set pointer for 'where to start' on fixing the rest of the name */
228 *start = &name[scp->translated_path_length];
232 pstrcpy(dirpath, scp->translated_path);
233 return (namelen == scp->translated_path_length);
238 /*************************************************************************** **
239 * Initializes or clears the stat cache.
244 * ************************************************************************** **
246 BOOL reset_stat_cache( void )
248 static BOOL initialised;
249 if (!lp_stat_cache()) return True;
252 hash_clear(&stat_cache);
255 initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE,
256 (compare_function)(strcmp));
258 } /* reset_stat_cache */