filesystem and existing files

With filesystem, you can do filesystem::path P(....); filesystem::ofstream S; if (!filesystem::exists(P)) { S.open(P); } But isn't there a race condition here which could lead to a file created by another process being wiped out, or added to? Is there a way to get ofstream::open to fail if the file already exists? It succeeds on VC7.1 but in some situations I don't want it to succeed. Cheers Russell

Hi Russell,
With filesystem, you can do
filesystem::path P(....); filesystem::ofstream S; if (!filesystem::exists(P)) { S.open(P); }
But isn't there a race condition here which could lead to a file created by another process being wiped out, or added to?
Is there a way to get ofstream::open to fail if the file already exists? It succeeds on VC7.1 but in some situations I don't want it to succeed.
I believe you can't do it portably.... on Linux, for example, there's O_EXCL flag to the 'open' system call, but you can't specify this flag via boost::fs. In fact, this is related to temporary file discussion we had on dev list before. Probably, it means we need a function in boost::fs to do what you ask for. - Volodya

Vladimir Prus wrote:
I believe you can't do it portably.... on Linux, for example, there's O_EXCL flag to the 'open' system call, but you can't specify this flag via boost::fs. In fact, this is related to temporary file discussion we had on dev list before.
Probably, it means we need a function in boost::fs to do what you ask for.
I think it would be useful, although I'm not sure how you could integrate it with std streams? If you use CreateFile in Win32 with CREATE_NEW (which fails if exists), can you construct a std::ofstream with that handle? (I guess it would be similar in linux)? Cheers Russell

Russell Hind wrote:
Vladimir Prus wrote:
I believe you can't do it portably.... on Linux, for example, there's O_EXCL flag to the 'open' system call, but you can't specify this flag via boost::fs. In fact, this is related to temporary file discussion we had on dev list before.
Probably, it means we need a function in boost::fs to do what you ask for.
I think it would be useful, although I'm not sure how you could integrate it with std streams? If you use CreateFile in Win32 with CREATE_NEW (which fails if exists), can you construct a std::ofstream with that handle? (I guess it would be similar in linux)?
I'm probably missing something very basic, but you can open fstream using the filename. After all, CreateFile made sure that file did not exist before and created the new one for your use. Now, you can safely open it by name. The only problem (as discussed of dev-) is when somebody removes the file between the time you called CreateFile and you construct fstream. On Unix, this can be prevented by checking directory permissions. I'm not sure what permission models are there on Windows... last used it during Win95 days. - Volodya

Vladimir Prus wrote:
I'm probably missing something very basic, but you can open fstream using the filename. After all, CreateFile made sure that file did not exist before and created the new one for your use. Now, you can safely open it by name.
No, I was missing something. I hadn't thought of this. I was only trying to do it in one go. Thanks for the suggestion. Russell
participants (2)
-
Russell Hind
-
Vladimir Prus