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
* 3. All advertising materials mentioning features or use of this software
14
* must display the following acknowledgement:
15
* This product includes software developed by the University of
16
* California, Berkeley and its contributors.
17
* 4. Neither the name of the University nor the names of its contributors
18
* may be used to endorse or promote products derived from this software
19
* without specific prior written permission.
21
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
#if !defined(lint) && defined(LIBC_SCCS)
35
static char copyright[] =
36
"@(#) Copyright (c) 1992, 1993, 1994\n\
37
The Regents of the University of California. All rights reserved.\n";
40
#if !defined(lint) && defined(LIBC_SCCS)
41
static char sccsid[] = "@(#)dbtest.c 8.17 (Berkeley) 9/1/94";
44
#include <sys/param.h>
61
enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
63
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
64
#define ATTR(x) __attribute__(x)
69
void compare __P((DBT *, DBT *));
70
DBTYPE dbtype __P((char *));
71
void dump __P((DB *, int));
72
void err __P((const char *, ...)) ATTR ((__format__(__printf__,1,2))) ATTR ((__noreturn__));
73
void get __P((DB *, DBT *));
74
void getdata __P((DB *, DBT *, DBT *));
75
void put __P((DB *, DBT *, DBT *));
76
void rem __P((DB *, DBT *));
77
char *sflags __P((int));
78
void synk __P((DB *));
79
void *rfile __P((char *, size_t *));
80
void seq __P((DB *, DBT *));
81
u_int setflags __P((char *));
82
void *setinfo __P((DBTYPE, char *));
83
void usage __P((void));
84
void *xmalloc __P((char *, size_t));
86
DBTYPE type; /* Database type. */
87
void *infop; /* Iflags. */
88
u_long lineno; /* Current line in test script. */
89
u_int flags; /* Current DB flags. */
90
int ofd = STDOUT_FILENO; /* Standard output fd. */
92
DB *XXdbp; /* Global for gdb. */
93
int XXlineno; /* Fast breakpoint for gdb. */
102
enum S command, state;
104
DBT data, key, keydata;
106
int ch, oflags, sflag;
107
char *fname, *infoarg, *p, *t, buf[8 * 1024];
111
oflags = O_CREAT | O_RDWR | O_BINARY;
113
while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
125
if ((ofd = open(optarg,
126
O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
127
err("%s: %s", optarg, strerror(errno));
143
type = dbtype(*argv++);
145
/* Open the descriptor file. */
146
if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
147
err("%s: %s", *argv, strerror(errno));
149
/* Set up the db structure as necessary. */
153
for (p = strtok(infoarg, ",\t "); p != NULL;
154
p = strtok(0, ",\t "))
156
infop = setinfo(type, p);
159
* Open the DB. Delete any preexisting copy, you almost never
160
* want it around, and it often screws up tests.
163
p = getenv("TMPDIR");
166
(void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
172
if ((dbp = dbopen(fname,
173
oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
174
err("dbopen: %s", strerror(errno));
179
(p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
180
/* Delete the newline, displaying the key/data is easier. */
181
if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
183
if ((len = strlen(buf)) == 0 || isspace((int) *p) || *p == '#')
186
/* Convenient gdb break point. */
187
if (XXlineno == lineno)
190
case 'c': /* compare */
191
if (state != COMMAND)
192
err("line %lu: not expecting command", lineno);
197
if (state != COMMAND)
198
err("line %lu: not expecting command", lineno);
199
/* Don't display the newline, if CR at EOL. */
200
if (p[len - 2] == '\r')
202
if (write(ofd, p + 1, len - 1) != len - 1 ||
203
write(ofd, "\n", 1) != 1)
204
err("write: %s", strerror(errno));
207
if (state != COMMAND)
208
err("line %lu: not expecting command", lineno);
213
if (state != COMMAND)
214
err("line %lu: not expecting command", lineno);
218
case 'r': /* remove */
219
if (state != COMMAND)
220
err("line %lu: not expecting command", lineno);
221
if (flags == R_CURSOR) {
230
if (state != COMMAND)
231
err("line %lu: not expecting command", lineno);
236
if (state != COMMAND)
237
err("line %lu: not expecting command", lineno);
238
if (flags == R_CURSOR) {
245
flags = setflags(p + 1);
247
case 'D': /* data file */
249
err("line %lu: not expecting data", lineno);
250
data.data = rfile(p + 1, &data.size);
254
err("line %lu: not expecting data", lineno);
255
data.data = xmalloc(p + 1, len - 1);
257
ldata: switch (command) {
259
compare(&keydata, &data);
262
put(dbp, &key, &data);
265
err("line %lu: command doesn't take data",
268
if (type != DB_RECNO)
273
case 'K': /* key file */
275
err("line %lu: not expecting a key", lineno);
276
if (type == DB_RECNO)
277
err("line %lu: 'K' not available for recno",
279
key.data = rfile(p + 1, &key.size);
283
err("line %lu: not expecting a key", lineno);
284
if (type == DB_RECNO) {
285
static recno_t recno;
288
key.size = sizeof(recno);
290
key.data = xmalloc(p + 1, len - 1);
293
lkey: switch (command) {
295
getdata(dbp, &key, &keydata);
300
if (type != DB_RECNO)
309
if ((type != DB_RECNO) && (flags != R_CURSOR))
315
if ((type != DB_RECNO) && (flags != R_CURSOR))
320
err("line %lu: command doesn't take a key",
325
dump(dbp, p[1] == 'r');
328
err("line %lu: %s: unknown command character",
334
* -l must be used (DB_LOCK must be set) for this to be
335
* used, otherwise a page will be locked and it will fail.
337
if (type == DB_BTREE && oflags & DB_LOCK)
341
err("db->close: %s", strerror(errno));
346
#define NOOVERWRITE "put failed, would overwrite key\n"
353
register u_char *p1, *p2;
355
if (db1->size != db2->size) {
356
printf("compare failed: key->data len %lu != data len %lu\n",
357
(u_long) db1->size, (u_long) db2->size);
361
len = MIN(db1->size, db2->size);
362
for (p1 = db1->data, p2 = db2->data; len--;)
363
if (*p1++ != *p2++) {
364
err("compare failed at offset %d\n",
365
p1 - (u_char *)db1->data);
377
switch (dbp->get(dbp, kp, &data, flags)) {
379
(void)write(ofd, data.data, data.size);
380
if (ofd == STDOUT_FILENO)
381
(void)write(ofd, "\n", 1);
384
err("line %lu: get: %s", lineno, strerror(errno));
387
#define NOSUCHKEY "get failed, no such key\n"
388
if (ofd != STDOUT_FILENO) {
389
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
392
(void)fprintf(stderr, "%lu: %.*s: %s",
393
lineno, (int) MIN(kp->size, 20), (char *) kp->data,
405
switch (dbp->get(dbp, kp, dp, flags)) {
409
err("line %lu: getdata: %s", lineno, strerror(errno));
412
err("line %lu: getdata failed, no such key", lineno);
422
switch (dbp->put(dbp, kp, dp, flags)) {
426
err("line %lu: put: %s", lineno, strerror(errno));
429
(void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
439
switch (dbp->del(dbp, kp, flags)) {
443
err("line %lu: rem: %s", lineno, strerror(errno));
446
#define NOSUCHKEY "rem failed, no such key\n"
447
if (ofd != STDOUT_FILENO)
448
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
449
else if (flags != R_CURSOR)
450
(void)fprintf(stderr, "%lu: %.*s: %s",
451
lineno, (int) MIN(kp->size, 20), (char *) kp->data,
454
(void)fprintf(stderr,
455
"%lu: rem of cursor failed\n", lineno);
465
switch (dbp->sync(dbp, flags)) {
469
err("line %lu: synk: %s", lineno, strerror(errno));
481
switch (dbp->seq(dbp, kp, &data, flags)) {
483
(void)write(ofd, data.data, data.size);
484
if (ofd == STDOUT_FILENO)
485
(void)write(ofd, "\n", 1);
488
err("line %lu: seq: %s", lineno, strerror(errno));
491
#define NOSUCHKEY "seq failed, no such key\n"
492
if (ofd != STDOUT_FILENO)
493
(void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
494
else if (flags == R_CURSOR)
495
(void)fprintf(stderr, "%lu: %.*s: %s",
496
lineno, (int) MIN(kp->size, 20), (char *) kp->data,
499
(void)fprintf(stderr,
500
"%lu: seq (%s) failed\n", lineno, sflags(flags));
521
for (;; lflags = nflags)
522
switch (dbp->seq(dbp, &key, &data, lflags)) {
524
(void)write(ofd, data.data, data.size);
525
if (ofd == STDOUT_FILENO)
526
(void)write(ofd, "\n", 1);
531
err("line %lu: (dump) seq: %s",
532
lineno, strerror(errno));
544
for (; isspace((int) *s); ++s);
545
if (*s == '\n' || *s == '\0')
547
if ((p = strchr(s, '\n')) != NULL)
549
if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
550
if (!strcmp(s, "R_FIRST")) return (R_FIRST);
551
if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
552
if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
553
if (!strcmp(s, "R_LAST")) return (R_LAST);
554
if (!strcmp(s, "R_NEXT")) return (R_NEXT);
555
if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
556
if (!strcmp(s, "R_PREV")) return (R_PREV);
557
if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
559
err("line %lu: %s: unknown flag", lineno, s);
568
case R_CURSOR: return ("R_CURSOR");
569
case R_FIRST: return ("R_FIRST");
570
case R_IAFTER: return ("R_IAFTER");
571
case R_IBEFORE: return ("R_IBEFORE");
572
case R_LAST: return ("R_LAST");
573
case R_NEXT: return ("R_NEXT");
574
case R_NOOVERWRITE: return ("R_NOOVERWRITE");
575
case R_PREV: return ("R_PREV");
576
case R_SETCURSOR: return ("R_SETCURSOR");
586
if (!strcmp(s, "btree"))
588
if (!strcmp(s, "hash"))
590
if (!strcmp(s, "recno"))
592
err("%s: unknown type (use btree, hash or recno)", s);
606
if ((eq = strchr(s, '=')) == NULL)
607
err("%s: illegal structure set statement", s);
609
if (!isdigit((int) *eq))
610
err("%s: structure set statement must be a number", s);
614
if (!strcmp("flags", s)) {
618
if (!strcmp("cachesize", s)) {
619
ib.cachesize = atoi(eq);
622
if (!strcmp("maxkeypage", s)) {
623
ib.maxkeypage = atoi(eq);
626
if (!strcmp("minkeypage", s)) {
627
ib.minkeypage = atoi(eq);
630
if (!strcmp("lorder", s)) {
631
ib.lorder = atoi(eq);
634
if (!strcmp("psize", s)) {
640
if (!strcmp("bsize", s)) {
644
if (!strcmp("ffactor", s)) {
645
ih.ffactor = atoi(eq);
648
if (!strcmp("nelem", s)) {
652
if (!strcmp("cachesize", s)) {
653
ih.cachesize = atoi(eq);
656
if (!strcmp("lorder", s)) {
657
ih.lorder = atoi(eq);
662
if (!strcmp("flags", s)) {
666
if (!strcmp("cachesize", s)) {
667
rh.cachesize = atoi(eq);
670
if (!strcmp("lorder", s)) {
671
rh.lorder = atoi(eq);
674
if (!strcmp("reclen", s)) {
675
rh.reclen = atoi(eq);
678
if (!strcmp("bval", s)) {
682
if (!strcmp("psize", s)) {
688
err("%s: unknown structure value", s);
702
for (; isspace((int) *name); ++name);
703
if ((np = strchr(name, '\n')) != NULL)
705
if ((fd = open(name, O_RDONLY, 0)) < 0 ||
707
err("%s: %s\n", name, strerror(errno));
709
if (sb.st_size > (off_t)SIZE_T_MAX)
710
err("%s: %s\n", name, strerror(E2BIG));
712
if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
713
err("%s", strerror(errno));
714
(void)read(fd, p, (int)sb.st_size);
727
if ((p = (void *)malloc(len)) == NULL)
728
err("%s", strerror(errno));
729
memmove(p, text, len);
736
(void)fprintf(stderr,
737
"usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
744
err(const char *fmt, ...)
748
(void)fprintf(stderr, "dbtest: ");
749
(void)vfprintf(stderr, fmt, ap);
751
(void)fprintf(stderr, "\n");