Skip to content
forked from openjdk/jdk

Commit

Permalink
8307976: (fs) Files.createDirectories(dir) returns dir::toAbsolutePat…
Browse files Browse the repository at this point in the history
…h instead of dir

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed May 17, 2023
1 parent f57c783 commit 6d4782b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/java.base/share/classes/java/nio/file/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,15 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
// 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);
Expand All @@ -778,7 +779,7 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
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;
Expand All @@ -787,7 +788,7 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)

// create directories
Path child = parent;
for (Path name: parent.relativize(dir)) {
for (Path name: parent.relativize(absDir)) {
child = child.resolve(name);
createAndCheckIsDirectory(child, attrs);
}
Expand Down
12 changes: 9 additions & 3 deletions test/jdk/java/nio/file/Files/CreateDirectories.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,7 +32,7 @@

/*
* @test
* @bug 8032220 8293792
* @bug 8032220 8293792 8307976
* @summary Test java.nio.file.Files.createDirectories method
* @library ..
* @run testng CreateDirectories
Expand Down Expand Up @@ -91,7 +91,8 @@ public void testSymlinkDir() throws Exception {
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");
Expand Down Expand Up @@ -120,5 +121,10 @@ public void testCreateDirectories() throws IOException {
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);
}
}

0 comments on commit 6d4782b

Please sign in to comment.