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
Use truncate mode to replace the contents of existing files #5663
Conversation
90e6d18
to
0ebf564
Compare
`ContentResolver.openOutputStream(Uri)` does not truncate existing files. If the amount of data written is smaller than the file size, you end up with new data at the beginning of the file followed by old data at the end of the file.
0ebf564
to
b9b5cab
Compare
looping in product, we might want to warn the user if we're overwriting files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -34,7 +34,7 @@ class KeysExporter @Inject constructor( | |||
suspend fun export(password: String, uri: Uri) { | |||
withContext(dispatchers.io) { | |||
val data = session.cryptoService().exportRoomKeys(password) | |||
context.contentResolver.openOutputStream(uri) | |||
context.contentResolver.openOutputStream(uri, "wt") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: maybe creating a quick extension like this could help to avoid code duplication:
fun Context.safeOpenOutputStream(uri: Uri): OutputStream? {
return contentResolver.openOutputStream(uri, "wt")
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fast update!
Type of change
Content
Instances where
ContentResolver.openOutputStream(Uri)
is used to potentially open existing files for writing have been fixed.Motivation and context
ContentResolver.openOutputStream(Uri)
does not truncate existing files. If the amount of data written is smaller than the file size, you end up with new data at the beginning of the file followed by old data at the end of the file.Some places where
ContentResolver.openOutputStream(Uri)
is used have not been changed because they only deal with newly created files and are thus not a problem.Tests
The other export functionality can be tested in a similar way.
Tested devices
Checklist