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

Re: file information

by wog (Curate)
on Jan 01, 2002 at 20:58 UTC ( #135520=note: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Re: file information
by traveler (Parson) on Jan 02, 2002 at 01:11 UTC
    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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://135520]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-09-27 04:38 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (118 votes). Check out past polls.