From aaed2eb26c2431bf0601d26106b4c9583d727752 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 1 Nov 2023 21:27:04 +0100 Subject: [PATCH] feat: sanitize torrent filenames depending on current OS (#3823) Instead of applying the limitations of the least common denominator (Windows) everywhere. --- libtransmission/torrent-files.cc | 11 ++++++++++- tests/libtransmission/torrent-files-test.cc | 12 +++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/libtransmission/torrent-files.cc b/libtransmission/torrent-files.cc index 830f0d52d..6f5b76336 100644 --- a/libtransmission/torrent-files.cc +++ b/libtransmission/torrent-files.cc @@ -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) diff --git a/tests/libtransmission/torrent-files-test.cc b/tests/libtransmission/torrent-files-test.cc index 815a2ae33..0f8073004 100644 --- a/tests/libtransmission/torrent-files-test.cc +++ b/tests/libtransmission/torrent-files-test.cc @@ -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 },