mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-10-31 08:15:02 +00:00
Fix File.getSizeOfFiles() double counting some files (#7907)
The `File.length()` docs state: > Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory. On Android, it *seems* to currently have the right behaviour, summing up the size of its children, which made `File.getSizeOfFiles()` double-count the size of some directories. To avoid this, and be able to control the actual behaviour no matter what future Android versions do, let's count those ourselves.
This commit is contained in:
committed by
GitHub
parent
9b3a9a0a2d
commit
8a5c50d2b8
+5
-1
@@ -84,5 +84,9 @@ fun File.getSizeOfFiles(): Long {
|
||||
Timber.v("Get size of ${it.absolutePath}")
|
||||
true
|
||||
}
|
||||
.sumOf { it.length() }
|
||||
// Only sum the actual size of files, not directories, to avoid double counting or worse,
|
||||
// since the docs mention that `length()` for directories is not reliable.
|
||||
// For directories, we can assume a size of 4096 bytes (4 KiB) as a placeholder,
|
||||
// which is a common block size on many file systems.
|
||||
.sumOf { if (it.isFile) it.length() else 4096L }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user