Skip to content

Commit 7752088

Browse files
committed
[IO-791] Regression in FileUtils.touch - no longer creates parent
directories
1 parent 6921f87 commit 7752088

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ The <action> type attribute can be add,update,fix,remove.
4747
</properties>
4848

4949
<body>
50+
<release version="2.12.1" date="2023-MM-DD" description="Java 8 required.">
51+
<!-- FIX -->
52+
<action issue="IO-791" dev="ggregory" type="fix" due-to="Chad Wilson, Gary Gregory">
53+
Regression in FileUtils.touch - no longer creates parent directories.
54+
</action>
55+
</release>
5056
<release version="2.12.0" date="2023-05-13" description="Java 8 required.">
5157
<!-- FIX -->
5258
<action issue="IO-697" dev="kinow" type="fix" due-to="otter606">

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ static Set<FileVisitOption> toFileVisitOptionSet(final FileVisitOption... fileVi
15991599

16001600
/**
16011601
* Implements behavior similar to the Unix "touch" utility. Creates a new file with size 0, or, if the file exists, just
1602-
* updates the file's modified time.
1602+
* updates the file's modified time. this method creates parent directories if they do not exist.
16031603
*
16041604
* @param file the file to touch.
16051605
* @return The given file.
@@ -1610,6 +1610,7 @@ static Set<FileVisitOption> toFileVisitOptionSet(final FileVisitOption... fileVi
16101610
public static Path touch(final Path file) throws IOException {
16111611
Objects.requireNonNull(file, "file");
16121612
if (!Files.exists(file)) {
1613+
createParentDirectories(file);
16131614
Files.createFile(file);
16141615
} else {
16151616
FileTimes.setLastModifiedTime(file);

src/test/java/org/apache/commons/io/FileUtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,19 @@ public void testTouch() throws IOException {
27312731
assertTrue(getLastModifiedMillis(file) <= nowMillis + delta, "FileUtils.touch() changed lastModified to less than now+3s");
27322732
}
27332733

2734+
@Test
2735+
public void testTouchDirDoesNotExist() throws Exception {
2736+
final File file = new File("target/does-not-exist", "touchme.txt");
2737+
final File parentDir = file.getParentFile();
2738+
file.delete();
2739+
parentDir.delete();
2740+
assertFalse(parentDir.exists());
2741+
assertFalse(file.exists());
2742+
FileUtils.touch(file);
2743+
assertTrue(parentDir.exists());
2744+
assertTrue(file.exists());
2745+
}
2746+
27342747
@Test
27352748
public void testToURLs1() throws Exception {
27362749
final File[] files = {

0 commit comments

Comments
 (0)