Skip to content

Commit 3d618d0

Browse files
committed
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. (cherry picked from commit 64a4682)
1 parent 0edfb46 commit 3d618d0

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

src/java.base/windows/classes/sun/nio/fs/WindowsPathParser.java

-10
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,9 @@ private static String normalize(String root, String path, int pathOff) {
171171
// whether the element at that position is a slash
172172
int pathPos = srcPos;
173173

174-
char lastC = 0;
175174
while (pathPos < result.length) {
176175
char c = result[pathPos];
177176
if (isSlash(c)) {
178-
if (lastC == ' ')
179-
throw new InvalidPathException(path,
180-
"Trailing char <" + lastC + ">",
181-
pathPos - 1);
182177
int nchars = pathPos - srcPos;
183178
System.arraycopy(result, srcPos, result, dstPos, nchars);
184179
dstPos += nchars;
@@ -191,15 +186,10 @@ private static String normalize(String root, String path, int pathOff) {
191186
throw new InvalidPathException(path,
192187
"Illegal char <" + c + ">",
193188
pathPos);
194-
lastC = c;
195189
pathPos++;
196190
}
197191
}
198192
if (srcPos != pathPos) {
199-
if (lastC == ' ')
200-
throw new InvalidPathException(path,
201-
"Trailing char <" + lastC + ">",
202-
pathPos - 1);
203193
int nchars = pathPos - srcPos;
204194
System.arraycopy(result, srcPos, result, dstPos, nchars);
205195
dstPos += nchars;

test/jdk/java/nio/file/Path/PathOps.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1509,10 +1509,10 @@ static void doWindowsTests() {
15091509
.invalid();
15101510
test("foo\u0000\bar")
15111511
.invalid();
1512-
test("C:\\foo ") // trailing space
1513-
.invalid();
1512+
test("C:\\foo ")
1513+
.string("C:\\foo ");// trailing space
15141514
test("C:\\foo \\bar")
1515-
.invalid();
1515+
.string("C:\\foo \\bar");
15161516
//test("C:\\foo.") // trailing dot
15171517
//.invalid();
15181518
//test("C:\\foo...\\bar")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2022 JetBrains s.r.o.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
* @bug 8190546
26+
* @summary Verifies that a path name that ends with a space can be
27+
* successfully created.
28+
*/
29+
30+
import java.nio.file.Path;
31+
import java.nio.file.Files;
32+
import java.io.IOException;
33+
34+
public class TrailingSpace {
35+
static void testResolve(String parent, String... paths) {
36+
final Path p = Path.of(parent, paths);
37+
System.out.println("Path successfully created: " + p);
38+
}
39+
40+
static void testResolve(String path) {
41+
final Path p = Path.of(path);
42+
System.out.println("Path successfully created: " + p);
43+
}
44+
45+
static void testDelete(String path) throws IOException {
46+
final Path p = Files.createDirectories(Path.of(path));
47+
Files.delete(p);
48+
}
49+
50+
public static void main(String args[]) throws IOException {
51+
testResolve("./", "ends-with-space ");
52+
testResolve("ends-with-space ");
53+
testResolve("1", "2", "ends-with-space ", "3");
54+
testResolve("1\\2\\ends-with-space \\3");
55+
testResolve("1/2/ends-with-space /3");
56+
testResolve("1/2/ends-with-space \\3");
57+
testResolve("ends-with-space /");
58+
testResolve("ends-with-space ///");
59+
testResolve("ends-with-space \\");
60+
testResolve("ends-with-space \\\\\\");
61+
62+
testDelete("ends-with-space ");
63+
testDelete("ends-with-space-1 \\");
64+
}
65+
}

0 commit comments

Comments
 (0)