summaryrefslogtreecommitdiff
path: root/source3/smbd/statcache.c
blob: 812e0dde37505fa400461579bf2ab3d6d18f0dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* 
   Unix SMB/CIFS implementation.
   stat cache code
   Copyright (C) Andrew Tridgell 1992-2000
   Copyright (C) Jeremy Allison 1999-2000
   
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern BOOL case_sensitive;


/****************************************************************************
 Stat cache code used in unix_convert.
*****************************************************************************/

typedef struct {
	char *original_path;
	char *translated_path;
	size_t translated_path_length;
	char names[2]; /* This is extended via malloc... */
} stat_cache_entry;

#define INIT_STAT_CACHE_SIZE 512
static hash_table stat_cache;

/****************************************************************************
 Add an entry into the stat cache.
*****************************************************************************/

void stat_cache_add( char *full_orig_name, char *orig_translated_path)
{
  stat_cache_entry *scp;
  stat_cache_entry *found_scp;
  char *translated_path;
  size_t translated_path_length;

  char *original_path;
  size_t original_path_length;

  hash_element *hash_elem;

  if (!lp_stat_cache()) return;

  /*
   * Don't cache trivial valid directory entries.
   */
  if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
     (strcmp(full_orig_name, "..") == 0))
    return;

  /*
   * If we are in case insentive mode, we don't need to
   * store names that need no translation - else, it
   * would be a waste.
   */

  if(case_sensitive && (strcmp(full_orig_name, orig_translated_path) == 0))
    return;

  /*
   * Remove any trailing '/' characters from the
   * translated path.
   */

  translated_path = strdup(orig_translated_path);
  if (!translated_path)
	  return;

  translated_path_length = strlen(translated_path);

  if(translated_path[translated_path_length-1] == '/') {
    translated_path[translated_path_length-1] = '\0';
    translated_path_length--;
  }

  original_path = strdup(full_orig_name);
  if (!original_path) 
	  return;

  original_path_length = strlen(original_path);

  if(!case_sensitive)
	  strupper(original_path);
  
#if 0
  /*
   * We will only replace namelen characters 
   * of full_orig_name.
   * StrnCpy always null terminates.
   */

  smbStrnCpy(orig_name, full_orig_name, namelen);
  if(!case_sensitive)
    strupper( orig_name );
#endif

  /*
   * Check this name doesn't exist in the cache before we 
   * add it.
   */

  if ((hash_elem = hash_lookup(&stat_cache, original_path))) {
	  found_scp = (stat_cache_entry *)(hash_elem->value);
	  if (strcmp((found_scp->translated_path), orig_translated_path) == 0) {
		  /* already in hash table */
		  return;
	  }
	  /* hash collision - remove before we re-add */
	  hash_remove(&stat_cache, hash_elem);
  }  
  
  /*
   * New entry.
   */
  
  if((scp = (stat_cache_entry *)malloc(sizeof(stat_cache_entry)
				       +original_path_length
				       +translated_path_length)) == NULL) {
	  DEBUG(0,("stat_cache_add: Out of memory !\n"));
	  return;
  }

  scp->original_path = scp->names;
  scp->translated_path = scp->names + original_path_length + 1;
  safe_strcpy(scp->original_path, original_path, original_path_length);
  safe_strcpy(scp->translated_path, translated_path, translated_path_length);
  scp->translated_path_length = translated_path_length;

  hash_insert(&stat_cache, (char *)scp, original_path);

  DEBUG(5,("stat_cache_add: Added entry %s -> %s\n", scp->original_path, scp->translated_path));
}

/**
 * Look through the stat cache for an entry
 *
 * The hash-table's internals will promote it to the top if found.
 *
 * @param conn    A connection struct to do the stat() with.
 * @param name    The path we are attempting to cache, modified by this routine
 *                to be correct as far as the cache can tell us
 * @param dirpath The path as far as the stat cache told us.
 * @param start   A pointer into name, for where to 'start' in fixing the rest of the name up.
 * @param psd     A stat buffer, NOT from the cache, but just a side-effect.
 *
 * @return True if we translated (and did a scuccessful stat on) the entire name.
 *
 */

BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath, 
		       char **start, SMB_STRUCT_STAT *pst)
{
  stat_cache_entry *scp;
  pstring chk_name;
  size_t namelen;
  hash_element *hash_elem;
  char *sp;

  if (!lp_stat_cache())
    return False;
 
  namelen = strlen(name);

  *start = name;

  DO_PROFILE_INC(statcache_lookups);

  /*
   * Don't lookup trivial valid directory entries.
   */
  if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
    DO_PROFILE_INC(statcache_misses);
    return False;
  }

  pstrcpy(chk_name, name);
  if(!case_sensitive)
    strupper( chk_name );

  while (1) {
    hash_elem = hash_lookup(&stat_cache, chk_name);
    if(hash_elem == NULL) {
      /*
       * Didn't find it - remove last component for next try.
       */
      sp = strrchr_m(chk_name, '/');
      if (sp) {
        *sp = '\0';
      } else {
        /*
         * We reached the end of the name - no match.
         */
	DO_PROFILE_INC(statcache_misses);
        return False;
      }
      if((*chk_name == '\0') || (strcmp(chk_name, ".") == 0)
                          || (strcmp(chk_name, "..") == 0)) {
	DO_PROFILE_INC(statcache_misses);
        return False;
      }
    } else {
      scp = (stat_cache_entry *)(hash_elem->value);
      DO_PROFILE_INC(statcache_hits);
      if(vfs_stat(conn,scp->translated_path, pst) != 0) {
        /* Discard this entry - it doesn't exist in the filesystem.  */
        hash_remove(&stat_cache, hash_elem);
        return False;
      }
      memcpy(name, scp->translated_path, MIN(sizeof(pstring)-1, scp->translated_path_length));

      /* set pointer for 'where to start' on fixing the rest of the name */
      *start = &name[scp->translated_path_length];
      if(**start == '/')
        ++*start;

      pstrcpy(dirpath, scp->translated_path);
      return (namelen == scp->translated_path_length);
    }
  }
}

/*************************************************************************** **
 * Initializes or clears the stat cache.
 *
 *  Input:  none.
 *  Output: none.
 *
 * ************************************************************************** **
 */
BOOL reset_stat_cache( void )
{
	static BOOL initialised;
	if (!lp_stat_cache()) return True;

	if (initialised) {
		hash_clear(&stat_cache);
	}

	initialised = hash_table_init( &stat_cache, INIT_STAT_CACHE_SIZE, 
				       (compare_function)(strcmp));
	return initialised;
} /* reset_stat_cache  */