-- |
-- Module      :  System.AtomicWrite.Writer.ByteString.Binary
-- Copyright   :  © 2015-2019 Stack Builders Inc.
-- License     :  MIT
--
-- Maintainer  :  Stack Builders <hackage@stackbuilders.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- Provides functionality to dump the contents of a ByteString
-- to a file open in binary mode

module System.AtomicWrite.Writer.ByteString.Binary (atomicWriteFile, atomicWriteFileWithMode) where

import           System.AtomicWrite.Internal (atomicWriteFileMaybeModeBinary)

import           System.Posix.Types          (FileMode)

import           Data.ByteString             (ByteString, hPutStr)


-- | Creates or modifies a file atomically on POSIX-compliant
-- systems while preserving permissions. The file is opened in
-- binary mode.
atomicWriteFile ::
  FilePath      -- ^ The path where the file will be updated or created
  -> ByteString -- ^ The content to write to the file
  -> IO ()
atomicWriteFile :: FilePath -> ByteString -> IO ()
atomicWriteFile = Maybe FileMode -> FilePath -> ByteString -> IO ()
atomicWriteFileMaybeMode Maybe FileMode
forall a. Maybe a
Nothing

-- | Creates or modifies a file atomically on POSIX-compliant
-- systems and updates permissions. The file is opened in binary
-- mode.
atomicWriteFileWithMode ::
  FileMode
  -> FilePath      -- ^ The path where the file will be updated or created
  -> ByteString -- ^ The content to write to the file
  -> IO ()
atomicWriteFileWithMode :: FileMode -> FilePath -> ByteString -> IO ()
atomicWriteFileWithMode FileMode
mode =
  Maybe FileMode -> FilePath -> ByteString -> IO ()
atomicWriteFileMaybeMode (Maybe FileMode -> FilePath -> ByteString -> IO ())
-> Maybe FileMode -> FilePath -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ FileMode -> Maybe FileMode
forall a. a -> Maybe a
Just FileMode
mode

-- | Helper function for opening the file in binary mode.
atomicWriteFileMaybeMode ::
  Maybe FileMode
  -> FilePath      -- ^ The path where the file will be updated or created
  -> ByteString -- ^ The content to write to the file
  -> IO ()
atomicWriteFileMaybeMode :: Maybe FileMode -> FilePath -> ByteString -> IO ()
atomicWriteFileMaybeMode Maybe FileMode
mmode FilePath
path = Maybe FileMode
-> FilePath
-> (Handle -> ByteString -> IO ())
-> ByteString
-> IO ()
forall a.
Maybe FileMode -> FilePath -> (Handle -> a -> IO ()) -> a -> IO ()
atomicWriteFileMaybeModeBinary Maybe FileMode
mmode FilePath
path Handle -> ByteString -> IO ()
hPutStr