📄 db_load.c
字号:
if (strcmp(name, "keys") == 0) { if (strcmp(value, "1") == 0) *keysp = 1; else if (strcmp(value, "0") == 0) *keysp = 0; else { badnum(dbenv); return (1); } continue; }#ifdef notyet NUMBER(name, value, "bt_maxkey", set_bt_maxkey);#endif NUMBER(name, value, "bt_minkey", set_bt_minkey); NUMBER(name, value, "db_lorder", set_lorder); NUMBER(name, value, "db_pagesize", set_pagesize); FLAG(name, value, "chksum", DB_CHKSUM_SHA1); FLAG(name, value, "duplicates", DB_DUP); FLAG(name, value, "dupsort", DB_DUPSORT); NUMBER(name, value, "h_ffactor", set_h_ffactor); NUMBER(name, value, "h_nelem", set_h_nelem); NUMBER(name, value, "re_len", set_re_len); STRING(name, value, "re_pad", set_re_pad); FLAG(name, value, "recnum", DB_RECNUM); FLAG(name, value, "renumber", DB_RENUMBER); dbp->errx(dbp, "unknown command-line configuration keyword \"%s\"", name); return (1); } return (0);nameerr: dbp->err(dbp, ret, "%s: %s=%s", G(progname), name, value); return (1);}/* * rheader -- * Read the header message. */intrheader(dbenv, dbp, dbtypep, subdbp, checkprintp, keysp) DB_ENV *dbenv; DB *dbp; DBTYPE *dbtypep; char **subdbp; int *checkprintp, *keysp;{ long val; int ch, first, hdr, linelen, buflen, ret, start; char *buf, *name, *p, *value; *dbtypep = DB_UNKNOWN; *checkprintp = 0; name = p = NULL; /* * We start with a smallish buffer; most headers are small. * We may need to realloc it for a large subdatabase name. */ buflen = 4096; if (G(hdrbuf) == NULL) { hdr = 0; if ((buf = (char *)malloc(buflen)) == NULL) {memerr: dbp->errx(dbp, "could not allocate buffer %d", buflen); return (1); } G(hdrbuf) = buf; G(origline) = G(lineno); } else { hdr = 1; buf = G(hdrbuf); G(lineno) = G(origline); } start = 0; for (first = 1;; first = 0) { ++G(lineno); /* Read a line, which may be of arbitrary length, into buf. */ linelen = 0; buf = &G(hdrbuf)[start]; if (hdr == 0) { for (;;) { if ((ch = getchar()) == EOF) { if (!first || ferror(stdin)) goto badfmt; G(endofile) = 1; break; } if (ch == '\n') break; buf[linelen++] = ch; /* If the buffer is too small, double it. */ if (linelen + start == buflen) { G(hdrbuf) = (char *)realloc(G(hdrbuf), buflen *= 2); if (G(hdrbuf) == NULL) goto memerr; buf = &G(hdrbuf)[start]; } } if (G(endofile) == 1) break; buf[linelen++] = '\0'; } else linelen = strlen(buf) + 1; start += linelen; if (name != NULL) { *p = '='; free(name); name = NULL; } /* If we don't see the expected information, it's an error. */ if ((name = strdup(buf)) == NULL) goto memerr; if ((p = strchr(name, '=')) == NULL) goto badfmt; *p++ = '\0'; value = p--; if (name[0] == '\0' || value[0] == '\0') goto badfmt; if (strcmp(name, "HEADER") == 0) break; if (strcmp(name, "VERSION") == 0) { /* * Version 1 didn't have a "VERSION" header line. We * only support versions 1, 2, and 3 of the dump format. */ G(version) = atoi(value); if (G(version) > 3) { dbp->errx(dbp, "line %lu: VERSION %d is unsupported", G(lineno), G(version)); goto err; } continue; } if (strcmp(name, "format") == 0) { if (strcmp(value, "bytevalue") == 0) { *checkprintp = 0; continue; } if (strcmp(value, "print") == 0) { *checkprintp = 1; continue; } goto badfmt; } if (strcmp(name, "type") == 0) { if (strcmp(value, "btree") == 0) { *dbtypep = DB_BTREE; continue; } if (strcmp(value, "hash") == 0) { *dbtypep = DB_HASH; continue; } if (strcmp(value, "recno") == 0) { *dbtypep = DB_RECNO; continue; } if (strcmp(value, "queue") == 0) { *dbtypep = DB_QUEUE; continue; } dbp->errx(dbp, "line %lu: unknown type", G(lineno)); goto err; } if (strcmp(name, "database") == 0 || strcmp(name, "subdatabase") == 0) { if ((ret = convprintable(dbenv, value, subdbp)) != 0) { dbp->err(dbp, ret, "error reading db name"); goto err; } continue; } if (strcmp(name, "keys") == 0) { if (strcmp(value, "1") == 0) *keysp = 1; else if (strcmp(value, "0") == 0) *keysp = 0; else { badnum(dbenv); goto err; } continue; }#ifdef notyet NUMBER(name, value, "bt_maxkey", set_bt_maxkey);#endif NUMBER(name, value, "bt_minkey", set_bt_minkey); NUMBER(name, value, "db_lorder", set_lorder); NUMBER(name, value, "db_pagesize", set_pagesize); NUMBER(name, value, "extentsize", set_q_extentsize); FLAG(name, value, "chksum", DB_CHKSUM_SHA1); FLAG(name, value, "duplicates", DB_DUP); FLAG(name, value, "dupsort", DB_DUPSORT); NUMBER(name, value, "h_ffactor", set_h_ffactor); NUMBER(name, value, "h_nelem", set_h_nelem); NUMBER(name, value, "re_len", set_re_len); STRING(name, value, "re_pad", set_re_pad); FLAG(name, value, "recnum", DB_RECNUM); FLAG(name, value, "renumber", DB_RENUMBER); dbp->errx(dbp, "unknown input-file header configuration keyword \"%s\"", name); goto err; } ret = 0; if (0) {nameerr: dbp->err(dbp, ret, "%s: %s=%s", G(progname), name, value); ret = 1; } if (0)err: ret = 1; if (0) {badfmt: dbp->errx(dbp, "line %lu: unexpected format", G(lineno)); ret = 1; } if (name != NULL) { *p = '='; free(name); } return (ret);}/* * convprintable -- * Convert a printable-encoded string into a newly allocated string. * * In an ideal world, this would probably share code with dbt_rprint, but * that's set up to read character-by-character (to avoid large memory * allocations that aren't likely to be a problem here), and this has fewer * special cases to deal with. * * Note that despite the printable encoding, the char * interface to this * function (which is, not coincidentally, also used for database naming) * means that outstr cannot contain any nuls. */intconvprintable(dbenv, instr, outstrp) DB_ENV *dbenv; char *instr, **outstrp;{ char c, *outstr; int e1, e2; /* * Just malloc a string big enough for the whole input string; * the output string will be smaller (or of equal length). */ if ((outstr = (char *)malloc(strlen(instr))) == NULL) return (ENOMEM); *outstrp = outstr; e1 = e2 = 0; for ( ; *instr != '\0'; instr++) if (*instr == '\\') { if (*++instr == '\\') { *outstr++ = '\\'; continue; } c = digitize(dbenv, *instr, &e1) << 4; c |= digitize(dbenv, *++instr, &e2); if (e1 || e2) { badend(dbenv); return (EINVAL); } *outstr++ = c; } else *outstr++ = *instr; *outstr = '\0'; return (0);}/* * dbt_rprint -- * Read a printable line into a DBT structure. */intdbt_rprint(dbenv, dbtp) DB_ENV *dbenv; DBT *dbtp;{ u_int32_t len; u_int8_t *p; int c1, c2, e, escape, first; char buf[32]; ++G(lineno); first = 1; e = escape = 0; for (p = dbtp->data, len = 0; (c1 = getchar()) != '\n';) { if (c1 == EOF) { if (len == 0) { G(endofile) = G(endodata) = 1; return (0); } badend(dbenv); return (1); } if (first) { first = 0; if (G(version) > 1) { if (c1 != ' ') { buf[0] = c1; if (fgets(buf + 1, sizeof(buf) - 1, stdin) == NULL || strcmp(buf, "DATA=END\n") != 0) { badend(dbenv); return (1); } G(endodata) = 1; return (0); } continue; } } if (escape) { if (c1 != '\\') { if ((c2 = getchar()) == EOF) { badend(dbenv); return (1); } c1 = digitize(dbenv, c1, &e) << 4 | digitize(dbenv, c2, &e); if (e) return (1); } escape = 0; } else if (c1 == '\\') { escape = 1; continue; } if (len >= dbtp->ulen - 10) { dbtp->ulen *= 2; if ((dbtp->data = (void *)realloc(dbtp->data, dbtp->ulen)) == NULL) { dbenv->err(dbenv, ENOMEM, NULL); return (1); } p = (u_int8_t *)dbtp->data + len; } ++len; *p++ = c1; } dbtp->size = len; return (0);}/* * dbt_rdump -- * Read a byte dump line into a DBT structure. */intdbt_rdump(dbenv, dbtp) DB_ENV *dbenv; DBT *dbtp;{ u_int32_t len; u_int8_t *p; int c1, c2, e, first; char buf[32]; ++G(lineno); first = 1; e = 0; for (p = dbtp->data, len = 0; (c1 = getchar()) != '\n';) { if (c1 == EOF) { if (len == 0) { G(endofile) = G(endodata) = 1; return (0); } badend(dbenv); return (1); } if (first) { first = 0; if (G(version) > 1) { if (c1 != ' ') { buf[0] = c1; if (fgets(buf + 1, sizeof(buf) - 1, stdin) == NULL || strcmp(buf, "DATA=END\n") != 0) { badend(dbenv); return (1); } G(endodata) = 1; return (0); } continue; } } if ((c2 = getchar()) == EOF) { badend(dbenv); return (1); } if (len >= dbtp->ulen - 10) { dbtp->ulen *= 2; if ((dbtp->data = (void *)realloc(dbtp->data, dbtp->ulen)) == NULL) { dbenv->err(dbenv, ENOMEM, NULL); return (1); } p = (u_int8_t *)dbtp->data + len; } ++len; *p++ = digitize(dbenv, c1, &e) << 4 | digitize(dbenv, c2, &e); if (e) return (1); } dbtp->size = len; return (0);}/* * dbt_rrecno -- * Read a record number dump line into a DBT structure. */intdbt_rrecno(dbenv, dbtp, ishex) DB_ENV *dbenv; DBT *dbtp; int ishex;{ char buf[32], *p, *q; ++G(lineno); if (fgets(buf, sizeof(buf), stdin) == NULL) { G(endofile) = G(endodata) = 1; return (0); } if (strcmp(buf, "DATA=END\n") == 0) { G(endodata) = 1; return (0); } if (buf[0] != ' ') goto bad; /* * If we're expecting a hex key, do an in-place conversion * of hex to straight ASCII before calling __db_getulong(). */ if (ishex) { for (p = q = buf + 1; *q != '\0' && *q != '\n';) { /* * 0-9 in hex are 0x30-0x39, so this is easy. * We should alternate between 3's and [0-9], and * if the [0-9] are something unexpected, * __db_getulong will fail, so we only need to catch * end-of-string conditions. */ if (*q++ != '3') goto bad; if (*q == '\n' || *q == '\0') goto bad; *p++ = *q++; } *p = '\0'; } if (__db_getulong(NULL, G(progname), buf + 1, 0, 0, (u_long *)dbtp->data)) {bad: badend(dbenv); return (1); } dbtp->size = sizeof(db_recno_t); return (0);}/* * digitize -- * Convert a character to an integer. */intdigitize(dbenv, c, errorp) DB_ENV *dbenv; int c, *errorp;{ switch (c) { /* Don't depend on ASCII ordering. */ case '0': return (0); case '1': return (1); case '2': return (2); case '3': return (3); case '4': return (4); case '5': return (5); case '6': return (6); case '7': return (7); case '8': return (8); case '9': return (9); case 'a': return (10); case 'b': return (11); case 'c': return (12); case 'd': return (13); case 'e': return (14); case 'f': return (15); } dbenv->errx(dbenv, "unexpected hexadecimal value"); *errorp = 1; return (0);}/* * badnum -- * Display the bad number message. */voidbadnum(dbenv) DB_ENV *dbenv;{ dbenv->errx(dbenv, "boolean name=value pairs require a value of 0 or 1");}/* * badend -- * Display the bad end to input message. */voidbadend(dbenv) DB_ENV *dbenv;{ dbenv->errx(dbenv, "unexpected end of input data or key/data pair");}/* * usage -- * Display the usage message. */intusage(){ (void)fprintf(stderr, "%s\n\t%s\n", "usage: db_load [-nTV] [-c name=value] [-f file]", "[-h home] [-P password] [-t btree | hash | recno | queue] db_file"); return (EXIT_FAILURE);}intversion_check(progname) const char *progname;{ int v_major, v_minor, v_patch; /* Make sure we're loaded with the right version of the DB library. */ (void)db_version(&v_major, &v_minor, &v_patch); if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) { fprintf(stderr, "%s: version %d.%d.%d doesn't match library version %d.%d.%d\n", progname, DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH, v_major, v_minor, v_patch); return (EXIT_FAILURE); } return (0);}intenv_create(dbenvp, ldg) DB_ENV **dbenvp; LDG *ldg;{ DB_ENV *dbenv; int ret; if ((ret = db_env_create(dbenvp, 0)) != 0) { fprintf(stderr, "%s: db_env_create: %s\n", ldg->progname, db_strerror(ret)); return (ret); } dbenv = *dbenvp; dbenv->set_errfile(dbenv, stderr); dbenv->set_errpfx(dbenv, ldg->progname); if (ldg->passwd != NULL && (ret = dbenv->set_encrypt(dbenv, ldg->passwd, DB_ENCRYPT_AES)) != 0) { dbenv->err(dbenv, ret, "set_passwd"); return (ret); } if ((ret = db_init(dbenv, ldg->home, ldg->cache, &ldg->private)) != 0) return (ret); dbenv->app_private = ldg; return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -