Skip to content

Commit 397f69d

Browse files
author
Gary Gregory
committed
Add PathUtils.createParentDirectories(Path, FileAttribute...).
Fix XML attribute syntax error in changes.xml.
1 parent 8e83b13 commit 397f69d

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ The <action> type attribute can be add,update,fix,remove.
8181
<action dev="ggregory" type="add" due-to="Gary Gregory">
8282
Let org.apache.commons.io.filefilter classes work with java.nio.file.Files#newDirectoryStream(Path, DirectoryStream.Filter).
8383
</action>
84-
<action issue-"IO-510" dev="ggregory" type="add" due-to="Gary Gregory, Apache Spark, David Mollitor">
84+
<action issue="IO-510" dev="ggregory" type="add" due-to="Gary Gregory, Apache Spark, David Mollitor">
8585
Add and adapt ReadAheadInputStream and BufferedFileChannelInputStream from Apache Spark.
8686
</action>
87+
<action dev="ggregory" type="add" due-to="Gary Gregory">
88+
Add PathUtils.createParentDirectories(Path, FileAttribute...).
89+
</action>
8790
<!-- UPDATES -->
8891
<action dev="ggregory" type="update" due-to="Dependabot">
8992
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.nio.file.attribute.AclFileAttributeView;
3838
import java.nio.file.attribute.BasicFileAttributes;
3939
import java.nio.file.attribute.DosFileAttributeView;
40+
import java.nio.file.attribute.FileAttribute;
4041
import java.nio.file.attribute.PosixFileAttributeView;
4142
import java.nio.file.attribute.PosixFileAttributes;
4243
import java.nio.file.attribute.PosixFilePermission;
@@ -280,6 +281,29 @@ public static PathCounters countDirectory(final Path directory) throws IOExcepti
280281
return visitFileTree(new CountingPathVisitor(Counters.longPathCounters()), directory).getPathCounters();
281282
}
282283

284+
/**
285+
* Creates the parent directories for the given {@code path}.
286+
* <p>
287+
* Returns the {@code path}'s parent directory if it already exists.
288+
* </p>
289+
*
290+
* @param path The path to a file (or directory).
291+
* @param attrs An optional list of file attributes to set atomically when creating the directories.
292+
* @return The Path for the {@code path}'s parent directory or null if the given path has no parent.
293+
* @throws IOException if an I/O error occurs
294+
* @since 2.9.0
295+
*/
296+
public static Path createParentDirectories(final Path path, FileAttribute<?>... attrs) throws IOException {
297+
final Path parent = path.getParent();
298+
if (parent == null) {
299+
return null;
300+
}
301+
if (Files.isDirectory(parent)) {
302+
return parent;
303+
}
304+
return Files.createDirectories(parent, attrs);
305+
}
306+
283307
/**
284308
* Gets the current directory.
285309
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@
3030

3131
import org.apache.commons.io.filefilter.NameFileFilter;
3232
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
3334

35+
/**
36+
* Tests {@link PathUtils}.
37+
*/
3438
public class PathUtilsTest extends TestArguments {
3539

3640
private static final String PATH_FIXTURE = "NOTICE.txt";
3741

42+
/**
43+
* A temporary directory managed by JUnit.
44+
*/
45+
@TempDir
46+
public Path tempDir;
47+
3848
@Test
3949
public void testCopyFile() throws IOException {
4050
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
@@ -49,6 +59,16 @@ public void testCopyFile() throws IOException {
4959
}
5060
}
5161

62+
@Test
63+
public void testCreateDirectoriesAlreadyExists() throws IOException {
64+
assertEquals(tempDir.getParent(), PathUtils.createParentDirectories(tempDir));
65+
}
66+
67+
@Test
68+
public void testCreateDirectoriesNew() throws IOException {
69+
assertEquals(tempDir, PathUtils.createParentDirectories(tempDir.resolve("child")));
70+
}
71+
5272
@Test
5373
public void testNewDirectoryStream() throws Exception {
5474
final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);

0 commit comments

Comments
 (0)