s3-build: avoid to globally include printing and spoolss headers.
[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-2007
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7    Copyright (C) Volker Lendecke 2007
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/messaging.h"
25
26 /****************************************************************************
27  Stat cache code used in unix_convert.
28 *****************************************************************************/
29
30 /**
31  * Add an entry into the stat cache.
32  *
33  * @param full_orig_name       The original name as specified by the client
34  * @param orig_translated_path The name on our filesystem.
35  *
36  * @note Only the first strlen(orig_translated_path) characters are stored
37  *       into the cache.  This means that full_orig_name will be internally
38  *       truncated.
39  *
40  */
41
42 void stat_cache_add( const char *full_orig_name,
43                 char *translated_path,
44                 bool case_sensitive)
45 {
46         size_t translated_path_length;
47         char *original_path;
48         size_t original_path_length;
49         char saved_char;
50         TALLOC_CTX *ctx = talloc_tos();
51
52         if (!lp_stat_cache()) {
53                 return;
54         }
55
56         /*
57          * Don't cache trivial valid directory entries such as . and ..
58          */
59
60         if ((*full_orig_name == '\0')
61             || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
62                 return;
63         }
64
65         /*
66          * If we are in case insentive mode, we don't 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, translated_path) == 0)) {
72                 return;
73         }
74
75         /*
76          * Remove any trailing '/' characters from the
77          * translated path.
78          */
79
80         translated_path_length = strlen(translated_path);
81
82         if(translated_path[translated_path_length-1] == '/') {
83                 translated_path_length--;
84         }
85
86         if(case_sensitive) {
87                 original_path = talloc_strdup(ctx,full_orig_name);
88         } else {
89                 original_path = talloc_strdup_upper(ctx,full_orig_name);
90         }
91
92         if (!original_path) {
93                 return;
94         }
95
96         original_path_length = strlen(original_path);
97
98         if(original_path[original_path_length-1] == '/') {
99                 original_path[original_path_length-1] = '\0';
100                 original_path_length--;
101         }
102
103         if (original_path_length != translated_path_length) {
104                 if (original_path_length < translated_path_length) {
105                         DEBUG(0, ("OOPS - tried to store stat cache entry "
106                         "for weird length paths [%s] %lu and [%s] %lu)!\n",
107                                   original_path,
108                                   (unsigned long)original_path_length,
109                                   translated_path,
110                                   (unsigned long)translated_path_length));
111                         TALLOC_FREE(original_path);
112                         return;
113                 }
114
115                 /* we only want to index by the first part of original_path,
116                         up to the length of translated_path */
117
118                 original_path[translated_path_length] = '\0';
119                 original_path_length = translated_path_length;
120         }
121
122         /* Ensure we're null terminated. */
123         saved_char = translated_path[translated_path_length];
124         translated_path[translated_path_length] = '\0';
125
126         /*
127          * New entry or replace old entry.
128          */
129
130         memcache_add(
131                 smbd_memcache(), STAT_CACHE,
132                 data_blob_const(original_path, original_path_length),
133                 data_blob_const(translated_path, translated_path_length + 1));
134
135         DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
136                  (unsigned long)translated_path,
137                  (unsigned int)translated_path_length,
138                  original_path,
139                  translated_path));
140
141         translated_path[translated_path_length] = saved_char;
142         TALLOC_FREE(original_path);
143 }
144
145 /**
146  * Look through the stat cache for an entry
147  *
148  * @param conn    A connection struct to do the stat() with.
149  * @param name    The path we are attempting to cache, modified by this routine
150  *                to be correct as far as the cache can tell us. We assume that
151  *                it is a talloc'ed string from top of stack, we free it if
152  *                necessary.
153  * @param dirpath The path as far as the stat cache told us. Also talloced
154  *                from top of stack.
155  * @param start   A pointer into name, for where to 'start' in fixing the rest
156  *                of the name up.
157  * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
158  *
159  * @return True if we translated (and did a scuccessful stat on) the entire
160  *                name.
161  *
162  */
163
164 bool stat_cache_lookup(connection_struct *conn,
165                         char **pp_name,
166                         char **pp_dirpath,
167                         char **pp_start,
168                         SMB_STRUCT_STAT *pst)
169 {
170         char *chk_name;
171         size_t namelen;
172         bool sizechanged = False;
173         unsigned int num_components = 0;
174         char *translated_path;
175         size_t translated_path_length;
176         DATA_BLOB data_val;
177         char *name;
178         TALLOC_CTX *ctx = talloc_tos();
179         struct smb_filename smb_fname;
180
181         *pp_dirpath = NULL;
182         *pp_start = *pp_name;
183
184         if (!lp_stat_cache()) {
185                 return False;
186         }
187
188         name = *pp_name;
189         namelen = strlen(name);
190
191         DO_PROFILE_INC(statcache_lookups);
192
193         /*
194          * Don't lookup trivial valid directory entries.
195          */
196         if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
197                 return False;
198         }
199
200         if (conn->case_sensitive) {
201                 chk_name = talloc_strdup(ctx,name);
202                 if (!chk_name) {
203                         DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
204                         return False;
205                 }
206
207         } else {
208                 chk_name = talloc_strdup_upper(ctx,name);
209                 if (!chk_name) {
210                         DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
211                         return False;
212                 }
213
214                 /*
215                  * In some language encodings the length changes
216                  * if we uppercase. We need to treat this differently
217                  * below.
218                  */
219                 if (strlen(chk_name) != namelen) {
220                         sizechanged = True;
221                 }
222         }
223
224         while (1) {
225                 char *sp;
226
227                 data_val = data_blob_null;
228
229                 if (memcache_lookup(
230                             smbd_memcache(), STAT_CACHE,
231                             data_blob_const(chk_name, strlen(chk_name)),
232                             &data_val)) {
233                         break;
234                 }
235
236                 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
237                                 chk_name ));
238                 /*
239                  * Didn't find it - remove last component for next try.
240                  */
241                 if (!(sp = strrchr_m(chk_name, '/'))) {
242                         /*
243                          * We reached the end of the name - no match.
244                          */
245                         DO_PROFILE_INC(statcache_misses);
246                         TALLOC_FREE(chk_name);
247                         return False;
248                 }
249
250                 *sp = '\0';
251
252                 /*
253                  * Count the number of times we have done this, we'll
254                  * need it when reconstructing the string.
255                  */
256
257                 if (sizechanged) {
258                         num_components++;
259                 }
260
261                 if ((*chk_name == '\0')
262                     || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
263                         DO_PROFILE_INC(statcache_misses);
264                         TALLOC_FREE(chk_name);
265                         return False;
266                 }
267         }
268
269         translated_path = talloc_strdup(ctx,(char *)data_val.data);
270         if (!translated_path) {
271                 smb_panic("talloc failed");
272         }
273         translated_path_length = data_val.length - 1;
274
275         DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
276                   "-> [%s]\n", chk_name, translated_path ));
277         DO_PROFILE_INC(statcache_hits);
278
279         ZERO_STRUCT(smb_fname);
280         smb_fname.base_name = translated_path;
281
282         if (SMB_VFS_STAT(conn, &smb_fname) != 0) {
283                 /* Discard this entry - it doesn't exist in the filesystem. */
284                 memcache_delete(smbd_memcache(), STAT_CACHE,
285                                 data_blob_const(chk_name, strlen(chk_name)));
286                 TALLOC_FREE(chk_name);
287                 TALLOC_FREE(translated_path);
288                 return False;
289         }
290         *pst = smb_fname.st;
291
292         if (!sizechanged) {
293                 memcpy(*pp_name, translated_path,
294                        MIN(namelen, translated_path_length));
295         } else {
296                 if (num_components == 0) {
297                         name = talloc_strndup(ctx, translated_path,
298                                            translated_path_length);
299                 } else {
300                         char *sp;
301
302                         sp = strnrchr_m(name, '/', num_components);
303                         if (sp) {
304                                 name = talloc_asprintf(ctx,"%.*s%s",
305                                          (int)translated_path_length,
306                                          translated_path, sp);
307                         } else {
308                                 name = talloc_strndup(ctx,
309                                                 translated_path,
310                                                 translated_path_length);
311                         }
312                 }
313                 if (name == NULL) {
314                         /*
315                          * TODO: Get us out of here with a real error message
316                          */
317                         smb_panic("talloc failed");
318                 }
319                 TALLOC_FREE(*pp_name);
320                 *pp_name = name;
321         }
322
323
324         /* set pointer for 'where to start' on fixing the rest of the name */
325         *pp_start = &name[translated_path_length];
326         if (**pp_start == '/') {
327                 ++*pp_start;
328         }
329
330         *pp_dirpath = translated_path;
331         TALLOC_FREE(chk_name);
332         return (namelen == translated_path_length);
333 }
334
335 /***************************************************************************
336  Tell all smbd's to delete an entry.
337 **************************************************************************/
338
339 void send_stat_cache_delete_message(const char *name)
340 {
341 #ifdef DEVELOPER
342         message_send_all(smbd_messaging_context(),
343                         MSG_SMB_STAT_CACHE_DELETE,
344                         name,
345                         strlen(name)+1,
346                         NULL);
347 #endif
348 }
349
350 /***************************************************************************
351  Delete an entry.
352 **************************************************************************/
353
354 void stat_cache_delete(const char *name)
355 {
356         char *lname = talloc_strdup_upper(talloc_tos(), name);
357
358         if (!lname) {
359                 return;
360         }
361         DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
362                         lname, name ));
363
364         memcache_delete(smbd_memcache(), STAT_CACHE,
365                         data_blob_const(lname, talloc_get_size(lname)-1));
366         TALLOC_FREE(lname);
367 }
368
369 /***************************************************************
370  Compute a hash value based on a string key value.
371  The function returns the bucket index number for the hashed key.
372  JRA. Use a djb-algorithm hash for speed.
373 ***************************************************************/
374
375 unsigned int fast_string_hash(TDB_DATA *key)
376 {
377         unsigned int n = 0;
378         const char *p;
379         for (p = (const char *)key->dptr; *p != '\0'; p++) {
380                 n = ((n << 5) + n) ^ (unsigned int)(*p);
381         }
382         return n;
383 }
384
385 /***************************************************************************
386  Initializes or clears the stat cache.
387 **************************************************************************/
388
389 bool reset_stat_cache( void )
390 {
391         if (!lp_stat_cache())
392                 return True;
393
394         memcache_flush(smbd_memcache(), STAT_CACHE);
395
396         return True;
397 }