Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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_resetxlog/pg_resetxlog.c
src/port/dirent.c
src/port/dirmod.c
src/port/pgcheckdir.c

index 400968ce399c82c318b3ff78c5c47991c8c0c3d1..84d80f215c560f0a0ae54bc5d5cbd5425d3b6e50 100644 (file)
@@ -98,7 +98,7 @@ CleanupPriorWALFiles(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 in
@@ -132,7 +132,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 e20fbbb625a99894360804c6aecc06c41a773b19..a99b94ef1b545c244e09f59a550aafe504c8dc74 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 8fdc86d7d471d7fc822ba0e402bc0dcc7c70988b..de23dedbf2b17c6cd25bf89c7fe405cf8f34f992 100644 (file)
@@ -291,7 +291,7 @@ pg_scandir_internal(const char *dirname,
 
    *namelist = NULL;
 
-   while ((direntry = readdir(dirdesc)) != NULL)
+   while (errno = 0, (direntry = readdir(dirdesc)) != NULL)
    {
        /* Invoke the selector function to see if the direntry matches */
        if (!selector || (*selector) (direntry))
@@ -324,7 +324,17 @@ pg_scandir_internal(const char *dirname,
        }
    }
 
-   closedir(dirdesc);
+#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)
+       pg_log(PG_FATAL, "could not read directory \"%s\": %s\n", dirname, getErrorText(errno));
+
+   if (closedir(dirdesc))
+       pg_log(PG_FATAL, "could not close directory \"%s\": %s\n", dirname, getErrorText(errno));
 
    return count;
 }
index ca12b9af77cbbf7bfb14196f7cbb0759a74d83ad..a3211b1f041837bd795465143beca193d5eddd8e 100644 (file)
@@ -1687,11 +1687,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 54cc5b03024bcf3cc2c36d1824ef1e51747c7662..5ea51a02ef5f7c221c3451f88e8170e51db8a8f0 100644 (file)
@@ -742,8 +742,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)
@@ -767,25 +766,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
@@ -817,8 +818,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)
@@ -831,25 +831,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);
 }
 
 
@@ -873,8 +875,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 ||
@@ -888,25 +889,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 6219ccb46947901b4c3da961d1cae5bbe6ff7706..9631eeee46bb0dede655c47d81c0b14f42f58e25 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 17fd96573dbdddafdc17a7d36491cb901d2be916..f61d68b301a5f6d15435c78c46125c484987343a 100644 (file)
@@ -448,8 +448,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)
        {
@@ -461,17 +460,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
@@ -484,7 +480,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 9453bcb68a680d59546b42cab6cee5d29fbc988b..3a2a34cbc61da65361f22613cb12bb9baa1e177a 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;