7

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

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
I don't understand how this would work. The file would surely still be deleted when the FILE_DELETE_ON_CLOSE handle is closed?
@MHebes nope, it would be deleted when all handles to the file are closed. Which is why this works.
I should have been more clear @MHebes but the idea is to reach the condition where it is known that the file would need to be kept or not, and add - or not add - the flag then.
6

It is in fact possible to do what you want:

  • Open the file with DELETE access (0x10000), and without FILE_FLAG_DELETE_ON_CLOSE.

  • Call SetFileInformationByHandle with information class FileDispositionInfo (4) and a pointer to a FILE_DISPOSITION_INFO struct whose DeleteFile member indicates whether the file should be deleted. (The struct has no other members.) You can set and reset this flag as you like.

If you set FILE_FLAG_DELETE_ON_CLOSE at creation time then this flag is ignored, even though setting it to FALSE returns a success code. I think there is no way to cancel FILE_FLAG_DELETE_ON_CLOSE.

Comments

0

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.

2 Comments

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.
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.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.