-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mountinfo: fix PrefixFilter() being too greedy
The PrefixFilter matched on a literal "prefix", making it too greedy, and, for example, filtering on "/a" prefix would also return mounts for "/aaa" or "/abc". This patch changes the matching to match on a prefix / parent _path_ instead. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- Loading branch information
Showing
2 changed files
with
39 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mountinfo | ||
|
||
import "testing" | ||
|
||
func TestPrefixFilter(t *testing.T) { | ||
tests := []struct { | ||
prefix string | ||
mountPoint string | ||
shouldSkip bool | ||
}{ | ||
{prefix: "/a", mountPoint: "/a", shouldSkip: false}, | ||
{prefix: "/a", mountPoint: "/a/b", shouldSkip: false}, | ||
{prefix: "/a", mountPoint: "/aa", shouldSkip: true}, | ||
{prefix: "/a", mountPoint: "/aa/b", shouldSkip: true}, | ||
|
||
// invalid prefix: prefix path must be cleaned and have no trailing slash | ||
{prefix: "/a/", mountPoint: "/a", shouldSkip: true}, | ||
{prefix: "/a/", mountPoint: "/a/b", shouldSkip: true}, | ||
} | ||
for _, tc := range tests { | ||
filter := PrefixFilter(tc.prefix) | ||
skip, _ := filter(&Info{Mountpoint: tc.mountPoint}) | ||
if skip != tc.shouldSkip { | ||
if tc.shouldSkip { | ||
t.Errorf("prefix %q: expected %q to be skipped", tc.prefix, tc.mountPoint) | ||
} else { | ||
t.Errorf("prefix %q: expected %q not to be skipped", tc.prefix, tc.mountPoint) | ||
} | ||
} | ||
} | ||
} |