[libc-commits] [libc] 55612b8 - [libc] Switch sys/stat implementations over to libc_errno.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Mon Mar 13 21:03:08 PDT 2023


Author: Siva Chandra Reddy
Date: 2023-03-14T04:02:45Z
New Revision: 55612b8ec4210ae9c67658cb83393e8b4e8169d4

URL: https://github.com/llvm/llvm-project/commit/55612b8ec4210ae9c67658cb83393e8b4e8169d4
DIFF: https://github.com/llvm/llvm-project/commit/55612b8ec4210ae9c67658cb83393e8b4e8169d4.diff

LOG: [libc] Switch sys/stat implementations over to libc_errno.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D146004

Added: 
    

Modified: 
    libc/src/sys/stat/linux/CMakeLists.txt
    libc/src/sys/stat/linux/fstat.cpp
    libc/src/sys/stat/linux/kernel_statx.h
    libc/src/sys/stat/linux/lstat.cpp
    libc/src/sys/stat/linux/stat.cpp
    libc/test/src/sys/stat/lstat_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/sys/stat/linux/CMakeLists.txt b/libc/src/sys/stat/linux/CMakeLists.txt
index 20deb795fde30..415d2fa5c8771 100644
--- a/libc/src/sys/stat/linux/CMakeLists.txt
+++ b/libc/src/sys/stat/linux/CMakeLists.txt
@@ -74,7 +74,6 @@ add_header_library(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.__support.common
-    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -87,6 +86,7 @@ add_entrypoint_object(
     .kernel_statx
     libc.include.fcntl
     libc.include.sys_stat
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -99,6 +99,7 @@ add_entrypoint_object(
     .kernel_statx
     libc.include.fcntl
     libc.include.sys_stat
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
@@ -111,4 +112,5 @@ add_entrypoint_object(
     .kernel_statx
     libc.include.fcntl
     libc.include.sys_stat
+    libc.src.errno.errno
 )

diff  --git a/libc/src/sys/stat/linux/fstat.cpp b/libc/src/sys/stat/linux/fstat.cpp
index 6baf716157ee4..e187e5cd48eb6 100644
--- a/libc/src/sys/stat/linux/fstat.cpp
+++ b/libc/src/sys/stat/linux/fstat.cpp
@@ -8,6 +8,7 @@
 
 #include "src/sys/stat/fstat.h"
 #include "kernel_statx.h"
+#include "src/errno/libc_errno.h"
 
 #include "src/__support/common.h"
 
@@ -17,7 +18,12 @@
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(int, fstat, (int fd, struct stat *statbuf)) {
-  return statx(fd, "", AT_EMPTY_PATH, statbuf);
+  int err = statx(fd, "", AT_EMPTY_PATH, statbuf);
+  if (err != 0) {
+    libc_errno = err;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/sys/stat/linux/kernel_statx.h b/libc/src/sys/stat/linux/kernel_statx.h
index c3d5777eb9781..cd2119a58e130 100644
--- a/libc/src/sys/stat/linux/kernel_statx.h
+++ b/libc/src/sys/stat/linux/kernel_statx.h
@@ -12,7 +12,6 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
-#include <errno.h>
 #include <stdint.h>
 #include <sys/stat.h>
 #include <sys/syscall.h> // For syscall numbers.
@@ -76,10 +75,8 @@ LIBC_INLINE int statx(int dirfd, const char *__restrict path, int flags,
   ::statx_buf xbuf;
   long ret = __llvm_libc::syscall_impl(SYS_statx, dirfd, path, flags,
                                        ::STATX_BASIC_STATS_MASK, &xbuf);
-  if (ret < 0) {
-    errno = -ret;
-    return -1;
-  }
+  if (ret < 0)
+    return -ret;
 
   statbuf->st_dev = MKDEV(xbuf.stx_dev_major, xbuf.stx_dev_minor);
   statbuf->st_ino = xbuf.stx_ino;

diff  --git a/libc/src/sys/stat/linux/lstat.cpp b/libc/src/sys/stat/linux/lstat.cpp
index 816760a8c1452..15a721ba784bc 100644
--- a/libc/src/sys/stat/linux/lstat.cpp
+++ b/libc/src/sys/stat/linux/lstat.cpp
@@ -8,6 +8,7 @@
 
 #include "src/sys/stat/lstat.h"
 #include "kernel_statx.h"
+#include "src/errno/libc_errno.h"
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
@@ -20,7 +21,12 @@ namespace __llvm_libc {
 LLVM_LIBC_FUNCTION(int, lstat,
                    (const char *__restrict path,
                     struct stat *__restrict statbuf)) {
-  return statx(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, statbuf);
+  int err = statx(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, statbuf);
+  if (err != 0) {
+    libc_errno = err;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/sys/stat/linux/stat.cpp b/libc/src/sys/stat/linux/stat.cpp
index 92e661898aa33..9c3b56bff4746 100644
--- a/libc/src/sys/stat/linux/stat.cpp
+++ b/libc/src/sys/stat/linux/stat.cpp
@@ -8,6 +8,7 @@
 
 #include "src/sys/stat/stat.h"
 #include "kernel_statx.h"
+#include "src/errno/libc_errno.h"
 
 #include "src/__support/common.h"
 
@@ -19,7 +20,12 @@ namespace __llvm_libc {
 LLVM_LIBC_FUNCTION(int, stat,
                    (const char *__restrict path,
                     struct stat *__restrict statbuf)) {
-  return statx(AT_FDCWD, path, 0, statbuf);
+  int err = statx(AT_FDCWD, path, 0, statbuf);
+  if (err != 0) {
+    libc_errno = err;
+    return -1;
+  }
+  return 0;
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/test/src/sys/stat/lstat_test.cpp b/libc/test/src/sys/stat/lstat_test.cpp
index b5a06c82d5fc7..6cedb2fc5fcf8 100644
--- a/libc/test/src/sys/stat/lstat_test.cpp
+++ b/libc/test/src/sys/stat/lstat_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/errno/libc_errno.h"
 #include "src/fcntl/open.h"
 #include "src/sys/stat/lstat.h"
 #include "src/unistd/close.h"
@@ -14,7 +15,6 @@
 #include "test/UnitTest/Test.h"
 #include "utils/testutils/FDReader.h"
 
-#include <errno.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 
@@ -27,11 +27,11 @@ TEST(LlvmLibcLStatTest, CreatAndReadMode) {
   // make it readonly using chmod. We test that chmod actually succeeded by
   // trying to open the file for writing and failing.
   constexpr const char *TEST_FILE = "testdata/lstat.test";
-  errno = 0;
+  libc_errno = 0;
 
   int fd = __llvm_libc::open(TEST_FILE, O_CREAT | O_WRONLY, S_IRWXU);
   ASSERT_GT(fd, 0);
-  ASSERT_EQ(errno, 0);
+  ASSERT_EQ(libc_errno, 0);
   ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
 
   struct stat statbuf;
@@ -43,9 +43,9 @@ TEST(LlvmLibcLStatTest, CreatAndReadMode) {
 }
 
 TEST(LlvmLibcLStatTest, NonExistentFile) {
-  errno = 0;
+  libc_errno = 0;
   using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
   struct stat statbuf;
   ASSERT_THAT(__llvm_libc::lstat("non-existent-file", &statbuf), Fails(ENOENT));
-  errno = 0;
+  libc_errno = 0;
 }


        


More information about the libc-commits mailing list