Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5d6c240

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 c923e82 commit 5d6c240

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/port/pgcheckdir.c

+19-4
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
@@ -64,9 +68,20 @@ pg_check_dir(const char *dir)
6468
}
6569
}
6670

67-
if (errno || closedir(chkdir))
71+
if (errno)
6872
result = -1; /* some kind of I/O error? */
6973

74+
/* Close chkdir and avoid overwriting the readdir errno on success */
75+
readdir_errno = errno;
76+
if (closedir(chkdir))
77+
result = -1; /* error executing closedir */
78+
else
79+
errno = readdir_errno;
80+
81+
/* We report on mount point if we find a lost+found directory */
82+
if (result == 1 && mount_found)
83+
result = 3;
84+
7085
/* We report on dot-files if we _only_ find dot files */
7186
if (result == 1 && dot_found)
7287
result = 2;

0 commit comments

Comments
 (0)