1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-14 02:22:38 +03:00

Make sure -D is an absolute path when starting server on Windows.

This is needed because Windows services may get started with a different
current directory than where pg_ctl is executed.  We want relative -D
paths to be interpreted relative to pg_ctl's CWD, similarly to what
happens on other platforms.

In support of this, move the backend's make_absolute_path() function
into src/port/path.c (where it probably should have been long since)
and get rid of the rather inferior version in pg_regress.

Kumar Rajeev Rastogi, reviewed by MauMau
This commit is contained in:
Tom Lane
2014-04-04 18:42:13 -04:00
parent 8120c7452a
commit 9aca512506
6 changed files with 124 additions and 101 deletions

View File

@@ -13,7 +13,11 @@
*-------------------------------------------------------------------------
*/
#include "c.h"
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#include <ctype.h>
#include <sys/stat.h>
@@ -549,6 +553,111 @@ no_match:
}
/*
* make_absolute_path
*
* If the given pathname isn't already absolute, make it so, interpreting
* it relative to the current working directory.
*
* Also canonicalizes the path. The result is always a malloc'd copy.
*
* In backend, failure cases result in ereport(ERROR); in frontend,
* we write a complaint on stderr and return NULL.
*
* Note: interpretation of relative-path arguments during postmaster startup
* should happen before doing ChangeToDataDir(), else the user will probably
* not like the results.
*/
char *
make_absolute_path(const char *path)
{
char *new;
/* Returning null for null input is convenient for some callers */
if (path == NULL)
return NULL;
if (!is_absolute_path(path))
{
char *buf;
size_t buflen;
buflen = MAXPGPATH;
for (;;)
{
buf = malloc(buflen);
if (!buf)
{
#ifndef FRONTEND
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
#else
fprintf(stderr, _("out of memory\n"));
return NULL;
#endif
}
if (getcwd(buf, buflen))
break;
else if (errno == ERANGE)
{
free(buf);
buflen *= 2;
continue;
}
else
{
free(buf);
#ifndef FRONTEND
elog(ERROR, "could not get current working directory: %m");
#else
fprintf(stderr, _("could not get current working directory: %s\n"),
strerror(errno));
return NULL;
#endif
}
}
new = malloc(strlen(buf) + strlen(path) + 2);
if (!new)
{
free(buf);
#ifndef FRONTEND
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
#else
fprintf(stderr, _("out of memory\n"));
return NULL;
#endif
}
sprintf(new, "%s/%s", buf, path);
free(buf);
}
else
{
new = strdup(path);
if (!new)
{
#ifndef FRONTEND
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
#else
fprintf(stderr, _("out of memory\n"));
return NULL;
#endif
}
}
/* Make sure punctuation is canonical, too */
canonicalize_path(new);
return new;
}
/*
* get_share_path
*/