postgresql/src/port/dirent.c
Michael Paquier 754b90f657 Fix error handling of readdir() port implementation on first file lookup
The implementation of readdir() in src/port/ which gets used by MSVC has
been added in 399a36a, and since the beginning it considers all errors
on the first file lookup as ENOENT, setting errno accordingly and
letting the routine caller think that the directory is empty.  While
this is normally enough for the case of the backend, this can confuse
callers of this routine on Windows as all errors would map to the same
behavior.  So, for example, even permission errors would be thought as
having an empty directory, while there could be contents in it.

This commit changes the error handling so as readdir() gets a behavior
similar to native implementations: force errno=0 when seeing
ERROR_FILE_NOT_FOUND as error and consider other errors as plain
failures.

While looking at the patch, I noticed that MinGW does not enforce
errno=0 when looking at the first file, but it gets enforced on the next
file lookups.  A comment related to that was incorrect in the code.

Reported-by: Yuri Kurenkov
Diagnosed-by: Yuri Kurenkov, Grigory Smolkin
Author:	Konstantin Knizhnik
Reviewed-by: Andrew Dunstan, Michael Paquier
Discussion: https://postgr.es/m/2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru
Backpatch-through: 9.4
2019-03-04 09:49:06 +09:00

124 lines
2.4 KiB
C

/*-------------------------------------------------------------------------
*
* dirent.c
* opendir/readdir/closedir for win32/msvc
*
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/port/dirent.c
*
*-------------------------------------------------------------------------
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#include <dirent.h>
struct DIR
{
char *dirname;
struct dirent ret; /* Used to return to caller */
HANDLE handle;
};
DIR *
opendir(const char *dirname)
{
DWORD attr;
DIR *d;
/* Make sure it is a directory */
attr = GetFileAttributes(dirname);
if (attr == INVALID_FILE_ATTRIBUTES)
{
errno = ENOENT;
return NULL;
}
if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
{
errno = ENOTDIR;
return NULL;
}
d = malloc(sizeof(DIR));
if (!d)
{
errno = ENOMEM;
return NULL;
}
d->dirname = malloc(strlen(dirname) + 4);
if (!d->dirname)
{
errno = ENOMEM;
free(d);
return NULL;
}
strcpy(d->dirname, dirname);
if (d->dirname[strlen(d->dirname) - 1] != '/' &&
d->dirname[strlen(d->dirname) - 1] != '\\')
strcat(d->dirname, "\\"); /* Append backslash if not already there */
strcat(d->dirname, "*"); /* Search for entries named anything */
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
return d;
}
struct dirent *
readdir(DIR *d)
{
WIN32_FIND_DATA fd;
if (d->handle == INVALID_HANDLE_VALUE)
{
d->handle = FindFirstFile(d->dirname, &fd);
if (d->handle == INVALID_HANDLE_VALUE)
{
/* If there are no files, force errno=0 (unlike mingw) */
if (GetLastError() == ERROR_FILE_NOT_FOUND)
errno = 0;
else
_dosmaperr(GetLastError());
return NULL;
}
}
else
{
if (!FindNextFile(d->handle, &fd))
{
/* If there are no more files, force errno=0 (like mingw) */
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
else
_dosmaperr(GetLastError());
return NULL;
}
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
d->ret.d_namlen = strlen(d->ret.d_name);
return &d->ret;
}
int
closedir(DIR *d)
{
int ret = 0;
if (d->handle != INVALID_HANDLE_VALUE)
ret = !FindClose(d->handle);
free(d->dirname);
free(d);
return ret;
}