tags:

views:

44

answers:

3

in my app I'd like to open a temp file with FILE_FLAG_DELETE_ON_CLOSE. however there are some cases where the temp file needs to be kept and is quite large

I'd like to remove the FILE_FLAG_DELETE_ON_CLOSE attribute on an opened handle? is this possible? copying the contents of the file or renaming isnt quite what I want, I'd like to remove the attribute. this is due to how i'm transacting some writes, in my app closing the handle will open me up to a race condition

A: 

Sure, you don't have to have that flag. If there are no other flags, pass a value of zero.

It's a complex system call, but most of the flags for dwFlagsAndAttributes are rarely used. See this.

wallyk
He's asking if he can remove the flag after calling CreateFlile (where it gets set on open) but before calling CloseHandle.
selbie
+2  A: 

No, it's not possible once you've done the create. A dangerous idea might start with the statement, "That flag is only relevant to the handle the link was opened on." So creating a new link might "fix" it. I haven't thought about that more than five seconds, but it's the only slightly clever thing coming to me at midnight.

jrtipton
This isn't going to work. The documentation does not place any restrictions on the order of deletion - merely that all handles to the file must have been opened with FILE_SHARE_DELETE.
Chris Becke
To clarify, my point was that creating a new link and letting the old one die would keep the file around. But that's weird, and releases the original name. Your suggestion to just use two handles all of the time is better, but this should work. (DELETE_ON_CLOSE does not delete a file but rather a link.)
jrtipton
+3  A: 

You could do it the other way around. Open the file first, specifying FILE_SHARE_DELETE. Then, when you need to close the file, open it again with FILE_DELETE_ON_CLOSE, then close both handles. Or, just close it, and it won't be deleted.

Chris Becke
This is smarter than what I said (though it's still not possible to change it on a per-handle basis). You just have to make certain you can tolerate FILE_SHARE_DELETE.
jrtipton