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