You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/11/14 14:17:32 UTC

[commons-io] branch master updated: Add PathUtils.createParentDirectories(Path, FileAttribute...).

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 397f69d  Add PathUtils.createParentDirectories(Path, FileAttribute...).
397f69d is described below

commit 397f69d2438f95f7946d83f1b7f240f93febbb3a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 14 09:17:24 2020 -0500

    Add PathUtils.createParentDirectories(Path, FileAttribute...).
    
    Fix XML attribute syntax error in changes.xml.
---
 src/changes/changes.xml                            |  5 ++++-
 .../java/org/apache/commons/io/file/PathUtils.java | 24 ++++++++++++++++++++++
 .../org/apache/commons/io/file/PathUtilsTest.java  | 20 ++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cee894d..7128572 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -81,9 +81,12 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Let org.apache.commons.io.filefilter classes work with java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
       </action>
-      <action issue-"IO-510" dev="ggregory" type="add" due-to="Gary Gregory, Apache Spark, David Mollitor">
+      <action issue="IO-510" dev="ggregory" type="add" due-to="Gary Gregory, Apache Spark, David Mollitor">
         Add and adapt ReadAheadInputStream and BufferedFileChannelInputStream from Apache Spark.
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add PathUtils.createParentDirectories(Path, FileAttribute...).
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
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 f197aed..f0ba36f 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -37,6 +37,7 @@ import java.nio.file.attribute.AclEntry;
 import java.nio.file.attribute.AclFileAttributeView;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.nio.file.attribute.DosFileAttributeView;
+import java.nio.file.attribute.FileAttribute;
 import java.nio.file.attribute.PosixFileAttributeView;
 import java.nio.file.attribute.PosixFileAttributes;
 import java.nio.file.attribute.PosixFilePermission;
@@ -281,6 +282,29 @@ public final class PathUtils {
     }
 
     /**
+     * Creates the parent directories for the given {@code path}. 
+     * <p>
+     * Returns the {@code path}'s parent directory if it already exists.
+     * </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.
+     * @return The Path for the {@code path}'s parent directory or null if the given path has no parent.
+     * @throws IOException if an I/O error occurs
+     * @since 2.9.0
+     */
+    public static Path createParentDirectories(final Path path, FileAttribute<?>... attrs) throws IOException {
+        final Path parent = path.getParent();
+        if (parent == null) {
+            return null;
+        }
+        if (Files.isDirectory(parent)) {
+            return parent;
+        }
+        return Files.createDirectories(parent, attrs);
+    }
+    
+    /**
      * Gets the current directory.
      *
      * @return the current directory.
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 4103aec..00c7e7f 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -30,11 +30,21 @@ import java.util.Iterator;
 
 import org.apache.commons.io.filefilter.NameFileFilter;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
+/**
+ * Tests {@link PathUtils}.
+ */
 public class PathUtilsTest extends TestArguments {
 
     private static final String PATH_FIXTURE = "NOTICE.txt";
 
+    /**
+     * A temporary directory managed by JUnit.
+     */
+    @TempDir
+    public Path tempDir;
+
     @Test
     public void testCopyFile() throws IOException {
         final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
@@ -50,6 +60,16 @@ public class PathUtilsTest extends TestArguments {
     }
 
     @Test
+    public void testCreateDirectoriesAlreadyExists() throws IOException {
+        assertEquals(tempDir.getParent(), PathUtils.createParentDirectories(tempDir));
+    }
+
+    @Test
+    public void testCreateDirectoriesNew() throws IOException {
+        assertEquals(tempDir, PathUtils.createParentDirectories(tempDir.resolve("child")));
+    }
+
+    @Test
     public void testNewDirectoryStream() throws Exception {
         final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
         try (final DirectoryStream<Path> stream = PathUtils.newDirectoryStream(PathUtils.current(), pathFilter)) {