port latest changes from SAMBA_3_0 tree
[nivanova/samba-autobuild/.git] / source3 / 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  Stat cache code used in unix_convert.
29 *****************************************************************************/
30
31 typedef struct {
32         char *original_path;
33         char *translated_path;
34         size_t translated_path_length;
35         char names[2]; /* This is extended via malloc... */
36 } stat_cache_entry;
37
38 #define INIT_STAT_CACHE_SIZE 512
39 static hash_table stat_cache;
40
41 /**
42  * Add an entry into the stat cache.
43  *
44  * @param full_orig_name       The original name as specified by the client
45  * @param orig_translated_path The name on our filesystem.
46  * 
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
49  *       truncated.
50  *
51  */
52
53 void stat_cache_add( const char *full_orig_name, const char *orig_translated_path)
54 {
55         stat_cache_entry *scp;
56         stat_cache_entry *found_scp;
57         char *translated_path;
58         size_t translated_path_length;
59
60         char *original_path;
61         size_t original_path_length;
62
63         hash_element *hash_elem;
64
65         if (!lp_stat_cache())
66                 return;
67
68         /*
69          * Don't cache trivial valid directory entries.
70          */
71
72         if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
73                         (strcmp(full_orig_name, "..") == 0))
74                 return;
75
76         /*
77          * If we are in case insentive mode, we don't need to
78          * store names that need no translation - else, it
79          * would be a waste.
80          */
81
82         if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
83                 return;
84
85         /*
86          * Remove any trailing '/' characters from the
87          * translated path.
88          */
89
90         translated_path = strdup(orig_translated_path);
91         if (!translated_path)
92                 return;
93
94         translated_path_length = strlen(translated_path);
95
96         if(translated_path[translated_path_length-1] == '/') {
97                 translated_path[translated_path_length-1] = '\0';
98                 translated_path_length--;
99         }
100
101         if(case_sensitive) {
102                 original_path = strdup(full_orig_name);
103         } else {
104                 original_path = strdup_upper(full_orig_name);
105         }
106
107         if (!original_path) {
108                 SAFE_FREE(translated_path);
109                 return;
110         }
111
112         original_path_length = strlen(original_path);
113
114         if(original_path[original_path_length-1] == '/') {
115                 original_path[original_path_length-1] = '\0';
116                 original_path_length--;
117         }
118
119         if (original_path_length != translated_path_length) {
120                 if (original_path_length < translated_path_length) {
121                         DEBUG(0, ("OOPS - tried to store stat cache entry for werid length paths [%s] %u and [%s] %u)!\n",
122                                                 original_path, original_path_length, translated_path, translated_path_length));
123                         SAFE_FREE(original_path);
124                         SAFE_FREE(translated_path);
125                         return;
126                 }
127
128                 /* we only want to store the first part of original_path,
129                         up to the length of translated_path */
130
131                 original_path[translated_path_length] = '\0';
132                 original_path_length = translated_path_length;
133         }
134
135         /*
136          * Check this name doesn't exist in the cache before we 
137          * add it.
138          */
139
140         if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
141                 found_scp = (stat_cache_entry *)(hash_elem->value);
142                 if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
143                         /* already in hash table */
144                         SAFE_FREE(original_path);
145                         SAFE_FREE(translated_path);
146                         return;
147                 }
148                 /* hash collision - remove before we re-add */
149                 hash_remove(&stat_cache, hash_elem);
150         }  
151   
152         /*
153          * New entry.
154          */
155   
156         if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
157                                                 +original_path_length
158                                                 +translated_path_length)) == NULL) {
159                 DEBUG(0,("stat_cache_add: Out of memory !\n"));
160                 SAFE_FREE(original_path);
161                 SAFE_FREE(translated_path);
162                 return;
163         }
164
165         scp->original_path = scp->names;
166         /* pointer into the structure... */
167         scp->translated_path = scp->names + original_path_length + 1;
168         safe_strcpy(scp->original_path, original_path, original_path_length);
169         safe_strcpy(scp->translated_path, translated_path, translated_path_length);
170         scp->translated_path_length = translated_path_length;
171
172         hash_insert(&stat_cache, (char *)scp, original_path);
173
174         SAFE_FREE(original_path);
175         SAFE_FREE(translated_path);
176
177         DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
178 }
179
180 /**
181  * Look through the stat cache for an entry
182  *
183  * The hash-table's internals will promote it to the top if found.
184  *
185  * @param conn    A connection struct to do the stat() with.
186  * @param name    The path we are attempting to cache, modified by this routine
187  *                to be correct as far as the cache can tell us
188  * @param dirpath The path as far as the stat cache told us.
189  * @param start   A pointer into name, for where to 'start' in fixing the rest of the name up.
190  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
191  *
192  * @return True if we translated (and did a scuccessful stat on) the entire name.
193  *
194  */
195
196 BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath, 
197                        char **start, SMB_STRUCT_STAT *pst)
198 {
199         stat_cache_entry *scp;
200         char *chk_name;
201         size_t namelen;
202         hash_element *hash_elem;
203         char *sp;
204         BOOL sizechanged = False;
205         unsigned int num_components = 0;
206
207         if (!lp_stat_cache())
208                 return False;
209  
210         namelen = strlen(name);
211
212         *start = name;
213
214         DO_PROFILE_INC(statcache_lookups);
215
216         /*
217          * Don't lookup trivial valid directory entries.
218          */
219         if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
220                 DO_PROFILE_INC(statcache_misses);
221                 return False;
222         }
223
224         if (case_sensitive) {
225                 chk_name = strdup(name);
226                 if (!chk_name) {
227                         DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
228                         return False;
229                 }
230
231         } else {
232                 chk_name = strdup_upper(name);
233                 if (!chk_name) {
234                         DEBUG(0, ("stat_cache_lookup: strdup_upper failed!\n"));
235                         return False;
236                 }
237
238                 /*
239                  * In some language encodings the length changes
240                  * if we uppercase. We need to treat this differently
241                  * below.
242                  */
243                 if (strlen(chk_name) != namelen)
244                         sizechanged = True;
245         }
246
247         while (1) {
248                 hash_elem = hash_lookup(&stat_cache, chk_name);
249                 if(hash_elem == NULL) {
250                         /*
251                          * Didn't find it - remove last component for next try.
252                          */
253                         sp = strrchr_m(chk_name, '/');
254                         if (sp) {
255                                 *sp = '\0';
256                                 /*
257                                  * Count the number of times we have done this,
258                                  * we'll need it when reconstructing the string.
259                                  */
260                                 if (sizechanged)
261                                         num_components++;
262
263                         } else {
264                                 /*
265                                  * We reached the end of the name - no match.
266                                  */
267                                 DO_PROFILE_INC(statcache_misses);
268                                 SAFE_FREE(chk_name);
269                                 return False;
270                         }
271                         if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
272                                         || (strcmp(chk_name, "..") == 0)) {
273                                 DO_PROFILE_INC(statcache_misses);
274                                 SAFE_FREE(chk_name);
275                                 return False;
276                         }
277                 } else {
278                         scp = (stat_cache_entry *)(hash_elem->value);
279                         DO_PROFILE_INC(statcache_hits);
280                         if(SMB_VFS_STAT(conn,scp->translated_path, pst) != 0) {
281                                 /* Discard this entry - it doesn't exist in the filesystem.  */
282                                 hash_remove(&stat_cache, hash_elem);
283                                 SAFE_FREE(chk_name);
284                                 return False;
285                         }
286
287                         if (!sizechanged) {
288                                 memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));
289                         } else if (num_components == 0) {
290                                 pstrcpy(name, scp->translated_path);
291                         } else {
292                                 sp = strnrchr_m(name, '/', num_components);
293                                 if (sp) {
294                                         pstring last_component;
295                                         pstrcpy(last_component, sp);
296                                         pstrcpy(name, scp->translated_path);
297                                         pstrcat(name, last_component);
298                                 } else {
299                                         pstrcpy(name, scp->translated_path);
300                                 }
301                         }
302
303                         /* set pointer for 'where to start' on fixing the rest of the name */
304                         *start = &name[scp->translated_path_length];
305                         if(**start == '/')
306                                 ++*start;
307
308                         pstrcpy(dirpath, scp->translated_path);
309                         SAFE_FREE(chk_name);
310                         return (namelen == scp->translated_path_length);
311                 }
312         }
313 }
314
315 /*************************************************************************** **
316  * Initializes or clears the stat cache.
317  *
318  *  Input:  none.
319  *  Output: none.
320  *
321  * ************************************************************************** **
322  */
323 BOOL reset_stat_cache( void )
324 {
325         static BOOL initialised;
326         if (!lp_stat_cache())
327                 return True;
328
329         if (initialised) {
330                 hash_clear(&stat_cache);
331         }
332
333         initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, 
334                                        (compare_function)(strcmp));
335         return initialised;
336 }