-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(journal): Make own version of
fileAllocate
that works on Mac OS
- Loading branch information
1 parent
44acff8
commit 34c251f
Showing
5 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters