From 2664349c1614b55c6910f9c2f7e7ee8fab0f2a9e Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 5 Jan 2024 15:35:49 +0100 Subject: [PATCH] Fix potential race condition Signed-off-by: alperozturk --- .../files/downloader/FileDownloadWorker.kt | 11 ++++++++--- .../operations/DownloadFileOperation.java | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index 178761ef84..3efb81a35d 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -66,6 +66,7 @@ class FileDownloadWorker( private val TAG = FileDownloadWorker::class.java.simpleName private var currentDownload: DownloadFileOperation? = null + private val lock = Any() const val FILES_SEPARATOR = "," const val FOLDER_REMOTE_PATH = "FOLDER_REMOTE_PATH" @@ -84,12 +85,16 @@ class FileDownloadWorker( const val EXTRA_ACCOUNT_NAME = "EXTRA_ACCOUNT_NAME" fun isDownloading(user: User, file: OCFile): Boolean { - return currentDownload?.isActive(user, file) ?: false + synchronized(lock) { + return currentDownload?.isActive(user, file) ?: false + } } fun cancelCurrentDownload(user: User, file: OCFile) { - if (currentDownload?.isActive(user, file) == true) { - currentDownload?.cancel() + synchronized(lock) { + if (currentDownload?.isActive(user, file) == true) { + currentDownload?.cancel() + } } } diff --git a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index 1aaca76493..e0e28af5a4 100644 --- a/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/app/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -170,6 +170,11 @@ public class DownloadFileOperation extends RemoteOperation { } } + Context operationContext = context.get(); + if (operationContext == null) { + return new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR); + } + RemoteOperationResult result; File newFile = null; boolean moved; @@ -190,6 +195,8 @@ public class DownloadFileOperation extends RemoteOperation { result = downloadOperation.execute(client); + + if (result.isSuccess()) { modificationTimestamp = downloadOperation.getModificationTimestamp(); etag = downloadOperation.getEtag(); @@ -204,13 +211,13 @@ public class DownloadFileOperation extends RemoteOperation { // decrypt file if (file.isEncrypted()) { - FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.get().getContentResolver()); + FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, operationContext.getContentResolver()); - OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath()); + OCFile parent = fileDataStorageManager.getFileByEncryptedRemotePath(file.getParentRemotePath()); DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, - context.get(), + operationContext, user); if (metadata == null) { @@ -228,7 +235,7 @@ public class DownloadFileOperation extends RemoteOperation { key, iv, authenticationTag, - new ArbitraryDataProviderImpl(context.get()), + new ArbitraryDataProviderImpl(operationContext), user); try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) { @@ -248,7 +255,7 @@ public class DownloadFileOperation extends RemoteOperation { } else if (downloadType == DownloadType.EXPORT) { new FileExportUtils().exportFile(file.getFileName(), file.getMimeType(), - context.get().getContentResolver(), + operationContext.getContentResolver(), null, tmpFile); if (!tmpFile.delete()) { @@ -256,6 +263,7 @@ public class DownloadFileOperation extends RemoteOperation { } } } + Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());