simple-sendfile 0.2.28 → 0.2.29
raw patch · 5 files changed
+88/−14 lines, 5 filesdep ~unix
Dependency ranges changed: unix
Files
- Network/Sendfile/BSD.hsc +15/−5
- Network/Sendfile/Linux.hsc +15/−8
- Network/Sendfile/Types.hs +24/−0
- simple-sendfile.cabal +1/−1
- test/SendfileSpec.hs +33/−0
Network/Sendfile/BSD.hsc view
@@ -22,7 +22,11 @@ import Network.Sendfile.Types import Network.Socket import Network.Socket.ByteString-import System.Posix.IO+import System.Posix.IO ( OpenMode(..)+ , OpenFileFlags(..)+ , defaultFileFlags+ , closeFd+ ) import System.Posix.Types #include <sys/types.h>@@ -44,7 +48,7 @@ sendfile sock path range hook = bracket setup teardown $ \fd -> sendfileFd sock fd range hook where- setup = openFd path ReadOnly Nothing defaultFileFlags{nonBlock=True}+ setup = openFd path ReadOnly defaultFileFlags{nonBlock=True} teardown = closeFd -- |@@ -59,7 +63,10 @@ sendfileFd :: Socket -> Fd -> FileRange -> IO () -> IO () sendfileFd sock fd range hook = do-#if MIN_VERSION_network(3,0,0)+#if MIN_VERSION_network(3,1,0)+ withFdSocket sock $ \s -> do+ let dst = Fd s+#elif MIN_VERSION_network(3,0,0) dst <- Fd <$> fdSocket sock #else let dst = Fd $ fdSocket sock@@ -108,7 +115,7 @@ sendfileWithHeader sock path range hook hdr = bracket setup teardown $ \fd -> sendfileFdWithHeader sock fd range hook hdr where- setup = openFd path ReadOnly Nothing defaultFileFlags{nonBlock=True}+ setup = openFd path ReadOnly defaultFileFlags{nonBlock=True} teardown = closeFd -- |@@ -128,7 +135,10 @@ sendfileFdWithHeader :: Socket -> Fd -> FileRange -> IO () -> [ByteString] -> IO () sendfileFdWithHeader sock fd range hook hdr = do-#if MIN_VERSION_network(3,0,0)+#if MIN_VERSION_network(3,1,0)+ withFdSocket sock $ \s -> do+ let dst = Fd s+#elif MIN_VERSION_network(3,0,0) dst <- Fd <$> fdSocket sock #else let dst = Fd $ fdSocket sock
Network/Sendfile/Linux.hsc view
@@ -10,12 +10,10 @@ , sendfileFdWithHeader ) where -import Control.Applicative import Control.Exception import Control.Monad import Data.ByteString as B import Data.ByteString.Internal-import Data.Int import Foreign.C.Error (eAGAIN, getErrno, throwErrno) import Foreign.C.Types import Foreign.Marshal (alloca)@@ -26,8 +24,11 @@ import Network.Sendfile.Types import Network.Socket import System.Posix.Files-import System.Posix.IO-import qualified System.Posix.IO.ByteString as B+import System.Posix.IO ( OpenMode(..)+ , OpenFileFlags(..)+ , defaultFileFlags+ , closeFd+ ) import System.Posix.Types #include <sys/sendfile.h>@@ -66,14 +67,14 @@ sendfile sock path range hook = bracket setup teardown $ \fd -> sendfileFd sock fd range hook where- setup = openFd path ReadOnly Nothing defaultFileFlags{nonBlock=True}+ setup = openFd path ReadOnly defaultFileFlags{nonBlock=True} teardown = closeFd sendfile' :: Fd -> ByteString -> FileRange -> IO () -> IO () sendfile' dst path range hook = bracket setup teardown $ \src -> sendfileFd' dst src range hook where- setup = B.openFd path ReadOnly Nothing defaultFileFlags{nonBlock=True}+ setup = openFdBS path ReadOnly defaultFileFlags{nonBlock=True} teardown = closeFd -- |@@ -93,7 +94,10 @@ -- is sent and bofore waiting the socket to be ready for writing. sendfileFd :: Socket -> Fd -> FileRange -> IO () -> IO () sendfileFd sock fd range hook = do-#if MIN_VERSION_network(3,0,0)+#if MIN_VERSION_network(3,1,0)+ withFdSocket sock $ \s -> do+ let dst = Fd s+#elif MIN_VERSION_network(3,0,0) dst <- Fd <$> fdSocket sock #else let dst = Fd $ fdSocket sock@@ -204,7 +208,10 @@ sendMsgMore :: Socket -> ByteString -> IO () sendMsgMore sock bs = withForeignPtr fptr $ \ptr -> do-#if MIN_VERSION_network(3,0,0)+#if MIN_VERSION_network(3,1,0)+ withFdSocket sock $ \fd -> do+ let s = Fd fd+#elif MIN_VERSION_network(3,0,0) s <- Fd <$> fdSocket sock #else let s = Fd $ fdSocket sock
Network/Sendfile/Types.hs view
@@ -1,5 +1,13 @@+{-# LANGUAGE CPP #-}+ module Network.Sendfile.Types where +import qualified Data.ByteString as B++import qualified System.Posix.IO as P+import qualified System.Posix.IO.ByteString as PB+import qualified System.Posix.Types as P+ -- | -- File range for 'sendfile'. @@ -8,3 +16,19 @@ rangeOffset :: Integer , rangeLength :: Integer }++-- | 'openFd's signature changed in @unix-2.8@. This is a portable wrapper.+openFd :: FilePath -> P.OpenMode -> P.OpenFileFlags -> IO P.Fd+#if MIN_VERSION_unix(2,8,0)+openFd path mode flags = P.openFd path mode flags+#else+openFd path mode flags = P.openFd path mode Nothing flags+#endif++-- | 'openFd's signature changed in @unix-2.8@. This is a portable wrapper.+openFdBS :: B.ByteString -> P.OpenMode -> P.OpenFileFlags -> IO P.Fd+#if MIN_VERSION_unix(2,8,0)+openFdBS path mode flags = PB.openFd path mode flags+#else+openFdBS path mode flags = PB.openFd path mode Nothing flags+#endif
simple-sendfile.cabal view
@@ -1,5 +1,5 @@ Name: simple-sendfile-Version: 0.2.28+Version: 0.2.29 Author: Kazu Yamamoto <kazu@iij.ad.jp> Maintainer: Kazu Yamamoto <kazu@iij.ad.jp> License: BSD3
test/SendfileSpec.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module SendfileSpec where@@ -87,18 +88,34 @@ sendFileCore :: FileRange -> [ByteString] -> IO ExitCode sendFileCore range headers = bracket setup teardown $ \(s2,_) -> do+#if MIN_VERSION_conduit(1,3,0)+ runResourceT $ runConduit (sourceSocket s2 .| sinkFile outputFile)+#else runResourceT $ sourceSocket s2 $$ sinkFile outputFile+#endif runResourceT $ copyfile range system $ "cmp -s " ++ outputFile ++ " " ++ expectedFile where copyfile EntireFile = do -- of course, we can use <> here+#if MIN_VERSION_conduit(1,3,0)+ runConduit (sourceList headers .| sinkFile expectedFile)+ runConduit (sourceFile inputFile .| sinkAppendFile expectedFile)+#else sourceList headers $$ sinkFile expectedFile sourceFile inputFile $$ sinkAppendFile expectedFile+#endif copyfile (PartOfFile off len) = do+#if MIN_VERSION_conduit(1,3,0)+ runConduit (sourceList headers .| sinkFile expectedFile)+ runConduit (sourceFile inputFile+ .| CB.isolate (off' + len')+ .| (CB.take off' >> sinkAppendFile expectedFile))+#else sourceList headers $$ sinkFile expectedFile sourceFile inputFile $= CB.isolate (off' + len') $$ (CB.take off' >> sinkAppendFile expectedFile)+#endif where off' = fromIntegral off len' = fromIntegral len@@ -139,7 +156,11 @@ sendIllegalCore :: FileRange -> [ByteString] -> IO () sendIllegalCore range headers = bracket setup teardown $ \(s2,_) -> do+#if MIN_VERSION_conduit(1,3,0)+ runResourceT $ runConduit (sourceSocket s2 .| sinkFile outputFile)+#else runResourceT $ sourceSocket s2 $$ sinkFile outputFile+#endif return () where setup = do@@ -177,11 +198,19 @@ truncateFileCore :: [ByteString] -> IO () truncateFileCore headers = bracket setup teardown $ \(s2,_) -> do+#if MIN_VERSION_conduit(1,3,0)+ runResourceT $ runConduit (sourceSocket s2 .| sinkFile outputFile)+#else runResourceT $ sourceSocket s2 $$ sinkFile outputFile+#endif return () where setup = do+#if MIN_VERSION_conduit(1,3,0)+ runResourceT $ runConduit (sourceFile inputFile .| sinkFile tempFile)+#else runResourceT $ sourceFile inputFile $$ sinkFile tempFile+#endif (s1,s2) <- socketPair AF_UNIX Stream 0 ref <- newIORef (1 :: Int) tid <- forkIO (sf s1 ref `finally` sendEOF s1)@@ -214,5 +243,9 @@ sinkAppendFile :: MonadResource m => FilePath+#if MIN_VERSION_conduit(1,3,0)+ -> ConduitT ByteString Void m ()+#else -> Sink ByteString m ()+#endif sinkAppendFile fp = sinkIOHandle (openBinaryFile fp AppendMode)