From debcbb4f3cf6100f2c423ab637f75073cabbc343 Mon Sep 17 00:00:00 2001 From: matil019 Date: Sun, 17 Apr 2022 14:06:35 +0900 Subject: [PATCH 1/2] Fix the error handling of posix_fallocate other than FreeBSD This commit introduces a CPP guard to take care of the difference between OSes of the errors of posix_fallocate. On Linux and NetBSD, posix_fallocate reports error by returning an error number. errno is not set. On the other hand, on FreeBSD, posix_fallocate returns -1 and sets errno. The existing code could handle FreeBSD-style errors only. --- System/Posix/Fcntl.hsc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/System/Posix/Fcntl.hsc b/System/Posix/Fcntl.hsc index 3735fc09..05690543 100644 --- a/System/Posix/Fcntl.hsc +++ b/System/Posix/Fcntl.hsc @@ -87,8 +87,16 @@ fileAdvise _ _ _ _ = return () -- @since 2.7.1.0 fileAllocate :: Fd -> FileOffset -> FileOffset -> IO () #if HAVE_POSIX_FALLOCATE +#if defined(freebsd_HOST_OS) fileAllocate fd off len = do throwErrnoIfMinus1_ "fileAllocate" (c_posix_fallocate (fromIntegral fd) (fromIntegral off) (fromIntegral len)) +#else +fileAllocate fd off len = do + ret <- c_posix_fallocate (fromIntegral fd) (fromIntegral off) (fromIntegral len) + if ret == 0 + then pure () + else ioError (errnoToIOError "fileAllocate" (Errno ret) Nothing Nothing) +#endif foreign import capi safe "fcntl.h posix_fallocate" c_posix_fallocate :: CInt -> COff -> COff -> IO CInt From e8dda978f9e5665371dfc5d697df47654a0860a9 Mon Sep 17 00:00:00 2001 From: matil019 Date: Sun, 17 Apr 2022 16:46:44 +0900 Subject: [PATCH 2/2] Remove obsolete FreeBSD-style error handling The FreeBSD-style error (return -1, set errno) is obsolete as of FreeBSD 11.0; it now reports errors as same as the other OSes. --- System/Posix/Fcntl.hsc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/System/Posix/Fcntl.hsc b/System/Posix/Fcntl.hsc index 05690543..29ff5466 100644 --- a/System/Posix/Fcntl.hsc +++ b/System/Posix/Fcntl.hsc @@ -87,16 +87,11 @@ fileAdvise _ _ _ _ = return () -- @since 2.7.1.0 fileAllocate :: Fd -> FileOffset -> FileOffset -> IO () #if HAVE_POSIX_FALLOCATE -#if defined(freebsd_HOST_OS) -fileAllocate fd off len = do - throwErrnoIfMinus1_ "fileAllocate" (c_posix_fallocate (fromIntegral fd) (fromIntegral off) (fromIntegral len)) -#else fileAllocate fd off len = do ret <- c_posix_fallocate (fromIntegral fd) (fromIntegral off) (fromIntegral len) if ret == 0 then pure () else ioError (errnoToIOError "fileAllocate" (Errno ret) Nothing Nothing) -#endif foreign import capi safe "fcntl.h posix_fallocate" c_posix_fallocate :: CInt -> COff -> COff -> IO CInt