Skip to content

Commit

Permalink
Changes when trying to compile:
Browse files Browse the repository at this point in the history
- Fix Makefile to include check_dir.c
- Add function mkdir_p that was taken from initdb.c
- Replace some strcpy for assignment to const char * to keep compiler quite
- Add STANDBY_NORMAL to initialize action
- fix typos and add headers needed to compile
  • Loading branch information
Jaime Casanova authored and Jaime Casanova committed Sep 29, 2010
1 parent c809558 commit 3b8e818
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) 2ndQuadrant, 2010

repmgrd_OBJS = dbutils.o config.o repmgrd.o
repmgr_OBJS = dbutils.o config.o repmgr.o
repmgr_OBJS = dbutils.o check_dir.o config.o repmgr.o

PG_CPPFLAGS = -I$(libpq_srcdir)
PG_LIBS = $(libpq_pgport)
Expand Down
127 changes: 119 additions & 8 deletions check_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "postgres_fe.h"
#include "check_dir.h"


static int mkdir_p(char *path, mode_t omode);

/*
* make sure the directory either doesn't exist or is empty
* we use this function to check the new data directory and
Expand All @@ -22,15 +29,12 @@
* or -1 if trouble accessing directory
*/
int
check_dir(const char *dir)
check_dir(char *dir)
{
DIR *chkdir;
struct dirent *file;
int result = 1;

char *dummy_file;
FILE *dummy_fd;

errno = 0;

chkdir = opendir(dir);
Expand Down Expand Up @@ -65,7 +69,7 @@ check_dir(const char *dir)
closedir(chkdir);

if (errno != 0)
return -1 /* some kind of I/O error? */
return -1; /* some kind of I/O error? */

return result;
}
Expand All @@ -75,7 +79,7 @@ check_dir(const char *dir)
* Create directory
*/
bool
create_directory(const char *dir)
create_directory(char *dir)
{
if (mkdir_p(dir, 0700) == 0)
return true;
Expand All @@ -87,7 +91,114 @@ create_directory(const char *dir)
}

bool
set_directory_permissions(const char *dir)
set_directory_permissions(char *dir)
{
return (chmod(dir, 0700) != 0) ? false : true;
}



/* function from initdb.c */
/* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */

/*
* this tries to build all the elements of a path to a directory a la mkdir -p
* we assume the path is in canonical form, i.e. uses / as the separator
* we also assume it isn't null.
*
* note that on failure, the path arg has been modified to show the particular
* directory level we had problems with.
*/
static int
mkdir_p(char *path, mode_t omode)
{
return (chmod(data_dir, 0700) != 0) ? false : true;
struct stat sb;
mode_t numask,
oumask;
int first,
last,
retval;
char *p;

p = path;
oumask = 0;
retval = 0;

#ifdef WIN32
/* skip network and drive specifiers for win32 */
if (strlen(p) >= 2)
{
if (p[0] == '/' && p[1] == '/')
{
/* network drive */
p = strstr(p + 2, "/");
if (p == NULL)
return 1;
}
else if (p[1] == ':' &&
((p[0] >= 'a' && p[0] <= 'z') ||
(p[0] >= 'A' && p[0] <= 'Z')))
{
/* local drive */
p += 2;
}
}
#endif

if (p[0] == '/') /* Skip leading '/'. */
++p;
for (first = 1, last = 0; !last; ++p)
{
if (p[0] == '\0')
last = 1;
else if (p[0] != '/')
continue;
*p = '\0';
if (!last && p[1] == '\0')
last = 1;
if (first)
{
/*
* POSIX 1003.2: For each dir operand that does not name an
* existing directory, effects equivalent to those caused by the
* following command shall occcur:
*
* mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
* dir
*
* We change the user's umask and then restore it, instead of
* doing chmod's.
*/
oumask = umask(0);
numask = oumask & ~(S_IWUSR | S_IXUSR);
(void) umask(numask);
first = 0;
}
if (last)
(void) umask(oumask);

/* check for pre-existing directory; ok if it's a parent */
if (stat(path, &sb) == 0)
{
if (!S_ISDIR(sb.st_mode))
{
if (last)
errno = EEXIST;
else
errno = ENOTDIR;
retval = 1;
break;
}
}
else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
{
retval = 1;
break;
}
if (!last)
*p = '/';
}
if (!first && !last)
(void) umask(oumask);
return retval;
}
6 changes: 3 additions & 3 deletions check_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
*
*/

int check_dir(const char *dir);
bool create_directory(const char *dir);
bool set_directory_permissions(const char *dir);
int check_dir(char *dir);
bool create_directory(char *dir);
bool set_directory_permissions(char *dir);
2 changes: 1 addition & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "repmgr.h"

void
parse_config(const *char config_file, char *cluster_name, int *node, char *conninfo)
parse_config(const char *config_file, char *cluster_name, int *node, char *conninfo)
{
char *s, buff[256];
FILE *fp = fopen (config_file, "r");
Expand Down
6 changes: 3 additions & 3 deletions dbutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ guc_setted(PGconn *conn, const char *parameter, const char *op, const char *valu
}


char *
const char *
get_cluster_size(PGconn *conn)
{
PGresult *res;
char *size;
const char *size;
char sqlquery[8192];

sprintf(sqlquery, "SELECT pg_size_pretty(SUM(pg_database_size(oid))::bigint) "
Expand All @@ -129,7 +129,7 @@ get_cluster_size(PGconn *conn)
PQfinish(conn);
exit(1);
}
strcpy(size, PQgetvalue(res, 0, 0))
size = PQgetvalue(res, 0, 0);
PQclear(res);
return size;
}
Loading

0 comments on commit 3b8e818

Please sign in to comment.