Re: How do I check if a file is in use?
- From: "Ancient_Hacker" <grg2@xxxxxxxxxxx>
- Date: 21 Sep 2006 04:22:45 -0700
Mark wrote:
Hi all,
This is something which has been bugging me for ages. How can I check
if a file is already in use by a different program?
There's a very basic problem here-- nobody can predict the future.
Even if there was a standard C function named FileIsFree( char * Path
), there's no way it, or any other function, could ensure that file
would still exist, be free, or contain the same data, even one CPU
clock cycle later.
So any attempt to do this is bound to fail, some of the time. I assume
that's not good enough for a production program.
What you need to do is find some operation that is "atomic", i.e.
indivisible, irrevocable, and can only happen to a unbusy file.
For example, change the program or batch file that is writing the file
to write the file to /tmp/foo.bar. When it's done, have it close the
file, then rename it to the proper destination file name. On every OS
I can forsee, "rename" is an atomic operation, i.e. you'll never see
the file in both places, or no place (well, maybe this) at any one
time.
If you can't change the source program, maybe you can wrap it in a
batch file that does the renaming.
-----
Another possibility, very non-portable, but in Windows you can make API
requests that cause a function in your program to be called every time
a file or directory changes. That's a very good low-overhead way to
do what you need, but specific to Windows.
I don't think the Unix flavors have this kind of hook but of course it
may be a recent addition or a Linux kernel addon.
.
- Follow-Ups:
- Re: How do I check if a file is in use?
- From: Keith Thompson
- Re: How do I check if a file is in use?
- References:
- How do I check if a file is in use?
- From: Mark
- How do I check if a file is in use?
- Prev by Date: qustion about function pointer.
- Next by Date: Re: Exercise 1-10 in K&R2
- Previous by thread: Re: How do I check if a file is in use?
- Next by thread: Re: How do I check if a file is in use?
- Index(es):
Relevant Pages
|