- removed all our old wildcard matching code and replaced it with a
[tprouty/samba.git] / source / smbd / statcache.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    stat cache code
5    Copyright (C) Andrew Tridgell 1992-2000
6    Copyright (C) Jeremy Allison 1999-200
7    
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern BOOL case_sensitive;
27
28
29 /****************************************************************************
30  Stat cache code used in unix_convert.
31 *****************************************************************************/
32
33 static int global_stat_cache_lookups;
34 static int global_stat_cache_misses;
35 static int global_stat_cache_hits;
36
37 /****************************************************************************
38  Stat cache statistics code.
39 *****************************************************************************/
40
41 void print_stat_cache_statistics(void)
42 {
43   double eff;
44
45   if(global_stat_cache_lookups == 0)
46     return;
47
48   eff = (100.0* (double)global_stat_cache_hits)/(double)global_stat_cache_lookups;
49
50   DEBUG(0,("stat cache stats: lookups = %d, hits = %d, misses = %d, \
51 stat cache was %f%% effective.\n", global_stat_cache_lookups,
52        global_stat_cache_hits, global_stat_cache_misses, eff ));
53 }
54
55 typedef struct {
56   int name_len;
57   char names[2]; /* This is extended via malloc... */
58 } stat_cache_entry;
59
60 #define INIT_STAT_CACHE_SIZE 512
61 static hash_table stat_cache;
62
63 /****************************************************************************
64  Add an entry into the stat cache.
65 *****************************************************************************/
66
67 void stat_cache_add( char *full_orig_name, char *orig_translated_path)
68 {
69   stat_cache_entry *scp;
70   stat_cache_entry *found_scp;
71   pstring orig_name;
72   pstring translated_path;
73   int namelen;
74   hash_element *hash_elem;
75
76   if (!lp_stat_cache()) return;
77
78   namelen = strlen(orig_translated_path);
79
80   /*
81    * Don't cache trivial valid directory entries.
82    */
83   if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
84      (strcmp(full_orig_name, "..") == 0))
85     return;
86
87   /*
88    * If we are in case insentive mode, we need to
89    * store names that need no translation - else, it
90    * would be a waste.
91    */
92
93   if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
94     return;
95
96   /*
97    * Remove any trailing '/' characters from the
98    * translated path.
99    */
100
101   pstrcpy(translated_path, orig_translated_path);
102   if(translated_path[namelen-1] == '/') {
103     translated_path[namelen-1] = '\0';
104     namelen--;
105   }
106
107   /*
108    * We will only replace namelen characters 
109    * of full_orig_name.
110    * StrnCpy always null terminates.
111    */
112
113   StrnCpy(orig_name, full_orig_name, namelen);
114   if(!case_sensitive)
115     strupper( orig_name );
116
117   /*
118    * Check this name doesn't exist in the cache before we 
119    * add it.
120    */
121
122   if ((hash_elem = hash_lookup(&stat_cache, orig_name))) {
123     found_scp = (stat_cache_entry *)(hash_elem->value);
124     if (strcmp((found_scp->names+found_scp->name_len+1), translated_path) == 0) {
125       return;
126     } else {
127       hash_remove(&stat_cache, hash_elem);
128       if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
129         DEBUG(0,("stat_cache_add: Out of memory !\n"));
130         return;
131       }
132       pstrcpy(scp->names, orig_name);
133       pstrcpy((scp->names+namelen+1), translated_path);
134       scp->name_len = namelen;
135       hash_insert(&stat_cache, (char *)scp, orig_name);
136     }
137     return;
138   } else {
139
140     /*
141      * New entry.
142      */
143
144     if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
145       DEBUG(0,("stat_cache_add: Out of memory !\n"));
146       return;
147     }
148     pstrcpy(scp->names, orig_name);
149     pstrcpy(scp->names+namelen+1, translated_path);
150     scp->name_len = namelen;
151     hash_insert(&stat_cache, (char *)scp, orig_name);
152   }
153
154   DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->names, (scp->names+scp->name_len+1)));
155 }
156
157 /****************************************************************************
158  Look through the stat cache for an entry - promote it to the top if found.
159  Return True if we translated (and did a scuccessful stat on) the entire name.
160 *****************************************************************************/
161
162 BOOL stat_cache_lookup(connection_struct *conn, char *name, char *dirpath, 
163                        char **start, SMB_STRUCT_STAT *pst)
164 {
165   stat_cache_entry *scp;
166   char *trans_name;
167   pstring chk_name;
168   int namelen;
169   hash_element *hash_elem;
170   char *sp;
171
172   if (!lp_stat_cache())
173     return False;
174  
175   namelen = strlen(name);
176
177   *start = name;
178   global_stat_cache_lookups++;
179
180   /*
181    * Don't lookup trivial valid directory entries.
182    */
183   if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
184     global_stat_cache_misses++;
185     return False;
186   }
187
188   pstrcpy(chk_name, name);
189   if(!case_sensitive)
190     strupper( chk_name );
191
192   while (1) {
193     hash_elem = hash_lookup(&stat_cache, chk_name);
194     if(hash_elem == NULL) {
195       /*
196        * Didn't find it - remove last component for next try.
197        */
198       sp = strrchr(chk_name, '/');
199       if (sp) {
200         *sp = '\0';
201       } else {
202         /*
203          * We reached the end of the name - no match.
204          */
205         global_stat_cache_misses++;
206         return False;
207       }
208       if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
209                           || (strcmp(chk_name, "..") == 0)) {
210         global_stat_cache_misses++;
211         return False;
212       }
213     } else {
214       scp = (stat_cache_entry *)(hash_elem->value);
215       global_stat_cache_hits++;
216       trans_name = scp->names+scp->name_len+1;
217       if(conn->vfs_ops.stat(dos_to_unix(trans_name,False), pst) != 0) {
218         /* Discard this entry - it doesn't exist in the filesystem.  */
219         hash_remove(&stat_cache, hash_elem);
220         return False;
221       }
222       memcpy(name, trans_name, scp->name_len);
223       *start = &name[scp->name_len];
224       if(**start == '/')
225         ++*start;
226       StrnCpy( dirpath, trans_name, name - (*start));
227       return (namelen == scp->name_len);
228     }
229   }
230 }
231
232 /*************************************************************************** **
233  * Initializes or clears the stat cache.
234  *
235  *  Input:  none.
236  *  Output: none.
237  *
238  * ************************************************************************** **
239  */
240 BOOL reset_stat_cache( void )
241 {
242   return hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, (compare_function)(strcmp));
243 } /* reset_stat_cache  */