NFSv4: Add support for chmod/lchmod/fchmod

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
libnfs-4.0.0-vitalif
Ronnie Sahlberg 2017-09-01 14:02:43 +10:00
parent c7655f90ca
commit 6c034223fd
11 changed files with 601 additions and 13 deletions

View File

@ -474,10 +474,15 @@ int nfs3_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
int nfs4_chdir_async(struct nfs_context *nfs, const char *path,
nfs_cb cb, void *private_data);
int nfs4_chmod_async_internal(struct nfs_context *nfs, const char *path,
int no_follow, int mode, nfs_cb cb,
void *private_data);
int nfs4_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
void *private_data);
int nfs4_create_async(struct nfs_context *nfs, const char *path, int flags,
int mode, nfs_cb cb, void *private_data);
int nfs4_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode,
nfs_cb cb, void *private_data);
int nfs4_fstat64_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
void *private_data);
int nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,

View File

@ -1212,7 +1212,8 @@ nfs_chmod(struct nfs_context *nfs, const char *path, int mode)
cb_data.is_finished = 0;
if (nfs_chmod_async(nfs, path, mode, chmod_cb, &cb_data) != 0) {
nfs_set_error(nfs, "nfs_chmod_async failed");
nfs_set_error(nfs, "nfs_chmod_async failed. %s",
nfs_get_error(nfs));
return -1;
}
@ -1229,7 +1230,8 @@ nfs_lchmod(struct nfs_context *nfs, const char *path, int mode)
cb_data.is_finished = 0;
if (nfs_lchmod_async(nfs, path, mode, chmod_cb, &cb_data) != 0) {
nfs_set_error(nfs, "nfs_lchmod_async failed");
nfs_set_error(nfs, "nfs_lchmod_async failed. %s",
nfs_get_error(nfs));
return -1;
}
@ -1267,7 +1269,8 @@ nfs_fchmod(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode)
cb_data.is_finished = 0;
if (nfs_fchmod_async(nfs, nfsfh, mode, fchmod_cb, &cb_data) != 0) {
nfs_set_error(nfs, "nfs_fchmod_async failed");
nfs_set_error(nfs, "nfs_fchmod_async failed. %s",
nfs_get_error(nfs));
return -1;
}

View File

@ -1398,9 +1398,12 @@ nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode,
case NFS_V3:
return nfs3_chmod_async_internal(nfs, path, 0, mode,
cb, private_data);
case NFS_V4:
return nfs4_chmod_async_internal(nfs, path, 0, mode,
cb, private_data);
default:
nfs_set_error(nfs, "%s does not support NFSv4",
__FUNCTION__);
nfs_set_error(nfs, "%s does not support NFSv%d",
__FUNCTION__, nfs->version);
return -1;
}
}
@ -1413,9 +1416,12 @@ nfs_lchmod_async(struct nfs_context *nfs, const char *path, int mode,
case NFS_V3:
return nfs3_chmod_async_internal(nfs, path, 1, mode,
cb, private_data);
case NFS_V4:
return nfs4_chmod_async_internal(nfs, path, 1, mode,
cb, private_data);
default:
nfs_set_error(nfs, "%s does not support NFSv4",
__FUNCTION__);
nfs_set_error(nfs, "%s does not support NFSv%d",
__FUNCTION__, nfs->version);
return -1;
}
}
@ -1427,9 +1433,11 @@ nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode,
switch (nfs->version) {
case NFS_V3:
return nfs3_fchmod_async(nfs, nfsfh, mode, cb, private_data);
case NFS_V4:
return nfs4_fchmod_async(nfs, nfsfh, mode, cb, private_data);
default:
nfs_set_error(nfs, "%s does not support NFSv4",
__FUNCTION__);
nfs_set_error(nfs, "%s does not support NFSv%d",
__FUNCTION__, nfs->version);
return -1;
}
}

View File

@ -704,6 +704,28 @@ nfs4_op_truncate(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
return 1;
}
static int
nfs4_op_chmod(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
void *sabuf)
{
SETATTR4args *saargs;
static uint32_t mask[2] = {0,
1 << (FATTR4_MODE - 32)};
op[0].argop = OP_SETATTR;
saargs = &op[0].nfs_argop4_u.opsetattr;
saargs->stateid.seqid = fh->stateid.seqid;
memcpy(saargs->stateid.other, fh->stateid.other, 12);
saargs->obj_attributes.attrmask.bitmap4_len = 2;
saargs->obj_attributes.attrmask.bitmap4_val = mask;
saargs->obj_attributes.attr_vals.attrlist4_len = 4;
saargs->obj_attributes.attr_vals.attrlist4_val = sabuf;
return 1;
}
static int
nfs4_op_readdir(struct nfs_context *nfs, nfs_argop4 *op, uint64_t cookie)
{
@ -3871,3 +3893,148 @@ nfs4_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb,
return 0;
}
static void
nfs4_chmod_close_cb(struct rpc_context *rpc, int status, void *command_data,
void *private_data)
{
struct nfs4_cb_data *data = private_data;
struct nfs_context *nfs = data->nfs;
COMPOUND4res *res = command_data;
assert(rpc->magic == RPC_CONTEXT_MAGIC);
if (res) {
nfs_increment_seqid(nfs, res->status);
}
if (check_nfs4_error(nfs, status, data, res, "CLOSE")) {
return;
}
data->cb(0, nfs, NULL, data->private_data);
free_nfs4_cb_data(data);
}
static void
nfs4_chmod_open_cb(struct rpc_context *rpc, int status, void *command_data,
void *private_data)
{
struct nfs4_cb_data *data = private_data;
struct nfs_context *nfs = data->nfs;
struct nfsfh *fh = data->filler.blob0.val;
COMPOUND4res *res = command_data;
COMPOUND4args args;
nfs_argop4 op[4];
int i;
if (check_nfs4_error(nfs, status, data, res, "OPEN")) {
return;
}
i = nfs4_op_putfh(nfs, &op[0], fh);
i += nfs4_op_chmod(nfs, &op[i], fh, data->filler.blob3.val);
i += nfs4_op_close(nfs, &op[i], fh);
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = i;
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_async(nfs->rpc, nfs4_chmod_close_cb, &args,
data) != 0) {
/* Not much we can do but leak one fd on the server :( */
data->cb(-ENOMEM, nfs, nfs_get_error(nfs), data->private_data);
free_nfs4_cb_data(data);
return;
}
}
int
nfs4_chmod_async_internal(struct nfs_context *nfs, const char *path,
int no_follow, int mode, nfs_cb cb,
void *private_data)
{
struct nfs4_cb_data *data;
uint32_t m;
data = init_cb_data_split_path(nfs, path);
if (data == NULL) {
return -1;
}
data->cb = cb;
data->private_data = private_data;
data->open_cb = nfs4_chmod_open_cb;
if (no_follow) {
data->flags |= LOOKUP_FLAG_NO_FOLLOW;
}
data->filler.blob3.val = malloc(sizeof(uint32_t));
if (data->filler.blob3.val == NULL) {
nfs_set_error(nfs, "Out of memory");
free_nfs4_cb_data(data);
return -1;
}
data->filler.blob3.free = free;
m = htonl(mode);
memcpy(data->filler.blob3.val, &m, sizeof(uint32_t));
if (nfs4_open_async_internal(nfs, data, O_WRONLY, 0) < 0) {
return -1;
}
return 0;
}
int
nfs4_fchmod_async(struct nfs_context *nfs, struct nfsfh *fh, int mode,
nfs_cb cb, void *private_data)
{
COMPOUND4args args;
nfs_argop4 op[2];
struct nfs4_cb_data *data;
uint32_t m;
int i;
data = malloc(sizeof(*data));
if (data == NULL) {
nfs_set_error(nfs, "Out of memory.");
return -1;
}
memset(data, 0, sizeof(*data));
data->nfs = nfs;
data->cb = cb;
data->private_data = private_data;
data->filler.blob3.val = malloc(sizeof(uint32_t));
if (data->filler.blob3.val == NULL) {
nfs_set_error(nfs, "Out of memory");
free_nfs4_cb_data(data);
return -1;
}
data->filler.blob3.free = free;
m = htonl(mode);
memcpy(data->filler.blob3.val, &m, sizeof(uint32_t));
memset(op, 0, sizeof(op));
i = nfs4_op_putfh(nfs, &op[0], fh);
i += nfs4_op_chmod(nfs, &op[i], fh, data->filler.blob3.val);
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = i;
args.argarray.argarray_val = op;
if (rpc_nfs4_compound_async(nfs->rpc, nfs4_fsync_cb, &args,
data) != 0) {
data->filler.blob0.val = NULL;
free_nfs4_cb_data(data);
return -1;
}
return 0;
}

View File

@ -4,10 +4,11 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/nfsc \
AM_CFLAGS = $(WARN_CFLAGS)
LDADD = ../lib/libnfs.la
noinst_PROGRAMS = prog_create prog_fstat prog_ftruncate prog_link prog_lseek \
prog_lstat prog_mkdir prog_mknod prog_mount prog_open_read \
prog_open_write prog_rename prog_rmdir prog_stat prog_statvfs \
prog_symlink prog_timeout prog_truncate prog_unlink
noinst_PROGRAMS = prog_chmod prog_create prog_fchmod prog_fstat \
prog_ftruncate prog_lchmod prog_link prog_lseek prog_lstat \
prog_mkdir prog_mknod prog_mount prog_open_read prog_open_write \
prog_rename prog_rmdir prog_stat prog_statvfs prog_symlink \
prog_timeout prog_truncate prog_unlink
EXTRA_PROGRAMS = ld_timeout
CLEANFILES = ld_timeout.o ld_timeout.so

92
tests/prog_chmod.c Normal file
View File

@ -0,0 +1,92 @@
/* -*- 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_chmod <url> <cwd> <path> <mode(octal)>"
"\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct nfs_context *nfs = NULL;
struct nfs_url *url = NULL;
int ret = 0;
int mode;
if (argc != 5) {
usage();
}
mode = strtol(argv[4], NULL, 8);
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_chmod(nfs, argv[3], mode)) {
fprintf(stderr, "Failed to chmod(): %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
finished:
nfs_destroy_url(url);
nfs_destroy_context(nfs);
return ret;
}

106
tests/prog_fchmod.c Normal file
View File

@ -0,0 +1,106 @@
/* -*- 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_fchmod <url> <cwd> <path> <mode(octal)>"
"\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct nfs_context *nfs = NULL;
struct nfs_url *url = NULL;
int ret = 0;
int mode;
struct nfsfh *fh;
if (argc != 5) {
usage();
}
mode = strtol(argv[4], NULL, 8);
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_open(nfs, argv[3], O_RDWR, &fh)) {
fprintf(stderr, "Failed to open file : %s\n",
nfs_get_error(nfs));
exit(1);
}
if (nfs_fchmod(nfs, fh, mode)) {
fprintf(stderr, "Failed to fchmod(): %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
if (nfs_close(nfs, fh)) {
fprintf(stderr, "Failed to close(): %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
finished:
nfs_destroy_url(url);
nfs_destroy_context(nfs);
return ret;
}

92
tests/prog_lchmod.c Normal file
View File

@ -0,0 +1,92 @@
/* -*- 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_lchmod <url> <cwd> <path> <mode(octal)>"
"\n");
exit(1);
}
int main(int argc, char *argv[])
{
struct nfs_context *nfs = NULL;
struct nfs_url *url = NULL;
int ret = 0;
int mode;
if (argc != 5) {
usage();
}
mode = strtol(argv[4], NULL, 8);
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_lchmod(nfs, argv[3], mode)) {
fprintf(stderr, "Failed to lchmod(): %s\n",
nfs_get_error(nfs));
ret = 1;
goto finished;
}
finished:
nfs_destroy_url(url);
nfs_destroy_context(nfs);
return ret;
}

38
tests/test_0370_chmod.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/sh
. ./functions.sh
echo "NFSv${VERS} Basic chmod tests."
start_share
dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null
echo -n "test chmod(0600) ... "
./prog_chmod "${TESTURL}/?version=${VERS}" "." /testfile 0600 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0600 ... "
grep "nfs_mode:100600" "${TESTDIR}/output" >/dev/null || failure
success
echo -n "test chmod(0755) ... "
./prog_chmod "${TESTURL}/?version=${VERS}" "." /testfile 0755 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0755 ... "
grep "nfs_mode:100755" "${TESTDIR}/output" >/dev/null || failure
success
stop_share
exit 0

38
tests/test_0380_lchmod.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/sh
. ./functions.sh
echo "NFSv${VERS} Basic lchmod tests."
start_share
dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null
echo -n "test lchmod(0600) ... "
./prog_lchmod "${TESTURL}/?version=${VERS}" "." /testfile 0600 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0600 ... "
grep "nfs_mode:100600" "${TESTDIR}/output" >/dev/null || failure
success
echo -n "test lchmod(0755) ... "
./prog_lchmod "${TESTURL}/?version=${VERS}" "." /testfile 0755 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0755 ... "
grep "nfs_mode:100755" "${TESTDIR}/output" >/dev/null || failure
success
stop_share
exit 0

38
tests/test_0390_fchmod.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/sh
. ./functions.sh
echo "NFSv${VERS} Basic fchmod tests."
start_share
dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null
echo -n "test fchmod(0600) ... "
./prog_fchmod "${TESTURL}/?version=${VERS}" "." /testfile 0600 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0600 ... "
grep "nfs_mode:100600" "${TESTDIR}/output" >/dev/null || failure
success
echo -n "test fchmod(0755) ... "
./prog_fchmod "${TESTURL}/?version=${VERS}" "." /testfile 0755 || failure
success
echo -n "Stat the file ... "
./prog_stat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success
echo -n "Verifying the mode is 0755 ... "
grep "nfs_mode:100755" "${TESTDIR}/output" >/dev/null || failure
success
stop_share
exit 0