O_EXCL under Windows?

2005-02-25 Thread Hrvoje Niksic
Is there a way to get the functionality of open(..., O_CREAT|O_EXCL)
under Windows?  For those who don't know, O_EXCL opens the file
"exclusively", guaranteeing that the file we're opening will not be
overwritten.  (Note that it's not enough to check that the file
doesn't exist before opening it; it can spring into existence between
the check and the open.)

I cannot find a way to do the equivalent on Windows.  Does O_EXCL just
work on Windows?  A search for O_EXCL on MSDN doesn't reveal any
results.


Re: O_EXCL under Windows?

2005-02-25 Thread Gisle Vanem
"Hrvoje Niksic" wrote:
Is there a way to get the functionality of open(..., O_CREAT|O_EXCL)
under Windows?  For those who don't know, O_EXCL opens the file
"exclusively", guaranteeing that the file we're opening will not be
overwritten.  (Note that it's not enough to check that the file
doesn't exist before opening it; it can spring into existence between
the check and the open.)
This works with MingW and MSVC. Watcom acts a bit odd if you use
fdopen (fd, "w+") afterwards. A snippet from my contribution to libnet
(works across processes of course):
#include 
#include 
#include 
int open_flags = O_WRONLY | O_CREAT | O_TRUNC;
/* possibly drop the O_WRONLY for r/w */
int fd = sopen (file_name, open_flags | O_BINARY | _O_SEQUENTIAL,
SH_DENYWR, S_IREAD | S_IWRITE);
_O_SEQUENTIAL is just to tell the cache manager to stop wasting
memory. 

--gv