2020-11-05 22:45:37 +00:00
|
|
|
{- moving files
|
|
|
|
-
|
|
|
|
- Copyright 2011-2020 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
|
|
|
module Utility.MoveFile (
|
|
|
|
moveFile,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad
|
|
|
|
import System.IO.Error
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
#ifndef mingw32_HOST_OS
|
2023-03-21 22:22:41 +00:00
|
|
|
import System.PosixCompat.Files (isDirectory)
|
2020-11-23 18:00:17 +00:00
|
|
|
import Control.Monad.IfElse
|
2020-11-05 22:45:37 +00:00
|
|
|
import Utility.SafeCommand
|
|
|
|
#endif
|
|
|
|
|
|
|
|
import Utility.SystemDirectory
|
|
|
|
import Utility.Tmp
|
|
|
|
import Utility.Exception
|
|
|
|
import Utility.Monad
|
2022-06-22 20:47:34 +00:00
|
|
|
import Utility.FileSystemEncoding
|
|
|
|
import qualified Utility.RawFilePath as R
|
2023-11-29 18:38:50 +00:00
|
|
|
import Author
|
|
|
|
|
|
|
|
copyright :: Copyright
|
|
|
|
copyright = author JoeyHess (2022-11)
|
2020-11-05 22:45:37 +00:00
|
|
|
|
|
|
|
{- Moves one filename to another.
|
|
|
|
- First tries a rename, but falls back to moving across devices if needed. -}
|
2022-06-22 20:47:34 +00:00
|
|
|
moveFile :: RawFilePath -> RawFilePath -> IO ()
|
|
|
|
moveFile src dest = tryIO (R.rename src dest) >>= onrename
|
2020-11-05 22:45:37 +00:00
|
|
|
where
|
|
|
|
onrename (Right _) = noop
|
|
|
|
onrename (Left e)
|
|
|
|
| isPermissionError e = rethrow
|
|
|
|
| isDoesNotExistError e = rethrow
|
2022-06-22 20:47:34 +00:00
|
|
|
| otherwise = viaTmp mv (fromRawFilePath dest) ()
|
2020-11-05 22:45:37 +00:00
|
|
|
where
|
|
|
|
rethrow = throwM e
|
|
|
|
|
|
|
|
mv tmp () = do
|
|
|
|
-- copyFile is likely not as optimised as
|
|
|
|
-- the mv command, so we'll use the command.
|
|
|
|
--
|
2022-06-22 20:47:34 +00:00
|
|
|
-- But, while Windows has a "mv", it does not
|
|
|
|
-- seem very reliable, so use copyFile there.
|
2020-11-05 22:45:37 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
-- If dest is a directory, mv would move the file
|
|
|
|
-- into it, which is not desired.
|
|
|
|
whenM (isdir dest) rethrow
|
2023-11-29 18:38:50 +00:00
|
|
|
ok <- copyright =<< boolSystem "mv"
|
2022-06-22 20:47:34 +00:00
|
|
|
[ Param "-f"
|
|
|
|
, Param (fromRawFilePath src)
|
|
|
|
, Param tmp
|
|
|
|
]
|
2020-11-05 22:45:37 +00:00
|
|
|
let e' = e
|
|
|
|
#else
|
2022-06-22 20:47:34 +00:00
|
|
|
r <- tryIO $ copyFile (fromRawFilePath src) tmp
|
2020-11-05 22:45:37 +00:00
|
|
|
let (ok, e') = case r of
|
|
|
|
Left err -> (False, err)
|
|
|
|
Right _ -> (True, e)
|
|
|
|
#endif
|
|
|
|
unless ok $ do
|
|
|
|
-- delete any partial
|
|
|
|
_ <- tryIO $ removeFile tmp
|
|
|
|
throwM e'
|
|
|
|
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
isdir f = do
|
2022-09-05 17:44:03 +00:00
|
|
|
r <- tryIO $ R.getSymbolicLinkStatus f
|
2020-11-05 22:45:37 +00:00
|
|
|
case r of
|
|
|
|
(Left _) -> return False
|
|
|
|
(Right s) -> return $ isDirectory s
|
|
|
|
#endif
|