From 6f03927fce038096f53ca67eeab9adb24938f8a6 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 21 Mar 2014 13:45:11 -0400 Subject: 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. --- src/bin/pg_basebackup/pg_receivexlog.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/bin/pg_basebackup/pg_receivexlog.c') diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c index c233779b89c..1eda4b6b002 100644 --- a/src/bin/pg_basebackup/pg_receivexlog.c +++ b/src/bin/pg_basebackup/pg_receivexlog.c @@ -139,7 +139,7 @@ FindStreamingStart(uint32 *tli) disconnect_and_exit(1); } - while ((dirent = readdir(dir)) != NULL) + while (errno = 0, (dirent = readdir(dir)) != NULL) { uint32 tli; XLogSegNo segno; @@ -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) { -- cgit v1.2.3