-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add fstat to OsSysCalls #24298
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
Add fstat to OsSysCalls #24298
Changes from all commits
017797b
9e8d1b4
f29cf99
ec3358f
9bd5d0a
13d2137
660c8db
46afa09
64a6ab8
4ece430
5458415
6dfddbb
8a8b5c9
37d6ca0
bdbf2ea
3efd132
4b0ef0d
7e9d582
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 |
|---|---|---|
| @@ -1,9 +1,73 @@ | ||
| #ifdef WIN32 | ||
| #include <fcntl.h> // Needed for `O_CREAT` and `O_RDWR` on Win32. | ||
|
|
||
| #endif | ||
|
|
||
| #include "source/common/api/os_sys_calls_impl.h" | ||
|
|
||
| #include "test/test_common/environment.h" | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| // Test happy path for `open`, `pwrite`, `pread`, `fstat`, `close`, `stat` and `unlink`. | ||
| TEST(OsSyscallsTest, OpenPwritePreadFstatCloseStatUnlink) { | ||
|
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. ugh I really want to break these out but you can't really fully test read without write/open, ditto fstat. so yeah can leave as is but if fully testing close requires tweaking anything let's at least add line breaks between the different sections. if close is tested we can merge as-is
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. Close is [kind of] tested, in that this new test tests the new path, and existing tests implicitly cover the old path. But it turns out Windows doesn't like to unlink a file that was created without explicit windows-specific permissions flags, so that shot the auto-merge. :( |
||
| auto& os_syscalls = Api::OsSysCallsSingleton::get(); | ||
| std::string path{TestEnvironment::temporaryPath("envoy_test")}; | ||
| TestEnvironment::createPath(path); | ||
| std::string file_path = path + "/file"; | ||
| absl::string_view file_contents = "12345"; | ||
| // Test `open` | ||
| #ifdef WIN32 | ||
| int pmode = _S_IWRITE | _S_IREAD; | ||
| #else | ||
| int pmode = S_IRUSR | S_IWUSR; | ||
| #endif | ||
| Api::SysCallIntResult open_result = os_syscalls.open(file_path.c_str(), O_CREAT | O_RDWR, pmode); | ||
| EXPECT_NE(open_result.return_value_, -1); | ||
| EXPECT_EQ(open_result.errno_, 0); | ||
| os_fd_t fd = open_result.return_value_; | ||
| #ifdef WIN32 | ||
| // `pwrite` and `pread` are not supported. Just write some bytes so we can still test stat. | ||
| EXPECT_EQ(file_contents.size(), ::_write(fd, file_contents.begin(), file_contents.size())); | ||
| #else | ||
| // Test `pwrite` | ||
| Api::SysCallSizeResult write_result = | ||
| os_syscalls.pwrite(fd, file_contents.begin(), file_contents.size(), 0); | ||
| EXPECT_EQ(write_result.return_value_, file_contents.size()); | ||
| EXPECT_EQ(write_result.errno_, 0); | ||
| // Test `pread` | ||
| char read_buffer[5]; | ||
| Api::SysCallSizeResult read_result = os_syscalls.pread(fd, read_buffer, sizeof(read_buffer), 0); | ||
| EXPECT_EQ(read_result.return_value_, sizeof(read_buffer)); | ||
| EXPECT_EQ(read_result.errno_, 0); | ||
| absl::string_view read_buffer_view{read_buffer, sizeof(read_buffer)}; | ||
| EXPECT_EQ(file_contents, read_buffer_view); | ||
| #endif | ||
| // Test `fstat` | ||
| struct stat fstat_value; | ||
| Api::SysCallIntResult fstat_result = os_syscalls.fstat(fd, &fstat_value); | ||
| EXPECT_EQ(fstat_result.return_value_, 0); | ||
| EXPECT_EQ(fstat_result.errno_, 0); | ||
| EXPECT_EQ(fstat_value.st_size, file_contents.size()); | ||
| // Test `close` | ||
| Api::SysCallIntResult close_result = os_syscalls.close(fd); | ||
| EXPECT_EQ(close_result.return_value_, 0); | ||
| EXPECT_EQ(close_result.errno_, 0); | ||
| // Test `stat` | ||
| struct stat stat_value; | ||
| Api::SysCallIntResult stat_result = os_syscalls.stat(file_path.c_str(), &stat_value); | ||
| EXPECT_EQ(stat_result.return_value_, 0); | ||
| EXPECT_EQ(stat_result.errno_, 0); | ||
| EXPECT_EQ(stat_value.st_size, file_contents.size()); | ||
| // Test `unlink` | ||
| Api::SysCallIntResult unlink_result = os_syscalls.unlink(file_path.c_str()); | ||
| EXPECT_EQ(unlink_result.return_value_, 0); | ||
| EXPECT_EQ(unlink_result.errno_, 0); | ||
| TestEnvironment::removePath(path); | ||
| } | ||
|
|
||
| TEST(OsSyscallsTest, SetAlternateGetifaddrs) { | ||
| auto& os_syscalls = Api::OsSysCallsSingleton::get(); | ||
| const bool pre_alternate_support = os_syscalls.supportsGetifaddrs(); | ||
|
|
||
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.
this doesn't appear to be called out in the release note. is it tested in the new test?
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.
Discussed in Slack; in existing code close is only called where closesocket behavior is fine. My hope is to later change this back and make it explicitly closesocket (for all platforms), and remove everything I've done here after implementing [fstat, pread, pwrite, tmpfiles] in
Envoy::Filesystem::Fileand migratingAsyncFileManagerto using that. But getting it in here allows me to make progress elsewhere while that chain of changes is in flight.