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