diff options
author | Bruce Momjian <bruce@momjian.us> | 2014-03-21 13:45:11 -0400 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 2014-03-21 13:45:11 -0400 |
commit | 6f03927fce038096f53ca67eeab9adb24938f8a6 (patch) | |
tree | e6ebc4031e1ec37c0766e1ae6baa83f39b0da227 /src/common/pgfnames.c | |
parent | 68a2e52bbaf98f136a96b3a0d734ca52ca440a95 (diff) | |
download | postgresql-6f03927fce038096f53ca67eeab9adb24938f8a6.tar.gz postgresql-6f03927fce038096f53ca67eeab9adb24938f8a6.zip |
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.
Diffstat (limited to 'src/common/pgfnames.c')
-rw-r--r-- | src/common/pgfnames.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/common/pgfnames.c b/src/common/pgfnames.c index 9a68163dd76..51a848a1fdb 100644 --- a/src/common/pgfnames.c +++ b/src/common/pgfnames.c @@ -50,8 +50,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) { @@ -63,14 +62,10 @@ 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 - */ + /* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */ if (GetLastError() == ERROR_NO_MORE_FILES) errno = 0; #endif @@ -87,7 +82,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; } |