From 072887281ab9eefa5dd1ff92e053de310746cccb Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 7 Sep 2020 09:59:58 +0200 Subject: [PATCH] Fix npe on computing enough space with non-downloaded folder content Signed-off-by: tobiasKaminsky --- .../android/ui/fragment/OCFileListFragmentIT.kt | 15 +++++++++++++++ .../android/ui/fragment/OCFileListFragment.java | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt b/src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt index 9cf4c8f5e817..0dabe88dc559 100644 --- a/src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt +++ b/src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt @@ -392,9 +392,24 @@ class OCFileListFragmentIT : AbstractOnServerIT() { assertTrue(sut.checkIfEnoughSpace(200L, ocFile)) ocFile.fileLength = 100 + assertFalse(sut.checkIfEnoughSpace(50L, ocFile)) + + ocFile.fileLength = 44 assertTrue(sut.checkIfEnoughSpace(50L, ocFile)) ocFile.fileLength = 100 assertTrue(sut.checkIfEnoughSpace(100L, ocFile)) } + + @Test + @SuppressWarnings("MagicNumber") + fun testEnoughSpaceWithNoLocalFolder() { + val sut = OCFileListFragment() + val ocFile = OCFile("/test/") + + ocFile.mimeType = "DIR" + + ocFile.fileLength = 100 + assertTrue(sut.checkIfEnoughSpace(200L, ocFile)) + } } diff --git a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 77b98d530d5f..8ec8ba40a674 100644 --- a/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -100,6 +100,7 @@ import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.EncryptionUtils; import com.owncloud.android.utils.FileSortOrder; +import com.owncloud.android.utils.FileStorageUtils; import com.owncloud.android.utils.MimeTypeUtil; import com.owncloud.android.utils.ThemeUtils; @@ -1775,13 +1776,22 @@ private void syncAndCheckFiles(Collection files) { public boolean checkIfEnoughSpace(long availableSpaceOnDevice, OCFile file) { if (file.isFolder()) { // on folders we assume that we only need difference - return availableSpaceOnDevice > (file.getFileLength() - new File(file.getStoragePath()).length()); + return availableSpaceOnDevice > (file.getFileLength() - localFolderSize(file)); } else { // on files complete file must first be stored, then target gets overwritten return availableSpaceOnDevice > file.getFileLength(); } } + private long localFolderSize(OCFile file) { + if (file.getStoragePath() == null) { + // not yet downloaded anything + return 0; + } else { + return FileStorageUtils.getFolderSize(new File(file.getStoragePath())); + } + } + private void showSpaceErrorDialog(OCFile file, long availableSpaceOnDevice) { SyncFileNotEnoughSpaceDialogFragment dialog = SyncFileNotEnoughSpaceDialogFragment.newInstance(file, availableSpaceOnDevice);