1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: db_archive.c,v 12.13 2008/01/08 20:58:11 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13#ifndef lint
14static const char copyright[] =
15    "Copyright (c) 1996,2008 Oracle.  All rights reserved.\n";
16#endif
17
18int main __P((int, char *[]));
19int usage __P((void));
20int version_check __P((void));
21
22const char *progname;
23
24int
25main(argc, argv)
26	int argc;
27	char *argv[];
28{
29	extern char *optarg;
30	extern int optind;
31	DB_ENV	*dbenv;
32	u_int32_t flags;
33	int ch, exitval, ret, verbose;
34	char **file, *home, **list, *passwd;
35
36	if ((progname = __db_rpath(argv[0])) == NULL)
37		progname = argv[0];
38	else
39		++progname;
40
41	if ((ret = version_check()) != 0)
42		return (ret);
43
44	dbenv = NULL;
45	flags = 0;
46	exitval = verbose = 0;
47	home = passwd = NULL;
48	file = list = NULL;
49	while ((ch = getopt(argc, argv, "adh:lP:sVv")) != EOF)
50		switch (ch) {
51		case 'a':
52			LF_SET(DB_ARCH_ABS);
53			break;
54		case 'd':
55			LF_SET(DB_ARCH_REMOVE);
56			break;
57		case 'h':
58			home = optarg;
59			break;
60		case 'l':
61			LF_SET(DB_ARCH_LOG);
62			break;
63		case 'P':
64			passwd = strdup(optarg);
65			memset(optarg, 0, strlen(optarg));
66			if (passwd == NULL) {
67				fprintf(stderr, "%s: strdup: %s\n",
68				    progname, strerror(errno));
69				return (EXIT_FAILURE);
70			}
71			break;
72		case 's':
73			LF_SET(DB_ARCH_DATA);
74			break;
75		case 'V':
76			printf("%s\n", db_version(NULL, NULL, NULL));
77			return (EXIT_SUCCESS);
78		case 'v':
79			/*
80			 * !!!
81			 * The verbose flag no longer actually does anything,
82			 * but it's left rather than adding it back at some
83			 * future date.
84			 */
85			verbose = 1;
86			break;
87		case '?':
88		default:
89			return (usage());
90		}
91	argc -= optind;
92	argv += optind;
93
94	if (argc != 0)
95		return (usage());
96
97	/* Handle possible interruptions. */
98	__db_util_siginit();
99
100	/*
101	 * Create an environment object and initialize it for error
102	 * reporting.
103	 */
104	if ((ret = db_env_create(&dbenv, 0)) != 0) {
105		fprintf(stderr,
106		    "%s: db_env_create: %s\n", progname, db_strerror(ret));
107		goto shutdown;
108	}
109
110	dbenv->set_errfile(dbenv, stderr);
111	dbenv->set_errpfx(dbenv, progname);
112
113	if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
114	    passwd, DB_ENCRYPT_AES)) != 0) {
115		dbenv->err(dbenv, ret, "set_passwd");
116		goto shutdown;
117	}
118	/*
119	 * If attaching to a pre-existing environment fails, create a
120	 * private one and try again.
121	 */
122	if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
123	    (ret == DB_VERSION_MISMATCH ||
124	    (ret = dbenv->open(dbenv, home, DB_CREATE |
125	    DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
126		dbenv->err(dbenv, ret, "DB_ENV->open");
127		goto shutdown;
128	}
129
130	/* Get the list of names. */
131	if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
132		dbenv->err(dbenv, ret, "DB_ENV->log_archive");
133		goto shutdown;
134	}
135
136	/* Print the list of names. */
137	if (list != NULL) {
138		for (file = list; *file != NULL; ++file)
139			printf("%s\n", *file);
140		free(list);
141	}
142
143	if (0) {
144shutdown:	exitval = 1;
145	}
146	if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
147		exitval = 1;
148		fprintf(stderr,
149		    "%s: dbenv->close: %s\n", progname, db_strerror(ret));
150	}
151
152	if (passwd != NULL)
153		free(passwd);
154
155	/* Resend any caught signal. */
156	__db_util_sigresend();
157
158	return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
159}
160
161int
162usage()
163{
164	(void)fprintf(stderr,
165	    "usage: %s [-adlsVv] [-h home] [-P password]\n", progname);
166	return (EXIT_FAILURE);
167}
168
169int
170version_check()
171{
172	int v_major, v_minor, v_patch;
173
174	/* Make sure we're loaded with the right version of the DB library. */
175	(void)db_version(&v_major, &v_minor, &v_patch);
176	if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
177		fprintf(stderr,
178	"%s: version %d.%d doesn't match library version %d.%d\n",
179		    progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
180		    v_major, v_minor);
181		return (EXIT_FAILURE);
182	}
183	return (0);
184}
185