From 7234b1f9a7cb8f4b82260bcacab71e4cf439467c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 17 Jul 2019 14:22:21 -0400 Subject: [PATCH] small optimisation to file copying Avoid statting file, just try to remove it. Also a comment to explain why it tries to remove it, which was puzzling me when I revisited this code until I saw that cp fails to overwrite a mode 444 file, including perhaps one left by a previous interrupted cp. This commit was sponsored by Fernando Jimenez on Patreon. --- Utility/CopyFile.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Utility/CopyFile.hs b/Utility/CopyFile.hs index 5555faf1e8..971fb5b07c 100644 --- a/Utility/CopyFile.hs +++ b/Utility/CopyFile.hs @@ -38,8 +38,9 @@ copyMetaDataParams meta = map snd $ filter fst - and because this allows easy access to features like cp --reflink. -} copyFileExternal :: CopyMetaData -> FilePath -> FilePath -> IO Bool copyFileExternal meta src dest = do - whenM (doesFileExist dest) $ - removeFile dest + -- Delete any existing dest file because an unwritable file + -- would prevent cp from working. + void $ tryIO $ removeFile dest boolSystem "cp" $ params ++ [File src, File dest] where params @@ -52,8 +53,7 @@ copyFileExternal meta src dest = do copyCoW :: CopyMetaData -> FilePath -> FilePath -> IO Bool copyCoW meta src dest | BuildInfo.cp_reflink_supported = do - whenM (doesFileExist dest) $ - removeFile dest + void $ tryIO $ removeFile dest -- When CoW is not supported, cp will complain to stderr, -- so have to discard its stderr. ok <- catchBoolIO $ do