Add missing error checking in readdir() loops.
authorTom Lane <[email protected]>
Thu, 24 Mar 2005 02:11:33 +0000 (02:11 +0000)
committerTom Lane <[email protected]>
Thu, 24 Mar 2005 02:11:33 +0000 (02:11 +0000)
src/port/copydir.c
src/port/dirmod.c

index 0f833e5af18bc5ff737b10554a35000d4abd667c..6040657b963968839675d336f1b933f291e72b4a 100644 (file)
@@ -56,6 +56,7 @@ copydir(char *fromdir, char *todir)
                return -1;
        }
 
+       errno = 0;
        while ((xlde = readdir(xldir)) != NULL)
        {
                snprintf(fromfl, MAXPGPATH, "%s/%s", fromdir, xlde->d_name);
@@ -68,6 +69,24 @@ copydir(char *fromdir, char *todir)
                        FreeDir(xldir);
                        return -1;
                }
+               errno = 0;
+       }
+#ifdef WIN32
+
+       /*
+        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
+        * not in released version
+        */
+       if (GetLastError() == ERROR_NO_MORE_FILES)
+               errno = 0;
+#endif
+       if (errno)
+       {
+               ereport(WARNING,
+                               (errcode_for_file_access(),
+                                errmsg("could not read directory \"%s\": %m", fromdir)));
+               FreeDir(xldir);
+               return -1;
        }
 
        FreeDir(xldir);
index ec0d6716ed71a7705b191a452c6c14d7ee1b380d..5ba5d4a6a79e12646b9cbb2b5d2734c6844662cf 100644 (file)
@@ -326,10 +326,19 @@ fnames(char *path)
 
        dir = opendir(path);
        if (dir == NULL)
+       {
+#ifndef FRONTEND
+               elog(WARNING, "could not open directory \"%s\": %m", path);
+#else
+               fprintf(stderr, _("could not open directory \"%s\": %s\n"),
+                               path, strerror(errno));
+#endif
                return NULL;
+       }
 
        filenames = (char **) palloc(fnsize * sizeof(char *));
 
+       errno = 0;
        while ((file = readdir(dir)) != NULL)
        {
                if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
@@ -342,6 +351,25 @@ fnames(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
+        */
+       if (GetLastError() == ERROR_NO_MORE_FILES)
+               errno = 0;
+#endif
+       if (errno)
+       {
+#ifndef FRONTEND
+               elog(WARNING, "could not read directory \"%s\": %m", path);
+#else
+               fprintf(stderr, _("could not read directory \"%s\": %s\n"),
+                               path, strerror(errno));
+#endif
        }
 
        filenames[numnames] = NULL;
@@ -434,7 +462,8 @@ report_and_fail:
 #ifndef FRONTEND
        elog(WARNING, "could not remove file or directory \"%s\": %m", filepath);
 #else
-       fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"), filepath, strerror(errno));
+       fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
+                       filepath, strerror(errno));
 #endif
        fnames_cleanup(filenames);
        return false;