Skip to content

Commit c6b8bcf

Browse files
authored
Merge pull request #11745 from Stypox/truncate-before-export
Fix downloading/exporting when overwriting file would not truncate
2 parents 57e66b1 + e31a8ad commit c6b8bcf

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
7676

7777
try {
7878
outFile = new StoredFileHelper(this, path, "application/json");
79-
outputStream = new SharpOutputStream(outFile.getStream());
79+
// truncate the file before writing to it, otherwise if the new content is smaller than
80+
// the previous file size, the file will retain part of the previous content and be
81+
// corrupted
82+
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
8083
} catch (final IOException e) {
8184
handleError(e);
8285
return START_NOT_STICKY;

app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
2424
*/
2525
@Throws(Exception::class)
2626
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
27-
file.create()
28-
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
27+
// truncate the file before writing to it, otherwise if the new content is smaller than the
28+
// previous file size, the file will retain part of the previous content and be corrupted
29+
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
2930
// add the database
3031
ZipHelper.addFileToZip(
3132
outZip,

app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ public SharpStream getStream() throws IOException {
189189
}
190190
}
191191

192+
public SharpStream openAndTruncateStream() throws IOException {
193+
final SharpStream sharpStream = getStream();
194+
try {
195+
sharpStream.setLength(0);
196+
} catch (final Throwable e) {
197+
// we can't use try-with-resources here, since we only want to close the stream if an
198+
// exception occurs, but leave it open if everything goes well
199+
sharpStream.close();
200+
throw e;
201+
}
202+
return sharpStream;
203+
}
204+
192205
/**
193206
* Indicates whether it's using the {@code java.io} API.
194207
*

app/src/test/java/org/schabi/newpipe/settings/ImportExportManagerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ImportExportManagerTest {
5555
`when`(sharedPreferences.all).thenReturn(expectedPreferences)
5656

5757
val output = File.createTempFile("newpipe_", "")
58-
`when`(storedFileHelper.stream).thenReturn(FileStream(output))
58+
`when`(storedFileHelper.openAndTruncateStream()).thenReturn(FileStream(output))
5959
ImportExportManager(fileLocator).exportDatabase(sharedPreferences, storedFileHelper)
6060

6161
val zipFile = ZipFile(output)

0 commit comments

Comments
 (0)