]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fall back to emulation for unsupported posix_fallocate (#1222)
authorOleg Sidorkin <osidorkin@gmail.com>
Mon, 14 Nov 2022 18:53:50 +0000 (21:53 +0300)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 18:53:50 +0000 (19:53 +0100)
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.

src/Util.cpp

index eaac8b9536503f936a16ddb7e10ec6306e423de6..e802a83be83c7721f769ddb0c9aa4de75cb6da14 100644 (file)
@@ -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