Skip to content

Commit

Permalink
feat(journal): Make own version of fileAllocate that works on Mac OS
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-daniel-gustafsson committed Jan 12, 2022
1 parent 44acff8 commit 34c251f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/journal/cbits/mac_falloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <fcntl.h>
#include <unistd.h>

// based on https://github.com/coilhq/tigerbeetle/blob/7e12fccc4859f035cd26af29b8e9f9749a0899a3/src/storage.zig#L430
int mac_fallocate(int fd, off_t offset, off_t length) {
fstore_t store = {
.fst_flags = F_ALLOCATECONTIG | F_ALLOCATEALL,
.fst_posmode = F_PEOFPOSMODE,
.fst_offset = 0,
.fst_length = offset + length,
.fst_bytesalloc = 0,
};

int res = fcntl(fd, F_PREALLOCATE, &store);
if (res != 0) {
store.fst_flags = F_ALLOCATEALL;
res = fcntl(fd, F_PREALLOCATE, &store);
}

if (res != 0) {
return res;
}

return ftruncate(fd, length);
}
13 changes: 11 additions & 2 deletions src/journal/journal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ extra-source-files:
LICENSE
README.md
cbits/atomic.c
cbits/mac_falloc.c

library
hs-source-dirs: src/
hs-source-dirs: src
if os(darwin)
hs-source-dirs: osx
if os(linux)
hs-source-dirs: linux

-- GHC boot library dependencies:
-- (https://gitlab.haskell.org/ghc/ghc/-/blob/master/packages)
Expand All @@ -49,6 +54,7 @@ library
Journal.CRC32
Journal.Internal
Journal.Internal.Atomics
Journal.Internal.FileAllocate
Journal.Internal.Parse
Journal.Internal.Metrics
Journal.Internal.ByteBuffer
Expand All @@ -62,7 +68,10 @@ library
ghc-options: -O2
if os(linux)
extra-libraries: atomic
c-sources: cbits/atomic.c
c-sources:
cbits/atomic.c
if os(darwin)
c-sources: cbits/mac_falloc.c
default-language: Haskell2010

test-suite test
Expand Down
12 changes: 12 additions & 0 deletions src/journal/linux/Journal/Internal/FileAllocate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Journal.Internal.FileAllocate
(fileAllocate)
where

import Foreign
import Foreign.C.Types

import System.Posix.Types
import qualified System.Posix.Fcntl as Posix

fileAllocate :: Fd -> FileOffset -> FileOffset -> IO ()
fileAllocate = Posix.fileAllocate
20 changes: 20 additions & 0 deletions src/journal/osx/Journal/Internal/FileAllocate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{-# LANGUAGE ForeignFunctionInterface #-}
module Journal.Internal.FileAllocate
(fileAllocate)
where

import Foreign
import Foreign.C.Types

import System.Posix.Types

foreign import ccall unsafe "mac_fallocate"
mac_fallocate :: CInt -> COff -> COff -> IO Int

fileAllocate :: Fd -> FileOffset -> FileOffset -> IO ()
fileAllocate fd off len = do
res <- mac_fallocate (fromIntegral fd) off len
if res == 0
then return ()
else error $ "fileAllocate " <> show fd <> " " <> show off <> " " <> show len
<> " ;;; returned returned non-zero result: " <> show res
2 changes: 1 addition & 1 deletion src/journal/src/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import System.Directory
, getFileSize
)
import System.FilePath (takeDirectory, (</>))
import System.Posix.Fcntl (fileAllocate)
import System.Posix.IO (fdWriteBuf)

import Journal.Internal
import Journal.Internal.BufferClaim
import Journal.Internal.ByteBufferPtr
import Journal.Internal.FileAllocate (fileAllocate)
import Journal.Internal.Mmap (sysconfPageSize)
import Journal.Types
import Journal.Types.AtomicCounter
Expand Down

0 comments on commit 34c251f

Please sign in to comment.