1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)log_archive.c 10.44 (Sleepycat) 10/9/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <unistd.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_dispatch.h"
1N/A#include "shqueue.h"
1N/A#include "log.h"
1N/A#include "common_ext.h"
1N/A#include "clib_ext.h" /* XXX: needed for getcwd. */
1N/A
1N/Astatic int __absname __P((char *, char *, char **));
1N/Astatic int __build_data __P((DB_LOG *, char *, char ***, void *(*)(size_t)));
1N/Astatic int __cmpfunc __P((const void *, const void *));
1N/Astatic int __usermem __P((char ***, void *(*)(size_t)));
1N/A
1N/A/*
1N/A * log_archive --
1N/A * Supporting function for db_archive(1).
1N/A */
1N/Aint
1N/Alog_archive(dblp, listp, flags, db_malloc)
1N/A DB_LOG *dblp;
1N/A char ***listp;
1N/A u_int32_t flags;
1N/A void *(*db_malloc) __P((size_t));
1N/A{
1N/A DBT rec;
1N/A DB_LSN stable_lsn;
1N/A u_int32_t fnum;
1N/A int array_size, n, ret;
1N/A char **array, **arrayp, *name, *p, *pref, buf[MAXPATHLEN];
1N/A
1N/A name = NULL;
1N/A COMPQUIET(fnum, 0);
1N/A
1N/A LOG_PANIC_CHECK(dblp);
1N/A
1N/A#define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)
1N/A if (flags != 0) {
1N/A if ((ret =
1N/A __db_fchk(dblp->dbenv, "log_archive", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A if ((ret =
1N/A __db_fcchk(dblp->dbenv,
1N/A "log_archive", flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Get the absolute pathname of the current directory. It would
1N/A * be nice to get the shortest pathname of the database directory,
1N/A * but that's just not possible.
1N/A */
1N/A if (LF_ISSET(DB_ARCH_ABS)) {
1N/A errno = 0;
1N/A if ((pref = getcwd(buf, sizeof(buf))) == NULL)
1N/A return (errno == 0 ? ENOMEM : errno);
1N/A } else
1N/A pref = NULL;
1N/A
1N/A switch (LF_ISSET(~DB_ARCH_ABS)) {
1N/A case DB_ARCH_DATA:
1N/A return (__build_data(dblp, pref, listp, db_malloc));
1N/A case DB_ARCH_LOG:
1N/A memset(&rec, 0, sizeof(rec));
1N/A if (F_ISSET(dblp, DB_AM_THREAD))
1N/A F_SET(&rec, DB_DBT_MALLOC);
1N/A if ((ret = log_get(dblp, &stable_lsn, &rec, DB_LAST)) != 0)
1N/A return (ret);
1N/A if (F_ISSET(dblp, DB_AM_THREAD))
1N/A __os_free(rec.data, rec.size);
1N/A fnum = stable_lsn.file;
1N/A break;
1N/A case 0:
1N/A if ((ret = __log_findckp(dblp, &stable_lsn)) != 0) {
1N/A /*
1N/A * A return of DB_NOTFOUND means that we didn't find
1N/A * any records in the log (so we are not going to be
1N/A * deleting any log files).
1N/A */
1N/A if (ret != DB_NOTFOUND)
1N/A return (ret);
1N/A *listp = NULL;
1N/A return (0);
1N/A }
1N/A /* Remove any log files before the last stable LSN. */
1N/A fnum = stable_lsn.file - 1;
1N/A break;
1N/A }
1N/A
1N/A#define LIST_INCREMENT 64
1N/A /* Get some initial space. */
1N/A array_size = 10;
1N/A if ((ret = __os_malloc(sizeof(char *) * array_size, NULL, &array)) != 0)
1N/A return (ret);
1N/A array[0] = NULL;
1N/A
1N/A /* Build an array of the file names. */
1N/A for (n = 0; fnum > 0; --fnum) {
1N/A if ((ret = __log_name(dblp, fnum, &name, NULL, 0)) != 0)
1N/A goto err;
1N/A if (__os_exists(name, NULL) != 0) {
1N/A __os_freestr(name);
1N/A name = NULL;
1N/A break;
1N/A }
1N/A
1N/A if (n >= array_size - 1) {
1N/A array_size += LIST_INCREMENT;
1N/A if ((ret = __os_realloc(&array,
1N/A sizeof(char *) * array_size)) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A if (LF_ISSET(DB_ARCH_ABS)) {
1N/A if ((ret = __absname(pref, name, &array[n])) != 0)
1N/A goto err;
1N/A __os_freestr(name);
1N/A } else if ((p = __db_rpath(name)) != NULL) {
1N/A if ((ret = __os_strdup(p + 1, &array[n])) != 0)
1N/A goto err;
1N/A __os_freestr(name);
1N/A } else
1N/A array[n] = name;
1N/A
1N/A name = NULL;
1N/A array[++n] = NULL;
1N/A }
1N/A
1N/A /* If there's nothing to return, we're done. */
1N/A if (n == 0) {
1N/A *listp = NULL;
1N/A ret = 0;
1N/A goto err;
1N/A }
1N/A
1N/A /* Sort the list. */
1N/A qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
1N/A
1N/A /* Rework the memory. */
1N/A if ((ret = __usermem(&array, db_malloc)) != 0)
1N/A goto err;
1N/A
1N/A *listp = array;
1N/A return (0);
1N/A
1N/Aerr: if (array != NULL) {
1N/A for (arrayp = array; *arrayp != NULL; ++arrayp)
1N/A __os_freestr(*arrayp);
1N/A __os_free(array, sizeof(char *) * array_size);
1N/A }
1N/A if (name != NULL)
1N/A __os_freestr(name);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __build_data --
1N/A * Build a list of datafiles for return.
1N/A */
1N/Astatic int
1N/A__build_data(dblp, pref, listp, db_malloc)
1N/A DB_LOG *dblp;
1N/A char *pref, ***listp;
1N/A void *(*db_malloc) __P((size_t));
1N/A{
1N/A DBT rec;
1N/A DB_LSN lsn;
1N/A __log_register_args *argp;
1N/A u_int32_t rectype;
1N/A int array_size, last, n, nxt, ret;
1N/A char **array, **arrayp, *p, *real_name;
1N/A
1N/A /* Get some initial space. */
1N/A array_size = 10;
1N/A if ((ret = __os_malloc(sizeof(char *) * array_size, NULL, &array)) != 0)
1N/A return (ret);
1N/A array[0] = NULL;
1N/A
1N/A memset(&rec, 0, sizeof(rec));
1N/A if (F_ISSET(dblp, DB_AM_THREAD))
1N/A F_SET(&rec, DB_DBT_MALLOC);
1N/A for (n = 0, ret = log_get(dblp, &lsn, &rec, DB_FIRST);
1N/A ret == 0; ret = log_get(dblp, &lsn, &rec, DB_NEXT)) {
1N/A if (rec.size < sizeof(rectype)) {
1N/A ret = EINVAL;
1N/A __db_err(dblp->dbenv, "log_archive: bad log record");
1N/A goto lg_free;
1N/A }
1N/A
1N/A memcpy(&rectype, rec.data, sizeof(rectype));
1N/A if (rectype != DB_log_register) {
1N/A if (F_ISSET(dblp, DB_AM_THREAD)) {
1N/A __os_free(rec.data, rec.size);
1N/A rec.data = NULL;
1N/A }
1N/A continue;
1N/A }
1N/A if ((ret = __log_register_read(rec.data, &argp)) != 0) {
1N/A ret = EINVAL;
1N/A __db_err(dblp->dbenv,
1N/A "log_archive: unable to read log record");
1N/A goto lg_free;
1N/A }
1N/A
1N/A if (n >= array_size - 1) {
1N/A array_size += LIST_INCREMENT;
1N/A if ((ret = __os_realloc(&array,
1N/A sizeof(char *) * array_size)) != 0)
1N/A goto lg_free;
1N/A }
1N/A
1N/A if ((ret = __os_strdup(argp->name.data, &array[n])) != 0) {
1N/Alg_free: if (F_ISSET(&rec, DB_DBT_MALLOC) && rec.data != NULL)
1N/A __os_free(rec.data, rec.size);
1N/A goto err1;
1N/A }
1N/A
1N/A array[++n] = NULL;
1N/A __os_free(argp, 0);
1N/A
1N/A if (F_ISSET(dblp, DB_AM_THREAD)) {
1N/A __os_free(rec.data, rec.size);
1N/A rec.data = NULL;
1N/A }
1N/A }
1N/A
1N/A /* If there's nothing to return, we're done. */
1N/A if (n == 0) {
1N/A ret = 0;
1N/A *listp = NULL;
1N/A goto err1;
1N/A }
1N/A
1N/A /* Sort the list. */
1N/A qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
1N/A
1N/A /*
1N/A * Build the real pathnames, discarding nonexistent files and
1N/A * duplicates.
1N/A */
1N/A for (last = nxt = 0; nxt < n;) {
1N/A /*
1N/A * Discard duplicates. Last is the next slot we're going
1N/A * to return to the user, nxt is the next slot that we're
1N/A * going to consider.
1N/A */
1N/A if (last != nxt) {
1N/A array[last] = array[nxt];
1N/A array[nxt] = NULL;
1N/A }
1N/A for (++nxt; nxt < n &&
1N/A strcmp(array[last], array[nxt]) == 0; ++nxt) {
1N/A __os_freestr(array[nxt]);
1N/A array[nxt] = NULL;
1N/A }
1N/A
1N/A /* Get the real name. */
1N/A if ((ret = __db_appname(dblp->dbenv,
1N/A DB_APP_DATA, NULL, array[last], 0, NULL, &real_name)) != 0)
1N/A goto err2;
1N/A
1N/A /* If the file doesn't exist, ignore it. */
1N/A if (__os_exists(real_name, NULL) != 0) {
1N/A __os_freestr(real_name);
1N/A __os_freestr(array[last]);
1N/A array[last] = NULL;
1N/A continue;
1N/A }
1N/A
1N/A /* Rework the name as requested by the user. */
1N/A __os_freestr(array[last]);
1N/A array[last] = NULL;
1N/A if (pref != NULL) {
1N/A ret = __absname(pref, real_name, &array[last]);
1N/A __os_freestr(real_name);
1N/A if (ret != 0)
1N/A goto err2;
1N/A } else if ((p = __db_rpath(real_name)) != NULL) {
1N/A ret = __os_strdup(p + 1, &array[last]);
1N/A __os_freestr(real_name);
1N/A if (ret != 0)
1N/A goto err2;
1N/A } else
1N/A array[last] = real_name;
1N/A ++last;
1N/A }
1N/A
1N/A /* NULL-terminate the list. */
1N/A array[last] = NULL;
1N/A
1N/A /* Rework the memory. */
1N/A if ((ret = __usermem(&array, db_malloc)) != 0)
1N/A goto err1;
1N/A
1N/A *listp = array;
1N/A return (0);
1N/A
1N/Aerr2: /*
1N/A * XXX
1N/A * We've possibly inserted NULLs into the array list, so clean up a
1N/A * bit so that the other error processing works.
1N/A */
1N/A if (array != NULL)
1N/A for (; nxt < n; ++nxt)
1N/A __os_freestr(array[nxt]);
1N/A /* FALLTHROUGH */
1N/A
1N/Aerr1: if (array != NULL) {
1N/A for (arrayp = array; *arrayp != NULL; ++arrayp)
1N/A __os_freestr(*arrayp);
1N/A __os_free(array, array_size * sizeof(char *));
1N/A }
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __absname --
1N/A * Return an absolute path name for the file.
1N/A */
1N/Astatic int
1N/A__absname(pref, name, newnamep)
1N/A char *pref, *name, **newnamep;
1N/A{
1N/A size_t l_pref, l_name;
1N/A int isabspath, ret;
1N/A char *newname;
1N/A
1N/A l_name = strlen(name);
1N/A isabspath = __os_abspath(name);
1N/A l_pref = isabspath ? 0 : strlen(pref);
1N/A
1N/A /* Malloc space for concatenating the two. */
1N/A if ((ret = __os_malloc(l_pref + l_name + 2, NULL, &newname)) != 0)
1N/A return (ret);
1N/A *newnamep = newname;
1N/A
1N/A /* Build the name. If `name' is an absolute path, ignore any prefix. */
1N/A if (!isabspath) {
1N/A memcpy(newname, pref, l_pref);
1N/A if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
1N/A newname[l_pref++] = PATH_SEPARATOR[0];
1N/A }
1N/A memcpy(newname + l_pref, name, l_name + 1);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __usermem --
1N/A * Create a single chunk of memory that holds the returned information.
1N/A * If the user has their own malloc routine, use it.
1N/A */
1N/Astatic int
1N/A__usermem(listp, db_malloc)
1N/A char ***listp;
1N/A void *(*db_malloc) __P((size_t));
1N/A{
1N/A size_t len;
1N/A int ret;
1N/A char **array, **arrayp, **orig, *strp;
1N/A
1N/A /* Find out how much space we need. */
1N/A for (len = 0, orig = *listp; *orig != NULL; ++orig)
1N/A len += sizeof(char *) + strlen(*orig) + 1;
1N/A len += sizeof(char *);
1N/A
1N/A /* Allocate it and set up the pointers. */
1N/A if ((ret = __os_malloc(len, db_malloc, &array)) != 0)
1N/A return (ret);
1N/A
1N/A strp = (char *)(array + (orig - *listp) + 1);
1N/A
1N/A /* Copy the original information into the new memory. */
1N/A for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
1N/A len = strlen(*orig);
1N/A memcpy(strp, *orig, len + 1);
1N/A *arrayp = strp;
1N/A strp += len + 1;
1N/A
1N/A __os_freestr(*orig);
1N/A }
1N/A
1N/A /* NULL-terminate the list. */
1N/A *arrayp = NULL;
1N/A
1N/A __os_free(*listp, 0);
1N/A *listp = array;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/Astatic int
1N/A__cmpfunc(p1, p2)
1N/A const void *p1, *p2;
1N/A{
1N/A return (strcmp(*((char * const *)p1), *((char * const *)p2)));
1N/A}