Add PathUtils.createParentDirectories(Path, FileAttribute...).
Fix XML attribute syntax error in changes.xml.
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
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;
@@ -280,6 +281,29 @@ public static PathCounters countDirectory(final Path directory) throws IOExcepti
return visitFileTree(new CountingPathVisitor(Counters.longPathCounters()), directory).getPathCounters();
}
+ /**
+ * 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.
*

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
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());
@@ -49,6 +59,16 @@ public void testCopyFile() throws IOException {
}
}
+ @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);

Add PathUtils.createParentDirectories(Path, FileAttribute...).

Fix XML attribute syntax error in changes.xml.