opening a file for writing and reading

Duncan Coutts duncan.coutts at googlemail.com
Tue Jan 12 06:03:05 EST 2010


On Mon, 2010-01-11 at 20:22 -0800, Bryan O'Sullivan wrote:
> On Mon, Jan 11, 2010 at 7:49 PM, Jeremy Shaw <jeremy at n-heptane.com>
> wrote:
>         Alas, I get the error, openBinaryFile: resource busy (file is
>         locked).
>         
>         What is the proper work around for this?
> 
> 
> This behaviour is specified in the Haskell standard. I don't know what
> the rationale is intended to be, but I don't like the behaviour
> either.

I think in general it's to help you not shoot yourself in the foot by
having one part of the code write over stuff you're in the middle of
reading. In particular this would help for programs of the form

$ ./prog foo -o foo

where it ends up using the same file as both input and output. If this
was what you actually intended to do then of course you can use the same
single read/write file handle.

There's also a specific case where it stops you shooting yourself in the
foot. A file handle that you're using with hGetContents really should
not change underneath you. That's why it gets put into the semi-closed
state. But if you could have other open write handles to the same file
then you can still shoot yourself in the foot and write weirdy
non-deterministic programs.

> I believe you can work around it by using System.Posix.IO to open a
> file, then convert the Fd to a Handle.

Right, you can bypass the safety mechanism that way.

Duncan



More information about the Libraries mailing list