9 typedef struct { /* XXX: Globals. */
10 const char *progname; /* Program name. */
11 char *hdrbuf; /* Input file header. */
12 u_long lineno; /* Input file line number. */
13 u_long origline; /* Original file line number. */
14 int endodata; /* Reached the end of a database. */
15 int endofile; /* Reached the end of the input. */
16 int version; /* Input version. */
17 char *home; /* Env home. */
18 char *passwd; /* Env passwd. */
19 int private; /* Private env. */
20 u_int32_t cache; /* Env cache size. */
23 int db_init __P((DB_ENV *, char *, u_int32_t, int *));
24 int env_create __P((DB_ENV **, LDG *));
25 int main __P((int, char *[]));
26 int usage __P((void));
27 int version_check __P((void));
29 const char *progname = "rpmdb_reset";
36 enum { NOTSET, FILEID_RESET, LSN_RESET, STANDARD_LOAD } mode;
45 if ((exitval = version_check()) != 0)
48 ldg.progname = progname;
50 ldg.endodata = ldg.endofile = 0;
52 ldg.cache = (1024 * 1024);
63 * There are two modes for db_load: -r and everything else. The -r
64 * option zeroes out the database LSN's or resets the file ID, it
65 * doesn't really "load" a new database. The functionality is in
66 * db_load because we don't have a better place to put it, and we
67 * don't want to create a new utility for just that functionality.
69 while ((ch = getopt(argc, argv, "h:r:V")) != EOF)
75 if (strcmp(optarg, "lsn") == 0)
77 else if (strcmp(optarg, "fileid") == 0)
85 printf("%s\n", db_version(NULL, NULL, NULL));
86 return (EXIT_SUCCESS);
95 if (argc != 1 || mode == NOTSET) {
101 * Create an environment object initialized for error reporting, and
104 if (env_create(&dbenv, &ldg) != 0)
107 /* If we're resetting the LSNs, that's an entirely separate path. */
110 exitval = dbenv->fileid_reset(dbenv, argv[0], 0);
113 exitval = dbenv->lsn_reset(dbenv, argv[0], 0);
122 if ((ret = dbenv->close(dbenv, 0)) != 0) {
125 "%s: dbenv->close: %s\n", ldg.progname, db_strerror(ret));
128 if (ldg.passwd != NULL)
137 * Create the environment and initialize it for error reporting.
140 env_create(dbenvp, ldg)
147 if ((ret = db_env_create(dbenvp, 0)) != 0) {
148 fprintf(stderr, "%s: db_env_create: %s\n",
149 ldg->progname, db_strerror(ret));
153 dbenv->set_errfile(dbenv, stderr);
154 dbenv->set_errpfx(dbenv, ldg->progname);
155 if (ldg->passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
156 ldg->passwd, DB_ENCRYPT_AES)) != 0) {
157 dbenv->err(dbenv, ret, "set_passwd");
160 if ((ret = db_init(dbenv, ldg->home, ldg->cache, &ldg->private)) != 0)
162 dbenv->app_private = ldg;
169 * Initialize the environment.
172 db_init(dbenv, home, cache, is_private)
182 /* We may be loading into a live environment. Try and join. */
183 flags = DB_USE_ENVIRON |
184 DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN;
185 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
187 if (ret == DB_VERSION_MISMATCH)
191 * We're trying to load a database.
193 * An environment is required because we may be trying to look at
194 * databases in directories other than the current one. We could
195 * avoid using an environment iff the -h option wasn't specified,
196 * but that seems like more work than it's worth.
198 * No environment exists (or, at least no environment that includes
199 * an mpool region exists). Create one, but make it private so that
200 * no files are actually created.
202 #define LF_SET(f) ((flags) |= (f))
203 #define LF_CLR(f) ((flags) &= ~(f))
204 LF_CLR(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN);
205 LF_SET(DB_CREATE | DB_PRIVATE);
207 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
208 dbenv->err(dbenv, ret, "set_cachesize");
211 if ((ret = dbenv->open(dbenv, home, flags, 0)) == 0)
214 /* An environment is required. */
215 err: dbenv->err(dbenv, ret, "DB_ENV->open");
221 * Display the usage message.
226 (void)fprintf(stderr, "usage: %s %s\n\t%s %s\n",
228 progname, "-r lsn | fileid [-h home] db_file");
229 return (EXIT_FAILURE);
235 int v_major, v_minor, v_patch;
237 /* Make sure we're loaded with the right version of the DB library. */
238 (void)db_version(&v_major, &v_minor, &v_patch);
239 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
241 "%s: version %d.%d doesn't match library version %d.%d\n",
242 progname, DB_VERSION_MAJOR,
243 DB_VERSION_MINOR, v_major, v_minor);
244 return (EXIT_FAILURE);