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.
This commit is contained in:
Joey Hess 2019-07-17 14:22:21 -04:00
parent 21ff5e1e5a
commit 7234b1f9a7
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -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