LIB/TESTS: fix moveFileOverwrite and add tests

We must not allow to move a file to a directory, deleting that
directory and effectively changing the filetype.
This commit is contained in:
2016-05-08 23:20:00 +02:00
parent 8646a6338c
commit 5b6a342a9e
10 changed files with 112 additions and 3 deletions

View File

@@ -676,7 +676,7 @@ moveFile from to = do
-- Safety/reliability concerns:
--
-- * copy-delete fallback is inherently non-atomic
-- * checks for destination file existence explicitly
-- * checks for file types and destination file existence explicitly
--
-- Throws:
--
@@ -691,9 +691,20 @@ moveFileOverwrite :: Path Abs -- ^ file to move
-> IO ()
moveFileOverwrite from to = do
throwSameFile from to
exists <- (||) <$> doesFileExist to <*> doesDirectoryExist to
ft <- getFileType from
writable <- isWritable $ P.dirname to
when (exists && writable) (easyDelete to)
case ft of
RegularFile -> do
exists <- doesFileExist to
when (exists && writable) (deleteFile to)
SymbolicLink -> do
exists <- doesFileExist to
when (exists && writable) (deleteFile to)
Directory -> do
exists <- doesDirectoryExist to
when (exists && writable) (deleteDir to)
_ -> ioError $ userError $ "Don't know how to handle filetype " ++
show ft
moveFile from to