mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +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:
@ -256,7 +256,7 @@ CustomizableCleanupPriorWALFiles(void)
|
|||||||
*/
|
*/
|
||||||
if ((xldir = opendir(archiveLocation)) != NULL)
|
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
|
* 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)
|
if (debug)
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fprintf(stderr, "%s: archiveLocation \"%s\" open error\n", progname, archiveLocation);
|
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);
|
fflush(stderr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1582,11 +1582,7 @@ ReadDir(DIR *dir, const char *dirname)
|
|||||||
return dent;
|
return dent;
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
/*
|
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
@ -861,7 +861,7 @@ check_data_dir(char *dir)
|
|||||||
if (!chkdir)
|
if (!chkdir)
|
||||||
return (errno == ENOENT) ? 0 : -1;
|
return (errno == ENOENT) ? 0 : -1;
|
||||||
|
|
||||||
while ((file = readdir(chkdir)) != NULL)
|
while (errno = 0, (file = readdir(chkdir)) != NULL)
|
||||||
{
|
{
|
||||||
if (strcmp(".", file->d_name) == 0 ||
|
if (strcmp(".", file->d_name) == 0 ||
|
||||||
strcmp("..", file->d_name) == 0)
|
strcmp("..", file->d_name) == 0)
|
||||||
@ -877,18 +877,12 @@ check_data_dir(char *dir)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
/*
|
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
closedir(chkdir);
|
if (errno || closedir(chkdir))
|
||||||
|
|
||||||
if (errno != 0)
|
|
||||||
result = -1; /* some kind of I/O error? */
|
result = -1; /* some kind of I/O error? */
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -700,8 +700,7 @@ FindEndOfXLOG(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
while (errno = 0, (xlde = readdir(xldir)) != NULL)
|
||||||
while ((xlde = readdir(xldir)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (strlen(xlde->d_name) == 24 &&
|
if (strlen(xlde->d_name) == 24 &&
|
||||||
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
|
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
|
||||||
@ -725,25 +724,27 @@ FindEndOfXLOG(void)
|
|||||||
newXlogSeg = seg;
|
newXlogSeg = seg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
|
||||||
|
|
||||||
/*
|
#ifdef WIN32
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (errno)
|
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));
|
progname, XLOGDIR, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
closedir(xldir);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Finally, convert to new xlog seg size, and advance by one to ensure we
|
* Finally, convert to new xlog seg size, and advance by one to ensure we
|
||||||
@ -775,8 +776,7 @@ KillExistingXLOG(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
while (errno = 0, (xlde = readdir(xldir)) != NULL)
|
||||||
while ((xlde = readdir(xldir)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (strlen(xlde->d_name) == 24 &&
|
if (strlen(xlde->d_name) == 24 &&
|
||||||
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
|
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
|
||||||
@ -789,25 +789,27 @@ KillExistingXLOG(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
|
||||||
|
|
||||||
/*
|
#ifdef WIN32
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (errno)
|
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));
|
progname, XLOGDIR, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
closedir(xldir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -831,8 +833,7 @@ KillExistingArchiveStatus(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
while (errno = 0, (xlde = readdir(xldir)) != NULL)
|
||||||
while ((xlde = readdir(xldir)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
|
if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
|
||||||
(strcmp(xlde->d_name + 24, ".ready") == 0 ||
|
(strcmp(xlde->d_name + 24, ".ready") == 0 ||
|
||||||
@ -846,25 +847,27 @@ KillExistingArchiveStatus(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
|
||||||
|
|
||||||
/*
|
#ifdef WIN32
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (errno)
|
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));
|
progname, ARCHSTATDIR, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
closedir(xldir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,15 +100,19 @@ readdir(DIR *d)
|
|||||||
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
|
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
|
||||||
* long */
|
* long */
|
||||||
d->ret.d_namlen = strlen(d->ret.d_name);
|
d->ret.d_namlen = strlen(d->ret.d_name);
|
||||||
|
|
||||||
return &d->ret;
|
return &d->ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
closedir(DIR *d)
|
closedir(DIR *d)
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
if (d->handle != INVALID_HANDLE_VALUE)
|
if (d->handle != INVALID_HANDLE_VALUE)
|
||||||
FindClose(d->handle);
|
ret = !FindClose(d->handle);
|
||||||
free(d->dirname);
|
free(d->dirname);
|
||||||
free(d);
|
free(d);
|
||||||
return 0;
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -330,8 +330,7 @@ pgfnames(const char *path)
|
|||||||
|
|
||||||
filenames = (char **) palloc(fnsize * sizeof(char *));
|
filenames = (char **) palloc(fnsize * sizeof(char *));
|
||||||
|
|
||||||
errno = 0;
|
while (errno = 0, (file = readdir(dir)) != NULL)
|
||||||
while ((file = readdir(dir)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
|
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
|
||||||
{
|
{
|
||||||
@ -343,17 +342,14 @@ pgfnames(const char *path)
|
|||||||
}
|
}
|
||||||
filenames[numnames++] = pstrdup(file->d_name);
|
filenames[numnames++] = pstrdup(file->d_name);
|
||||||
}
|
}
|
||||||
errno = 0;
|
|
||||||
}
|
}
|
||||||
#ifdef WIN32
|
|
||||||
|
|
||||||
/*
|
#ifdef WIN32
|
||||||
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
/* Bug in old Mingw dirent.c; fixed in mingw-runtime-3.2, 2003-10-10 */
|
||||||
* released version
|
|
||||||
*/
|
|
||||||
if (GetLastError() == ERROR_NO_MORE_FILES)
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (errno)
|
if (errno)
|
||||||
{
|
{
|
||||||
#ifndef FRONTEND
|
#ifndef FRONTEND
|
||||||
@ -366,7 +362,15 @@ pgfnames(const char *path)
|
|||||||
|
|
||||||
filenames[numnames] = NULL;
|
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;
|
return filenames;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user