]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/directory.c
don't store in struct volume pointers to charset function table, we only need texten...
[netatalk.git] / etc / afpd / directory.c
index bd6542f739350476268996c82dca7021e662ff33..cd64283f43aea797385df38722f6ee1a9d7e8be2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: directory.c,v 1.99 2009-07-20 18:31:04 didg Exp $
+ * $Id: directory.c,v 1.116 2009-10-29 11:35:58 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -31,20 +31,21 @@ char *strchr (), *strrchr ();
 #endif
 #include <stdio.h>
 #include <stdlib.h>
-#include <dirent.h>
 
 #include <grp.h>
 #include <pwd.h>
 #include <sys/param.h>
 #include <errno.h>
 #include <utime.h>
-#include <atalk/adouble.h>
 
+#include <atalk/adouble.h>
+#include <atalk/vfs.h>
 #include <atalk/afp.h>
 #include <atalk/util.h>
 #include <atalk/cnid.h>
 #include <atalk/logger.h>
 #include <atalk/uuid.h>
+#include <atalk/unix.h>
 
 #include "directory.h"
 #include "desktop.h"
@@ -55,6 +56,7 @@ char *strchr (), *strrchr ();
 #include "globals.h"
 #include "unix.h"
 #include "mangle.h"
+#include "hash.h"
 
 #ifdef HAVE_NFSv4_ACLS
 extern void addir_inherit_acl(const struct vol *vol);
@@ -93,9 +95,7 @@ static struct dir rootpar  = { SENTINEL, SENTINEL, NULL, 0,
  * how exciting.
  */
 struct dir *
-            dirsearch( vol, did )
-            const struct vol   *vol;
-u_int32_t      did;
+            dirsearch(const struct vol *vol, u_int32_t did)
 {
     struct dir *dir;
 
@@ -132,18 +132,6 @@ u_int32_t  did;
 }
 
 /* ------------------- */
-#ifdef ATACC
-int path_isadir(struct path *o_path)
-{
-    return o_path->d_dir != NULL;
-#if 0
-    return o_path->m_name == '\0' || /* we are in a it */
-           !o_path->st_valid ||      /* in cache but we can't chdir in it */ 
-           (!o_path->st_errno && S_ISDIR(o_path->st.st_mode)); /* not in cache an can't chdir */
-#endif
-}
-#endif
-
 int get_afp_errno(const int param)
 {
     if (afp_errno != AFPERR_DID1)
@@ -179,9 +167,7 @@ struct dir *dir = NULL;
  * and we are really bad in this case.
  */
 struct dir *
-            dirlookup( vol, did )
-            const struct vol   *vol;
-u_int32_t      did;
+            dirlookup( struct vol *vol, u_int32_t did)
 {
     struct dir   *ret;
     char        *upath;
@@ -295,9 +281,7 @@ static void dirchildremove(struct dir *a,struct dir *b)
 
 /* --------------------------- */
 /* rotate the tree to the left */
-static void dir_leftrotate(vol, dir)
-struct vol *vol;
-struct dir *dir;
+static void dir_leftrotate(struct vol *vol, struct dir *dir)
 {
     struct dir *right = dir->d_right;
 
@@ -326,9 +310,7 @@ struct dir *dir;
 
 
 /* rotate the tree to the right */
-static void dir_rightrotate(vol, dir)
-struct vol *vol;
-struct dir *dir;
+static void dir_rightrotate(struct vol *vol, struct dir *dir)
 {
     struct dir *left = dir->d_left;
 
@@ -356,9 +338,7 @@ struct dir *dir;
 
 #if 0
 /* recolor after a removal */
-static struct dir *dir_rmrecolor(vol, dir)
-            struct vol *vol;
-struct dir *dir;
+static struct dir *dir_rmrecolor(struct vol *vol, struct dir *dir)
 {
     struct dir *leaf;
 
@@ -562,9 +542,7 @@ static void dir_remove( const struct vol *vol _U_, struct dir       *dir)
  * process. It's fixable within afpd if fnctl_lock, doable with smb and
  * next to impossible for nfs and local filesystem access.
  */
-static void dir_invalidate( vol, dir )
-const struct vol *vol;
-struct dir *dir;
+static void dir_invalidate( const struct vol *vol, struct dir *dir)
 {
     if (curdir == dir) {
         /* v_root can't be deleted */
@@ -578,9 +556,7 @@ struct dir *dir;
 }
 
 /* ------------------------------------ */
-static struct dir *dir_insert(vol, dir)
-            const struct vol *vol;
-struct dir *dir;
+static struct dir *dir_insert(const struct vol *vol, struct dir *dir)
 {
     struct dir *pdir;
 
@@ -646,7 +622,7 @@ caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
 
 
     /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
-    if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, strlen(path->u_name), u2_path, sizeof(u2_path)) ) 
+    if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) ) 
         LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
 
     /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
@@ -655,7 +631,7 @@ caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
         if (NULL == check_dirent(vol, de->d_name))
             continue;
 
-        if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, strlen(de->d_name), u2_dename, sizeof(u2_dename)) )
+        if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
             continue;
 
         if (strcasecmp_w( u2_path, u2_dename) == 0) {
@@ -693,10 +669,7 @@ caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
  * as a side-effect, movecwd to that point and return the new dir
  */
 static struct dir *
-            extenddir( vol, dir, path )
-struct vol     *vol;
-struct dir     *dir;
-struct path *path;
+            extenddir(struct vol *vol, struct dir *dir, struct path *path)
 {
     path->d_dir = NULL;
 
@@ -707,6 +680,8 @@ struct path *path;
 
     if (check_name(vol, path->u_name)) {
         /* the name is illegal */
+        LOG(log_info, logtype_afpd, "extenddir: illegal path: '%s'", path->u_name);
+        path->u_name = NULL;
         afp_errno = AFPERR_PARAM;
         return NULL;
     }
@@ -735,30 +710,6 @@ struct path *path;
     return( dir );
 }
 
-/* -------------------
-   system rmdir with afp error code.
-   ENOENT is not an error.
- */
-int netatalk_rmdir(const char *name)
-{
-    if (rmdir(name) < 0) {
-        switch ( errno ) {
-        case ENOENT :
-            break;
-        case ENOTEMPTY : 
-            return AFPERR_DIRNEMPT;
-        case EPERM:
-        case EACCES :
-            return AFPERR_ACCESS;
-        case EROFS:
-            return AFPERR_VLOCK;
-        default :
-            return AFPERR_PARAM;
-        }
-    }
-    return AFP_OK;
-}
-
 /* -------------------------
    appledouble mkdir afp error code.
 */
@@ -785,28 +736,6 @@ static int netatalk_mkdir(const char *name)
     return AFP_OK;
 }
 
-/* -------------------
-   system unlink with afp error code.
-   ENOENT is not an error.
- */
-int netatalk_unlink(const char *name)
-{
-    if (unlink(name) < 0) {
-        switch (errno) {
-        case ENOENT :
-            break;
-        case EROFS:
-            return AFPERR_VLOCK;
-        case EPERM:
-        case EACCES :
-            return AFPERR_ACCESS;
-        default :
-            return AFPERR_PARAM;
-        }
-    }
-    return AFP_OK;
-}
-
 /* ------------------- */
 static int deletedir(char *dir)
 {
@@ -941,9 +870,7 @@ copydir_done:
 /* --- public functions follow --- */
 
 /* NOTE: we start off with at least one node (the root directory). */
-static struct dir *dirinsert( vol, dir )
-            struct vol *vol;
-struct dir     *dir;
+static struct dir *dirinsert(struct vol *vol, struct dir *dir)
 {
     struct dir *node;
 
@@ -1001,10 +928,7 @@ struct dir        *dir;
 
 /* ---------------------------- */
 struct dir *
-            adddir( vol, dir, path)
-struct vol     *vol;
-struct dir     *dir;
-struct path     *path;
+            adddir(struct vol *vol, struct dir *dir, struct path *path)
 {
     struct dir *cdir, *edir;
     int                upathlen;
@@ -1030,9 +954,9 @@ struct path     *path;
         LOG(log_error, logtype_afpd, "adddir: malloc: %s", strerror(errno) );
         return NULL;
     }
-    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, path->m_name, strlen(path->m_name), &cdir->d_m_name_ucs2)) {
-       LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
-       cdir->d_m_name_ucs2 = NULL;
+    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, path->m_name, -1, (char **)&cdir->d_m_name_ucs2)) {
+        LOG(log_error, logtype_afpd, "Couldn't set UCS2 name for %s", name);
+        cdir->d_m_name_ucs2 = NULL;
     }
 
     cdir->d_did = id;
@@ -1088,8 +1012,7 @@ void dirfreename(struct dir *dir)
     free(dir->d_m_name);
 }
 
-void dirfree( dir )
-struct dir     *dir;
+void dirfree(struct dir *dir)
 {
     if (!dir || (dir == SENTINEL))
         return;
@@ -1150,8 +1073,8 @@ const struct dir *k = key;
        0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
     };
 
-    const unsigned char *str = k->d_u_name;
-    hash_val_t acc = 0;
+    const unsigned char *str = (unsigned char *)(k->d_u_name);
+    hash_val_t acc = k->d_parent->d_did;
 
     while (*str) {
        acc ^= randbox[(*str + acc) & 0xf];
@@ -1261,10 +1184,7 @@ static struct path *invalidate (const struct vol *vol, struct dir *dir, struct p
                  
 */
 struct path *
-cname( vol, dir, cpath )
-const struct vol       *vol;
-struct dir     *dir;
-char   **cpath;
+cname(struct vol *vol, struct dir *dir, char **cpath)
 {
     struct dir            *cdir, *scdir=NULL;
     static char                   path[ MAXPATHLEN + 1];
@@ -1415,7 +1335,7 @@ char      **cpath;
             scdir = NULL;
            if ( cdir && (vol->v_flags & AFPVOL_CASEINSEN) &&
                     (size_t)-1 != convert_string_allocate(((ret.m_type == 3)?CH_UTF8_MAC:vol->v_maccharset), 
-                                                          CH_UCS2, path, strlen(path), (char **)&tmpname) )
+                                                          CH_UCS2, path, -1, (char **)&tmpname) )
             {
                 while (cdir) {
                     if (!cdir->d_m_name_ucs2) {
@@ -1484,7 +1404,7 @@ noucsfallback:
 
         if ( cdir == NULL ) {
 
-            if ( len > 0) {
+            if ( len > 0 || !ret.u_name ) {
                 return NULL;
             }
 
@@ -1498,9 +1418,7 @@ noucsfallback:
 /*
  * Move curdir to dir, with a possible chdir()
  */
-int movecwd( vol, dir)
-const struct vol       *vol;
-struct dir     *dir;
+int movecwd(const struct vol *vol, struct dir *dir)
 {
     char path[MAXPATHLEN + 1];
     struct dir *d;
@@ -1612,7 +1530,7 @@ void setdiroffcnt(struct dir *dir, struct stat *st,  u_int32_t count)
  * is our cached offspring count valid?
 */
 
-int diroffcnt(struct dir *dir, struct stat *st)
+static int diroffcnt(struct dir *dir, struct stat *st)
 {
     return st->st_ctime == dir->ctime;
 }
@@ -1640,7 +1558,7 @@ static int invisible_dots(const struct vol *vol, const char *name)
 int getdirparams(const struct vol *vol,
                  u_int16_t bitmap, struct path *s_path,
                  struct dir *dir, 
-                 char *buf, int *buflen )
+                 char *buf, size_t *buflen )
 {
     struct maccess     ma;
     struct adouble     ad;
@@ -1879,10 +1797,7 @@ int path_error(struct path *path, int error)
 }
 
 /* ----------------------------- */
-int afp_setdirparams(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj;
-char   *ibuf, *rbuf _U_;
-int    ibuflen _U_, *rbuflen;
+int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
     struct vol *vol;
     struct dir *dir;
@@ -2300,11 +2215,7 @@ setdirparam_done:
     return err;
 }
 
-
-int afp_syncdir(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char    *ibuf, *rbuf _U_;
-int     ibuflen _U_, *rbuflen;
+int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
 #ifdef HAVE_DIRFD
     DIR                  *dp;
@@ -2326,65 +2237,75 @@ int     ibuflen _U_, *rbuflen;
 
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( did );
-    if (NULL == ( dir = dirlookup( vol, did )) ) {
-        return afp_errno; /* was AFPERR_NOOBJ */
-    }
 
-    if (movecwd( vol, dir ) < 0 )
-        return ( AFPERR_NOOBJ ); 
+    /* 
+     * Here's the deal:
+     * if it's CNID 2 our only choice to meet the specs is call sync.
+     * For any other CNID just sync that dir. To my knowledge the
+     * intended use of FPSyncDir is to sync the volume so all we're
+     * ever going to see here is probably CNID 2. Anyway, we' prepared.
+    */
 
-/*
-  Assuming only OSens that have dirfd also may require fsyncing directories
-  in order to flush metadata e.g. Linux.
-*/
+    if ( ntohl(did) == 2 ) {
+        sync();
+    } else {
+        if (NULL == ( dir = dirlookup( vol, did )) ) {
+            return afp_errno; /* was AFPERR_NOOBJ */
+        }
+        
+        if (movecwd( vol, dir ) < 0 )
+            return ( AFPERR_NOOBJ ); 
+        
+        /*
+         * Assuming only OSens that have dirfd also may require fsyncing directories
+         * in order to flush metadata e.g. Linux.
+         */
     
 #ifdef HAVE_DIRFD
-    if (NULL == ( dp = opendir( "." )) ) {
-        switch( errno ) {
-        case ENOENT :
-            return( AFPERR_NOOBJ );
-        case EACCES :
+        if (NULL == ( dp = opendir( "." )) ) {
+            switch( errno ) {
+            case ENOENT :
+                return( AFPERR_NOOBJ );
+            case EACCES :
             return( AFPERR_ACCESS );
-        default :
-            return( AFPERR_PARAM );
+            default :
+                return( AFPERR_PARAM );
+            }
         }
-    }
-
-    LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
-
-    dfd = dirfd( dp );
-    if ( fsync ( dfd ) < 0 )
-        LOG(log_error, logtype_afpd, "afp_syncdir(%s):  %s",
-            dir->d_u_name, strerror(errno) );
-    closedir(dp); /* closes dfd too */
+        
+        LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
+        
+        dfd = dirfd( dp );
+        if ( fsync ( dfd ) < 0 )
+            LOG(log_error, logtype_afpd, "afp_syncdir(%s):  %s",
+                dir->d_u_name, strerror(errno) );
+        closedir(dp); /* closes dfd too */
 #endif
-
-    if ( -1 == (dfd = open(vol->vfs->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
-        switch( errno ) {
-        case ENOENT:
-            return( AFPERR_NOOBJ );
+        
+        if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
+            switch( errno ) {
+            case ENOENT:
+                return( AFPERR_NOOBJ );
         case EACCES:
             return( AFPERR_ACCESS );
-        default:
-            return( AFPERR_PARAM );
-        }        
+            default:
+                return( AFPERR_PARAM );
+            }        
+        }
+        
+        LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
+            vol->ad_path(".", ADFLAGS_DIR) );
+        
+        if ( fsync(dfd) < 0 )
+            LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
+                vol->ad_path(dir->d_u_name, ADFLAGS_DIR), strerror(errno) );
+        close(dfd);
     }
 
-    LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
-        vol->vfs->ad_path(".", ADFLAGS_DIR) );
-    
-    if ( fsync(dfd) < 0 )
-        LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
-            vol->vfs->ad_path(dir->d_u_name, ADFLAGS_DIR), strerror(errno) );
-    close(dfd);
-
     return ( AFP_OK );
 }
 
-int afp_createdir(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj;
-char   *ibuf, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     struct adouble     ad;
     struct vol         *vol;
@@ -2472,10 +2393,10 @@ createdir_done:
  * newparent curdir
  *
 */
-int renamedir(vol, src, dst, dir, newparent, newname)
-const struct vol *vol;
-char   *src, *dst, *newname;
-struct dir     *dir, *newparent;
+int renamedir(const struct vol *vol, char *src, char *dst, 
+    struct dir *dir, 
+    struct dir *newparent, 
+    char *newname)
 {
     struct adouble     ad;
     struct dir         *parent;
@@ -2509,7 +2430,7 @@ struct dir        *dir, *newparent;
         }
     }
 
-    vol->vfs->rf_renamedir(vol, src, dst);
+    vol->vfs->vfs_renamedir(vol, src, dst);
 
     len = strlen( newname );
     /* rename() succeeded so we need to update our tree even if we can't open
@@ -2553,7 +2474,7 @@ struct dir        *dir, *newparent;
        free(dir->d_m_name_ucs2);
 
     dir->d_m_name_ucs2 = NULL;
-    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, strlen(dir->d_m_name), (char**)&dir->d_m_name_ucs2))
+    if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, -1, (char**)&dir->d_m_name_ucs2))
         dir->d_m_name_ucs2 = NULL;
 
     if (( parent = dir->d_parent ) == NULL ) {
@@ -2572,8 +2493,7 @@ struct dir        *dir, *newparent;
 }
 
 /* delete an empty directory */
-int deletecurdir( vol)
-const struct vol       *vol;
+int deletecurdir(const struct vol *vol)
 {
     struct dirent *de;
     struct stat st;
@@ -2598,7 +2518,7 @@ const struct vol  *vol;
             return  AFPERR_OLOCK;
         }
     }
-    err = vol->vfs->rf_deletecurdir(vol);
+    err = vol->vfs->vfs_deletecurdir(vol);
     if (err) {
         return err;
     }
@@ -2645,10 +2565,7 @@ delete_done:
     return err;
 }
 
-int afp_mapid(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj;
-char   *ibuf, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     struct passwd      *pw;
     struct group       *gr;
@@ -2656,8 +2573,7 @@ int       ibuflen _U_, *rbuflen;
     u_int32_t           id;
     int                        len, sfunc;
     int         utf8 = 0;
-    uuidtype_t          type;
-
+    
     ibuf++;
     sfunc = (unsigned char) *ibuf++;
     *rbuflen = 0;
@@ -2680,7 +2596,7 @@ int       ibuflen _U_, *rbuflen;
                 return( AFPERR_NOITEM );
             }
            len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
-                                            pw->pw_name, strlen(pw->pw_name), &name);
+                                            pw->pw_name, -1, &name);
        } else {
            len = 0;
            name = NULL;
@@ -2695,7 +2611,7 @@ int       ibuflen _U_, *rbuflen;
                 return( AFPERR_NOITEM );
             }
            len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
-                                            gr->gr_name, strlen(gr->gr_name), &name);
+                                            gr->gr_name, -1, &name);
        } else {
            len = 0;
            name = NULL;
@@ -2761,10 +2677,7 @@ int      ibuflen _U_, *rbuflen;
     return( AFP_OK );
 }
 
-int afp_mapname(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf, *rbuf;
-int    ibuflen _U_, *rbuflen;
+int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     struct passwd      *pw;
     struct group       *gr;
@@ -2856,10 +2769,7 @@ int      ibuflen _U_, *rbuflen;
 /* ------------------------------------
   variable DID support 
 */
-int afp_closedir(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf _U_, *rbuf _U_;
-int    ibuflen _U_, *rbuflen;
+int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
 #if 0
     struct vol   *vol;
@@ -2895,10 +2805,7 @@ int      ibuflen _U_, *rbuflen;
 /* did creation gets done automatically 
  * there's a pb again with case but move it to cname
 */
-int afp_opendir(obj, ibuf, ibuflen, rbuf, rbuflen )
-AFPObj  *obj _U_;
-char   *ibuf, *rbuf;
-int    ibuflen  _U_, *rbuflen;
+int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen  _U_, char *rbuf, size_t *rbuflen)
 {
     struct vol         *vol;
     struct dir         *parentdir;