]> granicus.if.org Git - postgresql/commitdiff
Properly check for readdir/closedir() failures
authorBruce Momjian <bruce@momjian.us>
Fri, 21 Mar 2014 17:45:11 +0000 (13:45 -0400)
committerBruce Momjian <bruce@momjian.us>
Fri, 21 Mar 2014 17:45:11 +0000 (13:45 -0400)
Clear errno before calling readdir() and handle old MinGW errno bug
while adding full test coverage for readdir/closedir failures.

Backpatch through 8.4.

contrib/pg_archivecleanup/pg_archivecleanup.c
contrib/pg_standby/pg_standby.c
contrib/pg_upgrade/file.c
src/backend/storage/file/fd.c
src/bin/pg_basebackup/pg_receivexlog.c
src/bin/pg_resetxlog/pg_resetxlog.c
src/port/dirent.c
src/port/dirmod.c
src/port/pgcheckdir.c

index 8f77998de12f95f41bb95c3e05a14de6cdf18047..29271e000ac884782121e2ca8576d1105c5b731c 100644 (file)
@@ -117,7 +117,7 @@ CleanupPriorWALFiles(void)
 
        if ((xldir = opendir(archiveLocation)) != NULL)
        {
-               while ((xlde = readdir(xldir)) != NULL)
+               while (errno = 0, (xlde = readdir(xldir)) != NULL)
                {
                        strncpy(walfile, xlde->d_name, MAXPGPATH);
                        TrimExtension(walfile, additional_ext);
@@ -175,7 +175,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
                fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
index 0f1e0c1dfc86d2392bc3f16b7b01ae006267d45a..bdb3362b4b671ad35f65d2ab9c4f3fa925aeb758 100644 (file)
@@ -256,7 +256,7 @@ CustomizableCleanupPriorWALFiles(void)
                 */
                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
@@ -294,6 +294,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)
                                fprintf(stderr, "\n");
                }
@@ -301,7 +311,10 @@ CustomizableCleanupPriorWALFiles(void)
                        fprintf(stderr, "%s: could not open archive location \"%s\": %s\n",
                                        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);
        }
 }
index a5d92c62fce80bbd582b727b6f5c66c72ec0d84e..b76fcdbf79e70262ed6918c2f36dd94356cbfe8a 100644 (file)
@@ -258,10 +258,7 @@ load_directory(const char *dirname, char ***namelist)
        }
 
 #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
@@ -270,7 +267,9 @@ load_directory(const char *dirname, char ***namelist)
                pg_log(PG_FATAL, "could not read directory \"%s\": %s\n",
                           dirname, getErrorText(errno));
 
-       closedir(dirdesc);
+       if (closedir(dirdesc))
+               pg_log(PG_FATAL, "could not close directory \"%s\": %s\n",
+                          dirname, getErrorText(errno));
 
        return count;
 }
index 8410b7a528fe476ceb6ad22e5e125b9be82f9d10..3a595a3295fd64f457631472ac3a6eef13f8ad8a 100644 (file)
@@ -1746,11 +1746,7 @@ ReadDir(DIR *dir, const char *dirname)
                return dent;
 
 #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
index 2d9f6c441b1a5c0898916695774ec848267bdde8..5f07bb28074c6e42565af3eab11475908b3e8556 100644 (file)
@@ -123,7 +123,7 @@ FindStreamingStart(XLogRecPtr currentpos, uint32 currenttimeline)
                disconnect_and_exit(1);
        }
 
-       while ((dirent = readdir(dir)) != NULL)
+       while (errno = 0, (dirent = readdir(dir)) != NULL)
        {
                char            fullpath[MAXPGPATH];
                struct stat statbuf;
@@ -197,7 +197,25 @@ FindStreamingStart(XLogRecPtr currentpos, uint32 currenttimeline)
                }
        }
 
-       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_log > 0 || high_seg > 0)
        {
index 80e826897c7a8b5ab8ee7ab5d4175f38fe8ce8d3..f10524f4b7c1c4ab88a460efaa6f209ace6f3487 100644 (file)
@@ -748,8 +748,7 @@ FindEndOfXLOG(void)
                exit(1);
        }
 
-       errno = 0;
-       while ((xlde = readdir(xldir)) != NULL)
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
        {
                if (strlen(xlde->d_name) == 24 &&
                        strspn(xlde->d_name, "0123456789ABCDEF") == 24)
@@ -773,25 +772,27 @@ FindEndOfXLOG(void)
                                newXlogSeg = seg;
                        }
                }
-               errno = 0;
        }
-#ifdef WIN32
 
-       /*
-        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
-        * released version
-        */
+#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 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));
                exit(1);
        }
-       closedir(xldir);
 
        /*
         * Finally, convert to new xlog seg size, and advance by one to ensure we
@@ -823,8 +824,7 @@ KillExistingXLOG(void)
                exit(1);
        }
 
-       errno = 0;
-       while ((xlde = readdir(xldir)) != NULL)
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
        {
                if (strlen(xlde->d_name) == 24 &&
                        strspn(xlde->d_name, "0123456789ABCDEF") == 24)
@@ -837,25 +837,27 @@ KillExistingXLOG(void)
                                exit(1);
                        }
                }
-               errno = 0;
        }
-#ifdef WIN32
 
-       /*
-        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
-        * released version
-        */
+#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 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));
                exit(1);
        }
-       closedir(xldir);
 }
 
 
@@ -879,8 +881,7 @@ KillExistingArchiveStatus(void)
                exit(1);
        }
 
-       errno = 0;
-       while ((xlde = readdir(xldir)) != NULL)
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
        {
                if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
                        (strcmp(xlde->d_name + 24, ".ready") == 0 ||
@@ -894,25 +895,27 @@ KillExistingArchiveStatus(void)
                                exit(1);
                        }
                }
-               errno = 0;
        }
-#ifdef WIN32
 
-       /*
-        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
-        * released version
-        */
+#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 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));
                exit(1);
        }
-       closedir(xldir);
 }
 
 
index 162e40a21acc1d082b85236989d0ca350bd6020c..6fdb24f2974a06610327d4b10b528e98aa62af9f 100644 (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;
 }
index 514424f82e635dd721b184e009dccce1a4d52bf7..80932ed966d16f534066723001de64b161e75e00 100644 (file)
@@ -456,8 +456,7 @@ pgfnames(const char *path)
 
        filenames = (char **) palloc(fnsize * sizeof(char *));
 
-       errno = 0;
-       while ((file = readdir(dir)) != NULL)
+       while (errno = 0, (file = readdir(dir)) != NULL)
        {
                if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
                {
@@ -469,17 +468,14 @@ pgfnames(const char *path)
                        }
                        filenames[numnames++] = pstrdup(file->d_name);
                }
-               errno = 0;
        }
-#ifdef WIN32
 
-       /*
-        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
-        * released version
-        */
+#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)
        {
 #ifndef FRONTEND
@@ -492,7 +488,15 @@ pgfnames(const char *path)
 
        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;
 }
index f4bb0f218a7fed20fc31d5542413e4fa639a4d80..36545618e454b434a338a8d17c6a655494d9acab 100644 (file)
@@ -32,14 +32,12 @@ pg_check_dir(const char *dir)
        DIR                *chkdir;
        struct dirent *file;
 
-       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)
@@ -55,18 +53,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? */
 
        return result;