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

Hi all,

I want to know whether the file has been opened by some other operator or not before going to update the file?

How can I achieve this?

Thanks in Advance,
Srikrishnan
  • Comment on How to identify a file is in readonly mode

Replies are listed 'Best First'.
Re: How to identify a file is in readonly mode
by Marshall (Canon) on Jul 15, 2009 at 13:09 UTC
    All of this is of course very application specific. But first, you should understand that once a file is open, the name in directory means NOTHING. Lets say that you have 5 programs that are reading file X at the moment. They all have their own file handle to that file - these processes use the file handle to talk to the file - NOT the name of the file.

    Think of a file as a collection of bits on the disk. The directory provides a name that points to that set of bits. Update: small clarification: the "file name" only comes into play during "open()", once this "set of bits" on the disk is "open", the file name has no meaning at all.

    Can I "unlink" a name to a collection of bits? Absolutely! That removes a name association to those collection of bits - it does not delete those bits. Will the file system keep this straight? Yes! (ok, not on DOS (Win98), but NTFS (Win NT+) and UNIX file systems can do this). Can I have a file handle open to a file "with no name", eg a file which has no directory entry? Yes!

    Its been awhile since I wrote a Windows installation program, you may need to rename file X before you can unlink the name from the directory. Update: small clarification: Now you can copy in a new file "X". There will be a very small window where file "X" doesn't exist at all. Depending upon how this is done, it could just be some microseconds, but it could happen - just something to think about. If something only happens 10/million times, it will happen if the program is run enough million times!

    So anyway in this case, say we wind up with 5 processes reading the "old file X". Remember that they aren't using the name "X", they are using a file handle to those bits that used to be called "X". Now we have a new file X. Every new program that starts and wants file X will get the new set of bits. But what about those other guys who have the old "stale" set of bits"? Well, they keep reading and using them until we say different. The file system will keep those "stale bits" in play until everybody using them "closes" the file.

    This gets so messy with the Microsoft O/S that it generates lots of reboots because there is not such a big line between application and O/S.

    So, Can I replace a read-only file with another? Yes! But you need a plan to tell the folks who were using the old version to read the file again.

    I am not considering the case where multiple programs may be updating a file - that's way more complex. First let's get straight on the "hey, 5 people are reading a file and I want to give them a new file" case.

Re: How to identify a file is in readonly mode
by gulden (Monk) on Jul 15, 2009 at 12:03 UTC
    You can use flock, to grant EXCLUSIVE or SHARED access using LOCKs, but that implies that both uses flock. This my solve or not your problem... If you want to to use flock see this link too.
Re: How to identify a file is in readonly mode
by cdarke (Prior) on Jul 15, 2009 at 12:42 UTC
    Since you are on Windows, use the Win32::File (bundled with ActiveState perl) CreateFile interface. Set svShare to zero.
Re: How to identify a file is in readonly mode
by JavaFan (Canon) on Jul 15, 2009 at 11:45 UTC
    In general, you cannot. On some OSses, if you have the right permissions, there sometimes may be a way.
      Hi, Thanks for your reply I am using Windows XP professional. In this is there any way, if need I will get rights from my Administrator. Can you help me in this regard? Srikrishnan
        No, I cannot help you. I don't have much Windows knowledge.
Re: How to identify a file is in readonly mode
by Bloodnok (Vicar) on Jul 15, 2009 at 14:41 UTC
    AFAIR, open FILE, ">$some_file" will fail if some other process already has the file open for writing, thus you could eval the call (to open) and proceed based on the caught exception - if any.

    A user level that continues to overstate my experience :-))
      Hi,
      You can refer this Node in the Q & A section .
      Raja
        Hmmm, not quite.

        -w tests to see if the script is capable of writing (to) the file i.e. using the statically defined permissions on the file, not dynamically i.e. run-time determination, as required by the OP.

        A user level that continues to overstate my experience :-))