]> granicus.if.org Git - postgresql/blobdiff - src/bin/pg_resetxlog/pg_resetxlog.c
Properly check for readdir/closedir() failures
[postgresql] / src / bin / pg_resetxlog / pg_resetxlog.c
index 54690af0311300510c399061cd18be0ed38afc28..af2cec7e932cd6d392b4968f84fa2831739843d0 100644 (file)
@@ -821,8 +821,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)
@@ -844,25 +843,27 @@ FindEndOfXLOG(void)
                        if (segno > newXlogSegNo)
                                newXlogSegNo = segno;
                }
-               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
@@ -892,8 +893,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)
@@ -906,25 +906,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);
 }
 
 
@@ -948,8 +950,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 ||
@@ -963,25 +964,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);
 }