Skip to content

Commit 298dd49

Browse files
Merge pull request #13694 from nextcloud/backport/13612/stable-3.30
[stable-3.30] BugFix - NPE internalFolderSyncTimestamp & File Existence Check
2 parents 2e7430b + bb00c0f commit 298dd49

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

app/src/main/java/com/nextcloud/client/jobs/InternalTwoWaySyncWork.kt

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ import com.nextcloud.client.device.PowerManagementService
1515
import com.nextcloud.client.network.ConnectivityService
1616
import com.owncloud.android.MainApp
1717
import com.owncloud.android.datamodel.FileDataStorageManager
18+
import com.owncloud.android.datamodel.OCFile
1819
import com.owncloud.android.lib.common.utils.Log_OC
1920
import com.owncloud.android.operations.SynchronizeFolderOperation
2021
import com.owncloud.android.utils.FileStorageUtils
2122
import java.io.File
2223

23-
@Suppress("Detekt.NestedBlockDepth")
24+
@Suppress("Detekt.NestedBlockDepth", "ReturnCount")
2425
class InternalTwoWaySyncWork(
2526
private val context: Context,
2627
params: WorkerParameters,
@@ -47,13 +48,8 @@ class InternalTwoWaySyncWork(
4748
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user)
4849

4950
for (folder in folders) {
50-
val freeSpaceLeft = File(folder.storagePath).getFreeSpace()
51-
val localFolderSize = FileStorageUtils.getFolderSize(File(folder.storagePath, MainApp.getDataFolder()))
52-
val remoteFolderSize = folder.fileLength
53-
54-
if (freeSpaceLeft < (remoteFolderSize - localFolderSize)) {
55-
Log_OC.d(TAG, "Not enough space left!")
56-
result = false
51+
checkFreeSpace(folder)?.let { checkFreeSpaceResult ->
52+
return checkFreeSpaceResult
5753
}
5854

5955
Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
@@ -85,6 +81,31 @@ class InternalTwoWaySyncWork(
8581
}
8682
}
8783

84+
@Suppress("TooGenericExceptionCaught")
85+
private fun checkFreeSpace(folder: OCFile): Result? {
86+
val storagePath = folder.storagePath ?: MainApp.getStoragePath()
87+
val file = File(storagePath)
88+
89+
if (!file.exists()) return null
90+
91+
return try {
92+
val freeSpaceLeft = file.freeSpace
93+
val localFolder = File(storagePath, MainApp.getDataFolder())
94+
val localFolderSize = FileStorageUtils.getFolderSize(localFolder)
95+
val remoteFolderSize = folder.fileLength
96+
97+
if (freeSpaceLeft < (remoteFolderSize - localFolderSize)) {
98+
Log_OC.d(TAG, "Not enough space left!")
99+
Result.failure()
100+
} else {
101+
null
102+
}
103+
} catch (e: Exception) {
104+
Log_OC.d(TAG, "Error caught at checkFreeSpace: $e")
105+
null
106+
}
107+
}
108+
88109
companion object {
89110
const val TAG = "InternalTwoWaySyncWork"
90111
}

app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ private OCFile createFileInstance(FileEntity fileEntity) {
10401040
ocFile.setLivePhoto(fileEntity.getMetadataLivePhoto());
10411041
ocFile.setHidden(nullToZero(fileEntity.getHidden()) == 1);
10421042
ocFile.setE2eCounter(fileEntity.getE2eCounter());
1043-
ocFile.setInternalFolderSyncTimestamp(fileEntity.getInternalTwoWaySync());
1043+
ocFile.setInternalFolderSyncTimestamp(nullToZero(fileEntity.getInternalTwoWaySync()));
10441044

10451045
String sharees = fileEntity.getSharees();
10461046
// Surprisingly JSON deserialization causes significant overhead.

app/src/main/java/com/owncloud/android/datamodel/OCFile.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.io.File;
3636
import java.util.ArrayList;
3737
import java.util.List;
38+
import java.util.Objects;
3839

3940
import androidx.annotation.NonNull;
4041
import androidx.annotation.Nullable;
@@ -1055,11 +1056,15 @@ public void setE2eCounter(@Nullable Long e2eCounter) {
10551056
}
10561057

10571058
public boolean isInternalFolderSync() {
1059+
if (internalFolderSyncTimestamp == null) {
1060+
return false;
1061+
}
1062+
10581063
return internalFolderSyncTimestamp >= 0;
10591064
}
10601065

10611066
public Long getInternalFolderSyncTimestamp() {
1062-
return internalFolderSyncTimestamp;
1067+
return Objects.requireNonNullElse(internalFolderSyncTimestamp, -1L);
10631068
}
10641069

10651070
public void setInternalFolderSyncTimestamp(Long internalFolderSyncTimestamp) {

0 commit comments

Comments
 (0)