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

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