From: Oleg Sidorkin Date: Mon, 14 Nov 2022 18:53:50 +0000 (+0300) Subject: fix: Fall back to emulation for unsupported posix_fallocate (#1222) X-Git-Tag: v4.7.4~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec77044ea62bab253e62ee5aaf2646a2ee551004;p=thirdparty%2Fccache.git fix: Fall back to emulation for unsupported posix_fallocate (#1222) posix_fallocate can return EINVAL if filesystem doesn't support it. Fall back to emulation in this case. E.g. ZFS does so on FreeBSD (haven't tested with ZFS on linux). This fixes Utill::fallocate unit tests on ZFS. --- diff --git a/src/Util.cpp b/src/Util.cpp index eaac8b953..e802a83be 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -482,8 +482,13 @@ int fallocate(int fd, long new_size) { #ifdef HAVE_POSIX_FALLOCATE - return posix_fallocate(fd, 0, new_size); -#else + const int posix_fallocate_err = posix_fallocate(fd, 0, new_size); + if (posix_fallocate_err == 0 || posix_fallocate_err != EINVAL) { + return posix_fallocate_err; + } + // the underlying filesystem does not support the operation so fallback to + // lseeks +#endif off_t saved_pos = lseek(fd, 0, SEEK_END); off_t old_size = lseek(fd, 0, SEEK_END); if (old_size == -1) { @@ -510,7 +515,6 @@ fallocate(int fd, long new_size) lseek(fd, saved_pos, SEEK_SET); free(buf); return err; -#endif } std::string