Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_archive.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_archive.c,v 12.4 2005/09/09 12:38:30 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef lint
00013 static const char copyright[] =
00014     "Copyright (c) 1996-2005\nSleepycat Software Inc.  All rights reserved.\n";
00015 #endif
00016 
00017 #ifndef NO_SYSTEM_INCLUDES
00018 #include <sys/types.h>
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #endif
00025 
00026 #include "db_int.h"
00027 
00028 int db_archive_main __P((int, char *[]));
00029 int db_archive_usage __P((void));
00030 int db_archive_version_check __P((void));
00031 
00032 const char *progname;
00033 
00034 int
00035 db_archive(args)
00036         char *args;
00037 {
00038         int argc;
00039         char **argv;
00040 
00041         __db_util_arg("db_archive", args, &argc, &argv);
00042         return (db_archive_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
00043 }
00044 
00045 #include <stdio.h>
00046 #define ERROR_RETURN    ERROR
00047 
00048 int
00049 db_archive_main(argc, argv)
00050         int argc;
00051         char *argv[];
00052 {
00053         extern char *optarg;
00054         extern int optind, __db_getopt_reset;
00055         DB_ENV  *dbenv;
00056         u_int32_t flags;
00057         int ch, exitval, ret, verbose;
00058         char **file, *home, **list, *passwd;
00059 
00060         if ((progname = strrchr(argv[0], '/')) == NULL)
00061                 progname = argv[0];
00062         else
00063                 ++progname;
00064 
00065         if ((ret = db_archive_version_check()) != 0)
00066                 return (ret);
00067 
00068         dbenv = NULL;
00069         flags = 0;
00070         exitval = verbose = 0;
00071         home = passwd = NULL;
00072         file = list = NULL;
00073         __db_getopt_reset = 1;
00074         while ((ch = getopt(argc, argv, "adh:lP:sVv")) != EOF)
00075                 switch (ch) {
00076                 case 'a':
00077                         LF_SET(DB_ARCH_ABS);
00078                         break;
00079                 case 'd':
00080                         LF_SET(DB_ARCH_REMOVE);
00081                         break;
00082                 case 'h':
00083                         home = optarg;
00084                         break;
00085                 case 'l':
00086                         LF_SET(DB_ARCH_LOG);
00087                         break;
00088                 case 'P':
00089                         passwd = strdup(optarg);
00090                         memset(optarg, 0, strlen(optarg));
00091                         if (passwd == NULL) {
00092                                 fprintf(stderr, "%s: strdup: %s\n",
00093                                     progname, strerror(errno));
00094                                 return (EXIT_FAILURE);
00095                         }
00096                         break;
00097                 case 's':
00098                         LF_SET(DB_ARCH_DATA);
00099                         break;
00100                 case 'V':
00101                         printf("%s\n", db_version(NULL, NULL, NULL));
00102                         return (EXIT_SUCCESS);
00103                 case 'v':
00104                         verbose = 1;
00105                         break;
00106                 case '?':
00107                 default:
00108                         return (db_archive_usage());
00109                 }
00110         argc -= optind;
00111         argv += optind;
00112 
00113         if (argc != 0)
00114                 return (db_archive_usage());
00115 
00116         /* Handle possible interruptions. */
00117         __db_util_siginit();
00118 
00119         /*
00120          * Create an environment object and initialize it for error
00121          * reporting.
00122          */
00123         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00124                 fprintf(stderr,
00125                     "%s: db_env_create: %s\n", progname, db_strerror(ret));
00126                 goto shutdown;
00127         }
00128 
00129         dbenv->set_errfile(dbenv, stderr);
00130         dbenv->set_errpfx(dbenv, progname);
00131 
00132         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00133             passwd, DB_ENCRYPT_AES)) != 0) {
00134                 dbenv->err(dbenv, ret, "set_passwd");
00135                 goto shutdown;
00136         }
00137         /*
00138          * If attaching to a pre-existing environment fails, create a
00139          * private one and try again.
00140          */
00141         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
00142             (ret == DB_VERSION_MISMATCH ||
00143             (ret = dbenv->open(dbenv, home, DB_CREATE |
00144             DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
00145                 dbenv->err(dbenv, ret, "DB_ENV->open");
00146                 goto shutdown;
00147         }
00148 
00149         /* Get the list of names. */
00150         if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
00151                 dbenv->err(dbenv, ret, "DB_ENV->log_archive");
00152                 goto shutdown;
00153         }
00154 
00155         /* Print the list of names. */
00156         if (list != NULL) {
00157                 for (file = list; *file != NULL; ++file)
00158                         printf("%s\n", *file);
00159                 free(list);
00160         }
00161 
00162         if (0) {
00163 shutdown:       exitval = 1;
00164         }
00165         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00166                 exitval = 1;
00167                 fprintf(stderr,
00168                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00169         }
00170 
00171         if (passwd != NULL)
00172                 free(passwd);
00173 
00174         /* Resend any caught signal. */
00175         __db_util_sigresend();
00176 
00177         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00178 }
00179 
00180 int
00181 db_archive_usage()
00182 {
00183         (void)fprintf(stderr,
00184             "usage: %s [-adlsVv] [-h home] [-P password]\n", progname);
00185         return (EXIT_FAILURE);
00186 }
00187 
00188 int
00189 db_archive_version_check()
00190 {
00191         int v_major, v_minor, v_patch;
00192 
00193         /* Make sure we're loaded with the right version of the DB library. */
00194         (void)db_version(&v_major, &v_minor, &v_patch);
00195         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00196                 fprintf(stderr,
00197         "%s: version %d.%d doesn't match library version %d.%d\n",
00198                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00199                     v_major, v_minor);
00200                 return (EXIT_FAILURE);
00201         }
00202         return (0);
00203 }

Generated on Sun Dec 25 12:14:15 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2