]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added function statvfs to class SDir
authorArvin Schnell <aschnell@suse.de>
Fri, 18 Sep 2020 14:34:58 +0000 (16:34 +0200)
committerArvin Schnell <aschnell@suse.de>
Fri, 18 Sep 2020 14:34:58 +0000 (16:34 +0200)
snapper/FileUtils.cc
snapper/FileUtils.h
snapper/Snapper.cc

index 0b3c453dc9296ad6f73e3b3257563f7b1fcb30b9..4a4d85504cfbaeb86d95e9db26b771411d2b8012 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <sys/mount.h>
 #include <sys/xattr.h>
+#include <sys/statvfs.h>
 #include <fcntl.h>
 #include <stddef.h>
 #include <dirent.h>
@@ -386,6 +387,21 @@ namespace snapper
     }
 
 
+    std::pair<unsigned long long, unsigned long long>
+    SDir::statvfs() const
+    {
+       struct statvfs64 fsbuf;
+       if (fstatvfs64(dirfd, &fsbuf) != 0)
+           SN_THROW(IOErrorException(sformat("statvfs64 failed path:%s errno:%d (%s)", base_path.c_str(),
+                                             errno, stringerror(errno).c_str())));
+
+       // f_bavail is used (not f_bfree) since df seems to do the
+       // same. Thus it allows the user to check the result easily.
+
+       return make_pair(fsbuf.f_blocks * fsbuf.f_bsize, fsbuf.f_bavail * fsbuf.f_bsize);
+    }
+
+
     int
     SDir::mktemp(string& name) const
     {
index 3cd2e56e2b3dd473e91b465bdd10dbc3e771a39e..b0a35f7e6c2d7e874c11b8a2d99c524beea3cf70 100644 (file)
@@ -93,6 +93,9 @@ namespace snapper
        int rename(const string& oldname, const string& newname) const;
        int fsync() const;
 
+       // Query size and free.
+       std::pair<unsigned long long, unsigned long long> statvfs() const;
+
        int mktemp(string& name) const;
        bool mkdtemp(string& name) const;
 
index 23086b1a8347102276ea37e02d3b6507c0f941a8..3366773b43e79b0a456043ffd1ef8fce60d4615f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) [2011-2015] Novell, Inc.
- * Copyright (c) [2016,2018] SUSE LLC
+ * Copyright (c) [2016-2020] SUSE LLC
  *
  * All Rights Reserved.
  *
@@ -25,7 +25,6 @@
 
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/statvfs.h>
 #include <glob.h>
 #include <string.h>
 #include <mntent.h>
@@ -795,13 +794,9 @@ namespace snapper
        quota_rescan(general_dir.fd());
        sync(general_dir.fd());
 
-       struct statvfs64 fsbuf;
-       if (fstatvfs64(general_dir.fd(), &fsbuf) != 0)
-           SN_THROW(QuotaException("statvfs64 failed"));
-
        QuotaData quota_data;
 
-       quota_data.size = fsbuf.f_blocks * fsbuf.f_bsize;
+       std::tie(quota_data.size, std::ignore) = general_dir.statvfs();
 
        QGroupUsage qgroup_usage = qgroup_query_usage(general_dir.fd(), btrfs->getQGroup());
        quota_data.used = qgroup_usage.exclusive;
@@ -833,17 +828,8 @@ namespace snapper
 
        filesystem->sync();
 
-       struct statvfs64 fsbuf;
-       if (fstatvfs64(general_dir.fd(), &fsbuf) != 0)
-           SN_THROW(FreeSpaceException("statvfs64 failed"));
-
        FreeSpaceData free_space_data;
-
-       // f_bavail is used (not f_bfree) since df seems to do the
-       // same. Thus it allows the user to check the result easily.
-
-       free_space_data.size = fsbuf.f_blocks * fsbuf.f_bsize;
-       free_space_data.free = fsbuf.f_bavail * fsbuf.f_bsize;
+       std::tie(free_space_data.size, free_space_data.free) = general_dir.statvfs();
 
        y2mil("size:" << free_space_data.size << " free:" << free_space_data.free);