1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-28 18:48:04 +03:00

Properly check for readdir/closedir() failures

Clear errno before calling readdir() and handle old MinGW errno bug
while adding full test coverage for readdir/closedir failures.

Backpatch through 8.4.
This commit is contained in:
Bruce Momjian
2014-03-21 13:45:11 -04:00
parent 68a2e52bba
commit 6f03927fce
10 changed files with 122 additions and 65 deletions

View File

@@ -105,15 +105,19 @@ readdir(DIR *d)
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
* long */
d->ret.d_namlen = strlen(d->ret.d_name);
return &d->ret;
}
int
closedir(DIR *d)
{
int ret = 0;
if (d->handle != INVALID_HANDLE_VALUE)
FindClose(d->handle);
ret = !FindClose(d->handle);
free(d->dirname);
free(d);
return 0;
return ret;
}

View File

@@ -33,14 +33,11 @@ pg_check_dir(const char *dir)
struct dirent *file;
bool dot_found = false;
errno = 0;
chkdir = opendir(dir);
if (chkdir == NULL)
return (errno == ENOENT) ? 0 : -1;
while ((file = readdir(chkdir)) != NULL)
while (errno = 0, (file = readdir(chkdir)) != NULL)
{
if (strcmp(".", file->d_name) == 0 ||
strcmp("..", file->d_name) == 0)
@@ -68,17 +65,12 @@ pg_check_dir(const char *dir)
}
#ifdef WIN32
/*
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
closedir(chkdir);
if (errno != 0)
if (errno || closedir(chkdir))
result = -1; /* some kind of I/O error? */
/* We report on dot-files if we _only_ find dot files */