Some cleanups:
[vlendec/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   int name_len;
34   char names[2]; /* This is extended via malloc... */
35 } stat_cache_entry;
36
37 #define INIT_STAT_CACHE_SIZE 512
38 static hash_table stat_cache;
39
40 /****************************************************************************
41  Add an entry into the stat cache.
42 *****************************************************************************/
43
44 void stat_cache_add( char *full_orig_name, char *orig_translated_path)
45 {
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;
52
53   if (!lp_stat_cache()) return;
54
55   namelen = strlen(orig_translated_path);
56
57   /*
58    * Don't cache trivial valid directory entries.
59    */
60   if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
61      (strcmp(full_orig_name, "..") == 0))
62     return;
63
64   /*
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.
68    */
69
70   if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
71     return;
72
73   /*
74    * Remove any trailing '/' characters from the
75    * translated path.
76    */
77
78   pstrcpy(translated_path, orig_translated_path);
79   if(translated_path[namelen-1] == '/') {
80     translated_path[namelen-1] = '\0';
81     namelen--;
82   }
83
84   /*
85    * We will only replace namelen characters 
86    * of full_orig_name.
87    * StrnCpy always null terminates.
88    */
89
90   StrnCpy(orig_name, full_orig_name, namelen);
91   if(!case_sensitive)
92     strupper( orig_name );
93
94   /*
95    * Check this name doesn't exist in the cache before we 
96    * add it.
97    */
98
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;
108       }
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);
113     }
114     return;
115   } else {
116
117     /*
118      * New entry.
119      */
120
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;
124     }
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);
129   }
130
131   DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->names, (scp->names+scp->name_len+1)));
132 }
133
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 *****************************************************************************/
138
139 BOOL stat_cache_lookup(connection_struct *conn, char *name, char *dirpath, 
140                        char **start, SMB_STRUCT_STAT *pst)
141 {
142   stat_cache_entry *scp;
143   char *trans_name;
144   pstring chk_name;
145   int namelen;
146   hash_element *hash_elem;
147   char *sp;
148
149   if (!lp_stat_cache())
150     return False;
151  
152   namelen = strlen(name);
153
154   *start = name;
155
156   DO_PROFILE_INC(statcache_lookups);
157
158   /*
159    * Don't lookup trivial valid directory entries.
160    */
161   if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
162     DO_PROFILE_INC(statcache_misses);
163     return False;
164   }
165
166   pstrcpy(chk_name, name);
167   if(!case_sensitive)
168     strupper( chk_name );
169
170   while (1) {
171     hash_elem = hash_lookup(&stat_cache, chk_name);
172     if(hash_elem == NULL) {
173       /*
174        * Didn't find it - remove last component for next try.
175        */
176       sp = strrchr_m(chk_name, '/');
177       if (sp) {
178         *sp = '\0';
179       } else {
180         /*
181          * We reached the end of the name - no match.
182          */
183         DO_PROFILE_INC(statcache_misses);
184         return False;
185       }
186       if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
187                           || (strcmp(chk_name, "..") == 0)) {
188         DO_PROFILE_INC(statcache_misses);
189         return False;
190       }
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;
199       }
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);
206     }
207   }
208 }
209
210 /*************************************************************************** **
211  * Initializes or clears the stat cache.
212  *
213  *  Input:  none.
214  *  Output: none.
215  *
216  * ************************************************************************** **
217  */
218 BOOL reset_stat_cache( void )
219 {
220         static BOOL initialised;
221         if (!lp_stat_cache()) return True;
222
223         if (initialised) {
224                 hash_clear(&stat_cache);
225         }
226
227         initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, 
228                                        (compare_function)(strcmp));
229         return initialised;
230 } /* reset_stat_cache  */