+ results <- mapM (import_file cfg) (OC.xml_files opt_config)
+
+ -- Zip the results with the files list to find out which ones can be
+ -- deleted.
+ let result_pairs = zip (OC.xml_files opt_config) results
+ let victims = filter (\(_,result) -> result) result_pairs
+ mapM_ ((kill True) . fst) victims
+
+ where
+ kill try_again path = do
+ removeFile path `catchIOError` exception_handler
+ report_info $ "Removed imported file " ++ path ++ "."
+ where
+ -- | A wrapper around threadDelay which takes seconds instead of
+ -- microseconds as its argument.
+ thread_sleep :: Int -> IO ()
+ thread_sleep seconds = do
+ let microseconds = seconds * (10 ^ (6 :: Int))
+ threadDelay microseconds
+
+ exception_handler :: IOError -> IO ()
+ exception_handler e = do
+ report_error (show e)
+ report_error $ "Failed to remove imported file " ++ path ++ "."
+ if try_again then do
+ report_info $ "Waiting 5 seconds to attempt removal again..."
+ thread_sleep 5
+ kill False path
+ else
+ report_info $ "Giving up on " ++ path ++ "."