44bae48990c2c4aa06d02bccd38e75673641c3b7
[tprouty/samba.git] / source / smbd / statcache.c
1 /* 
2    Unix SMB/CIFS implementation.
3    stat cache code
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Jeremy Allison 1999-2000
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 extern BOOL case_sensitive;
26
27
28 /****************************************************************************
29  Stat cache code used in unix_convert.
30 *****************************************************************************/
31
32 typedef struct {
33         char *original_path;
34         char *translated_path;
35         size_t translated_path_length;
36         char names[2]; /* This is extended via malloc... */
37 } stat_cache_entry;
38
39 #define INIT_STAT_CACHE_SIZE 512
40 static hash_table stat_cache;
41
42 /**
43  * Add an entry into the stat cache.
44  *
45  * @param full_orig_name       The original name as specified by the client
46  * @param orig_translated_path The name on our filesystem.
47  * 
48  * @note Only the first strlen(orig_translated_path) characters are stored 
49  *       into the cache.  This means that full_orig_name will be internally
50  *       truncated.
51  *
52  */
53
54 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path)
55 {
56   stat_cache_entry *scp;
57   stat_cache_entry *found_scp;
58   char *translated_path;
59   size_t translated_path_length;
60
61   char *original_path;
62   size_t original_path_length;
63
64   hash_element *hash_elem;
65
66   if (!lp_stat_cache()) return;
67
68   /*
69    * Don't cache trivial valid directory entries.
70    */
71   if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
72      (strcmp(full_orig_name, "..") == 0))
73     return;
74
75   /*
76    * If we are in case insentive mode, we don't need to
77    * store names that need no translation - else, it
78    * would be a waste.
79    */
80
81   if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
82     return;
83
84   /*
85    * Remove any trailing '/' characters from the
86    * translated path.
87    */
88
89   translated_path = strdup(orig_translated_path);
90   if (!translated_path)
91           return;
92
93   translated_path_length = strlen(translated_path);
94
95   if(translated_path[translated_path_length-1] == '/') {
96     translated_path[translated_path_length-1] = '\0';
97     translated_path_length--;
98   }
99
100   original_path = strdup(full_orig_name);
101   if (!original_path) {
102           SAFE_FREE(translated_path);
103           return;
104   }
105
106   original_path_length = strlen(original_path);
107
108   if(original_path[original_path_length-1] == '/') {
109     original_path[original_path_length-1] = '\0';
110     original_path_length--;
111   }
112
113   if(!case_sensitive)
114           strupper(original_path);
115
116   if (original_path_length != translated_path_length) {
117           if (original_path_length < translated_path_length) {
118                   DEBUG(0, ("OOPS - tried to store stat cache entry for werid length paths [%s] %u and [%s] %u)!\n", original_path, original_path_length, translated_path, translated_path_length));
119                   SAFE_FREE(original_path);
120                   SAFE_FREE(translated_path);
121                   return;
122           }
123
124           /* we only want to store the first part of original_path,
125              up to the length of translated_path */
126
127           original_path[translated_path_length] = '\0';
128           original_path_length = translated_path_length;
129   }
130
131   /*
132    * Check this name doesn't exist in the cache before we 
133    * add it.
134    */
135
136   if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
137           found_scp = (stat_cache_entry *)(hash_elem->value);
138           if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
139                   /* already in hash table */
140                   SAFE_FREE(original_path);
141                   SAFE_FREE(translated_path);
142                   return;
143           }
144           /* hash collision - remove before we re-add */
145           hash_remove(&stat_cache, hash_elem);
146   }  
147   
148   /*
149    * New entry.
150    */
151   
152   if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
153                                        +original_path_length
154                                        +translated_path_length)) == NULL) {
155           DEBUG(0,("stat_cache_add: Out of memory !\n"));
156           SAFE_FREE(original_path);
157           SAFE_FREE(translated_path);
158           return;
159   }
160
161   scp->original_path = scp->names;
162   scp->translated_path = scp->names + original_path_length + 1;
163   safe_strcpy(scp->original_path, original_path, original_path_length);
164   safe_strcpy(scp->translated_path, translated_path, translated_path_length);
165   scp->translated_path_length = translated_path_length;
166
167   hash_insert(&stat_cache, (char *)scp, original_path);
168
169   SAFE_FREE(original_path);
170   SAFE_FREE(translated_path);
171
172   DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
173 }
174
175 /**
176  * Look through the stat cache for an entry
177  *
178  * The hash-table's internals will promote it to the top if found.
179  *
180  * @param conn    A connection struct to do the stat() with.
181  * @param name    The path we are attempting to cache, modified by this routine
182  *                to be correct as far as the cache can tell us
183  * @param dirpath The path as far as the stat cache told us.
184  * @param start   A pointer into name, for where to 'start' in fixing the rest of the name up.
185  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
186  *
187  * @return True if we translated (and did a scuccessful stat on) the entire name.
188  *
189  */
190
191 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath, 
192                        char **start, SMB_STRUCT_STAT *pst)
193 {
194   stat_cache_entry *scp;
195   pstring chk_name;
196   size_t namelen;
197   hash_element *hash_elem;
198   char *sp;
199
200   if (!lp_stat_cache())
201     return False;
202  
203   namelen = strlen(name);
204
205   *start = name;
206
207   DO_PROFILE_INC(statcache_lookups);
208
209   /*
210    * Don't lookup trivial valid directory entries.
211    */
212   if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
213     DO_PROFILE_INC(statcache_misses);
214     return False;
215   }
216
217   pstrcpy(chk_name, name);
218   if(!case_sensitive)
219     strupper( chk_name );
220
221   while (1) {
222     hash_elem = hash_lookup(&stat_cache, chk_name);
223     if(hash_elem == NULL) {
224       /*
225        * Didn't find it - remove last component for next try.
226        */
227       sp = strrchr_m(chk_name, '/');
228       if (sp) {
229         *sp = '\0';
230       } else {
231         /*
232          * We reached the end of the name - no match.
233          */
234         DO_PROFILE_INC(statcache_misses);
235         return False;
236       }
237       if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
238                           || (strcmp(chk_name, "..") == 0)) {
239         DO_PROFILE_INC(statcache_misses);
240         return False;
241       }
242     } else {
243       scp = (stat_cache_entry *)(hash_elem->value);
244       DO_PROFILE_INC(statcache_hits);
245       if(vfs_stat(conn,scp->translated_path, pst) != 0) {
246         /* Discard this entry - it doesn't exist in the filesystem.  */
247         hash_remove(&stat_cache, hash_elem);
248         return False;
249       }
250       memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));
251
252       /* set pointer for 'where to start' on fixing the rest of the name */
253       *start = &name[scp->translated_path_length];
254       if(**start == '/')
255         ++*start;
256
257       pstrcpy(dirpath, scp->translated_path);
258       return (namelen == scp->translated_path_length);
259     }
260   }
261 }
262
263 /*************************************************************************** **
264  * Initializes or clears the stat cache.
265  *
266  *  Input:  none.
267  *  Output: none.
268  *
269  * ************************************************************************** **
270  */
271 BOOL reset_stat_cache( void )
272 {
273         static BOOL initialised;
274         if (!lp_stat_cache()) return True;
275
276         if (initialised) {
277                 hash_clear(&stat_cache);
278         }
279
280         initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, 
281                                        (compare_function)(strcmp));
282         return initialised;
283 } /* reset_stat_cache  */