From b943a9b0572fe3cfd6c8892b4d9b1de4c4871417 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 8 Mar 2013 09:56:19 +0000 Subject: [PATCH] merged long-name truncation fix from RC_0_16 --- ChangeLog | 1 + src/torrent_info.cpp | 51 +++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index a26cf6dfe..e8847730a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ * fix uTP edge case where udp socket buffer fills up * fix nagle implementation in uTP + * fix long filename truncation on windows * distinguish file open mode when checking files and downloading/seeding with bittorrent. updates storage interface * improve file_storage::map_file when dealing with invalid input * improve handling of invalid utf-8 sequences in strings in torrent files diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 9f903df95..81ec99ead 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -189,23 +189,48 @@ namespace libtorrent return true; } - TORRENT_EXTRA_EXPORT void trim_path_element(std::string& path_element) + TORRENT_EXTRA_EXPORT void trim_path_element(std::string& element) { const int max_path_len = TORRENT_MAX_PATH; - if (int(path_element.size()) > max_path_len) + + // on windows, the max path is expressed in + // unicode characters, not bytes +#if defined TORRENT_WINDOWS + std::wstring path_element; + utf8_wchar(element, path_element); + if (path_element.size() <= max_path_len) return; + + // truncate filenames that are too long. But keep extensions! + std::wstring ext; + wchar_t const* ext1 = wcsrchr(path_element.c_str(), '.'); + if (ext1 != NULL) ext = ext1; + + if (ext.size() > 15) { - // truncate filenames that are too long. But keep extensions! - std::string ext = extension(path_element); - if (ext.size() > 15) - { - path_element.resize(max_path_len); - } - else - { - path_element.resize(max_path_len - ext.size()); - path_element += ext; - } + path_element.resize(max_path_len); } + else + { + path_element.resize(max_path_len - ext.size()); + path_element += ext; + } + wchar_utf8(path_element, element); +#else + std::string& path_element = element; + if (int(path_element.size()) <= max_path_len) return; + + // truncate filenames that are too long. But keep extensions! + std::string ext = extension(path_element); + if (ext.size() > 15) + { + path_element.resize(max_path_len); + } + else + { + path_element.resize(max_path_len - ext.size()); + path_element += ext; + } +#endif } TORRENT_EXTRA_EXPORT std::string sanitize_path(std::string const& p)