Herb's warning fixes. Also the POSIX locking fix.
[ira/wip.git] / source3 / 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 typedef struct {
34   int name_len;
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
45 void stat_cache_add( char *full_orig_name, char *orig_translated_path)
46 {
47   stat_cache_entry *scp;
48   stat_cache_entry *found_scp;
49   pstring orig_name;
50   pstring translated_path;
51   int namelen;
52   hash_element *hash_elem;
53
54   if (!lp_stat_cache()) return;
55
56   namelen = strlen(orig_translated_path);
57
58   /*
59    * Don't cache trivial valid directory entries.
60    */
61   if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
62      (strcmp(full_orig_name, "..") == 0))
63     return;
64
65   /*
66    * If we are in case insentive mode, we need to
67    * store names that need no translation - else, it
68    * would be a waste.
69    */
70
71   if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
72     return;
73
74   /*
75    * Remove any trailing '/' characters from the
76    * translated path.
77    */
78
79   pstrcpy(translated_path, orig_translated_path);
80   if(translated_path[namelen-1] == '/') {
81     translated_path[namelen-1] = '\0';
82     namelen--;
83   }
84
85   /*
86    * We will only replace namelen characters 
87    * of full_orig_name.
88    * StrnCpy always null terminates.
89    */
90
91   StrnCpy(orig_name, full_orig_name, namelen);
92   if(!case_sensitive)
93     strupper( orig_name );
94
95   /*
96    * Check this name doesn't exist in the cache before we 
97    * add it.
98    */
99
100   if ((hash_elem = hash_lookup(&stat_cache, orig_name))) {
101     found_scp = (stat_cache_entry *)(hash_elem->value);
102     if (strcmp((found_scp->names+found_scp->name_len+1), translated_path) == 0) {
103       return;
104     } else {
105       hash_remove(&stat_cache, hash_elem);
106       if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
107         DEBUG(0,("stat_cache_add: Out of memory !\n"));
108         return;
109       }
110       pstrcpy(scp->names, orig_name);
111       pstrcpy((scp->names+namelen+1), translated_path);
112       scp->name_len = namelen;
113       hash_insert(&stat_cache, (char *)scp, orig_name);
114     }
115     return;
116   } else {
117
118     /*
119      * New entry.
120      */
121
122     if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)+2*namelen)) == NULL) {
123       DEBUG(0,("stat_cache_add: Out of memory !\n"));
124       return;
125     }
126     pstrcpy(scp->names, orig_name);
127     pstrcpy(scp->names+namelen+1, translated_path);
128     scp->name_len = namelen;
129     hash_insert(&stat_cache, (char *)scp, orig_name);
130   }
131
132   DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->names, (scp->names+scp->name_len+1)));
133 }
134
135 /****************************************************************************
136  Look through the stat cache for an entry - promote it to the top if found.
137  Return True if we translated (and did a scuccessful stat on) the entire name.
138 *****************************************************************************/
139
140 BOOL stat_cache_lookup(connection_struct *conn, char *name, char *dirpath, 
141                        char **start, SMB_STRUCT_STAT *pst)
142 {
143   stat_cache_entry *scp;
144   char *trans_name;
145   pstring chk_name;
146   int namelen;
147   hash_element *hash_elem;
148   char *sp;
149
150   if (!lp_stat_cache())
151     return False;
152  
153   namelen = strlen(name);
154
155   *start = name;
156
157   DO_PROFILE_INC(statcache_lookups);
158
159   /*
160    * Don't lookup trivial valid directory entries.
161    */
162   if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
163     DO_PROFILE_INC(statcache_misses);
164     return False;
165   }
166
167   pstrcpy(chk_name, name);
168   if(!case_sensitive)
169     strupper( chk_name );
170
171   while (1) {
172     hash_elem = hash_lookup(&stat_cache, chk_name);
173     if(hash_elem == NULL) {
174       /*
175        * Didn't find it - remove last component for next try.
176        */
177       sp = strrchr(chk_name, '/');
178       if (sp) {
179         *sp = '\0';
180       } else {
181         /*
182          * We reached the end of the name - no match.
183          */
184         DO_PROFILE_INC(statcache_misses);
185         return False;
186       }
187       if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
188                           || (strcmp(chk_name, "..") == 0)) {
189         DO_PROFILE_INC(statcache_misses);
190         return False;
191       }
192     } else {
193       scp = (stat_cache_entry *)(hash_elem->value);
194       DO_PROFILE_INC(statcache_hits);
195       trans_name = scp->names+scp->name_len+1;
196       if(conn->vfs_ops.stat(conn,dos_to_unix(trans_name,False), pst) != 0) {
197         /* Discard this entry - it doesn't exist in the filesystem.  */
198         hash_remove(&stat_cache, hash_elem);
199         return False;
200       }
201       memcpy(name, trans_name, scp->name_len);
202       *start = &name[scp->name_len];
203       if(**start == '/')
204         ++*start;
205       StrnCpy( dirpath, trans_name, name - (*start));
206       return (namelen == scp->name_len);
207     }
208   }
209 }
210
211 /*************************************************************************** **
212  * Initializes or clears the stat cache.
213  *
214  *  Input:  none.
215  *  Output: none.
216  *
217  * ************************************************************************** **
218  */
219 BOOL reset_stat_cache( void )
220 {
221         static BOOL initialised;
222         if (!lp_stat_cache()) return True;
223
224         if (!initialised) {
225                 initialised = True;
226                 return hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, (compare_function)(strcmp));
227         }
228         hash_clear(&stat_cache);
229         return hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, (compare_function)(strcmp));
230
231 } /* reset_stat_cache  */