int util_salvage(WT_SESSION *session, int argc, char *argv[]) { WT_DECL_RET; int ch; const char *force; char *uri; force = NULL; uri = NULL; while ((ch = __wt_getopt(progname, argc, argv, "F")) != EOF) switch (ch) { case 'F': force = "force"; break; case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* The remaining argument is the file name. */ if (argc != 1) return (usage()); if ((uri = util_uri(session, *argv, "file")) == NULL) return (1); if ((ret = session->salvage(session, uri, force)) != 0) (void)util_err(session, ret, "session.salvage: %s", uri); else { /* * Verbose configures a progress counter, move to the next * line. */ if (verbose) printf("\n"); } free(uri); return (ret); }
int util_read(WT_SESSION *session, int argc, char *argv[]) { WT_CURSOR *cursor; WT_DECL_RET; uint64_t recno; int ch; bool rkey, rval; char *uri, *value; uri = NULL; while ((ch = __wt_getopt(progname, argc, argv, "")) != EOF) switch (ch) { case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* The remaining arguments are a uri followed by a list of keys. */ if (argc < 2) return (usage()); if ((uri = util_uri(session, *argv, "table")) == NULL) return (1); /* * Open the object; free allocated memory immediately to simplify * future error handling. */ if ((ret = session->open_cursor(session, uri, NULL, NULL, &cursor)) != 0) (void)util_err(session, ret, "%s: session.open_cursor", uri); free(uri); if (ret != 0) return (ret); /* * A simple search only makes sense if the key format is a string or a * record number, and the value format is a single string. */ if (strcmp(cursor->key_format, "r") != 0 && strcmp(cursor->key_format, "S") != 0) { fprintf(stderr, "%s: read command only possible when the key format is " "a record number or string\n", progname); return (1); } rkey = strcmp(cursor->key_format, "r") == 0; if (strcmp(cursor->value_format, "S") != 0) { fprintf(stderr, "%s: read command only possible when the value format is " "a string\n", progname); return (1); } /* * Run through the keys, returning non-zero on error or if any requested * key isn't found. */ for (rval = false; *++argv != NULL;) { if (rkey) { if (util_str2recno(session, *argv, &recno)) return (1); cursor->set_key(cursor, recno); } else cursor->set_key(cursor, *argv); switch (ret = cursor->search(cursor)) { case 0: if ((ret = cursor->get_value(cursor, &value)) != 0) return (util_cerr(cursor, "get_value", ret)); if (printf("%s\n", value) < 0) return (util_err(session, EIO, NULL)); break; case WT_NOTFOUND: (void)util_err(session, 0, "%s: not found", *argv); rval = true; break; default: return (util_cerr(cursor, "search", ret)); } } return (rval ? 1 : 0); }
int util_dump(WT_SESSION *session, int argc, char *argv[]) { WT_CURSOR *cursor; WT_DECL_RET; size_t len; int ch, i; char *checkpoint, *config, *p, *simpleuri, *uri; bool hex, json, reverse; hex = json = reverse = false; checkpoint = config = simpleuri = uri = NULL; cursor = NULL; while ((ch = __wt_getopt(progname, argc, argv, "c:f:jrx")) != EOF) switch (ch) { case 'c': checkpoint = __wt_optarg; break; case 'f': /* output file */ if (freopen(__wt_optarg, "w", stdout) == NULL) return (util_err( session, errno, "%s: reopen", __wt_optarg)); break; case 'j': json = true; break; case 'r': reverse = true; break; case 'x': hex = true; break; case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* -j and -x are incompatible. */ if (hex && json) { fprintf(stderr, "%s: the -j and -x dump options are incompatible\n", progname); goto err; } /* The remaining argument is the uri. */ if (argc < 1 || (argc != 1 && !json)) return (usage()); if (json && (dump_json_begin(session) != 0 || dump_prefix(session, hex, json) != 0)) goto err; for (i = 0; i < argc; i++) { if (json && i > 0) if (dump_json_separator(session) != 0) goto err; free(uri); free(simpleuri); uri = simpleuri = NULL; if ((uri = util_uri(session, argv[i], "table")) == NULL) goto err; len = checkpoint == NULL ? 0 : strlen("checkpoint=") + strlen(checkpoint) + 1; len += strlen(json ? "dump=json" : (hex ? "dump=hex" : "dump=print")); if ((config = malloc(len + 10)) == NULL) goto err; if (checkpoint == NULL) config[0] = '\0'; else { (void)strcpy(config, "checkpoint="); (void)strcat(config, checkpoint); (void)strcat(config, ","); } (void)strcat(config, json ? "dump=json" : (hex ? "dump=hex" : "dump=print")); if ((ret = session->open_cursor( session, uri, NULL, config, &cursor)) != 0) { fprintf(stderr, "%s: cursor open(%s) failed: %s\n", progname, uri, session->strerror(session, ret)); goto err; } if ((simpleuri = strdup(uri)) == NULL) { (void)util_err(session, errno, NULL); goto err; } if ((p = strchr(simpleuri, '(')) != NULL) *p = '\0'; if (dump_config(session, simpleuri, cursor, hex, json) != 0) goto err; if (dump_record(cursor, reverse, json) != 0) goto err; if (json && dump_json_table_end(session) != 0) goto err; ret = cursor->close(cursor); cursor = NULL; if (ret != 0) { (void)util_err(session, ret, NULL); goto err; } } if (json && dump_json_end(session) != 0) goto err; if (0) { err: ret = 1; } free(config); free(uri); free(simpleuri); if (cursor != NULL && (ret = cursor->close(cursor)) != 0) { (void)util_err(session, ret, NULL); ret = 1; } return (ret); }
int util_stat(WT_SESSION *session, int argc, char *argv[]) { WT_CURSOR *cursor; WT_DECL_RET; size_t urilen; int ch; const char *config, *desc, *pval; char *objname, *uri; bool objname_free; objname_free = false; objname = uri = NULL; config = NULL; while ((ch = __wt_getopt(progname, argc, argv, "af")) != EOF) switch (ch) { case 'a': /* * Historically, the -a option meant include all of the * statistics; because we are opening the database with * statistics=(all), that is now the default, allow the * option for compatibility. */ config = NULL; break; case 'f': config = "statistics=(fast)"; break; case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* * If there are no arguments, the statistics cursor operates on the * connection, otherwise, the optional remaining argument is a file * or LSM name. */ switch (argc) { case 0: objname = (char *)""; break; case 1: if ((objname = util_uri(session, *argv, "table")) == NULL) return (1); objname_free = true; break; default: return (usage()); } urilen = strlen("statistics:") + strlen(objname) + 1; if ((uri = calloc(urilen, 1)) == NULL) { fprintf(stderr, "%s: %s\n", progname, strerror(errno)); goto err; } if ((ret = __wt_snprintf(uri, urilen, "statistics:%s", objname)) != 0) { fprintf(stderr, "%s: %s\n", progname, strerror(ret)); goto err; } if ((ret = session->open_cursor(session, uri, NULL, config, &cursor)) != 0) { fprintf(stderr, "%s: cursor open(%s) failed: %s\n", progname, uri, session->strerror(session, ret)); goto err; } /* List the statistics. */ while ( (ret = cursor->next(cursor)) == 0 && (ret = cursor->get_value(cursor, &desc, &pval, NULL)) == 0) if (printf("%s=%s\n", desc, pval) < 0) { (void)util_err(session, errno, "printf"); goto err; } if (ret == WT_NOTFOUND) ret = 0; if (ret != 0) { fprintf(stderr, "%s: cursor get(%s) failed: %s\n", progname, objname, session->strerror(session, ret)); goto err; } if (0) { err: ret = 1; } if (objname_free) free(objname); free(uri); return (ret); }