Skip to content

Commit ab1ddbb

Browse files
authored
Merge pull request #805 from IntersectMBO/js/older-unix
Allow unix 2.8.6
2 parents be88b48 + 8ca4ac8 commit ab1ddbb

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

blockio/blockio.cabal

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,21 @@ library
9393

9494
if os(linux)
9595
hs-source-dirs: src-linux
96-
other-modules: System.FS.BlockIO.Internal
97-
build-depends: unix ^>=2.8.7
96+
build-depends: unix ^>=2.8.6
97+
other-modules:
98+
System.FS.BlockIO.Internal
99+
System.FS.BlockIO.Internal.Fcntl
98100

99101
if !flag(serialblockio)
100102
other-modules: System.FS.BlockIO.Async
101103
build-depends: blockio-uring ^>=0.1
102104

103105
elif os(osx)
104106
hs-source-dirs: src-macos
105-
build-depends: unix ^>=2.8.7
106-
other-modules: System.FS.BlockIO.Internal
107+
build-depends: unix ^>=2.8.6
108+
other-modules:
109+
System.FS.BlockIO.Internal
110+
System.FS.BlockIO.Internal.Fcntl
107111

108112
elif os(windows)
109113
hs-source-dirs: src-windows

blockio/src-linux/System/FS/BlockIO/Internal.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{-# LANGUAGE CPP #-}
2-
32
module System.FS.BlockIO.Internal (
43
ioHasBlockIO
54
) where
65

76
import qualified System.FS.API as FS
87
import System.FS.API (FsPath, Handle (..), HasFS)
98
import System.FS.BlockIO.API (Advice (..), FileOffset, HasBlockIO)
9+
import qualified System.FS.BlockIO.Internal.Fcntl as Fcntl
1010
import qualified System.FS.BlockIO.IO.Internal as IOI
1111
import System.FS.IO (HandleIO)
1212
import qualified System.FS.IO.Handle as FS
13-
import qualified System.Posix.Fcntl as Fcntl
13+
import qualified System.Posix.Fcntl as Fcntl (Advice (..), fileAdvise,
14+
fileAllocate)
1415
import qualified System.Posix.Files as Unix
1516
import qualified System.Posix.Unistd as Unix
1617

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{-# LANGUAGE CPP #-}
2+
-- | Compatibility layer for the @unix@ package to provide a @fileSetCaching@ function.
3+
--
4+
-- @unix >= 2.8.7@ defines a @fileSetCaching@ function, but @unix < 2.8.7@ does not. This module defines the function for @unix@ versions @< 2.8.7@. The implementation is adapted from https://github.com/haskell/unix/blob/v2.8.8.0/System/Posix/Fcntl.hsc#L116-L182.
5+
--
6+
-- NOTE: in the future if we no longer support @unix@ versions @< 2.8.7@, then this module can be removed.
7+
module System.FS.BlockIO.Internal.Fcntl (fileSetCaching) where
8+
9+
#if MIN_VERSION_unix(2,8,7)
10+
11+
import System.Posix.Fcntl (fileSetCaching)
12+
13+
#else
14+
15+
-- hsc2hs does not define _GNU_SOURCE, so a .hsc file must define it explicitly
16+
-- or O_DIRECT stays hidden. The unix package doesn’t define it in source, but
17+
-- its configure script calls AC_USE_SYSTEM_EXTENSIONS, which adds -D_GNU_SOURCE
18+
-- to the build CFLAGS, and those flags are passed on to hsc2hs through the
19+
-- generated `config.mk`.
20+
#define _GNU_SOURCE
21+
22+
#include <fcntl.h>
23+
24+
import Data.Bits (complement, (.&.), (.|.))
25+
import Foreign.C (throwErrnoIfMinus1, throwErrnoIfMinus1_)
26+
import System.Posix.Internals
27+
import System.Posix.Types (Fd (Fd))
28+
29+
-- | For simplification, we considered that Linux !HAS_F_NOCACHE and HAS_O_DIRECT
30+
fileSetCaching :: Fd -> Bool -> IO ()
31+
fileSetCaching (Fd fd) val = do
32+
r <- throwErrnoIfMinus1 "fileSetCaching" (c_fcntl_read fd #{const F_GETFL})
33+
let r' | val = fromIntegral r .&. complement opt_val
34+
| otherwise = fromIntegral r .|. opt_val
35+
throwErrnoIfMinus1_ "fileSetCaching" (c_fcntl_write fd #{const F_SETFL} r')
36+
where
37+
opt_val = #{const O_DIRECT}
38+
#endif

blockio/src-macos/System/FS/BlockIO/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ module System.FS.BlockIO.Internal (
55
import qualified System.FS.API as FS
66
import System.FS.API (FsPath, Handle (..), HasFS)
77
import System.FS.BlockIO.API (Advice (..), FileOffset, HasBlockIO)
8+
import qualified System.FS.BlockIO.Internal.Fcntl as Unix
89
import qualified System.FS.BlockIO.IO.Internal as IOI
910
import qualified System.FS.BlockIO.Serial as Serial
1011
import System.FS.IO (HandleIO)
1112
import qualified System.FS.IO.Handle as FS
12-
import qualified System.Posix.Fcntl as Unix
1313
import qualified System.Posix.Files as Unix
1414
import qualified System.Posix.Unistd as Unix
1515

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE CPP #-}
2+
-- | Compatibility layer for the @unix@ package to provide a @fileSetCaching@ function.
3+
--
4+
-- @unix >= 2.8.7@ defines a @fileSetCaching@ function, but @unix < 2.8.7@ does not. This module defines the function for @unix@ versions @< 2.8.7@. The implementation is adapted from https://github.com/haskell/unix/blob/v2.8.8.0/System/Posix/Fcntl.hsc#L116-L182.
5+
--
6+
-- NOTE: in the future if we no longer support @unix@ versions @< 2.8.7@, then this module can be removed.
7+
module System.FS.BlockIO.Internal.Fcntl (fileSetCaching) where
8+
9+
#if MIN_VERSION_unix(2,8,7)
10+
11+
import System.Posix.Fcntl (fileSetCaching)
12+
13+
#else
14+
15+
#include <fcntl.h>
16+
17+
import Foreign.C (throwErrnoIfMinus1_)
18+
import System.Posix.Internals
19+
import System.Posix.Types (Fd (Fd))
20+
21+
-- | For simplification, we considered that MacOS HAS_F_NOCACHE and !HAS_O_DIRECT
22+
fileSetCaching :: Fd -> Bool -> IO ()
23+
fileSetCaching (Fd fd) val = do
24+
throwErrnoIfMinus1_ "fileSetCaching" (c_fcntl_write fd #{const F_NOCACHE} (if val then 0 else 1))
25+
#endif

0 commit comments

Comments
 (0)