From: Arvin Schnell Date: Thu, 25 Jul 2013 13:39:16 +0000 (+0200) Subject: - allow to change config via DBus and with command line tool X-Git-Tag: v0.1.6~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=48154979307e9f062d79d78f9bc9f200e11b0fa7;p=thirdparty%2Fsnapper.git - allow to change config via DBus and with command line tool --- diff --git a/client/commands.cc b/client/commands.cc index 127880a8..8123f305 100644 --- a/client/commands.cc +++ b/client/commands.cc @@ -63,6 +63,19 @@ command_get_xconfig(DBus::Connection& conn, const string& config_name) } +void +command_set_xconfig(DBus::Connection& conn, const string& config_name, + const map& raw) +{ + DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "SetConfig"); + + DBus::Hoho hoho(call); + hoho << config_name << raw; + + conn.send_with_reply_and_block(call); +} + + void command_create_xconfig(DBus::Connection& conn, const string& config_name, const string& subvolume, const string& fstype, const string& template_name) diff --git a/client/commands.h b/client/commands.h index 7fe96647..51d1319b 100644 --- a/client/commands.h +++ b/client/commands.h @@ -39,6 +39,10 @@ command_list_xconfigs(DBus::Connection& conn); XConfigInfo command_get_xconfig(DBus::Connection& conn, const string& config_name); +void +command_set_xconfig(DBus::Connection& conn, const string& config_name, + const map& raw); + void command_create_xconfig(DBus::Connection& conn, const string& config_name, const string& subvolume, const string& fstype, const string& template_name); diff --git a/client/snapper.cc b/client/snapper.cc index daf95ccc..54f34b22 100644 --- a/client/snapper.cc +++ b/client/snapper.cc @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -34,6 +36,7 @@ #include #include #include +#include #include "utils/text.h" #include "utils/Table.h" @@ -42,13 +45,6 @@ #include "commands.h" #include "cleanup.h" -#ifdef ENABLE_XATTRS - #include - #include - #include - - #include -#endif using namespace snapper; using namespace std; @@ -324,6 +320,94 @@ command_delete_config(DBus::Connection& conn) } +void +help_get_config() +{ + cout << _(" Get config:") << endl + << _("\tsnapper get-config") << endl + << endl; +} + + +void +command_get_config(DBus::Connection& conn) +{ + getopts.parse("get-config", GetOpts::no_options); + if (getopts.hasArgs()) + { + cerr << _("Command 'get-config' does not take arguments.") << endl; + exit(EXIT_FAILURE); + } + + Table table; + + TableHeader header; + header.add(_("Key")); + header.add(_("Value")); + table.setHeader(header); + + XConfigInfo ci = command_get_xconfig(conn, config_name); + + for (map::const_iterator it = ci.raw.begin(); it != ci.raw.end(); ++it) + { + TableRow row; + row.add(it->first); + row.add(it->second); + table.add(row); + } + + cout << table; +} + + +void +help_set_config() +{ + cout << _(" Set config:") << endl + << _("\tsnapper set-config ") << endl + << endl; +} + + +void +command_set_config(DBus::Connection& conn) +{ + getopts.parse("set-config", GetOpts::no_options); + if (!getopts.hasArgs()) + { + cerr << _("Command 'set-config' needs at least one argument.") << endl; + exit(EXIT_FAILURE); + } + + map raw; + + while (getopts.hasArgs()) + { + string arg = getopts.popArg(); + + string::size_type pos = arg.find("="); + if (pos == string::npos) + { + cerr << _("Invalid configdata.") << endl; + exit(EXIT_FAILURE); + } + + string key = boost::trim_copy(arg.substr(0, pos)); + string value = boost::trim_copy(arg.substr(pos + 1)); + + if (key.empty()) + { + cerr << _("Invalid configdata.") << endl; + exit(EXIT_FAILURE); + } + + raw[key] = value; + } + + command_set_xconfig(conn, config_name, raw); +} + + void help_list() { @@ -1114,6 +1198,7 @@ command_debug(DBus::Connection& conn) #ifdef ENABLE_XATTRS + void help_xa_diff() { @@ -1171,6 +1256,7 @@ command_xa_diff(DBus::Connection& conn) } } } + #endif @@ -1225,6 +1311,8 @@ help() help_list_configs(); help_create_config(); help_delete_config(); + help_get_config(); + help_set_config(); help_list(); help_create(); help_modify(); @@ -1254,6 +1342,8 @@ main(int argc, char** argv) cmds["list-configs"] = command_list_configs; cmds["create-config"] = command_create_config; cmds["delete-config"] = command_delete_config; + cmds["get-config"] = command_get_config; + cmds["set-config"] = command_set_config; cmds["list"] = command_list; cmds["create"] = command_create; cmds["modify"] = command_modify; diff --git a/doc/dbus-protocol.txt b/doc/dbus-protocol.txt index 10178a7c..3629aecf 100644 --- a/doc/dbus-protocol.txt +++ b/doc/dbus-protocol.txt @@ -1,6 +1,7 @@ method ListConfigs method GetConfig config-name +method SetConfig config-name configdata method CreateConfig config-name subvolume fstype template-name diff --git a/doc/snapper.xml.in b/doc/snapper.xml.in index bb84d2eb..89065f0d 100644 --- a/doc/snapper.xml.in +++ b/doc/snapper.xml.in @@ -261,6 +261,24 @@ + + + + Displays the settings of the configuration. + + + + + configdata + + Changes the settings of the configuration. The settings + configdata are a list of key-value-pairs separated + by spaces and the key and value must be separated by an equal sign, + e.g. "NUMBER_CLEANUP=yes NUMBER_LIMIT=10". The value of SUBVOLUME and FSTYPE + cannot be changed. + + + diff --git a/package/snapper.changes b/package/snapper.changes index 13102825..db60a1be 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Thu Jul 25 14:40:31 CEST 2013 - aschnell@suse.de + +- allow to change config via DBus and with command line tool + ------------------------------------------------------------------- Tue Jul 09 14:00:16 CEST 2013 - aschnell@suse.de diff --git a/server/Client.cc b/server/Client.cc index 81fc2bfb..012b36e1 100644 --- a/server/Client.cc +++ b/server/Client.cc @@ -177,6 +177,10 @@ Client::introspect(DBus::Connection& conn, DBus::Message& msg) " \n" " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" @@ -205,6 +209,11 @@ Client::introspect(DBus::Connection& conn, DBus::Message& msg) " \n" " \n" + " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" @@ -431,6 +440,18 @@ Client::signal_config_created(DBus::Connection& conn, const string& config_name) } +void +Client::signal_config_modified(DBus::Connection& conn, const string& config_name) +{ + DBus::MessageSignal msg(PATH, INTERFACE, "ConfigModified"); + + DBus::Hoho hoho(msg); + hoho << config_name; + + conn.send(msg); +} + + void Client::signal_config_deleted(DBus::Connection& conn, const string& config_name) { @@ -501,7 +522,7 @@ Client::list_configs(DBus::Connection& conn, DBus::Message& msg) DBus::Hoho hoho(reply); hoho.open_array(DBus::TypeInfo::signature); for (MetaSnappers::const_iterator it = meta_snappers.begin(); it != meta_snappers.end(); ++it) - hoho << it->config_info; + hoho << it->getConfigInfo(); hoho.close_array(); conn.send(reply); @@ -527,9 +548,36 @@ Client::get_config(DBus::Connection& conn, DBus::Message& msg) DBus::MessageMethodReturn reply(msg); DBus::Hoho hoho(reply); - hoho << it->config_info; + hoho << it->getConfigInfo(); + + conn.send(reply); +} + + +void +Client::set_config(DBus::Connection& conn, DBus::Message& msg) +{ + string config_name; + + DBus::Hihi hihi(msg); + map raw; + hihi >> config_name >> raw; + + y2deb("SetConfig config_name:" << config_name << " raw:" << raw); + + boost::shared_lock lock(big_mutex); + + MetaSnappers::iterator it = meta_snappers.find(config_name); + + check_permission(conn, msg); + + it->setConfigInfo(raw); + + DBus::MessageMethodReturn reply(msg); conn.send(reply); + + signal_config_modified(conn, config_name); } @@ -886,7 +934,7 @@ Client::create_post_snapshot(DBus::Connection& conn, DBus::Message& msg) snap2->flushInfo(); bool tmp; - if (it->config_info.getValue("BACKGROUND_COMPARISON", tmp) && tmp) + if (it->getConfigInfo().getValue("BACKGROUND_COMPARISON", tmp) && tmp) backgrounds.add_task(it, snap1, snap2); DBus::MessageMethodReturn reply(msg); @@ -1222,6 +1270,8 @@ Client::dispatch(DBus::Connection& conn, DBus::Message& msg) create_config(conn, msg); else if (msg.is_method_call(INTERFACE, "GetConfig")) get_config(conn, msg); + else if (msg.is_method_call(INTERFACE, "SetConfig")) + set_config(conn, msg); else if (msg.is_method_call(INTERFACE, "DeleteConfig")) delete_config(conn, msg); else if (msg.is_method_call(INTERFACE, "LockConfig")) @@ -1338,6 +1388,11 @@ Client::dispatch(DBus::Connection& conn, DBus::Message& msg) DBus::MessageError reply(msg, "error.unknown_file", DBUS_ERROR_FAILED); conn.send(reply); } + catch (const InvalidConfigdataException& e) + { + DBus::MessageError reply(msg, "error.invalid_configdata", DBUS_ERROR_FAILED); + conn.send(reply); + } catch (const InvalidUserdataException& e) { DBus::MessageError reply(msg, "error.invalid_userdata", DBUS_ERROR_FAILED); diff --git a/server/Client.h b/server/Client.h index 6182cdf6..e7932b39 100644 --- a/server/Client.h +++ b/server/Client.h @@ -72,6 +72,7 @@ public: void check_snapshot_in_use(const MetaSnapper& meta_snapper, unsigned int number) const; void signal_config_created(DBus::Connection& conn, const string& config_name); + void signal_config_modified(DBus::Connection& conn, const string& config_name); void signal_config_deleted(DBus::Connection& conn, const string& config_name); void signal_snapshot_created(DBus::Connection& conn, const string& config_name, unsigned int num); @@ -82,6 +83,7 @@ public: void list_configs(DBus::Connection& conn, DBus::Message& msg); void get_config(DBus::Connection& conn, DBus::Message& msg); + void set_config(DBus::Connection& conn, DBus::Message& msg); void create_config(DBus::Connection& conn, DBus::Message& msg); void delete_config(DBus::Connection& conn, DBus::Message& msg); void lock_config(DBus::Connection& conn, DBus::Message& msg); diff --git a/server/MetaSnapper.cc b/server/MetaSnapper.cc index f90a06fa..478feedd 100644 --- a/server/MetaSnapper.cc +++ b/server/MetaSnapper.cc @@ -158,9 +158,40 @@ get_group_uids(const char* groupname, vector& uids) } -MetaSnapper::MetaSnapper(const ConfigInfo& config_info) +MetaSnapper::MetaSnapper(ConfigInfo& config_info) : config_info(config_info), snapper(NULL) { + set_permissions(); +} + + +MetaSnapper::~MetaSnapper() +{ + delete snapper; +} + + +void +MetaSnapper::setConfigInfo(const map& raw) +{ + if (raw.find("SUBVOLUME") != raw.end() || raw.find("FSTYPE") != raw.end()) + throw InvalidConfigdataException(); + + for (map::const_iterator it = raw.begin(); it != raw.end(); ++it) + config_info.setValue(it->first, it->second); + + config_info.save(); + + if (raw.find("ALLOW_USERS") != raw.end() || raw.find("ALLOW_GROUPS") != raw.end()) + set_permissions(); +} + + +void +MetaSnapper::set_permissions() +{ + uids.clear(); + vector users; if (config_info.getValue("ALLOW_USERS", users)) { @@ -188,12 +219,6 @@ MetaSnapper::MetaSnapper(const ConfigInfo& config_info) } -MetaSnapper::~MetaSnapper() -{ - delete snapper; -} - - Snapper* MetaSnapper::getSnapper() { @@ -229,7 +254,7 @@ MetaSnappers::init() { list config_infos = Snapper::getConfigs(); - for (list::const_iterator it = config_infos.begin(); it != config_infos.end(); ++it) + for (list::iterator it = config_infos.begin(); it != config_infos.end(); ++it) { #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) entries.emplace_back(*it); diff --git a/server/MetaSnapper.h b/server/MetaSnapper.h index 955fe6f0..4afda9e7 100644 --- a/server/MetaSnapper.h +++ b/server/MetaSnapper.h @@ -86,12 +86,13 @@ class MetaSnapper : public RefCounter { public: - MetaSnapper(const ConfigInfo& config_info); + MetaSnapper(ConfigInfo& config_info); ~MetaSnapper(); const string& configName() const { return config_info.getConfigName(); } - const ConfigInfo config_info; + const ConfigInfo& getConfigInfo() const { return config_info; } + void setConfigInfo(const map& raw); vector uids; @@ -103,6 +104,10 @@ public: private: + void set_permissions(); + + ConfigInfo config_info; + Snapper* snapper; }; diff --git a/snapper/AsciiFile.cc b/snapper/AsciiFile.cc index 9f8b9fff..e3e55eef 100644 --- a/snapper/AsciiFile.cc +++ b/snapper/AsciiFile.cc @@ -160,6 +160,14 @@ AsciiFile::save() } + void + SysconfigFile::save() + { + if (modified && AsciiFile::save()) + modified = false; + } + + void SysconfigFile::setValue(const string& key, bool value) { diff --git a/snapper/AsciiFile.h b/snapper/AsciiFile.h index 17fec737..9286dd04 100644 --- a/snapper/AsciiFile.h +++ b/snapper/AsciiFile.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2004-2012] Novell, Inc. + * Copyright (c) [2004-2013] Novell, Inc. * * All Rights Reserved. * @@ -94,6 +94,8 @@ namespace snapper SysconfigFile(const string& name) : AsciiFile(name), modified(false) {} ~SysconfigFile() { if (modified) save(); } + void save(); + void setValue(const string& key, bool value); bool getValue(const string& key, bool& value) const; diff --git a/snapper/Snapper.h b/snapper/Snapper.h index 480881aa..6b1672e6 100644 --- a/snapper/Snapper.h +++ b/snapper/Snapper.h @@ -68,6 +68,12 @@ namespace snapper virtual const char* what() const throw() { return "invalid config"; } }; + struct InvalidConfigdataException : public SnapperException + { + explicit InvalidConfigdataException() throw() {} + virtual const char* what() const throw() { return "invalid configdata"; } + }; + struct InvalidUserdataException : public SnapperException { explicit InvalidUserdataException() throw() {}