postgres 之 initdb 源码分析 六

2.14 函数set_info_version

/*
 * extract the strange version of version required for information schema
 * (09.08.0007abc)
 */
static void
set_info_version(void)
{
	char	   *letterversion;
	long		major = 0,minor = 0,micro = 0;
	char	   *endptr;
	char	   *vstr = pg_strdup(PG_VERSION);
	char	   *ptr;

	ptr = vstr + (strlen(vstr) - 1);
	while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
		ptr--;
	letterversion = ptr + 1;
	major = strtol(vstr,&endptr,10);
	if (*endptr)
		minor = strtol(endptr + 1,10);
	if (*endptr)
		micro = strtol(endptr + 1,10);
	snprintf(infoversion,sizeof(infoversion),"%02ld.%02ld.%04ld%s",major,minor,micro,letterversion);
}


2.15 函数setup_data_file_paths(void)

void
setup_data_file_paths(void)
{
	set_input(&bki_file,"postgres.bki");
	set_input(&desc_file,"postgres.description");
	set_input(&shdesc_file,"postgres.shdescription");
	set_input(&hba_file,"pg_hba.conf.sample");
	set_input(&ident_file,"pg_ident.conf.sample");
	set_input(&conf_file,"postgresql.conf.sample");
	set_input(&conversion_file,"conversion_create.sql");
	set_input(&dictionary_file,"snowball_create.sql");
	set_input(&info_schema_file,"information_schema.sql");
	set_input(&features_file,"sql_features.txt");
	set_input(&system_views_file,"system_views.sql");

	if (show_setting || debug)
	{
		fprintf(stderr,"VERSION=%s\n"
				"PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
				"POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
				"POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
				"POSTGREsql_CONF_SAMPLE=%s\n"
				"PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",PG_VERSION,pg_data,share_path,bin_path,username,bki_file,desc_file,shdesc_file,conf_file,hba_file,ident_file);
		if (show_setting)
			exit(0);
	}

	check_input(bki_file);
	check_input(desc_file);
	check_input(shdesc_file);
	check_input(hba_file);
	check_input(ident_file);
	check_input(conf_file);
	check_input(conversion_file);
	check_input(dictionary_file);
	check_input(info_schema_file);
	check_input(features_file);
	check_input(system_views_file);
}

2.15.1 函数set_input

/*
 * set name of given input file variable under data directory
 */
static void
set_input(char **dest,char *filename)
{
	*dest = pg_malloc(strlen(share_path) + strlen(filename) + 2);
	sprintf(*dest,"%s/%s",filename);
}


2.15.2 函数check_setup

/*
 * check that given input file exists
 */
static void
check_input(char *path)
{
	struct stat statbuf;

	if (stat(path,&statbuf) != 0)
	{
		if (errno == ENOENT)
		{
			fprintf(stderr,_("%s: file \"%s\" does not exist\n"),progname,path);
			fprintf(stderr,_("This might mean you have a corrupted installation or identified\n"
					"the wrong directory with the invocation option -L.\n"));
		}
		else
		{
			fprintf(stderr,_("%s: Could not access file \"%s\": %s\n"),path,strerror(errno));
			fprintf(stderr,_("This might mean you have a corrupted installation or identified\n"
					"the wrong directory with the invocation option -L.\n"));
		}
		exit(1);
	}
	if (!S_ISREG(statbuf.st_mode))
	{
		fprintf(stderr,_("%s: file \"%s\" is not a regular file\n"),path);
		fprintf(stderr,_("This might mean you have a corrupted installation or identified\n"
		  "the wrong directory with the invocation option -L.\n"));
		exit(1);
	}
}

2.16 函数 setup_locale_encoding()

为新建的数据库设置认的编码方式

void
setup_locale_encoding(void)
{
	int			user_enc;

	setlocales();

	if (strcmp(lc_ctype,lc_collate) == 0 &&
		strcmp(lc_ctype,lc_time) == 0 &&
		strcmp(lc_ctype,lc_numeric) == 0 &&
		strcmp(lc_ctype,lc_monetary) == 0 &&
		strcmp(lc_ctype,lc_messages) == 0)
		printf(_("The database cluster will be initialized with locale \"%s\".\n"),lc_ctype);
	else
	{
		printf(_("The database cluster will be initialized with locales\n"
				 "  COLLATE:  %s\n"
				 "  CTYPE:    %s\n"
				 "  MESSAGES: %s\n"
				 "  MONETARY: %s\n"
				 "  NUMERIC:  %s\n"
				 "  TIME:     %s\n"),lc_collate,lc_ctype,lc_messages,lc_monetary,lc_numeric,lc_time);
	}

	if (strlen(encoding) == 0)
	{
		int			ctype_enc;

		ctype_enc = pg_get_encoding_from_locale(lc_ctype,true);

		if (ctype_enc == -1)
		{
			/* Couldn't recognize the locale's codeset */
			fprintf(stderr,_("%s: Could not find suitable encoding for locale \"%s\"\n"),lc_ctype);
			fprintf(stderr,_("Rerun %s with the -E option.\n"),progname);
			fprintf(stderr,_("Try \"%s --help\" for more information.\n"),progname);
			exit(1);
		}
		else if (!pg_valid_server_encoding_id(ctype_enc))
		{
			/*
			 * We recognized it,but it's not a legal server encoding. On
			 * Windows,UTF-8 works with any locale,so we can fall back to
			 * UTF-8.
			 */
#ifdef WIN32
			printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
			"The default database encoding will be set to \"%s\" instead.\n"),pg_encoding_to_char(ctype_enc),pg_encoding_to_char(PG_UTF8));
			ctype_enc = PG_UTF8;
			encodingid = encodingid_to_string(ctype_enc);
#else
			fprintf(stderr,_("%s: locale \"%s\" requires unsupported encoding \"%s\"\n"),pg_encoding_to_char(ctype_enc));
			fprintf(stderr,_("Encoding \"%s\" is not allowed as a server-side encoding.\n"
				"Rerun %s with a different locale selection.\n"),progname);
			exit(1);
#endif
		}
		else
		{
			encodingid = encodingid_to_string(ctype_enc);
			printf(_("The default database encoding has accordingly been set to \"%s\".\n"),pg_encoding_to_char(ctype_enc));
		}
	}
	else
		encodingid = get_encoding_id(encoding);

	user_enc = atoi(encodingid);
	if (!check_locale_encoding(lc_ctype,user_enc) ||
		!check_locale_encoding(lc_collate,user_enc))
		exit(1);				/* check_locale_encoding printed the error */

}


2.16.1 得到第二行输出

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...