2 * SDBM Database Utility
4 * (c) 2000--2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "lib/db_internal.h"
13 #include "lib/fastbuf.h"
23 static int cache=1024;
24 static int force_key=-2;
25 static int force_val=-2;
26 static int force_page=-1;
28 #define SDBM_DUMP_MAGIC 0x321f120e
29 #define SDBM_DUMP_VERSION 1
32 dump(char *db, char *dmp)
36 struct sdbm_options op;
39 bzero(&op, sizeof(op));
45 die("Source open failed: %m");
47 dest = bopen(dmp, O_WRONLY | O_CREAT | O_TRUNC, 65536);
48 bputl(dest, SDBM_DUMP_MAGIC);
49 bputl(dest, SDBM_DUMP_VERSION);
50 bputl(dest, src->page_order);
51 bputl(dest, src->key_size);
52 bputl(dest, src->val_size);
54 fprintf(stderr, "Dumping database...\n");
58 byte key[65536], val[65536];
59 int klen = sizeof(key);
60 int vlen = sizeof(val);
61 e = sdbm_get_next(src, key, &klen, val, &vlen);
65 fprintf(stderr, "sdbm_get_next: error %d\n", e);
68 fprintf(stderr, "%d\r", c);
72 bwrite(dest, key, klen);
74 bwrite(dest, val, vlen);
79 fprintf(stderr, "Dumped %d records\n", c);
83 restore(char *dmp, char *db)
87 struct sdbm_options op;
90 src = bopen(dmp, O_RDONLY, 65536);
91 if (bgetl(src) != SDBM_DUMP_MAGIC ||
92 bgetl(src) != SDBM_DUMP_VERSION)
93 die("%s: not a sdbm dump", dmp);
95 bzero(&op, sizeof(op));
98 if (e < 0 && errno != ENOENT)
100 op.cache_size = cache;
101 op.flags = SDBM_CREAT | SDBM_WRITE | SDBM_FAST;
102 op.page_order = bgetl(src);
104 op.page_order = force_page;
105 op.key_size = bgetl(src);
107 op.key_size = force_key;
108 op.val_size = bgetl(src);
110 op.val_size = force_val;
111 dest = sdbm_open(&op);
113 die("Destination open failed");
115 fprintf(stderr, "Restoring database...\n");
118 byte key[65536], val[65536];
123 breadb(src, key, klen);
126 die("Corrupted dump file: value missing");
127 breadb(src, val, vlen);
130 fprintf(stderr, "%d\r", c);
133 if (sdbm_store(dest, key, klen, val, vlen) == 0)
134 fprintf(stderr, "sdbm_store: duplicate key\n");
139 fprintf(stderr, "Restored %d records\n", c);
143 rebuild(char *sdb, char *ddb)
145 struct sdbm *src, *dest;
146 struct sdbm_options op;
149 bzero(&op, sizeof(op));
153 src = sdbm_open(&op);
155 die("Source open failed: %m");
159 if (e < 0 && errno != ENOENT)
161 op.cache_size = cache;
162 op.flags = SDBM_CREAT | SDBM_WRITE | SDBM_FAST;
163 op.page_order = (force_page >= 0) ? (u32) force_page : src->root->page_order;
164 op.key_size = (force_key >= -1) ? force_key : src->root->key_size;
165 op.val_size = (force_val >= -1) ? force_val : src->root->val_size;
166 dest = sdbm_open(&op);
168 die("Destination open failed");
170 fprintf(stderr, "Rebuilding database...\n");
174 byte key[65536], val[65536];
175 int klen = sizeof(key);
176 int vlen = sizeof(val);
177 e = sdbm_get_next(src, key, &klen, val, &vlen);
181 fprintf(stderr, "sdbm_get_next: error %d\n", e);
184 fprintf(stderr, "%d\r", c);
187 if (sdbm_store(dest, key, klen, val, vlen) == 0)
188 fprintf(stderr, "sdbm_store: duplicate key\n");
193 fprintf(stderr, "Copied %d records\n", c);
197 main(int argc, char **argv)
201 while ((o = getopt(argc, argv, "vc:k:d:p:")) >= 0)
211 force_key=atol(optarg);
214 force_val=atol(optarg);
217 force_page=atol(optarg);
221 fprintf(stderr, "Usage: db-tool [<options>] <command> <database>\n\
225 -c<n>\t\tUse cache of <n> pages\n\
226 -d<n>\t\tSet data size to <n> (-1=variable) [restore,rebuild]\n\
227 -k<n>\t\tSet key size to <n> (-1=variable) [restore,rebuild]\n\
228 -p<n>\t\tSet page order to <n> [restore,rebuild]\n\
231 b <db> <new>\tRebuild database\n\
232 d <db> <dump>\tDump database\n\
233 r <dump> <db>\tRestore database from dump\n\
239 if (argc < 1 || strlen(argv[0]) != 1)
247 rebuild(argv[1], argv[2]);
252 dump(argv[1], argv[2]);
257 restore(argv[1], argv[2]);