2 * UCW Library -- Configuration files: journaling
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include <ucw/getopt.h>
14 #include <ucw/conf-internal.h>
15 #include <ucw/mempool.h>
20 struct old_pools *prev;
22 }; // link-list of older cf_pool's
24 struct cf_journal_item {
25 struct cf_journal_item *prev;
32 cf_journal_block(void *ptr, uns len)
34 struct cf_context *cc = cf_get_context();
35 if (!cc->need_journal)
37 struct cf_journal_item *ji = cf_malloc(sizeof(struct cf_journal_item) + len);
38 ji->prev = cc->journal;
41 memcpy(ji->copy, ptr, len);
47 // swaps the contents of the memory and the journal, and reverses the list
49 struct cf_context *cc = cf_get_context();
50 struct cf_journal_item *curr, *prev, *next;
51 for (next=NULL, curr=cc->journal; curr; next=curr, curr=prev)
55 for (uns i=0; i<curr->len; i++)
57 byte x = curr->copy[i];
58 curr->copy[i] = curr->ptr[i];
65 struct cf_journal_item *
66 cf_journal_new_transaction(uns new_pool)
68 struct cf_context *cc = cf_get_context();
70 cc->pool = mp_new(1<<10);
71 struct cf_journal_item *oldj = cc->journal;
77 cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj)
79 struct cf_context *cc = cf_get_context();
82 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
89 struct cf_journal_item **j = &cc->journal;
97 cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj)
99 struct cf_context *cc = cf_get_context();
100 if (!cc->need_journal)
101 die("Cannot rollback the configuration, because the journal is disabled.");
107 cc->pool = cc->pools ? cc->pools->pool : NULL;
112 cf_journal_delete(void)
114 struct cf_context *cc = cf_get_context();
115 for (struct old_pools *p=cc->pools; p; p=cc->pools)
122 /* TODO: more space efficient journal */