2 * See the file LICENSE for redistribution information.
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid[] = "@(#)log_archive.c 10.23 (Sleepycat) 8/23/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
24 #include "db_dispatch.h"
28 #include "common_ext.h"
30 static int absname __P((char *, char *, char **));
31 static int build_data __P((DB_LOG *, char *, char ***, void *(*)(size_t)));
32 static int cmpfunc __P((const void *, const void *));
33 static int usermem __P((char ***, void *(*)(size_t)));
37 * Supporting function for db_archive(1).
40 log_archive(logp, listp, flags, db_malloc)
44 void *(*db_malloc) __P((size_t));
49 int array_size, n, ret;
50 char **array, **arrayp, *name, *p, *pref, buf[MAXPATHLEN];
52 fnum = 0; /* XXX: Shut the compiler up. */
54 #define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)
57 __db_fchk(logp->dbenv, "log_archive", flags, OKFLAGS)) != 0)
60 __db_fcchk(logp->dbenv,
61 "log_archive", flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
66 * Get the absolute pathname of the current directory. It would
67 * be nice to get the shortest pathname of the database directory,
68 * but that's just not possible.
70 if (LF_ISSET(DB_ARCH_ABS)) {
72 if ((pref = getcwd(buf, sizeof(buf))) == NULL)
73 return (errno == 0 ? ENOMEM : errno);
77 switch (LF_ISSET(~DB_ARCH_ABS)) {
79 return (build_data(logp, pref, listp, db_malloc));
81 memset(&rec, 0, sizeof(rec));
82 if (F_ISSET(logp, DB_AM_THREAD))
83 F_SET(&rec, DB_DBT_MALLOC);
84 if ((ret = log_get(logp, &stable_lsn, &rec, DB_LAST)) != 0)
86 if (F_ISSET(logp, DB_AM_THREAD))
88 fnum = stable_lsn.file;
91 if ((ret = __log_findckp(logp, &stable_lsn)) != 0) {
92 if (ret != DB_NOTFOUND)
97 /* Remove any log files before the last stable LSN. */
98 fnum = stable_lsn.file - 1;
102 #define LIST_INCREMENT 64
103 /* Get some initial space. */
105 (char **)malloc(sizeof(char *) * (array_size = 10))) == NULL)
109 /* Build an array of the file names. */
110 for (n = 0; fnum > 0; --fnum) {
111 if ((ret = __log_name(logp->dbenv, fnum, &name)) != 0)
113 if (__db_exists(name, NULL) != 0)
116 if (n >= array_size - 1) {
117 array_size += LIST_INCREMENT;
118 if ((array = (char **)realloc(array,
119 sizeof(char *) * array_size)) == NULL) {
125 if (LF_ISSET(DB_ARCH_ABS)) {
126 if ((ret = absname(pref, name, &array[n])) != 0)
129 } else if ((p = __db_rpath(name)) != NULL) {
130 if ((array[n] = (char *)strdup(p + 1)) == NULL) {
141 /* If there's nothing to return, we're done. */
149 qsort(array, (size_t)n, sizeof(char *), cmpfunc);
151 /* Rework the memory. */
152 if ((ret = usermem(&array, db_malloc)) != 0)
158 err: if (array != NULL) {
159 for (arrayp = array; *arrayp != NULL; ++arrayp)
168 * Build a list of datafiles for return.
171 build_data(logp, pref, listp, db_malloc)
173 char *pref, ***listp;
174 void *(*db_malloc) __P((size_t));
178 __log_register_args *argp;
180 int array_size, last, n, nxt, ret;
181 char **array, **arrayp, *p, *real_name;
183 /* Get some initial space. */
185 (char **)malloc(sizeof(char *) * (array_size = 10))) == NULL)
189 memset(&rec, 0, sizeof(rec));
190 if (F_ISSET(logp, DB_AM_THREAD))
191 F_SET(&rec, DB_DBT_MALLOC);
192 for (n = 0, ret = log_get(logp, &lsn, &rec, DB_FIRST);
193 ret == 0; ret = log_get(logp, &lsn, &rec, DB_NEXT)) {
194 if (rec.size < sizeof(rectype)) {
196 __db_err(logp->dbenv, "log_archive: bad log record");
200 memcpy(&rectype, rec.data, sizeof(rectype));
201 if (rectype != DB_log_register) {
202 if (F_ISSET(logp, DB_AM_THREAD)) {
208 if ((ret = __log_register_read(rec.data, &argp)) != 0) {
210 __db_err(logp->dbenv,
211 "log_archive: unable to read log record");
215 if (n >= array_size - 1) {
216 array_size += LIST_INCREMENT;
217 if ((array = (char **)realloc(array,
218 sizeof(char *) * array_size)) == NULL) {
224 if ((array[n] = (char *)strdup(argp->name.data)) == NULL) {
226 lg_free: if (F_ISSET(&rec, DB_DBT_MALLOC) && rec.data != NULL)
234 if (F_ISSET(logp, DB_AM_THREAD)) {
240 /* If there's nothing to return, we're done. */
248 qsort(array, (size_t)n, sizeof(char *), cmpfunc);
251 * Build the real pathnames, discarding nonexistent files and
254 for (last = nxt = 0; nxt < n;) {
256 * Discard duplicates. Last is the next slot we're going
257 * to return to the user, nxt is the next slot that we're
261 array[last] = array[nxt];
264 for (++nxt; nxt < n &&
265 strcmp(array[last], array[nxt]) == 0; ++nxt) {
270 /* Get the real name. */
271 if ((ret = __db_appname(logp->dbenv,
272 DB_APP_DATA, NULL, array[last], NULL, &real_name)) != 0)
275 /* If the file doesn't exist, ignore it. */
276 if (__db_exists(real_name, NULL) != 0) {
283 /* Rework the name as requested by the user. */
287 ret = absname(pref, real_name, &array[last]);
291 } else if ((p = __db_rpath(real_name)) != NULL) {
292 array[last] = (char *)strdup(p + 1);
294 if (array[last] == NULL)
297 array[last] = real_name;
301 /* NULL-terminate the list. */
304 /* Rework the memory. */
305 if ((ret = usermem(&array, db_malloc)) != 0)
313 * We've possibly inserted NULLs into the array list, so clean up a
314 * bit so that the other error processing works.
317 for (; nxt < n; ++nxt)
321 err1: if (array != NULL) {
322 for (arrayp = array; *arrayp != NULL; ++arrayp)
331 * Return an absolute path name for the file.
334 absname(pref, name, newnamep)
335 char *pref, *name, **newnamep;
337 size_t l_pref, l_name;
340 l_pref = strlen(pref);
341 l_name = strlen(name);
343 /* Malloc space for concatenating the two. */
344 if ((newname = (char *)malloc(l_pref + l_name + 2)) == NULL)
347 /* Build the name. */
348 memcpy(newname, pref, l_pref);
349 if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
350 newname[l_pref++] = PATH_SEPARATOR[0];
351 memcpy(newname + l_pref, name, l_name + 1);
359 * Create a single chunk of memory that holds the returned information.
360 * If the user has their own malloc routine, use it.
365 void *(*func) __P((size_t));
368 char **array, **arrayp, **orig, *strp;
370 /* Find out how much space we need. */
371 for (len = 0, orig = *listp; *orig != NULL; ++orig)
372 len += sizeof(char *) + strlen(*orig) + 1;
373 len += sizeof(char *);
376 * Allocate it and set up the pointers.
379 * Don't simplify this expression, SunOS compilers don't like it.
382 array = (char **)malloc(len);
384 array = (char **)func(len);
387 strp = (char *)(array + (orig - *listp) + 1);
389 /* Copy the original information into the new memory. */
390 for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
392 memcpy(strp, *orig, len + 1);
399 /* NULL-terminate the list. */
412 return (strcmp(*((char **)p1), *((char **)p2)));