提交 3f235e0d 编写于 作者: D Daniel Beck 提交者: GitHub

Merge pull request #2738 from jglick/RewindableRotatingFileOutputStream-JENKINS-16634

[JENKINS-16634] Do not fail to write a log file just because something deleted the parent directory
...@@ -772,6 +772,7 @@ THE SOFTWARE. ...@@ -772,6 +772,7 @@ THE SOFTWARE.
<forkCount>0.5C</forkCount> <forkCount>0.5C</forkCount>
<reuseForks>true</reuseForks> <reuseForks>true</reuseForks>
<argLine>-XX:MaxPermSize=128m -noverify</argLine> <!-- some versions of JDK7/8 causes VerifyError during mock tests: http://code.google.com/p/powermock/issues/detail?id=504 --> <argLine>-XX:MaxPermSize=128m -noverify</argLine> <!-- some versions of JDK7/8 causes VerifyError during mock tests: http://code.google.com/p/powermock/issues/detail?id=504 -->
<trimStackTrace>false</trimStackTrace> <!-- SUREFIRE-1226 workaround -->
</configuration> </configuration>
</plugin> </plugin>
<plugin><!-- set main class --> <plugin><!-- set main class -->
......
...@@ -28,6 +28,7 @@ import java.io.FileNotFoundException; ...@@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
/** /**
* {@link OutputStream} that writes to a file. * {@link OutputStream} that writes to a file.
...@@ -48,6 +49,7 @@ public class RewindableFileOutputStream extends OutputStream { ...@@ -48,6 +49,7 @@ public class RewindableFileOutputStream extends OutputStream {
private synchronized OutputStream current() throws IOException { private synchronized OutputStream current() throws IOException {
if (current == null) { if (current == null) {
if (!closed) { if (!closed) {
FileUtils.forceMkdir(out.getParentFile());
try { try {
current = new FileOutputStream(out,false); current = new FileOutputStream(out,false);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
......
...@@ -9,16 +9,20 @@ import org.junit.Test; ...@@ -9,16 +9,20 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
/** public class RewindableRotatingFileOutputStreamTest {
* @author Kohsuke Kawaguchi
*/ @Rule
public class ReopenableRotatingFileOutputStreamTest { public TemporaryFolder tmp = new TemporaryFolder();
@Test @Test
public void rotation() throws IOException, InterruptedException { public void rotation() throws IOException, InterruptedException {
File base = File.createTempFile("test", "log"); File base = tmp.newFile("test.log");
ReopenableRotatingFileOutputStream os = new ReopenableRotatingFileOutputStream(base,3); RewindableRotatingFileOutputStream os = new RewindableRotatingFileOutputStream(base,3);
PrintWriter w = new PrintWriter(os,true); PrintWriter w = new PrintWriter(os,true);
for (int i=0; i<=4; i++) { for (int i=0; i<=4; i++) {
w.println("Content"+i); w.println("Content"+i);
...@@ -35,4 +39,21 @@ public class ReopenableRotatingFileOutputStreamTest { ...@@ -35,4 +39,21 @@ public class ReopenableRotatingFileOutputStreamTest {
os.deleteAll(); os.deleteAll();
} }
@Issue("JENKINS-16634")
@Test
public void deletedFolder() throws Exception {
File dir = tmp.newFolder("dir");
File base = new File(dir, "x.log");
RewindableRotatingFileOutputStream os = new RewindableRotatingFileOutputStream(base, 3);
for (int i = 0; i < 2; i++) {
FileUtils.deleteDirectory(dir);
os.write('.');
FileUtils.deleteDirectory(dir);
os.write('.');
FileUtils.deleteDirectory(dir);
os.rewind();
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册