Open a file and make an
FD for it. Truncates the file to zero
size when the
IOMode is
WriteMode.
openFileWith takes two actions,
act1 and
act2, to perform after opening the file.
act1 is passed a file descriptor and I/O device type for the
newly opened file. If an exception occurs in
act1, then the
file will be closed.
act1 must not close the file
itself. If it does so and then receives an exception, then the
exception handler will attempt to close it again, which is
impermissible.
act2 is performed with asynchronous exceptions masked. It is
passed a function to restore the masking state and the result of
act1. It /must not/ throw an exception (or deliver one via an
interruptible operation) without first closing the file or arranging
for it to be closed.
act2 may close the file, but is
not required to do so. If
act2 leaves the file open, then the
file will remain open on return from
openFileWith.
Code calling
openFileWith that wishes to install a finalizer to
close the file should do so in
act2. Doing so in
act1 could potentially close the file in the finalizer first
and then in the exception handler. See
openFile' for an example
of this use. Regardless, the caller is responsible for ensuring that
the file is eventually closed, perhaps using
bracket.