{-# LINE 1 "libraries/unix/System/Posix/Fcntl.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Fcntl
-- Copyright   :  (c) The University of Glasgow 2014
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  non-portable (requires POSIX)
--
-- POSIX file control support
--
-- @since 2.7.1.0
-----------------------------------------------------------------------------




module System.Posix.Fcntl (
    -- * File allocation
    Advice(..), fileAdvise,
    fileAllocate,
    -- * File caching
    fileGetCaching,
    fileSetCaching,
  ) where

import Foreign.C
import System.Posix.Types


{-# LINE 34 "libraries/unix/System/Posix/Fcntl.hsc" #-}
import System.IO.Error ( ioeSetLocation )
import GHC.IO.Exception ( unsupportedOperation )

{-# LINE 37 "libraries/unix/System/Posix/Fcntl.hsc" #-}


{-# LINE 42 "libraries/unix/System/Posix/Fcntl.hsc" #-}


{-# LINE 44 "libraries/unix/System/Posix/Fcntl.hsc" #-}
import System.Posix.Internals (c_fcntl_write)

{-# LINE 46 "libraries/unix/System/Posix/Fcntl.hsc" #-}

-- -----------------------------------------------------------------------------
-- File control

-- | Advice parameter for 'fileAdvise' operation.
--
-- For more details, see documentation of @posix_fadvise(2)@.
--
-- @since 2.7.1.0
data Advice
  = AdviceNormal
  | AdviceRandom
  | AdviceSequential
  | AdviceWillNeed
  | AdviceDontNeed
  | AdviceNoReuse
  deriving Advice -> Advice -> Bool
(Advice -> Advice -> Bool)
-> (Advice -> Advice -> Bool) -> Eq Advice
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Advice -> Advice -> Bool
== :: Advice -> Advice -> Bool
$c/= :: Advice -> Advice -> Bool
/= :: Advice -> Advice -> Bool
Eq

-- | Performs @posix_fadvise(2)@ operation on file-descriptor.
--
-- If platform does not provide @posix_fadvise(2)@ 'fileAdvise'
-- becomes a no-op.
--
-- (use @#if HAVE_POSIX_FADVISE@ CPP guard to detect availability)
--
-- @since 2.7.1.0
fileAdvise :: Fd -> FileOffset -> FileOffset -> Advice -> IO ()

{-# LINE 88 "libraries/unix/System/Posix/Fcntl.hsc" #-}
fileAdvise :: Fd -> FileOffset -> FileOffset -> Advice -> IO ()
fileAdvise Fd
_ FileOffset
_ FileOffset
_ Advice
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

{-# LINE 90 "libraries/unix/System/Posix/Fcntl.hsc" #-}

-- | Performs @posix_fallocate(2)@ operation on file-descriptor.
--
-- Throws 'IOError' (\"unsupported operation\") if platform does not
-- provide @posix_fallocate(2)@.
--
-- (use @#if HAVE_POSIX_FALLOCATE@ CPP guard to detect availability).
--
-- @since 2.7.1.0
fileAllocate :: Fd -> FileOffset -> FileOffset -> IO ()

{-# LINE 110 "libraries/unix/System/Posix/Fcntl.hsc" #-}
{-# WARNING fileAllocate
    "operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_POSIX_FALLOCATE@)" #-}
fileAllocate :: Fd -> FileOffset -> FileOffset -> IO ()
fileAllocate Fd
_ FileOffset
_ FileOffset
_ = IOError -> IO ()
forall a. IOError -> IO a
ioError (IOError -> String -> IOError
ioeSetLocation IOError
unsupportedOperation
                              String
"fileAllocate")

{-# LINE 115 "libraries/unix/System/Posix/Fcntl.hsc" #-}

-- -----------------------------------------------------------------------------
-- File caching

-- | Performs the @fcntl(2)@ operation on a file-desciptor to get the cache mode.
--
-- If the cache mode is 'False', then cache effects for file system reads and
-- writes are minimised or otherwise eliminated. If the cache mode is 'True',
-- then cache effects occur like normal.
--
-- On Linux, FreeBSD, and NetBSD this checks whether the @O_DIRECT@ file flag is
-- set.
--
-- Throws 'IOError' (\"unsupported operation\") if platform does not support
-- getting the cache mode.
--
-- Use @#if HAVE_O_DIRECT@ CPP guard to detect availability. Use @#include
-- "HsUnix.h"@ to bring @HAVE_O_DIRECT@ into scope.
--
-- @since 2.8.7.0
fileGetCaching :: Fd -> IO Bool

{-# LINE 143 "libraries/unix/System/Posix/Fcntl.hsc" #-}
{-# WARNING fileGetCaching
     "operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_O_DIRECT@)" #-}
fileGetCaching :: Fd -> IO Bool
fileGetCaching Fd
_ = IOError -> IO Bool
forall a. IOError -> IO a
ioError (IOError -> String -> IOError
ioeSetLocation IOError
unsupportedOperation String
"fileGetCaching")

{-# LINE 147 "libraries/unix/System/Posix/Fcntl.hsc" #-}

-- | Performs the @fcntl(2)@ operation on a file-desciptor to set the cache
-- mode.
--
-- If the cache mode is 'False', then cache effects for file system reads and
-- writes are minimised or otherwise eliminated. If the cache mode is 'True',
-- then cache effects occur like normal.
--
-- On Linux, FreeBSD, and NetBSD this sets the @O_DIRECT@ file flag. On OSX,
-- this sets the @F_NOCACHE@ @fcntl@ flag.
--
-- Throws 'IOError' (\"unsupported operation\") if platform does not support
-- setting the cache mode.
--
-- Use @#if HAVE_O_DIRECT || HAVE_F_NOCACHE@ CPP guard to detect availability.
-- Use @#include "HsUnix.h"@ to bring @HAVE_O_DIRECT@ and @HAVE_F_NOCACHE@ into
-- scope.
--
-- @since 2.8.7.0
fileSetCaching :: Fd -> Bool -> IO ()

{-# LINE 176 "libraries/unix/System/Posix/Fcntl.hsc" #-}
fileSetCaching :: Fd -> Bool -> IO ()
fileSetCaching (Fd CInt
fd) Bool
val = do
    String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"fileSetCaching" (CInt -> CInt -> CLong -> IO CInt
c_fcntl_write CInt
fd CInt
48 (if Bool
val then CLong
0 else CLong
1))
{-# LINE 178 "libraries/unix/System/Posix/Fcntl.hsc" #-}

{-# LINE 183 "libraries/unix/System/Posix/Fcntl.hsc" #-}