diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index e30b443acf1..94bfae53f89 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -233,9 +233,19 @@ public class RolloverFileOutputStream extends OutputStream File file = new File(_filename); _filename = file.getCanonicalPath(); file = new File(_filename); - File dir = new File(file.getParent()); - if (!dir.isDirectory() || !dir.canWrite()) + File dir = file.getParentFile(); + if (!dir.exists()) + { + throw new IOException("Log directory does not exist. Path=" + dir); + } + else if (!dir.isDirectory()) + { + throw new IOException("Path for Log directory is not a directory. Path=" + dir); + } + else if (!dir.canWrite()) + { throw new IOException("Cannot write log directory " + dir); + } // Is this a rollover file? String filename = file.getName(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index ee3872f98ea..c19bc4184fe 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -36,7 +36,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; @ExtendWith(WorkDirExtension.class) public class RolloverFileOutputStreamTest @@ -181,6 +184,26 @@ public class RolloverFileOutputStreamTest assertSequence(midnight, expected); } + @Test + public void testMissingDirectory() + { + + String templateString = "missingDir/test-rofos-yyyy_mm_dd.log"; + Throwable error; + try (RolloverFileOutputStream rofos = new RolloverFileOutputStream(templateString)) + { + throw new IllegalStateException(); + } + catch (Throwable t) + { + error = t; + } + assertNotNull(error); + assertThat(error, instanceOf(IOException.class)); + error.getMessage(); + assertThat(error.getMessage(), containsString("Log directory does not exist.")); + } + @Test public void testFileHandling() throws Exception {