I'm trying to create a tar file from a directory's contents using Codec.Archive.Tar, but I also want to clean up the directory once the tar file has been created. Here's a small example which demonstrates my issue:
import System.Directory
import qualified Codec.Archive.Tar as T
listFile = do createDirectory "dir"
createDirectory "dir/dir2"
tarfile <- fmap T.write $ T.pack "dir" ["dir2"]
removeDirectoryRecursive "dir"
return tarfile
When I call the function listFile e.g. from within ghci, I get back
"*** Exception: dir/dir2: getModificationTime:getFileTimes:getFileStatus: does not exist (No such file or directory)
which I'm guessing is caused by the tar file being generated lazily and the directory being cleaned up strictly. Thus the directory is deleted before the tar file is actually created.
First, am I correct in my analysis of why this is failing? If so, what can I do to fix this? I don't want to generate the tar file strictly because it could be fairly large and I don't want to store it all in memory. What's the "idiomatic" way to delay deleting the directory until the tar file has been generated?