Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tell if a file is already open?

Status
Not open for further replies.

chughes

MIS
Aug 7, 2001
27
US
Hi folks,
I'm pretty much a novice at perl.
I'd like to know if there is way to tell
if a file is open in perl.
I'm getting ready to write some file management
scripts and this would come in awfully handy.
I can't find anything that mentions this in any
documentation.... anyone here got any ideas?
I need to know if a file is open before I move
a file; the software I am using will write the
file back to it's original location if it is moved
while the application is open. (then I have duplicate files blah blah blah....)
thanks in dvnace
Chris
 
Look at the file locking function (flock):
You could try to obtain exclusive access to a file. If you succeed, then (I think) you can assume no one else has the file open (or at least an active open file handle, some programs cache, and there's no way around that). ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
I'm not sure that icrf is correct on this (rare) ocassion. Depending on the underlying operating system, flock can often be an advisory protocol rather than a mandatory one. This means that it only works if every app accessing the same file calls flock. From what you outline I think that this might not be the case in your situation.

In a *x environment, you could try fiddling with the output from lsof but you would have problems with timing issues (lack of atomicity).
 
this is going to be windows XP/2000 accessing files on
a novell server....
so what you are saying is this is only good if the other
applications that are accessing the file have to use flock - ruh roh. Probably not... I'll see in the next week whether or not this will work....
thanks for the advise ( no pun intended)
Chris
 
If your file handling is exclusively from Perl, you could implement a lock database that all your perl programs lookup for access to files, and modify if they take a lock on a file.

It would mean writing a file handler to implement each of the file access functions with read/update access to the lock database.

Just a thought.
;P
 
Chris

If you're on Novell then I don't think locking is advisory. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top