Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix downloading/exporting when overwriting file would not truncate #11745

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI

try {
outFile = new StoredFileHelper(this, path, "application/json");
outputStream = new SharpOutputStream(outFile.getStream());
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
} catch (final IOException e) {
handleError(e);
return START_NOT_STICKY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
*/
@Throws(Exception::class)
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
file.create()
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
// add the database
ZipHelper.addFileToZip(
outZip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ public SharpStream getStream() throws IOException {
}
}

public SharpStream openAndTruncateStream() throws IOException {
final SharpStream sharpStream = getStream();
try {
sharpStream.setLength(0);
} catch (final Throwable e) {
// we can't use try-with-resources here, since we only want to close the stream if an
// exception occurs, but leave it open if everything goes well
sharpStream.close();
throw e;
}
return sharpStream;
}

/**
* Indicates whether it's using the {@code java.io} API.
*
Expand Down
Loading