off by one bug in string length; CR 1159
[Samba.git] / source / smbd / statcache.c
blobf4b613428a223803976b2861f90da93e1df36263
1 /*
2 Unix SMB/CIFS implementation.
3 stat cache code
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.
23 #include "includes.h"
25 extern BOOL case_sensitive;
28 /****************************************************************************
29 Stat cache code used in unix_convert.
30 *****************************************************************************/
32 typedef struct {
33 int name_len;
34 char names[2]; /* This is extended via malloc... */
35 } stat_cache_entry;
37 #define INIT_STAT_CACHE_SIZE 512
38 static hash_table stat_cache;
40 /****************************************************************************
41 Add an entry into the stat cache.
42 *****************************************************************************/
44 void stat_cache_add( char *full_orig_name, char *orig_translated_path)
46 stat_cache_entry *scp;
47 stat_cache_entry *found_scp;
48 pstring orig_name;
49 pstring translated_path;
50 int namelen;
51 hash_element *hash_elem;
53 if (!lp_stat_cache()) return;
55 namelen = strlen(orig_translated_path);
58 * Don't cache trivial valid directory entries.
60 if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
61 (strcmp(full_orig_name, "..") == 0))
62 return;
65 * If we are in case insentive mode, we need to
66 * store names that need no translation - else, it
67 * would be a waste.
70 if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
71 return;
74 * Remove any trailing '/' characters from the
75 * translated path.
78 pstrcpy(translated_path, orig_translated_path);
79 if(translated_path[namelen-1] == '/') {
80 translated_path[namelen-1] = '\0';
81 namelen--;
85 * We will only replace namelen characters
86 * of full_orig_name.
87 * StrnCpy always null terminates.
90 StrnCpy(orig_name, full_orig_name, namelen);
91 if(!case_sensitive)
92 strupper( orig_name );
95 * Check this name doesn't exist in the cache before we
96 * add it.
99 if ((hash_elem = hash_lookup(&stat_cache, orig_name))) {
100 found_scp = (stat_cache_entry *)(hash_elem->value);
101 if (strcmp((found_scp->names+found_scp->name_len+1), translated_path) == 0) {
102 return;
103 } else {
104 hash_remove(&stat_cache, hash_elem);
105 if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
106 DEBUG(0,("stat_cache_add: Out of memory !\n"));
107 return;
109 pstrcpy(scp->names, orig_name);
110 pstrcpy((scp->names+namelen+1), translated_path);
111 scp->name_len = namelen;
112 hash_insert(&stat_cache, (char *)scp, orig_name);
114 return;
115 } else {
118 * New entry.
121 if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
122 DEBUG(0,("stat_cache_add: Out of memory !\n"));
123 return;
125 safe_strcpy(scp->names, orig_name, namelen);
126 safe_strcpy(scp->names+namelen+1, translated_path, namelen);
127 scp->name_len = namelen;
128 hash_insert(&stat_cache, (char *)scp, orig_name);
131 DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->names, (scp->names+scp->name_len+1)));
134 /****************************************************************************
135 Look through the stat cache for an entry - promote it to the top if found.
136 Return True if we translated (and did a scuccessful stat on) the entire name.
137 *****************************************************************************/
139 BOOL stat_cache_lookup(connection_struct *conn, char *name, char *dirpath,
140 char **start, SMB_STRUCT_STAT *pst)
142 stat_cache_entry *scp;
143 char *trans_name;
144 pstring chk_name;
145 int namelen;
146 hash_element *hash_elem;
147 char *sp;
149 if (!lp_stat_cache())
150 return False;
152 namelen = strlen(name);
154 *start = name;
156 DO_PROFILE_INC(statcache_lookups);
159 * Don't lookup trivial valid directory entries.
161 if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
162 DO_PROFILE_INC(statcache_misses);
163 return False;
166 pstrcpy(chk_name, name);
167 if(!case_sensitive)
168 strupper( chk_name );
170 while (1) {
171 hash_elem = hash_lookup(&stat_cache, chk_name);
172 if(hash_elem == NULL) {
174 * Didn't find it - remove last component for next try.
176 sp = strrchr_m(chk_name, '/');
177 if (sp) {
178 *sp = '\0';
179 } else {
181 * We reached the end of the name - no match.
183 DO_PROFILE_INC(statcache_misses);
184 return False;
186 if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
187 || (strcmp(chk_name, "..") == 0)) {
188 DO_PROFILE_INC(statcache_misses);
189 return False;
191 } else {
192 scp = (stat_cache_entry *)(hash_elem->value);
193 DO_PROFILE_INC(statcache_hits);
194 trans_name = scp->names+scp->name_len+1;
195 if(vfs_stat(conn,trans_name, pst) != 0) {
196 /* Discard this entry - it doesn't exist in the filesystem. */
197 hash_remove(&stat_cache, hash_elem);
198 return False;
200 memcpy(name, trans_name, scp->name_len);
201 *start = &name[scp->name_len];
202 if(**start == '/')
203 ++*start;
204 StrnCpy( dirpath, trans_name, name - (*start));
205 return (namelen == scp->name_len);
210 /*************************************************************************** **
211 * Initializes or clears the stat cache.
213 * Input: none.
214 * Output: none.
216 * ************************************************************************** **
218 BOOL reset_stat_cache( void )
220 static BOOL initialised;
221 if (!lp_stat_cache()) return True;
223 if (initialised) {
224 hash_clear(&stat_cache);
227 initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE,
228 (compare_function)(strcmp));
229 return initialised;
230 } /* reset_stat_cache */