1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
/*
* Copyright (c) [2011-2014] Novell, Inc.
* Copyright (c) [2020-2023] 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 "config.h"
#include <iostream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <snapper/AppUtil.h>
#include <snapper/SystemCmd.h>
#include <snapper/SnapperDefines.h>
#include <snapper/Filesystem.h>
#include "utils/text.h"
#include "misc.h"
unsigned int
read_num(const string& str)
{
istringstream s(str);
unsigned int num = 0;
s >> num;
if (s.fail() || !s.eof())
{
cerr << sformat(_("Invalid snapshot '%s'."), str.c_str()) << endl;
exit(EXIT_FAILURE);
}
return num;
}
map<string, string>
read_userdata(const string& s, const map<string, string>& old)
{
if (s.empty())
{
cerr << _("Empty userdata.") << endl;
exit(EXIT_FAILURE);
}
list<string> tmp;
boost::split(tmp, s, boost::is_any_of(","), boost::token_compress_on);
if (tmp.empty())
{
cerr << _("Empty userdata.") << endl;
exit(EXIT_FAILURE);
}
map<string, string> userdata = old;
for (list<string>::const_iterator it = tmp.begin(); it != tmp.end(); ++it)
{
string::size_type pos = it->find("=");
if (pos == string::npos)
{
cerr << sformat(_("Userdata '%s' does not include '=' sign."), it->c_str()) << endl;
exit(EXIT_FAILURE);
}
string key = boost::trim_copy(it->substr(0, pos));
string value = boost::trim_copy(it->substr(pos + 1));
if (key.empty())
{
cerr << sformat(_("Userdata '%s' has empty key."), it->c_str()) << endl;
exit(EXIT_FAILURE);
}
if (value.empty())
userdata.erase(key);
else
userdata[key] = value;
}
return userdata;
}
string
show_userdata(const map<string, string>& userdata)
{
string s;
for (map<string, string>::const_iterator it = userdata.begin(); it != userdata.end(); ++it)
{
if (!s.empty())
s += ", ";
s += it->first + "=" + it->second;
}
return s;
}
map<string, string>
read_configdata(const vector<string>& v, const map<string, string>& old)
{
if (v.empty())
{
cerr << _("Empty configdata.") << endl;
exit(EXIT_FAILURE);
}
map<string, string> configdata = old;
for (vector<string>::const_iterator it = v.begin(); it != v.end(); ++it)
{
string::size_type pos = it->find("=");
if (pos == string::npos)
{
cerr << sformat(_("Configdata '%s' does not include '=' sign."), it->c_str()) << endl;
exit(EXIT_FAILURE);
}
string key = boost::trim_copy(it->substr(0, pos));
string value = boost::trim_copy(it->substr(pos + 1));
if (key.empty())
{
cerr << sformat(_("Configdata '%s' has empty key."), it->c_str()) << endl;
exit(EXIT_FAILURE);
}
configdata[key] = value;
}
return configdata;
}
string
username(uid_t uid)
{
string username;
gid_t gid;
if (!get_uid_username_gid(uid, username, gid))
return sformat("unknown (%d)", uid);
return username;
}
const Filesystem*
get_filesystem(const ProxyConfig& config, const string& target_root)
{
const map<string, string>& raw = config.getAllValues();
map<string, string>::const_iterator pos1 = raw.find(KEY_FSTYPE);
map<string, string>::const_iterator pos2 = raw.find(KEY_SUBVOLUME);
if (pos1 == raw.end() || pos2 == raw.end())
{
cerr << _("Failed to initialize filesystem handler.") << endl;
exit(EXIT_FAILURE);
}
try
{
return Filesystem::create(pos1->second, pos2->second, target_root);
}
catch (const InvalidConfigException& e)
{
SN_CAUGHT(e);
cerr << _("Failed to initialize filesystem handler.") << endl;
exit(EXIT_FAILURE);
}
}
Differ::Differ()
: command(DIFFBIN " --new-file --unified")
{
}
void
Differ::run(const string& f1, const string& f2) const
{
string tmp = command;
if (!extensions.empty())
tmp += " " + extensions;
tmp += " " + quote(f1) + " " + quote(f2);
SystemCmd cmd(tmp);
for (const string& line : cmd.get_stdout())
cout << line << endl;
for (const string& line : cmd.get_stderr())
cerr << line << endl;
}
|