in reply to Check whether a file is open

This is one thorny problem. Take, for example, a file arriving via an ftp or scp delivery. The client program may open the file start writing, pause, write some more, operate slowly due to the bandwidth and eventually, maybe sometime finish the transfer. In the meantime your process, running quickly on the local box how no way of knowing whether it is safe to operate on the file.

Grrrrr there's no cross platform, consistent way to find out if a file is open. If you're in this position, you have to develop some sanity check method(s) to assure yourself the process writing to the file is finished.

If you're beginning to think I've been bitten by this, you're right. What we eventually did was:

At least the above method has worked for us for over two years now.

I wish there was a better way. If there is one I'd love to hear about it. If there is a filesysystem that implements something like electricians locks I'd put it on all the systems I'm responsible for.

Be Appropriate && Follow Your Curiosity

Replies are listed 'Best First'.
Re^2: Check whether a file is open
by tirwhan (Abbot) on Jun 02, 2005 at 16:53 UTC
    I wish there was a better way. If there is one I'd love to hear about it. If there is a filesysystem that implements something like electricians locks I'd put it on all the systems I'm responsible for.

    The Reiser4 filesystem implements atomic transactions which can span multiple read/write operations. So your applications can just bunch together operations into a single transaction and not have to worry about what else is accessing the file. You have to use the Reiser-specific system calls though, otherwise it's just atomic to a single operation.

    Of course Reiser4 isn't exactly what you'd call production-ready yet...

Re^2: Check whether a file is open
by ambrus (Abbot) on Jun 02, 2005 at 21:12 UTC

    See thread of file locking. Mandatory locking exists (under some systems), but is not a solution.