db_hotbackup.c

来自「关于Berkelay数据库的共享源码」· C语言 代码 · 共 840 行 · 第 1/2 页

C
840
字号
		    DB_INIT_LOG | DB_INIT_TXN | DB_PRIVATE | DB_USE_ENVIRON,		    0)) != 0)) {			dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);			return (1);		}		if (log_dir != NULL && *log_dir == NULL) {			(void)dbenv->get_lg_dir(dbenv, log_dir);			if (*log_dir == NULL)				*log_dir = home;		}		if (data_dir != NULL && *data_dir == NULL)			(void)dbenv->get_data_dirs(dbenv, data_dir);		break;	case OPEN_HOT_BACKUP:		/*		 * Opening the backup copy of the database environment.  We		 * better be the only user, we're running recovery.		 */		if ((ret = dbenv->open(dbenv, home, DB_CREATE |		    DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE |		    DB_RECOVER_FATAL | DB_USE_ENVIRON, 0)) != 0) {			dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);			return (1);		}		break;	}	*dbenvp = dbenv;	return (0);}/* * backup_dir_clean -- *	Clean out the backup directory. */intdb_hotbackup_backup_dir_clean(dbenv, backup_dir, remove_maxp, update, verbose)	DB_ENV *dbenv;	char *backup_dir;	int *remove_maxp, update, verbose;{	int cnt, fcnt, ret, v;	char **names, buf[DB_MAXPATHLEN];	/* Get a list of file names. */	if ((ret = __os_dirlist(dbenv, backup_dir, &names, &fcnt)) != 0) {		dbenv->err(dbenv, ret, "%s: directory read", backup_dir);		return (1);	}	for (cnt = fcnt; --cnt >= 0;) {		/*		 * Skip log files (if update wasn't specified).		 */		if (strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1)) {			if (update)				continue;		} else {			/* Track the highest-numbered log file removed. */			v = atoi(names[cnt] + sizeof(LFPREFIX) - 1);			if (*remove_maxp < v)				*remove_maxp = v;		}		if ((size_t)snprintf(buf, sizeof(buf),		    "%s/%s", backup_dir, names[cnt]) >= sizeof(buf)) {			dbenv->err(dbenv, ret,			    "%s/%s: path too long", backup_dir, names[cnt]);			return (1);		}		if (verbose)			printf("%s: removing %s\n", progname, buf);		if ((ret = remove(buf)) != 0) {			dbenv->err(dbenv, ret, "%s: remove", buf);			return (1);		}	}	__os_dirfree(dbenv, names, fcnt);	if (verbose && *remove_maxp != 0)		printf("%s: highest numbered log file removed: %d\n",		    progname, *remove_maxp);	return (0);}/* * read_data_dir -- *	Read a directory looking for databases to copy. */intdb_hotbackup_read_data_dir(dbenv, home, backup_dir, dir, verbose, db_config)	DB_ENV *dbenv;	char *home, *backup_dir;	const char *dir;	int verbose, db_config;{	int cnt, fcnt, ret;	char *bd, **names;	char buf[DB_MAXPATHLEN], bbuf[DB_MAXPATHLEN];	bd = backup_dir;	if (db_config && dir != home) {		/* Build a path name to the destination. */		if ((size_t)(cnt = snprintf(bbuf, sizeof(bbuf),		      "%s/%s/", backup_dir, dir)) >= sizeof(buf)) {			dbenv->errx(dbenv,			     "%s/%s: path too long", backup_dir, dir);			return (1);		}		bd = bbuf;		/* Create the path. */		if ((ret = dbenv->set_intermediate_dir(		    dbenv, __db_omode("rwx------"), 0)) != 0 ||		    (ret = __db_mkpath(dbenv, bd)) != 0) {			dbenv->err(dbenv, ret, "%s: cannot create", bd);			return (1);		}		/* step on the trailing '/' */		bd[cnt] = '\0';		/* Build a path name to the source. */		if ((size_t)snprintf(buf, sizeof(buf),		    "%s/%s", home, dir) >= sizeof(buf)) {			dbenv->errx(dbenv,			    "%s/%s: path too long", home, dir);			return (1);		}		dir = buf;	}	/* Get a list of file names. */	if ((ret = __os_dirlist(dbenv, dir, &names, &fcnt)) != 0) {		dbenv->err(dbenv, ret, "%s: directory read", dir);		return (1);	}	for (cnt = fcnt; --cnt >= 0;) {		/*		 * Skip files in DB's name space (but not Queue		 * extent files, we need them).		 */		if (!strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1))			continue;		if (!strncmp(names[cnt],		    DB_REGION_PREFIX, sizeof(DB_REGION_PREFIX) - 1) &&		    strncmp(names[cnt],		    QUEUE_EXTENT_PREFIX, sizeof(QUEUE_EXTENT_PREFIX) - 1))			continue;		/*		 * Skip DB_CONFIG.		 */		if (!db_config &&		     !strncmp(names[cnt], "DB_CONFIG", sizeof("DB_CONFIG")))			continue;		/* Copy the file. */		if ((ret = db_hotbackup_data_copy(		    dbenv, names[cnt], dir, bd, verbose)) != 0)			return (1);	}	__os_dirfree(dbenv, names, fcnt);	return (0);}/* * read_log_dir -- * *	Read a directory looking for log files to copy.  If home * is passed then we are possibly using a log dir in the destination, * following DB_CONFIG configuration. */intdb_hotbackup_read_log_dir(dbenv, home, backup_dir, log_dir, copy_minp, update, verbose)	DB_ENV *dbenv;	char *home, *backup_dir;	const char *log_dir;	int *copy_minp, update, verbose;{	u_int32_t aflag;	int cnt, ret, v;	char **begin, **names, *backupd;	const char *logd;	char from[DB_MAXPATHLEN], to[DB_MAXPATHLEN];	logd = log_dir;	backupd = backup_dir;	if (home != NULL && log_dir != home) {		if ((size_t)snprintf(from, sizeof(from),		    "%s/%s", home, log_dir) >= sizeof(from)) {			dbenv->errx(dbenv,			    "%s/%s: path too long", home, log_dir);			return (1);		}		logd = strdup(from);		if ((size_t)(cnt = snprintf(to, sizeof(to),		    "%s/%s/", backup_dir, log_dir)) >= sizeof(to)) {			dbenv->errx(dbenv,			    "%s/%s: path too long", backup_dir, log_dir);			return (1);		}		backupd = strdup(to);		/* Create the path. */		if ((ret = dbenv->set_intermediate_dir(		    dbenv, __db_omode("rwx------"), 0)) != 0 ||		    (ret = __db_mkpath(dbenv, backupd)) != 0) {			dbenv->err(dbenv, ret, "%s: cannot create", backupd);			return (1);		}		/* step on the trailing '/' */		backupd[cnt] = '\0';	}again:	aflag = DB_ARCH_LOG;	/*	 * If this is an update and we are deleting files, first process	 * those files that can be removed, then repeat with the rest.	 */	if (update)		aflag = 0;	/* Get a list of file names to be copied. */	if ((ret = dbenv->log_archive(dbenv, &names, aflag)) != 0) {		dbenv->err(dbenv, ret, "%s: log_archive", log_dir);		return (1);	}	if (names == NULL)		goto done;	begin = names;	for (; *names != NULL; names++) {		/* Track the lowest-numbered log file copied. */		v = atoi(*names + sizeof(LFPREFIX) - 1);		if (*copy_minp == 0 || *copy_minp > v)			*copy_minp = v;		if ((size_t)snprintf(from, sizeof(from),		    "%s/%s", logd, *names) >= sizeof(from)) {			dbenv->errx(dbenv,			    "%s/%s: path too long", log_dir, *names);			return (1);		}		/*		 * If we're going to remove the file, attempt to rename the		 * instead of copying and then removing.  The likely failure		 * is EXDEV (source and destination are on different volumes).		 * Fall back to a copy, regardless of the error.  We don't		 * worry about partial contents, the copy truncates the file		 * on open.		 */		if (update) {			if ((size_t)snprintf(to, sizeof(to),			    "%s/%s", backupd, *names) >= sizeof(to)) {				dbenv->errx(dbenv,				    "%s/%s: path too long", backupd, *names);				return (1);			}			if (rename(from, to) == 0) {				if (verbose)					printf("%s: moving %s to %s\n",					   progname, from, to);				continue;			}		}		/* Copy the file. */		if ((ret = db_hotbackup_data_copy(dbenv,		    *names, logd, backupd, verbose)) != 0)			return (1);		if (update) {			if (verbose)				printf("%s: removing %s\n", progname, from);			if ((ret = __os_unlink(dbenv, from)) != 0) {				dbenv->err(dbenv, ret,				     "unlink of %s failed", from);				return (1);			}		}	}	free(begin);done:	if (update) {		update = 0;		goto again;	}	if (verbose && *copy_minp != 0)		printf("%s: lowest numbered log file copied: %d\n",		    progname, *copy_minp);	if (logd != log_dir)		free((char *)logd);	if (backupd != backup_dir)		free(backupd);	return (0);}/* * data_copy -- *	Copy a file into the backup directory. */intdb_hotbackup_data_copy(dbenv, file, from_dir, to_dir, verbose)	DB_ENV *dbenv;	const char *file, *from_dir, *to_dir;	int verbose;{	ssize_t nr, nw;	size_t offset;	int ret, rfd, wfd;	char *buf, *taddr;	ret = 0;	rfd = wfd = -1;	if (verbose)		printf("%s: copying %s/%s to %s/%s\n",		    progname, from_dir, file, to_dir, file);	/*	 * We MUST copy multiples of the page size, atomically, to ensure a	 * database page is not updated by another thread of control during	 * the copy.	 *	 * !!!	 * The current maximum page size for Berkeley DB is 64KB; we will have	 * to increase this value if the maximum page size is ever more than a	 * megabyte	 */	if ((buf = malloc(MEGABYTE)) == NULL) {		dbenv->err(dbenv,		    errno, "%lu buffer allocation", (u_long)MEGABYTE);		return (1);	}	/* Open the input file. */	if ((u_int32_t)snprintf(	    buf, MEGABYTE, "%s/%s", from_dir, file) >= MEGABYTE) {		dbenv->errx(dbenv, "%s/%s: path too long", from_dir, file);		goto err;	}	if ((rfd = open(buf, O_RDONLY, 0)) == -1) {		dbenv->err(dbenv, errno, "%s", buf);		goto err;	}	/* Open the output file. */	if ((u_int32_t)snprintf(	    buf, MEGABYTE, "%s/%s", to_dir, file) >= MEGABYTE) {		dbenv->errx(dbenv, "%s/%s: path too long", to_dir, file);		goto err;	}	if ((wfd = open(	    buf, O_CREAT | O_TRUNC | O_WRONLY, __db_omode(OWNER_RW))) == -1)		goto err;	/* Copy the data. */	while ((nr = read(rfd, buf, MEGABYTE)) > 0)		for (taddr = buf, offset = 0;		    offset < (size_t)nr; taddr += nw, offset += (size_t)nw) {			RETRY_CHK(((nw = write(wfd,			    taddr, (u_int)(nr - offset))) < 0 ? 1 : 0), ret);			if (ret != 0)				break;		}	if (nr == -1) {		dbenv->err(dbenv, errno, "%s/%s: read", from_dir, file);		goto err;	}	if (ret != 0) {		dbenv->err(dbenv, errno, "%s: write %s/%s", to_dir, file);		goto err;	}	if (0) {err:		ret = 1;	}	if (buf != NULL)		free(buf);	if (rfd != -1)		(void)close(rfd);	/* We may be running on a remote filesystem; force the flush. */	if (wfd != -1 && (fsync(wfd) != 0 || close(wfd) != 0)) {		dbenv->err(dbenv,		    errno, "%s: fsync %s/%s", to_dir, file);		ret = 1;	}	return (ret);}intdb_hotbackup_usage(){	(void)fprintf(stderr, "usage: %s [-cDuVv]\n\t%s\n", progname,    "[-d data_dir ...] [-h home] [-l log_dir] [-P password] -b backup_dir");	return (EXIT_FAILURE);}intdb_hotbackup_version_check(){	int v_major, v_minor, v_patch;	/* Make sure we're loaded with the right version of the DB library. */	(void)db_version(&v_major, &v_minor, &v_patch);	if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {		fprintf(stderr,	"%s: version %d.%d doesn't match library version %d.%d\n",		    progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,		    v_major, v_minor);		return (EXIT_FAILURE);	}	return (0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?