Skip to content

Commit 221a5df

Browse files
authored
Fix SentryFileWriter and SentryFileOutputStream append overwrites file contents (#2304)
1 parent a4fb390 commit 221a5df

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Ensure potential callback exceptions are caught #2123 ([#2291](https://github.com/getsentry/sentry-java/pull/2291))
88
- Remove verbose FrameMetricsAggregator failure logging ([#2293](https://github.com/getsentry/sentry-java/pull/2293))
99
- Ignore broken regex for tracePropagationTarget ([#2288](https://github.com/getsentry/sentry-java/pull/2288))
10+
- Fix `SentryFileWriter`/`SentryFileOutputStream` append overwrites file contents ([#2304](https://github.com/getsentry/sentry-java/pull/2304))
1011

1112
### Features
1213

sentry-android-core/src/test/java/io/sentry/android/core/SendCachedEnvelopeIntegrationTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ class SendCachedEnvelopeIntegrationTest {
100100

101101
@Test
102102
fun `when synchronous send times out, continues the task on a background thread`() {
103-
val sut = fixture.getSut(hasStartupCrashMarker = true, delaySend = 50)
104-
fixture.options.startupCrashFlushTimeoutMillis = 10
103+
val sut = fixture.getSut(hasStartupCrashMarker = true, delaySend = 1000)
104+
fixture.options.startupCrashFlushTimeoutMillis = 100
105105

106106
sut.register(fixture.hub, fixture.options)
107107

108108
// first wait until synchronous send times out and check that the logger was hit in the catch block
109-
await.atLeast(11, MILLISECONDS)
109+
await.atLeast(500, MILLISECONDS)
110110
verify(fixture.logger).log(
111111
eq(DEBUG),
112112
eq("Synchronous send timed out, continuing in the background.")

sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class SentryFileOutputStream extends FileOutputStream {
2424
private final @NotNull FileIOSpanManager spanManager;
2525

2626
public SentryFileOutputStream(final @Nullable String name) throws FileNotFoundException {
27-
this(name != null ? new File(name) : null, HubAdapter.getInstance());
27+
this(name != null ? new File(name) : null, false, HubAdapter.getInstance());
2828
}
2929

3030
public SentryFileOutputStream(final @Nullable String name, final boolean append)
@@ -33,7 +33,7 @@ public SentryFileOutputStream(final @Nullable String name, final boolean append)
3333
}
3434

3535
public SentryFileOutputStream(final @Nullable File file) throws FileNotFoundException {
36-
this(file, HubAdapter.getInstance());
36+
this(file, false, HubAdapter.getInstance());
3737
}
3838

3939
public SentryFileOutputStream(final @Nullable File file, final boolean append)
@@ -45,9 +45,9 @@ public SentryFileOutputStream(final @NotNull FileDescriptor fdObj) {
4545
this(init(fdObj, null, HubAdapter.getInstance()), fdObj);
4646
}
4747

48-
SentryFileOutputStream(final @Nullable File file, final @NotNull IHub hub)
48+
SentryFileOutputStream(final @Nullable File file, final boolean append, final @NotNull IHub hub)
4949
throws FileNotFoundException {
50-
this(init(file, false, null, hub));
50+
this(init(file, append, null, hub));
5151
}
5252

5353
private SentryFileOutputStream(
@@ -72,7 +72,7 @@ private static FileOutputStreamInitData init(
7272
throws FileNotFoundException {
7373
final ISpan span = FileIOSpanManager.startSpan(hub, "file.write");
7474
if (delegate == null) {
75-
delegate = new FileOutputStream(file);
75+
delegate = new FileOutputStream(file, append);
7676
}
7777
return new FileOutputStreamInitData(
7878
file, append, span, delegate, hub.getOptions().isSendDefaultPii());

sentry/src/main/java/io/sentry/instrumentation/file/SentryFileWriter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public SentryFileWriter(final @NotNull FileDescriptor fd) {
3030
super(new SentryFileOutputStream(fd));
3131
}
3232

33-
SentryFileWriter(final @NotNull File file, final @NotNull IHub hub) throws FileNotFoundException {
34-
super(new SentryFileOutputStream(file, hub));
33+
SentryFileWriter(final @NotNull File file, final boolean append, final @NotNull IHub hub)
34+
throws FileNotFoundException {
35+
super(new SentryFileOutputStream(file, append, hub));
3536
}
3637
}

sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class SentryFileOutputStreamTest {
2222
internal fun getSut(
2323
tmpFile: File? = null,
2424
activeTransaction: Boolean = true,
25+
append: Boolean = false
2526
): SentryFileOutputStream {
2627
whenever(hub.options).thenReturn(SentryOptions())
2728
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
2829
if (activeTransaction) {
2930
whenever(hub.span).thenReturn(sentryTracer)
3031
}
31-
return SentryFileOutputStream(tmpFile, hub)
32+
return SentryFileOutputStream(tmpFile, append, hub)
3233
}
3334
}
3435

sentry/src/test/java/io/sentry/instrumentation/file/SentryFileWriterTest.kt

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ class SentryFileWriterTest {
2121
internal fun getSut(
2222
tmpFile: File,
2323
activeTransaction: Boolean = true,
24+
append: Boolean = false
2425
): SentryFileWriter {
2526
whenever(hub.options).thenReturn(SentryOptions())
2627
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
2728
if (activeTransaction) {
2829
whenever(hub.span).thenReturn(sentryTracer)
2930
}
30-
return SentryFileWriter(tmpFile, hub)
31+
return SentryFileWriter(tmpFile, append, hub)
3132
}
3233
}
3334

@@ -36,7 +37,7 @@ class SentryFileWriterTest {
3637

3738
private val fixture = Fixture()
3839

39-
private val tmpFile: File get() = tmpDir.newFile("test.txt")
40+
private val tmpFile: File by lazy { tmpDir.newFile("test.txt") }
4041

4142
@Test
4243
fun `captures a span`() {
@@ -53,4 +54,20 @@ class SentryFileWriterTest {
5354
assertEquals(fileIOSpan.isFinished, true)
5455
assertEquals(fileIOSpan.status, OK)
5556
}
57+
58+
@Test
59+
fun `append works`() {
60+
val writer1 = fixture.getSut(tmpFile, append = true)
61+
writer1.use {
62+
it.append("test")
63+
}
64+
65+
// second writer should not overwrite the file contents, but append to the existing content
66+
val writer2 = fixture.getSut(tmpFile, append = true)
67+
writer2.use {
68+
it.append("test2")
69+
}
70+
71+
assertEquals("testtest2", tmpFile.readText())
72+
}
5673
}

0 commit comments

Comments
 (0)