1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 00:04:06 +00:00

feat: sanitize torrent filenames depending on current OS (#3823)

Instead of applying the limitations of the least common denominator (Windows) everywhere.
This commit is contained in:
sfan5 2023-11-01 21:27:04 +01:00 committed by GitHub
parent c0ee65578c
commit aaed2eb26c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View file

@ -313,8 +313,12 @@ namespace
// COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
// Also avoid these names followed immediately by an extension;
// for example, NUL.txt is not recommended.
[[nodiscard]] bool isReservedFile(std::string_view in) noexcept
[[nodiscard]] bool isReservedFile([[maybe_unused]] std::string_view in) noexcept
{
#ifndef _WIN32
// Of course, on Unix-like platforms none of this applies.
return false;
#else
if (std::empty(in))
{
return false;
@ -351,6 +355,7 @@ namespace
std::begin(ReservedPrefixes),
std::end(ReservedPrefixes),
[in_upper_sv](auto const& prefix) { return tr_strv_starts_with(in_upper_sv, prefix); });
#endif
}
// https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
@ -359,6 +364,9 @@ namespace
// except for the following:
[[nodiscard]] auto constexpr isReservedChar(char ch) noexcept
{
#if !defined(_WIN32)
return ch == '/';
#else
switch (ch)
{
case '"':
@ -374,6 +382,7 @@ namespace
default:
return false;
}
#endif
}
void appendSanitizedComponent(std::string_view in, tr_pathbuf& out)

View file

@ -153,14 +153,20 @@ TEST_F(TorrentFilesTest, isSubpathPortable)
{ " foo", false },
{ "foo ", false },
// reserved names
// reserved names and characters (platform-dependent)
#ifdef _WIN32
{ "COM1", false },
{ "COM1.txt", false },
{ "Com1", false },
{ "com1", false },
// reserved characters
{ "hell:o.txt", false },
#else
{ "COM1", true },
{ "COM1.txt", true },
{ "Com1", true },
{ "com1", true },
{ "hell:o.txt", true },
#endif
// everything else
{ ".foo", true },