From 14ce81c081e2aa3104e78bb74a54ecccd4624d0d Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 9 Oct 2022 18:25:52 -0400 Subject: [PATCH] fs: get rid of silly base class that causes build failures now The base class thing was an ugly way to get around the lack of C99 compound literals in C++, and also to make the bare ioctls usable with the derived classes. Today, both clang and gcc have C99 compound literals, so there's no need to do crazy things with memset. We never used the derived classes for ioctls, and for this specific ioctl it would have been a very, very bad idea, so there's no need to support that either. We do need to jump through hoops for ostream& operator<<() but we had to do those anyway as there are other members in the derived type. So we can simply drop the base class, and build the args object on the stack in `do_ioctl`. This also removes the need to verify initialization. There's no bug here since the `info` member of the base class was never used in place by the derived class, but new compilers reject the flexible array member in the base class because the derived class makes `info` be not at the end of the struct any more: error: flexible array member btrfs_ioctl_same_args::info not at end of struct crucible::BtrfsExtentSame Fixes: https://github.com/Zygo/bees/issues/232 Signed-off-by: Zygo Blaxell --- include/crucible/fs.h | 4 +++- lib/fs.cc | 22 +++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/crucible/fs.h b/include/crucible/fs.h index 01bef77..f6db988 100644 --- a/include/crucible/fs.h +++ b/include/crucible/fs.h @@ -31,12 +31,14 @@ namespace crucible { BtrfsExtentInfo(int dst_fd, off_t dst_offset); }; - struct BtrfsExtentSame : public btrfs_ioctl_same_args { + struct BtrfsExtentSame { virtual ~BtrfsExtentSame(); BtrfsExtentSame(int src_fd, off_t src_offset, off_t src_length); void add(int fd, off_t offset); virtual void do_ioctl(); + uint64_t m_logical_offset = 0; + uint64_t m_length = 0; int m_fd; vector m_info; }; diff --git a/lib/fs.cc b/lib/fs.cc index 52d4930..a30b771 100644 --- a/lib/fs.cc +++ b/lib/fs.cc @@ -45,13 +45,10 @@ namespace crucible { } BtrfsExtentSame::BtrfsExtentSame(int src_fd, off_t src_offset, off_t src_length) : - btrfs_ioctl_same_args( (btrfs_ioctl_same_args) { } ), + m_logical_offset(src_offset), + m_length(src_length), m_fd(src_fd) { - assert(logical_offset == 0); - assert(length == 0); - logical_offset = src_offset; - length = src_length; } BtrfsExtentSame::~BtrfsExtentSame() @@ -118,11 +115,8 @@ namespace crucible { os << " '" << fd_name << "'"; }); } - os << ", .logical_offset = " << to_hex(bes.logical_offset); - os << ", .length = " << to_hex(bes.length); - os << ", .dest_count = " << bes.dest_count; - os << ", .reserved1 = " << bes.reserved1; - os << ", .reserved2 = " << bes.reserved2; + os << ", .logical_offset = " << to_hex(bes.m_logical_offset); + os << ", .length = " << to_hex(bes.m_length); os << ", .info[] = {"; for (size_t i = 0; i < bes.m_info.size(); ++i) { os << " [" << i << "] = " << &(bes.m_info[i]) << ","; @@ -145,9 +139,11 @@ namespace crucible { void BtrfsExtentSame::do_ioctl() { - dest_count = m_info.size(); - const size_t buf_size = sizeof(btrfs_ioctl_same_args) + dest_count * sizeof(btrfs_ioctl_same_extent_info); - ByteVector ioctl_arg(static_cast(*this), buf_size); + const size_t buf_size = sizeof(btrfs_ioctl_same_args) + m_info.size() * sizeof(btrfs_ioctl_same_extent_info); + ByteVector ioctl_arg( (btrfs_ioctl_same_args) { + .logical_offset = m_logical_offset, + .length = m_length, + }, buf_size); btrfs_ioctl_same_args *const ioctl_ptr = ioctl_arg.get(); size_t count = 0; for (auto i = m_info.cbegin(); i != m_info.cend(); ++i) {