#include "c.h"
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include "pg_config_paths.h"
Go to the source code of this file.
Defines | |
#define | IS_PATH_VAR_SEP(ch) ((ch) == ':') |
#define | skip_drive(path) (path) |
Functions | |
static void | make_relative_path (char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path) |
static void | trim_directory (char *path) |
static void | trim_trailing_separator (char *path) |
bool | has_drive_prefix (const char *path) |
char * | first_dir_separator (const char *filename) |
char * | first_path_var_separator (const char *pathlist) |
char * | last_dir_separator (const char *filename) |
void | make_native_path (char *filename) |
void | join_path_components (char *ret_path, const char *head, const char *tail) |
void | canonicalize_path (char *path) |
bool | path_contains_parent_reference (const char *path) |
bool | path_is_relative_and_below_cwd (const char *path) |
bool | path_is_prefix_of_path (const char *path1, const char *path2) |
const char * | get_progname (const char *argv0) |
static int | dir_strcmp (const char *s1, const char *s2) |
void | get_share_path (const char *my_exec_path, char *ret_path) |
void | get_etc_path (const char *my_exec_path, char *ret_path) |
void | get_include_path (const char *my_exec_path, char *ret_path) |
void | get_pkginclude_path (const char *my_exec_path, char *ret_path) |
void | get_includeserver_path (const char *my_exec_path, char *ret_path) |
void | get_lib_path (const char *my_exec_path, char *ret_path) |
void | get_pkglib_path (const char *my_exec_path, char *ret_path) |
void | get_locale_path (const char *my_exec_path, char *ret_path) |
void | get_doc_path (const char *my_exec_path, char *ret_path) |
void | get_html_path (const char *my_exec_path, char *ret_path) |
void | get_man_path (const char *my_exec_path, char *ret_path) |
bool | get_home_path (char *ret_path) |
void | get_parent_directory (char *path) |
#define IS_PATH_VAR_SEP | ( | ch | ) | ((ch) == ':') |
Definition at line 38 of file path.c.
Referenced by first_path_var_separator().
#define skip_drive | ( | path | ) | (path) |
Definition at line 74 of file path.c.
Referenced by canonicalize_path(), first_dir_separator(), get_progname(), has_drive_prefix(), join_path_components(), last_dir_separator(), path_contains_parent_reference(), trim_directory(), and trim_trailing_separator().
void canonicalize_path | ( | char * | path | ) |
Definition at line 216 of file path.c.
References skip_drive, trim_directory(), and trim_trailing_separator().
Referenced by adjust_data_dir(), check_canonical_path(), convert_and_check_filename(), create_script_for_old_cluster_deletion(), create_xlog_symlink(), CreateTableSpace(), do_copy(), exec_command(), find_in_dynamic_libpath(), find_my_exec(), find_other_exec(), load_libraries(), main(), make_absolute_path(), make_relative_path(), perform_fsync(), process_file(), resolve_symlinks(), set_pglocale_pgservice(), setup(), setup_bin_paths(), setup_pgdata(), SplitDirectoriesString(), and tokenize_inc_file().
{ char *p, *to_p; char *spath; bool was_sep = false; int pending_strips; #ifdef WIN32 /* * The Windows command processor will accept suitably quoted paths with * forward slashes, but barfs badly with mixed forward and back slashes. */ for (p = path; *p; p++) { if (*p == '\\') *p = '/'; } /* * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass \c\d" * as argv[2], so trim off trailing quote. */ if (p > path && *(p - 1) == '"') *(p - 1) = '/'; #endif /* * Removing the trailing slash on a path means we never get ugly double * trailing slashes. Also, Win32 can't stat() a directory with a trailing * slash. Don't remove a leading slash, though. */ trim_trailing_separator(path); /* * Remove duplicate adjacent separators */ p = path; #ifdef WIN32 /* Don't remove leading double-slash on Win32 */ if (*p) p++; #endif to_p = p; for (; *p; p++, to_p++) { /* Handle many adjacent slashes, like "/a///b" */ while (*p == '/' && was_sep) p++; if (to_p != p) *to_p = *p; was_sep = (*p == '/'); } *to_p = '\0'; /* * Remove any trailing uses of "." and process ".." ourselves * * Note that "/../.." should reduce to just "/", while "../.." has to be * kept as-is. In the latter case we put back mistakenly trimmed ".." * components below. Also note that we want a Windows drive spec to be * visible to trim_directory(), but it's not part of the logic that's * looking at the name components; hence distinction between path and * spath. */ spath = skip_drive(path); pending_strips = 0; for (;;) { int len = strlen(spath); if (len >= 2 && strcmp(spath + len - 2, "/.") == 0) trim_directory(path); else if (strcmp(spath, ".") == 0) { /* Want to leave "." alone, but "./.." has to become ".." */ if (pending_strips > 0) *spath = '\0'; break; } else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) || strcmp(spath, "..") == 0) { trim_directory(path); pending_strips++; } else if (pending_strips > 0 && *spath != '\0') { /* trim a regular directory name canceled by ".." */ trim_directory(path); pending_strips--; /* foo/.. should become ".", not empty */ if (*spath == '\0') strcpy(spath, "."); } else break; } if (pending_strips > 0) { /* * We could only get here if path is now totally empty (other than a * possible drive specifier on Windows). We have to put back one or * more ".."'s that we took off. */ while (--pending_strips > 0) strcat(path, "../"); strcat(path, ".."); } }
static int dir_strcmp | ( | const char * | s1, | |
const char * | s2 | |||
) | [static] |
Definition at line 453 of file path.c.
References IS_DIR_SEP, and pg_tolower().
Referenced by make_relative_path().
{ while (*s1 && *s2) { if ( #ifndef WIN32 *s1 != *s2 #else /* On windows, paths are case-insensitive */ pg_tolower((unsigned char) *s1) != pg_tolower((unsigned char) *s2) #endif && !(IS_DIR_SEP(*s1) && IS_DIR_SEP(*s2))) return (int) *s1 - (int) *s2; s1++, s2++; } if (*s1) return 1; /* s1 longer */ if (*s2) return -1; /* s2 longer */ return 0; }
char* first_dir_separator | ( | const char * | filename | ) |
Definition at line 95 of file path.c.
References IS_DIR_SEP, and skip_drive.
Referenced by check_restricted_library_name(), check_valid_extension_name(), check_valid_version_name(), expand_dynamic_library_name(), find_in_dynamic_libpath(), find_my_exec(), load_libraries(), and substitute_libpath_macro().
{ const char *p; for (p = skip_drive(filename); *p; p++) if (IS_DIR_SEP(*p)) return (char *) p; return NULL; }
char* first_path_var_separator | ( | const char * | pathlist | ) |
Definition at line 112 of file path.c.
References IS_PATH_VAR_SEP.
Referenced by find_in_dynamic_libpath(), and find_my_exec().
{ const char *p; /* skip_drive is not needed */ for (p = pathlist; *p; p++) if (IS_PATH_VAR_SEP(*p)) return (char *) p; return NULL; }
void get_doc_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 630 of file path.c.
References make_relative_path().
Referenced by show_docdir().
{ make_relative_path(ret_path, DOCDIR, PGBINDIR, my_exec_path); }
void get_etc_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 567 of file path.c.
References make_relative_path().
Referenced by process_psqlrc(), set_pglocale_pgservice(), and show_sysconfdir().
{ make_relative_path(ret_path, SYSCONFDIR, PGBINDIR, my_exec_path); }
bool get_home_path | ( | char * | ret_path | ) |
Definition at line 661 of file path.c.
References MAXPGPATH, pqGetpwuid(), snprintf(), and strlcpy().
Referenced by expand_tilde(), initializeInput(), and process_psqlrc().
{ #ifndef WIN32 char pwdbuf[BUFSIZ]; struct passwd pwdstr; struct passwd *pwd = NULL; if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0) return false; strlcpy(ret_path, pwd->pw_dir, MAXPGPATH); return true; #else char *tmppath; /* * Note: We use getenv here because the more modern * SHGetSpecialFolderPath() will force us to link with shell32.lib which * eats valuable desktop heap. */ tmppath = getenv("APPDATA"); if (!tmppath) return false; snprintf(ret_path, MAXPGPATH, "%s/postgresql", tmppath); return true; #endif }
void get_html_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 639 of file path.c.
References make_relative_path().
Referenced by show_htmldir().
{ make_relative_path(ret_path, HTMLDIR, PGBINDIR, my_exec_path); }
void get_include_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 576 of file path.c.
References make_relative_path().
Referenced by main(), and show_includedir().
{ make_relative_path(ret_path, INCLUDEDIR, PGBINDIR, my_exec_path); }
void get_includeserver_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 594 of file path.c.
References make_relative_path().
Referenced by show_includedir_server().
{ make_relative_path(ret_path, INCLUDEDIRSERVER, PGBINDIR, my_exec_path); }
void get_lib_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 603 of file path.c.
References make_relative_path().
Referenced by show_libdir().
{ make_relative_path(ret_path, LIBDIR, PGBINDIR, my_exec_path); }
void get_locale_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 621 of file path.c.
References make_relative_path().
Referenced by pg_bindtextdomain(), set_pglocale_pgservice(), and show_localedir().
{ make_relative_path(ret_path, LOCALEDIR, PGBINDIR, my_exec_path); }
void get_man_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 648 of file path.c.
References make_relative_path().
Referenced by show_mandir().
{ make_relative_path(ret_path, MANDIR, PGBINDIR, my_exec_path); }
void get_parent_directory | ( | char * | path | ) |
Definition at line 705 of file path.c.
References trim_directory().
Referenced by destroy_tablespace_directories(), main(), process_file(), TablespaceCreateDbspace(), and tokenize_inc_file().
{ trim_directory(path); }
void get_pkginclude_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 585 of file path.c.
References make_relative_path().
Referenced by main(), and show_pkgincludedir().
{ make_relative_path(ret_path, PKGINCLUDEDIR, PGBINDIR, my_exec_path); }
void get_pkglib_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 612 of file path.c.
References make_relative_path().
Referenced by getInstallationPaths(), PostgresMain(), show_pgxs(), and show_pkglibdir().
{ make_relative_path(ret_path, PKGLIBDIR, PGBINDIR, my_exec_path); }
const char* get_progname | ( | const char * | argv0 | ) |
Definition at line 415 of file path.c.
References EXE, last_dir_separator(), NULL, pg_strcasecmp(), progname, and skip_drive.
Referenced by get_opts(), handle_help_version_opts(), main(), parseCommandLine(), and regression_main().
{ const char *nodir_name; char *progname; nodir_name = last_dir_separator(argv0); if (nodir_name) nodir_name++; else nodir_name = skip_drive(argv0); /* * Make a copy in case argv[0] is modified by ps_status. Leaks memory, but * called only once. */ progname = strdup(nodir_name); if (progname == NULL) { fprintf(stderr, "%s: out of memory\n", nodir_name); abort(); /* This could exit the postmaster */ } #if defined(__CYGWIN__) || defined(WIN32) /* strip ".exe" suffix, regardless of case */ if (strlen(progname) > sizeof(EXE) - 1 && pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0) progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0'; #endif return progname; }
void get_share_path | ( | const char * | my_exec_path, | |
char * | ret_path | |||
) |
Definition at line 558 of file path.c.
References make_relative_path().
Referenced by get_extension_control_directory(), get_extension_control_filename(), get_extension_script_directory(), get_tsearch_config_filename(), ParseTzFile(), pg_TZDIR(), setup_bin_paths(), and show_sharedir().
{ make_relative_path(ret_path, PGSHAREDIR, PGBINDIR, my_exec_path); }
bool has_drive_prefix | ( | const char * | path | ) |
Definition at line 83 of file path.c.
References skip_drive.
Referenced by process_file().
{ return skip_drive(path) != path; }
void join_path_components | ( | char * | ret_path, | |
const char * | head, | |||
const char * | tail | |||
) |
Definition at line 180 of file path.c.
References IS_DIR_SEP, MAXPGPATH, skip_drive, snprintf(), and strlcpy().
Referenced by find_my_exec(), make_relative_path(), process_file(), resolve_symlinks(), and tokenize_inc_file().
{ if (ret_path != head) strlcpy(ret_path, head, MAXPGPATH); /* * Remove any leading "." in the tail component. * * Note: we used to try to remove ".." as well, but that's tricky to get * right; now we just leave it to be done by canonicalize_path() later. */ while (tail[0] == '.' && IS_DIR_SEP(tail[1])) tail += 2; if (*tail) { /* only separate with slash if head wasn't empty */ snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path), "%s%s", (*(skip_drive(head)) != '\0') ? "/" : "", tail); } }
char* last_dir_separator | ( | const char * | filename | ) |
Definition at line 130 of file path.c.
References IS_DIR_SEP, and skip_drive.
Referenced by ECPGconnect(), find_other_exec(), get_progname(), main(), resolve_symlinks(), setup(), and setup_bin_paths().
{ const char *p, *ret = NULL; for (p = skip_drive(filename); *p; p++) if (IS_DIR_SEP(*p)) ret = p; return (char *) ret; }
void make_native_path | ( | char * | filename | ) |
Definition at line 158 of file path.c.
Referenced by pgarch_archiveXlog(), and RestoreArchivedFile().
{ #ifdef WIN32 char *p; for (p = filename; *p; p++) if (*p == '/') *p = '\\'; #endif }
static void make_relative_path | ( | char * | ret_path, | |
const char * | target_path, | |||
const char * | bin_path, | |||
const char * | my_exec_path | |||
) | [static] |
Definition at line 501 of file path.c.
References canonicalize_path(), dir_strcmp(), i, IS_DIR_SEP, join_path_components(), MAXPGPATH, strlcpy(), trim_directory(), and trim_trailing_separator().
Referenced by get_doc_path(), get_etc_path(), get_html_path(), get_include_path(), get_includeserver_path(), get_lib_path(), get_locale_path(), get_man_path(), get_pkginclude_path(), get_pkglib_path(), and get_share_path().
{ int prefix_len; int tail_start; int tail_len; int i; /* * Determine the common prefix --- note we require it to end on a * directory separator, consider eg '/usr/lib' and '/usr/libexec'. */ prefix_len = 0; for (i = 0; target_path[i] && bin_path[i]; i++) { if (IS_DIR_SEP(target_path[i]) && IS_DIR_SEP(bin_path[i])) prefix_len = i + 1; else if (target_path[i] != bin_path[i]) break; } if (prefix_len == 0) goto no_match; /* no common prefix? */ tail_len = strlen(bin_path) - prefix_len; /* * Set up my_exec_path without the actual executable name, and * canonicalize to simplify comparison to bin_path. */ strlcpy(ret_path, my_exec_path, MAXPGPATH); trim_directory(ret_path); /* remove my executable name */ canonicalize_path(ret_path); /* * Tail match? */ tail_start = (int) strlen(ret_path) - tail_len; if (tail_start > 0 && IS_DIR_SEP(ret_path[tail_start - 1]) && dir_strcmp(ret_path + tail_start, bin_path + prefix_len) == 0) { ret_path[tail_start] = '\0'; trim_trailing_separator(ret_path); join_path_components(ret_path, ret_path, target_path + prefix_len); canonicalize_path(ret_path); return; } no_match: strlcpy(ret_path, target_path, MAXPGPATH); canonicalize_path(ret_path); }
bool path_contains_parent_reference | ( | const char * | path | ) |
Definition at line 338 of file path.c.
References NULL, and skip_drive.
Referenced by convert_and_check_filename(), and path_is_relative_and_below_cwd().
{ int path_len; path = skip_drive(path); /* C: shouldn't affect our conclusion */ path_len = strlen(path); /* * ".." could be the whole path; otherwise, if it's present it must be at * the beginning, in the middle, or at the end. */ if (strcmp(path, "..") == 0 || strncmp(path, "../", 3) == 0 || strstr(path, "/../") != NULL || (path_len >= 3 && strcmp(path + path_len - 3, "/..") == 0)) return true; return false; }
bool path_is_prefix_of_path | ( | const char * | path1, | |
const char * | path2 | |||
) |
Definition at line 400 of file path.c.
References IS_DIR_SEP.
Referenced by convert_and_check_filename(), and create_script_for_old_cluster_deletion().
{ int path1_len = strlen(path1); if (strncmp(path1, path2, path1_len) == 0 && (IS_DIR_SEP(path2[path1_len]) || path2[path1_len] == '\0')) return true; return false; }
bool path_is_relative_and_below_cwd | ( | const char * | path | ) |
Definition at line 367 of file path.c.
References is_absolute_path, IS_DIR_SEP, and path_contains_parent_reference().
Referenced by convert_and_check_filename().
{ if (is_absolute_path(path)) return false; /* don't allow anything above the cwd */ else if (path_contains_parent_reference(path)) return false; #ifdef WIN32 /* * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is * relative to the cwd on that drive, or the drive's root directory if * that drive has no cwd. Because the path itself cannot tell us which is * the case, we have to assume the worst, i.e. that it is not below the * cwd. We could use GetFullPathName() to find the full path but that * could change if the current directory for the drive changes underneath * us, so we just disallow it. */ else if (isalpha((unsigned char) path[0]) && path[1] == ':' && !IS_DIR_SEP(path[2])) return false; #endif else return true; }
static void trim_directory | ( | char * | path | ) | [static] |
Definition at line 719 of file path.c.
References IS_DIR_SEP, and skip_drive.
Referenced by canonicalize_path(), get_parent_directory(), and make_relative_path().
{ char *p; path = skip_drive(path); if (path[0] == '\0') return; /* back up over trailing slash(es) */ for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--) ; /* back up over directory name */ for (; !IS_DIR_SEP(*p) && p > path; p--) ; /* if multiple slashes before directory name, remove 'em all */ for (; p > path && IS_DIR_SEP(*(p - 1)); p--) ; /* don't erase a leading slash */ if (p == path && IS_DIR_SEP(*p)) p++; *p = '\0'; }
static void trim_trailing_separator | ( | char * | path | ) | [static] |
Definition at line 750 of file path.c.
References IS_DIR_SEP, and skip_drive.
Referenced by canonicalize_path(), and make_relative_path().
{ char *p; path = skip_drive(path); p = path + strlen(path); if (p > path) for (p--; p > path && IS_DIR_SEP(*p); p--) *p = '\0'; }