]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added checks for btrfs subvolumes
authorArvin Schnell <aschnell@suse.de>
Wed, 12 Sep 2012 10:03:22 +0000 (12:03 +0200)
committerArvin Schnell <aschnell@suse.de>
Wed, 12 Sep 2012 10:03:22 +0000 (12:03 +0200)
snapper/Filesystem.cc
snapper/Filesystem.h

index f6d054de761d5a0cf3cdbef4269a8ecfad737a40..19d18ce06509aa8a6979cf0363bdaa15d25e9b19 100644 (file)
@@ -72,7 +72,7 @@ namespace snapper
 {
 
     vector<string>
-    filter_mount_options(vector<string>& options)
+    filter_mount_options(const vector<string>& 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
     {
index 3c4c3330c533aa39bd2d5d821980be5c413182ad..c1fb0fad140de7bf5355776069fd3fee431d3460 100644 (file)
@@ -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;