Solved

deleting a shared file

Posted on 2002-06-11
5
245 Views
Last Modified: 2012-08-14

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
0
Comment
Question by:willmrk
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7071145
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.
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 7071153
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...
0
 
LVL 86

Expert Comment

by:jkr
ID: 7071168
>>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...
0
 
LVL 11

Expert Comment

by:griessh
ID: 7071732
Do you have to physically delete the file or would it be enough to delete the CONTENT of the file?
 
======
Werner
0
 
LVL 3

Accepted Solution

by:
jimbucci earned 100 total points
ID: 7093741
You need to delete a file that you have open.  So you close the file, because you can't delete it while you have it open.  At that instance that you close the file another machine opens the file reads the timestamp and decides that the file needs to be deleted.  You're setting yourself up for a race condition or possible deadlock which may only occur when multiple machines are vying for this resource.  What if one machine is made to actually delete the file while the others write to it?

Multiple remote access to a resource could be solved by using some kind of semaphore (another file) that each process must get before it can modify the resource.

Am I on the right track here?  I've been programming in Windows so these problems are easy to solve.
JB


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now