[libc-commits] [libc] [libc] Implement statx in sys/stat (#240022) (PR #240031)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 12:43:37 PST 2026


https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/240031

This PR implements `statx` in `<sys/stat.h>`.

- Connects public `statx` entrypoint to internal Linux syscall wrapper.
- Adds `struct statx` and `struct statx_timestamp` definitions to `llvm-libc-types` and `hdr/types`.
- Registers public declaration in `libc/include/sys/stat.yaml`.
- Registers entrypoints across Linux targets (`x86_64`, `aarch64`, `riscv`).
- Adds unit tests in `libc/test/src/sys/stat/statx_test.cpp`.

Fixes #240022.

>From fcc47e39dfb85eb6bf9e2964192a39e23b2f5647 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 02:12:46 +0530
Subject: [PATCH] [libc] Implement statx in sys/stat (#240022)

Implement standard Linux statx in <sys/stat.h>.

- Connects public statx entrypoint to internal syscall wrapper.
- Adds struct_statx definitions to llvm-libc-types and hdr/types.
- Registers public declaration in libc/include/sys/stat.yaml.
- Registers entrypoints across Linux targets (x86_64, aarch64, riscv).
- Adds unit tests in libc/test/src/sys/stat/statx_test.cpp.

Fixes #240022.
---
 libc/config/linux/aarch64/entrypoints.txt   |  1 +
 libc/config/linux/riscv/entrypoints.txt     |  1 +
 libc/config/linux/x86_64/entrypoints.txt    |  1 +
 libc/hdr/types/CMakeLists.txt               |  8 +++
 libc/hdr/types/struct_statx.h               | 27 ++++++++
 libc/include/llvm-libc-types/CMakeLists.txt |  1 +
 libc/include/llvm-libc-types/struct_statx.h | 46 ++++++++++++
 libc/include/sys/stat.yaml                  | 11 +++
 libc/src/sys/stat/CMakeLists.txt            |  7 ++
 libc/src/sys/stat/linux/CMakeLists.txt      | 14 ++++
 libc/src/sys/stat/linux/statx.cpp           | 34 +++++++++
 libc/src/sys/stat/statx.h                   | 22 ++++++
 libc/test/src/sys/stat/CMakeLists.txt       | 20 ++++++
 libc/test/src/sys/stat/statx_test.cpp       | 77 +++++++++++++++++++++
 14 files changed, 270 insertions(+)
 create mode 100644 libc/hdr/types/struct_statx.h
 create mode 100644 libc/include/llvm-libc-types/struct_statx.h
 create mode 100644 libc/src/sys/stat/linux/statx.cpp
 create mode 100644 libc/src/sys/stat/statx.h
 create mode 100644 libc/test/src/sys/stat/statx_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8ba560989af6af..32cd47474bc771 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -389,6 +389,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mknod
     libc.src.sys.stat.mknodat
     libc.src.sys.stat.stat
+    libc.src.sys.stat.statx
     libc.src.sys.stat.umask
     libc.src.sys.stat.utimensat
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 56813aaf66a91f..ab01a8f4c03435 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -427,6 +427,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mknod
     libc.src.sys.stat.mknodat
     libc.src.sys.stat.stat
+    libc.src.sys.stat.statx
     libc.src.sys.stat.umask
     libc.src.sys.stat.utimensat
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 25b24740400b97..7344d6464bd43e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -432,6 +432,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mknod
     libc.src.sys.stat.mknodat
     libc.src.sys.stat.stat
+    libc.src.sys.stat.statx
     libc.src.sys.stat.umask
     libc.src.sys.stat.utimensat
 
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index f28639d9d5623f..32c5ad1e234948 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -226,6 +226,14 @@ add_proxy_header_library(
     libc.include.llvm-libc-types.struct_stat
 )
 
+add_proxy_header_library(
+  struct_statx
+  HDRS
+    struct_statx.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_statx
+)
+
 add_proxy_header_library(
   struct_statfs
   HDRS
diff --git a/libc/hdr/types/struct_statx.h b/libc/hdr/types/struct_statx.h
new file mode 100644
index 00000000000000..bbd9893119d2bd
--- /dev/null
+++ b/libc/hdr/types/struct_statx.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy header for struct statx.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_STATX_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_STATX_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_statx.h"
+
+#else // Overlay mode
+
+#include <sys/stat.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_STATX_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 548d1cc92e5033..e8396194eb196b 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -232,6 +232,7 @@ add_header(
     .dev_t .ino_t .mode_t .nlink_t .uid_t .gid_t .off_t .struct_timespec
     .blksize_t .blkcnt_t
 )
+add_header(struct_statx HDR struct_statx.h)
 add_header(struct_tm HDR struct_tm.h)
 add_header(struct_passwd HDR struct_passwd.h DEPENDS .uid_t .gid_t)
 add_header(struct_group HDR struct_group.h DEPENDS .gid_t)
diff --git a/libc/include/llvm-libc-types/struct_statx.h b/libc/include/llvm-libc-types/struct_statx.h
new file mode 100644
index 00000000000000..11b5801b8c6cc0
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_statx.h
@@ -0,0 +1,46 @@
+//===-- Definition of struct statx ----------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_STATX_H
+#define LLVM_LIBC_TYPES_STRUCT_STATX_H
+
+#include <stdint.h>
+
+struct statx_timestamp {
+  int64_t tv_sec;
+  uint32_t tv_nsec;
+  int32_t __statx_timestamp_pad1[1];
+};
+
+struct statx {
+  uint32_t stx_mask;
+  uint32_t stx_blksize;
+  uint64_t stx_attributes;
+  uint32_t stx_nlink;
+  uint32_t stx_uid;
+  uint32_t stx_gid;
+  uint16_t stx_mode;
+  uint16_t __statx_pad1[1];
+  uint64_t stx_ino;
+  uint64_t stx_size;
+  uint64_t stx_blocks;
+  uint64_t stx_attributes_mask;
+  struct statx_timestamp stx_atime;
+  struct statx_timestamp stx_btime;
+  struct statx_timestamp stx_ctime;
+  struct statx_timestamp stx_mtime;
+  uint32_t stx_rdev_major;
+  uint32_t stx_rdev_minor;
+  uint32_t stx_dev_major;
+  uint32_t stx_dev_minor;
+  uint64_t stx_mnt_id;
+  uint64_t __statx_pad2;
+  uint64_t __statx_pad3[12];
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_STATX_H
diff --git a/libc/include/sys/stat.yaml b/libc/include/sys/stat.yaml
index 718aff2946ea2f..3322d32986dec5 100644
--- a/libc/include/sys/stat.yaml
+++ b/libc/include/sys/stat.yaml
@@ -17,6 +17,7 @@ types:
   - type_name: struct_timespec
   - type_name: ino_t
   - type_name: mode_t
+  - type_name: struct_statx
 enums: []
 objects: []
 functions:
@@ -94,6 +95,16 @@ functions:
     arguments:
       - type: const char *__restrict
       - type: struct stat *__restrict
+  - name: statx
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: int
+      - type: const char *__restrict
+      - type: int
+      - type: unsigned int
+      - type: struct statx *__restrict
   - name: umask
     return_type: mode_t
     arguments:
diff --git a/libc/src/sys/stat/CMakeLists.txt b/libc/src/sys/stat/CMakeLists.txt
index d07efde057eb43..e96a4761faacaf 100644
--- a/libc/src/sys/stat/CMakeLists.txt
+++ b/libc/src/sys/stat/CMakeLists.txt
@@ -93,6 +93,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.stat
 )
 
+add_entrypoint_object(
+  statx
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.statx
+)
+
 add_entrypoint_object(
   umask
   ALIAS
diff --git a/libc/src/sys/stat/linux/CMakeLists.txt b/libc/src/sys/stat/linux/CMakeLists.txt
index 78fc6bc1ef03d5..d8987ab0d6b75c 100644
--- a/libc/src/sys/stat/linux/CMakeLists.txt
+++ b/libc/src/sys/stat/linux/CMakeLists.txt
@@ -204,6 +204,20 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  statx
+  SRCS
+    statx.cpp
+  HDRS
+    ../statx.h
+  DEPENDS
+    libc.hdr.types.struct_statx
+    libc.src.__support.OSUtil.linux.syscall_wrappers.statx
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   umask
   SRCS
diff --git a/libc/src/sys/stat/linux/statx.cpp b/libc/src/sys/stat/linux/statx.cpp
new file mode 100644
index 00000000000000..c5fc4cc93fed75
--- /dev/null
+++ b/libc/src/sys/stat/linux/statx.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 statx.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/stat/statx.h"
+#include "hdr/types/struct_statx.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/statx.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, statx,
+                   (int dirfd, const char *__restrict path, int flags,
+                    unsigned int mask, struct statx *__restrict statxbuf)) {
+  auto result = linux_syscalls::statx(dirfd, path, flags, mask, statxbuf);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/stat/statx.h b/libc/src/sys/stat/statx.h
new file mode 100644
index 00000000000000..54e8725b40fd3a
--- /dev/null
+++ b/libc/src/sys/stat/statx.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for statx ----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_STAT_STATX_H
+#define LLVM_LIBC_SRC_SYS_STAT_STATX_H
+
+#include "hdr/types/struct_statx.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int statx(int dirfd, const char *__restrict path, int flags, unsigned int mask,
+          struct statx *__restrict statxbuf);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_STAT_STATX_H
diff --git a/libc/test/src/sys/stat/CMakeLists.txt b/libc/test/src/sys/stat/CMakeLists.txt
index c4ed2e7b11b5f1..019ba2eeba5553 100644
--- a/libc/test/src/sys/stat/CMakeLists.txt
+++ b/libc/test/src/sys/stat/CMakeLists.txt
@@ -241,6 +241,26 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
+add_libc_test(
+  statx_test
+  SUITE
+    libc-sys-stat-tests
+  SRCS
+    statx_test.cpp
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.sys_stat_macros
+    libc.hdr.types.mode_t
+    libc.hdr.types.struct_statx
+    libc.src.__support.CPP.scope
+    libc.src.sys.stat.statx
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.unlink
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   utimensat_test
   SUITE
diff --git a/libc/test/src/sys/stat/statx_test.cpp b/libc/test/src/sys/stat/statx_test.cpp
new file mode 100644
index 00000000000000..dd1c8d0aaf7246
--- /dev/null
+++ b/libc/test/src/sys/stat/statx_test.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 statx.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/types/mode_t.h"
+#include "hdr/types/struct_statx.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/open.h"
+#include "src/sys/stat/statx.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 namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcStatxTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcStatxTest, StatxWithAtFdcwd) {
+  constexpr const char *TEST_FILE = "testdata/statx.test";
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
+  ASSERT_GT(fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+  LIBC_NAMESPACE::cpp::scope_exit cleanup(
+      [&] { EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); });
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  struct statx statxbuf;
+  ASSERT_THAT(LIBC_NAMESPACE::statx(AT_FDCWD, TEST_FILE, 0, 0, &statxbuf),
+              Succeeds(0));
+
+  ASSERT_EQ(statxbuf.stx_mode, static_cast<uint16_t>(S_IRWXU | S_IFREG));
+}
+
+TEST_F(LlvmLibcStatxTest, StatxWithDirFd) {
+  constexpr const char *TEST_DIR = "testdata";
+  constexpr const char *TEST_FILE = "testdata/statx_dir.test";
+  constexpr const char *TEST_FILE_BASENAME = "statx_dir.test";
+
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
+  ASSERT_GT(fd, 0);
+  ASSERT_ERRNO_SUCCESS();
+  LIBC_NAMESPACE::cpp::scope_exit cleanup_file(
+      [&] { EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); });
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+  int dirfd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
+  ASSERT_GT(dirfd, 0);
+  ASSERT_ERRNO_SUCCESS();
+  LIBC_NAMESPACE::cpp::scope_exit cleanup_dir(
+      [&] { EXPECT_THAT(LIBC_NAMESPACE::close(dirfd), Succeeds(0)); });
+
+  struct statx statxbuf;
+  ASSERT_THAT(LIBC_NAMESPACE::statx(dirfd, TEST_FILE_BASENAME, 0, 0, &statxbuf),
+              Succeeds(0));
+
+  ASSERT_EQ(statxbuf.stx_mode, static_cast<uint16_t>(S_IRWXU | S_IFREG));
+}
+
+TEST_F(LlvmLibcStatxTest, StatxNonExistentFile) {
+  struct statx statxbuf;
+  EXPECT_THAT(
+      LIBC_NAMESPACE::statx(AT_FDCWD, "non_existent_file", 0, 0, &statxbuf),
+      Fails<int>(ENOENT));
+}



More information about the libc-commits mailing list