00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef lint
00011 static char copyright[] =
00012 "Copyright (c) 1996-2004\nSleepycat Software Inc. All rights reserved.\n";
00013 #endif
00014
00015 #include <sys/types.h>
00016
00017 #include <ctype.h>
00018 #include <errno.h>
00019 #include <fcntl.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023
00024 #include <db.h>
00025
00026
00027 typedef struct hashhdr185 {
00028 int magic;
00029 int version;
00030 u_int32_t lorder;
00031 int bsize;
00032 int bshift;
00033 int dsize;
00034 int ssize;
00035 int sshift;
00036 int ovfl_point;
00037
00038 int last_freed;
00039 int max_bucket;
00040 int high_mask;
00041 int low_mask;
00042
00043 int ffactor;
00044 int nkeys;
00045 } HASHHDR185;
00046 typedef struct htab185 {
00047 HASHHDR185 hdr;
00048 } HTAB185;
00049
00050
00051 typedef struct hashhdr186 {
00052 int32_t magic;
00053 int32_t version;
00054 int32_t lorder;
00055 int32_t bsize;
00056 int32_t bshift;
00057 int32_t ovfl_point;
00058 int32_t last_freed;
00059 int32_t max_bucket;
00060 int32_t high_mask;
00061 int32_t low_mask;
00062 int32_t ffactor;
00063 int32_t nkeys;
00064 int32_t hdrpages;
00065 int32_t h_charkey;
00066 #define NCACHED 32
00067 int32_t spares[NCACHED];
00068
00069 u_int16_t bitmaps[NCACHED];
00070 } HASHHDR186;
00071 typedef struct htab186 {
00072 void *unused[2];
00073 HASHHDR186 hdr;
00074 } HTAB186;
00075
00076 typedef struct _epgno {
00077 u_int32_t pgno;
00078 u_int16_t index;
00079 } EPGNO;
00080
00081 typedef struct _epg {
00082 void *page;
00083 u_int16_t index;
00084 } EPG;
00085
00086 typedef struct _cursor {
00087 EPGNO pg;
00088 DBT key;
00089 u_int32_t rcursor;
00090
00091 #define CURS_ACQUIRE 0x01
00092 #define CURS_AFTER 0x02
00093 #define CURS_BEFORE 0x04
00094 #define CURS_INIT 0x08
00095 u_int8_t flags;
00096 } CURSOR;
00097
00098
00099 typedef struct _btree {
00100 void *bt_mp;
00101
00102 void *bt_dbp;
00103
00104 EPG bt_cur;
00105 void *bt_pinned;
00106
00107 CURSOR bt_cursor;
00108
00109 EPGNO bt_stack[50];
00110 EPGNO *bt_sp;
00111
00112 DBT bt_rkey;
00113 DBT bt_rdata;
00114
00115 int bt_fd;
00116
00117 u_int32_t bt_free;
00118 u_int32_t bt_psize;
00119 u_int16_t bt_ovflsize;
00120 int bt_lorder;
00121
00122 enum { NOT, BACK, FORWARD } bt_order;
00123 EPGNO bt_last;
00124
00125
00126 int (*bt_cmp) __P((DBT *, DBT *));
00127
00128 size_t (*bt_pfx) __P((DBT *, DBT *));
00129
00130 int (*bt_irec) __P((struct _btree *, u_int32_t));
00131
00132 FILE *bt_rfp;
00133 int bt_rfd;
00134
00135 void *bt_cmap;
00136 void *bt_smap;
00137 void *bt_emap;
00138 size_t bt_msize;
00139
00140 u_int32_t bt_nrecs;
00141 size_t bt_reclen;
00142 u_char bt_bval;
00143
00144
00145
00146
00147
00148 #define B_INMEM 0x00001
00149 #define B_METADIRTY 0x00002
00150 #define B_MODIFIED 0x00004
00151 #define B_NEEDSWAP 0x00008
00152 #define B_RDONLY 0x00010
00153
00154 #define B_NODUPS 0x00020
00155 #define R_RECNO 0x00080
00156
00157 #define R_CLOSEFP 0x00040
00158 #define R_EOF 0x00100
00159 #define R_FIXLEN 0x00200
00160 #define R_MEMMAPPED 0x00400
00161 #define R_INMEM 0x00800
00162 #define R_MODIFIED 0x01000
00163 #define R_RDONLY 0x02000
00164
00165 #define B_DB_LOCK 0x04000
00166 #define B_DB_SHMEM 0x08000
00167 #define B_DB_TXN 0x10000
00168 u_int32_t flags;
00169 } BTREE;
00170
00171 void db_btree __P((DB *, int));
00172 void db_hash __P((DB *, int));
00173 void dbt_dump __P((DBT *));
00174 void dbt_print __P((DBT *));
00175 int main __P((int, char *[]));
00176 int usage __P((void));
00177
00178 int
00179 main(argc, argv)
00180 int argc;
00181 char *argv[];
00182 {
00183 extern char *optarg;
00184 extern int optind;
00185 DB *dbp;
00186 DBT key, data;
00187 int ch, pflag, rval;
00188
00189 pflag = 0;
00190 while ((ch = getopt(argc, argv, "f:p")) != EOF)
00191 switch (ch) {
00192 case 'f':
00193 if (freopen(optarg, "w", stdout) == NULL) {
00194 fprintf(stderr, "db_dump185: %s: %s\n",
00195 optarg, strerror(errno));
00196 return (EXIT_FAILURE);
00197 }
00198 break;
00199 case 'p':
00200 pflag = 1;
00201 break;
00202 case '?':
00203 default:
00204 return (usage());
00205 }
00206 argc -= optind;
00207 argv += optind;
00208
00209 if (argc != 1)
00210 return (usage());
00211
00212 if ((dbp = dbopen(argv[0], O_RDONLY, 0, DB_BTREE, NULL)) == NULL) {
00213 if ((dbp =
00214 dbopen(argv[0], O_RDONLY, 0, DB_HASH, NULL)) == NULL) {
00215 fprintf(stderr,
00216 "db_dump185: %s: %s\n", argv[0], strerror(errno));
00217 return (EXIT_FAILURE);
00218 }
00219 db_hash(dbp, pflag);
00220 } else
00221 db_btree(dbp, pflag);
00222
00223
00224
00225
00226
00227
00228 if (pflag)
00229 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
00230 dbt_print(&key);
00231 dbt_print(&data);
00232 }
00233 else
00234 while (!(rval = dbp->seq(dbp, &key, &data, R_NEXT))) {
00235 dbt_dump(&key);
00236 dbt_dump(&data);
00237 }
00238
00239 if (rval == -1) {
00240 fprintf(stderr, "db_dump185: seq: %s\n", strerror(errno));
00241 return (EXIT_FAILURE);
00242 }
00243 return (EXIT_SUCCESS);
00244 }
00245
00246
00247
00248
00249
00250 void
00251 db_hash(dbp, pflag)
00252 DB *dbp;
00253 int pflag;
00254 {
00255 HTAB185 *hash185p;
00256 HTAB186 *hash186p;
00257
00258 printf("format=%s\n", pflag ? "print" : "bytevalue");
00259 printf("type=hash\n");
00260
00261
00262 hash185p = dbp->internal;
00263 if (hash185p->hdr.version > 2) {
00264 hash186p = dbp->internal;
00265 printf("h_ffactor=%lu\n", (u_long)hash186p->hdr.ffactor);
00266 if (hash186p->hdr.lorder != 0)
00267 printf("db_lorder=%lu\n", (u_long)hash186p->hdr.lorder);
00268 printf("db_pagesize=%lu\n", (u_long)hash186p->hdr.bsize);
00269 } else {
00270 printf("h_ffactor=%lu\n", (u_long)hash185p->hdr.ffactor);
00271 if (hash185p->hdr.lorder != 0)
00272 printf("db_lorder=%lu\n", (u_long)hash185p->hdr.lorder);
00273 printf("db_pagesize=%lu\n", (u_long)hash185p->hdr.bsize);
00274 }
00275 printf("HEADER=END\n");
00276 }
00277
00278
00279
00280
00281
00282 void
00283 db_btree(dbp, pflag)
00284 DB *dbp;
00285 int pflag;
00286 {
00287 BTREE *btp;
00288
00289 btp = dbp->internal;
00290
00291 printf("format=%s\n", pflag ? "print" : "bytevalue");
00292 printf("type=btree\n");
00293 #ifdef NOT_AVAILABLE_IN_185
00294 printf("bt_minkey=%lu\n", (u_long)XXX);
00295 printf("bt_maxkey=%lu\n", (u_long)XXX);
00296 #endif
00297 if (btp->bt_lorder != 0)
00298 printf("db_lorder=%lu\n", (u_long)btp->bt_lorder);
00299 printf("db_pagesize=%lu\n", (u_long)btp->bt_psize);
00300 if (!(btp->flags & B_NODUPS))
00301 printf("duplicates=1\n");
00302 printf("HEADER=END\n");
00303 }
00304
00305 static char hex[] = "0123456789abcdef";
00306
00307
00308
00309
00310
00311 void
00312 dbt_dump(dbtp)
00313 DBT *dbtp;
00314 {
00315 size_t len;
00316 u_int8_t *p;
00317
00318 for (len = dbtp->size, p = dbtp->data; len--; ++p)
00319 (void)printf("%c%c",
00320 hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
00321 printf("\n");
00322 }
00323
00324
00325
00326
00327
00328 void
00329 dbt_print(dbtp)
00330 DBT *dbtp;
00331 {
00332 size_t len;
00333 u_int8_t *p;
00334
00335 for (len = dbtp->size, p = dbtp->data; len--; ++p)
00336 if (isprint((int)*p)) {
00337 if (*p == '\\')
00338 (void)printf("\\");
00339 (void)printf("%c", *p);
00340 } else
00341 (void)printf("\\%c%c",
00342 hex[(*p & 0xf0) >> 4], hex[*p & 0x0f]);
00343 printf("\n");
00344 }
00345
00346
00347
00348
00349
00350 int
00351 usage()
00352 {
00353 (void)fprintf(stderr, "usage: db_dump185 [-p] [-f file] db_file\n");
00354 return (EXIT_FAILURE);
00355 }