Skip to content
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

Replace use of readdir()/dirent.d_type with stat()/st_mode. #1874

Merged
merged 5 commits into from
Oct 17, 2017

Conversation

ggreenway
Copy link
Contributor

d_type is documented as only working with some filesystems, and that
all programs must handle a value of DT_UNKNOWN and make a stat() call
to retrieve the full information. To keep the code simpler, always make
the stat() call.

Signed-off-by: Greg Greenway ggreenway@apple.com

d_type is documented as only working with some filesystems, and that
all programs must handle a value of DT_UNKNOWN and make a stat() call
to retrieve the full information.  To keep the code simpler, always make
the stat() call.

Signed-off-by: Greg Greenway <ggreenway@apple.com>
Signed-off-by: Greg Greenway <ggreenway@apple.com>
struct stat stat_result;
int rc = ::stat(full_path.c_str(), &stat_result);
if (rc != 0) {
throw EnvoyException(fmt::format("unable to stat file: '{}'", full_path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this case? Perhaps move to utility function so can test with a bogus path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add some test coverage if I move stat() to OsSysCalls.

I think in the real world, the only way this fails is if someone deletes/moves the file after readdir() has returned it but before we stat() it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or make it RELEASE_ASSERT. Up to you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I test hack would be to just move the stat call to a small helper, and then pass in a bad file name, and I think it should fail and throw).

@@ -72,12 +75,20 @@ class RuntimeImplTest : public testing::Test {
EXPECT_CALL(dispatcher, createFilesystemWatcher_())
.WillOnce(ReturnNew<NiceMock<Filesystem::MockWatcher>>());

if (os_sys_calls_ == nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does setup actually get called more than once in a test? Otherwise I would just ASSERT it's nullptr or just let it leak and fail the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that was my hack so I could create this and set an EXPECT_CALL before this function creates the LoaderImpl. A cleaner way to do it would be to have a setup() and run() function, and insert my EXPECT_CALL between the two. Want me to do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just remove the if check. We do this all over the place in tests. If it leaks the leak checker will find it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nevermind. I see. Yeah I would switch to setup() and run() if you don't mind.

Signed-off-by: Greg Greenway <ggreenway@apple.com>
mattklein123
mattklein123 previously approved these changes Oct 17, 2017
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

if (recursive && entry->d_type == DT_DIR && std::string(entry->d_name) != "." &&
struct stat stat_result;
int rc = ::stat(file_name.c_str(), &stat_result);
EXPECT_EQ(rc, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would make this ASSERT_EQ, since if this fails, the code below is using junk.

Signed-off-by: Greg Greenway <ggreenway@apple.com>
htuch
htuch previously approved these changes Oct 17, 2017
…tions

Signed-off-by: Greg Greenway <ggreenway@apple.com>
@htuch htuch merged commit ce51d88 into envoyproxy:master Oct 17, 2017
rshriram pushed a commit to rshriram/envoy that referenced this pull request Oct 30, 2018
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
jpsim added a commit that referenced this pull request Nov 28, 2022
#1874)

Description: When running on iOS 12 or later, use the more modern and more featureful alternative to `SCNetworkReachability` from the Network framework.
Risk Level: Medium. It's likely that this new approach to observing network reachability will be more correct and reliable than the `SCNetworkReachability` approach, but it's still possible for differences in behavior to cause issues where the wrong radio type is selected on devices running iOS 12 or later. To mitigate, the new path monitor is opt-in only.
Testing: I've tested this on a physical iPhone, confirming that the logs about switching preferred network matches when either the reachability or path monitor mechanisms are used. Unit tests cover that appending `.enableNetworkPathMonitor()` to the builder commands flips the `useNetworkPathMonitor` flag to true.
Docs Changes: Added.
Release Notes: Added.

Signed-off-by: JP Simard <jp@jpsim.com>
jpsim added a commit that referenced this pull request Nov 29, 2022
#1874)

Description: When running on iOS 12 or later, use the more modern and more featureful alternative to `SCNetworkReachability` from the Network framework.
Risk Level: Medium. It's likely that this new approach to observing network reachability will be more correct and reliable than the `SCNetworkReachability` approach, but it's still possible for differences in behavior to cause issues where the wrong radio type is selected on devices running iOS 12 or later. To mitigate, the new path monitor is opt-in only.
Testing: I've tested this on a physical iPhone, confirming that the logs about switching preferred network matches when either the reachability or path monitor mechanisms are used. Unit tests cover that appending `.enableNetworkPathMonitor()` to the builder commands flips the `useNetworkPathMonitor` flag to true.
Docs Changes: Added.
Release Notes: Added.

Signed-off-by: JP Simard <jp@jpsim.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants