From: Arvin Schnell Date: Mon, 16 Jul 2012 12:17:47 +0000 (+0200) Subject: - work on dbus interface X-Git-Tag: v0.1.3~209 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6fb8dfeb6e34ca195cc479861e0c32e9f4728c4b;p=thirdparty%2Fsnapper.git - work on dbus interface --- diff --git a/client/commands.cc b/client/commands.cc index ce82ed90..99df195e 100644 --- a/client/commands.cc +++ b/client/commands.cc @@ -72,7 +72,7 @@ command_create_xconfig(DBus::Connection& conn, const string& config_name, const DBus::Hoho hoho(call); hoho << config_name << subvolume << fstype << template_name; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -84,7 +84,7 @@ command_delete_xconfig(DBus::Connection& conn, const string& config_name) DBus::Hoho hoho(call); hoho << config_name; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -135,7 +135,7 @@ command_set_xsnapshot(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << num << data.description << data.cleanup << data.userdata; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -211,7 +211,7 @@ command_delete_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << nums; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -224,7 +224,7 @@ command_mount_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << num; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -237,7 +237,7 @@ command_umount_xsnapshots(DBus::Connection& conn, const string& config_name, DBus::Hoho hoho(call); hoho << config_name << num; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -250,7 +250,7 @@ command_create_xcomparison(DBus::Connection& conn, const string& config_name, un DBus::Hoho hoho(call); hoho << config_name << number1 << number2; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -303,7 +303,7 @@ command_set_xundo(DBus::Connection& conn, const string& config_name, unsigned in DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << undos; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } @@ -316,38 +316,45 @@ command_set_xundo_all(DBus::Connection& conn, const string& config_name, unsigne DBus::Hoho hoho(call); hoho << config_name << number1 << number2 << undo; - DBus::Message reply = conn.send_and_reply_and_block(call); + conn.send_and_reply_and_block(call); } -XUndoStatistic -command_get_xundostatistic(DBus::Connection& conn, const string& config_name, unsigned int number1, - unsigned int number2) +vector +command_get_xundo_steps(DBus::Connection& conn, const string& config_name, unsigned int number1, + unsigned int number2) { - DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "GetUndoStatistic"); + DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "GetUndoSteps"); DBus::Hoho hoho(call); hoho << config_name << number1 << number2; DBus::Message reply = conn.send_and_reply_and_block(call); - XUndoStatistic ret; + vector undo_steps; DBus::Hihi hihi(reply); - hihi >> ret.numCreate >> ret.numModify >> ret.numDelete; + hihi >> undo_steps; - return ret; + return undo_steps; } -void -command_xundo_changes(DBus::Connection& conn, const string& config_name, unsigned int number1, - unsigned int number2) +bool +command_do_xundo_step(DBus::Connection& conn, const string& config_name, unsigned int number1, + unsigned int number2, const XUndoStep& undo_step) { - DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "UndoChanges"); + DBus::MessageMethodCall call(SERVICE, OBJECT, INTERFACE, "DoUndoStep"); DBus::Hoho hoho(call); - hoho << config_name << number1 << number2; + hoho << config_name << number1 << number2 << undo_step; DBus::Message reply = conn.send_and_reply_and_block(call); + + bool ret; + + DBus::Hihi hihi(reply); + hihi >> ret; + + return ret; } diff --git a/client/commands.h b/client/commands.h index 5e8e5147..0a0b75b5 100644 --- a/client/commands.h +++ b/client/commands.h @@ -103,10 +103,10 @@ void command_set_xundo_all(DBus::Connection& conn, const string& config_name, unsigned int number1, unsigned int number2, bool undo); -XUndoStatistic -command_get_xundostatistic(DBus::Connection& conn, const string& config_name, unsigned int number1, - unsigned int number2); +vector +command_get_xundo_steps(DBus::Connection& conn, const string& config_name, unsigned int number1, + unsigned int number2); -void -command_xundo_changes(DBus::Connection& conn, const string& config_name, unsigned int number1, - unsigned int number2); +bool +command_do_xundo_step(DBus::Connection& conn, const string& config_name, unsigned int number1, + unsigned int number2, const XUndoStep& undo_step); diff --git a/client/snapper.cc b/client/snapper.cc index 2163cf92..6b94633a 100644 --- a/client/snapper.cc +++ b/client/snapper.cc @@ -923,17 +923,66 @@ command_undo(DBus::Connection& conn) } } - XUndoStatistic s = command_get_xundostatistic(conn, config_name, nums.first, nums.second); + vector undo_steps = command_get_xundo_steps(conn, config_name, nums.first, nums.second); - if (s.empty()) + int numCreate = 0; + int numModify = 0; + int numDelete = 0; + + for (vector::const_iterator it = undo_steps.begin(); it != undo_steps.end(); ++it) + { + switch (it->action) + { + case CREATE: numCreate++; break; + case MODIFY: numModify++; break; + case DELETE: numDelete++; break; + } + } + + if (numCreate == 0 && numModify == 0 && numDelete == 0) { cout << _("nothing to do") << endl; return; } - cout << sformat(_("create:%d modify:%d delete:%d"), s.numCreate, s.numModify, s.numDelete) << endl; + cout << sformat(_("create:%d modify:%d delete:%d"), numCreate, numModify, numDelete) << endl; + + XConfigInfo ci = command_get_xconfig(conn, config_name); - command_xundo_changes(conn, config_name, nums.first, nums.second); + for (vector::const_iterator it = undo_steps.begin(); it != undo_steps.end(); ++it) + { + if (verbose) + { + switch (it->action) + { + case CREATE: + cout << sformat(_("creating %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + case MODIFY: + cout << sformat(_("modifying %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + case DELETE: + cout << sformat(_("deleting %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + } + } + + if (!command_do_xundo_step(conn, config_name, nums.first, nums.second, *it)) + { + switch (it->action) + { + case CREATE: + cout << sformat(_("failed to create %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + case MODIFY: + cout << sformat(_("failed to modify %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + case DELETE: + cout << sformat(_("failed to delete %s"), add_subvolume(ci.subvolume, it->name).c_str()) << endl; + break; + } + } + } } diff --git a/client/types.cc b/client/types.cc index d3e544c4..465421b1 100644 --- a/client/types.cc +++ b/client/types.cc @@ -57,19 +57,13 @@ XSnapshots::findPost(const_iterator pre) const } -bool -XUndoStatistic::empty() const -{ - return numCreate == 0 && numModify == 0 && numDelete == 0; -} - - namespace DBus { const char* TypeInfo::signature = "(ssa{ss})"; const char* TypeInfo::signature = "(uquussa{ss})"; const char* TypeInfo::signature = "(ssb)"; const char* TypeInfo::signature = "(sb)"; + const char* TypeInfo::signature = "(sq)"; Hihi& @@ -130,4 +124,25 @@ namespace DBus return hoho; } + + Hihi& + operator>>(Hihi& hihi, XUndoStep& data) + { + hihi.open_recurse(); + dbus_uint16_t tmp; + hihi >> data.name >> tmp; + data.action = static_cast(tmp); + hihi.close_recurse(); + return hihi; + } + + + Hoho& + operator<<(Hoho& hoho, const XUndoStep& data) + { + hoho.open_struct(); + hoho << data.name << static_cast(data.action); + hoho.close_struct(); + return hoho; + } } diff --git a/client/types.h b/client/types.h index 7975b458..033626f7 100644 --- a/client/types.h +++ b/client/types.h @@ -31,6 +31,7 @@ using std::map; #include "dbus/DBusConnection.h" #include "dbus/DBusMessage.h" #include "snapper/Snapshot.h" +#include "snapper/File.h" using namespace snapper; @@ -101,13 +102,10 @@ struct XUndo }; -struct XUndoStatistic +struct XUndoStep { - bool empty() const; - - unsigned int numCreate; - unsigned int numModify; - unsigned int numDelete; + string name; + Action action; }; @@ -118,6 +116,7 @@ namespace DBus template <> struct TypeInfo { static const char* signature; }; template <> struct TypeInfo { static const char* signature; }; template <> struct TypeInfo { static const char* signature; }; + template <> struct TypeInfo { static const char* signature; }; Hihi& operator>>(Hihi& hihi, XConfigInfo& data); @@ -130,5 +129,8 @@ namespace DBus Hoho& operator<<(Hoho& hoho, const XUndo& data); + Hihi& operator>>(Hihi& hihi, XUndoStep& data); + Hoho& operator<<(Hoho& hoho, const XUndoStep& data); + }; diff --git a/server/Types.cc b/server/Types.cc index 9bb62d82..3611f347 100644 --- a/server/Types.cc +++ b/server/Types.cc @@ -29,6 +29,7 @@ namespace DBus const char* TypeInfo::signature = "(uquussa{ss})"; const char* TypeInfo::signature = "(ssb)"; const char* TypeInfo::signature = "(sb)"; + const char* TypeInfo::signature = "(sq)"; Hoho& @@ -121,5 +122,27 @@ namespace DBus return hoho; } + + Hihi& + operator>>(Hihi& hihi, UndoStep& data) + { + hihi.open_recurse(); + dbus_uint16_t tmp; + hihi >> data.name >> tmp; + data.action = static_cast(tmp); + hihi.close_recurse(); + return hihi; + } + + + Hoho& + operator<<(Hoho& hoho, const UndoStep& data) + { + hoho.open_struct(); + hoho << data.name << static_cast(data.action); + hoho.close_struct(); + return hoho; + } + } diff --git a/server/Types.h b/server/Types.h index 735716d1..3677c6a9 100644 --- a/server/Types.h +++ b/server/Types.h @@ -57,6 +57,7 @@ namespace DBus template <> struct TypeInfo { static const char* signature; }; template <> struct TypeInfo { static const char* signature; }; template <> struct TypeInfo { static const char* signature; }; + template <> struct TypeInfo { static const char* signature; }; Hoho& operator<<(Hoho& hoho, const ConfigInfo& data); @@ -74,4 +75,7 @@ namespace DBus Hihi& operator>>(Hihi& hihi, Undo& data); Hoho& operator<<(Hoho& hoho, const Undo& data); + Hihi& operator>>(Hihi& hihi, UndoStep& data); + Hoho& operator<<(Hoho& hoho, const UndoStep& data); + }; diff --git a/server/snapperd.cc b/server/snapperd.cc index 01c02f4a..b8f4346c 100644 --- a/server/snapperd.cc +++ b/server/snapperd.cc @@ -202,19 +202,18 @@ reply_to_introspect(DBus::Connection& conn, DBus::Message& msg) " \n" " \n" - " \n" + " \n" " \n" " \n" " \n" - " \n" - " \n" - " \n" + " \n" " \n" - " \n" + " \n" " \n" " \n" " \n" + " \n" " \n" " \n" @@ -780,6 +779,9 @@ reply_to_command_umount_snapshot(DBus::Connection& conn, DBus::Message& msg) struct Comparing : public Job { + Comparing(Comparison* comparison) + : comparison(comparison) {} + DBus::Connection* conn; DBus::MessageMethodReturn* reply; @@ -819,10 +821,9 @@ reply_to_command_create_comparison(DBus::Connection& conn, DBus::Message& msg) assert(it != clients.end()); it->comparisons.push_back(comparison); - Comparing* job = new Comparing; + Comparing* job = new Comparing(comparison); job->conn = &conn; job->reply = new DBus::MessageMethodReturn(msg); - job->comparison = comparison; jobs.add(job); } @@ -842,7 +843,6 @@ Comparing::operator()() void Comparing::done() { - DBus::Hoho hoho(*reply); conn->send(*reply); delete reply; @@ -995,7 +995,7 @@ reply_to_command_set_undo_all(DBus::Connection& conn, DBus::Message& msg) void -reply_to_command_get_undo_statistics(DBus::Connection& conn, DBus::Message& msg) +reply_to_command_get_undo_steps(DBus::Connection& conn, DBus::Message& msg) { string config_name; dbus_uint32_t num1, num2; @@ -1003,7 +1003,7 @@ reply_to_command_get_undo_statistics(DBus::Connection& conn, DBus::Message& msg) DBus::Hihi hihi(msg); hihi >> config_name >> num1 >> num2; - y2mil("GetUndoStatistic config_name:" << config_name << " num1:" << num1 << " num2:" << + y2mil("GetUndoSteps config_name:" << config_name << " num1:" << num1 << " num2:" << num2); check_permission(conn, msg, config_name); @@ -1015,12 +1015,12 @@ reply_to_command_get_undo_statistics(DBus::Connection& conn, DBus::Message& msg) Comparison* comparison = it->find_comparison(config_name, num1, num2); - UndoStatistic statistic = comparison->getUndoStatistic(); + vector undo_steps = comparison->getUndoSteps(); DBus::MessageMethodReturn reply(msg); DBus::Hoho hoho(reply); - hoho << statistic.numCreate << statistic.numModify << statistic.numDelete; + hoho << undo_steps; conn.send(reply); } @@ -1028,10 +1028,14 @@ reply_to_command_get_undo_statistics(DBus::Connection& conn, DBus::Message& msg) struct Undoing : public Job { + Undoing(Comparison* comparison, const UndoStep& undo_step) + : comparison(comparison), undo_step(undo_step) {} + DBus::Connection* conn; DBus::MessageMethodReturn* reply; Comparison* comparison; + UndoStep undo_step; void done(); @@ -1043,15 +1047,16 @@ protected: void -reply_to_command_undo_changes(DBus::Connection& conn, DBus::Message& msg) +reply_to_command_do_undo_step(DBus::Connection& conn, DBus::Message& msg) { string config_name; dbus_uint32_t num1, num2; + UndoStep undo_step("", MODIFY); DBus::Hihi hihi(msg); - hihi >> config_name >> num1 >> num2; + hihi >> config_name >> num1 >> num2 >> undo_step; - y2mil("UndoChanges config_name:" << config_name << " num1:" << num1 << " num2:" << + y2mil("GetUndoSteps config_name:" << config_name << " num1:" << num1 << " num2:" << num2); check_permission(conn, msg, config_name); @@ -1063,10 +1068,9 @@ reply_to_command_undo_changes(DBus::Connection& conn, DBus::Message& msg) Comparison* comparison = it->find_comparison(config_name, num1, num2); - Undoing* job = new Undoing; + Undoing* job = new Undoing(comparison, undo_step); job->conn = &conn; job->reply = new DBus::MessageMethodReturn(msg); - job->comparison = comparison; jobs.add(job); } @@ -1077,7 +1081,10 @@ Undoing::operator()() { boost::this_thread::sleep(seconds(2)); - comparison->doUndo(); + bool ret = comparison->doUndoStep(undo_step); + + DBus::Hoho hoho(*reply); + hoho << ret; boost::this_thread::sleep(seconds(2)); } @@ -1086,7 +1093,6 @@ Undoing::operator()() void Undoing::done() { - DBus::Hoho hoho(*reply); conn->send(*reply); delete reply; @@ -1199,10 +1205,10 @@ dispatch(DBus::Connection& conn, DBus::Message& msg) reply_to_command_set_undo(conn, msg); else if (msg.is_method_call(INTERFACE, "SetUndoAll")) reply_to_command_set_undo_all(conn, msg); - else if (msg.is_method_call(INTERFACE, "GetUndoStatistic")) - reply_to_command_get_undo_statistics(conn, msg); - else if (msg.is_method_call(INTERFACE, "UndoChanges")) - reply_to_command_undo_changes(conn, msg); + else if (msg.is_method_call(INTERFACE, "GetUndoSteps")) + reply_to_command_get_undo_steps(conn, msg); + else if (msg.is_method_call(INTERFACE, "DoUndoStep")) + reply_to_command_do_undo_step(conn, msg); else if (msg.is_method_call(INTERFACE, "Debug")) reply_to_command_debug(conn, msg); else diff --git a/snapper/Comparison.cc b/snapper/Comparison.cc index f847beb8..81cc34ad 100644 --- a/snapper/Comparison.cc +++ b/snapper/Comparison.cc @@ -92,6 +92,26 @@ namespace snapper } + vector + Comparison::getUndoSteps() const + { + if (!initialized) + throw; + + return files.getUndoSteps(); + } + + + bool + Comparison::doUndoStep(const UndoStep& undo_step) + { + if (!initialized) + throw; + + return files.doUndoStep(undo_step); + } + + bool Comparison::doUndo() { diff --git a/snapper/Comparison.h b/snapper/Comparison.h index 474be89d..0250c25a 100644 --- a/snapper/Comparison.h +++ b/snapper/Comparison.h @@ -52,6 +52,10 @@ namespace snapper UndoStatistic getUndoStatistic() const; + vector getUndoSteps() const; + + bool doUndoStep(const UndoStep& undo_step); + bool doUndo(); private: diff --git a/snapper/File.cc b/snapper/File.cc index 1d69c5c6..ca0aba24 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -726,7 +726,7 @@ namespace snapper } - File::Action + Action File::getAction() const { if (getPreToPostStatus() == CREATED) @@ -751,9 +751,9 @@ namespace snapper { switch (it->getAction()) { - case File::CREATE: rs.numCreate++; break; - case File::MODIFY: rs.numModify++; break; - case File::DELETE: rs.numDelete++; break; + case CREATE: rs.numCreate++; break; + case MODIFY: rs.numModify++; break; + case DELETE: rs.numDelete++; break; } } } @@ -762,6 +762,50 @@ namespace snapper } + vector + Files::getUndoSteps() const + { + if (comparison->getSnapshot1()->isCurrent()) + throw IllegalSnapshotException(); + + vector undo_steps; + + for (vector::const_reverse_iterator it = entries.rbegin(); it != entries.rend(); ++it) + { + if (it->getUndo()) + { + if (it->getPreToPostStatus() == CREATED) + undo_steps.push_back(UndoStep(it->getName(), it->getAction())); + } + } + + for (vector::const_iterator it = entries.begin(); it != entries.end(); ++it) + { + if (it->getUndo()) + { + if (it->getPreToPostStatus() != CREATED) + undo_steps.push_back(UndoStep(it->getName(), it->getAction())); + } + } + + return undo_steps; + } + + + bool + Files::doUndoStep(const UndoStep& undo_step) + { + if (comparison->getSnapshot1()->isCurrent()) + throw IllegalSnapshotException(); + + vector::iterator it = find(undo_step.name); + if (it == end()) + return false; + + return it->doUndo(); + } + + bool Files::doUndo() { @@ -778,9 +822,7 @@ namespace snapper if (it->getUndo()) { if (it->getPreToPostStatus() == CREATED) - { it->doUndo(); - } } } @@ -789,9 +831,7 @@ namespace snapper if (it->getUndo()) { if (it->getPreToPostStatus() != CREATED) - { it->doUndo(); - } } } diff --git a/snapper/File.h b/snapper/File.h index caa2a79f..9482990b 100644 --- a/snapper/File.h +++ b/snapper/File.h @@ -56,6 +56,11 @@ namespace snapper LOC_PRE, LOC_POST, LOC_SYSTEM }; + enum Action + { + CREATE, MODIFY, DELETE + }; + struct UndoStatistic { @@ -71,6 +76,16 @@ namespace snapper }; + struct UndoStep + { + UndoStep(const string& name, Action action) + : name(name), action(action) {} + + string name; + Action action; + }; + + class File { public: @@ -97,8 +112,6 @@ namespace snapper void setUndo(bool value) { undo = value; } bool doUndo(); - enum Action { CREATE, MODIFY, DELETE }; - Action getAction() const; friend std::ostream& operator<<(std::ostream& s, const File& file); @@ -178,6 +191,10 @@ namespace snapper UndoStatistic getUndoStatistic() const; + vector getUndoSteps() const; + + bool doUndoStep(const UndoStep& undo_step); + bool doUndo(); const Comparison* comparison;