1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Move a couple of initdb's subroutines into src/port/.

mkdir_p and check_data_dir will be useful in CREATE TABLESPACE, since we
have agreed that that command should handle subdirectory creation just like
initdb creates the PGDATA directory.  Push them into src/port/ so that they
are available to both initdb and the backend.  Rename to pg_mkdir_p and
pg_check_dir, just to be on the safe side.  Add FreeBSD's copyright notice
to pgmkdirp.c, since that's where the code came from originally (this
really should have been in initdb.c).  Very marginal code/comment cleanup.
This commit is contained in:
Tom Lane
2010-12-10 19:42:44 -05:00
parent 04f4e10cfc
commit 671199929d
5 changed files with 240 additions and 169 deletions

73
src/port/pgcheckdir.c Normal file
View File

@ -0,0 +1,73 @@
/*-------------------------------------------------------------------------
*
* src/port/pgcheckdir.c
*
* A simple subroutine to check whether a directory exists and is empty or not.
* Useful in both initdb and the backend.
*
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*-------------------------------------------------------------------------
*/
#include "c.h"
#include <dirent.h>
/*
* Test to see if a directory exists and is empty or not.
*
* Returns:
* 0 if nonexistent
* 1 if exists and empty
* 2 if exists and not empty
* -1 if trouble accessing directory (errno reflects the error)
*/
int
pg_check_dir(const char *dir)
{
int result = 1;
DIR *chkdir;
struct dirent *file;
errno = 0;
chkdir = opendir(dir);
if (chkdir == NULL)
return (errno == ENOENT) ? 0 : -1;
while ((file = readdir(chkdir)) != NULL)
{
if (strcmp(".", file->d_name) == 0 ||
strcmp("..", file->d_name) == 0)
{
/* skip this and parent directory */
continue;
}
else
{
result = 2; /* not empty */
break;
}
}
#ifdef WIN32
/*
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
closedir(chkdir);
if (errno != 0)
result = -1; /* some kind of I/O error? */
return result;
}