Skip to content

Commit 9a90ec9

Browse files
committed
Improve pg_check_dir code and comments.
Avoid losing errno if readdir() fails and closedir() works. Consistently return 4 rather than 3 if both a lost+found directory and other files are found, rather than returning one value or the other depending on the order of the directory listing. Update comments to match the actual behavior. These oversights date to commits 6f03927 and 17f1523. Marco Nenciarini
1 parent 7bc6e59 commit 9a90ec9

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/port/pgcheckdir.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* Returns:
2323
* 0 if nonexistent
2424
* 1 if exists and empty
25-
* 2 if exists and not empty
25+
* 2 if exists and contains _only_ dot files
26+
* 3 if exists and contains a mount point
27+
* 4 if exists and not empty
2628
* -1 if trouble accessing directory (errno reflects the error)
2729
*/
2830
int
@@ -32,6 +34,8 @@ pg_check_dir(const char *dir)
3234
DIR *chkdir;
3335
struct dirent *file;
3436
bool dot_found = false;
37+
bool mount_found = false;
38+
int readdir_errno;
3539

3640
chkdir = opendir(dir);
3741
if (chkdir == NULL)
@@ -51,10 +55,10 @@ pg_check_dir(const char *dir)
5155
{
5256
dot_found = true;
5357
}
58+
/* lost+found directory */
5459
else if (strcmp("lost+found", file->d_name) == 0)
5560
{
56-
result = 3; /* not empty, mount point */
57-
break;
61+
mount_found = true;
5862
}
5963
#endif
6064
else
@@ -70,9 +74,20 @@ pg_check_dir(const char *dir)
7074
errno = 0;
7175
#endif
7276

73-
if (errno || closedir(chkdir))
77+
if (errno)
7478
result = -1; /* some kind of I/O error? */
7579

80+
/* Close chkdir and avoid overwriting the readdir errno on success */
81+
readdir_errno = errno;
82+
if (closedir(chkdir))
83+
result = -1; /* error executing closedir */
84+
else
85+
errno = readdir_errno;
86+
87+
/* We report on mount point if we find a lost+found directory */
88+
if (result == 1 && mount_found)
89+
result = 3;
90+
7691
/* We report on dot-files if we _only_ find dot files */
7792
if (result == 1 && dot_found)
7893
result = 2;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy