Use VFS I/O for stat_cache_lookup()
[samba.git] / source3 / smbd / filename.c
index 0910e03ee3409c7c6cc071bb7273cd4727212983..fb2f57339b4b68724bb42cc5f99e3718342767e7 100644 (file)
@@ -114,27 +114,18 @@ typedef struct {
 
 static ubi_dlList stat_cache = { NULL, (ubi_dlNodePtr)&stat_cache, 0};
 
-/****************************************************************************
- Compare two names in the stat cache - to check if we already have such an
- entry.
-*****************************************************************************/
-
-static BOOL stat_name_equal( char *s1, char *s2)
-{
-  return (case_sensitive ? (strcmp( s1, s2) == 0) : (StrCaseCmp(s1, s2) == 0));
-}
-
 /****************************************************************************
  Compare a pathname to a name in the stat cache - of a given length.
  Note - this code always checks that the next character in the pathname
  is either a '/' character, or a '\0' character - to ensure we only
- match *full* pathname components.
+ match *full* pathname components. Note we don't need to handle case
+ here, if we're case insensitive the stat cache orig names are all upper
+ case.
 *****************************************************************************/
 
 static BOOL stat_name_equal_len( char *stat_name, char *orig_name, int len)
 {
-  BOOL matched = (case_sensitive ? (strncmp( stat_name, orig_name, len) == 0) :
-                           (StrnCaseCmp(stat_name, orig_name, len) == 0));
+  BOOL matched = (memcmp( stat_name, orig_name, len) == 0);
   if(orig_name[len] != '/' && orig_name[len] != '\0')
     return False;
 
@@ -159,7 +150,8 @@ static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
   /*
    * Don't cache trivial valid directory entries.
    */
-  if(strequal(full_orig_name, ".") || strequal(full_orig_name, ".."))
+  if((*full_orig_name == '\0') || (strcmp(full_orig_name, ".") == 0) ||
+     (strcmp(full_orig_name, "..") == 0))
     return;
 
   /*
@@ -189,6 +181,8 @@ static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
    */
 
   StrnCpy(orig_name, full_orig_name, namelen);
+  if(!case_sensitive)
+    strupper( orig_name );
 
   /*
    * Check this name doesn't exist in the cache before we 
@@ -197,7 +191,7 @@ static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
 
   for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp; 
                         scp = (stat_cache_entry *)ubi_dlNext( scp )) {
-    if(stat_name_equal( scp->orig_name, orig_name) &&
+    if((strcmp( scp->orig_name, orig_name) == 0) &&
        (strcmp( scp->translated_name, translated_path) == 0)) {
       /*
        * Name does exist - promote it.
@@ -235,10 +229,13 @@ static void stat_cache_add( char *full_orig_name, char *orig_translated_path)
  Return True if we translated (and did a scuccessful stat on) the entire name.
 *****************************************************************************/
 
-static BOOL stat_cache_lookup( char *name, char *dirpath, char **start, SMB_STRUCT_STAT *pst)
+static BOOL stat_cache_lookup(struct connection_struct *conn, char *name, 
+                             char *dirpath, char **start, 
+                             SMB_STRUCT_STAT *pst)
 {
   stat_cache_entry *scp;
   stat_cache_entry *longest_hit = NULL;
+  pstring chk_name;
   int namelen;
 
   if (!lp_stat_cache()) return False;
@@ -251,15 +248,19 @@ static BOOL stat_cache_lookup( char *name, char *dirpath, char **start, SMB_STRU
   /*
    * Don't lookup trivial valid directory entries.
    */
-  if(strequal(name, ".") || strequal(name, "..")) {
+  if((*name == '\0') || (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0)) {
     global_stat_cache_misses++;
     return False;
   }
 
+  pstrcpy(chk_name, name);
+  if(!case_sensitive)
+    strupper( chk_name );
+
   for( scp = (stat_cache_entry *)ubi_dlFirst( &stat_cache); scp; 
                         scp = (stat_cache_entry *)ubi_dlNext( scp )) {
     if(scp->name_len <= namelen) {
-      if(stat_name_equal_len(scp->orig_name, name, scp->name_len)) {
+      if(stat_name_equal_len(scp->orig_name, chk_name, scp->name_len)) {
         if((longest_hit == NULL) || (longest_hit->name_len <= scp->name_len))
           longest_hit = scp;
       }
@@ -283,7 +284,7 @@ static BOOL stat_cache_lookup( char *name, char *dirpath, char **start, SMB_STRU
    * and then promote it to the top.
    */
 
-  if(dos_stat( longest_hit->translated_name, pst) != 0) {
+  if(conn->vfs_ops.stat(longest_hit->translated_name, pst) != 0) {
     /*
      * Discard this entry.
      */
@@ -333,7 +334,7 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
                   BOOL *bad_path, SMB_STRUCT_STAT *pst)
 {
   SMB_STRUCT_STAT st;
-  char *start, *end, *orig_start;
+  char *start, *end;
   pstring dirpath;
   pstring orig_path;
   int saved_errno;
@@ -344,6 +345,8 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
   extern char magic_char;
 #endif
 
+  DEBUG(5, ("unix_convert called on file \"%s\"\n", name));
+
   *dirpath = 0;
   *bad_path = False;
   if(pst) {
@@ -367,6 +370,18 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
 
   trim_string(name,"/","/");
 
+  /*
+   * If we trimmed down to a single '\0' character
+   * then we should use the "." directory to avoid
+   * searching the cache, but not if we are in a
+   * printing share.
+   */
+
+  if (!*name && (!conn -> printer)) {
+    name[0] = '.';
+    name[1] = '\0';
+  }
+
   /*
    * Ensure saved_last_component is valid even if file exists.
    */
@@ -398,18 +413,27 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
 
       for (s=name2 ; *s ; s++)
         if (!issafe(*s)) *s = '_';
-          pstrcpy(name,(char *)mktemp(name2));   
+      pstrcpy(name,(char *)mktemp(name2));       
     }      
     return(True);
   }
 
+  /*
+   * If we trimmed down to a single '\0' character
+   * then we will be using the "." directory.
+   * As we know this is valid we can return true here.
+   */
+
+  if(!*name)
+    return(True);
+
   start = name;
   while (strncmp(start,"./",2) == 0)
     start += 2;
 
   pstrcpy(orig_path, name);
 
-  if(stat_cache_lookup( name, dirpath, &start, &st)) {
+  if(stat_cache_lookup(conn, name, dirpath, &start, &st)) {
     if(pst)
       *pst = st;
     return True;
@@ -419,7 +443,7 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
    * stat the name - if it exists then we are all done!
    */
 
-  if (dos_stat(name,&st) == 0) {
+  if (conn->vfs_ops.stat(name,&st) == 0) {
     stat_cache_add(orig_path, name);
     DEBUG(5,("conversion finished %s -> %s\n",orig_path, name));
     if(pst)
@@ -469,7 +493,7 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
    * as is first, then trying to scan the directory for matching names.
    */
 
-  for (orig_start = start; start ; start = (end?end+1:(char *)NULL)) {
+  for (; start ; start = (end?end+1:(char *)NULL)) {
       /* 
        * Pinpoint the end of this section of the filename.
        */
@@ -487,7 +511,8 @@ BOOL unix_convert(char *name,connection_struct *conn,char *saved_last_component,
       /* 
        * Check if the name exists up to this point.
        */
-      if (dos_stat(name, &st) == 0) {
+
+      if (conn->vfs_ops.stat(name, &st) == 0) {
         /*
          * It exists. it must either be a directory or this must be
          * the last part of the path for it to be OK.
@@ -639,7 +664,7 @@ BOOL check_name(char *name,connection_struct *conn)
   if (!lp_symlinks(SNUM(conn)))
     {
       SMB_STRUCT_STAT statbuf;
-      if ( (dos_lstat(name,&statbuf) != -1) &&
+      if ( (conn->vfs_ops.lstat(dos_to_unix(name,False),&statbuf) != -1) &&
           (S_ISLNK(statbuf.st_mode)) )
         {
           DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));