pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Konstantin Knizhnik <k.knizhnik@postgrespro.ru>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: readdir is incorrectly implemented at Windows
Date: Mon, 25 Feb 2019 18:38:16 +0300
Message-ID: <2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru> (raw)

Hi hackers,

Small issue with readir implementation for Windows.
Right now it returns ENOENT in case of any error returned by FindFirstFile.
So all places in Postgres where opendir/listdir are used will assume 
that directory is empty and
do nothing without reporting any error.
It is not so good if directory is actually not empty but there are not 
enough permissions for accessing the directory and FindFirstFile
returns ERROR_ACCESS_DENIED:

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)
         {
             errno = ENOENT;
             return NULL;
         }
     }


Attached please find small patch fixing the problem.

-- 
Konstantin Knizhnik
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

Attachments:

dirent.patch (text/x-patch, 484B) download diff --git a/src/port/dirent.c b/src/port/dirent.c index 7a91450..284bf27 100644 --- a/src/port/dirent.c +++ b/src/port/dirent.c @@ -83,7 +83,13 @@ readdir(DIR *d) d->handle = FindFirstFile(d->dirname, &fd); if (d->handle == INVALID_HANDLE_VALUE) { - errno = ENOENT; + if (GetLastError() == ERROR_FILE_NOT_FOUND) + { + /* No more files, force errno=0 (unlike mingw) */ + errno = 0; + return NULL; + } + _dosmaperr(GetLastError()); return NULL; } }

view thread (8+ messages)  latest in thread

Message-ID: <2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru>
Permalink:  ../2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru/
Also on:    postgresql.org/message-id/2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: k.knizhnik@postgrespro.ru
  Subject: Re: readdir is incorrectly implemented at Windows
  In-Reply-To: <2cad7829-8d66-e39c-b937-ac825db5203d@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox