From 7600886d3d96be1f6f88dfc79a72c960f6d4934c Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Thu, 27 Mar 2014 19:54:24 +0100 Subject: [PATCH] Implement falloc equivalent in OSX --- configure.ac | 3 ++- src/AbstractDiskWriter.cc | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 909c0e35..9735e4e1 100644 --- a/configure.ac +++ b/configure.ac @@ -799,13 +799,14 @@ AC_CHECK_FUNCS([posix_fallocate],[have_posix_fallocate=yes]) ARIA2_CHECK_FALLOCATE if test "x$have_posix_fallocate" = "xyes" || test "x$have_fallocate" = "xyes" || + test "x$have_osx" = "xyes" || test "x$win_build" = "xyes"; then AC_DEFINE([HAVE_SOME_FALLOCATE], [1], [Define to 1 if *_fallocate is available.]) fi AM_CONDITIONAL([HAVE_SOME_FALLOCATE], [test "x$have_posix_fallocate" = "xyes" || test "x$have_fallocate" = "xyes" \ - || test "x$win_build" = "xyes"]) + || test "x$have_osx" = "xyes" || test "x$win_build" = "xyes"]) AC_CHECK_FUNCS([asctime_r], diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index d7a6641a..f977769a 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -483,6 +483,24 @@ void AbstractDiskWriter::allocate(int64_t offset, int64_t length, bool sparse) #ifdef HAVE_SOME_FALLOCATE # ifdef __MINGW32__ truncate(offset+length); +# elif defined(__APPLE__) && defined(__MACH__) + auto toalloc = offset + length - size(); + if (toalloc > 0) { + fstore_t fstore = {F_ALLOCATECONTIG | F_ALLOCATEALL, F_PEOFPOSMODE, 0, toalloc}; + if (fcntl(fd_, F_PREALLOCATE, &fstore) == -1) { + // Retry non-contig. + fstore.fst_flags = F_ALLOCATEALL; + if (fcntl(fd_, F_PREALLOCATE, &fstore) == -1) { + int err = errno; + throw DL_ABORT_EX3(err, + fmt("fcntl(F_PREALLOCATE failed. cause: %s", + util::safeStrerror(err).c_str()), + error_code::FILE_IO_ERROR); + } + } + } + // This forces the allocation on disk. + ftruncate(fd_, offset + length); # elif HAVE_FALLOCATE // For linux, we use fallocate to detect file system supports // fallocate or not.