2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static char copyright[] =
32 "@(#) Copyright (c) 1992, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)dbtest.c 8.17 (Berkeley) 9/1/94";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
56 enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
58 void compare(DBT *, DBT *);
59 DBTYPE dbtype(char *);
61 void err(const char *, ...) __printflike(1, 2);
62 void get(DB *, DBT *);
63 void getdata(DB *, DBT *, DBT *);
64 void put(DB *, DBT *, DBT *);
65 void rem(DB *, DBT *);
68 void *rfile(char *, size_t *);
69 void seq(DB *, DBT *);
70 u_int setflags(char *);
71 void *setinfo(DBTYPE, char *);
73 void *xmalloc(char *, size_t);
75 DBTYPE type; /* Database type. */
76 void *infop; /* Iflags. */
77 u_long lineno; /* Current line in test script. */
78 u_int flags; /* Current DB flags. */
79 int ofd = STDOUT_FILENO; /* Standard output fd. */
81 DB *XXdbp; /* Global for gdb. */
82 int XXlineno; /* Fast breakpoint for gdb. */
91 enum S command, state;
93 DBT data, key, keydata;
95 int ch, oflags, sflag;
96 char *fname, *infoarg, *p, *t, buf[8 * 1024];
100 oflags = O_CREAT | O_RDWR;
102 while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
114 if ((ofd = open(optarg,
115 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
116 err("%s: %s", optarg, strerror(errno));
132 type = dbtype(*argv++);
134 /* Open the descriptor file. */
135 if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
136 err("%s: %s", *argv, strerror(errno));
138 /* Set up the db structure as necessary. */
142 for (p = strtok(infoarg, ",\t "); p != NULL;
143 p = strtok(0, ",\t "))
145 infop = setinfo(type, p);
148 * Open the DB. Delete any preexisting copy, you almost never
149 * want it around, and it often screws up tests.
152 p = getenv("TMPDIR");
155 (void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
161 if ((dbp = dbopen(fname,
162 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
163 err("dbopen: %s", strerror(errno));
168 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
169 /* Delete the newline, displaying the key/data is easier. */
170 if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
172 if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
175 /* Convenient gdb break point. */
176 if (XXlineno == lineno)
179 case 'c': /* compare */
180 if (state != COMMAND)
181 err("line %lu: not expecting command", lineno);
186 if (state != COMMAND)
187 err("line %lu: not expecting command", lineno);
188 /* Don't display the newline, if CR at EOL. */
189 if (p[len - 2] == '\r')
191 if (write(ofd, p + 1, len - 1) != len - 1 ||
192 write(ofd, "\n", 1) != 1)
193 err("write: %s", strerror(errno));
196 if (state != COMMAND)
197 err("line %lu: not expecting command", lineno);
202 if (state != COMMAND)
203 err("line %lu: not expecting command", lineno);
207 case 'r': /* remove */
208 if (state != COMMAND)
209 err("line %lu: not expecting command", lineno);
210 if (flags == R_CURSOR) {
219 if (state != COMMAND)
220 err("line %lu: not expecting command", lineno);
225 if (state != COMMAND)
226 err("line %lu: not expecting command", lineno);
227 if (flags == R_CURSOR) {
234 flags = setflags(p + 1);
236 case 'D': /* data file */
238 err("line %lu: not expecting data", lineno);
239 data.data = rfile(p + 1, &data.size);
243 err("line %lu: not expecting data", lineno);
244 data.data = xmalloc(p + 1, len - 1);
246 ldata: switch (command) {
248 compare(&keydata, &data);
251 put(dbp, &key, &data);
254 err("line %lu: command doesn't take data",
257 if (type != DB_RECNO)
262 case 'K': /* key file */
264 err("line %lu: not expecting a key", lineno);
265 if (type == DB_RECNO)
266 err("line %lu: 'K' not available for recno",
268 key.data = rfile(p + 1, &key.size);
272 err("line %lu: not expecting a key", lineno);
273 if (type == DB_RECNO) {
274 static recno_t recno;
277 key.size = sizeof(recno);
279 key.data = xmalloc(p + 1, len - 1);
282 lkey: switch (command) {
284 getdata(dbp, &key, &keydata);
289 if (type != DB_RECNO)
298 if ((type != DB_RECNO) && (flags != R_CURSOR))
304 if ((type != DB_RECNO) && (flags != R_CURSOR))
309 err("line %lu: command doesn't take a key",
314 dump(dbp, p[1] == 'r');
317 err("line %lu: %s: unknown command character",
323 * -l must be used (DB_LOCK must be set) for this to be
324 * used, otherwise a page will be locked and it will fail.
326 if (type == DB_BTREE && oflags & DB_LOCK)
330 err("db->close: %s", strerror(errno));
335 #define NOOVERWRITE "put failed, would overwrite key\n"
344 if (db1->size != db2->size)
345 printf("compare failed: key->data len %lu != data len %lu\n",
346 db1->size, db2->size);
348 len = MIN(db1->size, db2->size);
349 for (p1 = db1->data, p2 = db2->data; len--;)
350 if (*p1++ != *p2++) {
351 printf("compare failed at offset %d\n",
352 p1 - (u_char *)db1->data);
364 switch (dbp->get(dbp, kp, &data, flags)) {
366 (void)write(ofd, data.data, data.size);
367 if (ofd == STDOUT_FILENO)
368 (void)write(ofd, "\n", 1);
371 err("line %lu: get: %s", lineno, strerror(errno));
374 #define NOSUCHKEY "get failed, no such key\n"
375 if (ofd != STDOUT_FILENO)
376 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
378 (void)fprintf(stderr, "%d: %.*s: %s",
379 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
390 switch (dbp->get(dbp, kp, dp, flags)) {
394 err("line %lu: getdata: %s", lineno, strerror(errno));
397 err("line %lu: getdata failed, no such key", lineno);
407 switch (dbp->put(dbp, kp, dp, flags)) {
411 err("line %lu: put: %s", lineno, strerror(errno));
414 (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
424 switch (dbp->del(dbp, kp, flags)) {
428 err("line %lu: rem: %s", lineno, strerror(errno));
431 #define NOSUCHKEY "rem failed, no such key\n"
432 if (ofd != STDOUT_FILENO)
433 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
434 else if (flags != R_CURSOR)
435 (void)fprintf(stderr, "%d: %.*s: %s",
436 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
438 (void)fprintf(stderr,
439 "%d: rem of cursor failed\n", lineno);
449 switch (dbp->sync(dbp, flags)) {
453 err("line %lu: synk: %s", lineno, strerror(errno));
465 switch (dbp->seq(dbp, kp, &data, flags)) {
467 (void)write(ofd, data.data, data.size);
468 if (ofd == STDOUT_FILENO)
469 (void)write(ofd, "\n", 1);
472 err("line %lu: seq: %s", lineno, strerror(errno));
475 #define NOSUCHKEY "seq failed, no such key\n"
476 if (ofd != STDOUT_FILENO)
477 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
478 else if (flags == R_CURSOR)
479 (void)fprintf(stderr, "%d: %.*s: %s",
480 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
482 (void)fprintf(stderr,
483 "%d: seq (%s) failed\n", lineno, sflags(flags));
504 for (;; flags = nflags)
505 switch (dbp->seq(dbp, &key, &data, flags)) {
507 (void)write(ofd, data.data, data.size);
508 if (ofd == STDOUT_FILENO)
509 (void)write(ofd, "\n", 1);
514 err("line %lu: (dump) seq: %s",
515 lineno, strerror(errno));
527 for (; isspace(*s); ++s);
528 if (*s == '\n' || *s == '\0')
530 if ((p = index(s, '\n')) != NULL)
532 if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
533 if (!strcmp(s, "R_FIRST")) return (R_FIRST);
534 if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
535 if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
536 if (!strcmp(s, "R_LAST")) return (R_LAST);
537 if (!strcmp(s, "R_NEXT")) return (R_NEXT);
538 if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
539 if (!strcmp(s, "R_PREV")) return (R_PREV);
540 if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
542 err("line %lu: %s: unknown flag", lineno, s);
551 case R_CURSOR: return ("R_CURSOR");
552 case R_FIRST: return ("R_FIRST");
553 case R_IAFTER: return ("R_IAFTER");
554 case R_IBEFORE: return ("R_IBEFORE");
555 case R_LAST: return ("R_LAST");
556 case R_NEXT: return ("R_NEXT");
557 case R_NOOVERWRITE: return ("R_NOOVERWRITE");
558 case R_PREV: return ("R_PREV");
559 case R_SETCURSOR: return ("R_SETCURSOR");
569 if (!strcmp(s, "btree"))
571 if (!strcmp(s, "hash"))
573 if (!strcmp(s, "recno"))
575 err("%s: unknown type (use btree, hash or recno)", s);
589 if ((eq = index(s, '=')) == NULL)
590 err("%s: illegal structure set statement", s);
593 err("%s: structure set statement must be a number", s);
597 if (!strcmp("flags", s)) {
601 if (!strcmp("cachesize", s)) {
602 ib.cachesize = atoi(eq);
605 if (!strcmp("maxkeypage", s)) {
606 ib.maxkeypage = atoi(eq);
609 if (!strcmp("minkeypage", s)) {
610 ib.minkeypage = atoi(eq);
613 if (!strcmp("lorder", s)) {
614 ib.lorder = atoi(eq);
617 if (!strcmp("psize", s)) {
623 if (!strcmp("bsize", s)) {
627 if (!strcmp("ffactor", s)) {
628 ih.ffactor = atoi(eq);
631 if (!strcmp("nelem", s)) {
635 if (!strcmp("cachesize", s)) {
636 ih.cachesize = atoi(eq);
639 if (!strcmp("lorder", s)) {
640 ih.lorder = atoi(eq);
645 if (!strcmp("flags", s)) {
649 if (!strcmp("cachesize", s)) {
650 rh.cachesize = atoi(eq);
653 if (!strcmp("lorder", s)) {
654 rh.lorder = atoi(eq);
657 if (!strcmp("reclen", s)) {
658 rh.reclen = atoi(eq);
661 if (!strcmp("bval", s)) {
665 if (!strcmp("psize", s)) {
671 err("%s: unknown structure value", s);
685 for (; isspace(*name); ++name);
686 if ((np = index(name, '\n')) != NULL)
688 if ((fd = open(name, O_RDONLY, 0)) < 0 ||
690 err("%s: %s\n", name, strerror(errno));
692 if (sb.st_size > (off_t)SIZE_T_MAX)
693 err("%s: %s\n", name, strerror(E2BIG));
695 if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
696 err("%s", strerror(errno));
697 (void)read(fd, p, (int)sb.st_size);
710 if ((p = (void *)malloc(len)) == NULL)
711 err("%s", strerror(errno));
712 memmove(p, text, len);
719 (void)fprintf(stderr,
720 "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
727 err(const char *fmt, ...)
732 (void)fprintf(stderr, "dbtest: ");
733 (void)vfprintf(stderr, fmt, ap);
735 (void)fprintf(stderr, "\n");