diff options
author | Keith Bostic <keith.bostic@wiredtiger.com> | 2011-07-11 10:23:19 -0400 |
---|---|---|
committer | Keith Bostic <keith.bostic@wiredtiger.com> | 2011-07-11 10:23:19 -0400 |
commit | ff902aa1d2dc1ea403aabe21fc441c0be79e077e (patch) | |
tree | 485668e1a75a5fc1d3754fe1c15dfd4ec3f61c69 | |
parent | db4df7f50398412c54d68abe40b8c5c056ae6156 (diff) | |
download | mongo-ff902aa1d2dc1ea403aabe21fc441c0be79e077e.tar.gz |
Give the utilities a common name-generation routine, and change the
dumpfile, dump and verify commands to support both a table and file
prefix.
--HG--
extra : rebase_source : 5a7ffe11814f3d62fff54ca332c3a4f1549bf7ba
-rw-r--r-- | src/utilities/util.h | 27 | ||||
-rw-r--r-- | src/utilities/util_dump.c | 25 | ||||
-rw-r--r-- | src/utilities/util_dumpfile.c | 21 | ||||
-rw-r--r-- | src/utilities/util_load.c | 32 | ||||
-rw-r--r-- | src/utilities/util_main.c | 73 | ||||
-rw-r--r-- | src/utilities/util_salvage.c | 21 | ||||
-rw-r--r-- | src/utilities/util_stat.c | 28 | ||||
-rw-r--r-- | src/utilities/util_verify.c | 24 |
8 files changed, 135 insertions, 116 deletions
diff --git a/src/utilities/util.h b/src/utilities/util.h index 1ebeefa7c70..461ff0f580e 100644 --- a/src/utilities/util.h +++ b/src/utilities/util.h @@ -16,17 +16,22 @@ */ #define WT_UNUSED(var) (void)(var) -int util_dump(int, char *[]); -int util_dumpfile(int, char *[]); -int util_load(int, char *[]); -int util_printlog(int, char *[]); -int util_salvage(int, char *[]); -int util_stat(int, char *[]); -int util_verify(int, char *[]); +#define UTIL_FILE_OK 0x01 /* file: prefix OK */ +#define UTIL_TABLE_OK 0x02 /* table: prefix OK */ +#define UTIL_STAT_OK 0x04 /* stat: prefix OK */ -extern const char *home; /* Home directory */ -extern const char *progname; /* Program name */ -extern const char *usage_prefix; /* Global arguments */ -extern int verbose; /* Verbose flag */ +extern const char *home; /* Home directory */ +extern const char *progname; /* Program name */ +extern const char *usage_prefix; /* Global arguments */ +extern int verbose; /* Verbose flag */ extern WT_EVENT_HANDLER *verbose_handler; + +int util_dump(int, char *[]); +int util_dumpfile(int, char *[]); +int util_load(int, char *[]); +char *util_name(const char *, const char *, u_int); +int util_printlog(int, char *[]); +int util_salvage(int, char *[]); +int util_stat(int, char *[]); +int util_verify(int, char *[]); diff --git a/src/utilities/util_dump.c b/src/utilities/util_dump.c index a3131429ce2..d2b3a6b2997 100644 --- a/src/utilities/util_dump.c +++ b/src/utilities/util_dump.c @@ -17,13 +17,12 @@ util_dump(int argc, char *argv[]) WT_CURSOR *cursor; WT_ITEM key, value; WT_SESSION *session; - size_t len; char cursor_config[100]; int ch, printable, ret, tret; - char *tablename; + char *name; conn = NULL; - tablename = NULL; + name = NULL; printable = 0; while ((ch = getopt(argc, argv, "f:p")) != EOF) @@ -48,13 +47,9 @@ util_dump(int argc, char *argv[]) /* The remaining argument is the table name. */ if (argc != 1) return (usage()); - - len = sizeof("table:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name( + *argv, "table", UTIL_FILE_OK | UTIL_TABLE_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "table:%s", *argv); if ((ret = wiredtiger_open(home, verbose ? verbose_handler : NULL, NULL, &conn)) != 0 || @@ -65,9 +60,9 @@ util_dump(int argc, char *argv[]) "dump,%s", printable ? "printable" : "raw"); if ((ret = session->open_cursor( - session, tablename, NULL, cursor_config, &cursor)) != 0) { + session, name, NULL, cursor_config, &cursor)) != 0) { fprintf(stderr, "%s: cursor open(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } @@ -89,7 +84,7 @@ util_dump(int argc, char *argv[]) ret = 0; else { fprintf(stderr, "%s: cursor get(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } @@ -99,8 +94,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } @@ -110,7 +105,7 @@ usage(void) { (void)fprintf(stderr, "usage: %s%s " - "dump [-p] [-f output-file] file\n", + "dump [-p] [-f output-file] table\n", progname, usage_prefix); return (EXIT_FAILURE); } diff --git a/src/utilities/util_dumpfile.c b/src/utilities/util_dumpfile.c index fbe9dde4472..d7c271bebb1 100644 --- a/src/utilities/util_dumpfile.c +++ b/src/utilities/util_dumpfile.c @@ -15,13 +15,11 @@ util_dumpfile(int argc, char *argv[]) { WT_CONNECTION *conn; WT_SESSION *session; - size_t len; int ch, ret, tret; - char *tablename; + char *name; conn = NULL; - tablename = NULL; - + name = NULL; while ((ch = getopt(argc, argv, "f:")) != EOF) switch (ch) { case 'f': /* output file */ @@ -41,21 +39,16 @@ util_dumpfile(int argc, char *argv[]) /* The remaining argument is the file name. */ if (argc != 1) return (usage()); - - len = sizeof("table:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name(*argv, "file", UTIL_FILE_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "table:%s", *argv); if ((ret = wiredtiger_open(home, NULL, NULL, &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) goto err; - if ((ret = session->dumpfile(session, tablename, NULL)) != 0) { + if ((ret = session->dumpfile(session, name, NULL)) != 0) { fprintf(stderr, "%s: dumpfile(%s): %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } if (verbose) @@ -67,8 +60,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/utilities/util_load.c b/src/utilities/util_load.c index b8c4972482e..f958c8be00d 100644 --- a/src/utilities/util_load.c +++ b/src/utilities/util_load.c @@ -27,16 +27,15 @@ util_load(int argc, char *argv[]) WT_CURSOR *cursor; WT_SESSION *session; struct record_t record; - size_t len; uint64_t insert_count; int ch, debug, eof, ret, printable, tret; const char *table_config; char cursor_config[100]; - char *tablename; + char *name; conn = NULL; table_config = NULL; - tablename = NULL; + name = NULL; debug = printable = 0; while ((ch = getopt(argc, argv, "c:df:T")) != EOF) @@ -77,32 +76,27 @@ util_load(int argc, char *argv[]) /* The remaining argument is the table name. */ if (argc != 1) return (usage()); - - len = sizeof("table:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name(*argv, "table", UTIL_TABLE_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "table:%s", *argv); if ((ret = wiredtiger_open(home, verbose ? verbose_handler : NULL, NULL, &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) goto err; - if ((ret = session->drop(session, tablename, "force")) != 0) + if ((ret = session->drop(session, name, "force")) != 0) goto err; - if ((ret = session->create(session, tablename, table_config)) != 0) + if ((ret = session->create(session, name, table_config)) != 0) goto err; snprintf(cursor_config, sizeof(cursor_config), "dump%s%s", printable ? ",printable" : ",raw", debug ? ",debug" : ""); if ((ret = session->open_cursor( - session, tablename, NULL, cursor_config, &cursor)) != 0) { + session, name, NULL, cursor_config, &cursor)) != 0) { fprintf(stderr, "%s: cursor open(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } @@ -113,7 +107,7 @@ util_load(int argc, char *argv[]) for (eof = 0; (ret = bulk_read(&record, &eof)) == 0 && !eof;) { /* Report on progress every 100 inserts. */ if (verbose && ++insert_count % 100 == 0) { - printf("\r\t%s: %" PRIu64, tablename, insert_count); + printf("\r\t%s: %" PRIu64, name, insert_count); fflush(stdout); } @@ -121,13 +115,13 @@ util_load(int argc, char *argv[]) cursor->set_value(cursor, &record.value); if ((ret = cursor->insert(cursor)) != 0) { fprintf(stderr, "%s: cursor insert(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } } if (verbose) - printf("\r\t%s: %" PRIu64 "\n", tablename, insert_count); + printf("\r\t%s: %" PRIu64 "\n", name, insert_count); if (0) { err: ret = 1; @@ -135,8 +129,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } @@ -205,7 +199,7 @@ usage(void) { (void)fprintf(stderr, "usage: %s%s " - "load [-dT] [-c configuration] [-f input-file] file\n", + "load [-dT] [-c configuration] [-f input-file] table\n", progname, usage_prefix); return (EXIT_FAILURE); } diff --git a/src/utilities/util_main.c b/src/utilities/util_main.c index 4506bb0f871..b15c7a9d796 100644 --- a/src/utilities/util_main.c +++ b/src/utilities/util_main.c @@ -13,12 +13,13 @@ const char *progname; /* Program name */ const char *usage_prefix = "[-Vv] [-h home]"; /* Global arguments */ int verbose; /* Verbose flag */ +static const char *command; /* Command name */ + static int usage(void); int main(int argc, char *argv[]) { - char *cmd; int ch, major_v, minor_v; /* Get the program name. */ @@ -26,6 +27,7 @@ main(int argc, char *argv[]) progname = argv[0]; else ++progname; + command = ""; /* Check the version against the library build. */ (void)wiredtiger_version(&major_v, & minor_v, NULL); @@ -66,35 +68,35 @@ main(int argc, char *argv[]) /* The next argument is the command name. */ if (argc < 1) return (usage()); - cmd = argv[0]; + command = argv[0]; /* Reset getopt. */ optreset = 1; optind = 1; - switch (cmd[0]) { + switch (command[0]) { case 'd': - if (strcmp(cmd, "dump") == 0) + if (strcmp(command, "dump") == 0) return (util_dump(argc, argv)); - if (strcmp(cmd, "dumpfile") == 0) + if (strcmp(command, "dumpfile") == 0) return (util_dumpfile(argc, argv)); break; case 'l': - if (strcmp(cmd, "load") == 0) + if (strcmp(command, "load") == 0) return (util_load(argc, argv)); break; case 'p': - if (strcmp(cmd, "printlog") == 0) + if (strcmp(command, "printlog") == 0) return (util_printlog(argc, argv)); break; case 's': - if (strcmp(cmd, "salvage") == 0) + if (strcmp(command, "salvage") == 0) return (util_salvage(argc, argv)); - if (strcmp(cmd, "stat") == 0) + if (strcmp(command, "stat") == 0) return (util_stat(argc, argv)); break; case 'v': - if (strcmp(cmd, "verify") == 0) + if (strcmp(command, "verify") == 0) return (util_verify(argc, argv)); break; default: @@ -127,3 +129,54 @@ usage(void) return (EXIT_FAILURE); } + +/* + * util_name -- + * Build a name. + */ +char * +util_name(const char *s, const char *type, u_int flags) +{ + size_t len; + int copy; + char *name; + + copy = 0; + if (strncmp(s, "file:", strlen("file:")) == 0) { + if (!(flags & UTIL_FILE_OK)) { + fprintf(stderr, + "%s: %s: \"file\" type not supported\n", + progname, command); + return (NULL); + } + copy = 1; + } else if (strncmp(s, "stat:", strlen("stat:")) == 0) { + if (!(flags & UTIL_STAT_OK)) { + fprintf(stderr, + "%s: %s: \"stat\" type not supported\n", + progname, command); + return (NULL); + } + copy = 1; + } else if (strncmp(s, "table:", strlen("table:")) == 0) { + if (!(flags & UTIL_TABLE_OK)) { + fprintf(stderr, + "%s: %s: \"table\" type not supported\n", + progname, command); + return (NULL); + } + copy = 1; + } + + len = 20 + strlen(s); + if ((name = calloc(len, 1)) == NULL) { + fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + return (NULL); + } + + if (copy) + strcpy(name, s); + else + snprintf(name, len, "%s:%s", type, s); + return (name); +} diff --git a/src/utilities/util_salvage.c b/src/utilities/util_salvage.c index a74b9401881..ee41d7ea1c8 100644 --- a/src/utilities/util_salvage.c +++ b/src/utilities/util_salvage.c @@ -15,13 +15,11 @@ util_salvage(int argc, char *argv[]) { WT_CONNECTION *conn; WT_SESSION *session; - size_t len; int ch, ret, tret; - char *tablename; + char *name; conn = NULL; - tablename = NULL; - + name = NULL; while ((ch = getopt(argc, argv, "")) != EOF) switch (ch) { case '?': @@ -34,22 +32,17 @@ util_salvage(int argc, char *argv[]) /* The remaining argument is the file name. */ if (argc != 1) return (usage()); - - len = sizeof("table:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name(*argv, "file", UTIL_FILE_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "table:%s", *argv); if ((ret = wiredtiger_open(home, verbose ? verbose_handler : NULL, NULL, &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) goto err; - if ((ret = session->salvage(session, tablename, NULL)) != 0) { + if ((ret = session->salvage(session, name, NULL)) != 0) { fprintf(stderr, "%s: salvage(%s): %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } if (verbose) @@ -61,8 +54,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/utilities/util_stat.c b/src/utilities/util_stat.c index 18ebd44efe4..1f9fd3e2f59 100644 --- a/src/utilities/util_stat.c +++ b/src/utilities/util_stat.c @@ -17,15 +17,12 @@ util_stat(int argc, char *argv[]) WT_CURSOR *cursor; WT_ITEM key, value; WT_SESSION *session; - size_t len; int ch, debug, ret, tret; - char cursor_config[100]; - char *tablename; + char cursor_config[100], *name; conn = NULL; - tablename = NULL; + name = NULL; debug = 0; - while ((ch = getopt(argc, argv, "d")) != EOF) switch (ch) { case 'd': @@ -38,16 +35,11 @@ util_stat(int argc, char *argv[]) argc -= optind; argv += optind; - /* The remaining argument is the table name. */ + /* The remaining argument is the stat name. */ if (argc != 1) return (usage()); - - len = sizeof("stat:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name(*argv, "stat", UTIL_STAT_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "stat:%s", *argv); if ((ret = wiredtiger_open(home, verbose ? verbose_handler : NULL, NULL, &conn)) != 0 || @@ -58,10 +50,10 @@ util_stat(int argc, char *argv[]) debug ? ",debug" : ""); - if ((ret = session->open_cursor(session, tablename, NULL, - cursor_config, &cursor)) != 0) { + if ((ret = session->open_cursor( + session, name, NULL, cursor_config, &cursor)) != 0) { fprintf(stderr, "%s: cursor open(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } @@ -82,7 +74,7 @@ util_stat(int argc, char *argv[]) ret = 0; else { fprintf(stderr, "%s: cursor get(%s) failed: %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } @@ -92,8 +84,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/utilities/util_verify.c b/src/utilities/util_verify.c index e2356538768..488b19ebdeb 100644 --- a/src/utilities/util_verify.c +++ b/src/utilities/util_verify.c @@ -15,13 +15,11 @@ util_verify(int argc, char *argv[]) { WT_CONNECTION *conn; WT_SESSION *session; - size_t len; int ch, ret, tret; - char *tablename; + char *name; conn = NULL; - tablename = NULL; - + name = NULL; while ((ch = getopt(argc, argv, "")) != EOF) switch (ch) { case '?': @@ -31,25 +29,21 @@ util_verify(int argc, char *argv[]) argc -= optind; argv += optind; - /* The remaining argument is the file name. */ + /* The remaining argument is the table name. */ if (argc != 1) return (usage()); - - len = sizeof("table:") + strlen(*argv); - if ((tablename = calloc(len, 1)) == NULL) { - fprintf(stderr, "%s: %s\n", progname, strerror(errno)); + if ((name = util_name( + *argv, "table", UTIL_FILE_OK | UTIL_TABLE_OK)) == NULL) return (EXIT_FAILURE); - } - snprintf(tablename, len, "table:%s", *argv); if ((ret = wiredtiger_open(home, verbose ? verbose_handler : NULL, NULL, &conn)) != 0 || (ret = conn->open_session(conn, NULL, NULL, &session)) != 0) goto err; - if ((ret = session->verify(session, tablename, NULL)) != 0) { + if ((ret = session->verify(session, name, NULL)) != 0) { fprintf(stderr, "%s: verify(%s): %s\n", - progname, tablename, wiredtiger_strerror(ret)); + progname, name, wiredtiger_strerror(ret)); goto err; } if (verbose) @@ -61,8 +55,8 @@ err: ret = 1; if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0) ret = tret; - if (tablename != NULL) - free(tablename); + if (name != NULL) + free(name); return ((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } |