vacuumlazy.c: Tweak local variable name.
[pgsql.git] / src / port / pgcheckdir.c
blob94aa40ca8f17100ffafb5d7965c66d17873be56a
1 /*-------------------------------------------------------------------------
3 * 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-2023, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * IDENTIFICATION
12 * src/port/pgcheckdir.c
13 *-------------------------------------------------------------------------
16 #include "c.h"
18 #include <dirent.h>
22 * Test to see if a directory exists and is empty or not.
24 * Returns:
25 * 0 if nonexistent
26 * 1 if exists and empty
27 * 2 if exists and contains _only_ dot files
28 * 3 if exists and contains a mount point
29 * 4 if exists and not empty
30 * -1 if trouble accessing directory (errno reflects the error)
32 int
33 pg_check_dir(const char *dir)
35 int result = 1;
36 DIR *chkdir;
37 struct dirent *file;
38 bool dot_found = false;
39 bool mount_found = false;
40 int readdir_errno;
42 chkdir = opendir(dir);
43 if (chkdir == NULL)
44 return (errno == ENOENT) ? 0 : -1;
46 while (errno = 0, (file = readdir(chkdir)) != NULL)
48 if (strcmp(".", file->d_name) == 0 ||
49 strcmp("..", file->d_name) == 0)
51 /* skip this and parent directory */
52 continue;
54 #ifndef WIN32
55 /* file starts with "." */
56 else if (file->d_name[0] == '.')
58 dot_found = true;
60 /* lost+found directory */
61 else if (strcmp("lost+found", file->d_name) == 0)
63 mount_found = true;
65 #endif
66 else
68 result = 4; /* not empty */
69 break;
73 if (errno)
74 result = -1; /* some kind of I/O error? */
76 /* Close chkdir and avoid overwriting the readdir errno on success */
77 readdir_errno = errno;
78 if (closedir(chkdir))
79 result = -1; /* error executing closedir */
80 else
81 errno = readdir_errno;
83 /* We report on mount point if we find a lost+found directory */
84 if (result == 1 && mount_found)
85 result = 3;
87 /* We report on dot-files if we _only_ find dot files */
88 if (result == 1 && dot_found)
89 result = 2;
91 return result;