Merge PR #38031 into master

* refs/pull/38031/head:
	client: check rdonly file handle on truncate
	test/libcephfs: test truncate on rdonly fd

Reviewed-by: Jeff Layton <jlayton@redhat.com>
This commit is contained in:
Patrick Donnelly 2020-11-25 13:28:25 -08:00
commit f7c9ce50ae
No known key found for this signature in database
GPG Key ID: 3A2A7E25BEA8AADB
2 changed files with 27 additions and 0 deletions

View File

@ -10183,6 +10183,8 @@ int Client::ftruncate(int fd, loff_t length, const UserPerm& perms)
if (f->flags & O_PATH)
return -EBADF;
#endif
if ((f->mode & CEPH_FILE_MODE_WR) == 0)
return -EBADF;
struct stat attr;
attr.st_size = length;
return _setattr(f->inode, &attr, CEPH_SETATTR_SIZE, perms);

View File

@ -33,6 +33,7 @@
#include <sys/xattr.h>
#endif
#include <fmt/format.h>
#include <map>
#include <vector>
#include <thread>
@ -80,6 +81,30 @@ TEST(LibCephFS, OpenEmptyComponent) {
ceph_shutdown(cmount);
}
TEST(LibCephFS, OpenReadTruncate) {
struct ceph_mount_info *cmount;
ASSERT_EQ(0, ceph_create(&cmount, NULL));
ASSERT_EQ(0, ceph_conf_read_file(cmount, NULL));
ASSERT_EQ(0, ceph_conf_parse_env(cmount, NULL));
ASSERT_EQ(0, ceph_mount(cmount, "/"));
auto path = fmt::format("test_open_rdt_{}", getpid());
int fd = ceph_open(cmount, path.c_str(), O_WRONLY|O_CREAT, 0666);
ASSERT_LE(0, fd);
auto data = std::string("hello world");
ASSERT_EQ(ceph_write(cmount, fd, data.c_str(), data.size(), 0), (int)data.size());
ASSERT_EQ(0, ceph_close(cmount, fd));
fd = ceph_open(cmount, path.c_str(), O_RDONLY, 0);
ASSERT_LE(0, fd);
ASSERT_EQ(ceph_ftruncate(cmount, fd, 0), -EBADF);
ASSERT_EQ(ceph_ftruncate(cmount, fd, 1), -EBADF);
ASSERT_EQ(0, ceph_close(cmount, fd));
ceph_shutdown(cmount);
}
TEST(LibCephFS, OpenReadWrite) {
struct ceph_mount_info *cmount;
ASSERT_EQ(0, ceph_create(&cmount, NULL));