Skip to content

Commit

Permalink
Fix potential race condition
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jan 5, 2024
1 parent c318b63 commit 2040c6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}
}

Context operationContext = context.get();
if (operationContext == null) {
return new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
}

RemoteOperationResult result;
File newFile = null;
boolean moved;
Expand All @@ -190,6 +195,8 @@ protected RemoteOperationResult run(OwnCloudClient client) {

result = downloadOperation.execute(client);



if (result.isSuccess()) {
modificationTimestamp = downloadOperation.getModificationTimestamp();
etag = downloadOperation.getEtag();
Expand All @@ -204,13 +211,13 @@ protected RemoteOperationResult run(OwnCloudClient client) {

// 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) {
Expand All @@ -228,7 +235,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
key,
iv,
authenticationTag,
new ArbitraryDataProviderImpl(context.get()),
new ArbitraryDataProviderImpl(operationContext),
user);

try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
Expand All @@ -248,14 +255,15 @@ protected RemoteOperationResult run(OwnCloudClient client) {
} else if (downloadType == DownloadType.EXPORT) {
new FileExportUtils().exportFile(file.getFileName(),
file.getMimeType(),
context.get().getContentResolver(),
operationContext.getContentResolver(),
null,
tmpFile);
if (!tmpFile.delete()) {
Log_OC.e(TAG, "Deletion of " + tmpFile.getAbsolutePath() + " failed!");
}
}
}

Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " +
result.getLogMessage());

Expand Down

0 comments on commit 2040c6f

Please sign in to comment.