-- | Common file system operations
module Database.LSMTree.Internal.FS (
    -- * Hard links
    hardLink
  , hardLinkDirectoryRec
    -- * Copy files and directories
  , copyFile'
  , copyFile
  , copyDirectoryRec
  , copyDirectoryRec'
    -- * Hard links with copy fallback
  , hardLinkOrCopyDirectoryRec
  ) where

import           Control.ActionRegistry
import           Control.Monad (forM_, void)
import           Control.Monad.Class.MonadThrow
import           Control.Monad.Primitive (PrimMonad)

import           Foreign.C.Error (eXDEV)
import           GHC.Stack (HasCallStack)
import qualified System.FS.API as FS
import           System.FS.API (FsError, FsPath, HasFS)
import qualified System.FS.API.Lazy as FSL
import qualified System.FS.BlockIO.API as FS
import           System.FS.BlockIO.API (HasBlockIO)
import           Text.Printf (printf)

{-------------------------------------------------------------------------------
  Hard links
-------------------------------------------------------------------------------}

{-# SPECIALISE
  hardLink ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> HasBlockIO IO h'
    -> FsPath
    -> FsPath
    -> IO ()
  #-}
-- | Create a hard link for a source file at a destination file /on the same
-- file system/.
--
-- If the source path and destination path are not on the same disk volume, an
-- 'FsError' with an @EXDEV@ error number will be thrown.
hardLink ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- | The file system
  -> HasFS m h
     -- | The file system
  -> HasBlockIO m h'
     -- | The path to the source file
  -> FsPath
     -- | The path to the destination file
  -> FsPath
  -> m ()
hardLink :: forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLink ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath FsPath
dstPath = do
    ActionRegistry m -> m () -> m () -> m ()
forall (m :: * -> *) a.
(PrimMonad m, MonadMask m, HasCallStack) =>
ActionRegistry m -> m a -> m () -> m a
withRollback_ ActionRegistry m
reg
      (HasBlockIO m h' -> FsPath -> FsPath -> m ()
forall (m :: * -> *) h. HasBlockIO m h -> FsPath -> FsPath -> m ()
FS.createHardLink HasBlockIO m h'
hbio FsPath
srcPath FsPath
dstPath)
      (HasFS m h -> HasCallStack => FsPath -> m ()
forall (m :: * -> *) h. HasFS m h -> HasCallStack => FsPath -> m ()
FS.removeFile HasFS m h
hfs FsPath
dstPath)

{-# SPECIALISE
  hardLinkDirectoryRec ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> HasBlockIO IO h'
    -> FsPath
    -> FsPath
    -> IO ()
  #-}
-- | Recursively create hard links for a source directory and its contents at a
-- destination directory /on the same file system/.
--
-- If the source path and destination path (or recursively their directory
-- contents) are not on the same disk volume, an 'FsError' with an @EXDEV@ error
-- number will be thrown.
hardLinkDirectoryRec ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- | The file system
  -> HasFS m h
     -- | The file system
  -> HasBlockIO m h'
     -- | The path to the source directory
  -> FsPath
     -- | The path to the destination directory
  -> FsPath
  -> m ()
hardLinkDirectoryRec :: forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLinkDirectoryRec ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath FsPath
dstPath = do
    Set String
entries <- HasFS m h -> HasCallStack => FsPath -> m (Set String)
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m (Set String)
FS.listDirectory HasFS m h
hfs FsPath
srcPath
    Set String -> (String -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Set String
entries ((String -> m ()) -> m ()) -> (String -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \String
entry -> do
      let srcPath' :: FsPath
srcPath' = FsPath
srcPath FsPath -> FsPath -> FsPath
FS.</> [String] -> FsPath
FS.mkFsPath [String
entry]
          dstPath' :: FsPath
dstPath' = FsPath
dstPath FsPath -> FsPath -> FsPath
FS.</> [String] -> FsPath
FS.mkFsPath [String
entry]
      Bool
isFile <- HasFS m h -> HasCallStack => FsPath -> m Bool
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m Bool
FS.doesFileExist HasFS m h
hfs FsPath
srcPath'
      if Bool
isFile then ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLink ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath' FsPath
dstPath'
      else do
        Bool
isDirectory <- HasFS m h -> HasCallStack => FsPath -> m Bool
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m Bool
FS.doesDirectoryExist HasFS m h
hfs FsPath
srcPath'
        if Bool
isDirectory then ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLinkDirectoryRec ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath' FsPath
dstPath'
        else String -> m ()
forall a. HasCallStack => String -> a
error (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String -> String -> String
forall r. PrintfType r => String -> r
printf
                String
"hardLinkDirectoryRec: %s is not a file or directory"
                (FsPath -> String
forall a. Show a => a -> String
show FsPath
srcPath')

{-------------------------------------------------------------------------------
  Copy files and directories
-------------------------------------------------------------------------------}

{-# SPECIALISE
  copyFile' ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> FsPath
    -> FsPath
    -> IO ()
  #-}
-- | Copy a source file to a destination file /on the same file system/.
copyFile' ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- | The file system
  -> HasFS m h
     -- | The path to the source file
  -> FS.FsPath
     -- | The path to the destination file
  -> FS.FsPath
  -> m ()
copyFile' :: forall (m :: * -> *) h.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m -> HasFS m h -> FsPath -> FsPath -> m ()
copyFile' ActionRegistry m
reg HasFS m h
hfs FsPath
srcPath FsPath
dstPath = ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyFile ActionRegistry m
reg HasFS m h
hfs FsPath
srcPath HasFS m h
hfs FsPath
dstPath

{-# SPECIALISE
  copyDirectoryRec' ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> FsPath
    -> FsPath
    -> IO ()
  #-}
-- | Recursively copy a source directory and its contents to a destination
-- directory /on the same file system/.
copyDirectoryRec' ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- The file system
  -> HasFS m h
     -- | The path to the source directory
  -> FsPath
     -- | The path to the destination directory
  -> FsPath
  -> m ()
copyDirectoryRec' :: forall (m :: * -> *) h.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m -> HasFS m h -> FsPath -> FsPath -> m ()
copyDirectoryRec' ActionRegistry m
reg HasFS m h
hfs FsPath
srcPath FsPath
dstPath = ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyDirectoryRec ActionRegistry m
reg HasFS m h
hfs FsPath
srcPath HasFS m h
hfs FsPath
dstPath

{-# SPECIALISE
  copyFile ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> FsPath
    -> HasFS IO h'
    -> FsPath
    -> IO ()
  #-}
-- | Copy a source file to a destination file /on potentially different file
-- systems/.
copyFile ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- | The source file system
  -> HasFS m h
     -- | The path to the source file with respect to the source fileystem
  -> FsPath
     -- | The destination file system
  -> HasFS m h'
     -- | The path to the destination file with respect to the destination
     -- fileystem
  -> FsPath
  -> m ()
copyFile :: forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyFile ActionRegistry m
reg HasFS m h
srcFS FsPath
srcPath HasFS m h'
dstFS FsPath
dstPath =
    HasFS m h -> FsPath -> OpenMode -> (Handle h -> m ()) -> m ()
forall (m :: * -> *) h a.
(HasCallStack, MonadThrow m) =>
HasFS m h -> FsPath -> OpenMode -> (Handle h -> m a) -> m a
FS.withFile HasFS m h
srcFS FsPath
srcPath OpenMode
FS.ReadMode ((Handle h -> m ()) -> m ()) -> (Handle h -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Handle h
srcH ->
    -- We use the action registry to open the destination file and to track that
    -- we should remove that file even if an exception occurs after we've exited
    -- copyFile.
    --
    -- The outer bracket is used to ensure that we close the file handle after
    -- we've written to it.
    m (Handle h') -> (Handle h' -> m ()) -> (Handle h' -> m ()) -> m ()
forall a b c. m a -> (a -> m b) -> (a -> m c) -> m c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (ActionRegistry m
-> m (Handle h') -> (Handle h' -> m ()) -> m (Handle h')
forall (m :: * -> *) a.
(PrimMonad m, MonadMask m, HasCallStack) =>
ActionRegistry m -> m a -> (a -> m ()) -> m a
withRollback ActionRegistry m
reg m (Handle h')
acq Handle h' -> m ()
rel) (HasFS m h' -> HasCallStack => Handle h' -> m ()
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => Handle h -> m ()
FS.hClose HasFS m h'
dstFS) ((Handle h' -> m ()) -> m ()) -> (Handle h' -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Handle h'
dstH -> do
      ByteString
bs <- HasFS m h -> Handle h -> m ByteString
forall (m :: * -> *) h.
Monad m =>
HasFS m h -> Handle h -> m ByteString
FSL.hGetAll HasFS m h
srcFS Handle h
srcH
      m Word64 -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m Word64 -> m ()) -> m Word64 -> m ()
forall a b. (a -> b) -> a -> b
$ HasFS m h' -> Handle h' -> ByteString -> m Word64
forall (m :: * -> *) h.
(HasCallStack, Monad m) =>
HasFS m h -> Handle h -> ByteString -> m Word64
FSL.hPutAll HasFS m h'
dstFS Handle h'
dstH ByteString
bs
  where
    acq :: m (Handle h')
acq      = HasFS m h' -> HasCallStack => FsPath -> OpenMode -> m (Handle h')
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> OpenMode -> m (Handle h)
FS.hOpen HasFS m h'
dstFS FsPath
dstPath (AllowExisting -> OpenMode
FS.WriteMode AllowExisting
FS.MustBeNew)
    -- hClose is idempotent, so it does not hurt to try closing before removing
    -- even if we are pretty sure we'll close the file handle as part of the
    -- @bracket@ above.
    rel :: Handle h' -> m ()
rel Handle h'
dstH = HasFS m h' -> HasCallStack => Handle h' -> m ()
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => Handle h -> m ()
FS.hClose HasFS m h'
dstFS Handle h'
dstH m () -> m () -> m ()
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> HasFS m h' -> HasCallStack => FsPath -> m ()
forall (m :: * -> *) h. HasFS m h -> HasCallStack => FsPath -> m ()
FS.removeFile HasFS m h'
dstFS FsPath
dstPath

{-# SPECIALISE
  copyDirectoryRec ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> FsPath
    -> HasFS IO h'
    -> FsPath
    -> IO ()
  #-}
-- | Recursively copy a source directory and its contents to a destination
-- directory /on potentially different file systems/
copyDirectoryRec ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- The source file system
  -> HasFS m h
     -- | The path to the source directory with respect to the source fileystem
  -> FsPath
     -- | The destination file system
  -> HasFS m h'
     -- | The path to the destination directory with respect to the destination
     -- fileystem
  -> FsPath
  -> m ()
copyDirectoryRec :: forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyDirectoryRec ActionRegistry m
reg HasFS m h
srcFS FsPath
srcPath HasFS m h'
dstFS FsPath
dstPath = do
    Set String
entries <- HasFS m h -> HasCallStack => FsPath -> m (Set String)
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m (Set String)
FS.listDirectory HasFS m h
srcFS FsPath
srcPath
    Set String -> (String -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Set String
entries ((String -> m ()) -> m ()) -> (String -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \String
entry -> do
      let srcPath' :: FsPath
srcPath' = FsPath
srcPath FsPath -> FsPath -> FsPath
FS.</> [String] -> FsPath
FS.mkFsPath [String
entry]
          dstPath' :: FsPath
dstPath' = FsPath
dstPath FsPath -> FsPath -> FsPath
FS.</> [String] -> FsPath
FS.mkFsPath [String
entry]
      Bool
isFile <- HasFS m h -> HasCallStack => FsPath -> m Bool
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m Bool
FS.doesFileExist HasFS m h
srcFS FsPath
srcPath'
      if Bool
isFile then ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyFile ActionRegistry m
reg HasFS m h
srcFS FsPath
srcPath' HasFS m h'
dstFS FsPath
dstPath'
      else do
        Bool
isDirectory <- HasFS m h -> HasCallStack => FsPath -> m Bool
forall (m :: * -> *) h.
HasFS m h -> HasCallStack => FsPath -> m Bool
FS.doesDirectoryExist HasFS m h
srcFS FsPath
srcPath'
        if Bool
isDirectory then ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> FsPath -> HasFS m h' -> FsPath -> m ()
copyDirectoryRec ActionRegistry m
reg HasFS m h
srcFS FsPath
srcPath' HasFS m h'
dstFS FsPath
dstPath'
        else String -> m ()
forall a. HasCallStack => String -> a
error (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String -> String -> String
forall r. PrintfType r => String -> r
printf
                String
"copyDirectoryRec: %s is not a file or directory"
                (FsPath -> String
forall a. Show a => a -> String
show FsPath
srcPath')

{-------------------------------------------------------------------------------
  Hard links with copy fallback
-------------------------------------------------------------------------------}

{-# SPECIALISE
  hardLinkOrCopyDirectoryRec ::
       HasCallStack
    => ActionRegistry IO
    -> HasFS IO h
    -> HasBlockIO IO h'
    -> FsPath
    -> FsPath
    -> IO ()
  #-}
-- | Attempt to use 'hardLinkDirectoryRec', and if that fails because the source
-- and destination directory are not on the same disk volume, use
-- 'copyDirectoryRec'' instead.
hardLinkOrCopyDirectoryRec ::
     (HasCallStack, MonadMask m, PrimMonad m)
  => ActionRegistry m
     -- | The file system
  -> HasFS m h
     -- | The file system
  -> HasBlockIO m h'
     -- | The path to the source directory
  -> FsPath
     -- | The path to the destination directory
  -> FsPath
  -> m ()
hardLinkOrCopyDirectoryRec :: forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLinkOrCopyDirectoryRec ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath FsPath
dstPath =
    (FsError -> Maybe FsError) -> m () -> (FsError -> m ()) -> m ()
forall e b a.
Exception e =>
(e -> Maybe b) -> m a -> (b -> m a) -> m a
forall (m :: * -> *) e b a.
(MonadCatch m, Exception e) =>
(e -> Maybe b) -> m a -> (b -> m a) -> m a
catchJust FsError -> Maybe FsError
ifEXDEV m ()
doHardLink (m () -> FsError -> m ()
forall a b. a -> b -> a
const m ()
doFallBack)
  where
    doHardLink :: m ()
doHardLink = ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
forall (m :: * -> *) h h'.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m
-> HasFS m h -> HasBlockIO m h' -> FsPath -> FsPath -> m ()
hardLinkDirectoryRec ActionRegistry m
reg HasFS m h
hfs HasBlockIO m h'
hbio FsPath
srcPath FsPath
dstPath
    doFallBack :: m ()
doFallBack = ActionRegistry m -> HasFS m h -> FsPath -> FsPath -> m ()
forall (m :: * -> *) h.
(HasCallStack, MonadMask m, PrimMonad m) =>
ActionRegistry m -> HasFS m h -> FsPath -> FsPath -> m ()
copyFile' ActionRegistry m
reg HasFS m h
hfs FsPath
srcPath FsPath
dstPath

    -- NOTE: On Windows, the error code is ERROR_NOT_SAME_DEVICE (17),
    --       but the Win32 primitive for creating hard links maps this
    --       to the POSIX error code EXDEV using the maperrno builtin.
    isEXDEV :: FsError -> Bool
    isEXDEV :: FsError -> Bool
isEXDEV FsError
e = FsError -> Maybe Errno
FS.fsErrorNo FsError
e Maybe Errno -> Maybe Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno -> Maybe Errno
forall a. a -> Maybe a
Just Errno
eXDEV
    ifEXDEV :: FsError -> Maybe FsError
ifEXDEV FsError
e = if FsError -> Bool
isEXDEV FsError
e then FsError -> Maybe FsError
forall a. a -> Maybe a
Just FsError
e else Maybe FsError
forall a. Maybe a
Nothing