2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id: log_archive.c,v 11.39 2002/08/06 05:00:31 bostic Exp $";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
23
#include "dbinc/db_page.h"
24
#include "dbinc/log.h"
25
#include "dbinc/qam.h"
26
#include "dbinc/txn.h"
28
static int __absname __P((DB_ENV *, char *, char *, char **));
29
static int __build_data __P((DB_ENV *, char *, char ***));
30
static int __cmpfunc __P((const void *, const void *));
31
static int __usermem __P((DB_ENV *, char ***));
35
* Supporting function for db_archive(1).
37
* PUBLIC: int __log_archive __P((DB_ENV *, char **[], u_int32_t));
40
__log_archive(dbenv, listp, flags)
49
__txn_ckp_args *ckp_args;
50
char **array, **arrayp, *name, *p, *pref, buf[MAXPATHLEN];
51
int array_size, db_arch_abs, n, ret;
55
ENV_REQUIRES_CONFIG(dbenv,
56
dbenv->lg_handle, "DB_ENV->log_archive", DB_INIT_LOG);
59
dblp = dbenv->lg_handle;
62
#define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)
65
dbenv, "DB_ENV->log_archive", flags, OKFLAGS)) != 0)
67
if ((ret = __db_fcchk(dbenv, "DB_ENV->log_archive",
68
flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
72
if (LF_ISSET(DB_ARCH_ABS)) {
78
if (flags == 0 || flags == DB_ARCH_DATA)
79
ENV_REQUIRES_CONFIG(dbenv,
80
dbenv->tx_handle, "DB_ENV->log_archive", DB_INIT_TXN);
83
* Get the absolute pathname of the current directory. It would
84
* be nice to get the shortest pathname of the database directory,
85
* but that's just not possible.
88
* Can't trust getcwd(3) to set a valid errno. If it doesn't, just
89
* guess that we ran out of memory.
93
if ((pref = getcwd(buf, sizeof(buf))) == NULL) {
94
if (__os_get_errno() == 0)
95
__os_set_errno(ENOMEM);
96
return (__os_get_errno());
103
return (__build_data(dbenv, pref, listp));
105
memset(&rec, 0, sizeof(rec));
106
if ((ret = dbenv->log_cursor(dbenv, &logc, 0)) != 0)
109
ZERO_LSN(stable_lsn);
111
ret = logc->get(logc, &stable_lsn, &rec, DB_LAST);
112
(void)logc->close(logc, 0);
115
fnum = stable_lsn.file;
118
memset(&rec, 0, sizeof(rec));
119
if (__txn_getckp(dbenv, &stable_lsn) != 0) {
121
* A failure return means that there's no checkpoint
122
* in the log (so we are not going to be deleting
128
if ((ret = dbenv->log_cursor(dbenv, &logc, 0)) != 0)
130
if ((ret = logc->get(logc, &stable_lsn, &rec, DB_SET)) != 0 ||
131
(ret = __txn_ckp_read(dbenv, rec.data, &ckp_args)) != 0) {
133
* A return of DB_NOTFOUND may only mean that the
134
* checkpoint LSN is before the beginning of the
135
* log files that we still have. This is not
136
* an error; it just means our work is done.
138
if (ret == DB_NOTFOUND) {
142
(void)logc->close(logc, 0);
145
if ((ret = logc->close(logc, 0)) != 0)
147
stable_lsn = ckp_args->ckp_lsn;
148
__os_free(dbenv, ckp_args);
150
/* Remove any log files before the last stable LSN. */
151
fnum = stable_lsn.file - 1;
155
#define LIST_INCREMENT 64
156
/* Get some initial space. */
158
if ((ret = __os_malloc(dbenv,
159
sizeof(char *) * array_size, &array)) != 0)
163
/* Build an array of the file names. */
164
for (n = 0; fnum > 0; --fnum) {
165
if ((ret = __log_name(dblp, fnum, &name, NULL, 0)) != 0)
167
if (__os_exists(name, NULL) != 0) {
168
if (LF_ISSET(DB_ARCH_LOG) && fnum == stable_lsn.file)
170
__os_free(dbenv, name);
175
if (n >= array_size - 2) {
176
array_size += LIST_INCREMENT;
177
if ((ret = __os_realloc(dbenv,
178
sizeof(char *) * array_size, &array)) != 0)
183
if ((ret = __absname(dbenv,
184
pref, name, &array[n])) != 0)
186
__os_free(dbenv, name);
187
} else if ((p = __db_rpath(name)) != NULL) {
188
if ((ret = __os_strdup(dbenv, p + 1, &array[n])) != 0)
190
__os_free(dbenv, name);
198
/* If there's nothing to return, we're done. */
206
qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
208
/* Rework the memory. */
209
if ((ret = __usermem(dbenv, &array)) != 0)
215
err: if (array != NULL) {
216
for (arrayp = array; *arrayp != NULL; ++arrayp)
217
__os_free(dbenv, *arrayp);
218
__os_free(dbenv, array);
221
__os_free(dbenv, name);
227
* Build a list of datafiles for return.
230
__build_data(dbenv, pref, listp)
232
char *pref, ***listp;
237
__dbreg_register_args *argp;
239
int array_size, last, n, nxt, ret, t_ret;
240
char **array, **arrayp, **list, **lp, *p, *real_name;
242
/* Get some initial space. */
244
if ((ret = __os_malloc(dbenv,
245
sizeof(char *) * array_size, &array)) != 0)
249
memset(&rec, 0, sizeof(rec));
250
if ((ret = dbenv->log_cursor(dbenv, &logc, 0)) != 0)
252
for (n = 0; (ret = logc->get(logc, &lsn, &rec, DB_PREV)) == 0;) {
253
if (rec.size < sizeof(rectype)) {
255
__db_err(dbenv, "DB_ENV->log_archive: bad log record");
259
memcpy(&rectype, rec.data, sizeof(rectype));
260
if (rectype != DB___dbreg_register)
263
__dbreg_register_read(dbenv, rec.data, &argp)) != 0) {
266
"DB_ENV->log_archive: unable to read log record");
270
if (n >= array_size - 2) {
271
array_size += LIST_INCREMENT;
272
if ((ret = __os_realloc(dbenv,
273
sizeof(char *) * array_size, &array)) != 0)
277
if ((ret = __os_strdup(dbenv,
278
argp->name.data, &array[n++])) != 0)
282
if (argp->ftype == DB_QUEUE) {
283
if ((ret = __qam_extent_names(dbenv,
284
argp->name.data, &list)) != 0)
287
lp != NULL && *lp != NULL; lp++) {
288
if (n >= array_size - 2) {
289
array_size += LIST_INCREMENT;
290
if ((ret = __os_realloc(dbenv,
292
array_size, &array)) != 0)
296
__os_strdup(dbenv, *lp, &array[n++])) != 0)
300
q_err: if (list != NULL)
301
__os_free(dbenv, list);
303
free_continue: __os_free(dbenv, argp);
307
if (ret == DB_NOTFOUND)
309
if ((t_ret = logc->close(logc, 0)) != 0 && ret == 0)
314
/* If there's nothing to return, we're done. */
322
qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
325
* Build the real pathnames, discarding nonexistent files and
328
for (last = nxt = 0; nxt < n;) {
330
* Discard duplicates. Last is the next slot we're going
331
* to return to the user, nxt is the next slot that we're
335
array[last] = array[nxt];
338
for (++nxt; nxt < n &&
339
strcmp(array[last], array[nxt]) == 0; ++nxt) {
340
__os_free(dbenv, array[nxt]);
344
/* Get the real name. */
345
if ((ret = __db_appname(dbenv,
346
DB_APP_DATA, array[last], 0, NULL, &real_name)) != 0)
349
/* If the file doesn't exist, ignore it. */
350
if (__os_exists(real_name, NULL) != 0) {
351
__os_free(dbenv, real_name);
352
__os_free(dbenv, array[last]);
357
/* Rework the name as requested by the user. */
358
__os_free(dbenv, array[last]);
361
ret = __absname(dbenv, pref, real_name, &array[last]);
362
__os_free(dbenv, real_name);
365
} else if ((p = __db_rpath(real_name)) != NULL) {
366
ret = __os_strdup(dbenv, p + 1, &array[last]);
367
__os_free(dbenv, real_name);
371
array[last] = real_name;
375
/* NULL-terminate the list. */
378
/* Rework the memory. */
379
if ((ret = __usermem(dbenv, &array)) != 0)
387
* We've possibly inserted NULLs into the array list, so clean up a
388
* bit so that the other error processing works.
391
for (; nxt < n; ++nxt)
392
__os_free(dbenv, array[nxt]);
395
err1: if (array != NULL) {
396
for (arrayp = array; *arrayp != NULL; ++arrayp)
397
__os_free(dbenv, *arrayp);
398
__os_free(dbenv, array);
405
* Return an absolute path name for the file.
408
__absname(dbenv, pref, name, newnamep)
410
char *pref, *name, **newnamep;
412
size_t l_pref, l_name;
416
l_name = strlen(name);
417
isabspath = __os_abspath(name);
418
l_pref = isabspath ? 0 : strlen(pref);
420
/* Malloc space for concatenating the two. */
421
if ((ret = __os_malloc(dbenv,
422
l_pref + l_name + 2, &newname)) != 0)
426
/* Build the name. If `name' is an absolute path, ignore any prefix. */
428
memcpy(newname, pref, l_pref);
429
if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
430
newname[l_pref++] = PATH_SEPARATOR[0];
432
memcpy(newname + l_pref, name, l_name + 1);
439
* Create a single chunk of memory that holds the returned information.
440
* If the user has their own malloc routine, use it.
443
__usermem(dbenv, listp)
449
char **array, **arrayp, **orig, *strp;
451
/* Find out how much space we need. */
452
for (len = 0, orig = *listp; *orig != NULL; ++orig)
453
len += sizeof(char *) + strlen(*orig) + 1;
454
len += sizeof(char *);
456
/* Allocate it and set up the pointers. */
457
if ((ret = __os_umalloc(dbenv, len, &array)) != 0)
460
strp = (char *)(array + (orig - *listp) + 1);
462
/* Copy the original information into the new memory. */
463
for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
465
memcpy(strp, *orig, len + 1);
469
__os_free(dbenv, *orig);
472
/* NULL-terminate the list. */
475
__os_free(dbenv, *listp);
485
return (strcmp(*((char * const *)p1), *((char * const *)p2)));