-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc] Implement fchown #167286
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
base: main
Are you sure you want to change the base?
[libc] Implement fchown #167286
Changes from 6 commits
9f7ee38
74e5658
e9af916
2b614b4
fd262a7
3476780
63492b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===-- Implementation header for fchown ------------------------*- 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_UNISTD_FCHOWN_H_ | ||
| #define LLVM_LIBC_SRC_UNISTD_FCHOWN_H_ | ||
|
|
||
| #include "hdr/types/gid_t.h" | ||
| #include "hdr/types/uid_t.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| int fchown(int fildes, uid_t owner, gid_t group); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_UNISTD_FCHOWN_H_ | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||
| //===-- Linux implementation of fchown ------------------------------------===// | ||||||||
| // | ||||||||
| // 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/unistd/fchown.h" | ||||||||
|
|
||||||||
| #include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||||||||
| #include "src/__support/common.h" | ||||||||
|
|
||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add includes for the proxy headers here, ideally we'd include everything we use.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks! |
||||||||
| #include "src/__support/libc_errno.h" | ||||||||
| #include "src/__support/macros/config.h" | ||||||||
| #include <sys/syscall.h> // For syscall numbers. | ||||||||
|
|
||||||||
| namespace LIBC_NAMESPACE_DECL { | ||||||||
|
|
||||||||
| LLVM_LIBC_FUNCTION(int, fchown, (int fildes, uid_t owner, gid_t group)) { | ||||||||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fchown, fildes, owner, group); | ||||||||
| if (ret < 0) { | ||||||||
| libc_errno = -ret; | ||||||||
| return -1; | ||||||||
| } | ||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| } // namespace LIBC_NAMESPACE_DECL | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //===-- Unittests for fchown ----------------------------------------------===// | ||
| // | ||
| // 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/fcntl/open.h" | ||
| #include "src/unistd/close.h" | ||
| #include "src/unistd/fchown.h" | ||
| #include "src/unistd/getgid.h" | ||
| #include "src/unistd/getuid.h" | ||
| #include "src/unistd/unlink.h" | ||
|
|
||
| #include "test/UnitTest/ErrnoCheckingTest.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include "hdr/fcntl_macros.h" | ||
| #include <sys/stat.h> | ||
|
|
||
| using LlvmLibcFchownTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
|
||
| TEST_F(LlvmLibcFchownTest, FchownSuccess) { | ||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
| uid_t my_uid = LIBC_NAMESPACE::getuid(); | ||
| gid_t my_gid = LIBC_NAMESPACE::getgid(); | ||
| constexpr const char *FILENAME = "fchown.test"; | ||
| auto TEST_FILE = libc_make_test_file_path(FILENAME); | ||
|
|
||
| // Create a test file. | ||
| int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_GT(write_fd, 0); | ||
|
|
||
| // Change the ownership of the file. | ||
| ASSERT_THAT(LIBC_NAMESPACE::fchown(write_fd, my_uid, my_gid), Succeeds(0)); | ||
|
|
||
| // Close the file descriptor. | ||
| ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0)); | ||
|
|
||
| // Clean up the test file. | ||
| ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0)); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcFchownTest, ChownInvalidFileDescriptor) { | ||
|
||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
| ASSERT_THAT(LIBC_NAMESPACE::fchown(-1, 1000, 1000), Fails(EBADF)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these header guards have an extra underscore at the end that they don't need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!