From cfffbe3e96716d9ed37579eb8b58ff845c9efca4 Mon Sep 17 00:00:00 2001
From: Matt Davis <mattdavis9@gmail.com>
Date: Mon, 11 Feb 2008 19:44:29 -0500
Subject: [PATCH] Adds the history undo/redo functionality to libparted and linux architecture

The following changes modify parted's library set to enable history.

    include/parted/Makefile.am:
    Adds history.h library header

    include/parted/device.h
    Avoids exposing a global PedHistoryManager by adding a pointer to one that
    should manage each device.

    include/parted/disk.h
    Adds the function: ped_disk_commit_to_history
    This routine stores disk commits to the history list, Where they can later
    be used to make the actual disk modifications all at once.

    include/parted/history.h
    Structures and routines to manage history.

    include/parted/parted.h
    Adds history.h library header

    libparted/Makefile.am
    Adds history.c

    libparted/arch/linux.c
    Initializes the PedHistoryManager for a Linux device.
    The other architectures need an initialization/free as well, but I will
    hold off on implementing that until we get this history modification
    in place.

    libparted/disk.c
    Implements ped_disk_commit_to_history()

    libparted/history.c
    Implements routines useful in managing parted disk history.
    These calls are called by parted's core
---
 include/parted/Makefile.am |    1 +
 include/parted/device.h    |    4 +
 include/parted/disk.h      |    3 +
 include/parted/history.h   |  120 +++++++++++++++++
 include/parted/parted.h    |    1 +
 libparted/Makefile.am      |    1 +
 libparted/arch/linux.c     |    6 +
 libparted/disk.c           |   11 ++
 libparted/history.c        |  309 ++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 456 insertions(+), 0 deletions(-)
 create mode 100644 include/parted/history.h
 create mode 100644 libparted/history.c

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/device.h b/include/parted/device.h
index 2a9679f..46c81f8 100644
--- a/include/parted/device.h
+++ b/include/parted/device.h
@@ -62,6 +62,8 @@ struct _PedCHSGeometry {
         int             sectors;
 };
 
+#include <parted/history.h>
+
 /** A block device - for example, /dev/hda, not /dev/hda3 */
 struct _PedDevice {
         PedDevice*      next;
@@ -87,6 +89,8 @@ struct _PedDevice {
         PedCHSGeometry  bios_geom;
         short           host, did;
 
+        PedHistManager* hist; /**< maintains a list of disk modifications */
+
         void*           arch_specific;
 };
 
diff --git a/include/parted/disk.h b/include/parted/disk.h
index b82ea0f..b007333 100644
--- a/include/parted/disk.h
+++ b/include/parted/disk.h
@@ -83,6 +83,7 @@ typedef const struct _PedDiskArchOps    PedDiskArchOps;
 #include <parted/filesys.h>
 #include <parted/natmath.h>
 #include <parted/geom.h>
+#include <parted/history.h>
 
 /** @} */
 
@@ -260,6 +261,8 @@ 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 PedHistRet ped_disk_commit_to_history (const PedDisk *disk,
+                                              PedHistManager *mgr);
 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..0e66ded
--- /dev/null
+++ b/include/parted/history.h
@@ -0,0 +1,120 @@
+/*
+    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_ALLOC,
+    PED_HISTORY_RET_NO_MANAGER,
+    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;
+    char         msg[32]; /* Error message */
+};
+
+
+/*
+ * Funcs
+ */
+
+/* Creates an empty history manager, or returns NULL on failure */
+extern PedHistManager *ped_history_new_manager (void);
+
+/* Frees all memory for an allocated history manager */
+extern PedHistRet ped_history_destroy_manager (PedHistManager *mgr);
+
+/* Add/Clear history */
+extern PedHistRet ped_history_add (PedHistManager *mgr, const char *cmd);
+extern PedHistRet ped_history_clear (PedHistManager *mgr);
+
+/* Iterating */
+extern const PedHistObj *ped_history_begin (PedHistManager *mgr);
+
+/* Duplicate of the most recent disk mod, this can safely be destroyed */
+extern PedDisk *ped_history_disk (PedHistManager *mgr);
+
+/* Before changes are committed */
+extern PedHistRet ped_history_undo (PedHistManager *mgr);
+extern PedHistRet ped_history_redo (PedHistManager *mgr);
+
+/* Alloc/dealloc */
+extern PedHistObj *ped_history_alloc_obj (void);
+extern void ped_history_dealloc_obj (PedHistObj *obj);
+
+/* 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 (PedHistManager *mgr,
+                                              PedHistPrintCB cb);
+
+/* Copy the most recent disk change */
+extern PedHistRet ped_history_add_disk (PedHistManager *mgr,
+                                        const PedDisk *disk);
+
+/* Print */
+extern void ped_history_print_debug (PedHistManager *mgr);
+extern const char *ped_history_print_ret (PedHistManager *mgr, PedHistRet val);
+
+
+#endif /* PED_HISTORY_H_INCLUDED */
diff --git a/include/parted/parted.h b/include/parted/parted.h
index 31ed6ec..32130d5 100644
--- a/include/parted/parted.h
+++ b/include/parted/parted.h
@@ -28,6 +28,7 @@ extern "C" {
 #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/libparted/Makefile.am b/libparted/Makefile.am
index 6f52193..6200166 100644
--- a/libparted/Makefile.am
+++ b/libparted/Makefile.am
@@ -26,6 +26,7 @@ libparted_la_SOURCES  = debug.c			\
 			timer.c			\
 			unit.c			\
 			disk.c			\
+			history.c		\
 			cs/geom.c		\
 			cs/constraint.c		\
 			cs/natmath.c		\
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 9854f51..1afcd0f 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -23,6 +23,7 @@
 #include <parted/parted.h>
 #include <parted/debug.h>
 #include <parted/linux.h>
+#include <parted/history.h>
 
 #include <ctype.h>
 #include <errno.h>
@@ -1117,6 +1118,10 @@ linux_new (const char* path)
         if (!dev->path)
                 goto error_free_dev;
 
+        dev->hist = ped_history_new_manager ();
+        if (!(dev->hist))
+                goto error;
+
         dev->arch_specific
                 = (LinuxSpecific*) ped_malloc (sizeof (LinuxSpecific));
         if (!dev->arch_specific)
@@ -1225,6 +1230,7 @@ error:
 static void
 linux_destroy (PedDevice* dev)
 {
+        ped_history_destroy_manager (dev->hist);
         ped_free (dev->arch_specific);
         ped_free (dev->path);
         ped_free (dev->model);
diff --git a/libparted/disk.c b/libparted/disk.c
index ec09996..3014289 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -511,6 +511,17 @@ ped_disk_commit (PedDisk* disk)
 	return ped_disk_commit_to_os (disk);
 }
 
+/** 
+ * This adds the disk changes to history.  
+ * These changes can further be undone or redone
+ * until the choses to save them to the partition table.
+ */
+PedHistRet
+ped_disk_commit_to_history (const PedDisk *disk, PedHistManager *mgr)
+{
+    return ped_history_add_disk (mgr, disk);
+}
+
 /**
  * \addtogroup PedPartition
  *
diff --git a/libparted/history.c b/libparted/history.c
new file mode 100644
index 0000000..f93112b
--- /dev/null
+++ b/libparted/history.c
@@ -0,0 +1,309 @@
+/*
+    libparted - a library for manipulating disk partitions
+    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 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/>.
+*/
+
+#include <parted/parted.h>
+
+
+/*
+ * Macros for handling error printing
+ */
+
+#if ENABLE_NLS
+#  include <libintl.h>
+#  include <locale.h>
+#  define _(String) dgettext (PACKAGE, String)
+#else
+#  define _(String) (String)
+#endif /* ENABLE_NLS */
+
+
+PedHistManager *
+ped_history_new_manager (void)
+{
+    return (PedHistManager *)(ped_calloc (sizeof (PedHistManager)));
+}
+
+
+PedHistRet
+ped_history_destroy_manager (PedHistManager *mgr)
+{
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    ped_history_clear (mgr);
+    ped_free (mgr);
+    return PED_HISTORY_RET_SUCCESS;
+}
+
+
+PedHistRet 
+ped_history_add (PedHistManager *mgr, const char *name)
+{
+    PedHistObj *addme;
+    size_t      name_length;
+    
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    /* Deep copy and add to end of list */
+    if (!(addme = ped_history_alloc_obj ()))
+        return PED_HISTORY_RET_NO_ALLOC;
+
+    /* Add the name using ped allocation */
+    name_length = strlen (name);
+    name_length = (name_length > PED_HISTORY_MAX_NAME) ?
+        PED_HISTORY_MAX_NAME : name_length;
+
+    if (!(addme->name = ped_malloc (name_length)))
+        return PED_HISTORY_RET_NO_ALLOC;
+
+    strncpy (addme->name, name, name_length);
+            
+    /* This becomes the new end, so remember who is before us */
+    addme->prev = mgr->end;
+
+    /* Add this to the end of the list */
+    if (mgr->end)
+        mgr->end->next = addme;
+    
+    mgr->end = addme;
+    
+    if (!mgr->begin)
+        mgr->begin = addme;
+
+    return PED_HISTORY_RET_SUCCESS;
+}
+
+
+PedHistRet 
+ped_history_clear (PedHistManager *mgr)
+{
+    PedHistObj *ii, *freeme;
+    
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+ 
+    ii = mgr->begin;
+    while (ii) {
+        freeme = ii;
+        ii = ii->next;
+        ped_history_dealloc_obj (freeme);
+    }
+
+    mgr->begin = NULL;
+    mgr->end = NULL;
+
+    return PED_HISTORY_RET_SUCCESS;
+}
+
+
+const PedHistObj *
+ped_history_begin (PedHistManager *mgr)
+{
+    if (!mgr)
+        return NULL;
+
+    return mgr->begin;
+}
+
+
+/* Return most recent (non-undone) disk modification */
+PedDisk *
+ped_history_disk (PedHistManager *mgr)
+{
+    PedHistObj *ii;
+    
+    for (ii = mgr->end; ii; ii = ii->prev)
+        if (!ii->ignore && ii->disk)
+            return ped_disk_duplicate (ii->disk);
+
+    return NULL;
+}
+
+
+PedHistRet 
+ped_history_undo (PedHistManager *mgr)
+{
+    PedHistObj *ii;
+    
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    /* Mark the most recent change as ignored  and that is an
+     * actual 'disk' modification
+     * Start with the last command issued before this 'undo' call
+     */
+     for (ii = mgr->end->prev; ii; ii = ii->prev)
+        if (!ii->ignore && ii->disk)
+            break;
+ 
+     if (!ii)
+        return PED_HISTORY_RET_NO_UNDO;
+
+     ii->ignore = 1;
+     return PED_HISTORY_RET_SUCCESS;
+}
+
+    
+PedHistRet 
+ped_history_redo (PedHistManager *mgr)
+{
+    PedHistObj *ii;
+    
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    /* Find the most recent undone entry that is a 'disk' mod */
+    for (ii = mgr->begin; ii; ii = ii->next)
+        if (ii->ignore && ii->disk)
+            break;
+
+    if (!ii)
+        return PED_HISTORY_RET_NO_REDO;
+
+    ii->ignore = 0;
+    return PED_HISTORY_RET_SUCCESS;
+}
+
+
+PedHistObj *
+ped_history_alloc_obj (void)
+{
+    return (PedHistObj *)ped_calloc (sizeof (PedHistObj));
+}
+
+
+void 
+ped_history_dealloc_obj (PedHistObj *obj)
+{
+    if (!obj)
+        return;
+
+    if (obj->disk)
+        ped_disk_destroy (obj->disk);
+  
+    ped_free (obj->name);
+    ped_free (obj);
+}
+
+
+PedHistRet 
+ped_history_commit_to_disk (PedHistManager *mgr, PedHistPrintCB cb)
+{
+    int         has_commit;
+    PedHistObj *ii;
+    
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    has_commit = 0;
+    for (ii = mgr->begin; ii; ii = ii->next) {
+        if (ii->disk && !ii->ignore) {
+            has_commit = 1;
+            if (ped_disk_commit (ii->disk) && cb)
+                cb (PED_HISTORY_RET_SUCCESS, ii);
+            else if (cb)
+                cb (PED_HISTORY_RET_ERROR, ii);
+        }
+    }
+  
+    /* Restart fresh */
+    ped_history_clear (mgr);
+
+    if (!has_commit)
+        return PED_HISTORY_RET_NO_SAVE;
+
+    return PED_HISTORY_RET_SUCCESS;
+} 
+
+
+PedHistRet
+ped_history_add_disk (PedHistManager *mgr, const PedDisk *disk)
+{
+    if (!mgr)
+        return PED_HISTORY_RET_NO_MANAGER;
+
+    mgr->end->disk = ped_disk_duplicate (disk);
+
+    return PED_HISTORY_RET_SUCCESS;
+}
+
+
+/* Print all history objects */
+void 
+ped_history_print_debug (PedHistManager *mgr)
+{
+    int         has_history;
+    PedHistObj *ii;
+
+    has_history = 0; 
+    for (ii = mgr->begin; ii; ii = ii->next) {
+
+        /* Only print disk changes */
+        if (!ii->disk)
+            continue;
+        
+        has_history = 1; 
+        printf (_("[History]\t"));
+    
+        if (ii->ignore)
+            printf (_(" (UNDONE)"));
+    }
+
+    if (!has_history)
+        printf (_(PED_HISTORY_TAG "No history available\n"));
+}
+
+
+const char *
+ped_history_print_ret (PedHistManager *mgr, PedHistRet val)
+{
+    switch (val)
+    {
+        case PED_HISTORY_RET_ERROR:
+            strncpy (mgr->msg, "unknown error", sizeof (mgr->msg));
+            break;
+
+        case PED_HISTORY_RET_NO_ALLOC:
+            strncpy (mgr->msg, "could not allocate memory", sizeof (mgr->msg));
+            break;
+        
+        case PED_HISTORY_RET_NO_MANAGER:
+            strncpy (mgr->msg, "no history manager available", sizeof (mgr->msg));
+            break;
+
+        case PED_HISTORY_RET_NO_UNDO:
+            strncpy (mgr->msg, "could not undo", sizeof (mgr->msg));
+            break;
+        
+        case PED_HISTORY_RET_NO_REDO:
+            strncpy (mgr->msg, "could not redo", sizeof (mgr->msg));
+            break;
+        
+        case PED_HISTORY_RET_NO_SAVE:
+            strncpy (mgr->msg, "nothing to save", sizeof (mgr->msg));
+            break;
+
+        default: 
+            memset (mgr->msg, 0, sizeof (mgr->msg));
+            break;
+    }
+
+    return mgr->msg;
+}
-- 
1.5.1.5

