mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
[fix] Improve handling of illegal vFAT filenames. (#14043)
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:
@@ -948,14 +948,17 @@ end
|
|||||||
|
|
||||||
--- Replaces characters that are invalid filenames.
|
--- 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
|
-- These characters are problematic on Windows filesystems. On Linux only
|
||||||
-- <code>/</code> poses a problem.
|
-- <code>/</code> poses a problem.
|
||||||
---- @string str filename
|
---- @string str filename
|
||||||
---- @treturn string sanitized filename
|
---- @treturn string sanitized filename
|
||||||
local function replaceAllInvalidChars(str)
|
local function replaceAllInvalidChars(str)
|
||||||
if str then
|
if str then
|
||||||
return str:gsub('[\\/:*?"<>|]', '_')
|
str = str:gsub('[\\/:*?"<>|]', '_')
|
||||||
|
str = str:gsub("[.%s]+$", "")
|
||||||
|
return str
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user