From: Arvin Schnell Date: Tue, 1 Oct 2013 15:24:51 +0000 (+0200) Subject: - moved btrfs ioctl calls to separate file X-Git-Tag: v0.1.7~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2cfc80d39180bf40546b5846f54afda7c84742c9;p=thirdparty%2Fsnapper.git - moved btrfs ioctl calls to separate file --- diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 57f11782..2c110c30 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -42,42 +42,13 @@ #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 diff --git a/snapper/Btrfs.h b/snapper/Btrfs.h index ebdda705..da459bf9 100644 --- a/snapper/Btrfs.h +++ b/snapper/Btrfs.h @@ -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 index 00000000..1882887f --- /dev/null +++ b/snapper/BtrfsUtils.cc @@ -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 +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_LIBBTRFS +#include +#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 index 00000000..ca7b028d --- /dev/null +++ b/snapper/BtrfsUtils.h @@ -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 diff --git a/snapper/Makefile.am b/snapper/Makefile.am index fba548da..a4a8c602 100644 --- a/snapper/Makefile.am +++ b/snapper/Makefile.am @@ -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