From 8a5c50d2b8a5f6a2475b314eaeaebe3762c0b81b Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Wed, 14 Oct 2026 11:13:14 +0200 Subject: [PATCH] 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. --- .../io/element/android/libraries/androidutils/file/File.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt index 7c5f0450e9..9023a8f935 100644 --- a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt +++ b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/file/File.kt @@ -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 } }