-savingOldConfig :: FilePath -> IO () -> IO ()
-savingOldConfig filename io = Exception.block $ do
- hPutStr stdout "Saving old package config file... "
- -- mv rather than cp because we've already done an hGetContents
- -- on this file so we won't be able to open it for writing
- -- unless we move the old one out of the way...
- let oldFile = filename ++ ".old"
- restore_on_error <- catch (renameFile filename oldFile >> return True) $
- \err -> do
- unless (isDoesNotExistError err) $ do
- hPutStrLn stderr (unwords ["Unable to rename", show filename,
- "to", show oldFile])
- ioError err
- return False
- (do hPutStrLn stdout "done."; io)
- `onException` do
- hPutStr stdout ("WARNING: an error was encountered while writing "
- ++ "the new configuration.\n")
- -- remove any partially complete new version:
- removeFile filename `catchIO` \_ -> return ()
- -- and attempt to restore the old one, if we had one:
- when restore_on_error $ do
- hPutStr stdout "Attempting to restore the old configuration... "
- do renameFile oldFile filename
- hPutStrLn stdout "done."
- `catchIO` \err -> hPutStrLn stdout ("Failed: " ++ show err)
- -- Note the above renameFile sometimes fails on Windows with
- -- "permission denied", I have no idea why --SDM.
-