From: Arvin Schnell Date: Thu, 11 Oct 2018 09:24:48 +0000 (+0200) Subject: - added functions to query default and active snapshot X-Git-Tag: v0.7.1~7^2~5 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=93004a2dcb2b597407eafe9cfaf666bf7e7f950d;p=thirdparty%2Fsnapper.git - added functions to query default and active snapshot --- diff --git a/LIBVERSION b/LIBVERSION index 6aba2b24..80895903 100644 --- a/LIBVERSION +++ b/LIBVERSION @@ -1 +1 @@ -4.2.0 +4.3.0 diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 6e98e252..b3bafb4a 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -1453,13 +1453,9 @@ namespace snapper std::pair - Btrfs::getDefault() const + Btrfs::idToNum(int fd, subvolid_t id) const { - SDir subvolume_dir = openSubvolumeDir(); - - subvolid_t id = get_default_id(subvolume_dir.fd()); - - string path = get_subvolume(subvolume_dir.fd(), id); + string path = get_subvolume(fd, id); Regex rx("/([0-9]+)/snapshot$"); if (!rx.match(path)) @@ -1474,6 +1470,16 @@ namespace snapper } + std::pair + Btrfs::getDefault() const + { + SDir subvolume_dir = openSubvolumeDir(); + int fd = subvolume_dir.fd(); + + return idToNum(fd, get_default_id(fd)); + } + + void Btrfs::setDefault(unsigned int num) const { @@ -1501,6 +1507,16 @@ namespace snapper } + std::pair + Btrfs::getActive() const + { + SDir subvolume_dir = openSubvolumeDir(); + int fd = subvolume_dir.fd(); + + return idToNum(fd, get_id(fd)); + } + + bool Btrfs::isActive(unsigned int num) const { @@ -1528,28 +1544,40 @@ namespace snapper bool Btrfs::isDefault(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } std::pair Btrfs::getDefault() const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } void Btrfs::setDefault(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); + } + + + std::pair + Btrfs::getActive() const + { + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } bool Btrfs::isActive(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } #endif diff --git a/snapper/Btrfs.h b/snapper/Btrfs.h index 3d312898..6f766b62 100644 --- a/snapper/Btrfs.h +++ b/snapper/Btrfs.h @@ -89,6 +89,8 @@ namespace snapper virtual bool isActive(unsigned int num) const; + virtual std::pair getActive() const; + virtual void sync() const; virtual qgroup_t getQGroup() const { return qgroup; } @@ -102,6 +104,12 @@ namespace snapper void addToFstabHelper(const string& default_subvolume_name) const; void removeFromFstabHelper() const; + /** + * Find the snapper snapshot number corresponding to the btrfs + * subvol id. + */ + std::pair idToNum(int fd, subvolid_t id) const; + }; } diff --git a/snapper/Exception.h b/snapper/Exception.h index dcadfa34..5cefc779 100644 --- a/snapper/Exception.h +++ b/snapper/Exception.h @@ -1,6 +1,6 @@ /* * Copyright (c) [2011-2014] Novell, Inc. - * Copyright (c) [2015] SUSE LLC + * Copyright (c) [2015,2018] SUSE LLC * * All Rights Reserved. * @@ -393,6 +393,11 @@ namespace snapper explicit InvalidGroupException() : Exception("invalid group") {} }; + struct UnsupportedException : public Exception + { + explicit UnsupportedException() : Exception("unsupported") {} + }; + } diff --git a/snapper/Filesystem.cc b/snapper/Filesystem.cc index 204ae98c..6c705523 100644 --- a/snapper/Filesystem.cc +++ b/snapper/Filesystem.cc @@ -1,6 +1,6 @@ /* * Copyright (c) [2011-2015] Novell, Inc. - * Copyright (c) [2016-2017] SUSE LLC + * Copyright (c) [2016-2018] SUSE LLC * * All Rights Reserved. * @@ -166,35 +166,48 @@ namespace snapper void Filesystem::createSnapshotOfDefault(unsigned int num, bool read_only, bool quota) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } bool Filesystem::isDefault(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } std::pair Filesystem::getDefault() const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } void Filesystem::setDefault(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); + } + + + std::pair + Filesystem::getActive() const + { + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } bool Filesystem::isActive(unsigned int num) const { - throw std::logic_error("not implemented"); + SN_THROW(UnsupportedException()); + __builtin_unreachable(); } diff --git a/snapper/Filesystem.h b/snapper/Filesystem.h index 81ff4169..63b4dd2a 100644 --- a/snapper/Filesystem.h +++ b/snapper/Filesystem.h @@ -1,6 +1,6 @@ /* * Copyright (c) [2011-2015] Novell, Inc. - * Copyright (c) [2016-2017] SUSE LLC + * Copyright (c) [2016-2018] SUSE LLC * * All Rights Reserved. * @@ -94,6 +94,8 @@ namespace snapper virtual void setDefault(unsigned int num) const; + virtual std::pair getActive() const; + virtual bool isActive(unsigned int num) const; virtual void sync() const; diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index 3b1d7c2e..985440f1 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -767,6 +767,24 @@ namespace snapper } + Snapshots::const_iterator + Snapshots::getDefault() const + { + std::pair tmp = snapper->getFilesystem()->getDefault(); + + return tmp.first ? find(tmp.second) : end(); + } + + + Snapshots::const_iterator + Snapshots::getActive() const + { + std::pair tmp = snapper->getFilesystem()->getActive(); + + return tmp.first ? find(tmp.second) : end(); + } + + struct num_is { num_is(unsigned int num) : num(num) {} diff --git a/snapper/Snapshot.h b/snapper/Snapshot.h index 700196bd..dc976456 100644 --- a/snapper/Snapshot.h +++ b/snapper/Snapshot.h @@ -235,6 +235,27 @@ namespace snapper const_iterator getSnapshotCurrent() const { return entries.begin(); } + /** + * Query the default snapshot. Only supported for btrfs. + * + * For btrfs the default btrfs snapshot will be mounted next + * time the filesystem is mounted (unless overridden). + * + * The default btrfs snapshot may not be controlled by snapper + * in which this function returns end(). + */ + const_iterator getDefault() const; + + /** + * Query the active snapshot. Only supported for btrfs. + * + * For btrfs the active btrfs snapshot is the one currently mounted. + * + * The active btrfs snapshot may not be controlled by snapper + * in which this function returns end(). + */ + const_iterator getActive() const; + private: void initialize();