This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6748b800bb7d069644907921a65c7e0d3f696fc3
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Oct 23 21:44:54 2026 +0200

    libs/libc/unistd: Make fdatasync() a function.
    
    fdatasync() was the macro "#define fdatasync(f) fsync(f)" in <unistd.h>
    and libc had no fdatasync symbol.  POSIX requires a function: a program
    cannot take its address, and a link test does not find it.  A program
    that then brings its own fallback "int fdatasync(int fd) { return
    fsync(fd); }" defines "int fsync(int fd) { return fsync(fd); }" instead:
    an endless loop that replaces fsync() in the whole program.
    
    Add fdatasync() to libc.  It calls fsync(): the file systems have only
    one sync operation.  Remove the macro.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/unistd.h                 |  5 +---
 libs/libc/libc.csv               |  1 +
 libs/libc/unistd/CMakeLists.txt  |  1 +
 libs/libc/unistd/Make.defs       |  3 ++-
 libs/libc/unistd/lib_fdatasync.c | 53 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 7d949a5e1bd..4a6f382f435 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -287,10 +287,6 @@
 #define STDIN_FILENO                     0       /* File number of stdin */
 #define STDOUT_FILENO                    1       /* File number of stdout */
 
-/* Helpers and legacy compatibility definitions */
-
-#define fdatasync(f)                     fsync(f)
-
 /* Accessor functions associated with getopt().  getopt() keeps optarg,
  * opterr, optind and optopt per task, so the names are macros.  A program
  * that brings its own getopt() and its own variables of these names can
@@ -388,6 +384,7 @@ int     close(int fd);
 int     dup(int fd);
 int     dup2(int fd1, int fd2);
 int     dup3(int fd1, int fd2, int flags);
+int     fdatasync(int fd);
 int     fsync(int fd);
 off_t   lseek(int fd, off_t offset, int whence);
 ssize_t read(int fd, FAR void *buf, size_t nbytes);
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 53f49225e80..8984f1576c6 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -64,6 +64,7 @@
 "exit","stdlib.h","","noreturn","int"
 "fchdir","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","int","int"
 "fclose","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
+"fdatasync","unistd.h","","int","int"
 "fdopen","stdio.h","defined(CONFIG_FILE_STREAM)","FAR FILE *","int","FAR const 
char *"
 "feof","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
 "ferror","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt
index 261dec9516e..312406e8108 100644
--- a/libs/libc/unistd/CMakeLists.txt
+++ b/libs/libc/unistd/CMakeLists.txt
@@ -42,6 +42,7 @@ set(SRCS
     lib_getoptoptp.c
     lib_times.c
     lib_alarm.c
+    lib_fdatasync.c
     lib_fstatvfs.c
     lib_statvfs.c
     lib_sleep.c
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index a4849f3e8d2..2f77ff220ca 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -27,7 +27,8 @@ CSRCS += lib_copy_file_range.c
 CSRCS += lib_getcwd.c lib_getentropy.c lib_getopt_common.c lib_getopt.c
 CSRCS += lib_getopt_long.c lib_getopt_longonly.c lib_getoptvars.c 
lib_getoptargp.c
 CSRCS += lib_getopterrp.c lib_getoptindp.c lib_getoptoptp.c lib_times.c
-CSRCS += lib_alarm.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c lib_nice.c
+CSRCS += lib_alarm.c lib_fdatasync.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c
+CSRCS += lib_nice.c
 CSRCS += lib_getrusage.c lib_utime.c lib_utimes.c lib_getdtablesize.c
 CSRCS += lib_getpagesize.c lib_setpriority.c lib_getpriority.c
 CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c lib_sethostname.c
diff --git a/libs/libc/unistd/lib_fdatasync.c b/libs/libc/unistd/lib_fdatasync.c
new file mode 100644
index 00000000000..c72524ef2b2
--- /dev/null
+++ b/libs/libc/unistd/lib_fdatasync.c
@@ -0,0 +1,53 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_fdatasync.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdatasync
+ *
+ * Description:
+ *   Write the data of the file to the storage device.  The file systems
+ *   have only one sync operation, which also writes the metadata, so this
+ *   is fsync().  fsync() is the cancellation point.
+ *
+ * Input Parameters:
+ *   fd - The file descriptor of the file
+ *
+ * Returned Value:
+ *   0 on success.  On failure, -1 is returned and errno is set as by
+ *   fsync().
+ *
+ ****************************************************************************/
+
+int fdatasync(int fd)
+{
+  return fsync(fd);
+}

Reply via email to