This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 07233c313e2 libs/libc: Support AT_EMPTY_PATH in fchmodat() and
fchownat()
07233c313e2 is described below
commit 07233c313e25c1e345bdcbe759c92db4120b2cca
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 3 02:21:51 2026 +0100
libs/libc: Support AT_EMPTY_PATH in fchmodat() and fchownat()
fstatat() takes AT_EMPTY_PATH (the file of dirfd itself), but fchmodat()
and fchownat() look up the empty path and fail with ENOENT. A program
that sets the mode of an open file with fchmodat(fd, "", mode,
AT_EMPTY_PATH) gets this error.
With an empty path and AT_EMPTY_PATH, fchmodat() now calls fchmod() on
dirfd and fchownat() calls fchown(), as fstatat() calls fstat(). With
AT_FDCWD they operate on the current working directory.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
libs/libc/misc/lib_fchmodat.c | 16 +++++++++++++++-
libs/libc/unistd/lib_fchownat.c | 16 +++++++++++++++-
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/libs/libc/misc/lib_fchmodat.c b/libs/libc/misc/lib_fchmodat.c
index 5083bdb19ae..be770f44d91 100644
--- a/libs/libc/misc/lib_fchmodat.c
+++ b/libs/libc/misc/lib_fchmodat.c
@@ -52,11 +52,15 @@
*
* If pathname is absolute, then dirfd is ignored.
*
+ * If pathname is an empty string and flags contains AT_EMPTY_PATH, then
+ * the call operates on the file referred to by dirfd (like fchmod()). If
+ * dirfd is AT_FDCWD, the call operates on the current working directory.
+ *
* Input Parameters:
* dirfd - The file descriptor of directory.
* path - A pointer to the path.
* mode - The access mode.
- * flags - Ignored.
+ * flags - AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH are supported.
*
* Returned Value:
* Return zero on success, or -1 if an error occurred (in which case,
@@ -69,6 +73,16 @@ int fchmodat(int dirfd, FAR const char *path, mode_t mode,
int flags)
FAR char *fullpath;
int ret;
+ if (path != NULL && path[0] == '\0' && (flags & AT_EMPTY_PATH) != 0)
+ {
+ if (dirfd != AT_FDCWD)
+ {
+ return fchmod(dirfd, mode);
+ }
+
+ path = ".";
+ }
+
fullpath = lib_get_pathbuffer();
if (fullpath == NULL)
{
diff --git a/libs/libc/unistd/lib_fchownat.c b/libs/libc/unistd/lib_fchownat.c
index 75b62d85e70..613cee75488 100644
--- a/libs/libc/unistd/lib_fchownat.c
+++ b/libs/libc/unistd/lib_fchownat.c
@@ -52,12 +52,16 @@
*
* If pathname is absolute, then dirfd is ignored.
*
+ * If pathname is an empty string and flags contains AT_EMPTY_PATH, then
+ * the call operates on the file referred to by dirfd (like fchown()). If
+ * dirfd is AT_FDCWD, the call operates on the current working directory.
+ *
* Input Parameters:
* dirfd - The file descriptor of directory.
* path - A pointer to the path.
* owner - The owner to set.
* group - The group to set.
- * flags - Ignored.
+ * flags - AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH are supported.
*
* Returned Value:
* Return zero on success, or -1 if an error occurred (in which case,
@@ -71,6 +75,16 @@ int fchownat(int dirfd, FAR const char *path, uid_t owner,
FAR char *fullpath;
int ret;
+ if (path != NULL && path[0] == '\0' && (flags & AT_EMPTY_PATH) != 0)
+ {
+ if (dirfd != AT_FDCWD)
+ {
+ return fchown(dirfd, owner, group);
+ }
+
+ path = ".";
+ }
+
fullpath = lib_get_pathbuffer();
if (fullpath == NULL)
{