summaryrefslogtreecommitdiff
path: root/src/utilities/util_main.c
blob: 7afa00ed30179208c162842e74e1fdc011b09507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 */

#include "util.h"

const char *progname;				/* Program name */
						/* Global arguments */
const char *usage_prefix = "[-Vv] [-C config] [-h home]";
int verbose;					/* Verbose flag */

static const char *command;			/* Command name */

static int usage(void);

int
main(int argc, char *argv[])
{
	WT_CONNECTION *conn;
	WT_SESSION *session;
	int ch, major_v, minor_v, ret, tret;
	const char *config;

	conn = NULL;
	ret = 0;

	/* Get the program name. */
	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		++progname;
	command = "";

	/* Check the version against the library build. */
	(void)wiredtiger_version(&major_v, & minor_v, NULL);
	if (major_v != WIREDTIGER_VERSION_MAJOR ||
	     minor_v != WIREDTIGER_VERSION_MINOR) {
		fprintf(stderr,
		    "%s: program build version %d.%d does not match "
		    "library build version %d.%d\n",
		    progname,
		    WIREDTIGER_VERSION_MAJOR, WIREDTIGER_VERSION_MINOR,
		    major_v,  minor_v);
		return (EXIT_FAILURE);
	}

	/* Check for standard options. */
	config = NULL;
	while ((ch = util_getopt(argc, argv, "C:h:Vv")) != EOF)
		switch (ch) {
		case 'C':			/* wiredtiger_open config */
			config = util_optarg;
			break;
		case 'h':			/* home directory */
			if (chdir(util_optarg) != 0) {
				fprintf(stderr, "%s: chdir %s: %s\n",
				    progname, util_optarg, strerror(errno));
				return (EXIT_FAILURE);
			}
			break;
		case 'V':			/* version */
			printf("%s\n", wiredtiger_version(NULL, NULL, NULL));
			return (EXIT_SUCCESS);
		case 'v':			/* version */
			verbose = 1;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= util_optind;
	argv += util_optind;

	/* The next argument is the command name. */
	if (argc < 1)
		return (usage());
	command = argv[0];

	/* Reset getopt. */
	util_optreset = 1;
	util_optind = 1;

	/* The copyright option doesn't require a database. */
	switch (command[0]) {
	case 'c':
		if (strcmp(command, "copyright") == 0) {
			util_copyright();
			return (EXIT_SUCCESS);
		}
		break;
	}

	/* The "create" and "load" commands can create the database. */
	if (config == NULL &&
	    (strcmp(command, "create") == 0 || strcmp(command, "load") == 0))
		config = "create";

	if ((ret = wiredtiger_open(".",
	    verbose ? verbose_handler : NULL, config, &conn)) != 0)
		goto err;
	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0)
		goto err;

	switch (command[0]) {
	case 'c':
		if (strcmp(command, "create") == 0)
			ret = util_create(session, argc, argv);
		else
			ret = usage();
		break;
	case 'd':
		if (strcmp(command, "drop") == 0)
			ret = util_drop(session, argc, argv);
		else if (strcmp(command, "dump") == 0)
			ret = util_dump(session, argc, argv);
		else if (strcmp(command, "dumpfile") == 0)
			ret = util_dumpfile(session, argc, argv);
		else
			ret = usage();
		break;
	case 'l':
		if (strcmp(command, "load") == 0)
			ret = util_load(session, argc, argv);
		else if (strcmp(command, "loadtext") == 0)
			ret = util_loadtext(session, argc, argv);
		else
			ret = usage();
		break;
	case 'p':
		if (strcmp(command, "printlog") == 0)
			ret = util_printlog(session, argc, argv);
		else
			ret = usage();
		break;
	case 'r':
		if (strcmp(command, "read") == 0)
			ret = util_read(session, argc, argv);
		else
			ret = usage();
		break;
	case 's':
		if (strcmp(command, "salvage") == 0)
			ret = util_salvage(session, argc, argv);
		else if (strcmp(command, "stat") == 0)
			ret = util_stat(session, argc, argv);
		else
			ret = usage();
		break;
	case 'v':
		if (strcmp(command, "verify") == 0)
			ret = util_verify(session, argc, argv);
		else
			ret = usage();
		break;
	default:
		ret = usage();
		break;
	}

err:	if (conn != NULL && (tret = conn->close(conn, NULL)) != 0 && ret == 0)
		ret = tret;

	return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

static int
usage(void)
{
	fprintf(stderr,
	    "WiredTiger Data Engine (version %d.%d)\n", 
	    WIREDTIGER_VERSION_MAJOR, WIREDTIGER_VERSION_MINOR);
	fprintf(stderr,
	    "global options:\n"
	    "\t-C\twiredtiger_open configuration\n"
	    "\t-h\tdatabase directory\n"
	    "\t-V\tdisplay library version and exit\n"
	    "\t-v\tverbose\n");
	fprintf(stderr,
	    "commands:\n"
	    "\tcopyright copyright information\n"
	    "\tcreate\t  create an object\n"
	    "\tdrop\t  drop a table\n"
	    "\tdump\t  dump a table\n"
	    "\tdumpfile  dump a physical file in debugging format\n"
	    "\tload\t  load a table\n"
	    "\tprintlog  display the database log\n"
	    "\tsalvage\t  salvage a file\n"
	    "\tstat\t  display statistics for a table\n"
	    "\tverify\t  verify a file\n");

	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, "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 = strlen(type) + strlen(s) + 2;
	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);
}