| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/FileSearch.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <filesystem> |
| 8 | #include <system_error> |
| 9 | |
| 10 | #include "Common/CommonPaths.h" |
| 11 | #include "Common/Logging/Log.h" |
| 12 | #include "Common/StringUtil.h" |
| 13 | |
| 14 | #ifdef _MSC_VER |
| 15 | #include <Windows.h> |
| 16 | #elifdef ANDROID |
| 17 | #include "jni/AndroidCommon/AndroidCommon.h" |
| 18 | #endif |
| 19 | |
| 20 | namespace fs = std::filesystem; |
| 21 | |
| 22 | namespace Common |
| 23 | { |
| 24 | std::vector<std::string> DoFileSearch(std::span<const std::string_view> directories, |
| 25 | std::span<const std::string_view> exts, bool recursive) |
| 26 | { |
| 27 | const bool accept_all = exts.empty(); |
| 28 | |
| 29 | std::vector<fs::path> native_exts; |
| 30 | for (const auto& ext : exts) |
| 31 | native_exts.push_back(StringToPath(ext)); |
| 32 | |
| 33 | // N.B. This avoids doing any copies |
| 34 | auto ext_matches = [&native_exts](const fs::path& path) { |
| 35 | const std::basic_string_view<fs::path::value_type> native_path = path.native(); |
| 36 | return std::ranges::any_of(native_exts, [&native_path](const auto& ext) { |
| 37 | const auto compare_len = ext.native().length(); |
| 38 | if (native_path.length() < compare_len) |
| 39 | return false; |
| 40 | const auto substr_to_compare = native_path.substr(native_path.length() - compare_len); |
| 41 | #ifdef _WIN32 |
| 42 | return CompareStringOrdinal(substr_to_compare.data(), static_cast<int>(compare_len), |
| 43 | ext.c_str(), static_cast<int>(compare_len), TRUE) == CSTR_EQUAL; |
| 44 | #else |
| 45 | return strncasecmp(substr_to_compare.data(), ext.c_str(), compare_len) == 0; |
| 46 | #endif |
| 47 | }); |
| 48 | }; |
| 49 | |
| 50 | std::vector<std::string> result; |
| 51 | auto add_filtered = [&](const fs::directory_entry& entry) { |
| 52 | auto& path = entry.path(); |
| 53 | if (accept_all || (!entry.is_directory() && ext_matches(path))) |
| 54 | result.emplace_back(PathToString(path)); |
| 55 | }; |
| 56 | for (const auto& directory : directories) |
| 57 | { |
| 58 | #ifdef ANDROID |
| 59 | if (IsPathAndroidContent(directory)) |
| 60 | { |
| 61 | const std::vector<std::string> partial_result = |
| 62 | DoFileSearchAndroidContent(directory, exts, recursive); |
| 63 | |
| 64 | result.insert(result.end(), std::make_move_iterator(partial_result.begin()), |
| 65 | std::make_move_iterator(partial_result.end())); |
| 66 | } |
| 67 | else |
| 68 | #endif |
| 69 | { |
| 70 | fs::path directory_path = StringToPath(directory); |
| 71 | std::error_code error; |
| 72 | if (recursive) |
| 73 | { |
| 74 | for (auto it = fs::recursive_directory_iterator(directory_path, error); |
| 75 | it != fs::recursive_directory_iterator(); it.increment(error)) |
| 76 | add_filtered(*it); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | for (auto it = fs::directory_iterator(directory_path, error); |
| 81 | it != fs::directory_iterator(); it.increment(error)) |
| 82 | add_filtered(*it); |
| 83 | } |
| 84 | if (error) |
| 85 | ERROR_LOG_FMT(COMMON, "{} error on {}: {}", __func__, directory, error.message()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Remove duplicates (occurring because caller gave e.g. duplicate or overlapping directories - |
| 90 | // not because std::filesystem returns duplicates). Also note that this pathname-based uniqueness |
| 91 | // isn't as thorough as std::filesystem::equivalent. |
| 92 | std::ranges::sort(result); |
| 93 | const auto unique_result = std::ranges::unique(result); |
| 94 | result.erase(unique_result.begin(), unique_result.end()); |
| 95 | |
| 96 | // Dolphin expects to be able to use "/" (DIR_SEP) everywhere. |
| 97 | // std::filesystem uses the OS separator. |
| 98 | constexpr fs::path::value_type os_separator = fs::path::preferred_separator; |
| 99 | static_assert(os_separator == DIR_SEP_CHR || os_separator == '\\', "Unsupported path separator"); |
| 100 | if constexpr (os_separator != DIR_SEP_CHR) |
| 101 | { |
| 102 | for (auto& path : result) |
| 103 | std::ranges::replace(path, '\\', DIR_SEP_CHR); |
| 104 | } |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | } // namespace Common |