[gnome-boxes/wip/snapshot-config-changes: 4/6] machine-config-editor: Create snapshots for config modifications




commit d75920b740af7e970df693295642613c77092307
Author: Felipe Borges <felipeborges gnome org>
Date:   Fri Oct 9 12:17:29 2020 +0200

    machine-config-editor: Create snapshots for config modifications
    
    The best way to guarantee secure modifications of a virtual machine's
    configuration is to create snapshots before the changes. This allows
    users to revert their machine configurations as well as the state
    of the machine's disk.

 data/ui/properties-toolbar.ui | 23 -----------
 src/config-editor.vala        | 89 +++++++++++++++++--------------------------
 src/properties-toolbar.vala   |  5 ---
 3 files changed, 35 insertions(+), 82 deletions(-)
---
diff --git a/data/ui/properties-toolbar.ui b/data/ui/properties-toolbar.ui
index 97ed7edb..20b3e057 100644
--- a/data/ui/properties-toolbar.ui
+++ b/data/ui/properties-toolbar.ui
@@ -135,29 +135,6 @@
           </packing>
         </child>
 
-        <child>
-          <object class="GtkButton" id="revert_button">
-            <property name="visible">True</property>
-            <property name="valign">center</property>
-            <property name="use-underline">True</property>
-            <property name="tooltip-text" translatable="yes">Revert changes to original 
configuration</property>
-            <signal name="clicked" handler="on_revert_changes_clicked"/>
-            <style>
-              <class name="image-button"/>
-            </style>
-            <child>
-              <object class="GtkImage">
-                <property name="visible">True</property>
-                <property name="icon-name">document-revert-symbolic</property>
-              </object>
-            </child>
-          </object>
-
-          <packing>
-            <property name="pack-type">end</property>
-          </packing>
-        </child>
-
         <child>
           <object class="GtkButton" id="save_button">
             <property name="visible">True</property>
diff --git a/src/config-editor.vala b/src/config-editor.vala
index 8346bbb9..8d1ce3f7 100644
--- a/src/config-editor.vala
+++ b/src/config-editor.vala
@@ -23,18 +23,46 @@ public void setup (LibvirtMachine machine) {
         buffer.set_text (machine.domain_config.to_xml ());
     }
 
-    public async void save () {
-        var saved = yield save_original_config (machine.domain_config);
-        if (!saved) {
-            var failed_to_save_msg = _("Unable to backup original configuration. Aborting.");
-            App.app.main_window.notificationbar.display_error (failed_to_save_msg);
+    private async long create_snapshot () {
+        long snapshot_timestamp = 0;
+        try {
+            var now = new GLib.DateTime.now_local ();
+            var snapshot = yield machine.create_snapshot (_("Configuration modified "));
+            try {
+                var config = snapshot.get_config (0);
+                snapshot_timestamp = config.get_creation_time ();
+            } catch (GLib.Error error) {
+                warning ("Failed to obtain snapshot configuration: %s", error.message);
+            }
+        } catch (GLib.Error error) {
+            warning ("Failed to create snapshot: %s", error.message);
+        }
 
-            return;
+        return snapshot_timestamp;
+    }
+
+    private void add_metadata (GVirConfig.Domain config, long snapshot_timestamp) {
+        string edited_xml = MANUALLY_EDITED_XML.printf (snapshot_timestamp);
+
+        try {
+            config.set_custom_xml (edited_xml, "edited", BOXES_NS_URI);
+        } catch (GLib.Error error) {
+            warning ("Failed to save custom XML: %s", error.message);
         }
+    }
 
+    public async void save () {
         var xml = view.buffer.text;
         if (machine.domain_config.to_xml () == xml) {
             debug ("Nothing changed in the VM configuration");
+
+            return;
+        }
+
+        var snapshot_timestamp = yield create_snapshot ();
+        if (snapshot_timestamp == 0) {
+            warning ("Failed to save changes!");
+
             return;
         }
 
@@ -45,7 +73,7 @@ public async void save () {
             warning ("Failed to save changes!\n");
         }
 
-        add_metadata (custom_config);
+        add_metadata (custom_config, snapshot_timestamp);
 
         try {
             machine.domain.set_config (custom_config);
@@ -65,51 +93,4 @@ public async void save () {
         }
     }
 
-    private void add_metadata (GVirConfig.Domain config) {
-        string edited_xml = MANUALLY_EDITED_XML.printf (1);
-
-        try {
-            config.set_custom_xml (edited_xml, "edited", BOXES_NS_URI); 
-        } catch (GLib.Error error) {
-            warning ("Failed to save custom XML: %s", error.message);
-        }
-    }
-
-    private async bool save_original_config (GVirConfig.Domain config) {
-        var old_config_path = get_user_pkgconfig (config.get_name () + FILE_SUFFIX);
-
-        try {
-            return FileUtils.set_contents (old_config_path, config.to_xml (), -1);
-        } catch (GLib.Error error) {
-            warning ("Failed to save original configuration: %s", error.message);
-
-            return false;
-        }
-    }
-
-    public async void revert_to_original () {
-        var original_config_path = get_user_pkgconfig (machine.domain_config.get_name () + FILE_SUFFIX);
-
-        string? data = null;
-        try {
-            FileUtils.get_contents (original_config_path, out data);
-        } catch (GLib.Error error) {
-            warning ("Failed to load original configuration: %s", error.message);
-            return;
-        }
-
-        if (data == null) {
-            warning ("Failed to load original configuration");
-            return;
-        }
-
-        try {
-            var config = new GVirConfig.Domain.from_xml (data);
-            machine.domain.set_config (config);
-
-            view.buffer.text = data;
-        } catch (GLib.Error error) {
-            warning ("Failed to load old configurations %s", error.message);
-        }
-    }
 }
diff --git a/src/properties-toolbar.vala b/src/properties-toolbar.vala
index 08ccd381..abfc1b4a 100644
--- a/src/properties-toolbar.vala
+++ b/src/properties-toolbar.vala
@@ -60,11 +60,6 @@ private void on_copy_clipboard_clicked () requires (page == PropsWindowPage.TROU
         props_window.copy_troubleshoot_log_to_clipboard ();
     }
 
-    [GtkCallback]
-    private void on_revert_changes_clicked () requires (page == PropsWindowPage.TEXT_EDITOR) {
-        props_window.config_editor.revert_to_original ();
-    }
-
     [GtkCallback]
     private void on_config_editor_save_clicked () {
         props_window.config_editor.save ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]