Commit 6f03927f authored by Bruce Momjian's avatar Bruce Momjian

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.
parent 68a2e52b
...@@ -106,7 +106,7 @@ CleanupPriorWALFiles(void) ...@@ -106,7 +106,7 @@ CleanupPriorWALFiles(void)
if ((xldir = opendir(archiveLocation)) != NULL) if ((xldir = opendir(archiveLocation)) != NULL)
{ {
while ((xlde = readdir(xldir)) != NULL) while (errno = 0, (xlde = readdir(xldir)) != NULL)
{ {
strncpy(walfile, xlde->d_name, MAXPGPATH); strncpy(walfile, xlde->d_name, MAXPGPATH);
TrimExtension(walfile, additional_ext); TrimExtension(walfile, additional_ext);
...@@ -164,7 +164,19 @@ CleanupPriorWALFiles(void) ...@@ -164,7 +164,19 @@ CleanupPriorWALFiles(void)
} }
} }
} }
closedir(xldir);
#ifdef WIN32
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
if (errno)
fprintf(stderr, "%s: could not read archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno));
if (closedir(xldir))
fprintf(stderr, "%s: could not close archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno));
} }
else else
fprintf(stderr, "%s: could not open archive location \"%s\": %s\n", fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
......
...@@ -245,7 +245,7 @@ CustomizableCleanupPriorWALFiles(void) ...@@ -245,7 +245,7 @@ CustomizableCleanupPriorWALFiles(void)
*/ */
if ((xldir = opendir(archiveLocation)) != NULL) if ((xldir = opendir(archiveLocation)) != NULL)
{ {
while ((xlde = readdir(xldir)) != NULL) while (errno = 0, (xlde = readdir(xldir)) != NULL)
{ {
/* /*
* We ignore the timeline part of the XLOG segment identifiers * We ignore the timeline part of the XLOG segment identifiers
...@@ -283,6 +283,16 @@ CustomizableCleanupPriorWALFiles(void) ...@@ -283,6 +283,16 @@ CustomizableCleanupPriorWALFiles(void)
} }
} }
} }
#ifdef WIN32
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
if (errno)
fprintf(stderr, "%s: could not read archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno));
if (debug) if (debug)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
...@@ -290,7 +300,10 @@ CustomizableCleanupPriorWALFiles(void) ...@@ -290,7 +300,10 @@ CustomizableCleanupPriorWALFiles(void)
fprintf(stderr, "%s: could not open archive location \"%s\": %s\n", fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno)); progname, archiveLocation, strerror(errno));
closedir(xldir); if (closedir(xldir))
fprintf(stderr, "%s: could not close archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno));
fflush(stderr); fflush(stderr);
} }
} }
......
...@@ -1957,10 +1957,7 @@ ReadDir(DIR *dir, const char *dirname) ...@@ -1957,10 +1957,7 @@ ReadDir(DIR *dir, const char *dirname)
return dent; return dent;
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
......
...@@ -565,10 +565,7 @@ walkdir(char *path, void (*action) (char *fname, bool isdir)) ...@@ -565,10 +565,7 @@ walkdir(char *path, void (*action) (char *fname, bool isdir))
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
...@@ -580,7 +577,12 @@ walkdir(char *path, void (*action) (char *fname, bool isdir)) ...@@ -580,7 +577,12 @@ walkdir(char *path, void (*action) (char *fname, bool isdir))
exit_nicely(); exit_nicely();
} }
closedir(dir); if (closedir(dir))
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, path, strerror(errno));
exit_nicely();
}
/* /*
* It's important to fsync the destination directory itself as individual * It's important to fsync the destination directory itself as individual
......
...@@ -139,7 +139,7 @@ FindStreamingStart(uint32 *tli) ...@@ -139,7 +139,7 @@ FindStreamingStart(uint32 *tli)
disconnect_and_exit(1); disconnect_and_exit(1);
} }
while ((dirent = readdir(dir)) != NULL) while (errno = 0, (dirent = readdir(dir)) != NULL)
{ {
uint32 tli; uint32 tli;
XLogSegNo segno; XLogSegNo segno;
...@@ -209,7 +209,25 @@ FindStreamingStart(uint32 *tli) ...@@ -209,7 +209,25 @@ FindStreamingStart(uint32 *tli)
} }
} }
closedir(dir); #ifdef WIN32
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
if (errno)
{
fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
progname, basedir, strerror(errno));
disconnect_and_exit(1);
}
if (closedir(dir))
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, basedir, strerror(errno));
disconnect_and_exit(1);
}
if (high_segno > 0) if (high_segno > 0)
{ {
......
...@@ -177,7 +177,7 @@ InitArchiveFmt_Directory(ArchiveHandle *AH) ...@@ -177,7 +177,7 @@ InitArchiveFmt_Directory(ArchiveHandle *AH)
struct dirent *d; struct dirent *d;
is_empty = true; is_empty = true;
while ((d = readdir(dir))) while (errno = 0, (d = readdir(dir)))
{ {
if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0)
{ {
...@@ -185,7 +185,20 @@ InitArchiveFmt_Directory(ArchiveHandle *AH) ...@@ -185,7 +185,20 @@ InitArchiveFmt_Directory(ArchiveHandle *AH)
break; break;
} }
} }
closedir(dir);
#ifdef WIN32
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
if (errno)
exit_horribly(modulename, "could not read directory \"%s\": %s\n",
ctx->directory, strerror(errno));
if (closedir(dir))
exit_horribly(modulename, "could not close directory \"%s\": %s\n",
ctx->directory, strerror(errno));
} }
} }
......
...@@ -821,8 +821,7 @@ FindEndOfXLOG(void) ...@@ -821,8 +821,7 @@ FindEndOfXLOG(void)
exit(1); exit(1);
} }
errno = 0; while (errno = 0, (xlde = readdir(xldir)) != NULL)
while ((xlde = readdir(xldir)) != NULL)
{ {
if (strlen(xlde->d_name) == 24 && if (strlen(xlde->d_name) == 24 &&
strspn(xlde->d_name, "0123456789ABCDEF") == 24) strspn(xlde->d_name, "0123456789ABCDEF") == 24)
...@@ -844,25 +843,27 @@ FindEndOfXLOG(void) ...@@ -844,25 +843,27 @@ FindEndOfXLOG(void)
if (segno > newXlogSegNo) if (segno > newXlogSegNo)
newXlogSegNo = segno; newXlogSegNo = segno;
} }
errno = 0;
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
if (errno) if (errno)
{ {
fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
progname, XLOGDIR, strerror(errno));
exit(1);
}
if (closedir(xldir))
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, XLOGDIR, strerror(errno)); progname, XLOGDIR, strerror(errno));
exit(1); exit(1);
} }
closedir(xldir);
/* /*
* Finally, convert to new xlog seg size, and advance by one to ensure we * Finally, convert to new xlog seg size, and advance by one to ensure we
...@@ -892,8 +893,7 @@ KillExistingXLOG(void) ...@@ -892,8 +893,7 @@ KillExistingXLOG(void)
exit(1); exit(1);
} }
errno = 0; while (errno = 0, (xlde = readdir(xldir)) != NULL)
while ((xlde = readdir(xldir)) != NULL)
{ {
if (strlen(xlde->d_name) == 24 && if (strlen(xlde->d_name) == 24 &&
strspn(xlde->d_name, "0123456789ABCDEF") == 24) strspn(xlde->d_name, "0123456789ABCDEF") == 24)
...@@ -906,25 +906,27 @@ KillExistingXLOG(void) ...@@ -906,25 +906,27 @@ KillExistingXLOG(void)
exit(1); exit(1);
} }
} }
errno = 0;
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
if (errno) if (errno)
{ {
fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
progname, XLOGDIR, strerror(errno));
exit(1);
}
if (closedir(xldir))
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, XLOGDIR, strerror(errno)); progname, XLOGDIR, strerror(errno));
exit(1); exit(1);
} }
closedir(xldir);
} }
...@@ -948,8 +950,7 @@ KillExistingArchiveStatus(void) ...@@ -948,8 +950,7 @@ KillExistingArchiveStatus(void)
exit(1); exit(1);
} }
errno = 0; while (errno = 0, (xlde = readdir(xldir)) != NULL)
while ((xlde = readdir(xldir)) != NULL)
{ {
if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 && if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
(strcmp(xlde->d_name + 24, ".ready") == 0 || (strcmp(xlde->d_name + 24, ".ready") == 0 ||
...@@ -963,25 +964,27 @@ KillExistingArchiveStatus(void) ...@@ -963,25 +964,27 @@ KillExistingArchiveStatus(void)
exit(1); exit(1);
} }
} }
errno = 0;
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
if (errno) if (errno)
{ {
fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"), fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
progname, ARCHSTATDIR, strerror(errno));
exit(1);
}
if (closedir(xldir))
{
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, ARCHSTATDIR, strerror(errno)); progname, ARCHSTATDIR, strerror(errno));
exit(1); exit(1);
} }
closedir(xldir);
} }
......
...@@ -50,8 +50,7 @@ pgfnames(const char *path) ...@@ -50,8 +50,7 @@ pgfnames(const char *path)
filenames = (char **) palloc(fnsize * sizeof(char *)); filenames = (char **) palloc(fnsize * sizeof(char *));
errno = 0; while (errno = 0, (file = readdir(dir)) != NULL)
while ((file = readdir(dir)) != NULL)
{ {
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
{ {
...@@ -63,14 +62,10 @@ pgfnames(const char *path) ...@@ -63,14 +62,10 @@ pgfnames(const char *path)
} }
filenames[numnames++] = pstrdup(file->d_name); filenames[numnames++] = pstrdup(file->d_name);
} }
errno = 0;
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
...@@ -87,7 +82,15 @@ pgfnames(const char *path) ...@@ -87,7 +82,15 @@ pgfnames(const char *path)
filenames[numnames] = NULL; filenames[numnames] = NULL;
closedir(dir); if (closedir(dir))
{
#ifndef FRONTEND
elog(WARNING, "could not close directory \"%s\": %m", path);
#else
fprintf(stderr, _("could not close directory \"%s\": %s\n"),
path, strerror(errno));
#endif
}
return filenames; return filenames;
} }
......
...@@ -105,15 +105,19 @@ readdir(DIR *d) ...@@ -105,15 +105,19 @@ readdir(DIR *d)
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
* long */ * long */
d->ret.d_namlen = strlen(d->ret.d_name); d->ret.d_namlen = strlen(d->ret.d_name);
return &d->ret; return &d->ret;
} }
int int
closedir(DIR *d) closedir(DIR *d)
{ {
int ret = 0;
if (d->handle != INVALID_HANDLE_VALUE) if (d->handle != INVALID_HANDLE_VALUE)
FindClose(d->handle); ret = !FindClose(d->handle);
free(d->dirname); free(d->dirname);
free(d); free(d);
return 0;
return ret;
} }
...@@ -33,14 +33,11 @@ pg_check_dir(const char *dir) ...@@ -33,14 +33,11 @@ pg_check_dir(const char *dir)
struct dirent *file; struct dirent *file;
bool dot_found = false; bool dot_found = false;
errno = 0;
chkdir = opendir(dir); chkdir = opendir(dir);
if (chkdir == NULL) if (chkdir == NULL)
return (errno == ENOENT) ? 0 : -1; return (errno == ENOENT) ? 0 : -1;
while ((file = readdir(chkdir)) != NULL) while (errno = 0, (file = readdir(chkdir)) != NULL)
{ {
if (strcmp(".", file->d_name) == 0 || if (strcmp(".", file->d_name) == 0 ||
strcmp("..", file->d_name) == 0) strcmp("..", file->d_name) == 0)
...@@ -68,17 +65,12 @@ pg_check_dir(const char *dir) ...@@ -68,17 +65,12 @@ pg_check_dir(const char *dir)
} }
#ifdef WIN32 #ifdef WIN32
/* /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES) if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0; errno = 0;
#endif #endif
closedir(chkdir); if (errno || closedir(chkdir))
if (errno != 0)
result = -1; /* some kind of I/O error? */ result = -1; /* some kind of I/O error? */
/* We report on dot-files if we _only_ find dot files */ /* We report on dot-files if we _only_ find dot files */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment