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

Ensure that non-glob faa path patterns are watched #212

Merged
merged 1 commit into from
Jan 16, 2025
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
20 changes: 16 additions & 4 deletions Source/santad/DataLayer/WatchItems.mm
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,26 @@ bool ParseConfig(NSDictionary *config,
return false;
}

for (size_t i = g->gl_offs; i < g->gl_pathc; i++) {
// If no paths were returned, check if the search pattern contained any magic
// characters. If not, we can go ahead and start monitoring the configured path.
if (g->gl_pathc == 0 && ((g->gl_flags & GLOB_MAGCHAR) == 0)) {
if (item->path_type == WatchItemPathType::kPrefix) {
tree_->InsertPrefix(g->gl_pathv[i], item);
tree_->InsertPrefix(item->path.c_str(), item);
} else {
tree_->InsertLiteral(g->gl_pathv[i], item);
tree_->InsertLiteral(item->path.c_str(), item);
}

paths_.insert({g->gl_pathv[i], item->path_type});
paths_.insert({item->path.c_str(), item->path_type});
} else {
for (size_t i = g->gl_offs; i < g->gl_pathc; i++) {
if (item->path_type == WatchItemPathType::kPrefix) {
tree_->InsertPrefix(g->gl_pathv[i], item);
} else {
tree_->InsertLiteral(g->gl_pathv[i], item);
}

paths_.insert({g->gl_pathv[i], item->path_type});
}
}
globfree(g);
}
Expand Down
19 changes: 19 additions & 0 deletions Source/santad/DataLayer/WatchItemsTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,25 @@ - (void)testState {
XCTAssertGreaterThanOrEqual(state.last_config_load_epoch, startTime);
}

- (void)testPathPatternExpectations {
NSMutableDictionary *config = WrapWatchItemsConfig(@{
@"rule1" : @{kWatchItemConfigKeyPaths : @[ @"abc", @"xyz*" ]},
});

WatchItemsPeer watchItems(@"my_fake_config_path", NULL, NULL);
watchItems.ReloadConfig(config);

// Ensure that non-glob patterns are watched
WatchItems::VersionAndPolicies policies = watchItems.FindPolciesForPaths({"abc"});
XCTAssertCStringEqual(policies.second[0].value_or(MakeBadPolicy())->name.c_str(), "rule1");

// Check that patterns with globs are not returned
policies = watchItems.FindPolciesForPaths({"xyz"});
XCTAssertFalse(policies.second[0].has_value());
policies = watchItems.FindPolciesForPaths({"xyzbar"});
XCTAssertFalse(policies.second[0].has_value());
}

- (void)testSetConfigAndSetConfigPath {
// Test internal state when switching back and forth between path-based and
// dictionary-based config options.
Expand Down
Loading