Skip to content

Commit 6d4782b

Browse files
author
Brian Burkhalter
committed
8307976: (fs) Files.createDirectories(dir) returns dir::toAbsolutePath instead of dir
Reviewed-by: alanb
1 parent f57c783 commit 6d4782b

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/java.base/share/classes/java/nio/file/Files.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,15 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
758758
// parent may not exist or other reason
759759
}
760760
SecurityException se = null;
761+
Path absDir = dir;
761762
try {
762-
dir = dir.toAbsolutePath();
763+
absDir = dir.toAbsolutePath();
763764
} catch (SecurityException x) {
764765
// don't have permission to get absolute path
765766
se = x;
766767
}
767768
// find a descendant that exists
768-
Path parent = dir.getParent();
769+
Path parent = absDir.getParent();
769770
while (parent != null) {
770771
try {
771772
provider(parent).checkAccess(parent);
@@ -778,7 +779,7 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
778779
if (parent == null) {
779780
// unable to find existing parent
780781
if (se == null) {
781-
throw new FileSystemException(dir.toString(), null,
782+
throw new FileSystemException(absDir.toString(), null,
782783
"Unable to determine if root directory exists");
783784
} else {
784785
throw se;
@@ -787,7 +788,7 @@ public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
787788

788789
// create directories
789790
Path child = parent;
790-
for (Path name: parent.relativize(dir)) {
791+
for (Path name: parent.relativize(absDir)) {
791792
child = child.resolve(name);
792793
createAndCheckIsDirectory(child, attrs);
793794
}

test/jdk/java/nio/file/Files/CreateDirectories.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,7 @@
3232

3333
/*
3434
* @test
35-
* @bug 8032220 8293792
35+
* @bug 8032220 8293792 8307976
3636
* @summary Test java.nio.file.Files.createDirectories method
3737
* @library ..
3838
* @run testng CreateDirectories
@@ -91,7 +91,8 @@ public void testSymlinkDir() throws Exception {
9191
public void testCreateDirectories() throws IOException {
9292
final Path tmpdir = TestUtil.createTemporaryDirectory();
9393
// a no-op
94-
Files.createDirectories(tmpdir);
94+
Path d = Files.createDirectories(tmpdir);
95+
assertTrue(d == tmpdir, d + " != " + tmpdir);
9596

9697
// create one directory
9798
Path subdir = tmpdir.resolve("a");
@@ -120,5 +121,10 @@ public void testCreateDirectories() throws IOException {
120121
Path root = Path.of("/");
121122
Files.createDirectories(root);
122123
Files.createDirectories(root.toAbsolutePath());
124+
125+
// the returned path should not be absolute
126+
Path temp = Path.of(".temp/temp.abc/temp.def");
127+
Path a = Files.createDirectories(temp);
128+
assertTrue(a == temp, a + " != " + temp);
123129
}
124130
}

0 commit comments

Comments
 (0)