8307976: (fs) Files.createDirectories(dir) returns dir::toAbsolutePath instead of dir

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-05-17 16:04:08 +00:00
parent f57c78337e
commit 6d4782bc73
2 changed files with 14 additions and 7 deletions

View File

@ -758,14 +758,15 @@ public final class Files {
// parent may not exist or other reason
}
SecurityException se = null;
Path absDir = dir;
try {
dir = dir.toAbsolutePath();
absDir = dir.toAbsolutePath();
} catch (SecurityException x) {
// don't have permission to get absolute path
se = x;
}
// find a descendant that exists
Path parent = dir.getParent();
Path parent = absDir.getParent();
while (parent != null) {
try {
provider(parent).checkAccess(parent);
@ -778,7 +779,7 @@ public final class Files {
if (parent == null) {
// unable to find existing parent
if (se == null) {
throw new FileSystemException(dir.toString(), null,
throw new FileSystemException(absDir.toString(), null,
"Unable to determine if root directory exists");
} else {
throw se;
@ -787,7 +788,7 @@ public final class Files {
// create directories
Path child = parent;
for (Path name: parent.relativize(dir)) {
for (Path name: parent.relativize(absDir)) {
child = child.resolve(name);
createAndCheckIsDirectory(child, attrs);
}

View File

@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,7 +32,7 @@ import org.testng.annotations.Test;
/*
* @test
* @bug 8032220 8293792
* @bug 8032220 8293792 8307976
* @summary Test java.nio.file.Files.createDirectories method
* @library ..
* @run testng CreateDirectories
@ -91,7 +91,8 @@ public class CreateDirectories {
public void testCreateDirectories() throws IOException {
final Path tmpdir = TestUtil.createTemporaryDirectory();
// a no-op
Files.createDirectories(tmpdir);
Path d = Files.createDirectories(tmpdir);
assertTrue(d == tmpdir, d + " != " + tmpdir);
// create one directory
Path subdir = tmpdir.resolve("a");
@ -120,5 +121,10 @@ public class CreateDirectories {
Path root = Path.of("/");
Files.createDirectories(root);
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);
}
}