2000-04-30 15:04:28 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-30 15:04:28 +04:00
stat cache code
Copyright ( C ) Andrew Tridgell 1992 - 2000
2004-08-25 05:04:02 +04:00
Copyright ( C ) Jeremy Allison 1999 - 2004
2003-03-18 01:23:51 +03:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2003
2000-04-30 15:04:28 +04:00
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 0213 9 , USA .
*/
# include "includes.h"
/****************************************************************************
Stat cache code used in unix_convert .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-25 02:48:49 +04:00
static TDB_CONTEXT * tdb_stat_cache ;
2000-04-30 15:04:28 +04:00
2003-03-18 01:23:51 +03:00
/**
* Add an entry into the stat cache .
*
* @ param full_orig_name The original name as specified by the client
* @ param orig_translated_path The name on our filesystem .
*
* @ note Only the first strlen ( orig_translated_path ) characters are stored
* into the cache . This means that full_orig_name will be internally
* truncated .
*
*/
2000-04-30 15:04:28 +04:00
2004-05-07 22:37:47 +04:00
void stat_cache_add ( const char * full_orig_name , const char * orig_translated_path , BOOL case_sensitive )
2000-04-30 15:04:28 +04:00
{
2003-07-02 04:08:29 +04:00
char * translated_path ;
size_t translated_path_length ;
2004-08-25 02:48:49 +04:00
TDB_DATA data_val ;
2003-07-02 04:08:29 +04:00
char * original_path ;
size_t original_path_length ;
2005-04-08 00:59:37 +04:00
size_t sc_size = lp_max_stat_cache_size ( ) ;
2003-07-02 04:08:29 +04:00
if ( ! lp_stat_cache ( ) )
return ;
2006-07-11 22:01:26 +04:00
if ( sc_size & & ( tdb_map_size ( tdb_stat_cache ) > sc_size * 1024 ) ) {
2005-04-08 00:59:37 +04:00
reset_stat_cache ( ) ;
}
2004-08-25 02:48:49 +04:00
ZERO_STRUCT ( data_val ) ;
2003-07-02 04:08:29 +04:00
/*
2003-09-05 23:59:55 +04:00
* Don ' t cache trivial valid directory entries such as . and . .
2003-07-02 04:08:29 +04:00
*/
2003-09-05 23:59:55 +04:00
if ( ( * full_orig_name = = ' \0 ' ) | | ( full_orig_name [ 0 ] = = ' . ' & &
( ( full_orig_name [ 1 ] = = ' \0 ' ) | |
2005-06-23 21:00:11 +04:00
( full_orig_name [ 1 ] = = ' . ' & & full_orig_name [ 2 ] = = ' \0 ' ) ) ) )
2003-07-02 04:08:29 +04:00
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 .
*/
2004-12-07 21:25:53 +03:00
translated_path = SMB_STRDUP ( orig_translated_path ) ;
2003-07-02 04:08:29 +04:00
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 - - ;
}
2003-07-27 07:40:45 +04:00
if ( case_sensitive ) {
2004-12-07 21:25:53 +03:00
original_path = SMB_STRDUP ( full_orig_name ) ;
2003-07-27 07:40:45 +04:00
} else {
original_path = strdup_upper ( full_orig_name ) ;
}
2003-07-02 04:08:29 +04:00
if ( ! original_path ) {
SAFE_FREE ( translated_path ) ;
return ;
}
original_path_length = strlen ( original_path ) ;
if ( original_path [ original_path_length - 1 ] = = ' / ' ) {
original_path [ original_path_length - 1 ] = ' \0 ' ;
original_path_length - - ;
}
if ( original_path_length ! = translated_path_length ) {
if ( original_path_length < translated_path_length ) {
2003-11-03 17:34:25 +03:00
DEBUG ( 0 , ( " OOPS - tried to store stat cache entry for weird length paths [%s] %lu and [%s] %lu)! \n " ,
original_path , ( unsigned long ) original_path_length , translated_path , ( unsigned long ) translated_path_length ) ) ;
2003-07-02 04:08:29 +04:00
SAFE_FREE ( original_path ) ;
SAFE_FREE ( translated_path ) ;
return ;
}
2004-08-25 02:48:49 +04:00
/* we only want to index by the first part of original_path,
2003-07-02 04:08:29 +04:00
up to the length of translated_path */
original_path [ translated_path_length ] = ' \0 ' ;
original_path_length = translated_path_length ;
}
/*
2004-08-25 02:48:49 +04:00
* New entry or replace old entry .
2003-07-02 04:08:29 +04:00
*/
2003-03-18 01:23:51 +03:00
2004-08-25 02:48:49 +04:00
data_val . dsize = translated_path_length + 1 ;
2007-03-29 13:35:51 +04:00
data_val . dptr = ( uint8 * ) translated_path ;
2003-07-02 04:08:29 +04:00
2004-08-25 02:48:49 +04:00
if ( tdb_store_bystring ( tdb_stat_cache , original_path , data_val , TDB_REPLACE ) ! = 0 ) {
DEBUG ( 0 , ( " stat_cache_add: Error storing entry %s -> %s \n " , original_path , translated_path ) ) ;
} else {
2005-09-13 01:47:42 +04:00
DEBUG ( 5 , ( " stat_cache_add: Added entry (%lx:size%x) %s -> %s \n " ,
( unsigned long ) data_val . dptr , ( unsigned int ) data_val . dsize , original_path , translated_path ) ) ;
2004-08-25 02:48:49 +04:00
}
2003-07-02 04:08:29 +04:00
SAFE_FREE ( original_path ) ;
SAFE_FREE ( translated_path ) ;
2000-04-30 15:04:28 +04:00
}
2003-03-18 01:23:51 +03:00
/**
* Look through the stat cache for an entry
*
* @ 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 .
*
*/
2000-04-30 15:04:28 +04:00
2003-03-18 01:23:51 +03:00
BOOL stat_cache_lookup ( connection_struct * conn , pstring name , pstring dirpath ,
2000-04-30 15:04:28 +04:00
char * * start , SMB_STRUCT_STAT * pst )
{
2003-07-27 07:40:45 +04:00
char * chk_name ;
2003-07-02 04:08:29 +04:00
size_t namelen ;
2003-07-03 00:01:51 +04:00
BOOL sizechanged = False ;
unsigned int num_components = 0 ;
2003-07-02 04:08:29 +04:00
if ( ! lp_stat_cache ( ) )
return False ;
2000-04-30 15:04:28 +04:00
2003-07-02 04:08:29 +04:00
namelen = strlen ( name ) ;
* start = name ;
DO_PROFILE_INC ( statcache_lookups ) ;
/*
* Don ' t lookup trivial valid directory entries .
*/
2003-09-06 01:30:50 +04:00
if ( ( * name = = ' \0 ' ) | | ( name [ 0 ] = = ' . ' & &
( ( name [ 1 ] = = ' \0 ' ) | |
( name [ 1 ] = = ' . ' & & name [ 1 ] = = ' \0 ' ) ) ) )
2003-07-02 04:08:29 +04:00
return False ;
2004-05-07 22:37:47 +04:00
if ( conn - > case_sensitive ) {
2004-12-07 21:25:53 +03:00
chk_name = SMB_STRDUP ( name ) ;
2003-07-27 07:40:45 +04:00
if ( ! chk_name ) {
DEBUG ( 0 , ( " stat_cache_lookup: strdup failed! \n " ) ) ;
return False ;
}
} else {
chk_name = strdup_upper ( name ) ;
if ( ! chk_name ) {
DEBUG ( 0 , ( " stat_cache_lookup: strdup_upper failed! \n " ) ) ;
return False ;
}
2003-07-03 00:01:51 +04:00
/*
* In some language encodings the length changes
* if we uppercase . We need to treat this differently
* below .
*/
if ( strlen ( chk_name ) ! = namelen )
sizechanged = True ;
}
2003-07-02 04:08:29 +04:00
while ( 1 ) {
2004-08-25 02:48:49 +04:00
TDB_DATA data_val ;
char * sp ;
data_val = tdb_fetch_bystring ( tdb_stat_cache , chk_name ) ;
if ( data_val . dptr = = NULL | | data_val . dsize = = 0 ) {
2003-09-06 01:30:50 +04:00
DEBUG ( 10 , ( " stat_cache_lookup: lookup failed for name [%s] \n " , chk_name ) ) ;
2003-07-02 04:08:29 +04:00
/*
* Didn ' t find it - remove last component for next try .
*/
sp = strrchr_m ( chk_name , ' / ' ) ;
if ( sp ) {
* sp = ' \0 ' ;
2003-07-03 00:01:51 +04:00
/*
* Count the number of times we have done this ,
* we ' ll need it when reconstructing the string .
*/
if ( sizechanged )
num_components + + ;
2003-07-02 04:08:29 +04:00
} else {
/*
* We reached the end of the name - no match .
*/
DO_PROFILE_INC ( statcache_misses ) ;
2003-07-27 07:40:45 +04:00
SAFE_FREE ( chk_name ) ;
2003-07-02 04:08:29 +04:00
return False ;
}
if ( ( * chk_name = = ' \0 ' ) | | ( strcmp ( chk_name , " . " ) = = 0 )
| | ( strcmp ( chk_name , " .. " ) = = 0 ) ) {
DO_PROFILE_INC ( statcache_misses ) ;
2003-07-27 07:40:45 +04:00
SAFE_FREE ( chk_name ) ;
2003-07-02 04:08:29 +04:00
return False ;
}
} else {
2004-08-25 02:48:49 +04:00
BOOL retval ;
2007-03-29 13:35:51 +04:00
char * translated_path = ( char * ) data_val . dptr ;
2004-08-25 02:48:49 +04:00
size_t translated_path_length = data_val . dsize - 1 ;
DEBUG ( 10 , ( " stat_cache_lookup: lookup succeeded for name [%s] -> [%s] \n " , chk_name , translated_path ) ) ;
2003-07-02 04:08:29 +04:00
DO_PROFILE_INC ( statcache_hits ) ;
2004-08-25 02:48:49 +04:00
if ( SMB_VFS_STAT ( conn , translated_path , pst ) ! = 0 ) {
2003-07-02 04:08:29 +04:00
/* Discard this entry - it doesn't exist in the filesystem. */
2004-08-25 02:48:49 +04:00
tdb_delete_bystring ( tdb_stat_cache , chk_name ) ;
2003-07-27 07:40:45 +04:00
SAFE_FREE ( chk_name ) ;
2004-08-25 02:48:49 +04:00
SAFE_FREE ( data_val . dptr ) ;
2003-07-02 04:08:29 +04:00
return False ;
}
2003-07-03 00:01:51 +04:00
if ( ! sizechanged ) {
2004-08-25 02:48:49 +04:00
memcpy ( name , translated_path , MIN ( sizeof ( pstring ) - 1 , translated_path_length ) ) ;
2003-07-08 00:22:35 +04:00
} else if ( num_components = = 0 ) {
2004-08-25 02:48:49 +04:00
pstrcpy ( name , translated_path ) ;
2003-07-03 00:01:51 +04:00
} else {
sp = strnrchr_m ( name , ' / ' , num_components ) ;
2003-07-08 00:22:35 +04:00
if ( sp ) {
pstring last_component ;
pstrcpy ( last_component , sp ) ;
2004-08-25 02:48:49 +04:00
pstrcpy ( name , translated_path ) ;
2003-07-08 00:22:35 +04:00
pstrcat ( name , last_component ) ;
} else {
2004-08-25 02:48:49 +04:00
pstrcpy ( name , translated_path ) ;
2003-07-03 00:01:51 +04:00
}
}
2003-07-02 04:08:29 +04:00
/* set pointer for 'where to start' on fixing the rest of the name */
2004-08-25 02:48:49 +04:00
* start = & name [ translated_path_length ] ;
2003-07-02 04:08:29 +04:00
if ( * * start = = ' / ' )
+ + * start ;
2004-08-25 02:48:49 +04:00
pstrcpy ( dirpath , translated_path ) ;
retval = ( namelen = = translated_path_length ) ? True : False ;
2003-07-27 07:40:45 +04:00
SAFE_FREE ( chk_name ) ;
2004-08-25 02:48:49 +04:00
SAFE_FREE ( data_val . dptr ) ;
return retval ;
2003-07-02 04:08:29 +04:00
}
}
2000-04-30 15:04:28 +04:00
}
2007-01-20 00:46:12 +03:00
/***************************************************************************
Tell all smbd ' s to delete an entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void send_stat_cache_delete_message ( const char * name )
{
# ifdef DEVELOPER
2007-05-15 19:49:55 +04:00
message_send_all ( smbd_messaging_context ( ) ,
MSG_SMB_STAT_CACHE_DELETE ,
2007-01-20 00:46:12 +03:00
name ,
strlen ( name ) + 1 ,
NULL ) ;
# endif
}
/***************************************************************************
Delete an entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void stat_cache_delete ( const char * name )
{
char * lname = strdup_upper ( name ) ;
if ( ! lname ) {
return ;
}
DEBUG ( 10 , ( " stat_cache_delete: deleting name [%s] -> %s \n " ,
lname , name ) ) ;
tdb_delete_bystring ( tdb_stat_cache , lname ) ;
SAFE_FREE ( lname ) ;
}
2004-08-25 05:04:02 +04:00
/***************************************************************
Compute a hash value based on a string key value .
The function returns the bucket index number for the hashed key .
JRA . Use a djb - algorithm hash for speed .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-25 02:48:49 +04:00
2006-07-11 22:01:26 +04:00
unsigned int fast_string_hash ( TDB_DATA * key )
2004-08-25 02:48:49 +04:00
{
2006-07-11 22:01:26 +04:00
unsigned int n = 0 ;
2004-08-25 02:48:49 +04:00
const char * p ;
2007-03-29 13:35:51 +04:00
for ( p = ( const char * ) key - > dptr ; * p ! = ' \0 ' ; p + + ) {
2006-07-11 22:01:26 +04:00
n = ( ( n < < 5 ) + n ) ^ ( unsigned int ) ( * p ) ;
2004-08-25 02:48:49 +04:00
}
return n ;
}
2004-08-25 05:04:02 +04:00
/***************************************************************************
Initializes or clears the stat cache .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-30 15:04:28 +04:00
BOOL reset_stat_cache ( void )
{
2003-07-02 04:08:29 +04:00
if ( ! lp_stat_cache ( ) )
return True ;
2000-05-04 10:28:38 +04:00
2004-08-25 02:48:49 +04:00
if ( tdb_stat_cache ) {
tdb_close ( tdb_stat_cache ) ;
2000-05-02 11:10:54 +04:00
}
2000-10-05 02:37:33 +04:00
2004-08-25 05:04:02 +04:00
/* Create the in-memory tdb using our custom hash function. */
2004-08-31 02:25:36 +04:00
tdb_stat_cache = tdb_open_ex ( " statcache " , 1031 , TDB_INTERNAL ,
2004-08-31 00:19:40 +04:00
( O_RDWR | O_CREAT ) , 0644 , NULL , fast_string_hash ) ;
2004-08-25 05:04:02 +04:00
2004-08-25 02:48:49 +04:00
if ( ! tdb_stat_cache )
return False ;
return True ;
2003-07-02 04:08:29 +04:00
}