[Haskell-cafe] about openTempFile

Don Stewart dons at galois.com
Wed Sep 17 11:28:07 EDT 2008


manlio_perillo:
> Hi.
> 
> After reading the chapter about IO in the "Real Word Haskell" book, I 
> have noted that there is no support for opening a temporary file that 
> will be automatically removed at program termination.
> 
> 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).
> 
> There are reasons why GHC library does not implement this?
> 
> 
> The Python version also set the FD_CLOEXEC, O_NOINHERIT and O_NOFOLLOW 
> flags (where available).
> The GHC version, instead, set the O_NOCTTY flag.

It would be something like:

    withTempFile :: FilePath -> String -> ((FilePath,Handle) -> IO r) -> IO r
    withTempFile name tmp = bracket (openTempFile name tmp)
                                    (\(f,h) -> hClose h >> removeFile f) 

-- Don


More information about the Haskell-Cafe mailing list