]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Add CmdMetaParse to parse info.xml checksum and content for snbk
authorCheng-Ling Lai <jameslai.tech@gmail.com>
Wed, 29 Jul 2026 07:03:21 +0000 (15:03 +0800)
committerCheng-Ling Lai <jameslai.tech@gmail.com>
Mon, 3 Aug 2026 01:12:13 +0000 (09:12 +0800)
12 files changed:
README.md
client/snbk/BackupConfig.cc
client/snbk/BackupConfig.h
client/snbk/CmdMetaParse.cc [new file with mode: 0644]
client/snbk/CmdMetaParse.h [new file with mode: 0644]
client/snbk/Makefile.am
configure.ac
dists/debian/control
dists/debian/snapper-Debian.dsc.in.in
dists/debian/snapper-Raspbian.dsc.in.in
dists/debian/snapper-xUbuntu.dsc.in.in
snapper.spec.in

index 2a7b0edfdb1b7fb4c8e6fc24a73abf1be38e76c6..0bfddfd577155e3290e27663c87d0804fba23975 100644 (file)
--- a/README.md
+++ b/README.md
@@ -28,7 +28,7 @@ sudo zypper install -t pattern SDK-C-C++
 sudo zypper install -t pattern devel_C_C++
 # install the extra packages for snapper development (both SLE and openSUSE)
 sudo zypper install git libmount-devel dbus-1-devel libacl-devel libboost_thread-devel libjson-c-devel \
-  docbook-xsl-stylesheets libxml2-devel libbtrfs-devel
+  docbook-xsl-stylesheets libxml2-devel libbtrfs-devel libopenssl-devel
 ```
 
 In the RHEL-based distributions, you can install the needed
@@ -41,7 +41,7 @@ sudo dnf groupinstall "Development Tools"
 sudo dnf install git libmount-devel dbus-devel libacl-devel \
     docbook-xsl libxml2-devel btrfs-progs-devel \
     cmake make automake gcc-c++ systemd-devel boost-devel \
-    ncurses-devel json-c-devel pam-devel
+    ncurses-devel json-c-devel pam-devel openssl-devel
 ```
 ### Building Snapper
 
index 35b2633ebba0350e7967b1f4ba8afb7d2d74ebca..ab72d390ea6585ae089c933bbbcc18b2991e1f59 100644 (file)
@@ -72,6 +72,7 @@ namespace snapper
        get_child_nodes(json_file.get_root(), "receive-options", receive_options);
 
        get_child_value(json_file.get_root(), "target-btrfs-bin", target_btrfs_bin);
+       get_child_value(json_file.get_root(), "target-cat-bin", target_cat_bin);
        get_child_value(json_file.get_root(), "target-ls-bin", target_ls_bin);
        get_child_value(json_file.get_root(), "target-mkdir-bin", target_mkdir_bin);
        get_child_value(json_file.get_root(), "target-rm-bin", target_rm_bin);
index f8c390157d763929ad4ca6ab6ed84c29b6230987..ce37075b2af0b7db832e824ea25b2334bed1f226 100644 (file)
@@ -75,6 +75,7 @@ namespace snapper
        vector<string> receive_options;
 
        string target_btrfs_bin = BTRFS_BIN;
+       string target_cat_bin = CAT_BIN;
        string target_ls_bin = LS_BIN;
        string target_mkdir_bin = MKDIR_BIN;
        string target_rm_bin = RM_BIN;
diff --git a/client/snbk/CmdMetaParse.cc b/client/snbk/CmdMetaParse.cc
new file mode 100644 (file)
index 0000000..7523689
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2026 SUSE LLC
+ *
+ * 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 <iomanip>
+#include <iostream>
+#include <sstream>
+
+#include <boost/algorithm/string.hpp>
+#include <openssl/sha.h>
+
+#include <snapper/Enum.h>
+#include <snapper/LoggerImpl.h>
+#include <snapper/SystemCmd.h>
+#include <snapper/XmlFile.h>
+
+#include "CmdMetaParse.h"
+
+#include "../misc.h"
+
+
+namespace snapper
+{
+    CmdMetaParse::CmdMetaParse(const Shell& shell, const string& snapshot_dir,
+                               const string& cat_bin)
+        : path(snapshot_dir + "/info.xml")
+    {
+       SystemCmd::Args cmd_args = { cat_bin, path };
+       SystemCmd cmd(shellify(shell, cmd_args));
+
+       if (cmd.retcode() != 0)
+       {
+           y2err("command '" << cmd.cmd() << "' failed: " << cmd.retcode());
+           for (const string& tmp : cmd.get_stdout())
+               y2err(tmp);
+           for (const string& tmp : cmd.get_stderr())
+               y2err(tmp);
+       }
+
+       content = boost::join(cmd.get_stdout(), "");
+
+       y2mil(*this);
+    }
+
+
+    string CmdMetaParse::get_checksum() const
+    {
+       if (!content.length())
+           return "";
+
+       unsigned char hash[SHA256_DIGEST_LENGTH];
+       SHA256(reinterpret_cast<const unsigned char*>(content.c_str()), content.length(),
+              hash);
+
+       std::stringstream ss;
+       for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
+       {
+           ss << std::hex << std::setw(2) << std::setfill('0')
+              << static_cast<int>(hash[i]);
+       }
+
+       return ss.str();
+    }
+
+
+    SnapshotMeta CmdMetaParse::get_meta() const
+    {
+       SnapshotMeta meta;
+
+       if (!content.length())
+       {
+           // The `info.xml` file might be missing.
+           // Using fallback values for snapshot metadata.
+           return meta;
+       }
+
+       XmlFile file(XmlFile::FromString, content);
+       const xmlNode* node = file.getRootElement();
+       string tmp;
+
+       if (!getChildValue(node, "type", tmp) || !toValue(tmp, meta.type, true))
+       {
+           // Log error for the missing attribute and retain the default value.
+           y2err("The type attribute is missing from " << path);
+       }
+
+       getChildValue(node, "pre_num", meta.pre_num);
+       getChildValue(node, "cleanup", meta.cleanup);
+
+       for (const xmlNode* tmp : getChildNodes(node, "userdata"))
+       {
+           string key, value;
+           getChildValue(tmp, "key", key);
+           getChildValue(tmp, "value", value);
+           if (!key.empty())
+               meta.userdata[key] = value;
+       }
+
+       return meta;
+    }
+
+
+    std::ostream& operator<<(std::ostream& s, const CmdMetaParse& cmd_metaparse)
+    {
+       SnapshotMeta meta = cmd_metaparse.get_meta();
+       s << "path: " << cmd_metaparse.path
+         << ", checksum: " << cmd_metaparse.get_checksum()
+         << ", type: " << toString(meta.type) << ", pre_num: " << meta.pre_num
+         << ", cleanup: " << meta.cleanup
+         << ", userdata: " << show_userdata(meta.userdata) << '\n';
+
+       return s;
+    }
+
+
+} // namespace snapper
diff --git a/client/snbk/CmdMetaParse.h b/client/snbk/CmdMetaParse.h
new file mode 100644 (file)
index 0000000..790cd3c
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2026 SUSE LLC
+ *
+ * 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_CMD_METAPARSE_H
+#define SNAPPER_CMD_METAPARSE_H
+
+
+#include <snapper/Snapshot.h>
+
+#include "Shell.h"
+
+
+namespace snapper
+{
+    using std::string;
+
+
+    struct SnapshotMeta
+    {
+       SnapshotType type = SnapshotType::SINGLE;
+       unsigned int pre_num = 0;
+       string cleanup;
+       map<string, string> userdata;
+    };
+
+
+    class CmdMetaParse
+    {
+    public:
+
+       CmdMetaParse(const Shell& shell, const string& snapshot_dir,
+                    const string& cat_bin);
+
+       string get_checksum() const;
+       SnapshotMeta get_meta() const;
+
+       friend std::ostream& operator<<(std::ostream& s,
+                                       const CmdMetaParse& cmd_metaparse);
+
+    private:
+
+       string path;
+       string content = "";
+    };
+
+} // namespace snapper
+
+
+#endif
index 6adc05f74bd1ec2e01301a002a6cac6b40b8f7c0..1523820109d45e4c75d7aab05e4ac80cf28a7ebc 100644 (file)
@@ -2,7 +2,8 @@
 # Makefile.am for snapper/client/snbk
 #
 
-AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS) $(JSON_C_CFLAGS)
+AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS) $(JSON_C_CFLAGS) $(CRYPTO_CFLAGS) \
+       $(XML2_CFLAGS)
 
 sbin_PROGRAMS = snbk
 
@@ -23,13 +24,17 @@ snbk_SOURCES =                                              \
        CmdBtrfs.cc             CmdBtrfs.h              \
        CmdLs.cc                CmdLs.h                 \
        CmdChecksum.cc          CmdChecksum.h           \
+       CmdMetaParse.cc         CmdMetaParse.h          \
        JsonFile.cc             JsonFile.h              \
        utils.cc                utils.h                 \
-       TreeView.cc             TreeView.h
+       TreeView.cc             TreeView.h              \
+       ../misc.cc              ../misc.h
 
 snbk_LDADD =                           \
        ../../snapper/libsnapper.la     \
        ../../dbus/libdbus.la           \
        ../proxy/libproxy.la            \
        ../proxy/libclient.la           \
-        $(JSON_C_LIBS)
+        $(JSON_C_LIBS)                 \
+        $(CRYPTO_LIBS)                 \
+        $(XML2_LIBS)
index 013a79c7ed1b8e77732bc51af16d2598920d1fbe..130d2eed0216632573abb1bf4081c03b73839403 100644 (file)
@@ -27,6 +27,7 @@ AC_PREFIX_DEFAULT(/usr)
 AC_PATH_PROG([XSLTPROC], [xsltproc], [/usr/bin/xsltproc])
 
 AC_PATH_PROG([BTRFS_BIN], [btrfs], [/usr/sbin/btrfs])
+AC_PATH_PROG([CAT_BIN], [cat], [/usr/bin/cat])
 AC_PATH_PROG([CHATTR_BIN], [chattr], [/usr/bin/chattr])
 AC_PATH_PROG([CHSNAP_BIN], [chsnap], [/sbin/chsnap])
 AC_PATH_PROG([CP_BIN], [cp], [/bin/cp])
@@ -45,6 +46,7 @@ AC_PATH_PROG([SHA256SUM_BIN], [sha256sum], [/usr/bin/sha256sum])
 AC_PATH_PROG([TOUCH_BIN], [touch], [/usr/bin/touch])
 
 AC_DEFINE_UNQUOTED([BTRFS_BIN], ["$BTRFS_BIN"], [Path of btrfs program.])
+AC_DEFINE_UNQUOTED([CAT_BIN], ["$CAT_BIN"], [Path of cat program.])
 AC_DEFINE_UNQUOTED([CHATTR_BIN], ["$CHATTR_BIN"], [Path of chattr program.])
 AC_DEFINE_UNQUOTED([CHSNAP_BIN], ["$CHSNAP_BIN"], [Path of chsnap program.])
 AC_DEFINE_UNQUOTED([CP_BIN], ["$CP_BIN"], [Path of cp program.])
@@ -195,6 +197,7 @@ fi
 PKG_CHECK_MODULES(DBUS, dbus-1)
 PKG_CHECK_MODULES(XML2, libxml-2.0)
 PKG_CHECK_MODULES(JSON_C, json-c, [], [AC_MSG_WARN([Cannot find json-c. Please install libjson-c-devel])])
+PKG_CHECK_MODULES([CRYPTO], [libcrypto])
 PKG_CHECK_MODULES(ZLIB, zlib)
 
 # Conditional support for libbtrfsutil based on existence of pkg-config file
index 023958d6b053e16c52e463a51d429ace685ac666..c6fa54ca4f7fe3eebb4b2e3e7523d0d5166c7c40 100644 (file)
@@ -2,7 +2,7 @@ Source: snapper
 Priority: optional
 Section: admin
 Maintainer: Arvin Schnell <aschnell@suse.com>
-Build-Depends: debhelper (>= 4.1.16), btrfs-progs, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libpam-dev, xsltproc, docbook-xsl
+Build-Depends: debhelper (>= 4.1.16), btrfs-progs, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libpam-dev, xsltproc, docbook-xsl, libssl-dev
 Homepage: http://en.opensuse.org/Portal:Snapper
 
 Package: snapper
index 01cc77dc639b6b47658f17b72dfaa762de04a597..7e9837411ec44bef082973eec8b445ca53d30ca9 100644 (file)
@@ -4,7 +4,7 @@ Version: @VERSION@-1
 Binary: snapper
 Maintainer: Arvin Schnell <aschnell@suse.com>
 Architecture: any
-Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, locales-all, ncurses-dev
+Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, locales-all, ncurses-dev, libssl-dev
 #
 # The 'Files' line is generated during 'make package':
 # Files:
index 01cc77dc639b6b47658f17b72dfaa762de04a597..7e9837411ec44bef082973eec8b445ca53d30ca9 100644 (file)
@@ -4,7 +4,7 @@ Version: @VERSION@-1
 Binary: snapper
 Maintainer: Arvin Schnell <aschnell@suse.com>
 Architecture: any
-Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, locales-all, ncurses-dev
+Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, locales-all, ncurses-dev, libssl-dev
 #
 # The 'Files' line is generated during 'make package':
 # Files:
index cbdb9df795b4bb4c4fbbecc081f2bf41f8c590a3..1dacaf0f3ea202162571ba01f5976c01a7374673 100644 (file)
@@ -4,7 +4,7 @@ Version: @VERSION@-1
 Binary: snapper
 Maintainer: Arvin Schnell <aschnell@suse.com>
 Architecture: any
-Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, language-pack-en, language-pack-de, language-pack-fr, ncurses-dev
+Build-Depends: debhelper (>= 4.1.16), acl-dev, btrfs-progs, dbus, g++, libboost-dev, libboost-thread-dev, libboost-test-dev, libxml2-dev, libz-dev, libdbus-1-dev, libjson-c-dev, libpam-dev, xsltproc, docbook-xsl, language-pack-en, language-pack-de, language-pack-fr, ncurses-dev, libssl-dev
 #
 # The 'Files' line is generated during 'make package':
 # Files:
index 53371e8619b8a8d99d8ef8faf9bc19e367c3d2d1..dab80a120688278c4df45dd7bd8bf2d611634af9 100644 (file)
@@ -106,6 +106,11 @@ BuildRequires:  zlib-devel
 %if %{with coverage}
 BuildRequires:  lcov
 %endif
+%if 0%{?suse_version}
+BuildRequires:  libopenssl-devel
+%else
+BuildRequires:  openssl-devel
+%endif
 Requires:       dbus-service
 Requires:       diffutils
 Requires:       libsnapper@LIBVERSION_MAJOR@ = %version