Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix - NPE internalFolderSyncTimestamp & File Existence Check #13612

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1125,7 +1125,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
6 changes: 5 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 @@ -1076,11 +1076,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
Loading