8307976: (fs) Files.createDirectories(dir) returns dir::toAbsolutePath instead of dir
Reviewed-by: alanb
This commit is contained in:
parent
f57c78337e
commit
6d4782bc73
src/java.base/share/classes/java/nio/file
test/jdk/java/nio/file/Files
@ -758,14 +758,15 @@ public final class Files {
|
|||||||
// parent may not exist or other reason
|
// parent may not exist or other reason
|
||||||
}
|
}
|
||||||
SecurityException se = null;
|
SecurityException se = null;
|
||||||
|
Path absDir = dir;
|
||||||
try {
|
try {
|
||||||
dir = dir.toAbsolutePath();
|
absDir = dir.toAbsolutePath();
|
||||||
} catch (SecurityException x) {
|
} catch (SecurityException x) {
|
||||||
// don't have permission to get absolute path
|
// don't have permission to get absolute path
|
||||||
se = x;
|
se = x;
|
||||||
}
|
}
|
||||||
// find a descendant that exists
|
// find a descendant that exists
|
||||||
Path parent = dir.getParent();
|
Path parent = absDir.getParent();
|
||||||
while (parent != null) {
|
while (parent != null) {
|
||||||
try {
|
try {
|
||||||
provider(parent).checkAccess(parent);
|
provider(parent).checkAccess(parent);
|
||||||
@ -778,7 +779,7 @@ public final class Files {
|
|||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
// unable to find existing parent
|
// unable to find existing parent
|
||||||
if (se == null) {
|
if (se == null) {
|
||||||
throw new FileSystemException(dir.toString(), null,
|
throw new FileSystemException(absDir.toString(), null,
|
||||||
"Unable to determine if root directory exists");
|
"Unable to determine if root directory exists");
|
||||||
} else {
|
} else {
|
||||||
throw se;
|
throw se;
|
||||||
@ -787,7 +788,7 @@ public final class Files {
|
|||||||
|
|
||||||
// create directories
|
// create directories
|
||||||
Path child = parent;
|
Path child = parent;
|
||||||
for (Path name: parent.relativize(dir)) {
|
for (Path name: parent.relativize(absDir)) {
|
||||||
child = child.resolve(name);
|
child = child.resolve(name);
|
||||||
createAndCheckIsDirectory(child, attrs);
|
createAndCheckIsDirectory(child, attrs);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -32,7 +32,7 @@ import org.testng.annotations.Test;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8032220 8293792
|
* @bug 8032220 8293792 8307976
|
||||||
* @summary Test java.nio.file.Files.createDirectories method
|
* @summary Test java.nio.file.Files.createDirectories method
|
||||||
* @library ..
|
* @library ..
|
||||||
* @run testng CreateDirectories
|
* @run testng CreateDirectories
|
||||||
@ -91,7 +91,8 @@ public class CreateDirectories {
|
|||||||
public void testCreateDirectories() throws IOException {
|
public void testCreateDirectories() throws IOException {
|
||||||
final Path tmpdir = TestUtil.createTemporaryDirectory();
|
final Path tmpdir = TestUtil.createTemporaryDirectory();
|
||||||
// a no-op
|
// a no-op
|
||||||
Files.createDirectories(tmpdir);
|
Path d = Files.createDirectories(tmpdir);
|
||||||
|
assertTrue(d == tmpdir, d + " != " + tmpdir);
|
||||||
|
|
||||||
// create one directory
|
// create one directory
|
||||||
Path subdir = tmpdir.resolve("a");
|
Path subdir = tmpdir.resolve("a");
|
||||||
@ -120,5 +121,10 @@ public class CreateDirectories {
|
|||||||
Path root = Path.of("/");
|
Path root = Path.of("/");
|
||||||
Files.createDirectories(root);
|
Files.createDirectories(root);
|
||||||
Files.createDirectories(root.toAbsolutePath());
|
Files.createDirectories(root.toAbsolutePath());
|
||||||
|
|
||||||
|
// the returned path should not be absolute
|
||||||
|
Path temp = Path.of(".temp/temp.abc/temp.def");
|
||||||
|
Path a = Files.createDirectories(temp);
|
||||||
|
assertTrue(a == temp, a + " != " + temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user