📄 initdb.c
字号:
oumask = umask(0); numask = oumask & ~(S_IWUSR | S_IXUSR); (void) umask(numask); first = 0; } if (last) (void) umask(oumask); /* check for pre-existing directory; ok if it's a parent */ if (stat(path, &sb) == 0) { if (!S_ISDIR(sb.st_mode)) { if (last) errno = EEXIST; else errno = ENOTDIR; retval = 1; break; } } else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) { retval = 1; break; } if (!last) *p = '/'; } if (!first && !last) (void) umask(oumask); return retval;}/* * clean up any files we created on failure * if we created the data directory remove it too */static voidexit_nicely(void){ if (!noclean) { if (made_new_pgdata) { fprintf(stderr, _("%s: removing data directory \"%s\"\n"), progname, pg_data); if (!rmtree(pg_data, true)) fprintf(stderr, _("%s: failed to remove data directory\n"), progname); } else if (found_existing_pgdata) { fprintf(stderr, _("%s: removing contents of data directory \"%s\"\n"), progname, pg_data); if (!rmtree(pg_data, false)) fprintf(stderr, _("%s: failed to remove contents of data directory\n"), progname); } /* otherwise died during startup, do nothing! */ } else { if (made_new_pgdata || found_existing_pgdata) fprintf(stderr, _("%s: data directory \"%s\" not removed at user's request\n"), progname, pg_data); } exit(1);}/* * find the current user * * on unix make sure it isn't really root */static char *get_id(void){#ifndef WIN32 struct passwd *pw; pw = getpwuid(geteuid());#ifndef __BEOS__ /* no root check on BEOS */ if (geteuid() == 0) /* 0 is root's uid */ { fprintf(stderr, _("%s: cannot be run as root\n" "Please log in (using, e.g., \"su\") as the " "(unprivileged) user that will\n" "own the server process.\n"), progname); exit(1); }#endif#else /* the windows code */ struct passwd_win32 { int pw_uid; char pw_name[128]; } pass_win32; struct passwd_win32 *pw = &pass_win32; DWORD pwname_size = sizeof(pass_win32.pw_name) - 1; pw->pw_uid = 1; GetUserName(pw->pw_name, &pwname_size);#endif return xstrdup(pw->pw_name);}static char *encodingid_to_string(int enc){ char result[20]; sprintf(result, "%d", enc); return xstrdup(result);}/* * get the encoding id for a given encoding name */static char *get_encoding_id(char *encoding_name){ int enc; if (encoding_name && *encoding_name) { if ((enc = pg_char_to_encoding(encoding_name)) >= 0 && pg_valid_server_encoding(encoding_name) >= 0) return encodingid_to_string(enc); } fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"), progname, encoding_name ? encoding_name : "(null)"); exit(1);}#if defined(HAVE_LANGINFO_H) && defined(CODESET)/* * Checks whether the encoding selected for PostgreSQL and the * encoding used by the system locale match. */struct encoding_match{ enum pg_enc pg_enc_code; char *system_enc_name;};struct encoding_match encoding_match_list[] = { {PG_EUC_JP, "EUC-JP"}, {PG_EUC_JP, "eucJP"}, {PG_EUC_JP, "IBM-eucJP"}, {PG_EUC_JP, "sdeckanji"}, {PG_EUC_CN, "EUC-CN"}, {PG_EUC_CN, "eucCN"}, {PG_EUC_CN, "IBM-eucCN"}, {PG_EUC_CN, "GB2312"}, {PG_EUC_CN, "dechanzi"}, {PG_EUC_KR, "EUC-KR"}, {PG_EUC_KR, "eucKR"}, {PG_EUC_KR, "IBM-eucKR"}, {PG_EUC_KR, "deckorean"}, {PG_EUC_KR, "5601"}, {PG_EUC_TW, "EUC-TW"}, {PG_EUC_TW, "eucTW"}, {PG_EUC_TW, "IBM-eucTW"}, {PG_EUC_TW, "cns11643"},#ifdef NOT_VERIFIED {PG_JOHAB, "???"},#endif {PG_UTF8, "UTF-8"}, {PG_UTF8, "utf8"}, {PG_LATIN1, "ISO-8859-1"}, {PG_LATIN1, "ISO8859-1"}, {PG_LATIN1, "iso88591"}, {PG_LATIN2, "ISO-8859-2"}, {PG_LATIN2, "ISO8859-2"}, {PG_LATIN2, "iso88592"}, {PG_LATIN3, "ISO-8859-3"}, {PG_LATIN3, "ISO8859-3"}, {PG_LATIN3, "iso88593"}, {PG_LATIN4, "ISO-8859-4"}, {PG_LATIN4, "ISO8859-4"}, {PG_LATIN4, "iso88594"}, {PG_LATIN5, "ISO-8859-9"}, {PG_LATIN5, "ISO8859-9"}, {PG_LATIN5, "iso88599"}, {PG_LATIN6, "ISO-8859-10"}, {PG_LATIN6, "ISO8859-10"}, {PG_LATIN6, "iso885910"}, {PG_LATIN7, "ISO-8859-13"}, {PG_LATIN7, "ISO8859-13"}, {PG_LATIN7, "iso885913"}, {PG_LATIN8, "ISO-8859-14"}, {PG_LATIN8, "ISO8859-14"}, {PG_LATIN8, "iso885914"}, {PG_LATIN9, "ISO-8859-15"}, {PG_LATIN9, "ISO8859-15"}, {PG_LATIN9, "iso885915"}, {PG_LATIN10, "ISO-8859-16"}, {PG_LATIN10, "ISO8859-16"}, {PG_LATIN10, "iso885916"}, {PG_WIN1252, "CP1252"}, {PG_WIN1256, "CP1256"}, {PG_WIN1258, "CP1258"},#ifdef NOT_VERIFIED {PG_WIN874, "???"},#endif {PG_KOI8R, "KOI8-R"}, {PG_WIN1251, "CP1251"}, {PG_WIN866, "CP866"}, {PG_ISO_8859_5, "ISO-8859-5"}, {PG_ISO_8859_5, "ISO8859-5"}, {PG_ISO_8859_5, "iso88595"}, {PG_ISO_8859_6, "ISO-8859-6"}, {PG_ISO_8859_6, "ISO8859-6"}, {PG_ISO_8859_6, "iso88596"}, {PG_ISO_8859_7, "ISO-8859-7"}, {PG_ISO_8859_7, "ISO8859-7"}, {PG_ISO_8859_7, "iso88597"}, {PG_ISO_8859_8, "ISO-8859-8"}, {PG_ISO_8859_8, "ISO8859-8"}, {PG_ISO_8859_8, "iso88598"}, {PG_SQL_ASCII, NULL} /* end marker */};static char *get_encoding_from_locale(const char *ctype){ char *save; char *sys; save = setlocale(LC_CTYPE, NULL); if (!save) return NULL; save = xstrdup(save); setlocale(LC_CTYPE, ctype); sys = nl_langinfo(CODESET); sys = xstrdup(sys); setlocale(LC_CTYPE, save); free(save); return sys;}static voidcheck_encodings_match(int pg_enc, const char *ctype){ char *sys; int i; sys = get_encoding_from_locale(ctype); for (i = 0; encoding_match_list[i].system_enc_name; i++) { if (pg_enc == encoding_match_list[i].pg_enc_code && pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0) { free(sys); return; } } fprintf(stderr, _("%s: warning: encoding mismatch\n"), progname); fprintf(stderr, _("The encoding you selected (%s) and the encoding that the selected\n" "locale uses (%s) are not known to match. This may lead to\n" "misbehavior in various character string processing functions. To fix\n" "this situation, rerun %s and either do not specify an encoding\n" "explicitly, or choose a matching combination.\n"), pg_encoding_to_char(pg_enc), sys, progname); free(sys); return;}static intfind_matching_encoding(const char *ctype){ char *sys; int i; sys = get_encoding_from_locale(ctype); for (i = 0; encoding_match_list[i].system_enc_name; i++) { if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0) { free(sys); return encoding_match_list[i].pg_enc_code; } } free(sys); return -1;}#endif /* HAVE_LANGINFO_H && CODESET *//* * get short version of VERSION */static char *get_short_version(void){ bool gotdot = false; int end; char *vr; vr = xstrdup(PG_VERSION); for (end = 0; vr[end] != '\0'; end++) { if (vr[end] == '.') { if (end == 0) return NULL; else if (gotdot) break; else gotdot = true; } else if (vr[end] < '0' || vr[end] > '9') { /* gone past digits and dots */ break; } } if (end == 0 || vr[end - 1] == '.' || !gotdot) return NULL; vr[end] = '\0'; return vr;}/* * make sure the data directory either doesn't exist or is empty * * Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty, * or -1 if trouble accessing directory */static intcheck_data_dir(void){ DIR *chkdir; struct dirent *file; int result = 1; errno = 0; chkdir = opendir(pg_data); if (!chkdir) return (errno == ENOENT) ? 0 : -1; while ((file = readdir(chkdir)) != NULL) { if (strcmp(".", file->d_name) == 0 || strcmp("..", file->d_name) == 0) { /* skip this and parent directory */ continue; } else { result = 2; /* not empty */ break; } }#ifdef WIN32 /* * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in * released version */ if (GetLastError() == ERROR_NO_MORE_FILES) errno = 0;#endif closedir(chkdir); if (errno != 0) result = -1; /* some kind of I/O error? */ return result;}/* * make the data directory (or one of its subdirectories if subdir is not NULL) */static boolmkdatadir(const char *subdir){ char *path; path = pg_malloc(strlen(pg_data) + 2 + (subdir == NULL ? 0 : strlen(subdir))); if (subdir != NULL) sprintf(path, "%s/%s", pg_data, subdir); else strcpy(path, pg_data); if (mkdir_p(path, 0700) == 0) return true; fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, path, strerror(errno)); return false;}/* * set name of given input file variable under data directory */static voidset_input(char **dest, char *filename){ *dest = pg_malloc(strlen(share_path) + strlen(filename) + 2); sprintf(*dest, "%s/%s", share_path, filename);}/* * check that given input file exists */static voidcheck_input(char *path){ struct stat statbuf; if (stat(path, &statbuf) != 0 || !S_ISREG(statbuf.st_mode)) { fprintf(stderr, _("%s: file \"%s\" does not exist\n" "This means you have a corrupted installation or identified\n" "the wrong directory with the invocation option -L.\n"), progname, path); exit(1); }}/* * write out the PG_VERSION file in the data dir, or its subdirectory * if extrapath is not NULL */static voidset_short_version(char *short_version, char *extrapath){ FILE *version_file; char *path; if (extrapath == NULL) { path = pg_malloc(strlen(pg_data) + 12); sprintf(path, "%s/PG_VERSION", pg_data); } else { path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13); sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath); } version_file = fopen(path, PG_BINARY_W); if (version_file == NULL) { fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), progname, path, strerror(errno)); exit_nicely(); } if (fprintf(version_file, "%s\n", short_version) < 0 || fclose(version_file)) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); exit_nicely(); }}/* * set up an empty config file so we can check buffers and connections */static voidset_null_conf(void){ FILE *conf_file; char *path; path = pg_malloc(strlen(pg_data) + 17); sprintf(path, "%s/postgresql.conf", pg_data); conf_file = fopen(path, PG_BINARY_W); if (conf_file == NULL) { fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"), progname, path, strerror(errno)); exit_nicely(); } if (fclose(conf_file)) { fprintf(stderr, _("%s: could not write file \"%s\": %s\n"), progname, path, strerror(errno)); exit_nicely(); }}/* * check how many connections we can sustain */static voidtest_connections(void){ char cmd[MAXPGPATH];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -