Solved

deleting a shared file

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
how to convert c++ code to Android App 3 93
VS2015 compilation and missing DLLs 9 147
Header of docx file 17 102
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

806 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