From: Arvin Schnell Date: Wed, 12 Sep 2012 10:03:22 +0000 (+0200) Subject: - added checks for btrfs subvolumes X-Git-Tag: v0.1.3~97 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0509909a5e25d988bf0e6b604e9565b44bc0313c;p=thirdparty%2Fsnapper.git - added checks for btrfs subvolumes --- diff --git a/snapper/Filesystem.cc b/snapper/Filesystem.cc index f6d054de..19d18ce0 100644 --- a/snapper/Filesystem.cc +++ b/snapper/Filesystem.cc @@ -72,7 +72,7 @@ namespace snapper { vector - filter_mount_options(vector& options) + filter_mount_options(const vector& options) { static const char* ign_opt[] = { "ro", "rw", @@ -129,9 +129,9 @@ namespace snapper } #ifdef UMOUNT_NOFOLLOW - int r2 = umount2(mount_point.c_str(), UMOUNT_NOFOLLOW); + int r2 = ::umount2(mount_point.c_str(), UMOUNT_NOFOLLOW); #else - int r2 = umount2(mount_point.c_str(), 0); + int r2 = ::umount2(mount_point.c_str(), 0); #endif if (r2 != 0) { @@ -248,6 +248,27 @@ namespace snapper } + SDir + Btrfs::openSubvolumeDir() const + { + SDir subvolume_dir = Filesystem::openSubvolumeDir(); + + struct stat stat; + if (subvolume_dir.stat(&stat) != 0) + { + throw IOErrorException(); + } + + if (!is_subvolume(stat)) + { + y2err("subvolume is not a btrfs snapshot"); + throw IOErrorException(); + } + + return subvolume_dir; + } + + SDir Btrfs::openInfosDir() const { @@ -260,6 +281,12 @@ namespace snapper throw IOErrorException(); } + if (!is_subvolume(stat)) + { + y2err(".snapshots is not a btrfs snapshot"); + throw IOErrorException(); + } + if (stat.st_uid != 0 || stat.st_gid != 0) { y2err("owner/group of .snapshots wrong"); @@ -341,8 +368,7 @@ namespace snapper struct stat stat; int r = info_dir.stat("snapshot", &stat, AT_SYMLINK_NOFOLLOW); - // check st_ino == 256 is copied from btrfsprogs - return r == 0 && stat.st_ino == 256 && S_ISDIR(stat.st_mode); + return r == 0 && is_subvolume(stat); } catch (const IOErrorException& e) { @@ -351,6 +377,14 @@ 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 { diff --git a/snapper/Filesystem.h b/snapper/Filesystem.h index 3c4c3330..c1fb0fad 100644 --- a/snapper/Filesystem.h +++ b/snapper/Filesystem.h @@ -94,6 +94,7 @@ namespace snapper virtual string snapshotDir(unsigned int num) const; + virtual SDir openSubvolumeDir() const; virtual SDir openInfosDir() const; virtual SDir openSnapshotDir(unsigned int num) const; @@ -108,6 +109,8 @@ namespace snapper 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;