TESTS: Tests and fixes for nfs_rename
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>libnfs-4.0.0-vitalif
parent
ea94d4e3a6
commit
903021ed59
|
@ -1459,7 +1459,8 @@ int nfs_rename(struct nfs_context *nfs, const char *oldpath, const char *newpath
|
|||
cb_data.is_finished = 0;
|
||||
|
||||
if (nfs_rename_async(nfs, oldpath, newpath, rename_cb, &cb_data) != 0) {
|
||||
nfs_set_error(nfs, "nfs_rename_async failed");
|
||||
nfs_set_error(nfs, "nfs_rename_async failed: %s",
|
||||
nfs_get_error(nfs));
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
115
lib/nfs3.c
115
lib/nfs3.c
|
@ -445,7 +445,7 @@ nfs3_lookuppath_async(struct nfs_context *nfs, const char *path, int no_follow,
|
|||
struct GETATTR3args args;
|
||||
struct nfs_fh *fh;
|
||||
|
||||
if (path[0] == '\0') {
|
||||
if (path == NULL || path[0] == '\0') {
|
||||
path = ".";
|
||||
}
|
||||
|
||||
|
@ -1244,10 +1244,10 @@ nfs3_link_async(struct nfs_context *nfs, const char *oldpath,
|
|||
}
|
||||
|
||||
struct nfs_rename_data {
|
||||
char *oldpath;
|
||||
char *oldparent;
|
||||
char *oldobject;
|
||||
struct nfs_fh olddir;
|
||||
char *newpath;
|
||||
char *newparent;
|
||||
char *newobject;
|
||||
struct nfs_fh newdir;
|
||||
};
|
||||
|
@ -1257,18 +1257,12 @@ free_nfs_rename_data(void *mem)
|
|||
{
|
||||
struct nfs_rename_data *data = mem;
|
||||
|
||||
if (data->oldpath != NULL) {
|
||||
free(data->oldpath);
|
||||
}
|
||||
if (data->olddir.val != NULL) {
|
||||
free(data->olddir.val);
|
||||
}
|
||||
if (data->newpath != NULL) {
|
||||
free(data->newpath);
|
||||
}
|
||||
if (data->newdir.val != NULL) {
|
||||
free(data->newdir.val);
|
||||
}
|
||||
free(data->oldparent);
|
||||
free(data->oldobject);
|
||||
free(data->olddir.val);
|
||||
free(data->newparent);
|
||||
free(data->newobject);
|
||||
free(data->newdir.val);
|
||||
free(data);
|
||||
}
|
||||
|
||||
|
@ -1291,8 +1285,8 @@ nfs3_rename_cb(struct rpc_context *rpc, int status, void *command_data,
|
|||
res = command_data;
|
||||
if (res->status != NFS3_OK) {
|
||||
nfs_set_error(nfs, "NFS: RENAME %s/%s -> %s/%s failed "
|
||||
"with %s(%d)", rename_data->oldpath,
|
||||
rename_data->oldobject, rename_data->newpath,
|
||||
"with %s(%d)", rename_data->oldparent,
|
||||
rename_data->oldobject, rename_data->newparent,
|
||||
rename_data->newobject,
|
||||
nfsstat3_to_str(res->status),
|
||||
nfsstat3_to_errno(res->status));
|
||||
|
@ -1344,15 +1338,6 @@ nfs3_rename_continue_1_internal(struct nfs_context *nfs,
|
|||
struct nfs_cb_data *data)
|
||||
{
|
||||
struct nfs_rename_data *rename_data = data->continue_data;
|
||||
char* newpath = strdup(rename_data->newpath);
|
||||
if (!newpath) {
|
||||
nfs_set_error(nfs, "Out of memory. Could not allocate "
|
||||
"memory to store target path for rename");
|
||||
data->cb(-ENOMEM, nfs, nfs_get_error(nfs),
|
||||
data->private_data);
|
||||
free_nfs_cb_data(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Drop the source directory from the cache */
|
||||
nfs_dircache_drop(nfs, &data->fh);
|
||||
|
@ -1361,19 +1346,17 @@ nfs3_rename_continue_1_internal(struct nfs_context *nfs,
|
|||
rename_data->olddir = data->fh;
|
||||
data->fh.val = NULL;
|
||||
|
||||
if (nfs3_lookuppath_async(nfs, rename_data->newpath, 0,
|
||||
if (nfs3_lookuppath_async(nfs, rename_data->newparent, 0,
|
||||
data->cb, data->private_data,
|
||||
nfs3_rename_continue_2_internal,
|
||||
rename_data, free_nfs_rename_data, 0) != 0) {
|
||||
data->cb(-ENOMEM, nfs, nfs_get_error(nfs),
|
||||
data->private_data);
|
||||
free_nfs_cb_data(data);
|
||||
free(newpath);
|
||||
return -1;
|
||||
}
|
||||
data->continue_data = NULL;
|
||||
free_nfs_cb_data(data);
|
||||
free(newpath);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1393,43 +1376,56 @@ nfs3_rename_async(struct nfs_context *nfs, const char *oldpath,
|
|||
}
|
||||
memset(rename_data, 0, sizeof(struct nfs_rename_data));
|
||||
|
||||
rename_data->oldpath = strdup(oldpath);
|
||||
if (rename_data->oldpath == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to allocate "
|
||||
"buffer for oldpath");
|
||||
rename_data->oldobject = strdup(oldpath);
|
||||
if (rename_data->oldobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to strdup "
|
||||
"oldpath");
|
||||
free_nfs_rename_data(rename_data);
|
||||
return -1;
|
||||
}
|
||||
ptr = strrchr(rename_data->oldpath, '/');
|
||||
ptr = strrchr(rename_data->oldobject, '/');
|
||||
if (ptr == NULL) {
|
||||
nfs_set_error(nfs, "Invalid path %s", oldpath);
|
||||
free_nfs_rename_data(rename_data);
|
||||
return -1;
|
||||
}
|
||||
*ptr = 0;
|
||||
ptr++;
|
||||
rename_data->oldobject = ptr;
|
||||
rename_data->oldparent = NULL;
|
||||
} else {
|
||||
*ptr = 0;
|
||||
rename_data->oldparent = rename_data->oldobject;
|
||||
|
||||
|
||||
rename_data->newpath = strdup(newpath);
|
||||
if (rename_data->newpath == NULL) {
|
||||
ptr++;
|
||||
rename_data->oldobject = strdup(ptr);
|
||||
}
|
||||
if (rename_data->oldobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to allocate "
|
||||
"buffer for newpath");
|
||||
"buffer for oldobject");
|
||||
free_nfs_rename_data(rename_data);
|
||||
return -1;
|
||||
}
|
||||
ptr = strrchr(rename_data->newpath, '/');
|
||||
|
||||
rename_data->newobject = strdup(newpath);
|
||||
if (rename_data->newobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to strdup "
|
||||
"newpath");
|
||||
free_nfs_rename_data(rename_data);
|
||||
return -1;
|
||||
}
|
||||
ptr = strrchr(rename_data->newobject, '/');
|
||||
if (ptr == NULL) {
|
||||
nfs_set_error(nfs, "Invalid path %s", newpath);
|
||||
rename_data->newparent = NULL;
|
||||
} else {
|
||||
*ptr = 0;
|
||||
rename_data->newparent = rename_data->newobject;
|
||||
|
||||
ptr++;
|
||||
rename_data->newobject = strdup(ptr);
|
||||
}
|
||||
if (rename_data->newobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to allocate "
|
||||
"buffer for newobject");
|
||||
free_nfs_rename_data(rename_data);
|
||||
return -1;
|
||||
}
|
||||
*ptr = 0;
|
||||
ptr++;
|
||||
rename_data->newobject = ptr;
|
||||
|
||||
|
||||
if (nfs3_lookuppath_async(nfs, rename_data->oldpath, 0,
|
||||
if (nfs3_lookuppath_async(nfs, rename_data->oldparent, 0,
|
||||
cb, private_data,
|
||||
nfs3_rename_continue_1_internal,
|
||||
rename_data, free_nfs_rename_data, 0) != 0) {
|
||||
|
@ -1543,11 +1539,15 @@ nfs3_symlink_async(struct nfs_context *nfs, const char *target,
|
|||
}
|
||||
|
||||
symlink_data->linkobject = strdup(linkname);
|
||||
/* Do we have a path ? */
|
||||
if (symlink_data->linkobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to strdup "
|
||||
"linkname");
|
||||
free_nfs_symlink_data(symlink_data);
|
||||
return -1;
|
||||
}
|
||||
ptr = strrchr(symlink_data->linkobject, '/');
|
||||
if (ptr == NULL) {
|
||||
/* pass this as NULL ? */
|
||||
symlink_data->linkparent = strdup("");
|
||||
symlink_data->linkparent = NULL;
|
||||
} else {
|
||||
*ptr = 0;
|
||||
symlink_data->linkparent = symlink_data->linkobject;
|
||||
|
@ -1556,13 +1556,6 @@ nfs3_symlink_async(struct nfs_context *nfs, const char *target,
|
|||
symlink_data->linkobject = strdup(ptr);
|
||||
}
|
||||
|
||||
if (symlink_data->linkparent == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to allocate "
|
||||
"mode buffer for new path");
|
||||
free_nfs_symlink_data(symlink_data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (symlink_data->linkobject == NULL) {
|
||||
nfs_set_error(nfs, "Out of memory, failed to allocate "
|
||||
"mode buffer for new path");
|
||||
|
|
|
@ -4,7 +4,7 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/nfsc \
|
|||
AM_CFLAGS = $(WARN_CFLAGS)
|
||||
LDADD = ../lib/libnfs.la
|
||||
|
||||
noinst_PROGRAMS = prog_fstat prog_stat prog_symlink prog_timeout
|
||||
noinst_PROGRAMS = prog_fstat prog_rename prog_stat prog_symlink prog_timeout
|
||||
|
||||
EXTRA_PROGRAMS = ld_timeout
|
||||
CLEANFILES = ld_timeout.o ld_timeout.so
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2017
|
||||
|
||||
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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "libnfs.h"
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
fprintf(stderr, "Usage: prog-rename <url> <cwd> <oldpath> <newpath>\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct nfs_context *nfs = NULL;
|
||||
struct nfs_url *url = NULL;
|
||||
int ret = 0;
|
||||
|
||||
if (argc != 5) {
|
||||
usage();
|
||||
}
|
||||
|
||||
nfs = nfs_init_context();
|
||||
if (nfs == NULL) {
|
||||
printf("failed to init context\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
url = nfs_parse_url_full(nfs, argv[1]);
|
||||
if (url == NULL) {
|
||||
fprintf(stderr, "%s\n", nfs_get_error(nfs));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nfs_mount(nfs, url->server, url->path) != 0) {
|
||||
fprintf(stderr, "Failed to mount nfs share : %s\n",
|
||||
nfs_get_error(nfs));
|
||||
ret = 1;
|
||||
goto finished;
|
||||
}
|
||||
|
||||
if (nfs_chdir(nfs, argv[2]) != 0) {
|
||||
fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
|
||||
argv[2], nfs_get_error(nfs));
|
||||
ret = 1;
|
||||
goto finished;
|
||||
}
|
||||
|
||||
if (nfs_rename(nfs, argv[3], argv[4])) {
|
||||
fprintf(stderr, "Failed to rename: %s\n",
|
||||
nfs_get_error(nfs));
|
||||
ret = 1;
|
||||
goto finished;
|
||||
}
|
||||
|
||||
finished:
|
||||
nfs_destroy_url(url);
|
||||
nfs_destroy_context(nfs);
|
||||
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./functions.sh
|
||||
|
||||
echo "basic rename test"
|
||||
|
||||
start_share
|
||||
|
||||
mkdir "${TESTDIR}/subdir"
|
||||
mkdir "${TESTDIR}/subdir2"
|
||||
|
||||
echo -n "Rename a root path (abs -> abs) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/testfile"
|
||||
./prog_rename "${TESTURL}/" "." /testfile /renamed1 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/renamed1" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a root path (rel -> abs) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/testfile"
|
||||
./prog_rename "${TESTURL}/" "." testfile /renamed2 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/renamed2" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a root path (rel -> rel) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/testfile"
|
||||
./prog_rename "${TESTURL}/" "." testfile renamed3 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/renamed3" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a root path (abs -> rel) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/testfile"
|
||||
./prog_rename "${TESTURL}/" "." /testfile renamed4 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/renamed4" >/dev/null || failure
|
||||
success
|
||||
|
||||
|
||||
|
||||
echo -n "Rename a subdir path (abs -> abs) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "." /subdir/testfile /subdir/renamed5 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/subdir/renamed5" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a subdir path (rel -> abs) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "." subdir/testfile /subdir/renamed6 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/subdir/renamed6" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a subdir path (rel -> rel) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "." subdir/testfile subdir/renamed7 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/subdir/renamed7" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a subdir path (abs -> rel) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "." /subdir/testfile subdir/renamed8 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/subdir/renamed8" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename a subdir path to a different dir (rel -> rel) ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "." /subdir/testfile subdir2/renamed9 || failure
|
||||
success
|
||||
|
||||
echo -n "Verify the new path ... "
|
||||
grep kangabanga "${TESTDIR}/subdir2/renamed9" >/dev/null || failure
|
||||
success
|
||||
|
||||
echo -n "Rename from different cwd ... "
|
||||
echo "kangabanga" > "${TESTDIR}/subdir/testfile"
|
||||
./prog_rename "${TESTURL}/" "subdir" ./testfile ../subdir2/renamed10 || failure
|
||||
success
|
||||
|
||||
echo -n "Rename from outside share ... "
|
||||
./prog_rename "${TESTURL}/" "subdir" ../../testfile ../subdir2/renamed11 2>/dev/null && failure
|
||||
success
|
||||
|
||||
|
||||
stop_share
|
||||
|
||||
exit 0
|
|
@ -53,12 +53,12 @@ libtool --mode=execute valgrind --leak-check=full --error-exitcode=1 ./prog_fsta
|
|||
success
|
||||
|
||||
echo -n "test nfs_symlink()/nfs_readlink() for memory leaks ... "
|
||||
libtool --mode=execute valgrind --leak-check=full --error-exitcode=1 ./prog_symlink "${TESTURL}/" kangabanga /symlink >/dev/null 2>&1 || failure
|
||||
libtool --mode=execute valgrind --leak-check=full --error-exitcode=1 ./prog_symlink "${TESTURL}/" "." kangabanga /symlink >/dev/null 2>&1 || failure
|
||||
success
|
||||
|
||||
echo -n "test nfs_symlink()/nfs_readlink() for memory leaks (2) ... "
|
||||
mkdir "${TESTDIR}/subdir"
|
||||
libtool --mode=execute valgrind --leak-check=full --error-exitcode=1 ./prog_symlink "${TESTURL}/" kangabanga /subdir/symlink >/dev/null 2>&1 || failure
|
||||
libtool --mode=execute valgrind --leak-check=full --error-exitcode=1 ./prog_symlink "${TESTURL}/" "." kangabanga /subdir/symlink >/dev/null 2>&1 || failure
|
||||
success
|
||||
|
||||
stop_share
|
||||
|
|
Loading…
Reference in New Issue