1 /*-------------------------------------------------------------------------
3 * src/port/pgcheckdir.c
5 * A simple subroutine to check whether a directory exists and is empty or not.
6 * Useful in both initdb and the backend.
8 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
20 * Test to see if a directory exists and is empty or not.
24 * 1 if exists and empty
25 * 2 if exists and not empty
26 * -1 if trouble accessing directory (errno reflects the error)
29 pg_check_dir(const char *dir)
34 bool dot_found = false;
36 chkdir = opendir(dir);
38 return (errno == ENOENT) ? 0 : -1;
40 while (errno = 0, (file = readdir(chkdir)) != NULL)
42 if (strcmp(".", file->d_name) == 0 ||
43 strcmp("..", file->d_name) == 0)
45 /* skip this and parent directory */
49 /* file starts with "." */
50 else if (file->d_name[0] == '.')
54 else if (strcmp("lost+found", file->d_name) == 0)
56 result = 3; /* not empty, mount point */
62 result = 4; /* not empty */
68 /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
69 if (GetLastError() == ERROR_NO_MORE_FILES)
73 if (errno || closedir(chkdir))
74 result = -1; /* some kind of I/O error? */
76 /* We report on dot-files if we _only_ find dot files */
77 if (result == 1 && dot_found)