Merge pull request #13612 from nextcloud/bugfix/npe-isInternalFolderSync

BugFix - NPE internalFolderSyncTimestamp & File Existence Check
This commit is contained in:
Tobias Kaminsky 2024-10-04 07:51:23 +02:00 committed by GitHub
commit 6d996f12da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 10 deletions

View file

@ -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,
@ -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!")
@ -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"
}

View file

@ -1125,7 +1125,7 @@ public class FileDataStorageManager {
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.

View file

@ -1076,11 +1076,15 @@ public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterfa
}
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) {