There may be a problem with the handling of O_CREAT in linux.
I've always thought that O_CREAT should be ignored in a open() call if
the file exists. I don't have the posix specs handy, so please correct
me if this is wrong.
You're right! This is definitely a place where we're not conformant
with the POSIX specs.
I had a look at linux/fs/open.c and the problem seems to be this but
of code in do_open() which assumes that O_CREAT implies opening for
writing:
if (flag & (O_TRUNC | O_CREAT))
flag |= 2;
changing it to:
if (flag & O_TRUNC)
flag |= 2;
would probably "fix" the problem, but might have some nasty side effects.
I've taken a look at the code, too, and I think your fix should be safe.
I haven't tried it though. It's definitely worth experimenting with.
- Ted