-
Notifications
You must be signed in to change notification settings - Fork 92
Get or set the file caching mode #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,23 +16,33 @@ | |
----------------------------------------------------------------------------- | ||
|
||
#include "HsUnix.h" | ||
#include <fcntl.h> | ||
|
||
module System.Posix.Fcntl ( | ||
-- * File allocation | ||
Advice(..), fileAdvise, | ||
fileAllocate, | ||
-- * File caching | ||
fileGetCaching, | ||
fileSetCaching, | ||
) where | ||
|
||
#if HAVE_POSIX_FALLOCATE || HAVE_POSIX_FADVISE | ||
import Foreign.C | ||
#endif | ||
import System.Posix.Types | ||
|
||
#if !HAVE_POSIX_FALLOCATE | ||
import System.IO.Error ( ioeSetLocation ) | ||
import GHC.IO.Exception ( unsupportedOperation ) | ||
#endif | ||
|
||
#ifndef darwin_HOST_OS | ||
import Data.Bits (complement, (.&.), (.|.)) | ||
import System.Posix.Internals (c_fcntl_read) | ||
#endif | ||
|
||
import System.Posix.Internals (c_fcntl_write) | ||
|
||
|
||
-- ----------------------------------------------------------------------------- | ||
-- File control | ||
|
||
|
@@ -101,3 +111,68 @@ foreign import capi safe "fcntl.h posix_fallocate" | |
fileAllocate _ _ _ = ioError (ioeSetLocation unsupportedOperation | ||
"fileAllocate") | ||
#endif | ||
|
||
-- ----------------------------------------------------------------------------- | ||
-- 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. | ||
Bodigrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- | ||
-- On Linux, FreeBSD, and NetBSD this checks whether the @O_DIRECT@ file flag is | ||
Bodigrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- set. | ||
-- | ||
-- Throws 'IOError' (\"unsupported operation\") if platform does not support | ||
-- reading the cache mode. | ||
-- | ||
-- (use @#if HAVE_O_DIRECT@ CPP guard to detect availability). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this macro available to a user though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer the documentation to say so explicitly, otherwise people will be left wondering who defines them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've now added a hint to use |
||
-- | ||
-- @since 2.8.x.y | ||
fileGetCaching :: Fd -> IO Bool | ||
#if HAVE_O_DIRECT | ||
fileGetCaching (Fd fd) = do | ||
r <- throwErrnoIfMinus1 "fileGetCaching" (c_fcntl_read fd #{const F_GETFL}) | ||
return ((r .&. opt_val) /= 0) | ||
where | ||
opt_val = #{const O_DIRECT} | ||
#else | ||
{-# WARNING fileGetCaching | ||
"operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_O_DIRECT@)" #-} | ||
fileGetCaching _ _ = ioError (ioeSetLocation unsupportedOperation "fileGetCaching") | ||
hasufell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
|
||
-- | 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 | ||
-- reading the cache mode. | ||
hasufell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- | ||
-- (use @#if HAVE_O_DIRECT || HAVE_F_NOCACHE@ CPP guard to detect availability). | ||
-- | ||
-- @since 2.8.x.y | ||
fileSetCaching :: Fd -> Bool -> IO () | ||
#if HAVE_O_DIRECT | ||
fileSetCaching (Fd fd) val = do | ||
r <- throwErrnoIfMinus1 "fileSetCaching" (c_fcntl_read fd #{const F_GETFL}) | ||
let r' | val = fromIntegral r .|. opt_val | ||
| otherwise = fromIntegral r .&. complement opt_val | ||
throwErrnoIfMinus1_ "fileSetCaching" (c_fcntl_write fd #{const F_SETFL} r') | ||
where | ||
opt_val = #{const O_DIRECT} | ||
#elif HAVE_F_NOCACHE | ||
fileSetCaching (Fd fd) val = do | ||
throwErrnoIfMinus1_ "fileSetCaching" (c_fcntl_write fd #{const F_NOCACHE} (if val then 1 else 0)) | ||
#else | ||
{-# WARNING fileGetCaching | ||
"operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_O_DIRECT || HAVE_F_NOCACHE @)" #-} | ||
fileGetCaching _ _ = ioError (ioeSetLocation unsupportedOperation "fileGetCaching") | ||
#endif |
Uh oh!
There was an error while loading. Please reload this page.