Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

file information

by hotshot (Prior)
on Jan 01, 2002 at 20:18 UTC ( [id://135516]=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys !
Is there a perl module to help me know if a certain file is currently open for editing by another application/editor?
I'm implementing a locking mechanizm for files that are in use by our system. as long as these files are edited through my program and shell I support, everyting is ok, but if someone edits files maually (through unix, with vi for example) I have trouble to know that, I can use ps to know if the file '/tmp/foo' is opened in vi, but I can't cover all cases, especially since there are a lot of editors, and the paths are not always absolute (you can do vi /tmp/foo, or vi foo from /tmp).
anyone has a suggetions?

Hotshot

Replies are listed 'Best First'.
Re: file information
by wog (Curate) on Jan 01, 2002 at 20:58 UTC
    There is (update: currently (that I am aware of)) no perl module which can tell if a file is open in another program.

    There is a utility called lsof which may let you fiquire out if the file is opened by another application on UNIX-like systems, but this is likely to be slow, and probably will suffer from race conditions (you check if the file is open elsewhere, someone edits the file, you do something with the file, someone stops editing the file. Oops.)

    If you really want to prevent people from doing things with these files you could probably use mandatory file locking, if your system supports it. This is likely to be very much unportable. (On Solaris one can do this by setting the setgid bit without setting the executable-by-group bit and using lockf or fcntl with F_GETLK, etc. (update: So much for trusting manpages... ("discretionary file locks", indeed). Much thanks to traveler) On Linux, it appears this cannot be done.)

    On Windows, you can actually get this information and/or obtain exclusive write access to a file... A Super Search of this site should tell you how.

    A better solution would probably be to either trust the user to not do that or, if possible, give the file permissions that prevent normal users from doing things with it (and presumably the "privallaged" users will be able to tell when they shouldn't do something. Or they'll modify your package to remove the restriction. (; ) If these are temporary files, you may even be able to remove the need for the file to actually have a name on disk. (Hint: on some UNIX systems, /dev/fd/X corresponds to fileno X. Or you can use open to turn a specific filehandle into stdin and stdout... And IO::File provides new_tmpfile.)

      On Linux, it appears this cannot be done.</cdnter>

      Since about kernel version 2.0.34 Linux has had mandatory locks. The semantics are based on POSIX/SVID. The details are described in Documentation/mandatory.txt. Using Linux mandatory locks is virtually identical to using them under Solaris.

      HTH, --traveler

Re: file information
by mojotoad (Monsignor) on Jan 01, 2002 at 22:15 UTC
    What is the nature of the files in question? If it is a particular set of text files, rather than all files on the system, then you might dispense with the need for locking altogether by managing their updates through CVS.

    Matt

Re: file information
by Aristotle (Chancellor) on Jan 02, 2002 at 06:13 UTC
    Not only is ps not to be trusted for reasons of absolute vs relative path; you can also launch vi with no file specified, then open the file from the editor, leaving no indication of it in the process list.

    Besides ++wog's excellent suggestions, you may also want to look at man fuser. (It seems that this command is more available; I have it on my box, but don't have lsof. If the latter doesn't work for you, fuser may be it.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://135516]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found