http://qs321.pair.com?node_id=135520


in reply to file information

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.)