Skip to content

Commit 46c2aba

Browse files
committed
[IO-796] FileAlreadyExistsException in
PathUtils.createParentDirectories(Path, LinkOption, FileAttribute<?>...)
1 parent 6a71042 commit 46c2aba

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ The <action> type attribute can be add,update,fix,remove.
5858
<action dev="ggregory" type="fix" due-to="Gary Gregory">
5959
AbstractOriginSupplier.checkOrigin() now throws IllegalStateException instead of NullPointerException.
6060
</action>
61+
<action issue="IO-796" dev="ggregory" type="fix" due-to="Giacomo Boccardo, Gary Gregory">
62+
FileAlreadyExistsException in PathUtils.createParentDirectories(Path, LinkOption, FileAttribute...).
63+
</action>
6164
<!-- ADD -->
6265
<action dev="ggregory" type="add" due-to="Gary Gregory">
6366
Add CharSequenceInputStream.Builder.

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ public static PathCounters countDirectoryAsBigInteger(final Path directory) thro
356356

357357
/**
358358
* Creates the parent directories for the given {@code path}.
359+
* <p>
360+
* If the parent directory already exists, then return it.
361+
* <p>
359362
*
360363
* @param path The path to a file (or directory).
361364
* @param attrs An optional list of file attributes to set atomically when creating the directories.
@@ -369,6 +372,9 @@ public static Path createParentDirectories(final Path path, final FileAttribute<
369372

370373
/**
371374
* Creates the parent directories for the given {@code path}.
375+
* <p>
376+
* If the parent directory already exists, then return it.
377+
* <p>
372378
*
373379
* @param path The path to a file (or directory).
374380
* @param linkOption A {@link LinkOption} or null.
@@ -377,10 +383,15 @@ public static Path createParentDirectories(final Path path, final FileAttribute<
377383
* @throws IOException if an I/O error occurs.
378384
* @since 2.12.0
379385
*/
380-
public static Path createParentDirectories(final Path path, final LinkOption linkOption, final FileAttribute<?>... attrs) throws IOException {
386+
public static Path createParentDirectories(final Path path, final LinkOption linkOption,
387+
final FileAttribute<?>... attrs) throws IOException {
381388
Path parent = getParent(path);
382389
parent = linkOption == LinkOption.NOFOLLOW_LINKS ? parent : readIfSymbolicLink(parent);
383-
return parent == null ? null : Files.createDirectories(parent, attrs);
390+
if (parent == null) {
391+
return null;
392+
}
393+
final boolean exists = linkOption == null ? Files.exists(parent) : Files.exists(parent, linkOption);
394+
return exists ? parent : Files.createDirectories(parent, attrs);
384395
}
385396

386397
/**

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.junit.jupiter.api.Assertions.assertNotNull;
2525
import static org.junit.jupiter.api.Assertions.assertNull;
2626
import static org.junit.jupiter.api.Assertions.assertThrows;
27-
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
2827
import static org.junit.jupiter.api.Assertions.assertTrue;
2928
import static org.junit.jupiter.api.Assumptions.assumeFalse;
3029

@@ -36,7 +35,6 @@
3635
import java.net.URL;
3736
import java.nio.charset.StandardCharsets;
3837
import java.nio.file.DirectoryStream;
39-
import java.nio.file.FileAlreadyExistsException;
4038
import java.nio.file.FileSystem;
4139
import java.nio.file.FileSystems;
4240
import java.nio.file.Files;
@@ -244,7 +242,7 @@ public void testCreateDirectoriesSymlink() throws IOException {
244242
@Test
245243
public void testCreateDirectoriesSymlinkClashing() throws IOException {
246244
final Path symlinkedDir = createTempSymlinkedRelativeDir();
247-
assertThrowsExactly(FileAlreadyExistsException.class, () -> PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
245+
assertEquals(symlinkedDir, PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
248246
}
249247

250248
@Test

0 commit comments

Comments
 (0)