Link to home
Start Free TrialLog in
Avatar of willmrk
willmrk

asked on

deleting a shared file


I have a file that exists on a server that multiple machines will be accessing using the program that I am writing. They access the file using the _fsopen command with _SH_DENYRW permissions. Each of the computers open the file, and checks the timestamp within the file. If it is not over the specified age, that machine will add data and close the file. If the timestamp is over the specified age, it needs to be deleted.

Can I just close the file and then delete it? But I'm thinking that by closing the file, one of the other machines will gain access to it and then it wont be able to be deleted. Is there a way to close the file, but keep ownership of it so no one else can access it before I delete it?

Thanks
Avatar of jkr
jkr
Flag of Germany image

I do not know of a way using '_fsopen()', but using 'CreateFile()' to open that file, you could specify 'FILE_FLAG_DELETE_ON_CLOSE', which pretty much does what you want:

FILE_FLAG_DELETE_ON_CLOSE
 Indicates that the operating system is to delete the file immediately after all of its handles have been closed, not just the handle for which you specified FILE_FLAG_DELETE_ON_CLOSE.
Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE is used.
 
Disregard my last comment - that doesn't make sense, as you do not know whether you you want to delete it before opening the file...
>>Can I just close the file and then delete it?

Actually, that's what I would do - just consider what could happen:

- you close the file and want to delete it
- another machine opens it and finds out that it should be deleted
- your delete operation fails
- the other machine deletes the file

I cannot think of any harm in that scenario...
Do you have to physically delete the file or would it be enough to delete the CONTENT of the file?
 
======
Werner
ASKER CERTIFIED SOLUTION
Avatar of jimbucci
jimbucci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial