[PATCH] History patch 1 of 3

Matt Davis mattdavis9 at gmail.com
Thu Feb 7 05:16:58 UTC 2008


The following changes modify to parted's core to enable history.

    parted/command.c:
    Captures user entered commands and stores them as nodes in a link-list
    These commands, when the "save" command is issued, are applied to disk.
    This allows the user to undo/redo their choices before the disk actually
    becomes modified.

    parted/parted.c:
    The commands that would normally make disk changes immediately, now are
    stored as nodes in a link list.  This allows disk modifications to be
    undone before they actually take affect.

    The changes here also implement the following commands:
    * undo: Undoes the most recent disk modification per session
    * redo: Redoes the most recent disk modification per session
    * save: Actually applies the disk modification list (history) to disk
    * history: Shows the list of commands executed
---
 parted/command.c |    2 +
 parted/parted.c  |  221 +++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 172 insertions(+), 51 deletions(-)

diff --git a/parted/command.c b/parted/command.c
index a3ae7c0..3d34d06 100644
--- a/parted/command.c
+++ b/parted/command.c
@@ -21,6 +21,7 @@
 #include "ui.h"
 
 #include <parted/debug.h>
+#include <parted/history.h>
 
 #include <stdlib.h>
 #include <string.h>
@@ -136,6 +137,7 @@ command_print_help (Command* cmd)
 int
 command_run (Command* cmd, PedDevice** dev)
 {
+	ped_history_add(str_list_convert(cmd->names));
 	return cmd->method (dev);
 }
 
diff --git a/parted/parted.c b/parted/parted.c
index 46999f5..1b97b56 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -42,6 +42,7 @@
 
 #include <parted/parted.h>
 #include <parted/debug.h>
+#include <parted/history.h>
 
 #include <ctype.h>
 #include <stdarg.h>
@@ -492,9 +493,10 @@ do_cp (PedDevice** dev)
         PedFileSystem*          dst_fs;
         PedFileSystemType*      dst_fs_type;
 
-        dst_disk = ped_disk_new (*dev);
-        if (!dst_disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(dst_disk = ped_history_disk ()))
+                if (!(dst_disk = ped_disk_new (*dev)))
+                        goto error;
 
         src_disk = dst_disk;
         if (!command_line_is_integer ()) {
@@ -533,8 +535,7 @@ do_cp (PedDevice** dev)
 /* update the partition table, close disks */
         if (!ped_partition_set_system (dst, dst_fs_type))
                 goto error_destroy_disk;
-        if (!ped_disk_commit (dst_disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (dst_disk);
         if (src_disk != dst_disk)
                 ped_disk_destroy (src_disk);
         ped_disk_destroy (dst_disk);
@@ -598,8 +599,12 @@ do_mklabel (PedDevice** dev)
         const PedDiskType*      type = ped_disk_probe (*dev);
 
         ped_exception_fetch_all ();
-        disk = ped_disk_new (*dev);
-        if (!disk) ped_exception_catch ();
+
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        ped_exception_catch ();
+
         ped_exception_leave_all ();
 
         if (disk) {
@@ -618,8 +623,7 @@ do_mklabel (PedDevice** dev)
         if (!disk)
                 goto error;
 
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (disk);
         ped_disk_destroy (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
@@ -641,9 +645,10 @@ do_mkfs (PedDevice** dev)
         const PedFileSystemType* type = ped_file_system_type_get ("ext2");
         PedFileSystem*          fs;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if  (!opt_script_mode && !_partition_warn_loss())
                 goto error_destroy_disk;
@@ -664,8 +669,7 @@ do_mkfs (PedDevice** dev)
                 goto error_destroy_disk;
         if (ped_partition_is_flag_available (part, PED_PARTITION_LBA))
                 ped_partition_set_flag (part, PED_PARTITION_LBA, 1);
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (disk);
         ped_disk_destroy (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
@@ -695,10 +699,11 @@ do_mkpart (PedDevice** dev)
         char*                    part_name = NULL;
         char                     *start_usr = NULL, *end_usr = NULL;
         char                     *start_sol = NULL, *end_sol = NULL;
-        
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+ 
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) {
                 part_type = PED_PARTITION_NORMAL;
@@ -807,8 +812,7 @@ do_mkpart (PedDevice** dev)
         if (ped_partition_is_flag_available (part, PED_PARTITION_LBA))
                 ped_partition_set_flag (part, PED_PARTITION_LBA, 1);
         
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (disk);
         
         /* clean up */
         ped_constraint_destroy (final_constraint);
@@ -880,9 +884,10 @@ do_mkpartfs (PedDevice** dev)
         char                *start_usr = NULL, *end_usr = NULL;
         char                *start_sol = NULL, *end_sol = NULL;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) {
                 part_type = PED_PARTITION_NORMAL;
@@ -982,8 +987,7 @@ do_mkpartfs (PedDevice** dev)
         if (!ped_partition_set_system (part, fs_type))
                 goto error_destroy_disk;
 
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (disk);
 
         /* clean up */
         ped_constraint_destroy (final_constraint);
@@ -1050,9 +1054,10 @@ do_move (PedDevice** dev)
         PedGeometry     *range_start = NULL, *range_end = NULL;
         PedGeometry     old_geom, new_geom;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error_destroy_disk;
@@ -1100,8 +1105,7 @@ do_move (PedDevice** dev)
                 goto error_close_fs;
         ped_file_system_close (fs_copy);
         ped_file_system_close (fs);
-        if (!ped_disk_commit (disk))
-                goto error_destroy_disk;
+        ped_disk_commit_to_history (disk);
         ped_disk_destroy (disk);
         if (range_start != NULL)
                 ped_geometry_destroy (range_start);
@@ -1134,9 +1138,10 @@ do_name (PedDevice** dev)
         PedPartition*   part = NULL;
         char*           name;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error_destroy_disk;
@@ -1149,7 +1154,7 @@ do_name (PedDevice** dev)
                 goto error_free_name;
         free (name);
 
-        if (!ped_disk_commit (disk))
+        ped_disk_commit_to_history (disk);
                 goto error_destroy_disk;
         ped_disk_destroy (disk);
         return 1;
@@ -1286,9 +1291,10 @@ do_print (PedDevice** dev)
         char*           tmp;
         wchar_t*        table_rendered;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         peek_word = command_line_peek_word ();
         if (peek_word) {
@@ -1651,7 +1657,7 @@ _rescue_add_partition (PedPartition* part)
         }
 
         ped_partition_set_system (part, fs_type);
-        ped_disk_commit (part->disk);
+        ped_disk_commit_to_history (part->disk);
         return 1;
 }
 
@@ -1779,9 +1785,10 @@ do_resize (PedDevice** dev)
         PedGeometry             *range_start = NULL, *range_end = NULL;
         PedGeometry             new_geom;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error_destroy_disk;
@@ -1827,7 +1834,7 @@ do_resize (PedDevice** dev)
                 ped_file_system_close (fs);
         }
 
-        ped_disk_commit (disk);
+        ped_disk_commit_to_history (disk);
         ped_constraint_destroy (constraint);
         ped_disk_destroy (disk);
         if (range_start != NULL)
@@ -1860,9 +1867,10 @@ do_rm (PedDevice** dev)
         PedDisk*                disk;
         PedPartition*           part = NULL;
 
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error_destroy_disk;
@@ -1870,7 +1878,7 @@ do_rm (PedDevice** dev)
                 goto error_destroy_disk;
 
         ped_disk_delete_partition (disk, part);
-        ped_disk_commit (disk);
+        ped_disk_commit_to_history (disk);
         ped_disk_destroy (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
@@ -1908,9 +1916,10 @@ do_set (PedDevice** dev)
         PedPartitionFlag        flag;
         int                     state;
         
-        disk = ped_disk_new (*dev);
-        if (!disk)
-                goto error;
+        /* Use the most recent disk from history if available */
+        if (!(disk = ped_history_disk ()))
+                if (!(disk = ped_disk_new (*dev)))
+                        goto error;
         
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error_destroy_disk;
@@ -1925,8 +1934,7 @@ do_set (PedDevice** dev)
     
         if (!ped_partition_set_flag (part, flag, state))
 	        	goto error_destroy_disk;
-    	if (!ped_disk_commit (disk))
-	        	goto error_destroy_disk;
+    	ped_disk_commit_to_history (disk);
     	ped_disk_destroy (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
@@ -1971,6 +1979,76 @@ do_version ()
     return 1;
 }
 
+static int
+do_undo ()
+{
+    PedHistRet err;
+
+    if ((err = ped_history_undo ()))
+            printf(PED_HISTORY_TAG "%s\n", ped_history_print_ret (err));
+
+    return 1;
+}
+    
+static int
+do_redo ()
+{
+    PedHistRet err;
+
+    if ((err = ped_history_redo ()))
+            printf(PED_HISTORY_TAG "%s\n", ped_history_print_ret (err));
+
+    return 1;
+}
+
+/* Called back each time a disk modification is 
+ * made from history.
+ */
+static void
+print_handler (PedHistRet val, PedHistObj *obj)
+{
+    switch (val)
+    {
+        case PED_HISTORY_RET_SUCCESS:
+            printf("Applying change %s (Success)\n", obj->name);
+            break;
+
+        case PED_HISTORY_RET_ERROR:
+            printf(PED_HISTORY_TAG "Applying change %s (Failed)\n", obj->name);
+            break;
+
+        default: break;
+    }
+}
+
+static int
+do_history ()
+{
+    const PedHistObj *ii;
+        
+    /* Only print changes that are undone or modify the disk */
+    for (ii = ped_history_begin (); ii; ii = ii->next)
+           if (PED_HISTORY_IS_UNDONE (ii))
+                   printf(PED_HISTORY_TAG "%s (Undone)\n", ii->name);
+            else if (ii->disk)
+                    printf(PED_HISTORY_TAG "%s\n", ii->name);
+
+    return 1;
+}
+    
+static int
+do_save ()
+{
+    PedHistRet err;
+
+    if ((err = ped_history_commit_to_disk (print_handler)))
+            printf(PED_HISTORY_TAG "%s\n", ped_history_print_ret (err));
+    else
+            printf(PED_HISTORY_TAG "Successfully modified the disk\n");
+
+    return 1;
+}
+ 
 static void
 _init_messages ()
 {
@@ -2289,6 +2367,47 @@ _("'version' displays copyright and version information corresponding to this "
 "copy of GNU Parted\n"),
 NULL), 1));
 
+command_register (commands, command_create (
+        str_list_create_unique ("undo", _("undo"), NULL),
+        do_undo,
+        str_list_create (
+_("undo                                     Undo change to partition table"), 
+  NULL),
+        str_list_create (
+_("'undo' undoes the previous partition table modifcation "), NULL), 1));
+
+command_register (commands, command_create (
+        str_list_create_unique ("redo", _("redo"), NULL),
+        do_redo,
+        str_list_create (
+_("redo                                     Redo change to partition table"), 
+  NULL),
+        str_list_create (
+_("'redo' redoes the previous change to the parititon table"),
+NULL), 1));
+
+
+
+command_register (commands, command_create (
+        str_list_create_unique ("history", _("history"), NULL),
+        do_history,
+        str_list_create (
+_("history                                  Display command history"), NULL),
+        str_list_create (
+_("'history' displays list of commands that have are to be executed\n"),
+NULL), 1));
+
+command_register (commands, command_create (
+        str_list_create_unique ("save", _("save"), NULL),
+        do_save,
+        str_list_create (
+_("save                                     Write changes to partition table"), 
+  NULL),
+        str_list_create (
+_("'save' commits the changes that were created during the current parted "
+   "session.  This writes the changes to disk."),
+NULL), 1));
+
 }
 
 static void
-- 
1.5.1.5


--ibTvN161/egqYuK8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="0002-History-patch-2-of-3.patch"



More information about the parted-devel mailing list