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;
46 if ((exitval = version_check()) != 0)
49 ldg.progname = progname;
51 ldg.endodata = ldg.endofile = 0;
53 ldg.cache = (1024 * 1024);
64 * There are two modes for db_load: -r and everything else. The -r
65 * option zeroes out the database LSN's or resets the file ID, it
66 * doesn't really "load" a new database. The functionality is in
67 * db_load because we don't have a better place to put it, and we
68 * don't want to create a new utility for just that functionality.
70 while ((ch = getopt(argc, argv, "h:r:V")) != EOF)
76 if (strcmp(optarg, "lsn") == 0)
78 else if (strcmp(optarg, "fileid") == 0)
86 printf("%s\n", db_version(NULL, NULL, NULL));
87 return (EXIT_SUCCESS);
96 if (argc != 1 || mode == NOTSET) {
102 * Create an environment object initialized for error reporting, and
105 if (env_create(&dbenv, &ldg) != 0)
108 /* If we're resetting the LSNs, that's an entirely separate path. */
111 exitval = dbenv->fileid_reset(dbenv, argv[0], 0);
114 exitval = dbenv->lsn_reset(dbenv, argv[0], 0);
123 if ((ret = dbenv->close(dbenv, 0)) != 0) {
126 "%s: dbenv->close: %s\n", ldg.progname, db_strerror(ret));
129 if (ldg.passwd != NULL)
138 * Create the environment and initialize it for error reporting.
141 env_create(dbenvp, ldg)
148 if ((ret = db_env_create(dbenvp, 0)) != 0) {
149 fprintf(stderr, "%s: db_env_create: %s\n",
150 ldg->progname, db_strerror(ret));
154 dbenv->set_errfile(dbenv, stderr);
155 dbenv->set_errpfx(dbenv, ldg->progname);
156 if (ldg->passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
157 ldg->passwd, DB_ENCRYPT_AES)) != 0) {
158 dbenv->err(dbenv, ret, "set_passwd");
161 if ((ret = db_init(dbenv, ldg->home, ldg->cache, &ldg->private)) != 0)
163 dbenv->app_private = ldg;
170 * Initialize the environment.
173 db_init(dbenv, home, cache, is_private)
183 /* We may be loading into a live environment. Try and join. */
184 flags = DB_USE_ENVIRON |
185 DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN;
186 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
188 if (ret == DB_VERSION_MISMATCH)
192 * We're trying to load a database.
194 * An environment is required because we may be trying to look at
195 * databases in directories other than the current one. We could
196 * avoid using an environment iff the -h option wasn't specified,
197 * but that seems like more work than it's worth.
199 * No environment exists (or, at least no environment that includes
200 * an mpool region exists). Create one, but make it private so that
201 * no files are actually created.
203 #define LF_SET(f) ((flags) |= (f))
204 #define LF_CLR(f) ((flags) &= ~(f))
205 LF_CLR(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN);
206 LF_SET(DB_CREATE | DB_PRIVATE);
208 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
209 dbenv->err(dbenv, ret, "set_cachesize");
212 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
215 /* An environment is required. */
216 err: dbenv->err(dbenv, ret, "DB_ENV->open");
222 * Display the usage message.
227 (void)fprintf(stderr, "usage: %s %s\n\t%s %s\n",
229 progname, "-r lsn | fileid [-h home] db_file");
230 return (EXIT_FAILURE);
236 int v_major, v_minor, v_patch;
238 /* Make sure we're loaded with the right version of the DB library. */
239 (void)db_version(&v_major, &v_minor, &v_patch);
240 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
242 "%s: version %d.%d doesn't match library version %d.%d\n",
243 progname, DB_VERSION_MAJOR,
244 DB_VERSION_MINOR, v_major, v_minor);
245 return (EXIT_FAILURE);