Problem with open() and O_CREAT

Fifer, Eric EFifer@sanwaint.com
Fri Mar 17 06:23:00 GMT 2000


"Corinna Vinschen" wrote:
>[...]
>your patch looks ok to me. Thank you!
>[...]
>But I wonder if we couldn't do without the request for the state
>of `creation_distribution'. Shouldn't this be enough when asking
>for GetLastError() == ERROR_ALREADY_EXISTS? I think so, because
>O_CREAT implies CREATE_ALWAYS or OPEN_ALWAYS if O_EXCL isn't
>used whereas O_EXCL results in a different behaviour, either.

I agree.  Although, I had to carefully reread MSDN and stare
at the code.  As you point out O_CREAT and O_EXCL result
in CREATE_NEW and in this case if the file already exists, the
CreateFile() will fail (instead of succeeding and setting
ERROR_ALREADY_EXISTS as it does with CREATE_ALWAYS or OPEN_ALWAYS).

Anyway, the simplified patch now becomes:

diff -rup winsup.orig/cygwin/fhandler.cc winsup/cygwin/fhandler.cc
--- winsup.orig/cygwin/fhandler.cc      Fri Mar 17 04:55:11 2000
+++ winsup/cygwin/fhandler.cc   Fri Mar 17 13:41:07 2000
@@ -340,7 +340,8 @@ fhandler_base::open (int flags, mode_t m
       goto done;
     }
 
-  if (flags & O_CREAT && get_device () == FH_DISK)
+  if (flags & O_CREAT && get_device () == FH_DISK &&
+      GetLastError () != ERROR_ALREADY_EXISTS)
     set_file_attribute (has_acls (), get_win32_name (), mode);
 
   namehash_ = hash_path_name (0, get_win32_name ());


Thanks.

Eric



More information about the Cygwin-developers mailing list