[PATCH] History rebased against master 1.9.0 on 3Nov2007 [1 of 3]

Matt Davis mattdavis9 at gmail.com
Sun Nov 4 00:41:56 UTC 2007


The following patch regards the history modification to parted, and not
libparted.  The changes exposed here concern the adding of the commands:
History, Undo, Redo, and Save.

These commands respectively display the "History" of disk changes to
be "Saved."  Before disk modification are "Saved" to disk, they can be
"Undone" or "Redone" if previously "Undone."

-Matt
---
 include/parted/Makefile.am |    1 +
 include/parted/disk.h      |    1 +
 include/parted/history.h   |  111 ++++++++++++++++++++++
 include/parted/parted.h    |    1 +
 parted/command.c           |    2 +
 parted/parted.c            |  221 ++++++++++++++++++++++++++++++++++----------
 6 files changed, 286 insertions(+), 51 deletions(-)
 create mode 100644 include/parted/history.h

diff --git a/include/parted/Makefile.am b/include/parted/Makefile.am
index dd0e1d4..88a426b 100644
--- a/include/parted/Makefile.am
+++ b/include/parted/Makefile.am
@@ -18,6 +18,7 @@ partedinclude_HEADERS = gnu.h		\
 			natmath.h	\
 			timer.h		\
 			unit.h		\
+			history.h	\
 			parted.h        \
 			$(S390_HDRS)
 
diff --git a/include/parted/disk.h b/include/parted/disk.h
index b82ea0f..69542c1 100644
--- a/include/parted/disk.h
+++ b/include/parted/disk.h
@@ -260,6 +260,7 @@ extern PedDisk* ped_disk_new_fresh (PedDevice* dev,
 extern PedDisk* ped_disk_duplicate (const PedDisk* old_disk);
 extern void ped_disk_destroy (PedDisk* disk);
 extern int ped_disk_commit (PedDisk* disk);
+extern void ped_disk_commit_to_history (const PedDisk *disk);
 extern int ped_disk_commit_to_dev (PedDisk* disk);
 extern int ped_disk_commit_to_os (PedDisk* disk);
 extern int ped_disk_check (const PedDisk* disk);
diff --git a/include/parted/history.h b/include/parted/history.h
new file mode 100644
index 0000000..b2b2cf5
--- /dev/null
+++ b/include/parted/history.h
@@ -0,0 +1,111 @@
+/*
+    libparted - a library for manipulating disk partitions
+    Copyright (C) 1999, 2000, 2001, 2007 Free Software Foundation, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef PED_HISTORY_H_INCLUDED
+#define PED_HISTORY_H_INCLUDED
+
+
+/*
+ * Macros
+ */
+
+#define PED_HISTORY_TAG      "[History] "
+#define PED_HISTORY_MAX_NAME 64
+#define PED_HISTORY_IS_UNDONE(_o) (_o->ignore && _o->disk)
+        
+
+typedef struct _PedHistObj PedHistObj;
+typedef struct _PedHistManager PedHistManager;
+typedef enum _PedHistRet PedHistRet;
+
+#include <parted/disk.h>
+
+/*
+ * Enums
+ */
+
+enum _PedHistRet {
+    PED_HISTORY_RET_SUCCESS,
+    PED_HISTORY_RET_ERROR,
+    PED_HISTORY_RET_NO_UNDO,
+    PED_HISTORY_RET_NO_REDO,
+    PED_HISTORY_RET_NO_SAVE,
+};
+
+
+/*
+ * Structs
+ */
+
+struct _PedHistObj {
+    int         id;
+    int         ignore; /* Undo/Redo functionality */
+    char       *name;   /* Command name */
+    PedDisk    *disk;
+    PedHistObj *prev;
+    PedHistObj *next;
+};
+
+
+struct _PedHistManager {
+    PedHistObj  *begin;
+    PedHistObj  *end;
+    int          n_objs;
+    int          id;
+};
+
+
+/*
+ * Funcs
+ */
+
+/* Add/Clear history */
+extern void ped_history_add (const char *cmd);
+extern void ped_history_clear (void);
+
+/* Iterating */
+extern const PedHistObj *ped_history_begin (void);
+
+/* Duplicate of the most recent disk mod, this can safely be destroyed */
+extern PedDisk *ped_history_disk (void);
+
+/* Before changes are committed */
+extern PedHistRet ped_history_undo (void);
+extern PedHistRet ped_history_redo (void);
+
+/* Write changes to disk
+ * Each change's success/failure value is passed 
+ * to the optional callback so that the end application
+ * can display such values appropriately.
+ */
+typedef void (*PedHistPrintCB) (PedHistRet val, PedHistObj *obj);
+extern PedHistRet ped_history_commit_to_disk (PedHistPrintCB cb);
+
+/* Copy the most recent disk change */
+extern void ped_history_add_disk (const PedDisk *disk);
+
+/* Print */
+extern const char *ped_history_print_ret (PedHistRet val);
+extern void ped_history_print_debug (void);
+
+/* Alloc/dealloc */
+extern PedHistObj *ped_history_alloc_obj (void);
+extern void ped_history_dealloc_obj (PedHistObj *obj);
+
+
+#endif /* PED_HISTORY_H_INCLUDED */
diff --git a/include/parted/parted.h b/include/parted/parted.h
index 1302d8f..f82d02f 100644
--- a/include/parted/parted.h
+++ b/include/parted/parted.h
@@ -30,6 +30,7 @@ typedef struct _PedArchitecture PedArchitecture;
 #include <parted/disk.h>
 #include <parted/exception.h>
 #include <parted/filesys.h>
+#include <parted/history.h>
 #include <parted/natmath.h>
 #include <parted/unit.h>
 
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 6a606ae..c5f21de 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -43,6 +43,7 @@
 
 #include <parted/parted.h>
 #include <parted/debug.h>
+#include <parted/history.h>
 
 #include <ctype.h>
 #include <stdarg.h>
@@ -493,9 +494,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 ()) {
@@ -534,8 +536,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);
@@ -599,8 +600,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) {
@@ -619,8 +624,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)
@@ -642,9 +646,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;
@@ -665,8 +670,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)
@@ -696,10 +700,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;
@@ -797,8 +802,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);
@@ -870,9 +874,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;
@@ -972,8 +977,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);
@@ -1040,9 +1044,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;
@@ -1090,8 +1095,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);
@@ -1124,9 +1128,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;
@@ -1139,7 +1144,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;
@@ -1275,9 +1280,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) {
@@ -1640,7 +1646,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;
 }
 
@@ -1768,9 +1774,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;
@@ -1816,7 +1823,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)
@@ -1849,9 +1856,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;
@@ -1859,7 +1867,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)
@@ -1897,9 +1905,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;
@@ -1914,8 +1923,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)
@@ -1960,6 +1968,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 ()
 {
@@ -2278,6 +2356,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


--rwEMma7ioTxnRzrJ--



More information about the parted-devel mailing list