diff options
author | Maxim Kartashev <maxim.kartashev@jetbrains.com> | 2022-05-05 16:45:05 +0300 |
---|---|---|
committer | Maxim Kartashev <maxim.kartashev@jetbrains.com> | 2022-05-11 17:04:14 +0300 |
commit | 9a81ca792e61ecb20bad7baa441cc33f0297b937 (patch) | |
tree | 47b89832358cbff4b4e9b4b9144eea4b89f9d47a | |
parent | 626ab933a9531d97cb6cfcdf8f7845079517c955 (diff) | |
download | JetBrainsRuntime-9a81ca792e61ecb20bad7baa441cc33f0297b937.tar.gz |
JBR-3498 Windows: exception when trying to delete a directory with a trailing space
Allow Windows Path to have a trailing space despite Windows naming conventions
discouraging it. Many programs - including Explorer - successfully work
with such files or directories.
-rw-r--r-- | src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java | 10 | ||||
-rw-r--r-- | test/jdk/java/nio/file/Path/PathOps.java | 6 | ||||
-rw-r--r-- | test/jdk/java/nio/file/Path/TrailingSpace.java | 65 |
3 files changed, 68 insertions, 13 deletions
diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java b/src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java index a7afbbe0274..ea0a189dd88 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java @@ -164,14 +164,9 @@ class WindowsPathParser { int len = path.length(); off = nextNonSlash(path, off, len); int start = off; - char lastC = 0; while (off < len) { char c = path.charAt(off); if (isSlash(c)) { - if (lastC == ' ') - throw new InvalidPathException(path, - "Trailing char <" + lastC + ">", - off - 1); sb.append(path, start, off); off = nextNonSlash(path, off, len); if (off != len) //no slash at the end of normalized path @@ -182,15 +177,10 @@ class WindowsPathParser { throw new InvalidPathException(path, "Illegal char <" + c + ">", off); - lastC = c; off++; } } if (start != off) { - if (lastC == ' ') - throw new InvalidPathException(path, - "Trailing char <" + lastC + ">", - off - 1); sb.append(path, start, off); } return sb.toString(); diff --git a/test/jdk/java/nio/file/Path/PathOps.java b/test/jdk/java/nio/file/Path/PathOps.java index d6992c8ed2c..17cceedf656 100644 --- a/test/jdk/java/nio/file/Path/PathOps.java +++ b/test/jdk/java/nio/file/Path/PathOps.java @@ -1412,10 +1412,10 @@ public class PathOps { .invalid(); test("foo\u0000\bar") .invalid(); - test("C:\\foo ") // trailing space - .invalid(); + test("C:\\foo ") + .string("C:\\foo ");// trailing space test("C:\\foo \\bar") - .invalid(); + .string("C:\\foo \\bar"); //test("C:\\foo.") // trailing dot //.invalid(); //test("C:\\foo...\\bar") diff --git a/test/jdk/java/nio/file/Path/TrailingSpace.java b/test/jdk/java/nio/file/Path/TrailingSpace.java new file mode 100644 index 00000000000..9172f809a40 --- /dev/null +++ b/test/jdk/java/nio/file/Path/TrailingSpace.java @@ -0,0 +1,65 @@ +/* + * Copyright 2022 JetBrains s.r.o. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8190546 + * @summary Verifies that a path name that ends with a space can be + * successfully created. + */ + +import java.nio.file.Path; +import java.nio.file.Files; +import java.io.IOException; + +public class TrailingSpace { + static void testResolve(String parent, String... paths) { + final Path p = Path.of(parent, paths); + System.out.println("Path successfully created: " + p); + } + + static void testResolve(String path) { + final Path p = Path.of(path); + System.out.println("Path successfully created: " + p); + } + + static void testDelete(String path) throws IOException { + final Path p = Files.createDirectories(Path.of(path)); + Files.delete(p); + } + + public static void main(String args[]) throws IOException { + testResolve("./", "ends-with-space "); + testResolve("ends-with-space "); + testResolve("1", "2", "ends-with-space ", "3"); + testResolve("1\\2\\ends-with-space \\3"); + testResolve("1/2/ends-with-space /3"); + testResolve("1/2/ends-with-space \\3"); + testResolve("ends-with-space /"); + testResolve("ends-with-space ///"); + testResolve("ends-with-space \\"); + testResolve("ends-with-space \\\\\\"); + + testDelete("ends-with-space "); + testDelete("ends-with-space-1 \\"); + } +} |