]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- moved btrfs ioctl calls to separate file
authorArvin Schnell <aschnell@suse.de>
Tue, 1 Oct 2013 15:24:51 +0000 (17:24 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 1 Oct 2013 15:24:51 +0000 (17:24 +0200)
snapper/Btrfs.cc
snapper/Btrfs.h
snapper/BtrfsUtils.cc [new file with mode: 0644]
snapper/BtrfsUtils.h [new file with mode: 0644]
snapper/Makefile.am

index 57f117827b90ceb4615d96491406df29a168fcb4..2c110c3079e7cdeb6bb59e106d5dffdd8985446f 100644 (file)
 
 #include "snapper/Log.h"
 #include "snapper/Btrfs.h"
+#include "snapper/BtrfsUtils.h"
 #include "snapper/File.h"
 #include "snapper/Snapper.h"
 #include "snapper/SnapperTmpl.h"
 #include "snapper/SnapperDefines.h"
 
 
-#ifndef HAVE_LIBBTRFS
-
-#define BTRFS_IOCTL_MAGIC 0x94
-#define BTRFS_PATH_NAME_MAX 4087
-#define BTRFS_SUBVOL_NAME_MAX 4039
-#define BTRFS_SUBVOL_RDONLY (1ULL << 1)
-
-#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, struct btrfs_ioctl_vol_args)
-#define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, struct btrfs_ioctl_vol_args)
-#define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, struct btrfs_ioctl_vol_args)
-#define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, struct btrfs_ioctl_vol_args_v2)
-
-struct btrfs_ioctl_vol_args
-{
-    __s64 fd;
-    char name[BTRFS_PATH_NAME_MAX + 1];
-};
-
-struct btrfs_ioctl_vol_args_v2
-{
-    __s64 fd;
-    __u64 transid;
-    __u64 flags;
-    __u64 unused[4];
-    char name[BTRFS_SUBVOL_NAME_MAX + 1];
-};
-
-#endif
-
-
 namespace snapper
 {
 
@@ -271,63 +242,6 @@ namespace snapper
     }
 
 
-    bool
-    Btrfs::is_subvolume(const struct stat& stat) const
-    {
-       // see btrfsprogs source code
-       return stat.st_ino == 256 && S_ISDIR(stat.st_mode);
-    }
-
-
-    bool
-    Btrfs::create_subvolume(int fddst, const string& name) const
-    {
-       struct btrfs_ioctl_vol_args args;
-       memset(&args, 0, sizeof(args));
-
-       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
-
-       return ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args) == 0;
-    }
-
-
-    bool
-    Btrfs::create_snapshot(int fd, int fddst, const string& name) const
-    {
-       struct btrfs_ioctl_vol_args_v2 args_v2;
-       memset(&args_v2, 0, sizeof(args_v2));
-
-       args_v2.fd = fd;
-       args_v2.flags = BTRFS_SUBVOL_RDONLY;
-       strncpy(args_v2.name, name.c_str(), sizeof(args_v2.name) - 1);
-
-       if (ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args_v2) == 0)
-           return true;
-       else if (errno != ENOTTY && errno != EINVAL)
-           return false;
-
-       struct btrfs_ioctl_vol_args args;
-       memset(&args, 0, sizeof(args));
-
-       args.fd = fd;
-       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
-
-       return ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args) == 0;
-    }
-
-
-    bool
-    Btrfs::delete_subvolume(int fd, const string& name) const
-    {
-       struct btrfs_ioctl_vol_args args;
-       memset(&args, 0, sizeof(args));
-
-       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
-
-       return ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args) == 0;
-    }
-
-
 #ifdef HAVE_LIBBTRFS
 
 
index ebdda705eb7895e4d2866eb1df8c3f908e876058..da459bf96f43248d813e4bcf8edb7d37aa83dee8 100644 (file)
@@ -60,14 +60,6 @@ namespace snapper
 
        virtual void cmpDirs(const SDir& dir1, const SDir& dir2, cmpdirs_cb_t cb) const;
 
-    private:
-
-       bool is_subvolume(const struct stat& stat) const;
-
-       bool create_subvolume(int fddst, const string& name) const;
-       bool create_snapshot(int fd, int fddst, const string& name) const;
-       bool delete_subvolume(int fd, const string& name) const;
-
     };
 
 }
diff --git a/snapper/BtrfsUtils.cc b/snapper/BtrfsUtils.cc
new file mode 100644 (file)
index 0000000..1882887
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) [2011-2013] Novell, Inc.
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#include "config.h"
+
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <asm/types.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#ifdef HAVE_LIBBTRFS
+#include <btrfs/ioctl.h>
+#endif
+
+#include "snapper/Log.h"
+#include "snapper/BtrfsUtils.h"
+
+
+#ifndef HAVE_LIBBTRFS
+
+#define BTRFS_IOCTL_MAGIC 0x94
+#define BTRFS_PATH_NAME_MAX 4087
+#define BTRFS_SUBVOL_NAME_MAX 4039
+#define BTRFS_SUBVOL_RDONLY (1ULL << 1)
+
+#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, struct btrfs_ioctl_vol_args)
+#define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, struct btrfs_ioctl_vol_args_v2)
+
+struct btrfs_ioctl_vol_args
+{
+    __s64 fd;
+    char name[BTRFS_PATH_NAME_MAX + 1];
+};
+
+struct btrfs_ioctl_vol_args_v2
+{
+    __s64 fd;
+    __u64 transid;
+    __u64 flags;
+    __u64 unused[4];
+    char name[BTRFS_SUBVOL_NAME_MAX + 1];
+};
+
+#endif
+
+
+namespace snapper
+{
+
+    bool
+    is_subvolume(const struct stat& stat)
+    {
+       // see btrfsprogs source code
+       return stat.st_ino == 256 && S_ISDIR(stat.st_mode);
+    }
+
+
+    bool
+    create_subvolume(int fddst, const string& name)
+    {
+       struct btrfs_ioctl_vol_args args;
+       memset(&args, 0, sizeof(args));
+
+       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
+
+       return ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args) == 0;
+    }
+
+
+    bool
+    create_snapshot(int fd, int fddst, const string& name)
+    {
+       struct btrfs_ioctl_vol_args_v2 args_v2;
+       memset(&args_v2, 0, sizeof(args_v2));
+
+       args_v2.fd = fd;
+       args_v2.flags = BTRFS_SUBVOL_RDONLY;
+       strncpy(args_v2.name, name.c_str(), sizeof(args_v2.name) - 1);
+
+       if (ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args_v2) == 0)
+           return true;
+       else if (errno != ENOTTY && errno != EINVAL)
+           return false;
+
+       struct btrfs_ioctl_vol_args args;
+       memset(&args, 0, sizeof(args));
+
+       args.fd = fd;
+       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
+
+       return ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args) == 0;
+    }
+
+
+    bool
+    delete_subvolume(int fd, const string& name)
+    {
+       struct btrfs_ioctl_vol_args args;
+       memset(&args, 0, sizeof(args));
+
+       strncpy(args.name, name.c_str(), sizeof(args.name) - 1);
+
+       return ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args) == 0;
+    }
+
+}
diff --git a/snapper/BtrfsUtils.h b/snapper/BtrfsUtils.h
new file mode 100644 (file)
index 0000000..ca7b028
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) [2011-2013] Novell, Inc.
+ *
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as published
+ * by the Free Software Foundation.
+ *
+ * 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, contact Novell, Inc.
+ *
+ * To contact Novell about this file by physical or electronic mail, you may
+ * find current contact information at www.novell.com.
+ */
+
+
+#ifndef SNAPPER_BTRFS_UTILS_H
+#define SNAPPER_BTRFS_UTILS_H
+
+
+namespace snapper
+{
+
+    bool is_subvolume(const struct stat& stat);
+
+    bool create_subvolume(int fddst, const string& name);
+    bool create_snapshot(int fd, int fddst, const string& name);
+    bool delete_subvolume(int fd, const string& name);
+
+}
+
+
+#endif
index fba548da46983f5dcf08e17ed974a7256f1698b2..a4a8c6020933d0a3e282454c5a76f96b1a8d28c0 100644 (file)
@@ -36,7 +36,8 @@ libsnapper_la_SOURCES =                                       \
 
 if ENABLE_BTRFS
 libsnapper_la_SOURCES +=                               \
-       Btrfs.cc                Btrfs.h
+       Btrfs.cc                Btrfs.h                 \
+       BtrfsUtils.cc           BtrfsUtils.h
 endif
 
 if ENABLE_EXT4