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


In reply to Re: file information by wog
in thread file information by hotshot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.