-
Notifications
You must be signed in to change notification settings - Fork 743
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
Complement for #3656 #3699
Merged
Merged
Complement for #3656 #3699
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ef1f5c
Avoid incomplete downloads in cache
SpiritCroc 512e1b3
Add changelog.d/3656.bugfix
SpiritCroc 2e64f89
Merge pull request #3656 from SpiritCroc/broken_downloads
bmarty 9c1bec9
Create AtomicFileCreator class to avoid code copy/paste
bmarty 7643cc5
Remove part file(s) in case of failure
bmarty a2996ee
Rename var
bmarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Avoid incomplete downloads in cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ import org.matrix.android.sdk.internal.di.SessionDownloadsDirectory | |
import org.matrix.android.sdk.internal.di.UnauthenticatedWithCertificateWithProgress | ||
import org.matrix.android.sdk.internal.session.download.DownloadProgressInterceptor.Companion.DOWNLOAD_PROGRESS_INTERCEPTOR_HEADER | ||
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers | ||
import org.matrix.android.sdk.internal.util.file.AtomicFileCreator | ||
import org.matrix.android.sdk.internal.util.md5 | ||
import org.matrix.android.sdk.internal.util.writeToFile | ||
import timber.log.Timber | ||
|
@@ -96,6 +97,9 @@ internal class DefaultFileService @Inject constructor( | |
} | ||
} | ||
|
||
var atomicFileCreator1: AtomicFileCreator? = null | ||
var atomicFileCreator2: AtomicFileCreator? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same renamed to clearCachedAtomicFime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to |
||
|
||
if (existingDownload != null) { | ||
// FIXME If the first downloader cancels then we'll unfortunately be cancelled too. | ||
return existingDownload.await() | ||
|
@@ -131,8 +135,11 @@ internal class DefaultFileService @Inject constructor( | |
Timber.v("Response size ${response.body?.contentLength()} - Stream available: ${!source.exhausted()}") | ||
|
||
// Write the file to cache (encrypted version if the file is encrypted) | ||
writeToFile(source.inputStream(), cachedFiles.file) | ||
// Write to a part file first, so if we abort before done, we don't have a broken cached file | ||
val atomicFileCreator = AtomicFileCreator(cachedFiles.file).also { atomicFileCreator1 = it } | ||
writeToFile(source.inputStream(), atomicFileCreator.partFile) | ||
response.close() | ||
atomicFileCreator.commit() | ||
} else { | ||
Timber.v("## FileService: cache hit for $url") | ||
} | ||
|
@@ -145,15 +152,18 @@ internal class DefaultFileService @Inject constructor( | |
Timber.v("## FileService: decrypt file") | ||
// Ensure the parent folder exists | ||
cachedFiles.decryptedFile.parentFile?.mkdirs() | ||
// Write to a part file first, so if we abort before done, we don't have a broken cached file | ||
val atomicFileCreator = AtomicFileCreator(cachedFiles.decryptedFile).also { atomicFileCreator2 = it } | ||
val decryptSuccess = cachedFiles.file.inputStream().use { inputStream -> | ||
cachedFiles.decryptedFile.outputStream().buffered().use { outputStream -> | ||
atomicFileCreator.partFile.outputStream().buffered().use { outputStream -> | ||
MXEncryptedAttachments.decryptAttachment( | ||
inputStream, | ||
elementToDecrypt, | ||
outputStream | ||
) | ||
} | ||
} | ||
atomicFileCreator.commit() | ||
if (!decryptSuccess) { | ||
throw IllegalStateException("Decryption error") | ||
} | ||
|
@@ -174,6 +184,11 @@ internal class DefaultFileService @Inject constructor( | |
} | ||
toNotify?.completeWith(result) | ||
|
||
result.onFailure { | ||
atomicFileCreator1?.cancel() | ||
atomicFileCreator2?.cancel() | ||
} | ||
|
||
return result.getOrThrow() | ||
} | ||
|
||
|
42 changes: 42 additions & 0 deletions
42
...-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/file/AtomicFileCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.util.file | ||
|
||
import timber.log.Timber | ||
import java.io.File | ||
|
||
internal class AtomicFileCreator(private val file: File) { | ||
val partFile = File(file.parentFile, "${file.name}.part") | ||
|
||
init { | ||
if (file.exists()) { | ||
Timber.w("## AtomicFileCreator: target file ${file.path} exists, it should not happen.") | ||
} | ||
if (partFile.exists()) { | ||
Timber.d("## AtomicFileCreator: discard aborted part file ${partFile.path}") | ||
// No need to delete the file, we will overwrite it | ||
} | ||
} | ||
|
||
fun cancel() { | ||
partFile.delete() | ||
} | ||
|
||
fun commit() { | ||
partFile.renameTo(file) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can it be renamed to something like encyptedCachedAtomicFile
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.
Renamed to
atomicFileDownload
, it can be a clear file