Esempio n. 1
0
/* create directory, also create parent directories if necessary */
int
dir_create_dir(const char *dir, mode_t mode)
{
	char copy[MAXPGPATH];
	char *parent;

	strncpy(copy, dir, MAXPGPATH);
	parent = dirname(copy);
	if (access(parent, F_OK) == -1)
		dir_create_dir(parent, mode);
#ifdef __darwin__
	if (mkdir(copy, mode) == -1)
#else
	if (mkdir(dir, mode) == -1)
#endif
	{
		if (errno == EEXIST)	/* already exist */
			return 0;
		ereport(ERROR,
			(errcode(ERROR_SYSTEM),
			 errmsg("could not create directory \"%s\": %s", dir, strerror(errno))));
	}

	return 0;
}
Esempio n. 2
0
/* copy contents of directory from_root into to_root */
void
dir_copy_files(const char *from_root, const char *to_root)
{
	int		i;
	parray *files = parray_new();

	/* don't copy root directory */
	dir_list_file(files, from_root, NULL, true, false);

	for (i = 0; i < parray_num(files); i++)
	{
		pgFile *file = (pgFile *) parray_get(files, i);

		if (S_ISDIR(file->mode))
		{
			char to_path[MAXPGPATH];
			join_path_components(to_path, to_root, file->path + strlen(from_root) + 1);
			if (verbose && !check)
				printf(_("create directory \"%s\"\n"),
					file->path + strlen(from_root) + 1);
			if (!check) {
				dir_create_dir(to_path, DIR_PERMISSION);
			}
			continue;
		}
		else if(S_ISREG(file->mode))
		{
			if (verbose && !check)
				printf(_("copy \"%s\"\n"),
					file->path + strlen(from_root) + 1);
			if (!check)
				copy_file(from_root, to_root, file, NO_COMPRESSION);
		}
	}

	/* cleanup */
	parray_walk(files, pgFileFree);
	parray_free(files);
}
Esempio n. 3
0
/* create directory, also create parent directories if necessary */
int
dir_create_dir(const char *dir, mode_t mode)
{
	char copy[MAXPGPATH];
	char *parent;

	strncpy(copy, dir, MAXPGPATH);
	parent = dirname(copy);
	if (access(parent, F_OK) == -1)
		dir_create_dir(parent, mode);
#ifdef MACOS
	if (mkdir(copy, mode) == -1)
#else
	if (mkdir(dir, mode) == -1)
#endif
	{
		if (errno == EEXIST)	/* already exist */
			return 0;
		elog(ERROR_SYSTEM, _("can't create directory \"%s\": %s"), dir,
			strerror(errno));
	}

	return 0;
}
Esempio n. 4
0
/*
 * Initialize backup catalog.
 */
int
do_init(void)
{
	char	path[MAXPGPATH];
	char   *log_directory = NULL;
	char   *archive_command = NULL;
	FILE   *fp;

	struct dirent **dp;
	int results;
	if (access(backup_path, F_OK) == 0){
		results = scandir(backup_path, &dp, selects, NULL);
		if(results != 0){
			elog(ERROR, _("backup catalog already exist. and it's not empty."));
		}
	}

	/* create backup catalog root directory */
	dir_create_dir(backup_path, DIR_PERMISSION);

	/* create directories for backup of online files */
	join_path_components(path, backup_path, RESTORE_WORK_DIR);
	dir_create_dir(path, DIR_PERMISSION);
	snprintf(path, lengthof(path), "%s/%s/%s", backup_path, RESTORE_WORK_DIR,
		PG_XLOG_DIR);
	dir_create_dir(path, DIR_PERMISSION);
	snprintf(path, lengthof(path), "%s/%s/%s", backup_path, RESTORE_WORK_DIR,
		SRVLOG_DIR);
	dir_create_dir(path, DIR_PERMISSION);

	/* create directory for timeline history files */
	join_path_components(path, backup_path, TIMELINE_HISTORY_DIR);
	dir_create_dir(path, DIR_PERMISSION);

	/* read postgresql.conf */
	if (pgdata)
	{
		join_path_components(path, pgdata, "postgresql.conf");
		parse_postgresql_conf(path, &log_directory, &archive_command);
	}

	/* create pg_rman.ini */
	join_path_components(path, backup_path, PG_RMAN_INI_FILE);
	fp = fopen(path, "wt");
	if (fp == NULL)
		elog(ERROR_SYSTEM, _("can't create pg_rman.ini: %s"), strerror(errno));

	/* set ARCLOG_PATH refered with log_directory */
	if (arclog_path == NULL && archive_command && archive_command[0])
	{
		char *command = pgut_strdup(archive_command);
		char *begin;
		char *end;
		char *fname;

		/* example: 'cp "%p" /path/to/arclog/"%f"' */
		for (begin = command; *begin;)
		{
			begin = begin + strspn(begin, " \n\r\t\v");
			end = begin + strcspn(begin, " \n\r\t\v");
			*end = '\0';

			if ((fname = strstr(begin, "%f")) != NULL)
			{
				while (strchr(" \n\r\t\v\"'", *begin))
					begin++;
				fname--;
				while (fname > begin && strchr(" \n\r\t\v\"'/", fname[-1]))
					fname--;
				*fname = '\0';

				if (is_absolute_path(begin))
					arclog_path = pgut_strdup(begin);
				break;
			}

			begin = end + 1;
		}

		free(command);
	}
	if (arclog_path)
	{
		fprintf(fp, "ARCLOG_PATH='%s'\n", arclog_path);
		elog(INFO, "ARCLOG_PATH is set to '%s'", arclog_path);
	}
	else if (archive_command && archive_command[0])
		elog(WARNING, "ARCLOG_PATH is not set because failed to parse archive_command '%s'."
				"Please set ARCLOG_PATH in pg_rman.ini or environmental variable", archive_command);
	else
		elog(WARNING, "ARCLOG_PATH is not set because archive_command is empty."
				"Please set ARCLOG_PATH in pg_rman.ini or environmental variable");

	/* set SRVLOG_PATH refered with log_directory */
	if (srvlog_path == NULL)
	{
		if (log_directory)
		{
			if (is_absolute_path(log_directory))
				srvlog_path = pgut_strdup(log_directory);
			else
			{
				srvlog_path = pgut_malloc(MAXPGPATH);
				join_path_components(srvlog_path, pgdata, log_directory);
			}
		}
		else if (pgdata)
		{
			/* default: log_directory = 'pg_log' */
			srvlog_path = pgut_malloc(MAXPGPATH);
			join_path_components(srvlog_path, pgdata, "pg_log");
		}
	}
	if (srvlog_path)
	{
		fprintf(fp, "SRVLOG_PATH='%s'\n", srvlog_path);
		elog(INFO, "SRVLOG_PATH is set to '%s'", srvlog_path);
	}

	fprintf(fp, "\n");
	fclose(fp);

	free(archive_command);
	free(log_directory);

	return 0;
}