{-# LANGUAGE CPP, ForeignFunctionInterface #-}
module Darcs.Util.Compat
( stdoutIsAPipe
, maybeRelink
, atomicCreate
, sloppyAtomicCreate
) where
#ifdef WIN32
#define USE_CREAT
#else
#if MIN_VERSION_unix(2,8,0)
#define USE_CREAT
#endif
#endif
import Darcs.Prelude
import Control.Monad ( unless )
import Foreign.C.Types ( CInt(..) )
import Foreign.C.String ( CString, withCString )
import Foreign.C.Error ( throwErrno, eEXIST, getErrno )
import System.Directory ( getCurrentDirectory )
import System.IO.Error ( mkIOError, alreadyExistsErrorType )
import System.Posix.Files ( stdFileMode )
import System.Posix.IO ( openFd, closeFd,
#ifdef USE_CREAT
creat,
#endif
defaultFileFlags, exclusive,
OpenMode(WriteOnly) )
import Darcs.Util.SignalHandler ( stdoutIsAPipe )
foreign import ccall unsafe "maybe_relink.h maybe_relink" maybe_relink
:: CString -> CString -> CInt -> IO CInt
maybeRelink :: String -> String -> IO Bool
maybeRelink :: String -> String -> IO Bool
maybeRelink String
src String
dst =
String -> (CString -> IO Bool) -> IO Bool
forall a. String -> (CString -> IO a) -> IO a
withCString String
src ((CString -> IO Bool) -> IO Bool)
-> (CString -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \CString
csrc ->
String -> (CString -> IO Bool) -> IO Bool
forall a. String -> (CString -> IO a) -> IO a
withCString String
dst ((CString -> IO Bool) -> IO Bool)
-> (CString -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \CString
cdst ->
do rc <- CString -> CString -> CInt -> IO CInt
maybe_relink CString
csrc CString
cdst CInt
1
case rc of
CInt
0 -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
CInt
1 -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
-1 -> String -> IO Bool
forall a. String -> IO a
throwErrno (String
"Relinking " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
dst)
-2 -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
-3 -> do String -> IO ()
putStrLn (String
"Relinking: race condition avoided on file " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
dst)
Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
CInt
_ -> String -> IO Bool
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Unexpected situation when relinking " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
dst)
sloppyAtomicCreate :: FilePath -> IO ()
sloppyAtomicCreate :: String -> IO ()
sloppyAtomicCreate String
fp
#ifdef USE_CREAT
= do fd <- String -> OpenMode -> OpenFileFlags -> IO Fd
openFd String
fp OpenMode
WriteOnly OpenFileFlags
flags {creat = Just stdFileMode}
#else
= do fd <- openFd fp WriteOnly (Just stdFileMode) flags
#endif
closeFd fd
where flags :: OpenFileFlags
flags = OpenFileFlags
defaultFileFlags { exclusive = True }
atomicCreate :: FilePath -> IO ()
atomicCreate :: String -> IO ()
atomicCreate String
fp = String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
fp ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
cstr -> do
rc <- CString -> IO CInt
c_atomic_create CString
cstr
unless (rc >= 0) $
do errno <- getErrno
pwd <- getCurrentDirectory
if errno == eEXIST
then ioError $ mkIOError alreadyExistsErrorType
("atomicCreate in "++pwd)
Nothing (Just fp)
else throwErrno $ "atomicCreate "++fp++" in "++pwd
foreign import ccall unsafe "atomic_create.h atomic_create" c_atomic_create
:: CString -> IO CInt