implement uci revert
authorFelix Fietkau <nbd@openwrt.org>
Sun, 3 Feb 2008 05:05:08 +0000 (06:05 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 3 Feb 2008 05:05:08 +0000 (06:05 +0100)
cli.c
file.c
history.c
uci.h
util.c

diff --git a/cli.c b/cli.c
index c3e92e3..946a905 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -31,6 +31,7 @@ enum {
        CMD_SET,
        CMD_DEL,
        CMD_RENAME,
        CMD_SET,
        CMD_DEL,
        CMD_RENAME,
+       CMD_REVERT,
        /* package cmds */
        CMD_SHOW,
        CMD_IMPORT,
        /* package cmds */
        CMD_SHOW,
        CMD_IMPORT,
@@ -49,6 +50,7 @@ static void uci_usage(int argc, char **argv)
                "\tget        <config>.<section>[.<option>]\n"
                "\tset        <config>.<section>[.<option>]=<value>\n"
                "\trename     <config>.<section>[.<option>]=<name>\n"
                "\tget        <config>.<section>[.<option>]\n"
                "\tset        <config>.<section>[.<option>]=<value>\n"
                "\trename     <config>.<section>[.<option>]=<name>\n"
+               "\trevert     <config>[.<section>[.<option>]]\n"
                "\n"
                "Options:\n"
                "\t-f <file>  use <file> as input instead of stdin\n"
                "\n"
                "Options:\n"
                "\t-f <file>  use <file> as input instead of stdin\n"
@@ -245,6 +247,9 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
        case CMD_RENAME:
                ret = uci_rename(ctx, p, section, option, value);
                break;
        case CMD_RENAME:
                ret = uci_rename(ctx, p, section, option, value);
                break;
+       case CMD_REVERT:
+               ret = uci_revert(ctx, &p, section, option);
+               break;
        case CMD_SET:
                ret = uci_set(ctx, p, section, option, value);
                break;
        case CMD_SET:
                ret = uci_set(ctx, p, section, option, value);
                break;
@@ -254,7 +259,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv)
        }
 
        /* no save necessary for get */
        }
 
        /* no save necessary for get */
-       if (cmd == CMD_GET)
+       if ((cmd == CMD_GET) || (cmd == CMD_REVERT))
                return 0;
 
        /* save changes, but don't commit them yet */
                return 0;
 
        /* save changes, but don't commit them yet */
@@ -286,6 +291,8 @@ static int uci_cmd(int argc, char **argv)
        else if (!strcasecmp(argv[0], "ren") ||
                 !strcasecmp(argv[0], "rename"))
                cmd = CMD_RENAME;
        else if (!strcasecmp(argv[0], "ren") ||
                 !strcasecmp(argv[0], "rename"))
                cmd = CMD_RENAME;
+       else if (!strcasecmp(argv[0], "revert"))
+               cmd = CMD_REVERT;
        else if (!strcasecmp(argv[0], "del"))
                cmd = CMD_DEL;
        else if (!strcasecmp(argv[0], "import"))
        else if (!strcasecmp(argv[0], "del"))
                cmd = CMD_DEL;
        else if (!strcasecmp(argv[0], "import"))
@@ -298,6 +305,7 @@ static int uci_cmd(int argc, char **argv)
                case CMD_SET:
                case CMD_DEL:
                case CMD_RENAME:
                case CMD_SET:
                case CMD_DEL:
                case CMD_RENAME:
+               case CMD_REVERT:
                        return uci_do_section_cmd(cmd, argc, argv);
                case CMD_SHOW:
                case CMD_EXPORT:
                        return uci_do_section_cmd(cmd, argc, argv);
                case CMD_SHOW:
                case CMD_EXPORT:
diff --git a/file.c b/file.c
index d81bbe8..169cf6e 100644 (file)
--- a/file.c
+++ b/file.c
@@ -521,8 +521,8 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u
        /* make sure no memory from previous parse attempts is leaked */
        uci_file_cleanup(ctx);
 
        /* make sure no memory from previous parse attempts is leaked */
        uci_file_cleanup(ctx);
 
-       pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
-       ctx->pctx = pctx;
+       uci_alloc_parse_context(ctx);
+       pctx = ctx->pctx;
        pctx->file = stream;
        if (*package && single) {
                pctx->package = *package;
        pctx->file = stream;
        if (*package && single) {
                pctx->package = *package;
@@ -673,7 +673,7 @@ int uci_commit(struct uci_context *ctx, struct uci_package **package, bool overw
                        /* freed together with the uci_package */
                        path = NULL;
 
                        /* freed together with the uci_package */
                        path = NULL;
 
-                       /* check for updated history, just in case */
+                       /* check for updated history, flush */
                        uci_load_history(ctx, p, true);
                } else {
                        /* flush history */
                        uci_load_history(ctx, p, true);
                } else {
                        /* flush history */
index 5b652b9..0526361 100644 (file)
--- a/history.c
+++ b/history.c
@@ -52,6 +52,20 @@ int uci_add_history_path(struct uci_context *ctx, const char *dir)
        return 0;
 }
 
        return 0;
 }
 
+static inline void uci_parse_history_tuple(struct uci_context *ctx, char **buf, char **package, char **section, char **option, char **value, bool *delete, bool *rename)
+{
+       if (**buf == '-') {
+               if (delete)
+                       *delete = true;
+               *buf += 1;
+       } else if (**buf == '@') {
+               if (rename)
+                       *rename = true;
+               *buf += 1;
+       }
+
+       UCI_INTERNAL(uci_parse_tuple, ctx, *buf, package, section, option, value);
+}
 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
 {
        bool delete = false;
 static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *p, char *buf)
 {
        bool delete = false;
@@ -61,15 +75,7 @@ static void uci_parse_history_line(struct uci_context *ctx, struct uci_package *
        char *option = NULL;
        char *value = NULL;
 
        char *option = NULL;
        char *value = NULL;
 
-       if (buf[0] == '-') {
-               delete = true;
-               buf++;
-       } else if (buf[0] == '@') {
-               rename = true;
-               buf++;
-       }
-
-       UCI_INTERNAL(uci_parse_tuple, ctx, buf, &package, &section, &option, &value);
+       uci_parse_history_tuple(ctx, &buf, &package, &section, &option, &value, &delete, &rename);
        if (!package || (strcmp(package, p->e.name) != 0))
                goto error;
        if (!uci_validate_name(section))
        if (!package || (strcmp(package, p->e.name) != 0))
                goto error;
        if (!uci_validate_name(section))
@@ -171,6 +177,114 @@ static void uci_load_history(struct uci_context *ctx, struct uci_package *p, boo
        ctx->errno = 0;
 }
 
        ctx->errno = 0;
 }
 
+static void uci_filter_history(struct uci_context *ctx, const char *name, char *section, char *option)
+{
+       struct uci_parse_context *pctx;
+       struct uci_element *e, *tmp;
+       struct uci_list list;
+       char *filename = NULL;
+       char *p = NULL;
+       char *s = NULL;
+       char *o = NULL;
+       char *v = NULL;
+       FILE *f = NULL;
+
+       uci_list_init(&list);
+       uci_alloc_parse_context(ctx);
+       pctx = ctx->pctx;
+
+       if ((asprintf(&filename, "%s/%s", ctx->savedir, name) < 0) || !filename)
+               UCI_THROW(ctx, UCI_ERR_MEM);
+
+       UCI_TRAP_SAVE(ctx, done);
+       f = uci_open_stream(ctx, filename, SEEK_SET, true, false);
+       pctx->file = f;
+       while (!feof(f)) {
+               struct uci_element *e;
+               char *buf;
+
+               uci_getln(ctx, 0);
+               buf = pctx->buf;
+               if (!buf[0])
+                       continue;
+
+               /* NB: need to allocate the element before the call to 
+                * uci_parse_history_tuple, otherwise the original string 
+                * gets modified before it is saved */
+               e = uci_alloc_generic(ctx, UCI_TYPE_HISTORY, pctx->buf, sizeof(struct uci_element));
+               uci_list_add(&list, &e->list);
+
+               uci_parse_history_tuple(ctx, &buf, &p, &s, &o, &v, NULL, NULL);
+               if (section) {
+                       if (!s || (strcmp(section, s) != 0))
+                               continue;
+               }
+               if (option) {
+                       if (!o || (strcmp(option, o) != 0))
+                               continue;
+               }
+               /* match, drop this element again */
+               uci_free_element(e);
+       }
+
+       /* rebuild the history file */
+       rewind(f);
+       ftruncate(fileno(f), 0);
+       uci_foreach_element_safe(&list, tmp, e) {
+               fprintf(f, "%s\n", e->name);
+               uci_free_element(e);
+       }
+       UCI_TRAP_RESTORE(ctx);
+
+done:
+       if (filename)
+               free(filename);
+       uci_close_stream(f);
+       uci_foreach_element_safe(&list, tmp, e) {
+               uci_free_element(e);
+       }
+       ctx->internal = true;
+       uci_cleanup(ctx);
+}
+
+int uci_revert(struct uci_context *ctx, struct uci_package **pkg, char *section, char *option)
+{
+       struct uci_package *p;
+       char *name = NULL;
+
+       UCI_HANDLE_ERR(ctx);
+       UCI_ASSERT(ctx, pkg != NULL);
+       p = *pkg;
+       UCI_ASSERT(ctx, p != NULL);
+       UCI_ASSERT(ctx, p->confdir);
+
+       /* 
+        * - flush unwritten changes
+        * - save the package name
+        * - unload the package
+        * - filter the history
+        * - reload the package
+        */
+       UCI_TRAP_SAVE(ctx, error);
+       UCI_INTERNAL(uci_save, ctx, p);
+       name = uci_strdup(ctx, p->e.name);
+
+       *pkg = NULL;
+       uci_free_package(&p);
+       uci_filter_history(ctx, name, section, option);
+
+       UCI_INTERNAL(uci_load, ctx, name, &p);
+       UCI_TRAP_RESTORE(ctx);
+
+       goto done;
+error:
+       if (name)
+               free(name);
+       UCI_THROW(ctx, ctx->errno);
+done:
+       return 0;
+}
+
 int uci_save(struct uci_context *ctx, struct uci_package *p)
 {
        FILE *f = NULL;
 int uci_save(struct uci_context *ctx, struct uci_package *p)
 {
        FILE *f = NULL;
diff --git a/uci.h b/uci.h
index 43270c4..8e73bfe 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -251,6 +251,15 @@ extern int uci_set_confdir(struct uci_context *ctx, const char *dir);
  */
 extern int uci_add_history_path(struct uci_context *ctx, const char *dir);
 
  */
 extern int uci_add_history_path(struct uci_context *ctx, const char *dir);
 
+/**
+ * uci_revert: revert all changes to a config item
+ * @ctx: uci context
+ * @p: pointer to a uci_package struct ptr (will be replaced by the revert)
+ * @section: section name (optional)
+ * @option option name (optional)
+ */
+extern int uci_revert(struct uci_context *ctx, struct uci_package **p, char *section, char *option);
+
 /* UCI data structures */
 enum uci_type {
        UCI_TYPE_HISTORY = 0,
 /* UCI data structures */
 enum uci_type {
        UCI_TYPE_HISTORY = 0,
diff --git a/util.c b/util.c
index e76ff6b..bcf65aa 100644 (file)
--- a/util.c
+++ b/util.c
@@ -72,6 +72,11 @@ static bool uci_validate_name(const char *str)
        return true;
 }
 
        return true;
 }
 
+static void uci_alloc_parse_context(struct uci_context *ctx)
+{
+       ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
+}
+
 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
 {
        char *last = NULL;
 int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
 {
        char *last = NULL;