]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/filedir.c
better error code for calls on a not existing file
[netatalk.git] / etc / afpd / filedir.c
index 8b4177cf4573e1c97e97f9b6cd7c6f3c2fbdd5c5..d2400262db06a3ce4ea8d83e913b9bfcef1b6036 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: filedir.c,v 1.39 2003-01-21 09:58:58 didg Exp $
+ * $Id: filedir.c,v 1.45 2003-04-20 06:53:40 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -178,7 +178,7 @@ int         ibuflen, *rbuflen;
     ibuf += sizeof( dbitmap );
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno;
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     st   = &s_path->st;
@@ -197,12 +197,11 @@ int               ibuflen, *rbuflen;
     buflen = 0;
     if (S_ISDIR(st->st_mode)) {
         if (dbitmap) {
-            if (*s_path->m_name != '\0') {
-                /* the dir wasn't in the cache and we weren't able to chdir in it.
-                */
-                return AFPERR_ACCESS;
-            }
-            ret = getdirparams(vol, dbitmap, s_path, curdir,
+            dir = s_path->dir;
+            if (!dir) 
+                return AFPERR_NOOBJ;
+
+            ret = getdirparams(vol, dbitmap, s_path, dir,
                                  rbuf + 3 * sizeof( u_int16_t ), &buflen );
             if (ret != AFP_OK )
                 return( ret );
@@ -210,8 +209,8 @@ int         ibuflen, *rbuflen;
         /* this is a directory */
         *(rbuf + 2 * sizeof( u_int16_t )) = (char) FILDIRBIT_ISDIR;
     } else {
-        if (fbitmap && ret = getfilparams(vol, fbitmap, s_path, curdir, 
-                                            rbuf + 3 * sizeof( u_int16_t ), &buflen )) != AFP_OK ) {
+        if (fbitmap && AFP_OK != (ret = getfilparams(vol, fbitmap, s_path, curdir, 
+                                            rbuf + 3 * sizeof( u_int16_t ), &buflen )) ) {
             return( ret );
         }
         /* this is a file */
@@ -264,7 +263,7 @@ int         ibuflen, *rbuflen;
     memcpy( &did, ibuf, sizeof( did));
     ibuf += sizeof( did);
 
-    if (( dir = dirlookup( vol, did )) == NULL ) {
+    if (NULL == ( dir = dirlookup( vol, did )) ) {
        return afp_errno;    
     }
 
@@ -272,8 +271,8 @@ int         ibuflen, *rbuflen;
     bitmap = ntohs( bitmap );
     ibuf += sizeof( bitmap );
 
-    if (( path = cname( vol, dir, &ibuf )) == NULL ) {
-       return afp_errno;    
+    if (NULL == ( path = cname( vol, dir, &ibuf ))) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     st   = &path->st;
@@ -362,10 +361,16 @@ int         isdir;
     
     if (!isdir) {
 #ifdef CNID_DB
-        p = mtoupath(vol, oldname);
+        p = mtoupath(vol, oldname, utf8_encoding());
+        if (!p) { 
+            return AFPERR_PARAM; /* can't convert */
+        }
         id = cnid_get(vol->v_db, sdir->d_did, p, strlen(p));
 #endif /* CNID_DB */
         p = ctoupath( vol, sdir, oldname );
+        if (!p) { 
+            return AFPERR_PARAM; /* pathname too long */
+        }
         path.st_valid = 0;
         path.u_name = p;
         if ((opened = of_findname(&path))) {
@@ -378,6 +383,9 @@ int         isdir;
         id = sdir->d_did; /* we already have the CNID */
 #endif /* CNID_DB */
         p = ctoupath( vol, sdir->d_parent, oldname );
+        if (!p) {
+            return AFPERR_PARAM;
+        }
         adflags = ADFLAGS_DIR;
     }
     /*
@@ -395,7 +403,9 @@ int         isdir;
             return(AFPERR_OLOCK);
     }
 
-    upath = mtoupath(vol, newname);
+    if (NULL == (upath = mtoupath(vol, newname, utf8_encoding()))){ 
+        return AFPERR_PARAM;
+    }
     path.u_name = upath;
     st = &path.st;    
     if (0 != (rc = check_name(vol, upath))) {
@@ -479,15 +489,20 @@ int               ibuflen, *rbuflen;
     }
 
     /* source pathname */
-    if (( path = cname( vol, sdir, &ibuf )) == NULL ) {
-       return afp_errno;    
+    if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     sdir = curdir;
     newname = obj->newtmp;
     oldname = obj->oldtmp;
+    isdir = path_isadir(path);
     if ( *path->m_name != '\0' ) {
         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
+        if (isdir) {
+            /* curdir parent dir, need to move sdir back */
+            sdir = path->dir;
+        }
     }
     else {
         if ( sdir->d_parent == NULL ) { /* root directory */
@@ -497,7 +512,6 @@ int         ibuflen, *rbuflen;
         if ( movecwd( vol, sdir->d_parent ) < 0 ) {
             return afp_errno;
         }
-        isdir = 1;
         strcpy(oldname, sdir->d_m_name);
     }
 
@@ -559,19 +573,21 @@ int               ibuflen, *rbuflen;
     }
 
     if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
-        return afp_errno;
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     upath = s_path->u_name;
-    if ( *s_path->m_name == '\0' ) {
-        rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
+    if ( path_isadir( s_path) ) {
+       if (*s_path->m_name != '\0') {
+           rc = AFPERR_ACCESS;
+       }
+       else {
+            rc = deletecurdir( vol, obj->oldtmp, AFPOBJ_TMPSIZ);
+        }
     } else if (of_findname(s_path)) {
         rc = AFPERR_BUSY;
-    } else if (AFP_OK == (rc = deletefile( upath, 1))) {
-#ifdef CNID_DB /* get rid of entry */
-        cnid_t id = cnid_get(vol->v_db, curdir->d_did, upath, strlen(upath));
-        cnid_delete(vol->v_db, id);
-#endif /* CNID_DB */
+    } else {
+        rc = deletefile(vol, upath, 1);
     }
     if ( rc == AFP_OK ) {
        curdir->offcnt--;
@@ -594,20 +610,32 @@ char      *u;
     char       *p;
     int                len;
 
+    if (u == NULL)
+        return NULL;
+        
     p = path + sizeof( path ) - 1;
     *p = '\0';
     len = strlen( u );
     p -= len;
     strncpy( p, u, len );
-    for ( d = dir; d->d_parent; d = d->d_parent ) {
-        *--p = '/';
+    if (dir) for ( d = dir; d->d_parent; d = d->d_parent ) {
         u = d->d_u_name;
         len = strlen( u );
+        if (p -len -1 < path) {
+            /* FIXME 
+               rather rare so LOG error and/or client message ?
+            */
+            return NULL;
+        }
+        *--p = '/';
         p -= len;
         strncpy( p, u, len );
     }
-    *--p = '/';
     len = strlen( vol->v_path );
+    if (p -len -1 < path) {
+        return NULL;
+    }
+    *--p = '/';
     p -= len;
     strncpy( p, vol->v_path, len );
 
@@ -619,7 +647,7 @@ const struct vol    *vol;
 struct dir     *dir;
 char   *name;
 {
-    return absupath(vol, dir, mtoupath(vol, name));
+    return absupath(vol, dir, mtoupath(vol, name, utf8_encoding()));
 }
 
 /* ------------------------- */
@@ -630,7 +658,7 @@ int         ibuflen, *rbuflen;
 {
     struct vol *vol;
     struct dir *sdir, *ddir;
-    int         isdir = 0;
+    int         isdir;
     char       *oldname, *newname;
     struct path *path;
     int                did;
@@ -661,7 +689,7 @@ int         ibuflen, *rbuflen;
     memcpy( &did, ibuf, sizeof( did ));
     ibuf += sizeof( int );
     if (NULL == ( sdir = dirlookup( vol, did )) ) {
-        return( AFPERR_PARAM );
+        return afp_errno; /* was AFPERR_PARAM */
     }
 
     memcpy( &did, ibuf, sizeof( did ));
@@ -669,29 +697,32 @@ int               ibuflen, *rbuflen;
 
     /* source pathname */
     if (NULL == ( path = cname( vol, sdir, &ibuf )) ) {
-        return( AFPERR_NOOBJ );
+        return get_afp_errno(AFPERR_NOOBJ); 
     }
 
     sdir = curdir;
     newname = obj->newtmp;
     oldname = obj->oldtmp;
+    
+    isdir = path_isadir(path);
     if ( *path->m_name != '\0' ) {
-        /* not a directory */
+        if (isdir) {
+            sdir = path->dir;
+       }
         strcpy(oldname, path->m_name); /* an extra copy for of_rename */
     } else {
-        isdir = 1;
         strcpy(oldname, sdir->d_m_name);
     }
 
     /* get the destination directory */
     if (NULL == ( ddir = dirlookup( vol, did )) ) {
-        return( AFPERR_PARAM );
+        return afp_errno; /*  was AFPERR_PARAM */
     }
     if (( path = cname( vol, ddir, &ibuf )) == NULL ) {
         return( AFPERR_NOOBJ );
     }
     if ( *path->m_name != '\0' ) {
-        return( AFPERR_BADTYPE );
+        return path_error(path, AFPERR_NOOBJ);
     }
 
     /* one more place where we know about path type */
@@ -706,8 +737,11 @@ int                ibuflen, *rbuflen;
     rc = moveandrename(vol, sdir, oldname, newname, isdir);
 
     if ( rc == AFP_OK ) {
-        char *upath = mtoupath(vol, newname);
-
+        char *upath = mtoupath(vol, newname, utf8_encoding());
+        
+        if (NULL == upath) {
+            return AFPERR_PARAM;
+        }
         curdir->offcnt++;
         sdir->offcnt--;
 #ifdef DROPKLUDGE