Skip to content

Commit 38957a2

Browse files
committed
extract separator
1 parent 3862969 commit 38957a2

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/CaseInsensitiveComparison.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
class CaseInsensitiveComparison extends FileAccessTreeComparison {
1313

14-
CaseInsensitiveComparison() {
15-
super(CaseInsensitiveComparison::caseInsensitiveCharacterComparator);
14+
CaseInsensitiveComparison(char separatorChar) {
15+
super(CaseInsensitiveComparison::caseInsensitiveCharacterComparator, separatorChar);
1616
}
1717

1818
/**

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/CaseSensitiveComparison.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
class CaseSensitiveComparison extends FileAccessTreeComparison {
1313

14-
CaseSensitiveComparison() {
15-
super(Character::compare);
14+
CaseSensitiveComparison(char separatorChar) {
15+
super(Character::compare, separatorChar);
1616
}
1717

1818
@Override

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
import org.elasticsearch.core.Nullable;
1313
import org.elasticsearch.core.Strings;
14+
import org.elasticsearch.core.SuppressForbidden;
1415
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement;
1516
import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode;
1617
import org.elasticsearch.logging.LogManager;
1718
import org.elasticsearch.logging.Logger;
1819

20+
import java.io.File;
1921
import java.io.IOException;
2022
import java.io.UncheckedIOException;
2123
import java.nio.file.Files;
@@ -172,11 +174,16 @@ static void validateExclusivePaths(List<ExclusivePath> exclusivePaths, FileAcces
172174
}
173175
}
174176

177+
@SuppressForbidden(reason = "we need the separator as a char, not a string")
178+
private static char separatorChar() {
179+
return File.separatorChar;
180+
}
181+
175182
private static final Logger logger = LogManager.getLogger(FileAccessTree.class);
176183
private static final String FILE_SEPARATOR = getDefaultFileSystem().getSeparator();
177184
static final FileAccessTreeComparison DEFAULT_COMPARISON = Platform.LINUX.isCurrent()
178-
? new CaseSensitiveComparison()
179-
: new CaseInsensitiveComparison();
185+
? new CaseSensitiveComparison(separatorChar())
186+
: new CaseInsensitiveComparison(separatorChar());
180187

181188
private final FileAccessTreeComparison comparison;
182189
/**

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeComparison.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import org.elasticsearch.core.Strings;
13-
import org.elasticsearch.core.SuppressForbidden;
1413
import org.elasticsearch.logging.LogManager;
1514
import org.elasticsearch.logging.Logger;
1615

17-
import java.io.File;
1816
import java.util.Comparator;
1917

2018
abstract class FileAccessTreeComparison {
@@ -27,12 +25,14 @@ interface CharComparator {
2725

2826
private final Comparator<String> pathComparator;
2927

28+
private final char separatorChar;
29+
3030
/**
3131
* For our lexicographic sort trick to work correctly, we must have path separators sort before
3232
* any other character so that files in a directory appear immediately after that directory.
3333
* For example, we require [/a, /a/b, /a.xml] rather than the natural order [/a, /a.xml, /a/b].
3434
*/
35-
FileAccessTreeComparison(CharComparator charComparator) {
35+
FileAccessTreeComparison(CharComparator charComparator, char separatorChar) {
3636
pathComparator = (s1, s2) -> {
3737
int len1 = s1.length();
3838
int len2 = s2.length();
@@ -44,8 +44,8 @@ interface CharComparator {
4444
if (comp == 0) {
4545
continue;
4646
}
47-
boolean c1IsSeparator = isPathSeparator(c1);
48-
boolean c2IsSeparator = isPathSeparator(c2);
47+
boolean c1IsSeparator = c1 == separatorChar;
48+
boolean c2IsSeparator = c2 == separatorChar;
4949
if (c1IsSeparator == false || c2IsSeparator == false) {
5050
if (c1IsSeparator) {
5151
return -1;
@@ -58,17 +58,17 @@ interface CharComparator {
5858
}
5959
return len1 - len2;
6060
};
61+
this.separatorChar = separatorChar;
6162
}
6263

6364
Comparator<String> pathComparator() {
6465
return pathComparator;
6566
}
6667

67-
@SuppressForbidden(reason = "we need the separator as a char, not a string")
6868
boolean isParent(String maybeParent, String path) {
6969
logger.trace(() -> Strings.format("checking isParent [%s] for [%s]", maybeParent, path));
7070
return pathStartsWith(path, maybeParent)
71-
&& (path.length() > maybeParent.length() && path.charAt(maybeParent.length()) == File.separatorChar);
71+
&& (path.length() > maybeParent.length() && path.charAt(maybeParent.length()) == separatorChar);
7272
}
7373

7474
boolean samePath(String currentPath, String nextPath) {
@@ -78,9 +78,4 @@ boolean samePath(String currentPath, String nextPath) {
7878
protected abstract boolean pathStartsWith(String pathString, String pathPrefix);
7979

8080
protected abstract boolean pathsAreEqual(String path1, String path2);
81-
82-
@SuppressForbidden(reason = "we need the separator as a char, not a string")
83-
private static boolean isPathSeparator(char c) {
84-
return c == File.separatorChar;
85-
}
8681
}

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeComparisonTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class FileAccessTreeComparisonTests extends ESTestCase {
2020

2121
public void testPathOrderPosix() {
22-
var pathComparator = new CaseSensitiveComparison().pathComparator();
22+
var pathComparator = new CaseSensitiveComparison('/').pathComparator();
2323

2424
// Unix-style
2525
// Directories come BEFORE files; note that this differs from natural lexicographical order
@@ -43,8 +43,8 @@ public void testPathOrderPosix() {
4343
assertThat(pathComparator.compare("/a\\b", "/a/b.txt"), greaterThan(0));
4444
}
4545

46-
public void testPathOrderWindowsOrMac() {
47-
var pathComparator = new CaseInsensitiveComparison().pathComparator();
46+
public void testPathOrderWindows() {
47+
var pathComparator = new CaseInsensitiveComparison('\\').pathComparator();
4848

4949
// Directories come BEFORE files; note that this differs from natural lexicographical order
5050
assertThat(pathComparator.compare("C:\\a\\b", "C:\\a.xml"), lessThan(0));
@@ -56,7 +56,10 @@ public void testPathOrderWindowsOrMac() {
5656
}
5757

5858
public void testPathOrderingSpecialCharacters() {
59-
var pathComparator = (randomBoolean() ? new CaseInsensitiveComparison() : new CaseSensitiveComparison()).pathComparator();
59+
var separatorChar = randomFrom('/', '\\');
60+
var pathComparator = (randomBoolean()
61+
? new CaseInsensitiveComparison(separatorChar)
62+
: new CaseSensitiveComparison(separatorChar)).pathComparator();
6063

6164
assertThat(pathComparator.compare("aa\uD801\uDC28", "aa\uD801\uDC28"), is(0));
6265
assertThat(pathComparator.compare("aa\uD801\uDC28", "aa\uD801\uDC28a"), lessThan(0));

libs/entitlement/src/test/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTreeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public void testInvalidExclusiveAccess() {
363363
}
364364

365365
public void testDuplicatePrunedPaths() {
366-
var comparison = new CaseSensitiveComparison();
366+
var comparison = new CaseSensitiveComparison('/');
367367
List<String> inputPaths = List.of("/a", "/a", "/a/b", "/a/b", "/b/c", "b/c/d", "b/c/d", "b/c/d", "e/f", "e/f");
368368
List<String> outputPaths = List.of("/a", "/b/c", "b/c/d", "e/f");
369369
var actual = FileAccessTree.pruneSortedPaths(inputPaths.stream().map(p -> normalizePath(path(p))).toList(), comparison);
@@ -372,7 +372,7 @@ public void testDuplicatePrunedPaths() {
372372
}
373373

374374
public void testDuplicatePrunedPathsWindows() {
375-
var comparison = new CaseInsensitiveComparison();
375+
var comparison = new CaseInsensitiveComparison('/');
376376
List<String> inputPaths = List.of("/a", "/A", "/a/b", "/a/B", "/b/c", "b/c/d", "B/c/d", "b/c/D", "e/f", "e/f");
377377
List<String> outputPaths = List.of("/a", "/b/c", "b/c/d", "e/f");
378378
var actual = FileAccessTree.pruneSortedPaths(inputPaths.stream().map(p -> normalizePath(path(p))).toList(), comparison);
@@ -384,7 +384,7 @@ public void testDuplicateExclusivePaths() {
384384
// Bunch o' handy definitions
385385
var pathAB = path("/a/b");
386386
var pathCD = path("/c/d");
387-
var comparison = randomBoolean() ? new CaseSensitiveComparison() : new CaseInsensitiveComparison();
387+
var comparison = randomBoolean() ? new CaseSensitiveComparison('/') : new CaseInsensitiveComparison('/');
388388
var originalFileData = FileData.ofPath(pathAB, READ).withExclusive(true);
389389
var fileDataWithWriteMode = FileData.ofPath(pathAB, READ_WRITE).withExclusive(true);
390390
var original = new ExclusiveFileEntitlement("component1", "module1", new FilesEntitlement(List.of(originalFileData)));

0 commit comments

Comments
 (0)