module System.LibFuse3.Internal.Resource where
import Control.Exception (catch, mask, throwIO)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Resource (ReleaseKey, ResourceT, allocate, getInternalState, runInternalState)
import Control.Monad.Trans.Resource.Internal (stateCleanupChecked)
import Foreign (Ptr, Storable, callocBytes, free, mallocBytes, new, newArray)
import Foreign.C (CString, newCString)
import System.Exit (ExitCode(ExitSuccess))
import System.Posix.Internals (newFilePath)
import System.Posix.Process (exitImmediately, forkProcess)
daemonizeResourceT :: ResourceT IO a -> ResourceT IO b
daemonizeResourceT :: forall a b. ResourceT IO a -> ResourceT IO b
daemonizeResourceT ResourceT IO a
res = do
InternalState
istate <- forall (m :: * -> *). Monad m => ResourceT m InternalState
getInternalState
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
ProcessID
_ <- IO () -> IO ProcessID
forkProcess forall a b. (a -> b) -> a -> b
$ forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
a
_ <- forall a. IO a -> IO a
restore (forall (m :: * -> *) a. ResourceT m a -> InternalState -> m a
runInternalState ResourceT IO a
res InternalState
istate) forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \SomeException
e -> do
Maybe SomeException -> InternalState -> IO ()
stateCleanupChecked (forall a. a -> Maybe a
Just SomeException
e) InternalState
istate
forall e a. Exception e => e -> IO a
throwIO SomeException
e
Maybe SomeException -> InternalState -> IO ()
stateCleanupChecked forall a. Maybe a
Nothing InternalState
istate
()
_ <- ExitCode -> IO ()
exitImmediately ExitCode
ExitSuccess
forall a. HasCallStack => a
undefined
resCallocBytes :: Int -> ResourceT IO (ReleaseKey, Ptr a)
resCallocBytes :: forall a. Int -> ResourceT IO (ReleaseKey, Ptr a)
resCallocBytes Int
n = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (forall a. Int -> IO (Ptr a)
callocBytes Int
n) forall a. Ptr a -> IO ()
free
resMallocBytes :: Int -> ResourceT IO (ReleaseKey, Ptr a)
resMallocBytes :: forall a. Int -> ResourceT IO (ReleaseKey, Ptr a)
resMallocBytes Int
n = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (forall a. Int -> IO (Ptr a)
mallocBytes Int
n) forall a. Ptr a -> IO ()
free
resNew :: Storable a => a -> ResourceT IO (ReleaseKey, Ptr a)
resNew :: forall a. Storable a => a -> ResourceT IO (ReleaseKey, Ptr a)
resNew a
a = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (forall a. Storable a => a -> IO (Ptr a)
new a
a) forall a. Ptr a -> IO ()
free
resNewCString :: String -> ResourceT IO (ReleaseKey, CString)
resNewCString :: String -> ResourceT IO (ReleaseKey, CString)
resNewCString String
s = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (String -> IO CString
newCString String
s) forall a. Ptr a -> IO ()
free
resNewFilePath :: FilePath -> ResourceT IO (ReleaseKey, CString)
resNewFilePath :: String -> ResourceT IO (ReleaseKey, CString)
resNewFilePath String
path = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (String -> IO CString
newFilePath String
path) forall a. Ptr a -> IO ()
free
resNewArray :: Storable a => [a] -> ResourceT IO (ReleaseKey, Ptr a)
resNewArray :: forall a. Storable a => [a] -> ResourceT IO (ReleaseKey, Ptr a)
resNewArray [a]
as = forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate (forall a. Storable a => [a] -> IO (Ptr a)
newArray [a]
as) forall a. Ptr a -> IO ()
free