Ensure functions in dirutil.c do not directly modify the provided path

This commit is contained in:
Ian Barwick
2019-01-16 17:24:31 +09:00
parent 8881b69c06
commit ff0e480fdd
2 changed files with 19 additions and 15 deletions

View File

@@ -50,7 +50,7 @@ typedef long pgpid_t;
* and tablespace directories. * and tablespace directories.
*/ */
DataDirState DataDirState
check_dir(char *path) check_dir(const char *path)
{ {
DIR *chkdir = NULL; DIR *chkdir = NULL;
struct dirent *file = NULL; struct dirent *file = NULL;
@@ -91,12 +91,17 @@ check_dir(char *path)
* Create directory with error log message when failing * Create directory with error log message when failing
*/ */
bool bool
create_dir(char *path) create_dir(const char *path)
{ {
if (mkdir_p(path, 0700) == 0) char create_dir_path[MAXPGPATH];
/* mkdir_p() may modify the supplied path */
strncpy(create_dir_path, path, MAXPGPATH);
if (mkdir_p(create_dir_path, 0700) == 0)
return true; return true;
log_error(_("unable to create directory \"%s\""), path); log_error(_("unable to create directory \"%s\""), create_dir_path);
log_detail("%s", strerror(errno)); log_detail("%s", strerror(errno));
return false; return false;
@@ -104,13 +109,12 @@ create_dir(char *path)
bool bool
set_dir_permissions(char *path) set_dir_permissions(const char *path)
{ {
return (chmod(path, 0700) != 0) ? false : true; return (chmod(path, 0700) != 0) ? false : true;
} }
/* function from initdb.c */ /* function from initdb.c */
/* source adapted from FreeBSD /src/bin/mkdir/mkdir.c */ /* source adapted from FreeBSD /src/bin/mkdir/mkdir.c */
@@ -223,7 +227,7 @@ is_pg_dir(const char *path)
* any further useful progress can be made. * any further useful progress can be made.
*/ */
PgDirState PgDirState
is_pg_running(char *path) is_pg_running(const char *path)
{ {
long pid; long pid;
FILE *pidf; FILE *pidf;
@@ -291,7 +295,7 @@ is_pg_running(char *path)
bool bool
create_pg_dir(char *path, bool force) create_pg_dir(const char *path, bool force)
{ {
/* Check this directory can be used as a PGDATA dir */ /* Check this directory can be used as a PGDATA dir */
switch (check_dir(path)) switch (check_dir(path))
@@ -358,7 +362,7 @@ create_pg_dir(char *path, bool force)
int int
rmdir_recursive(char *path) rmdir_recursive(const char *path)
{ {
return nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS); return nftw(path, unlink_dir_callback, 64, FTW_DEPTH | FTW_PHYS);
} }

View File

@@ -35,13 +35,13 @@ typedef enum
} PgDirState; } PgDirState;
extern int mkdir_p(char *path, mode_t omode); extern int mkdir_p(char *path, mode_t omode);
extern bool set_dir_permissions(char *path); extern bool set_dir_permissions(const char *path);
extern DataDirState check_dir(char *path); extern DataDirState check_dir(const char *path);
extern bool create_dir(char *path); extern bool create_dir(const char *path);
extern bool is_pg_dir(const char *path); extern bool is_pg_dir(const char *path);
extern PgDirState is_pg_running(char *path); extern PgDirState is_pg_running(const char *path);
extern bool create_pg_dir(char *path, bool force); extern bool create_pg_dir(const char *path, bool force);
extern int rmdir_recursive(char *path); extern int rmdir_recursive(const char *path);
#endif #endif