Skip to content

Commit

Permalink
Fix npe on computing enough space with non-downloaded folder content
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky authored and backportbot[bot] committed Sep 8, 2020
1 parent 7a2acb9 commit 0728872
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1775,13 +1776,22 @@ private void syncAndCheckFiles(Collection<OCFile> 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);
Expand Down

0 comments on commit 0728872

Please sign in to comment.