[libc-commits] [libc] [libc] Implement fallocate (PR #222843)

via libc-commits libc-commits at lists.llvm.org
Fri Sep 11 06:52:58 PDT 2026


https://github.com/afnrow updated https://github.com/llvm/llvm-project/pull/222843

>From 6cf81ad20764543ea272ec0ad51e9396e2a7b36f Mon Sep 17 00:00:00 2001
From: yahia ahmed <yahia.a.abdrabou at gmail.com>
Date: Fri, 11 Sep 2026 08:12:28 +0300
Subject: [PATCH 1/2] [libc] Implement fallocate

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/fcntl.yaml                       |  9 +++
 .../llvm-libc-macros/linux/fcntl-macros.h     |  8 +++
 .../linux/syscall_wrappers/CMakeLists.txt     | 14 +++++
 .../OSUtil/linux/syscall_wrappers/fallocate.h | 42 +++++++++++++
 libc/src/fcntl/CMakeLists.txt                 |  7 +++
 libc/src/fcntl/fallocate.h                    | 26 ++++++++
 libc/src/fcntl/linux/CMakeLists.txt           | 12 ++++
 libc/src/fcntl/linux/fallocate.cpp            | 32 ++++++++++
 libc/test/src/fcntl/CMakeLists.txt            | 16 +++++
 libc/test/src/fcntl/fallocate_test.cpp        | 59 +++++++++++++++++++
 libc/utils/docgen/fcntl.yaml                  |  2 +
 13 files changed, 229 insertions(+)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
 create mode 100644 libc/src/fcntl/fallocate.h
 create mode 100644 libc/src/fcntl/linux/fallocate.cpp
 create mode 100644 libc/test/src/fcntl/fallocate_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c0f06e70ac671..6b38ddd218173 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -42,6 +42,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
     libc.src.fcntl.posix_fadvise
+    libc.src.fcntl.fallocate
 
     # poll.h entrypoints
     libc.src.poll.poll
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index d9c027dfc2511..d9ce42047e6cf 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -53,6 +53,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
     libc.src.fcntl.posix_fadvise
+    libc.src.fcntl.fallocate
 
     # net/if.h entrypoints
     libc.src.net.if_indextoname
diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml
index e2f174447b6ba..1b609b97a107b 100644
--- a/libc/include/fcntl.yaml
+++ b/libc/include/fcntl.yaml
@@ -73,3 +73,12 @@ functions:
       - type: off_t
       - type: off_t
       - type: int
+  - name: fallocare
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: int
+      - type: int
+      - type: off_t
+      - type: off_t
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index 648c4a9ec00da..dc7be0b9045f9 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -24,6 +24,14 @@
 #define O_SYNC 004010000
 #define O_TRUNC 000001000
 
+#define FALLOC_FL_KEEP_SIZE 0x01
+#define FALLOC_FL_PUNCH_HOLE 0x02
+#define FALLOC_FL_NO_HIDE_STALE 0x04
+#define FALLOC_FL_COLLAPSE_RANGE 0x08
+#define FALLOC_FL_ZERO_RANGE 0x10
+#define FALLOC_FL_INSERT_RANGE 0x20
+#define FALLOC_FL_UNSHARE_RANGE 0x40
+
 #ifdef __aarch64__
 #define O_DIRECT 000200000
 #define O_DIRECTORY 000040000
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 44e505694fab1..fa629b82167b2 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1275,3 +1275,17 @@ add_header_library(
     libc.src.__support.macros.config
     libc.src.__support.OSUtil.osutil
 )
+
+add_header_library(
+  fallocate
+  HDRS
+    fallocate.h
+  DEPENDS
+    libc.src.__support.macros.config
+    libc.src.__support.common
+    libc.src.__support.OSUtil.osutil
+    libc.include.sys_syscall
+    libc.hdr.types.off_t
+    libc.hdr.errno_macros
+    libc.src.__support.error_or
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
new file mode 100644
index 0000000000000..88cf78cd345d1
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the declaration of the fallocate, which is the
+/// base syscall wrapper for all fallocate dependent calls.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FALLOCATE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FALLOCATE_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/off_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> fallocate(int fd, int mode, off_t offset, off_t size) {
+#ifdef SYS_fallocate
+#if !__SIZEOF__POINTER == 8 // 64 bit machines
+  /* TODO: Add support for 32 bits */
+  return Error(ENOSYS);
+#else
+  return syscall_checked<int>(SYS_fallocate, fd, mode, offset, size);
+#endif
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+#endif
diff --git a/libc/src/fcntl/CMakeLists.txt b/libc/src/fcntl/CMakeLists.txt
index 1d6c31cf98978..67e80a1888a32 100644
--- a/libc/src/fcntl/CMakeLists.txt
+++ b/libc/src/fcntl/CMakeLists.txt
@@ -36,3 +36,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.posix_fadvise
 )
+
+add_entrypoint_object(
+  fallocate
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fallocate
+)
diff --git a/libc/src/fcntl/fallocate.h b/libc/src/fcntl/fallocate.h
new file mode 100644
index 0000000000000..bfc5605fe8251
--- /dev/null
+++ b/libc/src/fcntl/fallocate.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for fallocate
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_FCNTL_FALLOCATE_H
+#define LLVM_LIBC_SRC_FCNTL_FALLOCATE_H
+
+#include "src/__support/macros/config.h"
+#include "hdr/types/off_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+  int fallocate(int fd, int mode, off_t offset, off_t size);
+
+}
+
+#endif
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index bba33928da17a..11b88e3af0197 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -60,3 +60,15 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.linux.syscall_wrappers.posix_fadvise
     libc.src.__support.common
 )
+
+add_entrypoint_object(
+  fallocate
+  SRCS
+    fallocate.cpp
+  HDRS
+    ../fallocate.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.__support.OSUtil.linux.syscall_wrappers.fallocate
+    libc.hdr.types.off_t
+)
diff --git a/libc/src/fcntl/linux/fallocate.cpp b/libc/src/fcntl/linux/fallocate.cpp
new file mode 100644
index 0000000000000..9237428f017e1
--- /dev/null
+++ b/libc/src/fcntl/linux/fallocate.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the declaration of the fallocate, which is the
+/// fcntl fallocate function for linux.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/fallocate.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/fallocate.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/libc_errno.h"
+#include "hdr/types/off_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+  LLVM_LIBC_FUNCTION(int, fallocate, (int fd, int mode, off_t offset, off_t size)) {
+    auto result = linux_syscalls::fallocate(fd, mode, offset, size);
+    if (!result.has_value()) {
+      libc_errno = result.error();
+      return -1;
+    }
+    return 0;
+  }
+}
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index bcce22406692b..f6c36d359a2ea 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -76,3 +76,19 @@ add_libc_test(
     libc.src.__support.CPP.scope
     libc.test.UnitTest.ErrnoCheckingTest
 )
+
+add_libc_test(
+  fallocate_test
+  SUITE
+    libc_fcntl_unittests
+  SRCS
+    fallocate_test.cpp
+  DEPENDS
+    libc.include.fcntl
+    libc.src.fcntl.fallocate
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.unlink
+    libc.src.sys.stat.fstat
+    libc.src.__support.libc_errno
+)
diff --git a/libc/test/src/fcntl/fallocate_test.cpp b/libc/test/src/fcntl/fallocate_test.cpp
new file mode 100644
index 0000000000000..62b2258ccdcbe
--- /dev/null
+++ b/libc/test/src/fcntl/fallocate_test.cpp
@@ -0,0 +1,59 @@
+//===-- Unittests for fallocate -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/fallocate.h"
+#include "src/fcntl/open.h"
+#include "src/sys/stat/fstat.h"
+#include "src/unistd/close.h"
+#include "src/unistd/unlink.h"
+#include "src/__support/libc_errno.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include <fcntl.h>
+#include <sys/stat.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcFallocateTest, BasicAllocate) {
+  constexpr char TEST_FILE[] = "testdata/fallocate_basic.test";
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0600);
+  ASSERT_GT(fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+
+  // Mode 0: expand file size to offset + len (4096 bytes)
+  ASSERT_THAT(LIBC_NAMESPACE::fallocate(fd, 0, 0, 4096), Succeeds(0));
+
+  struct stat st;
+  ASSERT_EQ(LIBC_NAMESPACE::fstat(fd, &st), 0);
+  ASSERT_EQ(static_cast<off_t>(st.st_size), static_cast<off_t>(4096));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST(LlvmLibcFallocateTest, KeepSize) {
+  constexpr char TEST_FILE[] = "testdata/fallocate_keep_size.test";
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0600);
+  ASSERT_GT(fd, 0);
+
+  // Pre-allocate space at offset 8192, length 4096, but keep file size at 0
+  ASSERT_THAT(
+      LIBC_NAMESPACE::fallocate(fd, FALLOC_FL_KEEP_SIZE, 8192, 4096),
+      Succeeds(0));
+
+  struct stat st;
+  ASSERT_EQ(LIBC_NAMESPACE::fstat(fd, &st), 0);
+  ASSERT_EQ(static_cast<off_t>(st.st_size), static_cast<off_t>(0));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
diff --git a/libc/utils/docgen/fcntl.yaml b/libc/utils/docgen/fcntl.yaml
index 7a6b656cc0a80..f9a9a0fd37e13 100644
--- a/libc/utils/docgen/fcntl.yaml
+++ b/libc/utils/docgen/fcntl.yaml
@@ -11,6 +11,8 @@ functions:
     in-latest-posix: ''
   posix_fallocate:
     in-latest-posix: ''
+  fallocate:
+    in-latest-posix: ''
 
 macros:
   AT_EACCESS:

>From 400742948cea80444bea6d906c1c91fffae9a6a4 Mon Sep 17 00:00:00 2001
From: yahia ahmed <yahia.a.abdrabou at gmail.com>
Date: Fri, 11 Sep 2026 16:36:12 +0300
Subject: [PATCH 2/2] Fix style issues and address comments

---
 libc/include/fcntl.yaml                       |  2 +-
 .../linux/syscall_wrappers/CMakeLists.txt     |  2 ++
 .../OSUtil/linux/syscall_wrappers/fallocate.h | 22 ++++++++++++-------
 libc/src/fcntl/fallocate.h                    |  5 ++---
 libc/src/fcntl/linux/fallocate.cpp            | 19 ++++++++--------
 libc/test/src/fcntl/fallocate_test.cpp        | 21 +++++++++---------
 libc/utils/docgen/fcntl.yaml                  |  2 +-
 7 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml
index 1b609b97a107b..eed5e002ed2fa 100644
--- a/libc/include/fcntl.yaml
+++ b/libc/include/fcntl.yaml
@@ -73,7 +73,7 @@ functions:
       - type: off_t
       - type: off_t
       - type: int
-  - name: fallocare
+  - name: fallocate
     standards:
       - linux
     return_type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index fa629b82167b2..c43bdd1726342 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1287,5 +1287,7 @@ add_header_library(
     libc.include.sys_syscall
     libc.hdr.types.off_t
     libc.hdr.errno_macros
+    libc.src.__support.CPP.limits
+    libc.src.__support.CPP.bit
     libc.src.__support.error_or
 )
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
index 88cf78cd345d1..cce4d819aacf4 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fallocate.h
@@ -17,6 +17,8 @@
 
 #include "hdr/errno_macros.h"
 #include "hdr/types/off_t.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/limits.h"
 #include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
@@ -27,14 +29,18 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> fallocate(int fd, int mode, off_t offset, off_t size) {
-#ifdef SYS_fallocate
-#if !__SIZEOF__POINTER == 8 // 64 bit machines
-  /* TODO: Add support for 32 bits */
-  return Error(ENOSYS);
-#else
-  return syscall_checked<int>(SYS_fallocate, fd, mode, offset, size);
-#endif
-#endif
+  if constexpr (sizeof(long) == sizeof(uint32_t) &&
+                sizeof(off_t) == sizeof(uint64_t)) {
+    uint64_t offset_bits = cpp::bit_cast<uint64_t>(offset);
+    long offset_low = static_cast<long>(offset_bits & UINT32_MAX);
+    long offset_high = static_cast<long>(offset_bits >> 32);
+    uint64_t len_bits = cpp::bit_cast<uint64_t>(size);
+    long len_low = static_cast<long>(len_bits & UINT32_MAX);
+    long len_high = static_cast<long>(len_bits >> 32);
+    return syscall_checked<int>(SYS_fallocate, fd, mode, offset_low,
+                                offset_high, len_low, len_high);
+  } else
+    return syscall_checked<int>(SYS_fallocate, fd, mode, offset, size);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/fcntl/fallocate.h b/libc/src/fcntl/fallocate.h
index bfc5605fe8251..7dcdc560c3751 100644
--- a/libc/src/fcntl/fallocate.h
+++ b/libc/src/fcntl/fallocate.h
@@ -14,13 +14,12 @@
 #ifndef LLVM_LIBC_SRC_FCNTL_FALLOCATE_H
 #define LLVM_LIBC_SRC_FCNTL_FALLOCATE_H
 
-#include "src/__support/macros/config.h"
 #include "hdr/types/off_t.h"
+#include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-  int fallocate(int fd, int mode, off_t offset, off_t size);
-
+int fallocate(int fd, int mode, off_t offset, off_t size);
 }
 
 #endif
diff --git a/libc/src/fcntl/linux/fallocate.cpp b/libc/src/fcntl/linux/fallocate.cpp
index 9237428f017e1..59361988848eb 100644
--- a/libc/src/fcntl/linux/fallocate.cpp
+++ b/libc/src/fcntl/linux/fallocate.cpp
@@ -14,19 +14,20 @@
 
 #include "src/fcntl/fallocate.h"
 
+#include "hdr/types/off_t.h"
 #include "src/__support/OSUtil/linux/syscall_wrappers/fallocate.h"
-#include "src/__support/macros/config.h"
 #include "src/__support/libc_errno.h"
-#include "hdr/types/off_t.h"
+#include "src/__support/macros/config.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-  LLVM_LIBC_FUNCTION(int, fallocate, (int fd, int mode, off_t offset, off_t size)) {
-    auto result = linux_syscalls::fallocate(fd, mode, offset, size);
-    if (!result.has_value()) {
-      libc_errno = result.error();
-      return -1;
-    }
-    return 0;
+LLVM_LIBC_FUNCTION(int, fallocate,
+                   (int fd, int mode, off_t offset, off_t size)) {
+  auto result = linux_syscalls::fallocate(fd, mode, offset, size);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
   }
+  return 0;
 }
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/fcntl/fallocate_test.cpp b/libc/test/src/fcntl/fallocate_test.cpp
index 62b2258ccdcbe..64050dcc61d15 100644
--- a/libc/test/src/fcntl/fallocate_test.cpp
+++ b/libc/test/src/fcntl/fallocate_test.cpp
@@ -6,17 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/fcntl_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/libc_errno.h"
 #include "src/fcntl/fallocate.h"
 #include "src/fcntl/open.h"
 #include "src/sys/stat/fstat.h"
+#include "src/sys/stat/stat.h"
 #include "src/unistd/close.h"
 #include "src/unistd/unlink.h"
-#include "src/__support/libc_errno.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
-#include "test/UnitTest/ErrnoCheckingTest.h"
-#include <fcntl.h>
-#include <sys/stat.h>
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
@@ -27,6 +28,10 @@ TEST(LlvmLibcFallocateTest, BasicAllocate) {
   int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0600);
   ASSERT_GT(fd, 0);
   ASSERT_ERRNO_SUCCESS();
+  LIBC_NAMESPACE::cpp::scope_exit cleanup_fd([&] {
+    EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+    EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+  });
 
   // Mode 0: expand file size to offset + len (4096 bytes)
   ASSERT_THAT(LIBC_NAMESPACE::fallocate(fd, 0, 0, 4096), Succeeds(0));
@@ -34,9 +39,6 @@ TEST(LlvmLibcFallocateTest, BasicAllocate) {
   struct stat st;
   ASSERT_EQ(LIBC_NAMESPACE::fstat(fd, &st), 0);
   ASSERT_EQ(static_cast<off_t>(st.st_size), static_cast<off_t>(4096));
-
-  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
-  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
 }
 
 TEST(LlvmLibcFallocateTest, KeepSize) {
@@ -46,9 +48,8 @@ TEST(LlvmLibcFallocateTest, KeepSize) {
   ASSERT_GT(fd, 0);
 
   // Pre-allocate space at offset 8192, length 4096, but keep file size at 0
-  ASSERT_THAT(
-      LIBC_NAMESPACE::fallocate(fd, FALLOC_FL_KEEP_SIZE, 8192, 4096),
-      Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::fallocate(fd, FALLOC_FL_KEEP_SIZE, 8192, 4096),
+              Succeeds(0));
 
   struct stat st;
   ASSERT_EQ(LIBC_NAMESPACE::fstat(fd, &st), 0);
diff --git a/libc/utils/docgen/fcntl.yaml b/libc/utils/docgen/fcntl.yaml
index f9a9a0fd37e13..1053f357901d9 100644
--- a/libc/utils/docgen/fcntl.yaml
+++ b/libc/utils/docgen/fcntl.yaml
@@ -12,7 +12,7 @@ functions:
   posix_fallocate:
     in-latest-posix: ''
   fallocate:
-    in-latest-posix: ''
+    c-definition: ''
 
 macros:
   AT_EACCESS:



More information about the libc-commits mailing list