Null-safety when calling PathUtils.createParentDirectories() with a
LinkOption not equal to NOFOLLOW_LINKS and the path is a root path which
means it has a null parent.
MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -1212,7 +1212,7 @@ public static DosFileAttributes readDosFileAttributes(final Path path, final Lin
}
private static Path readIfSymbolicLink(final Path path) throws IOException {
- return Files.isSymbolicLink(path) ? Files.readSymbolicLink(path) : path;
+ return path != null ? Files.isSymbolicLink(path) ? Files.readSymbolicLink(path) : path : null;
}
/**

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
+import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
@@ -175,6 +176,22 @@ public void testCreateDirectoriesAlreadyExists() throws IOException {
assertEquals(tempDirPath.getParent(), PathUtils.createParentDirectories(tempDirPath));
}
+ @Test
+ public void testCreateDirectoriesForRoots() throws IOException {
+ for (final File f : File.listRoots()) {
+ final Path path = f.toPath();
+ assertEquals(path.getParent(), PathUtils.createParentDirectories(path));
+ }
+ }
+
+ @Test
+ public void testCreateDirectoriesForRootsLinkOptionNull() throws IOException {
+ for (final File f : File.listRoots()) {
+ final Path path = f.toPath();
+ assertEquals(path.getParent(), PathUtils.createParentDirectories(path, (LinkOption) null));
+ }
+ }
+
@Test
public void testCreateDirectoriesNew() throws IOException {
assertEquals(tempDirPath, PathUtils.createParentDirectories(tempDirPath.resolve("child")));

Null-safety when calling PathUtils.createParentDirectories() with a
LinkOption not equal to NOFOLLOW_LINKS and the path is a root path which
means it has a null parent.