mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +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:
@@ -313,8 +313,12 @@ namespace
|
|||||||
// COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
|
// COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
|
||||||
// Also avoid these names followed immediately by an extension;
|
// Also avoid these names followed immediately by an extension;
|
||||||
// for example, NUL.txt is not recommended.
|
// 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))
|
if (std::empty(in))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -351,6 +355,7 @@ namespace
|
|||||||
std::begin(ReservedPrefixes),
|
std::begin(ReservedPrefixes),
|
||||||
std::end(ReservedPrefixes),
|
std::end(ReservedPrefixes),
|
||||||
[in_upper_sv](auto const& prefix) { return tr_strv_starts_with(in_upper_sv, prefix); });
|
[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
|
// https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
|
||||||
@@ -359,6 +364,9 @@ namespace
|
|||||||
// except for the following:
|
// except for the following:
|
||||||
[[nodiscard]] auto constexpr isReservedChar(char ch) noexcept
|
[[nodiscard]] auto constexpr isReservedChar(char ch) noexcept
|
||||||
{
|
{
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
return ch == '/';
|
||||||
|
#else
|
||||||
switch (ch)
|
switch (ch)
|
||||||
{
|
{
|
||||||
case '"':
|
case '"':
|
||||||
@@ -374,6 +382,7 @@ namespace
|
|||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void appendSanitizedComponent(std::string_view in, tr_pathbuf& out)
|
void appendSanitizedComponent(std::string_view in, tr_pathbuf& out)
|
||||||
|
|||||||
@@ -153,14 +153,20 @@ TEST_F(TorrentFilesTest, isSubpathPortable)
|
|||||||
{ " foo", false },
|
{ " foo", false },
|
||||||
{ "foo ", false },
|
{ "foo ", false },
|
||||||
|
|
||||||
// reserved names
|
// reserved names and characters (platform-dependent)
|
||||||
|
#ifdef _WIN32
|
||||||
{ "COM1", false },
|
{ "COM1", false },
|
||||||
{ "COM1.txt", false },
|
{ "COM1.txt", false },
|
||||||
{ "Com1", false },
|
{ "Com1", false },
|
||||||
{ "com1", false },
|
{ "com1", false },
|
||||||
|
|
||||||
// reserved characters
|
|
||||||
{ "hell:o.txt", false },
|
{ "hell:o.txt", false },
|
||||||
|
#else
|
||||||
|
{ "COM1", true },
|
||||||
|
{ "COM1.txt", true },
|
||||||
|
{ "Com1", true },
|
||||||
|
{ "com1", true },
|
||||||
|
{ "hell:o.txt", true },
|
||||||
|
#endif
|
||||||
|
|
||||||
// everything else
|
// everything else
|
||||||
{ ".foo", true },
|
{ ".foo", true },
|
||||||
|
|||||||
Reference in New Issue
Block a user