Skip to content

Commit 9ec1fc7

Browse files
authored
Internal: Remove Strings.cleanPath (#25209)
This commit removes the cleanPath method, in favor of using java's Path.normalize().
1 parent 1bd5cec commit 9ec1fc7

File tree

3 files changed

+6
-84
lines changed

3 files changed

+6
-84
lines changed

core/src/main/java/org/elasticsearch/common/Strings.java

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ public class Strings {
4949

5050
public static final String[] EMPTY_ARRAY = new String[0];
5151

52-
private static final String FOLDER_SEPARATOR = "/";
53-
54-
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
55-
56-
private static final String TOP_PATH = "src/test";
57-
58-
private static final String CURRENT_PATH = ".";
59-
6052
public static void spaceify(int spaces, String from, StringBuilder to) throws Exception {
6153
try (BufferedReader reader = new BufferedReader(new FastStringReader(from))) {
6254
String line;
@@ -403,66 +395,6 @@ public static boolean validFileNameExcludingAstrix(String fileName) {
403395
return true;
404396
}
405397

406-
/**
407-
* Normalize the path by suppressing sequences like "path/.." and
408-
* inner simple dots.
409-
* <p>The result is convenient for path comparison. For other uses,
410-
* notice that Windows separators ("\") are replaced by simple slashes.
411-
*
412-
* @param path the original path
413-
* @return the normalized path
414-
*/
415-
public static String cleanPath(String path) {
416-
if (path == null) {
417-
return null;
418-
}
419-
String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
420-
421-
// Strip prefix from path to analyze, to not treat it as part of the
422-
// first path element. This is necessary to correctly parse paths like
423-
// "file:core/../core/io/Resource.class", where the ".." should just
424-
// strip the first "core" directory while keeping the "file:" prefix.
425-
int prefixIndex = pathToUse.indexOf(":");
426-
String prefix = "";
427-
if (prefixIndex != -1) {
428-
prefix = pathToUse.substring(0, prefixIndex + 1);
429-
pathToUse = pathToUse.substring(prefixIndex + 1);
430-
}
431-
if (pathToUse.startsWith(FOLDER_SEPARATOR)) {
432-
prefix = prefix + FOLDER_SEPARATOR;
433-
pathToUse = pathToUse.substring(1);
434-
}
435-
436-
String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR);
437-
List<String> pathElements = new LinkedList<>();
438-
int tops = 0;
439-
440-
for (int i = pathArray.length - 1; i >= 0; i--) {
441-
String element = pathArray[i];
442-
if (CURRENT_PATH.equals(element)) {
443-
// Points to current directory - drop it.
444-
} else if (TOP_PATH.equals(element)) {
445-
// Registering top path found.
446-
tops++;
447-
} else {
448-
if (tops > 0) {
449-
// Merging path element with element corresponding to top path.
450-
tops--;
451-
} else {
452-
// Normal path element found.
453-
pathElements.add(0, element);
454-
}
455-
}
456-
}
457-
458-
// Remaining top paths need to be retained.
459-
for (int i = 0; i < tops; i++) {
460-
pathElements.add(0, TOP_PATH);
461-
}
462-
463-
return prefix + collectionToDelimitedString(pathElements, FOLDER_SEPARATOR);
464-
}
465-
466398
/**
467399
* Copy the given Collection into a String array.
468400
* The Collection must contain String elements only.

core/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@
2727
import org.elasticsearch.common.settings.Settings;
2828

2929
import java.io.IOException;
30-
import java.io.UncheckedIOException;
3130
import java.net.MalformedURLException;
3231
import java.net.URISyntaxException;
3332
import java.net.URL;
3433
import java.nio.file.FileStore;
3534
import java.nio.file.Files;
3635
import java.nio.file.Path;
37-
import java.util.ArrayList;
3836
import java.util.Collections;
39-
import java.util.HashMap;
4037
import java.util.List;
41-
import java.util.Map;
4238
import java.util.Objects;
4339
import java.util.function.Function;
4440

45-
import static org.elasticsearch.common.Strings.cleanPath;
46-
4741
/**
4842
* The environment of where things exists.
4943
*/
@@ -100,14 +94,14 @@ public class Environment {
10094
public Environment(Settings settings) {
10195
final Path homeFile;
10296
if (PATH_HOME_SETTING.exists(settings)) {
103-
homeFile = PathUtils.get(cleanPath(PATH_HOME_SETTING.get(settings)));
97+
homeFile = PathUtils.get(PATH_HOME_SETTING.get(settings)).normalize();
10498
} else {
10599
throw new IllegalStateException(PATH_HOME_SETTING.getKey() + " is not configured");
106100
}
107101

108102
// this is trappy, Setting#get(Settings) will get a fallback setting yet return false for Settings#exists(Settings)
109103
if (PATH_CONF_SETTING.exists(settings) || DEFAULT_PATH_CONF_SETTING.exists(settings)) {
110-
configFile = PathUtils.get(cleanPath(PATH_CONF_SETTING.get(settings)));
104+
configFile = PathUtils.get(PATH_CONF_SETTING.get(settings)).normalize();
111105
} else {
112106
configFile = homeFile.resolve("config");
113107
}
@@ -128,7 +122,7 @@ public Environment(Settings settings) {
128122
dataWithClusterFiles = new Path[]{homeFile.resolve("data").resolve(clusterName.value())};
129123
}
130124
if (PATH_SHARED_DATA_SETTING.exists(settings)) {
131-
sharedDataFile = PathUtils.get(cleanPath(PATH_SHARED_DATA_SETTING.get(settings)));
125+
sharedDataFile = PathUtils.get(PATH_SHARED_DATA_SETTING.get(settings)).normalize();
132126
} else {
133127
sharedDataFile = null;
134128
}
@@ -144,13 +138,13 @@ public Environment(Settings settings) {
144138

145139
// this is trappy, Setting#get(Settings) will get a fallback setting yet return false for Settings#exists(Settings)
146140
if (PATH_LOGS_SETTING.exists(settings) || DEFAULT_PATH_LOGS_SETTING.exists(settings)) {
147-
logsFile = PathUtils.get(cleanPath(PATH_LOGS_SETTING.get(settings)));
141+
logsFile = PathUtils.get(PATH_LOGS_SETTING.get(settings)).normalize();
148142
} else {
149143
logsFile = homeFile.resolve("logs");
150144
}
151145

152146
if (PIDFILE_SETTING.exists(settings)) {
153-
pidFile = PathUtils.get(cleanPath(PIDFILE_SETTING.get(settings)));
147+
pidFile = PathUtils.get(PIDFILE_SETTING.get(settings)).normalize();
154148
} else {
155149
pidFile = null;
156150
}

core/src/main/java/org/elasticsearch/node/InternalSettingsPreparer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
import java.nio.file.Path;
2525
import java.util.ArrayList;
2626
import java.util.Collections;
27-
import java.util.HashSet;
2827
import java.util.List;
2928
import java.util.Map;
30-
import java.util.Set;
3129
import java.util.function.Function;
3230

3331
import org.elasticsearch.cli.Terminal;
@@ -38,8 +36,6 @@
3836
import org.elasticsearch.common.settings.SettingsException;
3937
import org.elasticsearch.env.Environment;
4038

41-
import static org.elasticsearch.common.Strings.cleanPath;
42-
4339
public class InternalSettingsPreparer {
4440

4541
private static final String[] ALLOWED_SUFFIXES = {".yml", ".yaml", ".json"};
@@ -111,7 +107,7 @@ public static Environment prepareEnvironment(Settings input, Terminal terminal,
111107
environment = new Environment(output.build());
112108

113109
// we put back the path.logs so we can use it in the logging configuration file
114-
output.put(Environment.PATH_LOGS_SETTING.getKey(), cleanPath(environment.logsFile().toAbsolutePath().toString()));
110+
output.put(Environment.PATH_LOGS_SETTING.getKey(), environment.logsFile().toAbsolutePath().normalize().toString());
115111
return new Environment(output.build());
116112
}
117113

0 commit comments

Comments
 (0)