in reply to How to identify a file is in readonly mode

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.

  • Comment on Re: How to identify a file is in readonly mode