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
Solved

deleting a shared file

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 169
Best book to learn C++ 4 83
Eclipse IDE - Cannot copy/paste from console output 8 252
Safe conversion? 4 72
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 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