-
Notifications
You must be signed in to change notification settings - Fork 25.6k
FileAccessTree fixes for ordering and pruning #123291
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
|
|
@@ -46,8 +47,8 @@ private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup) | |
| readPaths.add(tempDir); | ||
| writePaths.add(tempDir); | ||
|
|
||
| readPaths.sort(String::compareTo); | ||
| writePaths.sort(String::compareTo); | ||
| readPaths.sort(PATH_ORDER); | ||
| writePaths.sort(PATH_ORDER); | ||
|
|
||
| this.readPaths = pruneSortedPaths(readPaths).toArray(new String[0]); | ||
| this.writePaths = pruneSortedPaths(writePaths).toArray(new String[0]); | ||
|
|
@@ -60,7 +61,7 @@ private static List<String> pruneSortedPaths(List<String> paths) { | |
| prunedReadPaths.add(currentPath); | ||
| for (int i = 1; i < paths.size(); ++i) { | ||
| String nextPath = paths.get(i); | ||
| if (nextPath.startsWith(currentPath) == false) { | ||
| if (isParent(currentPath, nextPath) == false) { | ||
| prunedReadPaths.add(nextPath); | ||
| currentPath = nextPath; | ||
| } | ||
|
|
@@ -88,21 +89,28 @@ static String normalizePath(Path path) { | |
| // Note that toAbsolutePath produces paths separated by the default file separator, | ||
| // so on Windows, if the given path uses forward slashes, this consistently | ||
| // converts it to backslashes. | ||
| return path.toAbsolutePath().normalize().toString(); | ||
| String result = path.toAbsolutePath().normalize().toString(); | ||
| while (result.endsWith(FILE_SEPARATOR)) { | ||
| result = result.substring(0, result.length() - FILE_SEPARATOR.length()); | ||
|
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. The fix to strip trailing separators |
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static boolean checkPath(String path, String[] paths) { | ||
| if (paths.length == 0) { | ||
| return false; | ||
| } | ||
| int ndx = Arrays.binarySearch(paths, path); | ||
| int ndx = Arrays.binarySearch(paths, path, PATH_ORDER); | ||
| if (ndx < -1) { | ||
| String maybeParent = paths[-ndx - 2]; | ||
| return path.startsWith(maybeParent) && path.startsWith(FILE_SEPARATOR, maybeParent.length()); | ||
| return isParent(paths[-ndx - 2], path); | ||
| } | ||
| return ndx >= 0; | ||
| } | ||
|
|
||
| private static boolean isParent(String maybeParent, String path) { | ||
| return path.startsWith(maybeParent) && path.startsWith(FILE_SEPARATOR, maybeParent.length()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
@@ -114,4 +122,30 @@ public boolean equals(Object o) { | |
| public int hashCode() { | ||
| return Objects.hash(Arrays.hashCode(readPaths), Arrays.hashCode(writePaths)); | ||
| } | ||
|
|
||
| /** | ||
| * For our lexicographic sort trick to work correctly, we must have path separators sort before | ||
| * any other character so that files in a directory appear immediately after that directory. | ||
| * For example, we require [/a, /a/b, /a.xml] rather than the natural order [/a, /a.xml, /a/b]. | ||
| */ | ||
| private static final Comparator<String> PATH_ORDER = (s1, s2) -> { | ||
| Path p1 = Path.of(s1); | ||
| Path p2 = Path.of(s2); | ||
| var i1 = p1.iterator(); | ||
| var i2 = p2.iterator(); | ||
| while (i1.hasNext() && i2.hasNext()) { | ||
| int cmp = i1.next().compareTo(i2.next()); | ||
| if (cmp != 0) { | ||
| return cmp; | ||
| } | ||
| } | ||
| if (i1.hasNext()) { | ||
| return 1; | ||
| } else if (i2.hasNext()) { | ||
| return -1; | ||
| } else { | ||
| assert p1.equals(p2); | ||
| return 0; | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The pruning fix.