Skip to content

Commit 0524f83

Browse files
authored
Merge 6abadd1 into afa17ae
2 parents afa17ae + 6abadd1 commit 0524f83

5 files changed

Lines changed: 30 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 19 additions & 2 deletions
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)