[PATCH] mkdir: alway check-for-existence

Ben cygwin@wijen.net
Mon Jun 3 18:31:00 GMT 2019

When using either CreateDirectory or NtCreateFile when creating a directory
that already exists, these functions return: ERROR_ALREADY_EXISTS

However when using these function to create a directory (and all its 
a normal use would be to start with mkdir(‘/c’) which translates to ‘C:\’
for which both of these functions return ‘ERROR_ACCESS_DENIED’

We could call NtCreateFile with 'FILE_OPEN_IF' instead of 'FILE_CREATE' but
since that's an internal function we better always check for existence

Signed-off-by: Ben Wijen <ben@wijen.net>
  winsup/cygwin/dir.cc | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc
index f43eae461..b757851d5 100644
--- a/winsup/cygwin/dir.cc
+++ b/winsup/cygwin/dir.cc
@@ -331,8 +331,10 @@ mkdir (const char *dir, mode_t mode)
        debug_printf ("got %d error from build_fh_name", fh->error ());
        set_errno (fh->error ());
+      else if (fh->exists ())
+    set_errno (EEXIST);
        else if (has_dot_last_component (dir, true))
-    set_errno (fh->exists () ? EEXIST : ENOENT);
+    set_errno (ENOENT);
        else if (!fh->mkdir (mode))
      res = 0;
        delete fh;


More information about the Cygwin-patches mailing list