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

db_upgrade.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_upgrade.c,v 12.5 2005/09/09 12:38:36 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_upgrade_main __P((int, char *[]));
00029 int db_upgrade_usage __P((void));
00030 int db_upgrade_version_check __P((void));
00031 
00032 const char *progname;
00033 
00034 int
00035 db_upgrade(args)
00036         char *args;
00037 {
00038         int argc;
00039         char **argv;
00040 
00041         __db_util_arg("db_upgrade", args, &argc, &argv);
00042         return (db_upgrade_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
00043 }
00044 
00045 #include <stdio.h>
00046 #define ERROR_RETURN    ERROR
00047 
00048 int
00049 db_upgrade_main(argc, argv)
00050         int argc;
00051         char *argv[];
00052 {
00053         extern char *optarg;
00054         extern int optind, __db_getopt_reset;
00055         DB *dbp;
00056         DB_ENV *dbenv;
00057         u_int32_t flags;
00058         int ch, exitval, nflag, ret, t_ret, verbose;
00059         char *home, *passwd;
00060 
00061         if ((progname = strrchr(argv[0], '/')) == NULL)
00062                 progname = argv[0];
00063         else
00064                 ++progname;
00065 
00066         if ((ret = db_upgrade_version_check()) != 0)
00067                 return (ret);
00068 
00069         dbenv = NULL;
00070         flags = nflag = verbose = 0;
00071         exitval = 0;
00072         home = passwd = NULL;
00073         __db_getopt_reset = 1;
00074         while ((ch = getopt(argc, argv, "h:NP:sVv")) != EOF)
00075                 switch (ch) {
00076                 case 'h':
00077                         home = optarg;
00078                         break;
00079                 case 'N':
00080                         nflag = 1;
00081                         break;
00082                 case 'P':
00083                         passwd = strdup(optarg);
00084                         memset(optarg, 0, strlen(optarg));
00085                         if (passwd == NULL) {
00086                                 fprintf(stderr, "%s: strdup: %s\n",
00087                                     progname, strerror(errno));
00088                                 return (EXIT_FAILURE);
00089                         }
00090                         break;
00091                 case 's':
00092                         LF_SET(DB_DUPSORT);
00093                         break;
00094                 case 'V':
00095                         printf("%s\n", db_version(NULL, NULL, NULL));
00096                         return (EXIT_SUCCESS);
00097                 case 'v':
00098                         verbose = 1;
00099                         break;
00100                 case '?':
00101                 default:
00102                         return (db_upgrade_usage());
00103                 }
00104         argc -= optind;
00105         argv += optind;
00106 
00107         if (argc <= 0)
00108                 return (db_upgrade_usage());
00109 
00110         /* Handle possible interruptions. */
00111         __db_util_siginit();
00112 
00113         /*
00114          * Create an environment object and initialize it for error
00115          * reporting.
00116          */
00117         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00118                 fprintf(stderr, "%s: db_env_create: %s\n",
00119                     progname, db_strerror(ret));
00120                 goto shutdown;
00121         }
00122 
00123         dbenv->set_errfile(dbenv, stderr);
00124         dbenv->set_errpfx(dbenv, progname);
00125 
00126         if (nflag) {
00127                 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
00128                         dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
00129                         goto shutdown;
00130                 }
00131                 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
00132                         dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
00133                         goto shutdown;
00134                 }
00135         }
00136 
00137         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00138             passwd, DB_ENCRYPT_AES)) != 0) {
00139                 dbenv->err(dbenv, ret, "set_passwd");
00140                 goto shutdown;
00141         }
00142 
00143         /*
00144          * If attaching to a pre-existing environment fails, create a
00145          * private one and try again.
00146          */
00147         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
00148             (ret == DB_VERSION_MISMATCH ||
00149             (ret = dbenv->open(dbenv, home,
00150             DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON,
00151             0)) != 0)) {
00152                 dbenv->err(dbenv, ret, "DB_ENV->open");
00153                 goto shutdown;
00154         }
00155 
00156         for (; !__db_util_interrupted() && argv[0] != NULL; ++argv) {
00157                 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
00158                         fprintf(stderr,
00159                             "%s: db_create: %s\n", progname, db_strerror(ret));
00160                         goto shutdown;
00161                 }
00162                 dbp->set_errfile(dbp, stderr);
00163                 dbp->set_errpfx(dbp, progname);
00164                 if ((ret = dbp->upgrade(dbp, argv[0], flags)) != 0)
00165                         dbp->err(dbp, ret, "DB->upgrade: %s", argv[0]);
00166                 if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) {
00167                         dbenv->err(dbenv, ret, "DB->close: %s", argv[0]);
00168                         ret = t_ret;
00169                 }
00170                 if (ret != 0)
00171                         goto shutdown;
00172                 /*
00173                  * People get concerned if they don't see a success message.
00174                  * If verbose is set, give them one.
00175                  */
00176                 if (verbose)
00177                         printf("%s: %s upgraded successfully\n",
00178                             progname, argv[0]);
00179         }
00180 
00181         if (0) {
00182 shutdown:       exitval = 1;
00183         }
00184         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00185                 exitval = 1;
00186                 fprintf(stderr,
00187                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00188         }
00189 
00190         if (passwd != NULL)
00191                 free(passwd);
00192 
00193         /* Resend any caught signal. */
00194         __db_util_sigresend();
00195 
00196         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00197 }
00198 
00199 int
00200 db_upgrade_usage()
00201 {
00202         fprintf(stderr, "usage: %s %s\n", progname,
00203             "[-NsVv] [-h home] [-P password] db_file ...");
00204         return (EXIT_FAILURE);
00205 }
00206 
00207 int
00208 db_upgrade_version_check()
00209 {
00210         int v_major, v_minor, v_patch;
00211 
00212         /* Make sure we're loaded with the right version of the DB library. */
00213         (void)db_version(&v_major, &v_minor, &v_patch);
00214         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00215                 fprintf(stderr,
00216         "%s: version %d.%d doesn't match library version %d.%d\n",
00217                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00218                     v_major, v_minor);
00219                 return (EXIT_FAILURE);
00220         }
00221         return (0);
00222 }

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