/*
- * $Id: directory.c,v 1.134 2010-02-19 10:51:59 franklahm Exp $
+ * $Id: directory.c,v 1.140 2010-03-12 15:16:49 franklahm Exp $
*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
struct dir *curdir;
-int afp_errno;
+int afp_errno;
#define SENTINEL (&sentinel)
static struct dir sentinel = { SENTINEL, SENTINEL, NULL, /* left, right, back */
* frPutAway: 4 home directory ID
*/
-/*!
- * @brief symlink safe chdir replacement
- *
- * Only chdirs to dir if it doesn't contain symlinks.
- *
- * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
- */
-static int lchdir(const char *dir)
-{
- int ret = 0;
- char buf[MAXPATHLEN+1];
-#ifdef REALPATH_TAKES_NULL
- char *rpath = NULL;
-#else
- char rpath[MAXPATHLEN+1];
-#endif
-
- /* dir might be an relative or an absolute path */
- if (dir[0] == '/') {
- /* absolute path, just make sure buf is prepared for strlcat */
- buf[0] = 0;
- } else {
- /* relative path, push cwd int buf */
- if (getcwd(buf, MAXPATHLEN) == NULL)
- return -1;
- if (strlcat(buf, "/", MAXPATHLEN) >= MAXPATHLEN)
- return -1;
- }
-
- if (strlcat(buf, dir, MAXPATHLEN) >= MAXPATHLEN)
- return -1;
-
-#ifdef REALPATH_TAKES_NULL
- if ((rpath = realpath(dir, NULL)) == NULL) {
-#else
- if (realpath(dir, rpath) == NULL) {
-#endif
- ret = -1;
- goto exit;
- }
-
- /*
- * Cases:
- * chdir request | realpath result | ret
- * (after getwcwd) | |
- * =======================================
- * /a/b/. | /a/b | 0
- * /a/b/. | /c | 1
- * /a/b/. | /c/d/e/f | 1
- */
- ret = 0;
- for (int i = 0; rpath[i]; i++) {
- if (buf[i] != rpath[i]) {
- ret = 1;
- goto exit;
- }
- }
-
- if (chdir(dir) != 0) {
- ret = -1;
- goto exit;
- }
-
-exit:
-#ifdef REALPATH_TAKES_NULL
- free(rpath);
-#endif
- return ret;
-}
-
static struct dir *
vol_tree_root(const struct vol *vol, u_int32_t did)
{
}
/* ------------------- */
-static int deletedir(char *dir)
+static int deletedir(int dirfd, char *dir)
{
char path[MAXPATHLEN + 1];
DIR *dp;
return AFPERR_PARAM;
/* already gone */
- if ((dp = opendir(dir)) == NULL)
+ if ((dp = opendirat(dirfd, dir)) == NULL)
return AFP_OK;
strcpy(path, dir);
break;
}
strcpy(path + len, de->d_name);
- if (lstat(path, &st)) {
+ if (lstatat(dirfd, path, &st)) {
continue;
}
if (S_ISDIR(st.st_mode)) {
- err = deletedir(path);
+ err = deletedir(dirfd, path);
} else {
- err = netatalk_unlink(path);
+ err = netatalk_unlinkat(dirfd, path);
}
}
closedir(dp);
/* okay. the directory is empty. delete it. note: we already got rid
of .AppleDouble. */
if (err == AFP_OK) {
- err = netatalk_rmdir(dir);
+ err = netatalk_rmdir(dirfd, dir);
}
return err;
}
/* do a recursive copy. */
-static int copydir(const struct vol *vol, char *src, char *dst)
+static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
{
char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
DIR *dp;
/* doesn't exist or the path is too long. */
if (((slen = strlen(src)) > sizeof(spath) - 2) ||
((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
- ((dp = opendir(src)) == NULL))
+ ((dp = opendirat(dirfd, src)) == NULL))
return AFPERR_PARAM;
/* try to create the destination directory */
}
strcpy(spath + slen, de->d_name);
- if (lstat(spath, &st) == 0) {
+ if (lstatat(dirfd, spath, &st) == 0) {
if (strlen(de->d_name) > drem) {
err = AFPERR_PARAM;
break;
strcpy(dpath + dlen, de->d_name);
if (S_ISDIR(st.st_mode)) {
- if (AFP_OK != (err = copydir(vol, spath, dpath)))
+ if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
goto copydir_done;
- } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
+ } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
goto copydir_done;
} else {
}
/* keep the same time stamp. */
- if (lstat(src, &st) == 0) {
+ if (lstatat(dirfd, src, &st) == 0) {
ut.actime = ut.modtime = st.st_mtime;
utime(dst, &ut);
}
return dir;
}
+#if 0
/* ------------------ */
static hash_val_t hash_fun_dir(const void *key)
{
}
return acc;
}
+#endif
#undef get16bits
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
}
p = path + sizeof(path) - 1;
- *p-- = '\0';
- *p = '.';
+ *p = '\0';
for ( d = dir; d->d_parent != NULL && d != curdir; d = d->d_parent ) {
u = d->d_u_name;
if (!u) {
p -= n;
memcpy( p, vol->v_path, n );
}
- if ( (ret = lchdir( p )) != 0 ) {
+ if ( (ret = lchdir(p )) != 0 ) {
LOG(log_debug, logtype_afpd, "movecwd('%s'): ret:%d, %u/%s", p, ret, errno, strerror(errno));
if (ret == 1) {
- /* p is a symlink */
+ /* p is a symlink or getcwd failed */
afp_errno = AFPERR_BADTYPE;
+ vol->v_curdir = curdir = vol->v_dir;
+ if (chdir(vol->v_path ) < 0) {
+ LOG(log_debug, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
+ /* XXX what do we do here? */
+ }
return -1;
}
switch (errno) {
* dst new unix filename (not a pathname)
* newname new mac name
* newparent curdir
- *
+ * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
*/
-int renamedir(const struct vol *vol, char *src, char *dst,
+int renamedir(const struct vol *vol,
+ int dirfd,
+ char *src,
+ char *dst,
struct dir *dir,
struct dir *newparent,
char *newname)
int len, err;
/* existence check moved to afp_moveandrename */
- if ( unix_rename( src, dst ) < 0 ) {
+ if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
switch ( errno ) {
case ENOENT :
return( AFPERR_NOOBJ );
case EXDEV:
/* this needs to copy and delete. bleah. that means we have
* to deal with entire directory hierarchies. */
- if ((err = copydir(vol, src, dst)) < 0) {
- deletedir(dst);
+ if ((err = copydir(vol, dirfd, src, dst)) < 0) {
+ deletedir(-1, dst);
return err;
}
- if ((err = deletedir(src)) < 0)
+ if ((err = deletedir(dirfd, src)) < 0)
return err;
break;
default :
}
}
- vol->vfs->vfs_renamedir(vol, src, dst);
+ vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
len = strlen( newname );
/* rename() succeeded so we need to update our tree even if we can't open
if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
ad_getattr(&ad, &ashort);
- ad_close( &ad, ADFLAGS_HF );
+ ad_close_metadata(&ad);
if ((ashort & htons(ATTRBIT_NODELETE))) {
return AFPERR_OLOCK;
}
goto delete_done;
}
- err = netatalk_rmdir_all_errors(fdir->d_u_name);
+ err = netatalk_rmdir_all_errors(-1, fdir->d_u_name);
if ( err == AFP_OK || err == AFPERR_NOOBJ) {
dirchildremove(curdir, fdir);
cnid_delete(vol->v_cdb, fdir->d_did);