2010-12-11 01:42:44 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2010-12-11 01:42:44 +01:00
|
|
|
* 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
|
2015-02-17 16:19:30 +01:00
|
|
|
* 2 if exists and contains _only_ dot files
|
|
|
|
* 3 if exists and contains a mount point
|
|
|
|
* 4 if exists and not empty
|
2010-12-11 01:42:44 +01:00
|
|
|
* -1 if trouble accessing directory (errno reflects the error)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_check_dir(const char *dir)
|
|
|
|
{
|
|
|
|
int result = 1;
|
|
|
|
DIR *chkdir;
|
|
|
|
struct dirent *file;
|
2013-02-17 00:52:50 +01:00
|
|
|
bool dot_found = false;
|
2015-02-17 16:19:30 +01:00
|
|
|
bool mount_found = false;
|
|
|
|
int readdir_errno;
|
2010-12-11 01:42:44 +01:00
|
|
|
|
|
|
|
chkdir = opendir(dir);
|
|
|
|
if (chkdir == NULL)
|
|
|
|
return (errno == ENOENT) ? 0 : -1;
|
|
|
|
|
2014-03-21 18:45:11 +01:00
|
|
|
while (errno = 0, (file = readdir(chkdir)) != NULL)
|
2010-12-11 01:42:44 +01:00
|
|
|
{
|
|
|
|
if (strcmp(".", file->d_name) == 0 ||
|
|
|
|
strcmp("..", file->d_name) == 0)
|
|
|
|
{
|
|
|
|
/* skip this and parent directory */
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-17 00:52:50 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
/* file starts with "." */
|
|
|
|
else if (file->d_name[0] == '.')
|
|
|
|
{
|
|
|
|
dot_found = true;
|
|
|
|
}
|
2015-02-17 16:19:30 +01:00
|
|
|
/* lost+found directory */
|
2013-02-17 00:52:50 +01:00
|
|
|
else if (strcmp("lost+found", file->d_name) == 0)
|
|
|
|
{
|
2015-02-17 16:19:30 +01:00
|
|
|
mount_found = true;
|
2013-02-17 00:52:50 +01:00
|
|
|
}
|
|
|
|
#endif
|
2010-12-11 01:42:44 +01:00
|
|
|
else
|
|
|
|
{
|
2013-02-17 00:52:50 +01:00
|
|
|
result = 4; /* not empty */
|
2010-12-11 01:42:44 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:19:30 +01:00
|
|
|
if (errno)
|
2010-12-11 01:42:44 +01:00
|
|
|
result = -1; /* some kind of I/O error? */
|
|
|
|
|
2015-02-17 16:19:30 +01:00
|
|
|
/* Close chkdir and avoid overwriting the readdir errno on success */
|
|
|
|
readdir_errno = errno;
|
|
|
|
if (closedir(chkdir))
|
|
|
|
result = -1; /* error executing closedir */
|
|
|
|
else
|
|
|
|
errno = readdir_errno;
|
|
|
|
|
|
|
|
/* We report on mount point if we find a lost+found directory */
|
|
|
|
if (result == 1 && mount_found)
|
|
|
|
result = 3;
|
|
|
|
|
2013-02-17 00:52:50 +01:00
|
|
|
/* We report on dot-files if we _only_ find dot files */
|
|
|
|
if (result == 1 && dot_found)
|
|
|
|
result = 2;
|
|
|
|
|
2010-12-11 01:42:44 +01:00
|
|
|
return result;
|
|
|
|
}
|