Exemple #1
0
bool
create_pg_dir(char *dir, bool force)
{
	bool		pg_dir = false;

	/* Check this directory could be used as a PGDATA dir */
	switch (check_dir(dir))
	{
		case 0:
			/* dir not there, must create it */
			log_info(_("creating directory \"%s\"...\n"), dir);

			if (!create_dir(dir))
			{
				log_err(_("couldn't create directory \"%s\"...\n"),
						dir);
				return false;
			}
			break;
		case 1:
			/* Present but empty, fix permissions and use it */
			log_info(_("checking and correcting permissions on existing directory %s ...\n"),
					 dir);

			if (!set_dir_permissions(dir))
			{
				log_err(_("could not change permissions of directory \"%s\": %s\n"),
						dir, strerror(errno));
				return false;
			}
			break;
		case 2:
			/* Present and not empty */
			log_warning(_("directory \"%s\" exists but is not empty\n"),
						dir);

			pg_dir = is_pg_dir(dir);

			/*
			 * we use force to reduce the time needed to restore a node which
			 * turn async after a failover or anything else
			 */
			if (pg_dir && force)
			{
				/* Let it continue */
				break;
			}
			else if (pg_dir && !force)
			{
				log_warning(_("\nThis looks like a PostgreSQL directory.\n"
							  "If you are sure you want to clone here, "
							  "please check there is no PostgreSQL server "
							  "running and use the --force option\n"));
				return false;
			}

			return false;
		default:
			/* Trouble accessing directory */
			log_err(_("could not access directory \"%s\": %s\n"),
					dir, strerror(errno));
			return false;
	}
	return true;
}
Exemple #2
0
bool
create_pg_dir(char *path, bool force)
{
	/* Check this directory can be used as a PGDATA dir */
	switch (check_dir(path))
	{
		case DIR_NOENT:
			/* directory does not exist, attempt to create it */
			log_info(_("creating directory \"%s\"..."), path);

			if (!create_dir(path))
			{
				log_error(_("unable to create directory \"%s\"..."),
						  path);
				return false;
			}
			break;
		case DIR_EMPTY:
			/* exists but empty, fix permissions and use it */
			log_info(_("checking and correcting permissions on existing directory \"%s\""),
					 path);

			if (!set_dir_permissions(path))
			{
				log_error(_("unable to change permissions of directory \"%s\""), path);
				log_detail("%s", strerror(errno));
				return false;
			}
			break;
		case DIR_NOT_EMPTY:
			/* exists but is not empty */
			log_warning(_("directory \"%s\" exists but is not empty"),
						path);

			if (is_pg_dir(path))
			{
				if (force == true)
				{
					log_notice(_("-F/--force provided - deleting existing data directory \"%s\""), path);
					nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS);
					return true;
				}

				return false;
			}
			else
			{
				if (force == true)
				{
					log_notice(_("deleting existing directory \"%s\""), path);
					nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS);
					return true;
				}
				return false;
			}
			break;
		case DIR_ERROR:
			log_error(_("could not access directory \"%s\": %s"),
					  path, strerror(errno));
			return false;
	}

	return true;
}