Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/snbk/BackupConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace snapper
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);
get_child_value(json_file.get_root(), "target-rmdir-bin", target_rmdir_bin);
get_child_value(json_file.get_root(), "target-sha256sum-bin", target_sha256sum_bin);
}


Expand Down
1 change: 1 addition & 0 deletions client/snbk/BackupConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace snapper
string target_mkdir_bin = MKDIR_BIN;
string target_rm_bin = RM_BIN;
string target_rmdir_bin = RMDIR_BIN;
string target_sha256sum_bin = SHA256SUM_BIN;

private:

Expand Down
125 changes: 125 additions & 0 deletions client/snbk/CmdMetaHash.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 <boost/algorithm/string.hpp>

#include <snapper/AppUtil.h>
#include <snapper/Exception.h>
#include <snapper/LoggerImpl.h>
#include <snapper/SnapperDefines.h>
#include <snapper/SystemCmd.h>

#include "../utils/text.h"

#include "CmdMetaHash.h"


namespace snapper
{

CmdMetaHash::CmdMetaHash(const Shell& shell, const string& chksum_bin,
const string& sh_bin, const string& path)
: path(path)
{
SystemCmd::Args cmd_args = {
sh_bin, "-c",
sformat("for d in $(ls -1 %s); do %s %s/$d/info.xml; done", path.c_str(),
chksum_bin.c_str(), path.c_str())
};
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);

SN_THROW(Exception(_("Hashing for snapshot metadata failed.")));
}

parse(cmd.get_stdout());

y2mil(*this);
}


const string& CmdMetaHash::get_hash(unsigned int num) const
{
auto pair = lookup.find(num);
if (pair == lookup.end())
{
string error = sformat(_("Meta hash of snapshot %d not found."), num);
SN_THROW(Exception(error));
}

return pair->second;
}


void CmdMetaHash::parse(const vector<string>& lines)
{
for (const string& line : lines)
{
// Extract the hash and the path
vector<string> parts;
boost::split(parts, line, boost::is_any_of(" "), boost::token_compress_on);
if (parts.size() != 2)
{
y2err("Invalid hash string: " << line);
SN_THROW(Exception(_("Invalid hash output format.")));
}

const string& hash = parts.front();
const string& path = parts.back();

// Split the path into components
vector<string> comps;
boost::split(comps, path, boost::is_any_of("/"), boost::token_compress_on);
if (comps.size() < 2)
{
SN_THROW(Exception(_("Unexpected path format.")));
}

// Convert the snapshot number (second‑to‑last component)
unsigned int num = stoi(comps[comps.size() - 2]);

// Store the hash
lookup[num] = hash;
}
}


std::ostream& operator<<(std::ostream& s, const CmdMetaHash& cmd_metahash)
{
s << "path: " << cmd_metahash.path << '\n';
for (const auto& [num, hash] : cmd_metahash.lookup)
{
s << sformat(" num: %d, hash: %s\n", num, hash.c_str());
}

return s;
}


} // namespace snapper
63 changes: 63 additions & 0 deletions client/snbk/CmdMetaHash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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_META_HASH_H
#define SNAPPER_CMD_META_HASH_H


#include <map>

#include "Shell.h"


namespace snapper
{
using std::string;


/**
* Find the hashes (by checksum) of info.xml for all snapshots.
*/
class CmdMetaHash
{
public:

CmdMetaHash(const Shell& shell, const string& chksum_bin, const string& sh_bin,
const string& path);

const string& get_hash(unsigned int num) const;

friend std::ostream& operator<<(std::ostream& s, const CmdMetaHash& cmd_metahash);

private:

const string path;

std::map<unsigned int, string> lookup;

void parse(const std::vector<string>& lines);
};

} // namespace snapper


#endif
1 change: 1 addition & 0 deletions client/snbk/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ snbk_SOURCES = \
Shell.cc Shell.h \
CmdBtrfs.cc CmdBtrfs.h \
CmdLs.cc CmdLs.h \
CmdMetaHash.cc CmdMetaHash.h \
JsonFile.cc JsonFile.h \
utils.cc utils.h \
TreeView.cc TreeView.h
Expand Down
Loading
Loading