module Database.LSMTree.Internal.FS (
hardLink
, hardLinkDirectoryRec
, copyFile'
, copyFile
, copyDirectoryRec
, copyDirectoryRec'
, 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)
{-# SPECIALISE
hardLink ::
HasCallStack
=> ActionRegistry IO
-> HasFS IO h
-> HasBlockIO IO h'
-> FsPath
-> FsPath
-> IO ()
#-}
hardLink ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> HasBlockIO m h'
-> FsPath
-> 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 ()
#-}
hardLinkDirectoryRec ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> HasBlockIO m h'
-> FsPath
-> 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')
{-# SPECIALISE
copyFile' ::
HasCallStack
=> ActionRegistry IO
-> HasFS IO h
-> FsPath
-> FsPath
-> IO ()
#-}
copyFile' ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> FS.FsPath
-> 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 ()
#-}
copyDirectoryRec' ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> FsPath
-> 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 ()
#-}
copyFile ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> FsPath
-> HasFS m h'
-> 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 ->
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)
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 ()
#-}
copyDirectoryRec ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> FsPath
-> HasFS m h'
-> 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')
{-# SPECIALISE
hardLinkOrCopyDirectoryRec ::
HasCallStack
=> ActionRegistry IO
-> HasFS IO h
-> HasBlockIO IO h'
-> FsPath
-> FsPath
-> IO ()
#-}
hardLinkOrCopyDirectoryRec ::
(HasCallStack, MonadMask m, PrimMonad m)
=> ActionRegistry m
-> HasFS m h
-> HasBlockIO m h'
-> FsPath
-> 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
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