1 /*-------------------------------------------------------------------------
4 * portable path handling routines
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
24 #define _WIN32_IE 0x0500
34 #include "pg_config_paths.h"
38 #define IS_PATH_VAR_SEP(ch) ((ch) == ':')
40 #define IS_PATH_VAR_SEP(ch) ((ch) == ';')
43 static void make_relative_path(char *ret_path, const char *target_path,
44 const char *bin_path, const char *my_exec_path);
45 static void trim_directory(char *path);
46 static void trim_trailing_separator(char *path);
52 * On Windows, a path may begin with "C:" or "//network/". Advance over
53 * this and point to the effective start of the path.
58 skip_drive(const char *path)
60 if (IS_DIR_SEP(path[0]) && IS_DIR_SEP(path[1]))
63 while (*path && !IS_DIR_SEP(*path))
66 else if (isalpha((unsigned char) path[0]) && path[1] == ':')
74 #define skip_drive(path) (path)
80 * Return true if the given pathname has a drive prefix.
83 has_drive_prefix(const char *path)
85 return skip_drive(path) != path;
91 * Find the location of the first directory separator, return
95 first_dir_separator(const char *filename)
99 for (p = skip_drive(filename); *p; p++)
106 * first_path_var_separator
108 * Find the location of the first path separator (i.e. ':' on
109 * Unix, ';' on Windows), return NULL if not found.
112 first_path_var_separator(const char *pathlist)
116 /* skip_drive is not needed */
117 for (p = pathlist; *p; p++)
118 if (IS_PATH_VAR_SEP(*p))
126 * Find the location of the last directory separator, return
130 last_dir_separator(const char *filename)
135 for (p = skip_drive(filename); *p; p++)
143 * make_native_path - on WIN32, change / to \ in the path
145 * This effectively undoes canonicalize_path.
147 * This is required because WIN32 COPY is an internal CMD.EXE
148 * command and doesn't process forward slashes in the same way
149 * as external commands. Quoting the first argument to COPY
150 * does not convert forward to backward slashes, but COPY does
151 * properly process quoted forward slashes in the second argument.
153 * COPY works with quoted forward slashes in the first argument
154 * only if the current directory is the same as the directory
155 * of the first argument.
158 make_native_path(char *filename)
163 for (p = filename; *p; p++)
171 * join_path_components - join two path components, inserting a slash
173 * We omit the slash if either given component is empty.
175 * ret_path is the output area (must be of size MAXPGPATH)
177 * ret_path can be the same as head, but not the same as tail.
180 join_path_components(char *ret_path,
181 const char *head, const char *tail)
183 if (ret_path != head)
184 strlcpy(ret_path, head, MAXPGPATH);
187 * Remove any leading "." in the tail component.
189 * Note: we used to try to remove ".." as well, but that's tricky to get
190 * right; now we just leave it to be done by canonicalize_path() later.
192 while (tail[0] == '.' && IS_DIR_SEP(tail[1]))
197 /* only separate with slash if head wasn't empty */
198 snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path),
200 (*(skip_drive(head)) != '\0') ? "/" : "",
208 * o make Win32 path use Unix slashes
209 * o remove trailing quote on Win32
210 * o remove trailing slash
211 * o remove duplicate adjacent separators
212 * o remove trailing '.'
213 * o process trailing '..' ourselves
216 canonicalize_path(char *path)
221 bool was_sep = false;
227 * The Windows command processor will accept suitably quoted paths with
228 * forward slashes, but barfs badly with mixed forward and back slashes.
230 for (p = path; *p; p++)
237 * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass \c\d"
238 * as argv[2], so trim off trailing quote.
240 if (p > path && *(p - 1) == '"')
245 * Removing the trailing slash on a path means we never get ugly double
246 * trailing slashes. Also, Win32 can't stat() a directory with a trailing
247 * slash. Don't remove a leading slash, though.
249 trim_trailing_separator(path);
252 * Remove duplicate adjacent separators
256 /* Don't remove leading double-slash on Win32 */
261 for (; *p; p++, to_p++)
263 /* Handle many adjacent slashes, like "/a///b" */
264 while (*p == '/' && was_sep)
268 was_sep = (*p == '/');
273 * Remove any trailing uses of "." and process ".." ourselves
275 * Note that "/../.." should reduce to just "/", while "../.." has to be
276 * kept as-is. In the latter case we put back mistakenly trimmed ".."
277 * components below. Also note that we want a Windows drive spec to be
278 * visible to trim_directory(), but it's not part of the logic that's
279 * looking at the name components; hence distinction between path and
282 spath = skip_drive(path);
286 int len = strlen(spath);
288 if (len >= 2 && strcmp(spath + len - 2, "/.") == 0)
289 trim_directory(path);
290 else if (strcmp(spath, ".") == 0)
292 /* Want to leave "." alone, but "./.." has to become ".." */
293 if (pending_strips > 0)
297 else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) ||
298 strcmp(spath, "..") == 0)
300 trim_directory(path);
303 else if (pending_strips > 0 && *spath != '\0')
305 /* trim a regular directory name canceled by ".." */
306 trim_directory(path);
308 /* foo/.. should become ".", not empty */
316 if (pending_strips > 0)
319 * We could only get here if path is now totally empty (other than a
320 * possible drive specifier on Windows). We have to put back one or
321 * more ".."'s that we took off.
323 while (--pending_strips > 0)
330 * Detect whether a path contains any parent-directory references ("..")
332 * The input *must* have been put through canonicalize_path previously.
334 * This is a bit tricky because we mustn't be fooled by "..a.." (legal)
335 * nor "C:.." (legal on Unix but not Windows).
338 path_contains_parent_reference(const char *path)
342 path = skip_drive(path); /* C: shouldn't affect our conclusion */
344 path_len = strlen(path);
347 * ".." could be the whole path; otherwise, if it's present it must be at
348 * the beginning, in the middle, or at the end.
350 if (strcmp(path, "..") == 0 ||
351 strncmp(path, "../", 3) == 0 ||
352 strstr(path, "/../") != NULL ||
353 (path_len >= 3 && strcmp(path + path_len - 3, "/..") == 0))
360 * Detect whether a path is only in or below the current working directory.
361 * An absolute path that matches the current working directory should
362 * return false (we only want relative to the cwd). We don't allow
363 * "/../" even if that would keep us under the cwd (it is too hard to
367 path_is_relative_and_below_cwd(const char *path)
369 if (is_absolute_path(path))
371 /* don't allow anything above the cwd */
372 else if (path_contains_parent_reference(path))
377 * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is
378 * relative to the cwd on that drive, or the drive's root directory if
379 * that drive has no cwd. Because the path itself cannot tell us which is
380 * the case, we have to assume the worst, i.e. that it is not below the
381 * cwd. We could use GetFullPathName() to find the full path but that
382 * could change if the current directory for the drive changes underneath
383 * us, so we just disallow it.
385 else if (isalpha((unsigned char) path[0]) && path[1] == ':' &&
386 !IS_DIR_SEP(path[2]))
394 * Detect whether path1 is a prefix of path2 (including equality).
396 * This is pretty trivial, but it seems better to export a function than
397 * to export IS_DIR_SEP.
400 path_is_prefix_of_path(const char *path1, const char *path2)
402 int path1_len = strlen(path1);
404 if (strncmp(path1, path2, path1_len) == 0 &&
405 (IS_DIR_SEP(path2[path1_len]) || path2[path1_len] == '\0'))
411 * Extracts the actual name of the program as called -
412 * stripped of .exe suffix if any
415 get_progname(const char *argv0)
417 const char *nodir_name;
420 nodir_name = last_dir_separator(argv0);
424 nodir_name = skip_drive(argv0);
427 * Make a copy in case argv[0] is modified by ps_status. Leaks memory, but
430 progname = strdup(nodir_name);
431 if (progname == NULL)
433 fprintf(stderr, "%s: out of memory\n", nodir_name);
434 abort(); /* This could exit the postmaster */
437 #if defined(__CYGWIN__) || defined(WIN32)
438 /* strip ".exe" suffix, regardless of case */
439 if (strlen(progname) > sizeof(EXE) - 1 &&
440 pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0)
441 progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0';
449 * dir_strcmp: strcmp except any two DIR_SEP characters are considered equal,
450 * and we honor filesystem case insensitivity if known
453 dir_strcmp(const char *s1, const char *s2)
461 /* On windows, paths are case-insensitive */
462 pg_tolower((unsigned char) *s1) != pg_tolower((unsigned char) *s2)
464 && !(IS_DIR_SEP(*s1) && IS_DIR_SEP(*s2)))
465 return (int) *s1 - (int) *s2;
469 return 1; /* s1 longer */
471 return -1; /* s2 longer */
477 * make_relative_path - make a path relative to the actual binary location
479 * This function exists to support relocation of installation trees.
481 * ret_path is the output area (must be of size MAXPGPATH)
482 * target_path is the compiled-in path to the directory we want to find
483 * bin_path is the compiled-in path to the directory of executables
484 * my_exec_path is the actual location of my executable
486 * We determine the common prefix of target_path and bin_path, then compare
487 * the remainder of bin_path to the last directory component(s) of
488 * my_exec_path. If they match, build the result as the part of my_exec_path
489 * preceding the match, joined to the remainder of target_path. If no match,
490 * return target_path as-is.
493 * target_path = '/usr/local/share/postgresql'
494 * bin_path = '/usr/local/bin'
495 * my_exec_path = '/opt/pgsql/bin/postmaster'
496 * Given these inputs, the common prefix is '/usr/local/', the tail of
497 * bin_path is 'bin' which does match the last directory component of
498 * my_exec_path, so we would return '/opt/pgsql/share/postgresql'
501 make_relative_path(char *ret_path, const char *target_path,
502 const char *bin_path, const char *my_exec_path)
510 * Determine the common prefix --- note we require it to end on a
511 * directory separator, consider eg '/usr/lib' and '/usr/libexec'.
514 for (i = 0; target_path[i] && bin_path[i]; i++)
516 if (IS_DIR_SEP(target_path[i]) && IS_DIR_SEP(bin_path[i]))
518 else if (target_path[i] != bin_path[i])
522 goto no_match; /* no common prefix? */
523 tail_len = strlen(bin_path) - prefix_len;
526 * Set up my_exec_path without the actual executable name, and
527 * canonicalize to simplify comparison to bin_path.
529 strlcpy(ret_path, my_exec_path, MAXPGPATH);
530 trim_directory(ret_path); /* remove my executable name */
531 canonicalize_path(ret_path);
536 tail_start = (int) strlen(ret_path) - tail_len;
537 if (tail_start > 0 &&
538 IS_DIR_SEP(ret_path[tail_start - 1]) &&
539 dir_strcmp(ret_path + tail_start, bin_path + prefix_len) == 0)
541 ret_path[tail_start] = '\0';
542 trim_trailing_separator(ret_path);
543 join_path_components(ret_path, ret_path, target_path + prefix_len);
544 canonicalize_path(ret_path);
549 strlcpy(ret_path, target_path, MAXPGPATH);
550 canonicalize_path(ret_path);
558 get_share_path(const char *my_exec_path, char *ret_path)
560 make_relative_path(ret_path, PGSHAREDIR, PGBINDIR, my_exec_path);
567 get_etc_path(const char *my_exec_path, char *ret_path)
569 make_relative_path(ret_path, SYSCONFDIR, PGBINDIR, my_exec_path);
576 get_include_path(const char *my_exec_path, char *ret_path)
578 make_relative_path(ret_path, INCLUDEDIR, PGBINDIR, my_exec_path);
582 * get_pkginclude_path
585 get_pkginclude_path(const char *my_exec_path, char *ret_path)
587 make_relative_path(ret_path, PKGINCLUDEDIR, PGBINDIR, my_exec_path);
591 * get_includeserver_path
594 get_includeserver_path(const char *my_exec_path, char *ret_path)
596 make_relative_path(ret_path, INCLUDEDIRSERVER, PGBINDIR, my_exec_path);
603 get_lib_path(const char *my_exec_path, char *ret_path)
605 make_relative_path(ret_path, LIBDIR, PGBINDIR, my_exec_path);
612 get_pkglib_path(const char *my_exec_path, char *ret_path)
614 make_relative_path(ret_path, PKGLIBDIR, PGBINDIR, my_exec_path);
621 get_locale_path(const char *my_exec_path, char *ret_path)
623 make_relative_path(ret_path, LOCALEDIR, PGBINDIR, my_exec_path);
630 get_doc_path(const char *my_exec_path, char *ret_path)
632 make_relative_path(ret_path, DOCDIR, PGBINDIR, my_exec_path);
639 get_html_path(const char *my_exec_path, char *ret_path)
641 make_relative_path(ret_path, HTMLDIR, PGBINDIR, my_exec_path);
648 get_man_path(const char *my_exec_path, char *ret_path)
650 make_relative_path(ret_path, MANDIR, PGBINDIR, my_exec_path);
657 * On Unix, this actually returns the user's home directory. On Windows
658 * it returns the PostgreSQL-specific application data folder.
661 get_home_path(char *ret_path)
665 struct passwd pwdstr;
666 struct passwd *pwd = NULL;
668 if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
670 strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
676 * Note: We use getenv here because the more modern
677 * SHGetSpecialFolderPath() will force us to link with shell32.lib which
678 * eats valuable desktop heap.
680 tmppath = getenv("APPDATA");
683 snprintf(ret_path, MAXPGPATH, "%s/postgresql", tmppath);
690 * get_parent_directory
692 * Modify the given string in-place to name the parent directory of the
695 * If the input is just a file name with no directory part, the result is
696 * an empty string, not ".". This is appropriate when the next step is
697 * join_path_components(), but might need special handling otherwise.
699 * Caution: this will not produce desirable results if the string ends
700 * with "..". For most callers this is not a problem since the string
701 * is already known to name a regular file. If in doubt, apply
702 * canonicalize_path() first.
705 get_parent_directory(char *path)
707 trim_directory(path);
714 * Trim trailing directory from path, that is, remove any trailing slashes,
715 * the last pathname component, and the slash just ahead of it --- but never
716 * remove a leading slash.
719 trim_directory(char *path)
723 path = skip_drive(path);
728 /* back up over trailing slash(es) */
729 for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--)
731 /* back up over directory name */
732 for (; !IS_DIR_SEP(*p) && p > path; p--)
734 /* if multiple slashes before directory name, remove 'em all */
735 for (; p > path && IS_DIR_SEP(*(p - 1)); p--)
737 /* don't erase a leading slash */
738 if (p == path && IS_DIR_SEP(*p))
745 * trim_trailing_separator
747 * trim off trailing slashes, but not a leading slash
750 trim_trailing_separator(char *path)
754 path = skip_drive(path);
755 p = path + strlen(path);
757 for (p--; p > path && IS_DIR_SEP(*p); p--)