1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1996-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9#include <sys/types.h> 10 11#include <stddef.h> 12#include <stdio.h> 13#include <stdlib.h> 14 15#ifdef _WIN32 16extern int getopt(int, char * const *, const char *); 17#else 18#include <unistd.h> 19#endif 20 21#include <db.h> 22 23int db_setup __P((const char *, const char *, FILE *, const char *)); 24int db_teardown __P((const char *, const char *, FILE *, const char *)); 25static int usage __P((void)); 26 27const char *progname = "ex_env"; /* Program name. */ 28 29/* 30 * An example of a program creating/configuring a Berkeley DB environment. 31 */ 32int 33main(argc, argv) 34 int argc; 35 char *argv[]; 36{ 37 extern char *optarg; 38 extern int optind; 39 const char *data_dir, *home; 40 41 int ch; 42 /* 43 * All of the shared database files live in home, but 44 * data files will live in data_dir. 45 */ 46 home = "TESTDIR"; 47 data_dir = "data"; 48 while ((ch = getopt(argc, argv, "h:d:")) != EOF) 49 switch (ch) { 50 case 'h': 51 home = optarg; 52 break; 53 case 'd': 54 data_dir = optarg; 55 break; 56 case '?': 57 default: 58 return (usage()); 59 } 60 argc -= optind; 61 argv += optind; 62 63 if (argc != 0) 64 return (usage()); 65 66 printf("Setup env\n"); 67 if (db_setup(home, data_dir, stderr, progname) != 0) 68 return (EXIT_FAILURE); 69 70 printf("Teardown env\n"); 71 if (db_teardown(home, data_dir, stderr, progname) != 0) 72 return (EXIT_FAILURE); 73 74 return (EXIT_SUCCESS); 75} 76 77int 78db_setup(home, data_dir, errfp, progname) 79 const char *home, *data_dir, *progname; 80 FILE *errfp; 81{ 82 DB_ENV *dbenv; 83 DB *dbp; 84 int ret; 85 86 /* 87 * Create an environment object and initialize it for error 88 * reporting. 89 */ 90 if ((ret = db_env_create(&dbenv, 0)) != 0) { 91 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret)); 92 return (1); 93 } 94 dbenv->set_errfile(dbenv, errfp); 95 dbenv->set_errpfx(dbenv, progname); 96 97 /* 98 * We want to specify the shared memory buffer pool cachesize, 99 * but everything else is the default. 100 */ 101 if ((ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0)) != 0) { 102 dbenv->err(dbenv, ret, "set_cachesize"); 103 dbenv->close(dbenv, 0); 104 return (1); 105 } 106 107 /* Databases are in a subdirectory. */ 108 (void)dbenv->set_data_dir(dbenv, data_dir); 109 110 /* Open the environment with full transactional support. */ 111 if ((ret = dbenv->open(dbenv, home, 112 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | 113 DB_INIT_TXN, 0644)) != 0) { 114 dbenv->err(dbenv, ret, "environment open: %s", home); 115 dbenv->close(dbenv, 0); 116 return (1); 117 } 118 119 /* 120 * Open a database in the environment to verify the data_dir 121 * has been set correctly. 122 * Create a database object and initialize it for error 123 * reporting. 124 */ 125 if ((ret = db_create(&dbp, dbenv, 0)) != 0){ 126 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret)); 127 return (1); 128 } 129 130 /* Open a database with DB_BTREE access method. */ 131 if ((ret = dbp->open(dbp, NULL, "exenv_db1.db", NULL, 132 DB_BTREE, DB_CREATE,0644)) != 0){ 133 fprintf(stderr, "database open: %s\n", db_strerror(ret)); 134 return (1); 135 } 136 137 /* Close the database handle. */ 138 if ((ret = dbp->close(dbp, 0)) != 0) { 139 fprintf(stderr, "database close: %s\n", db_strerror(ret)); 140 return (1); 141 } 142 143 /* Close the environment handle. */ 144 if ((ret = dbenv->close(dbenv, 0)) != 0) { 145 fprintf(stderr, "DB_ENV->close: %s\n", db_strerror(ret)); 146 return (1); 147 } 148 return (0); 149} 150 151int 152db_teardown(home, data_dir, errfp, progname) 153 const char *home, *data_dir, *progname; 154 FILE *errfp; 155{ 156 DB_ENV *dbenv; 157 int ret; 158 159 /* Remove the shared database regions. */ 160 if ((ret = db_env_create(&dbenv, 0)) != 0) { 161 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret)); 162 return (1); 163 } 164 dbenv->set_errfile(dbenv, errfp); 165 dbenv->set_errpfx(dbenv, progname); 166 167 (void)dbenv->set_data_dir(dbenv, data_dir); 168 169 /* Remove the environment. */ 170 if ((ret = dbenv->remove(dbenv, home, 0)) != 0) { 171 fprintf(stderr, "DB_ENV->remove: %s\n", db_strerror(ret)); 172 return (1); 173 } 174 return (0); 175} 176 177static int 178usage() 179{ 180 (void)fprintf(stderr, 181 "usage: %s [-h home] [-d data_dir]\n", progname); 182 return (EXIT_FAILURE); 183} 184