Solved

deleting a shared file

Posted on 2002-06-11
5
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

726 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