PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
path.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include "pg_config_paths.h"
Include dependency graph for path.c:

Go to the source code of this file.

Macros

#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 cleanup_path (char *path)
 
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)
 
char * make_absolute_path (const char *path)
 
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)
 

Macro Definition Documentation

#define IS_PATH_VAR_SEP (   ch)    ((ch) == ':')

Definition at line 42 of file path.c.

Referenced by first_path_var_separator().

Function Documentation

void canonicalize_path ( char *  path)

Definition at line 254 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_or_symlink(), CreateTableSpace(), do_copy(), exec_command_edit(), exec_command_write(), find_in_dynamic_libpath(), find_my_exec(), find_other_exec(), main(), make_absolute_path(), make_relative_path(), parseCommandLine(), process_file(), resolve_symlinks(), set_pglocale_pgservice(), setup(), setup_bin_paths(), setup_pgdata(), SplitDirectoriesString(), tablespace_list_append(), and tokenize_inc_file().

255 {
256  char *p,
257  *to_p;
258  char *spath;
259  bool was_sep = false;
260  int pending_strips;
261 
262 #ifdef WIN32
263 
264  /*
265  * The Windows command processor will accept suitably quoted paths with
266  * forward slashes, but barfs badly with mixed forward and back slashes.
267  */
268  for (p = path; *p; p++)
269  {
270  if (*p == '\\')
271  *p = '/';
272  }
273 
274  /*
275  * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass \c\d"
276  * as argv[2], so trim off trailing quote.
277  */
278  if (p > path && *(p - 1) == '"')
279  *(p - 1) = '/';
280 #endif
281 
282  /*
283  * Removing the trailing slash on a path means we never get ugly double
284  * trailing slashes. Also, Win32 can't stat() a directory with a trailing
285  * slash. Don't remove a leading slash, though.
286  */
288 
289  /*
290  * Remove duplicate adjacent separators
291  */
292  p = path;
293 #ifdef WIN32
294  /* Don't remove leading double-slash on Win32 */
295  if (*p)
296  p++;
297 #endif
298  to_p = p;
299  for (; *p; p++, to_p++)
300  {
301  /* Handle many adjacent slashes, like "/a///b" */
302  while (*p == '/' && was_sep)
303  p++;
304  if (to_p != p)
305  *to_p = *p;
306  was_sep = (*p == '/');
307  }
308  *to_p = '\0';
309 
310  /*
311  * Remove any trailing uses of "." and process ".." ourselves
312  *
313  * Note that "/../.." should reduce to just "/", while "../.." has to be
314  * kept as-is. In the latter case we put back mistakenly trimmed ".."
315  * components below. Also note that we want a Windows drive spec to be
316  * visible to trim_directory(), but it's not part of the logic that's
317  * looking at the name components; hence distinction between path and
318  * spath.
319  */
320  spath = skip_drive(path);
321  pending_strips = 0;
322  for (;;)
323  {
324  int len = strlen(spath);
325 
326  if (len >= 2 && strcmp(spath + len - 2, "/.") == 0)
327  trim_directory(path);
328  else if (strcmp(spath, ".") == 0)
329  {
330  /* Want to leave "." alone, but "./.." has to become ".." */
331  if (pending_strips > 0)
332  *spath = '\0';
333  break;
334  }
335  else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) ||
336  strcmp(spath, "..") == 0)
337  {
338  trim_directory(path);
339  pending_strips++;
340  }
341  else if (pending_strips > 0 && *spath != '\0')
342  {
343  /* trim a regular directory name canceled by ".." */
344  trim_directory(path);
345  pending_strips--;
346  /* foo/.. should become ".", not empty */
347  if (*spath == '\0')
348  strcpy(spath, ".");
349  }
350  else
351  break;
352  }
353 
354  if (pending_strips > 0)
355  {
356  /*
357  * We could only get here if path is now totally empty (other than a
358  * possible drive specifier on Windows). We have to put back one or
359  * more ".."'s that we took off.
360  */
361  while (--pending_strips > 0)
362  strcat(path, "../");
363  strcat(path, "..");
364  }
365 }
static void trim_directory(char *path)
Definition: path.c:868
#define skip_drive(path)
Definition: path.c:78
static void trim_trailing_separator(char *path)
Definition: path.c:899
void cleanup_path ( char *  path)

Definition at line 185 of file path.c.

References MAXPGPATH.

Referenced by get_configdata().

186 {
187 #ifdef WIN32
188  char *ptr;
189 
190  /*
191  * GetShortPathName() will fail if the path does not exist, or short names
192  * are disabled on this file system. In both cases, we just return the
193  * original path. This is particularly useful for --sysconfdir, which
194  * might not exist.
195  */
196  GetShortPathName(path, path, MAXPGPATH - 1);
197 
198  /* Replace '\' with '/' */
199  for (ptr = path; *ptr; ptr++)
200  {
201  if (*ptr == '\\')
202  *ptr = '/';
203  }
204 #endif
205 }
#define MAXPGPATH
static int dir_strcmp ( const char *  s1,
const char *  s2 
)
static

Definition at line 491 of file path.c.

References IS_DIR_SEP, and pg_tolower().

Referenced by make_relative_path().

492 {
493  while (*s1 && *s2)
494  {
495  if (
496 #ifndef WIN32
497  *s1 != *s2
498 #else
499  /* On windows, paths are case-insensitive */
500  pg_tolower((unsigned char) *s1) != pg_tolower((unsigned char) *s2)
501 #endif
502  && !(IS_DIR_SEP(*s1) && IS_DIR_SEP(*s2)))
503  return (int) *s1 - (int) *s2;
504  s1++, s2++;
505  }
506  if (*s1)
507  return 1; /* s1 longer */
508  if (*s2)
509  return -1; /* s2 longer */
510  return 0;
511 }
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
#define IS_DIR_SEP(ch)
Definition: port.h:75
char * s1
char * s2
char* first_dir_separator ( const char *  filename)

Definition at line 103 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().

104 {
105  const char *p;
106 
107  for (p = skip_drive(filename); *p; p++)
108  if (IS_DIR_SEP(*p))
109  return (char *) p;
110  return NULL;
111 }
#define skip_drive(path)
Definition: path.c:78
#define IS_DIR_SEP(ch)
Definition: port.h:75
static char * filename
Definition: pg_dumpall.c:90
char* first_path_var_separator ( const char *  pathlist)

Definition at line 120 of file path.c.

References IS_PATH_VAR_SEP.

Referenced by find_in_dynamic_libpath(), and find_my_exec().

121 {
122  const char *p;
123 
124  /* skip_drive is not needed */
125  for (p = pathlist; *p; p++)
126  if (IS_PATH_VAR_SEP(*p))
127  return (char *) p;
128  return NULL;
129 }
#define IS_PATH_VAR_SEP(ch)
Definition: path.c:42
void get_doc_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 776 of file path.c.

References make_relative_path().

Referenced by get_configdata().

777 {
778  make_relative_path(ret_path, DOCDIR, PGBINDIR, my_exec_path);
779 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_etc_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 713 of file path.c.

References make_relative_path().

Referenced by get_configdata(), process_psqlrc(), and set_pglocale_pgservice().

714 {
715  make_relative_path(ret_path, SYSCONFDIR, PGBINDIR, my_exec_path);
716 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
bool get_home_path ( char *  ret_path)

Definition at line 807 of file path.c.

References MAXPGPATH, pqGetpwuid(), snprintf(), and strlcpy().

Referenced by expand_tilde(), initializeInput(), and process_psqlrc().

808 {
809 #ifndef WIN32
810  char pwdbuf[BUFSIZ];
811  struct passwd pwdstr;
812  struct passwd *pwd = NULL;
813 
814  (void) pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd);
815  if (pwd == NULL)
816  return false;
817  strlcpy(ret_path, pwd->pw_dir, MAXPGPATH);
818  return true;
819 #else
820  char *tmppath;
821 
822  /*
823  * Note: We use getenv() here because the more modern SHGetFolderPath()
824  * would force the backend to link with shell32.lib, which eats valuable
825  * desktop heap. XXX This function is used only in psql, which already
826  * brings in shell32 via libpq. Moving this function to its own file
827  * would keep it out of the backend, freeing it from this concern.
828  */
829  tmppath = getenv("APPDATA");
830  if (!tmppath)
831  return false;
832  snprintf(ret_path, MAXPGPATH, "%s/postgresql", tmppath);
833  return true;
834 #endif
835 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, size_t buflen, struct passwd **result)
Definition: thread.c:95
void get_html_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 785 of file path.c.

References make_relative_path().

Referenced by get_configdata().

786 {
787  make_relative_path(ret_path, HTMLDIR, PGBINDIR, my_exec_path);
788 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_include_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 722 of file path.c.

References make_relative_path().

Referenced by get_configdata(), and main().

723 {
724  make_relative_path(ret_path, INCLUDEDIR, PGBINDIR, my_exec_path);
725 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_includeserver_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 740 of file path.c.

References make_relative_path().

Referenced by get_configdata().

741 {
742  make_relative_path(ret_path, INCLUDEDIRSERVER, PGBINDIR, my_exec_path);
743 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_lib_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 749 of file path.c.

References make_relative_path().

Referenced by get_configdata().

750 {
751  make_relative_path(ret_path, LIBDIR, PGBINDIR, my_exec_path);
752 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_locale_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 767 of file path.c.

References make_relative_path().

Referenced by get_configdata(), pg_bindtextdomain(), and set_pglocale_pgservice().

768 {
769  make_relative_path(ret_path, LOCALEDIR, PGBINDIR, my_exec_path);
770 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_man_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 794 of file path.c.

References make_relative_path().

Referenced by get_configdata().

795 {
796  make_relative_path(ret_path, MANDIR, PGBINDIR, my_exec_path);
797 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_parent_directory ( char *  path)

Definition at line 854 of file path.c.

References trim_directory().

Referenced by destroy_tablespace_directories(), fsync_parent_path(), main(), process_file(), TablespaceCreateDbspace(), and tokenize_inc_file().

855 {
856  trim_directory(path);
857 }
static void trim_directory(char *path)
Definition: path.c:868
void get_pkginclude_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 731 of file path.c.

References make_relative_path().

Referenced by get_configdata(), and main().

732 {
733  make_relative_path(ret_path, PKGINCLUDEDIR, PGBINDIR, my_exec_path);
734 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
void get_pkglib_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 758 of file path.c.

References make_relative_path().

Referenced by get_configdata(), getInstallationPaths(), and InitStandaloneProcess().

759 {
760  make_relative_path(ret_path, PKGLIBDIR, PGBINDIR, my_exec_path);
761 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
const char* get_progname ( const char *  argv0)

Definition at line 453 of file path.c.

References EXE, last_dir_separator(), pg_strcasecmp(), progname, and skip_drive.

Referenced by get_opts(), handle_help_version_opts(), main(), parseCommandLine(), and regression_main().

454 {
455  const char *nodir_name;
456  char *progname;
457 
458  nodir_name = last_dir_separator(argv0);
459  if (nodir_name)
460  nodir_name++;
461  else
462  nodir_name = skip_drive(argv0);
463 
464  /*
465  * Make a copy in case argv[0] is modified by ps_status. Leaks memory, but
466  * called only once.
467  */
468  progname = strdup(nodir_name);
469  if (progname == NULL)
470  {
471  fprintf(stderr, "%s: out of memory\n", nodir_name);
472  abort(); /* This could exit the postmaster */
473  }
474 
475 #if defined(__CYGWIN__) || defined(WIN32)
476  /* strip ".exe" suffix, regardless of case */
477  if (strlen(progname) > sizeof(EXE) - 1 &&
478  pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0)
479  progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0';
480 #endif
481 
482  return progname;
483 }
static char * argv0
Definition: pg_ctl.c:101
#define skip_drive(path)
Definition: path.c:78
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * progname
Definition: pg_standby.c:37
char * last_dir_separator(const char *filename)
Definition: path.c:138
#define EXE
Definition: port.h:113
void get_share_path ( const char *  my_exec_path,
char *  ret_path 
)

Definition at line 704 of file path.c.

References make_relative_path().

Referenced by get_configdata(), get_extension_control_directory(), get_extension_control_filename(), get_extension_script_directory(), get_tsearch_config_filename(), ParseTzFile(), pg_TZDIR(), and setup_bin_paths().

705 {
706  make_relative_path(ret_path, PGSHAREDIR, PGBINDIR, my_exec_path);
707 }
static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
Definition: path.c:539
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
bool has_drive_prefix ( const char *  path)

Definition at line 87 of file path.c.

References skip_drive.

Referenced by process_file().

88 {
89 #ifdef WIN32
90  return skip_drive(path) != path;
91 #else
92  return false;
93 #endif
94 }
#define skip_drive(path)
Definition: path.c:78
void join_path_components ( char *  ret_path,
const char *  head,
const char *  tail 
)

Definition at line 218 of file path.c.

References IS_DIR_SEP, MAXPGPATH, skip_drive, snprintf(), and strlcpy().

Referenced by find_my_exec(), main(), make_relative_path(), process_file(), resolve_symlinks(), and tokenize_inc_file().

220 {
221  if (ret_path != head)
222  strlcpy(ret_path, head, MAXPGPATH);
223 
224  /*
225  * Remove any leading "." in the tail component.
226  *
227  * Note: we used to try to remove ".." as well, but that's tricky to get
228  * right; now we just leave it to be done by canonicalize_path() later.
229  */
230  while (tail[0] == '.' && IS_DIR_SEP(tail[1]))
231  tail += 2;
232 
233  if (*tail)
234  {
235  /* only separate with slash if head wasn't empty */
236  snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path),
237  "%s%s",
238  (*(skip_drive(head)) != '\0') ? "/" : "",
239  tail);
240  }
241 }
#define skip_drive(path)
Definition: path.c:78
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define IS_DIR_SEP(ch)
Definition: port.h:75
#define MAXPGPATH
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char* last_dir_separator ( const char *  filename)

Definition at line 138 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().

139 {
140  const char *p,
141  *ret = NULL;
142 
143  for (p = skip_drive(filename); *p; p++)
144  if (IS_DIR_SEP(*p))
145  ret = p;
146  return (char *) ret;
147 }
#define skip_drive(path)
Definition: path.c:78
#define IS_DIR_SEP(ch)
Definition: port.h:75
static char * filename
Definition: pg_dumpall.c:90
char* make_absolute_path ( const char *  path)

Definition at line 608 of file path.c.

References _, buf, canonicalize_path(), elog, ereport, errcode(), errmsg(), ERROR, free, is_absolute_path, malloc, MAXPGPATH, and strerror().

Referenced by regression_main(), SelectConfigFiles(), and SetDataDir().

609 {
610  char *new;
611 
612  /* Returning null for null input is convenient for some callers */
613  if (path == NULL)
614  return NULL;
615 
616  if (!is_absolute_path(path))
617  {
618  char *buf;
619  size_t buflen;
620 
621  buflen = MAXPGPATH;
622  for (;;)
623  {
624  buf = malloc(buflen);
625  if (!buf)
626  {
627 #ifndef FRONTEND
628  ereport(ERROR,
629  (errcode(ERRCODE_OUT_OF_MEMORY),
630  errmsg("out of memory")));
631 #else
632  fprintf(stderr, _("out of memory\n"));
633  return NULL;
634 #endif
635  }
636 
637  if (getcwd(buf, buflen))
638  break;
639  else if (errno == ERANGE)
640  {
641  free(buf);
642  buflen *= 2;
643  continue;
644  }
645  else
646  {
647  int save_errno = errno;
648 
649  free(buf);
650  errno = save_errno;
651 #ifndef FRONTEND
652  elog(ERROR, "could not get current working directory: %m");
653 #else
654  fprintf(stderr, _("could not get current working directory: %s\n"),
655  strerror(errno));
656  return NULL;
657 #endif
658  }
659  }
660 
661  new = malloc(strlen(buf) + strlen(path) + 2);
662  if (!new)
663  {
664  free(buf);
665 #ifndef FRONTEND
666  ereport(ERROR,
667  (errcode(ERRCODE_OUT_OF_MEMORY),
668  errmsg("out of memory")));
669 #else
670  fprintf(stderr, _("out of memory\n"));
671  return NULL;
672 #endif
673  }
674  sprintf(new, "%s/%s", buf, path);
675  free(buf);
676  }
677  else
678  {
679  new = strdup(path);
680  if (!new)
681  {
682 #ifndef FRONTEND
683  ereport(ERROR,
684  (errcode(ERRCODE_OUT_OF_MEMORY),
685  errmsg("out of memory")));
686 #else
687  fprintf(stderr, _("out of memory\n"));
688  return NULL;
689 #endif
690  }
691  }
692 
693  /* Make sure punctuation is canonical, too */
694  canonicalize_path(new);
695 
696  return new;
697 }
void canonicalize_path(char *path)
Definition: path.c:254
int errcode(int sqlerrcode)
Definition: elog.c:575
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
#define is_absolute_path(filename)
Definition: port.h:77
#define ereport(elevel, rest)
Definition: elog.h:122
#define free(a)
Definition: header.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
#define elog
Definition: elog.h:219
#define _(x)
Definition: elog.c:84
void make_native_path ( char *  filename)

Definition at line 166 of file path.c.

Referenced by pgarch_archiveXlog(), and RestoreArchivedFile().

167 {
168 #ifdef WIN32
169  char *p;
170 
171  for (p = filename; *p; p++)
172  if (*p == '/')
173  *p = '\\';
174 #endif
175 }
static char * filename
Definition: pg_dumpall.c:90
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 539 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().

541 {
542  int prefix_len;
543  int tail_start;
544  int tail_len;
545  int i;
546 
547  /*
548  * Determine the common prefix --- note we require it to end on a
549  * directory separator, consider eg '/usr/lib' and '/usr/libexec'.
550  */
551  prefix_len = 0;
552  for (i = 0; target_path[i] && bin_path[i]; i++)
553  {
554  if (IS_DIR_SEP(target_path[i]) && IS_DIR_SEP(bin_path[i]))
555  prefix_len = i + 1;
556  else if (target_path[i] != bin_path[i])
557  break;
558  }
559  if (prefix_len == 0)
560  goto no_match; /* no common prefix? */
561  tail_len = strlen(bin_path) - prefix_len;
562 
563  /*
564  * Set up my_exec_path without the actual executable name, and
565  * canonicalize to simplify comparison to bin_path.
566  */
567  strlcpy(ret_path, my_exec_path, MAXPGPATH);
568  trim_directory(ret_path); /* remove my executable name */
569  canonicalize_path(ret_path);
570 
571  /*
572  * Tail match?
573  */
574  tail_start = (int) strlen(ret_path) - tail_len;
575  if (tail_start > 0 &&
576  IS_DIR_SEP(ret_path[tail_start - 1]) &&
577  dir_strcmp(ret_path + tail_start, bin_path + prefix_len) == 0)
578  {
579  ret_path[tail_start] = '\0';
580  trim_trailing_separator(ret_path);
581  join_path_components(ret_path, ret_path, target_path + prefix_len);
582  canonicalize_path(ret_path);
583  return;
584  }
585 
586 no_match:
587  strlcpy(ret_path, target_path, MAXPGPATH);
588  canonicalize_path(ret_path);
589 }
void canonicalize_path(char *path)
Definition: path.c:254
static void trim_directory(char *path)
Definition: path.c:868
#define IS_DIR_SEP(ch)
Definition: port.h:75
#define MAXPGPATH
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
static void trim_trailing_separator(char *path)
Definition: path.c:899
static int dir_strcmp(const char *s1, const char *s2)
Definition: path.c:491
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:218
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char bin_path[MAXPGPATH]
Definition: initdb.c:225
int i
bool path_contains_parent_reference ( const char *  path)

Definition at line 376 of file path.c.

References skip_drive.

Referenced by convert_and_check_filename(), and path_is_relative_and_below_cwd().

377 {
378  int path_len;
379 
380  path = skip_drive(path); /* C: shouldn't affect our conclusion */
381 
382  path_len = strlen(path);
383 
384  /*
385  * ".." could be the whole path; otherwise, if it's present it must be at
386  * the beginning, in the middle, or at the end.
387  */
388  if (strcmp(path, "..") == 0 ||
389  strncmp(path, "../", 3) == 0 ||
390  strstr(path, "/../") != NULL ||
391  (path_len >= 3 && strcmp(path + path_len - 3, "/..") == 0))
392  return true;
393 
394  return false;
395 }
#define skip_drive(path)
Definition: path.c:78
bool path_is_prefix_of_path ( const char *  path1,
const char *  path2 
)

Definition at line 438 of file path.c.

References IS_DIR_SEP.

Referenced by convert_and_check_filename(), create_script_for_old_cluster_deletion(), CreateTableSpace(), and parseCommandLine().

439 {
440  int path1_len = strlen(path1);
441 
442  if (strncmp(path1, path2, path1_len) == 0 &&
443  (IS_DIR_SEP(path2[path1_len]) || path2[path1_len] == '\0'))
444  return true;
445  return false;
446 }
#define IS_DIR_SEP(ch)
Definition: port.h:75
bool path_is_relative_and_below_cwd ( const char *  path)

Definition at line 405 of file path.c.

References is_absolute_path, IS_DIR_SEP, and path_contains_parent_reference().

Referenced by convert_and_check_filename().

406 {
407  if (is_absolute_path(path))
408  return false;
409  /* don't allow anything above the cwd */
410  else if (path_contains_parent_reference(path))
411  return false;
412 #ifdef WIN32
413 
414  /*
415  * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is
416  * relative to the cwd on that drive, or the drive's root directory if
417  * that drive has no cwd. Because the path itself cannot tell us which is
418  * the case, we have to assume the worst, i.e. that it is not below the
419  * cwd. We could use GetFullPathName() to find the full path but that
420  * could change if the current directory for the drive changes underneath
421  * us, so we just disallow it.
422  */
423  else if (isalpha((unsigned char) path[0]) && path[1] == ':' &&
424  !IS_DIR_SEP(path[2]))
425  return false;
426 #endif
427  else
428  return true;
429 }
#define IS_DIR_SEP(ch)
Definition: port.h:75
bool path_contains_parent_reference(const char *path)
Definition: path.c:376
#define is_absolute_path(filename)
Definition: port.h:77
static void trim_directory ( char *  path)
static

Definition at line 868 of file path.c.

References IS_DIR_SEP, and skip_drive.

Referenced by canonicalize_path(), get_parent_directory(), and make_relative_path().

869 {
870  char *p;
871 
872  path = skip_drive(path);
873 
874  if (path[0] == '\0')
875  return;
876 
877  /* back up over trailing slash(es) */
878  for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--)
879  ;
880  /* back up over directory name */
881  for (; !IS_DIR_SEP(*p) && p > path; p--)
882  ;
883  /* if multiple slashes before directory name, remove 'em all */
884  for (; p > path && IS_DIR_SEP(*(p - 1)); p--)
885  ;
886  /* don't erase a leading slash */
887  if (p == path && IS_DIR_SEP(*p))
888  p++;
889  *p = '\0';
890 }
#define skip_drive(path)
Definition: path.c:78
#define IS_DIR_SEP(ch)
Definition: port.h:75
static void trim_trailing_separator ( char *  path)
static

Definition at line 899 of file path.c.

References IS_DIR_SEP, and skip_drive.

Referenced by canonicalize_path(), and make_relative_path().

900 {
901  char *p;
902 
903  path = skip_drive(path);
904  p = path + strlen(path);
905  if (p > path)
906  for (p--; p > path && IS_DIR_SEP(*p); p--)
907  *p = '\0';
908 }
#define skip_drive(path)
Definition: path.c:78
#define IS_DIR_SEP(ch)
Definition: port.h:75