Solved

deleting a shared file

Posted on 2002-06-11
5
244 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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