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