forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc][c11] Add stdio.h's rename() function (llvm#85068)
Adds stdio.h's rename() function as defined in n3096. Fixes llvm#84980.
- Loading branch information
Showing
12 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters