]> git.hungrycats.org Git - bees/commitdiff
fs: add do_ioctl_nothrow and fsid methods to btrfs fs info
authorZygo Blaxell <bees@furryterror.org>
Mon, 27 Jan 2025 04:33:19 +0000 (23:33 -0500)
committerZygo Blaxell <bees@furryterror.org>
Fri, 7 Feb 2025 03:42:15 +0000 (22:42 -0500)
Enable use of the ioctl to probe whether two fds refer to the same btrfs,
without throwing an exception.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
include/crucible/fs.h
lib/fs.cc

index 6f955513d3846a95f79ca5c8552d5387f89ed598..35616810af31dc72b0a113310d1b35dd2c83f115 100644 (file)
@@ -246,9 +246,11 @@ namespace crucible {
        struct BtrfsIoctlFsInfoArgs : public btrfs_ioctl_fs_info_args_v3 {
                BtrfsIoctlFsInfoArgs();
                void do_ioctl(int fd);
+               bool do_ioctl_nothrow(int fd);
                uint16_t csum_type() const;
                uint16_t csum_size() const;
                uint64_t generation() const;
+               vector<uint8_t> fsid() const;
        };
 
        ostream & operator<<(ostream &os, const BtrfsIoctlFsInfoArgs &a);
index 3366d14b201767772470695c4c61ea9d75f49aa3..8c8226cfd64555da0036d2b6503776d7f1bc2339 100644 (file)
--- a/lib/fs.cc
+++ b/lib/fs.cc
@@ -1138,11 +1138,17 @@ namespace crucible {
        {
        }
 
-       void
-       BtrfsIoctlFsInfoArgs::do_ioctl(int fd)
+       bool
+       BtrfsIoctlFsInfoArgs::do_ioctl_nothrow(int const fd)
        {
                btrfs_ioctl_fs_info_args_v3 *p = static_cast<btrfs_ioctl_fs_info_args_v3 *>(this);
-               if (ioctl(fd, BTRFS_IOC_FS_INFO, p)) {
+               return 0 == ioctl(fd, BTRFS_IOC_FS_INFO, p);
+       }
+
+       void
+       BtrfsIoctlFsInfoArgs::do_ioctl(int const fd)
+       {
+               if (!do_ioctl_nothrow(fd)) {
                        THROW_ERRNO("BTRFS_IOC_FS_INFO: fd " << fd);
                }
        }
@@ -1159,6 +1165,13 @@ namespace crucible {
                return this->btrfs_ioctl_fs_info_args_v3::csum_size;
        }
 
+       vector<uint8_t>
+       BtrfsIoctlFsInfoArgs::fsid() const
+       {
+               const auto begin = btrfs_ioctl_fs_info_args_v3::fsid;
+               return vector<uint8_t>(begin, begin + BTRFS_FSID_SIZE);
+       }
+
        uint64_t
        BtrfsIoctlFsInfoArgs::generation() const
        {