Skip to content
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
12 changes: 12 additions & 0 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@
* /foo/../bar/ --> /bar/
* /foo/../bar/../baz --> /baz
* //foo//./bar --> /foo/bar
* /foo/bar/.. --> /foo
* /../ --> null
* </pre>
*
Expand All @@ -1029,6 +1030,13 @@
*/
public static String normalize(final String path) {
String normalized = path;
// A trailing "/." or "/.." is a segment too: add the slash the loops below look for, and drop it again after
boolean addedTrailingSlash = false;
if (normalized.startsWith("/") && (normalized.endsWith("/.") || normalized.endsWith("/.."))) {
normalized = normalized + "/";
addedTrailingSlash = true;
}

// Resolve occurrences of "//" in the normalized path
while (true) {
int index = normalized.indexOf("//");
Expand Down Expand Up @@ -1060,6 +1068,10 @@
normalized = normalized.substring(0, index2) + normalized.substring(index + 3);
}

if (addedTrailingSlash && normalized.length() > 1) {
normalized = normalized.substring(0, normalized.length() - 1);
}

// Return the normalized path that we have completed
return normalized;
}
Expand Down Expand Up @@ -1953,9 +1965,9 @@
fileReader = null;
} finally {
//noinspection deprecation
IOUtil.close(fileReader);

Check warning on line 1968 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-26.04 jdk-25-zulu 3.10.0

close(java.io.Reader) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1968 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / macos-latest jdk-25-zulu 3.10.0

close(java.io.Reader) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1968 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-25-zulu 3.10.0

close(java.io.Reader) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1968 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-8-zulu 3.10.0

close(java.io.Reader) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1968 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-21-zulu 3.10.0

close(java.io.Reader) in org.codehaus.plexus.util.IOUtil has been deprecated
//noinspection deprecation
IOUtil.close(fileWriter);

Check warning on line 1970 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-26.04 jdk-25-zulu 3.10.0

close(java.io.Writer) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1970 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / macos-latest jdk-25-zulu 3.10.0

close(java.io.Writer) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1970 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-25-zulu 3.10.0

close(java.io.Writer) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1970 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-8-zulu 3.10.0

close(java.io.Writer) in org.codehaus.plexus.util.IOUtil has been deprecated

Check warning on line 1970 in src/main/java/org/codehaus/plexus/util/FileUtils.java

View workflow job for this annotation

GitHub Actions / Build it / Verify / ubuntu-latest jdk-21-zulu 3.10.0

close(java.io.Writer) in org.codehaus.plexus.util.IOUtil has been deprecated
}
} else {
if (isSourceNewerThanDestination(from, to) || overwrite) {
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,12 @@ void normalize() {
"/foo/.././bar/",
"//foo//./bar",
"/../",
"/foo/../../"
"/foo/../../",
"/foo/.",
"/foo/..",
"/foo/bar/..",
"/..",
"/foo/../.."
};

final String[] dest = {
Expand All @@ -578,6 +583,11 @@ void normalize() {
"/bar/",
"/foo/bar",
null,
null,
"/foo",
"/",
"/foo",
null,
null
};

Expand Down
Loading