[fix] Improve handling of illegal vFAT filenames. (#14043)
Some checks failed
macos / macOS 13 x86-64 🔨15.2 🎯10.15 (push) Has been cancelled
macos / macOS 14 ARM64 🔨15.4 🎯11.0 (push) Has been cancelled

This change fixes an issue where a file/directory is not created when its name contains trailing spaces or dots.
The issue has been observed on a Kobo device, but probably also applies to other systems using vFAT.

We also might want to consider handling other illegal vFAT filenames like `NUL` or `AUX`.

Please see the [official Microsoft documentation](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions)
for more details on the naming conventions of vFAT (this change addresses the last bullet point).
This commit is contained in:
Michał Mnich
2025-07-13 22:34:17 +02:00
committed by GitHub
parent 9b07f943f5
commit 8db0dee5e0

View File

@@ -948,14 +948,17 @@ end
--- Replaces characters that are invalid filenames.
--
-- Replaces the characters <code>\/:*?"<>|</code> with an <code>_</code>.
-- Replaces the characters <code>\/:*?"<>|</code> with an <code>_</code>
-- and removes trailing dots and spaces, in line with <https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions>.
-- These characters are problematic on Windows filesystems. On Linux only
-- <code>/</code> poses a problem.
---- @string str filename
---- @treturn string sanitized filename
local function replaceAllInvalidChars(str)
if str then
return str:gsub('[\\/:*?"<>|]', '_')
str = str:gsub('[\\/:*?"<>|]', '_')
str = str:gsub("[.%s]+$", "")
return str
end
end