1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

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.
This commit is contained in:
Bruce Momjian
2014-03-21 13:45:11 -04:00
parent 192856b1b9
commit 4859a710ea
8 changed files with 109 additions and 63 deletions

View 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 archiveLocation \"%s\": %s\n",

View 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,13 +294,26 @@ 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");
}
else
fprintf(stderr, "%s: archiveLocation \"%s\" open error\n", progname, archiveLocation);
closedir(xldir);
if (closedir(xldir))
fprintf(stderr, "%s: could not close archive location \"%s\": %s\n",
progname, archiveLocation, strerror(errno));
fflush(stderr);
}
}

View File

@ -291,7 +291,7 @@ pg_scandir_internal(migratorContext *ctx, 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) (direntry))
@ -318,7 +318,17 @@ pg_scandir_internal(migratorContext *ctx, 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(ctx, PG_FATAL, "Could not read directory \"%s\": %s\n", dirname, getErrorText(errno));
if (closedir(dirdesc))
pg_log(ctx, PG_FATAL, "Could not close directory \"%s\": %s\n", dirname, getErrorText(errno));
return count;
}
@ -415,7 +425,7 @@ copy_dir(const char *src, const char *dst, bool force)
return -1;
}
while ((de = readdir(srcdir)) != NULL)
while (errno = 0, (de = readdir(srcdir)) != NULL)
{
char src_file[MAXPGPATH];
char dest_file[MAXPGPATH];
@ -460,9 +470,19 @@ copy_dir(const char *src, const char *dst, bool force)
}
}
#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)
return -1;
if (srcdir != NULL)
{
closedir(srcdir);
if (closedir(srcdir))
return -1;
srcdir = NULL;
}
return 1;