[libc] Switch sys/stat implementations over to libc_errno.
authorSiva Chandra Reddy <sivachandra@google.com>
Tue, 14 Mar 2023 01:12:11 +0000 (01:12 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Tue, 14 Mar 2023 04:02:45 +0000 (04:02 +0000)
Reviewed By: lntue

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

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

index 20deb79..415d2fa 100644 (file)
@@ -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
 )
index 6baf716..e187e5c 100644 (file)
@@ -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"
 
 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
index c3d5777..cd2119a 100644 (file)
@@ -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;
index 816760a..15a721b 100644 (file)
@@ -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
index 92e6618..9c3b56b 100644 (file)
@@ -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
index b5a06c8..6cedb2f 100644 (file)
@@ -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;
 }