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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Mon Sep 14 02:37:24 PDT 2026


================
@@ -0,0 +1,60 @@
+//===-- 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 "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 "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.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();
+  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));
----------------
labath wrote:

Could this use non-zero offsets? To test that the offset arg is passed correctly..

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


More information about the libc-commits mailing list