From ea84cbd11ee611b57ccb319c976ba24812ba1380 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Wed, 25 Mar 2026 15:48:25 +0100 Subject: [PATCH 1/2] fix(file-delete): local copy Signed-off-by: alperozturk96 --- .../datamodel/FileDataStorageManager.java | 24 ++++++++++++------- .../android/utils/FileStorageUtils.java | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index ff35d268a93f..31c498d9876a 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -47,6 +47,8 @@ import com.nextcloud.model.ShareeEntry; import com.nextcloud.utils.date.DateFormatPattern; import com.nextcloud.utils.extensions.DateExtensionsKt; +import com.nextcloud.utils.extensions.FileExtensionsKt; +import com.nextcloud.utils.extensions.StringExtensionsKt; import com.owncloud.android.MainApp; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import com.owncloud.android.lib.common.network.WebdavEntry; @@ -912,25 +914,31 @@ public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLoc } private boolean removeLocalCopyIfNeeded(OCFile ocFile, boolean removeLocalCopy, boolean removeDBData) { - String localPath = ocFile.getStoragePath(); - if (!removeLocalCopy) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false"); + Log_OC.w(TAG, "removeLocalCopyIfNeeded: removeLocalCopy=false"); return true; } - if (!ocFile.isDown()) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: file not downloaded -> skip"); + String localPath = ocFile.getStoragePath(); + final var file = FileExtensionsKt.toFile(localPath); + if (file == null) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: file exists -> skip"); return true; } - if (localPath == null) { - Log_OC.d(TAG, "removeLocalCopyIfNeeded: localPath is null -> skip"); + if (ocFile.isFolder()) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: file is folder -> skip"); return true; } - Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath); + String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile); + if (!localPath.equalsIgnoreCase(expectedPath)) { + Log_OC.w(TAG, "removeLocalCopyIfNeeded: Path mismatch! Expected " + expectedPath + + " but found " + localPath + ". Skipping deletion to prevent data loss."); + return true; + } + Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath); boolean success = new File(localPath).delete(); Log_OC.d(TAG, "removeLocalCopyIfNeeded: file deletion result=" + success); diff --git a/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java b/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java index c31e21a6a48d..4d1d7c1019ec 100644 --- a/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/FileStorageUtils.java @@ -173,6 +173,8 @@ public static String getSavePath(String accountName) { * Get local path where OCFile file is to be stored after upload. That is, * corresponding local path (in local owncloud storage) to remote uploaded * file. + *

+ * e.g. /storage/emulated/0/Android/media/com.nextcloud.client/nextcloud/admin@example.cloud/folder/file.txt */ public static String getDefaultSavePathFor(String accountName, OCFile file) { return getSavePath(accountName) + file.getDecryptedRemotePath(); From 5c69a91e76762d4c58f733eb9e9916889f63dcf6 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Wed, 8 Apr 2026 11:48:11 +0200 Subject: [PATCH 2/2] try to delete with expected path Signed-off-by: alperozturk96 --- .../android/datamodel/FileDataStorageManager.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 31c498d9876a..3cc664b7b6c3 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -935,11 +935,15 @@ private boolean removeLocalCopyIfNeeded(OCFile ocFile, boolean removeLocalCopy, if (!localPath.equalsIgnoreCase(expectedPath)) { Log_OC.w(TAG, "removeLocalCopyIfNeeded: Path mismatch! Expected " + expectedPath + " but found " + localPath + ". Skipping deletion to prevent data loss."); - return true; } Log_OC.d(TAG, "removeLocalCopyIfNeeded: deleting local file -> " + localPath); - boolean success = new File(localPath).delete(); + boolean success = false; + try { + success = new File(expectedPath).delete(); + } catch (Exception e) { + Log_OC.e(TAG, "removeLocalCopyIfNeeded: deletion error: ", e); + } Log_OC.d(TAG, "removeLocalCopyIfNeeded: file deletion result=" + success); if (!success) {