Debian Codegraph @

build_vxworks/util/db_archive.c

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8
9 #include "db_config.h"
10
11 #include "db_int.h"
12
13 #ifndef lint
14 static const char copyright[] =
15 "Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n";
16 #endif
17
18 int db_archive_main __P((int, char *[]));
19 int db_archive_usage __P((void));
20 int db_archive_version_check __P((void));
21
22 const char *progname;
23
24 int
25 db_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
38 int
39 db_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 if (passwd != NULL) {
80 fprintf(stderr, DB_STR("5135",
81 "Password may not be specified twice"));
82 free(passwd);
83 return (EXIT_FAILURE);
84 }
85 passwd = strdup(optarg);
86 memset(optarg, 0, strlen(optarg));
87 if (passwd == NULL) {
88 fprintf(stderr, DB_STR_A("5119",
89 "%s: strdup: %s\n", "%s %s\n"),
90 progname, strerror(errno));
91 return (EXIT_FAILURE);
92 }
93 break;
94 case 's':
95 LF_SET(DB_ARCH_DATA);
96 break;
97 case 'V':
98 printf("%s\n", db_version(NULL, NULL, NULL));
99 return (EXIT_SUCCESS);
100 case 'v':
101 /*
102 * !!!
103 * The verbose flag no longer actually does anything,
104 * but it's left rather than adding it back at some
105 * future date.
106 */
107 verbose = 1;
108 break;
109 case '?':
110 default:
111 return (db_archive_usage());
112 }
113 argc -= optind;
114 argv += optind;
115
116 if (argc != 0)
117 return (db_archive_usage());
118
119 /* Handle possible interruptions. */
120 __db_util_siginit();
121
122 /*
123 * Create an environment object and initialize it for error
124 * reporting.
125 */
126 if ((ret = db_env_create(&dbenv, 0)) != 0) {
127 fprintf(stderr,
128 "%s: db_env_create: %s\n", progname, db_strerror(ret));
129 goto err;
130 }
131
132 dbenv->set_errfile(dbenv, stderr);
133 dbenv->set_errpfx(dbenv, progname);
134
135 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
136 passwd, DB_ENCRYPT_AES)) != 0) {
137 dbenv->err(dbenv, ret, "set_passwd");
138 goto err;
139 }
140 /*
141 * If attaching to a pre-existing environment fails, create a
142 * private one and try again.
143 */
144 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
145 (ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT ||
146 (ret = dbenv->open(dbenv, home, DB_CREATE |
147 DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
148 dbenv->err(dbenv, ret, "DB_ENV->open");
149 goto err;
150 }
151
152 /* Get the list of names. */
153 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
154 dbenv->err(dbenv, ret, "DB_ENV->log_archive");
155 goto err;
156 }
157
158 /* Print the list of names. */
159 if (list != NULL) {
160 for (file = list; *file != NULL; ++file)
161 printf("%s\n", *file);
162 free(list);
163 }
164
165 if (0) {
166 err: exitval = 1;
167 }
168 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
169 exitval = 1;
170 fprintf(stderr,
171 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
172 }
173
174 if (passwd != NULL)
175 free(passwd);
176
177 /* Resend any caught signal. */
178 __db_util_sigresend();
179
180 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
181 }
182
183 int
184 db_archive_usage()
185 {
186 (void)fprintf(stderr,
187 "usage: %s [-adlsVv] [-h home] [-P password]\n", progname);
188 return (EXIT_FAILURE);
189 }
190
191 int
192 db_archive_version_check()
193 {
194 int v_major, v_minor, v_patch;
195
196 /* Make sure we're loaded with the right version of the DB library. */
197 (void)db_version(&v_major, &v_minor, &v_patch);
198 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
199 fprintf(stderr, DB_STR_A("5120",
200 "%s: version %d.%d doesn't match library version %d.%d\n",
201 "%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR,
202 DB_VERSION_MINOR, v_major, v_minor);
203 return (EXIT_FAILURE);
204 }
205 return (0);
206 }