]> mj.ucw.cz Git - libucw.git/commitdiff
Conf: Cleaned up use of journal
authorMartin Mares <mj@ucw.cz>
Sun, 29 Apr 2012 11:22:52 +0000 (13:22 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 29 Apr 2012 11:57:31 +0000 (13:57 +0200)
Introduced a new function cf_revert() for reverting the configuration
to its pristine state. It is automatically called upon cf_free_context().

Also, everything except cf_reload() now should work with journalling
disabled. In this case, nothing is ever rolled back.

ucw/conf-context.c
ucw/conf-input.c
ucw/conf-internal.h
ucw/conf-journal.c
ucw/conf-test.c
ucw/conf.h

index f4929644d5a74fb6da626c657d78724fc8a547d4..9572f06e22fb0a93e7e98f2a397528cddb9319e2 100644 (file)
@@ -17,7 +17,7 @@ static struct cf_context cf_default_context;
 static void
 cf_init_context(struct cf_context *cc)
 {
-  cc->need_journal = 1;
+  cc->enable_journal = 1;
   clist_init(&cc->conf_entries);
 }
 
@@ -32,9 +32,11 @@ cf_new_context(void)
 void
 cf_free_context(struct cf_context *cc)
 {
-  // FIXME: Roll back all transactions
   ASSERT(!cc->is_active);
   ASSERT(cc != &cf_default_context);
+  struct cf_context *prev = cf_switch_context(cc);
+  cf_revert();
+  cf_switch_context(prev);
   xfree(cc->parser);
   xfree(cc);
 }
index 3dc5db1d385ef96baae70de29102988d9f8c0649..9fa3a42fe5cb492d2d576eef03fd1b07af875fd5 100644 (file)
@@ -329,7 +329,7 @@ struct conf_entry { /* We remember a list of actions to apply upon reload */
 static void
 cf_remember_entry(struct cf_context *cc, uns type, const char *arg)
 {
-  if (!cc->need_journal)
+  if (!cc->enable_journal)
     return;
   struct conf_entry *ce = cf_malloc(sizeof(*ce));
   ce->type = type;
@@ -341,6 +341,7 @@ int
 cf_reload(const char *file)
 {
   struct cf_context *cc = cf_get_context();
+  ASSERT(cc->enable_journal);
   cf_journal_swap();
   struct cf_journal_item *oldj = cf_journal_new_transaction(1);
   uns ec = cc->everything_committed;
@@ -406,3 +407,10 @@ cf_set(const char *string)
     cf_journal_rollback_transaction(0, oldj);
   return err;
 }
+
+void
+cf_revert(void)
+{
+  cf_journal_swap();
+  cf_journal_delete();
+}
index 95ef6e6ff2892918610e73cdd7a28c9ee0580f73..67a96cf5baece5563bf866c68a57d265fcd33467 100644 (file)
@@ -49,7 +49,7 @@ struct cf_context {
   uns other_options;                   // used internally by cf_getopt()
   clist conf_entries;                  // files/strings to reload
   struct cf_journal_item *journal;     // journalling
-  int need_journal;
+  int enable_journal;
   struct old_pools *pools;
   struct item_stack stack[MAX_STACK_SIZE];     // interpreter stack
   uns stack_level;
index 6412328acaf9017f32f545167e747d52d463554e..1cd51a6aa1eee859ed786d7d503f55877ac077b0 100644 (file)
@@ -33,14 +33,14 @@ cf_set_journalling(int enable)
 {
   struct cf_context *cc = cf_get_context();
   ASSERT(!cc->journal);
-  cc->need_journal = enable;
+  cc->enable_journal = enable;
 }
 
 void
 cf_journal_block(void *ptr, uns len)
 {
   struct cf_context *cc = cf_get_context();
-  if (!cc->need_journal)
+  if (!cc->enable_journal)
     return;
   struct cf_journal_item *ji = cf_malloc(sizeof(struct cf_journal_item) + len);
   ji->prev = cc->journal;
@@ -105,8 +105,8 @@ void
 cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj)
 {
   struct cf_context *cc = cf_get_context();
-  if (!cc->need_journal)
-    die("Cannot rollback the configuration, because the journal is disabled.");
+  if (!cc->enable_journal)
+    return;
   cf_journal_swap();
   cc->journal = oldj;
   if (new_pool)
@@ -126,5 +126,3 @@ cf_journal_delete(void)
     mp_delete(p->pool);
   }
 }
-
-/* TODO: more space efficient journal */
index fb353f91bbf924d3af98ed8d10e08385e3b28acb..2eb516bd0610fad0d9074f7eede689a9c9b786c2 100644 (file)
@@ -2,6 +2,7 @@
  *     Insane tester of reading configuration files
  *
  *     (c) 2006 Robert Spalek <robert@ucw.cz>
+ *     (c) 2012 Martin Mares <mj@ucw.cz>
  */
 
 #include <ucw/lib.h>
@@ -15,7 +16,6 @@
 #include <time.h>
 
 static int verbose;
-static int new_context;
 static int reload;
 
 struct sub_sect_1 {
@@ -173,7 +173,7 @@ static struct option long_opts[] = {
 };
 
 static char *help = "\
-Usage: conf-test [ctxt] <options>\n\
+Usage: conf-test [ctxt] [nojournal] <options>\n\
 \n\
 Options:\n" CF_USAGE "\
 -r, --reload\t\tReload configuration\n\
@@ -195,12 +195,19 @@ int
 main(int argc, char *argv[])
 {
   log_init(argv[0]);
-
   struct cf_context *cc = NULL, *prev = NULL;
-  if (argc > 1 && !strcmp(argv[1], "ctxt")) {
-    cc = cf_new_context();
-    prev = cf_switch_context(cc);
-    argc--, argv++;
+
+  // Special arguments which have to be parsed before cf_getopt()
+  while (argc > 1) {
+    if (!strcmp(argv[1], "ctxt")) {
+      cc = cf_new_context();
+      prev = cf_switch_context(cc);
+      argc--, argv++;
+    } else if (!strcmp(argv[1], "nojournal")) {
+      cf_set_journalling(0);
+      argc--, argv++;
+    } else
+      break;
   }
 
   cf_declare_section("top", &cf_top, 0);
@@ -209,7 +216,6 @@ main(int argc, char *argv[])
   int opt;
   while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (opt) {
-      case 'n': new_context++; break;
       case 'r': reload++; break;
       case 'v': verbose++; break;
       default: usage("unknown option %c\n", opt);
@@ -228,10 +234,12 @@ main(int argc, char *argv[])
     bclose(out);
   }
 
-  if (new_context) {
+  if (cc) {
     cf_switch_context(prev);
     cf_free_context(cc);
   }
 
+  printf("%08x\n", ip);
+
   return 0;
 }
index fc228eb86826cb1bb15c0d38e0739306b13681cc..9e93eb0f22ee95e22a3a52d9ab74523c51071b34 100644 (file)
@@ -63,6 +63,7 @@ struct cf_context *cf_switch_context(struct cf_context *cc);
  * configuration specified in the file are undone.
  **/
 int cf_load(const char *file);
+
 /**
  * Reload configuration from @file, replace the old one.
  * If @file is NULL, reload all loaded configuration files and re-apply
@@ -71,6 +72,7 @@ int cf_load(const char *file);
  * settings are rolled back to the state before calling this function.
  **/
 int cf_reload(const char *file);
+
 /**
  * Parse some part of configuration passed in @string.
  * The syntax is the same as in the <<config:,configuration file>>.
@@ -97,6 +99,12 @@ void cf_open_group(void);
  **/
 int cf_close_group(void);
 
+/**
+ * Return all configuration items to their initial state before loading the
+ * configuration file. If journalling is disabled, it does nothing.
+ **/
+void cf_revert(void);
+
 /*** === Data types [[conf_types]] ***/
 
 enum cf_class {                                /** Class of the configuration item. **/