This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 46c2aba6 [IO-796] FileAlreadyExistsException in PathUtils.createParentDirectories(Path, LinkOption, FileAttribute<?>...) 46c2aba6 is described below commit 46c2aba6d9cb59e8072ee83499bd7835ac244559 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed May 31 11:21:53 2023 -0400 [IO-796] FileAlreadyExistsException in PathUtils.createParentDirectories(Path, LinkOption, FileAttribute<?>...) --- src/changes/changes.xml | 3 +++ src/main/java/org/apache/commons/io/file/PathUtils.java | 15 +++++++++++++-- .../java/org/apache/commons/io/file/PathUtilsTest.java | 4 +--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 08dd417f..4e6ad709 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -58,6 +58,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory"> AbstractOriginSupplier.checkOrigin() now throws IllegalStateException instead of NullPointerException. </action> + <action issue="IO-796" dev="ggregory" type="fix" due-to="Giacomo Boccardo, Gary Gregory"> + FileAlreadyExistsException in PathUtils.createParentDirectories(Path, LinkOption, FileAttribute...). + </action> <!-- ADD --> <action dev="ggregory" type="add" due-to="Gary Gregory"> Add CharSequenceInputStream.Builder. diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index aa9c6ecb..9d7984de 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -356,6 +356,9 @@ public final class PathUtils { /** * Creates the parent directories for the given {@code path}. + * <p> + * If the parent directory already exists, then return it. + * <p> * * @param path The path to a file (or directory). * @param attrs An optional list of file attributes to set atomically when creating the directories. @@ -369,6 +372,9 @@ public final class PathUtils { /** * Creates the parent directories for the given {@code path}. + * <p> + * If the parent directory already exists, then return it. + * <p> * * @param path The path to a file (or directory). * @param linkOption A {@link LinkOption} or null. @@ -377,10 +383,15 @@ public final class PathUtils { * @throws IOException if an I/O error occurs. * @since 2.12.0 */ - public static Path createParentDirectories(final Path path, final LinkOption linkOption, final FileAttribute<?>... attrs) throws IOException { + public static Path createParentDirectories(final Path path, final LinkOption linkOption, + final FileAttribute<?>... attrs) throws IOException { Path parent = getParent(path); parent = linkOption == LinkOption.NOFOLLOW_LINKS ? parent : readIfSymbolicLink(parent); - return parent == null ? null : Files.createDirectories(parent, attrs); + if (parent == null) { + return null; + } + final boolean exists = linkOption == null ? Files.exists(parent) : Files.exists(parent, linkOption); + return exists ? parent : Files.createDirectories(parent, attrs); } /** diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java index 12c1ec26..860ae36c 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java @@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -36,7 +35,6 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryStream; -import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -244,7 +242,7 @@ public class PathUtilsTest extends AbstractTempDirTest { @Test public void testCreateDirectoriesSymlinkClashing() throws IOException { final Path symlinkedDir = createTempSymlinkedRelativeDir(); - assertThrowsExactly(FileAlreadyExistsException.class, () -> PathUtils.createParentDirectories(symlinkedDir.resolve("child"))); + assertEquals(symlinkedDir, PathUtils.createParentDirectories(symlinkedDir.resolve("child"))); } @Test