Skip to content

Commit

Permalink
[libc] Switch sys/stat implementations over to libc_errno.
Browse files Browse the repository at this point in the history
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D146004
  • Loading branch information
Siva Chandra Reddy committed Mar 14, 2023
1 parent a1f8bab commit 55612b8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
4 changes: 3 additions & 1 deletion libc/src/sys/stat/linux/CMakeLists.txt
Expand Up @@ -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(
Expand All @@ -87,6 +86,7 @@ add_entrypoint_object(
.kernel_statx
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
)

add_entrypoint_object(
Expand All @@ -99,6 +99,7 @@ add_entrypoint_object(
.kernel_statx
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
)

add_entrypoint_object(
Expand All @@ -111,4 +112,5 @@ add_entrypoint_object(
.kernel_statx
libc.include.fcntl
libc.include.sys_stat
libc.src.errno.errno
)
8 changes: 7 additions & 1 deletion libc/src/sys/stat/linux/fstat.cpp
Expand Up @@ -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"

Expand All @@ -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
7 changes: 2 additions & 5 deletions libc/src/sys/stat/linux/kernel_statx.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion libc/src/sys/stat/linux/lstat.cpp
Expand Up @@ -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"
Expand All @@ -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
8 changes: 7 additions & 1 deletion libc/src/sys/stat/linux/stat.cpp
Expand Up @@ -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"

Expand All @@ -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
10 changes: 5 additions & 5 deletions libc/test/src/sys/stat/lstat_test.cpp
Expand Up @@ -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"
Expand All @@ -14,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>

Expand All @@ -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;
Expand All @@ -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;
}

0 comments on commit 55612b8

Please sign in to comment.