postgres.git / summary / log / commit / refs

commit    977f833772de09970da375fb68a46e33bbf397cb
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Mon Mar 04 00:50:19 2019 +0000
First released in: 12.0, 9.4.22, 9.5.17, 9.6.13, 10.8, 11.3

    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


src/port/dirent.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/port/dirent.c b/src/port/dirent.c index f59e8ef9475..8835a6b1f54 100644 --- a/src/port/dirent.c +++ b/src/port/dirent.c @@ -84,7 +84,11 @@ readdir(DIR *d) d->handle = FindFirstFile(d->dirname, &fd); if (d->handle == INVALID_HANDLE_VALUE) { - errno = ENOENT; + /* If there are no files, force errno=0 (unlike mingw) */ + if (GetLastError() == ERROR_FILE_NOT_FOUND) + errno = 0; + else + _dosmaperr(GetLastError()); return NULL; } } @@ -92,13 +96,11 @@ readdir(DIR *d) { if (!FindNextFile(d->handle, &fd)) { + /* If there are no more files, force errno=0 (like mingw) */ if (GetLastError() == ERROR_NO_MORE_FILES) - { - /* No more files, force errno=0 (unlike mingw) */ errno = 0; - return NULL; - } - _dosmaperr(GetLastError()); + else + _dosmaperr(GetLastError()); return NULL; } } [parent: 556fdd49c9e0]