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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Fri Sep 11 03:59:48 PDT 2026


================
@@ -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));
----------------
labath wrote:

Please use scope_exit for the cleanup.

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


More information about the libc-commits mailing list