10 typedef struct { /* XXX: Globals. */
11 const char *progname; /* Program name. */
12 char *hdrbuf; /* Input file header. */
13 u_long lineno; /* Input file line number. */
14 u_long origline; /* Original file line number. */
15 int endodata; /* Reached the end of a database. */
16 int endofile; /* Reached the end of the input. */
17 int version; /* Input version. */
18 char *home; /* Env home. */
19 char *passwd; /* Env passwd. */
20 int private; /* Private env. */
21 u_int32_t cache; /* Env cache size. */
24 int db_init __P((DB_ENV *, char *, u_int32_t, int *));
25 int env_create __P((DB_ENV **, LDG *));
26 int main __P((int, char *[]));
27 int usage __P((void));
28 int version_check __P((void));
30 const char *progname = "rpmdb_reset";
37 enum { NOTSET, FILEID_RESET, LSN_RESET, STANDARD_LOAD } mode;
44 if ((exitval = version_check()) != 0)
47 ldg.progname = progname;
49 ldg.endodata = ldg.endofile = 0;
51 ldg.cache = (1024 * 1024);
60 * There are two modes for db_load: -r and everything else. The -r
61 * option zeroes out the database LSN's or resets the file ID, it
62 * doesn't really "load" a new database. The functionality is in
63 * db_load because we don't have a better place to put it, and we
64 * don't want to create a new utility for just that functionality.
66 while ((ch = getopt(argc, argv, "h:r:V")) != EOF)
72 if (strcmp(optarg, "lsn") == 0)
74 else if (strcmp(optarg, "fileid") == 0)
82 printf("%s\n", db_version(NULL, NULL, NULL));
83 return (EXIT_SUCCESS);
92 if (argc != 1 || mode == NOTSET) {
98 * Create an environment object initialized for error reporting, and
101 if (env_create(&dbenv, &ldg) != 0)
104 /* If we're resetting the LSNs, that's an entirely separate path. */
107 exitval = dbenv->fileid_reset(dbenv, argv[0], 0);
110 exitval = dbenv->lsn_reset(dbenv, argv[0], 0);
119 if ((ret = dbenv->close(dbenv, 0)) != 0) {
122 "%s: dbenv->close: %s\n", ldg.progname, db_strerror(ret));
125 if (ldg.passwd != NULL)
134 * Create the environment and initialize it for error reporting.
137 env_create(dbenvp, ldg)
144 if ((ret = db_env_create(dbenvp, 0)) != 0) {
145 fprintf(stderr, "%s: db_env_create: %s\n",
146 ldg->progname, db_strerror(ret));
150 dbenv->set_errfile(dbenv, stderr);
151 dbenv->set_errpfx(dbenv, ldg->progname);
152 if (ldg->passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
153 ldg->passwd, DB_ENCRYPT_AES)) != 0) {
154 dbenv->err(dbenv, ret, "set_passwd");
157 if ((ret = db_init(dbenv, ldg->home, ldg->cache, &ldg->private)) != 0)
159 dbenv->app_private = ldg;
166 * Initialize the environment.
169 db_init(dbenv, home, cache, is_private)
179 /* We may be loading into a live environment. Try and join. */
180 flags = DB_USE_ENVIRON |
181 DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN;
182 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
184 if (ret == DB_VERSION_MISMATCH)
188 * We're trying to load a database.
190 * An environment is required because we may be trying to look at
191 * databases in directories other than the current one. We could
192 * avoid using an environment iff the -h option wasn't specified,
193 * but that seems like more work than it's worth.
195 * No environment exists (or, at least no environment that includes
196 * an mpool region exists). Create one, but make it private so that
197 * no files are actually created.
199 #define LF_SET(f) ((flags) |= (f))
200 #define LF_CLR(f) ((flags) &= ~(f))
201 LF_CLR(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN);
202 LF_SET(DB_CREATE | DB_PRIVATE);
204 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
205 dbenv->err(dbenv, ret, "set_cachesize");
208 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
211 /* An environment is required. */
212 err: dbenv->err(dbenv, ret, "DB_ENV->open");
218 * Display the usage message.
223 (void)fprintf(stderr, "usage: %s %s\n\t%s %s\n",
225 progname, "-r lsn | fileid [-h home] db_file");
226 return (EXIT_FAILURE);
232 int v_major, v_minor, v_patch;
234 /* Make sure we're loaded with the right version of the DB library. */
235 (void)db_version(&v_major, &v_minor, &v_patch);
236 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
238 "%s: version %d.%d doesn't match library version %d.%d\n",
239 progname, DB_VERSION_MAJOR,
240 DB_VERSION_MINOR, v_major, v_minor);
241 return (EXIT_FAILURE);