Skip to content

[libc][c11] Add stdio.h's rename() function #85068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.sprintf
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.sprintf
libc.src.stdio.snprintf
libc.src.stdio.fprintf
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.sprintf
libc.src.stdio.snprintf
libc.src.stdio.fprintf
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/stdio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ These functions operate on files on the host's system, without using the
Function_Name Available
============= =========
remove |check|
rename
rename |check|
tmpnam
============= =========

Expand Down
5 changes: 5 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>]
>,
FunctionSpec<
"rename",
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>]
>,
FunctionSpec<
"setbuf",
RetValSpec<VoidType>,
Expand Down
7 changes: 7 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.remove
)

add_entrypoint_object(
rename
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.rename
)

# These entrypoints have multiple potential implementations.
add_stdio_entrypoint_object(feof)
add_stdio_entrypoint_object(feof_unlocked)
Expand Down
12 changes: 12 additions & 0 deletions libc/src/stdio/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
rename
SRCS
rename.cpp
HDRS
../rename.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
26 changes: 26 additions & 0 deletions libc/src/stdio/linux/rename.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Linux implementation of rename ------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/stdio/rename.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/errno/libc_errno.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_rename, oldpath, newpath);

if (ret >= 0)
return 0;
libc_errno = -ret;
return -1;
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdio/rename.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header of rename -------------------------*- 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_STDIO_RENAME_H
#define LLVM_LIBC_SRC_STDIO_RENAME_H

namespace LIBC_NAMESPACE {

int rename(const char *oldpath, const char *newpath);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDIO_RENAME_H
15 changes: 15 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
libc.src.unistd.access
libc.src.unistd.close
)

add_libc_test(
rename_test
SUITE
libc_stdio_unittests
SRCS
rename_test.cpp
DEPENDS
libc.src.errno.errno
libc.src.fcntl.open
libc.src.stdio.rename
libc.src.unistd.access
libc.src.unistd.close
libc.test.UnitTest.ErrnoSetterMatcher
)
endif()

add_libc_test(
Expand Down
49 changes: 49 additions & 0 deletions libc/test/src/stdio/rename_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===-- Unittests for rename ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/stdio/rename.h"
#include "src/unistd/access.h"
#include "src/unistd/close.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcRenameTest, CreateAndRenameFile) {
// The test strategy is to create a file and rename it, and also verify that
// it was renamed.
LIBC_NAMESPACE::libc_errno = 0;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;

constexpr const char *FILENAME0 = "rename.test.file0";
auto TEST_FILEPATH0 = libc_make_test_file_path(FILENAME0);

int fd = LIBC_NAMESPACE::open(TEST_FILEPATH0, O_WRONLY | O_CREAT, S_IRWXU);
ASSERT_ERRNO_SUCCESS();
ASSERT_GT(fd, 0);
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Succeeds(0));

constexpr const char *FILENAME1 = "rename.test.file1";
auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);
ASSERT_THAT(LIBC_NAMESPACE::rename(TEST_FILEPATH0, TEST_FILEPATH1),
Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH1, F_OK), Succeeds(0));
ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Fails(ENOENT));
}

TEST(LlvmLibcRenameTest, RenameNonExistent) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;

constexpr const char *FILENAME1 = "rename.test.file1";
auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);

ASSERT_THAT(LIBC_NAMESPACE::rename("non-existent", TEST_FILEPATH1),
Fails(ENOENT));
}
11 changes: 11 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3230,6 +3230,17 @@ libc_function(
],
)

libc_function(
name = "rename",
srcs = ["src/stdio/linux/rename.cpp"],
hdrs = ["src/stdio/rename.h"],
deps = [
":__support_common",
":__support_osutil_syscall",
":errno",
],
)

############################### sys/stat targets ###############################

libc_function(
Expand Down