2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997-2009 Oracle. All rights reserved.
12 #include "dbinc/db_page.h"
13 #include "dbinc/log.h"
14 #include "dbinc/qam.h"
15 #include "dbinc/txn.h"
17 static int __absname __P((ENV *, char *, char *, char **));
18 static int __build_data __P((ENV *, char *, char ***));
19 static int __cmpfunc __P((const void *, const void *));
20 static int __log_archive __P((ENV *, char **[], u_int32_t));
21 static int __usermem __P((ENV *, char ***));
25 * ENV->log_archive pre/post processing.
27 * PUBLIC: int __log_archive_pp __P((DB_ENV *, char **[], u_int32_t));
30 __log_archive_pp(dbenv, listp, flags)
41 ENV_REQUIRES_CONFIG(env,
42 env->lg_handle, "DB_ENV->log_archive", DB_INIT_LOG);
44 #define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG | DB_ARCH_REMOVE)
47 env, "DB_ENV->log_archive", flags, OKFLAGS)) != 0)
49 if ((ret = __db_fcchk(env, "DB_ENV->log_archive",
50 flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
52 if ((ret = __db_fcchk(env, "DB_ENV->log_archive",
53 flags, DB_ARCH_REMOVE,
54 DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)) != 0)
59 REPLICATION_WRAP(env, (__log_archive(env, listp, flags)), 0, ret);
66 * ENV->log_archive. Internal.
69 __log_archive(env, listp, flags)
82 char **array, **arrayp, *name, *p, *pref;
84 char path[DB_MAXPATHLEN];
87 dblp = env->lg_handle;
88 lp = (LOG *)dblp->reginfo.primary;
94 if (flags != DB_ARCH_REMOVE)
97 /* There are no log files if logs are in memory. */
98 if (lp->db_log_inmemory) {
99 LF_CLR(~DB_ARCH_DATA);
105 * If the user wants the list of log files to remove and we're
106 * at a bad time in replication initialization, just return.
108 if (!LF_ISSET(DB_ARCH_DATA) &&
109 !LF_ISSET(DB_ARCH_LOG) && __rep_noarchive(env))
113 * Prepend the original absolute pathname if the user wants an
114 * absolute path to the database environment directory.
117 if (LF_ISSET(DB_ARCH_ABS)) {
120 * Can't trust getcwd(3) to set a valid errno, so don't display
121 * one unless we know it's good. It's likely a permissions
122 * problem: use something bland and useless in the default
123 * return value, so we don't send somebody off in the wrong
127 if (getcwd(path, sizeof(path)) == NULL) {
128 ret = __os_get_errno();
130 ret, "no absolute path for the current directory");
141 ret = __build_data(env, pref, listp);
144 memset(&rec, 0, sizeof(rec));
145 if ((ret = __log_cursor(env, &logc)) != 0)
148 ZERO_LSN(stable_lsn);
150 ret = __logc_get(logc, &stable_lsn, &rec, DB_LAST);
151 if ((t_ret = __logc_close(logc)) != 0 && ret == 0)
155 fnum = stable_lsn.file;
158 __log_autoremove(env);
162 ret = __log_get_stable_lsn(env, &stable_lsn);
164 * A return of DB_NOTFOUND means the checkpoint LSN
165 * is before the beginning of the log files we have.
166 * This is not an error; it just means we're done.
169 if (ret == DB_NOTFOUND)
173 /* Remove any log files before the last stable LSN. */
174 fnum = stable_lsn.file - 1;
177 ret = __db_unknown_path(env, "__log_archive");
181 #define LIST_INCREMENT 64
182 /* Get some initial space. */
184 if ((ret = __os_malloc(env,
185 sizeof(char *) * array_size, &array)) != 0)
189 /* Build an array of the file names. */
190 for (n = 0; fnum > 0; --fnum) {
191 if ((ret = __log_name(dblp, fnum, &name, NULL, 0)) != 0) {
192 __os_free(env, name);
195 if (__os_exists(env, name, NULL) != 0) {
196 if (LF_ISSET(DB_ARCH_LOG) && fnum == stable_lsn.file)
198 __os_free(env, name);
203 if (n >= array_size - 2) {
204 array_size += LIST_INCREMENT;
205 if ((ret = __os_realloc(env,
206 sizeof(char *) * array_size, &array)) != 0)
212 __absname(env, pref, name, &array[n])) != 0)
214 __os_free(env, name);
215 } else if ((p = __db_rpath(name)) != NULL) {
216 if ((ret = __os_strdup(env, p + 1, &array[n])) != 0)
218 __os_free(env, name);
226 /* If there's nothing to return, we're done. */
231 qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
233 /* Rework the memory. */
234 if ((ret = __usermem(env, &array)) != 0)
241 err: if (array != NULL) {
242 for (arrayp = array; *arrayp != NULL; ++arrayp)
243 __os_free(env, *arrayp);
244 __os_free(env, array);
247 __os_free(env, name);
254 * __log_get_stable_lsn --
255 * Get the stable lsn based on where checkpoints are.
257 * PUBLIC: int __log_get_stable_lsn __P((ENV *, DB_LSN *));
260 __log_get_stable_lsn(env, stable_lsn)
267 __txn_ckp_args *ckp_args;
270 lp = env->lg_handle->reginfo.primary;
273 memset(&rec, 0, sizeof(rec));
275 if ((ret = __log_get_cached_ckp_lsn(env, stable_lsn)) != 0)
278 * No need to check for a return value of DB_NOTFOUND;
279 * __txn_findlastckp returns 0 if no checkpoint record
280 * is found. Instead of checking the return value, we
281 * check to see if the return LSN has been filled in.
283 if (IS_ZERO_LSN(*stable_lsn) && (ret =
284 __txn_findlastckp(env, stable_lsn, NULL)) != 0)
287 * If the LSN has not been filled in return DB_NOTFOUND
288 * so that the caller knows it may be done.
290 if (IS_ZERO_LSN(*stable_lsn)) {
294 } else if ((ret = __txn_getckp(env, stable_lsn)) != 0)
296 if ((ret = __log_cursor(env, &logc)) != 0)
299 * Read checkpoint records until we find one that is on disk,
300 * then copy the ckp_lsn to the stable_lsn;
302 while ((ret = __logc_get(logc, stable_lsn, &rec, DB_SET)) == 0 &&
303 (ret = __txn_ckp_read(env, rec.data, &ckp_args)) == 0) {
304 if (stable_lsn->file < lp->s_lsn.file ||
305 (stable_lsn->file == lp->s_lsn.file &&
306 stable_lsn->offset < lp->s_lsn.offset)) {
307 *stable_lsn = ckp_args->ckp_lsn;
308 __os_free(env, ckp_args);
311 *stable_lsn = ckp_args->last_ckp;
312 __os_free(env, ckp_args);
314 if ((t_ret = __logc_close(logc)) != 0 && ret == 0)
321 * __log_autoremove --
322 * Delete any non-essential log files.
324 * PUBLIC: void __log_autoremove __P((ENV *));
327 __log_autoremove(env)
331 char **begin, **list;
334 * Complain if there's an error, but don't return the error to our
335 * caller. Auto-remove is done when writing a log record, and we
336 * don't want to fail a write, which could fail the corresponding
337 * committing transaction, for a permissions error.
339 if ((ret = __log_archive(env, &list, DB_ARCH_ABS)) != 0) {
340 if (ret != DB_NOTFOUND)
341 __db_err(env, ret, "log file auto-remove");
345 /* Remove the files. */
347 for (begin = list; *list != NULL; ++list)
348 (void)__os_unlink(env, *list, 0);
349 __os_ufree(env, begin);
355 * Build a list of datafiles for return.
358 __build_data(env, pref, listp)
360 char *pref, ***listp;
365 __dbreg_register_args *argp;
366 u_int array_size, last, n, nxt;
369 char **array, **arrayp, **list, **lp, *p, *real_name;
371 /* Get some initial space. */
373 if ((ret = __os_malloc(env,
374 sizeof(char *) * array_size, &array)) != 0)
378 memset(&rec, 0, sizeof(rec));
379 if ((ret = __log_cursor(env, &logc)) != 0)
381 for (n = 0; (ret = __logc_get(logc, &lsn, &rec, DB_PREV)) == 0;) {
382 if (rec.size < sizeof(rectype)) {
384 __db_errx(env, "DB_ENV->log_archive: bad log record");
388 LOGCOPY_32(env, &rectype, rec.data);
389 if (rectype != DB___dbreg_register)
392 __dbreg_register_read(env, rec.data, &argp)) != 0) {
395 "DB_ENV->log_archive: unable to read log record");
399 if (n >= array_size - 2) {
400 array_size += LIST_INCREMENT;
401 if ((ret = __os_realloc(env,
402 sizeof(char *) * array_size, &array)) != 0)
406 if ((ret = __os_strdup(env,
407 argp->name.data, &array[n++])) != 0)
411 if (argp->ftype == DB_QUEUE) {
412 if ((ret = __qam_extent_names(env,
413 argp->name.data, &list)) != 0)
416 lp != NULL && *lp != NULL; lp++) {
417 if (n >= array_size - 2) {
418 array_size += LIST_INCREMENT;
419 if ((ret = __os_realloc(env,
421 array_size, &array)) != 0)
425 __os_strdup(env, *lp, &array[n++])) != 0)
429 q_err: if (list != NULL)
430 __os_free(env, list);
432 free_continue: __os_free(env, argp);
436 if (ret == DB_NOTFOUND)
438 if ((t_ret = __logc_close(logc)) != 0 && ret == 0)
443 /* If there's nothing to return, we're done. */
451 qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
454 * Build the real pathnames, discarding nonexistent files and
457 for (last = nxt = 0; nxt < n;) {
459 * Discard duplicates. Last is the next slot we're going
460 * to return to the user, nxt is the next slot that we're
464 array[last] = array[nxt];
467 for (++nxt; nxt < n &&
468 strcmp(array[last], array[nxt]) == 0; ++nxt) {
469 __os_free(env, array[nxt]);
473 /* Get the real name. */
474 if ((ret = __db_appname(env,
475 DB_APP_DATA, array[last], NULL, &real_name)) != 0)
478 /* If the file doesn't exist, ignore it. */
479 if (__os_exists(env, real_name, NULL) != 0) {
480 __os_free(env, real_name);
481 __os_free(env, array[last]);
486 /* Rework the name as requested by the user. */
487 __os_free(env, array[last]);
490 ret = __absname(env, pref, real_name, &array[last]);
491 __os_free(env, real_name);
494 } else if ((p = __db_rpath(real_name)) != NULL) {
495 ret = __os_strdup(env, p + 1, &array[last]);
496 __os_free(env, real_name);
500 array[last] = real_name;
504 /* NULL-terminate the list. */
507 /* Rework the memory. */
508 if ((ret = __usermem(env, &array)) != 0)
516 * We've possibly inserted NULLs into the array list, so clean up a
517 * bit so that the other error processing works.
520 for (; nxt < n; ++nxt)
521 __os_free(env, array[nxt]);
524 err1: if (array != NULL) {
525 for (arrayp = array; *arrayp != NULL; ++arrayp)
526 __os_free(env, *arrayp);
527 __os_free(env, array);
534 * Return an absolute path name for the file.
537 __absname(env, pref, name, newnamep)
539 char *pref, *name, **newnamep;
541 size_t l_pref, l_name;
545 l_name = strlen(name);
546 isabspath = __os_abspath(name);
547 l_pref = isabspath ? 0 : strlen(pref);
549 /* Malloc space for concatenating the two. */
550 if ((ret = __os_malloc(env,
551 l_pref + l_name + 2, &newname)) != 0)
555 /* Build the name. If `name' is an absolute path, ignore any prefix. */
557 memcpy(newname, pref, l_pref);
558 if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
559 newname[l_pref++] = PATH_SEPARATOR[0];
561 memcpy(newname + l_pref, name, l_name + 1);
568 * Create a single chunk of memory that holds the returned information.
569 * If the user has their own malloc routine, use it.
572 __usermem(env, listp)
578 char **array, **arrayp, **orig, *strp;
580 /* Find out how much space we need. */
581 for (len = 0, orig = *listp; *orig != NULL; ++orig)
582 len += sizeof(char *) + strlen(*orig) + 1;
583 len += sizeof(char *);
585 /* Allocate it and set up the pointers. */
586 if ((ret = __os_umalloc(env, len, &array)) != 0)
589 strp = (char *)(array + (orig - *listp) + 1);
591 /* Copy the original information into the new memory. */
592 for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
594 memcpy(strp, *orig, len + 1);
598 __os_free(env, *orig);
601 /* NULL-terminate the list. */
604 __os_free(env, *listp);
614 return (strcmp(*((char * const *)p1), *((char * const *)p2)));