2 * SDBM Database Utility
4 * (c) 2000--2001 Martin Mares <mj@ucw.cz>
9 #include "lib/db_internal.h"
10 #include "lib/fastbuf.h"
21 static int cache=1024;
22 static int force_key=-2;
23 static int force_val=-2;
24 static int force_page=-1;
26 #define SDBM_DUMP_MAGIC 0x321f120e
27 #define SDBM_DUMP_VERSION 1
30 dump(char *db, char *dmp)
34 struct sdbm_options op;
37 bzero(&op, sizeof(op));
43 die("Source open failed: %m");
45 dest = bopen(dmp, O_WRONLY | O_CREAT | O_TRUNC, 65536);
46 bputl(dest, SDBM_DUMP_MAGIC);
47 bputl(dest, SDBM_DUMP_VERSION);
48 bputl(dest, src->page_order);
49 bputl(dest, src->key_size);
50 bputl(dest, src->val_size);
52 fprintf(stderr, "Dumping database...\n");
56 byte key[65536], val[65536];
57 int klen = sizeof(key);
58 int vlen = sizeof(val);
59 e = sdbm_get_next(src, key, &klen, val, &vlen);
63 fprintf(stderr, "sdbm_get_next: error %d\n", e);
66 fprintf(stderr, "%d\r", c);
70 bwrite(dest, key, klen);
72 bwrite(dest, val, vlen);
77 fprintf(stderr, "Dumped %d records\n", c);
81 restore(char *dmp, char *db)
85 struct sdbm_options op;
88 src = bopen(dmp, O_RDONLY, 65536);
89 if (bgetl(src) != SDBM_DUMP_MAGIC ||
90 bgetl(src) != SDBM_DUMP_VERSION)
91 die("%s: not a sdbm dump", dmp);
93 bzero(&op, sizeof(op));
96 if (e < 0 && errno != ENOENT)
98 op.cache_size = cache;
99 op.flags = SDBM_CREAT | SDBM_WRITE | SDBM_FAST;
100 op.page_order = bgetl(src);
102 op.page_order = force_page;
103 op.key_size = bgetl(src);
105 op.key_size = force_key;
106 op.val_size = bgetl(src);
108 op.val_size = force_val;
109 dest = sdbm_open(&op);
111 die("Destination open failed");
113 fprintf(stderr, "Restoring database...\n");
116 byte key[65536], val[65536];
121 breadb(src, key, klen);
124 die("Corrupted dump file: value missing");
125 breadb(src, val, vlen);
128 fprintf(stderr, "%d\r", c);
131 if (sdbm_store(dest, key, klen, val, vlen) == 0)
132 fprintf(stderr, "sdbm_store: duplicate key\n");
137 fprintf(stderr, "Restored %d records\n", c);
141 rebuild(char *sdb, char *ddb)
143 struct sdbm *src, *dest;
144 struct sdbm_options op;
147 bzero(&op, sizeof(op));
151 src = sdbm_open(&op);
153 die("Source open failed: %m");
157 if (e < 0 && errno != ENOENT)
159 op.cache_size = cache;
160 op.flags = SDBM_CREAT | SDBM_WRITE | SDBM_FAST;
161 op.page_order = (force_page >= 0) ? force_page : src->root->page_order;
162 op.key_size = (force_key >= -1) ? force_key : src->root->key_size;
163 op.val_size = (force_val >= -1) ? force_val : src->root->val_size;
164 dest = sdbm_open(&op);
166 die("Destination open failed");
168 fprintf(stderr, "Rebuilding database...\n");
172 byte key[65536], val[65536];
173 int klen = sizeof(key);
174 int vlen = sizeof(val);
175 e = sdbm_get_next(src, key, &klen, val, &vlen);
179 fprintf(stderr, "sdbm_get_next: error %d\n", e);
182 fprintf(stderr, "%d\r", c);
185 if (sdbm_store(dest, key, klen, val, vlen) == 0)
186 fprintf(stderr, "sdbm_store: duplicate key\n");
191 fprintf(stderr, "Copied %d records\n", c);
195 main(int argc, char **argv)
199 while ((o = getopt(argc, argv, "vc:k:d:p:")) >= 0)
209 force_key=atol(optarg);
212 force_val=atol(optarg);
215 force_page=atol(optarg);
219 fprintf(stderr, "Usage: db-tool [<options>] <command> <database>\n\
223 -c<n>\t\tUse cache of <n> pages\n\
224 -d<n>\t\tSet data size to <n> (-1=variable) [restore,rebuild]\n\
225 -k<n>\t\tSet key size to <n> (-1=variable) [restore,rebuild]\n\
226 -p<n>\t\tSet page order to <n> [restore,rebuild]\n\
229 b <db> <new>\tRebuild database\n\
230 d <db> <dump>\tDump database\n\
231 r <dump> <db>\tRestore database from dump\n\
237 if (argc < 1 || strlen(argv[0]) != 1)
245 rebuild(argv[1], argv[2]);
250 dump(argv[1], argv[2]);
255 restore(argv[1], argv[2]);