Skip to content

Commit

Permalink
Merge pull request #13694 from nextcloud/backport/13612/stable-3.30
Browse files Browse the repository at this point in the history
[stable-3.30] BugFix - NPE internalFolderSyncTimestamp & File Existence Check
  • Loading branch information
alperozturk96 authored Oct 7, 2024
2 parents 2e7430b + bb00c0f commit 298dd49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.network.ConnectivityService
import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.SynchronizeFolderOperation
import com.owncloud.android.utils.FileStorageUtils
import java.io.File

@Suppress("Detekt.NestedBlockDepth")
@Suppress("Detekt.NestedBlockDepth", "ReturnCount")
class InternalTwoWaySyncWork(
private val context: Context,
params: WorkerParameters,
Expand All @@ -47,13 +48,8 @@ class InternalTwoWaySyncWork(
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user)

for (folder in folders) {
val freeSpaceLeft = File(folder.storagePath).getFreeSpace()
val localFolderSize = FileStorageUtils.getFolderSize(File(folder.storagePath, MainApp.getDataFolder()))
val remoteFolderSize = folder.fileLength

if (freeSpaceLeft < (remoteFolderSize - localFolderSize)) {
Log_OC.d(TAG, "Not enough space left!")
result = false
checkFreeSpace(folder)?.let { checkFreeSpaceResult ->
return checkFreeSpaceResult
}

Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
Expand Down Expand Up @@ -85,6 +81,31 @@ class InternalTwoWaySyncWork(
}
}

@Suppress("TooGenericExceptionCaught")
private fun checkFreeSpace(folder: OCFile): Result? {
val storagePath = folder.storagePath ?: MainApp.getStoragePath()
val file = File(storagePath)

if (!file.exists()) return null

return try {
val freeSpaceLeft = file.freeSpace
val localFolder = File(storagePath, MainApp.getDataFolder())
val localFolderSize = FileStorageUtils.getFolderSize(localFolder)
val remoteFolderSize = folder.fileLength

if (freeSpaceLeft < (remoteFolderSize - localFolderSize)) {
Log_OC.d(TAG, "Not enough space left!")
Result.failure()
} else {
null
}
} catch (e: Exception) {
Log_OC.d(TAG, "Error caught at checkFreeSpace: $e")
null
}
}

companion object {
const val TAG = "InternalTwoWaySyncWork"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ private OCFile createFileInstance(FileEntity fileEntity) {
ocFile.setLivePhoto(fileEntity.getMetadataLivePhoto());
ocFile.setHidden(nullToZero(fileEntity.getHidden()) == 1);
ocFile.setE2eCounter(fileEntity.getE2eCounter());
ocFile.setInternalFolderSyncTimestamp(fileEntity.getInternalTwoWaySync());
ocFile.setInternalFolderSyncTimestamp(nullToZero(fileEntity.getInternalTwoWaySync()));

String sharees = fileEntity.getSharees();
// Surprisingly JSON deserialization causes significant overhead.
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/owncloud/android/datamodel/OCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -1055,11 +1056,15 @@ public void setE2eCounter(@Nullable Long e2eCounter) {
}

public boolean isInternalFolderSync() {
if (internalFolderSyncTimestamp == null) {
return false;
}

return internalFolderSyncTimestamp >= 0;
}

public Long getInternalFolderSyncTimestamp() {
return internalFolderSyncTimestamp;
return Objects.requireNonNullElse(internalFolderSyncTimestamp, -1L);
}

public void setInternalFolderSyncTimestamp(Long internalFolderSyncTimestamp) {
Expand Down

0 comments on commit 298dd49

Please sign in to comment.