[libc-commits] [libc] [libc] Implement fdatasync (PR #225613)
via libc-commits
libc-commits at lists.llvm.org
Wed Sep 23 00:16:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
This patch implements the fdatasync entry point for Linux, matching the existing fsync implementation. I'm adding it to all supported Linux architectures (x86_64, aarch64, arm, and riscv), where it's backed by the SYS_fdatasync syscall.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/225613.diff
13 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
- (modified) libc/config/linux/arm/entrypoints.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/include/unistd.yaml (+6)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+12)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h (+33)
- (modified) libc/src/unistd/CMakeLists.txt (+7)
- (added) libc/src/unistd/fdatasync.h (+25)
- (modified) libc/src/unistd/linux/CMakeLists.txt (+14)
- (added) libc/src/unistd/linux/fdatasync.cpp (+32)
- (modified) libc/test/src/unistd/CMakeLists.txt (+19)
- (added) libc/test/src/unistd/fdatasync_test.cpp (+49)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c6e8dc4e69fb4..4e5025e22ac2b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -435,6 +435,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 1602255c6559c..53ad6c42b6567 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -249,6 +249,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fsync
libc.src.unistd.ftruncate
libc.src.unistd.getcwd
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index c56fbf7974ade..f5367add50a6d 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -460,6 +460,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b7c05bc6bc6c5..0274770d049d4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -474,6 +474,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 890a4ae21f191..50863aef0e2ba 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -250,6 +250,12 @@ functions:
- type: int
- type: uid_t
- type: gid_t
+ - name: fdatasync
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
- name: fpathconf
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f5f28f51f49f6..316caccf4306a 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -928,6 +928,18 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ fdatasync
+ HDRS
+ fdatasync.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
fsync
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h
new file mode 100644
index 0000000000000..f59edfc1a239b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Syscall wrapper for fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // 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> fdatasync(int fd) {
+ return syscall_checked<int>(SYS_fdatasync, fd);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 97bae9a643f34..26c9d13680c19 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -119,6 +119,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fchown
)
+add_entrypoint_object(
+ fdatasync
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fdatasync
+)
+
add_entrypoint_object(
fork
ALIAS
diff --git a/libc/src/unistd/fdatasync.h b/libc/src/unistd/fdatasync.h
new file mode 100644
index 0000000000000..8dc456cd49f1f
--- /dev/null
+++ b/libc/src/unistd/fdatasync.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
+#define LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int fdatasync(int fd);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index d4e03819612b9..3c48ab56177eb 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -140,6 +140,20 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ fdatasync
+ SRCS
+ fdatasync.cpp
+ HDRS
+ ../fdatasync.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.__support.OSUtil.linux.syscall_wrappers.fdatasync
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
fork
SRCS
diff --git a/libc/src/unistd/linux/fdatasync.cpp b/libc/src/unistd/linux/fdatasync.cpp
new file mode 100644
index 0000000000000..8001c87d2ac17
--- /dev/null
+++ b/libc/src/unistd/linux/fdatasync.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
+/// Linux implementation of fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/fdatasync.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, fdatasync, (int fd)) {
+ ErrorOr<int> ret = linux_syscalls::fdatasync(fd);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 17960278bd3f7..74eb93d8635c3 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -204,6 +204,25 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ fdatasync_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ fdatasync_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.sys_stat_macros
+ libc.src.fcntl.open
+ libc.src.unistd.close
+ libc.src.unistd.fdatasync
+ libc.src.unistd.unlink
+ libc.src.unistd.write
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
ftruncate_test
SUITE
diff --git a/libc/test/src/unistd/fdatasync_test.cpp b/libc/test/src/unistd/fdatasync_test.cpp
new file mode 100644
index 0000000000000..2b33628619196
--- /dev/null
+++ b/libc/test/src/unistd/fdatasync_test.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/fdatasync.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcFdatasyncTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcFdatasyncTest, BasicSync) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *FILENAME = "fdatasync.test";
+ auto test_file = libc_make_test_file_path(FILENAME);
+ constexpr const char WRITE_DATA[] = "hello, fdatasync";
+ constexpr ssize_t WRITE_SIZE = sizeof(WRITE_DATA);
+
+ int fd = LIBC_NAMESPACE::open(test_file, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ ASSERT_THAT(LIBC_NAMESPACE::write(fd, WRITE_DATA, WRITE_SIZE),
+ Succeeds(WRITE_SIZE));
+ ASSERT_THAT(LIBC_NAMESPACE::fdatasync(fd), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::unlink(test_file), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFdatasyncTest, BadFd) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+ ASSERT_THAT(LIBC_NAMESPACE::fdatasync(-1), Fails(EBADF));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/225613
More information about the libc-commits
mailing list