fix: truncate filenames by bytes instead of characters to avoid File name too long (#1933)

* fix: truncate filenames by bytes instead of characters to avoid IOException File name too long

* add a CHANGELOG.md entry.
This commit is contained in:
Akiaki0324
2026-05-11 07:02:11 +08:00
committed by GitHub
parent 3064f51d25
commit 5bdb945406
3 changed files with 87 additions and 3 deletions

View File

@@ -9,6 +9,9 @@ package xyz.nulldev.androidcompat.util
// adopted from: https://github.com/tachiyomiorg/tachiyomi/blob/4cefbce7c34e724b409b6ba127f3c6c5c346ad8d/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt
object SafePath {
private const val MAX_FILENAME_CHARS = 240
private const val MAX_FILENAME_UTF8_BYTES = 240
/**
* Mutate the given filename to make it valid for a FAT filesystem,
* replacing any invalid characters with "_". This method doesn't allow hidden files (starting
@@ -27,11 +30,41 @@ object SafePath {
sb.append('_')
}
}
// Even though vfat allows 255 UCS-2 chars, we might eventually write to
// ext4 through a FUSE layer, so use that limit minus 15 reserved characters.
return sb.toString().take(240)
return truncateFilename(sb.toString())
}
private fun truncateFilename(filename: String): String {
// Keep a safety margin under common filesystem limits and satisfy both
// character count and UTF-8 byte-length constraints.
val output = StringBuilder(minOf(filename.length, MAX_FILENAME_CHARS))
var usedBytes = 0
var index = 0
while (index < filename.length && output.length < MAX_FILENAME_CHARS) {
val codePoint = Character.codePointAt(filename, index)
val codePointBytes = utf8ByteCount(codePoint)
if (usedBytes + codePointBytes > MAX_FILENAME_UTF8_BYTES) {
break
}
output.appendCodePoint(codePoint)
usedBytes += codePointBytes
index += Character.charCount(codePoint)
}
return output.toString()
}
private fun utf8ByteCount(codePoint: Int): Int =
when {
codePoint <= 0x7f -> 1
codePoint <= 0x7ff -> 2
codePoint <= 0xffff -> 3
else -> 4
}
/**
* Returns true if the given character is a valid filename character, false otherwise.
*/