
On Wed, Sep 17, 2008 at 02:10:56PM +0100, Dougal Stanton wrote:
On Wed, Sep 17, 2008 at 1:17 PM, Manlio Perillo
wrote: The Python tempfile module, as an example, implements a wrapper around mkstemp function that does exactly this, and the code is portable; on Windows it uses O_TEMPORARY_FILE flag, on POSIX systems the file is unlink-ed as soon as it is created (but note that the code is not signal safe - well, many functions in the Python standard library are not signal safe).
Something like:
withTempFile :: String -> ((FilePath, Handle) -> IO b) -> IO b withTempFile name action = do tmpdir <- getTemporaryDirectory bracket (openTempFile tmpdir name) (\(fp,h) -> hClose h >> removeFile fp) action
I don't know if this has the safety requirements you mean?
You need to be sure to use the bracket from Control.Exception, not the one from System.IO or IO (which won't work). And you also need special work to make the code safe from signals. But basically, this is the right idea. David