src/filesystem.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file filesystem.cpp | ||
| 3 | * @brief Implementation of file system utilities. | ||
| 4 | * | ||
| 5 | * Provides functions for file system operations, such as retrieving the directory | ||
| 6 | * of the currently executing module. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "DetourModKit/filesystem.hpp" | ||
| 10 | |||
| 11 | #include <windows.h> | ||
| 12 | #include <filesystem> | ||
| 13 | #include <iostream> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | |||
| 17 | using namespace DetourModKit; | ||
| 18 | |||
| 19 | namespace | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * @brief Resolves the directory of the currently executing module. | ||
| 23 | * | ||
| 24 | * @details Called exactly once; the result is cached by the caller. | ||
| 25 | * Returns a wide string to preserve full Unicode fidelity on Windows. | ||
| 26 | * Diagnostics are written to stderr rather than Logger because this | ||
| 27 | * function executes during Logger construction (via generate_log_file_path), | ||
| 28 | * and calling Logger::get_instance() here would deadlock on the magic-static | ||
| 29 | * guard that is already held by the in-progress Logger singleton init. | ||
| 30 | */ | ||
| 31 | 1 | std::wstring resolve_module_directory() | |
| 32 | { | ||
| 33 | 1 | HMODULE h_self_module = nullptr; | |
| 34 | 1 | std::wstring result_directory_path; | |
| 35 | |||
| 36 | try | ||
| 37 | { | ||
| 38 | // Use the address of the public function to locate the containing module (DLL or EXE). | ||
| 39 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 127 not taken.
|
1 | if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 40 | reinterpret_cast<LPCWSTR>(&Filesystem::get_runtime_directory), | ||
| 41 |
2/4✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 17 taken 1 time.
|
2 | &h_self_module) || |
| 42 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
|
1 | h_self_module == nullptr) |
| 43 | { | ||
| 44 | ✗ | const DWORD last_error = GetLastError(); | |
| 45 | ✗ | throw std::runtime_error("GetModuleHandleExW failed to retrieve module handle. Error: " + std::to_string(last_error)); | |
| 46 | } | ||
| 47 | |||
| 48 | // Dynamic buffer to support paths longer than MAX_PATH | ||
| 49 | 1 | DWORD buf_size = MAX_PATH; | |
| 50 |
1/2✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 106 not taken.
|
1 | std::wstring module_path_buffer(buf_size, L'\0'); |
| 51 | |||
| 52 | 1 | constexpr DWORD MAX_MODULE_PATH = 32768; | |
| 53 | |||
| 54 | for (;;) | ||
| 55 | { | ||
| 56 |
1/2✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 125 not taken.
|
1 | const DWORD path_length = GetModuleFileNameW(h_self_module, module_path_buffer.data(), buf_size); |
| 57 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 33 taken 1 time.
|
1 | if (path_length == 0) |
| 58 | { | ||
| 59 | ✗ | const DWORD last_error = GetLastError(); | |
| 60 | ✗ | throw std::runtime_error("GetModuleFileNameW failed to retrieve module path. Error: " + std::to_string(last_error)); | |
| 61 | } | ||
| 62 |
1/2✓ Branch 33 → 34 taken 1 time.
✗ Branch 33 → 36 not taken.
|
1 | if (path_length < buf_size) |
| 63 | { | ||
| 64 |
1/2✓ Branch 34 → 35 taken 1 time.
✗ Branch 34 → 125 not taken.
|
1 | module_path_buffer.resize(path_length); |
| 65 | 1 | break; | |
| 66 | } | ||
| 67 | ✗ | if (buf_size >= MAX_MODULE_PATH) | |
| 68 | { | ||
| 69 | ✗ | throw std::runtime_error("GetModuleFileNameW: path exceeds maximum supported length."); | |
| 70 | } | ||
| 71 | // Buffer was too small, double and retry | ||
| 72 | ✗ | buf_size = (buf_size <= MAX_MODULE_PATH / 2) ? buf_size * 2 : MAX_MODULE_PATH; | |
| 73 | ✗ | module_path_buffer.resize(buf_size, L'\0'); | |
| 74 | ✗ | } | |
| 75 | |||
| 76 |
1/2✓ Branch 45 → 46 taken 1 time.
✗ Branch 45 → 125 not taken.
|
1 | const std::filesystem::path module_full_path(module_path_buffer); |
| 77 |
2/4✓ Branch 46 → 47 taken 1 time.
✗ Branch 46 → 121 not taken.
✓ Branch 47 → 48 taken 1 time.
✗ Branch 47 → 119 not taken.
|
1 | result_directory_path = module_full_path.parent_path().wstring(); |
| 78 | 1 | } | |
| 79 | ✗ | catch (const std::filesystem::filesystem_error &fs_err) | |
| 80 | { | ||
| 81 | ✗ | std::cerr << "[DMK Filesystem WARNING] Filesystem error: " << fs_err.what() | |
| 82 | ✗ | << ". Attempting to fall back to current working directory." << '\n'; | |
| 83 | ✗ | } | |
| 84 | ✗ | catch (const std::exception &e) | |
| 85 | { | ||
| 86 | ✗ | std::cerr << "[DMK Filesystem WARNING] Failed to determine module directory: " << e.what() | |
| 87 | ✗ | << ". Attempting to fall back to current working directory." << '\n'; | |
| 88 | ✗ | } | |
| 89 | |||
| 90 |
1/2✗ Branch 55 → 56 not taken.
✓ Branch 55 → 96 taken 1 time.
|
1 | if (result_directory_path.empty()) |
| 91 | { | ||
| 92 | ✗ | DWORD cwd_len = GetCurrentDirectoryW(0, nullptr); | |
| 93 | ✗ | std::wstring current_dir_buffer; | |
| 94 | |||
| 95 | ✗ | if (cwd_len > 0) | |
| 96 | { | ||
| 97 | ✗ | current_dir_buffer.resize(cwd_len, L'\0'); | |
| 98 | ✗ | const DWORD written = GetCurrentDirectoryW(cwd_len, current_dir_buffer.data()); | |
| 99 | ✗ | if (written > 0 && written < cwd_len) | |
| 100 | { | ||
| 101 | ✗ | current_dir_buffer.resize(written); | |
| 102 | } | ||
| 103 | ✗ | else if (written >= cwd_len) | |
| 104 | { | ||
| 105 | // Directory changed between calls, retry with new size | ||
| 106 | ✗ | current_dir_buffer.resize(static_cast<size_t>(written) + 1, L'\0'); | |
| 107 | ✗ | const DWORD retry = GetCurrentDirectoryW(written + 1, current_dir_buffer.data()); | |
| 108 | ✗ | current_dir_buffer.resize(retry > 0 ? retry : 0); | |
| 109 | } | ||
| 110 | else | ||
| 111 | { | ||
| 112 | ✗ | current_dir_buffer.clear(); | |
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | ✗ | if (!current_dir_buffer.empty()) | |
| 117 | { | ||
| 118 | ✗ | result_directory_path = std::filesystem::path(current_dir_buffer).wstring(); | |
| 119 | std::cerr << "[DMK Filesystem WARNING] Using current working directory as fallback: " | ||
| 120 | ✗ | << std::filesystem::path(result_directory_path).string() << '\n'; | |
| 121 | } | ||
| 122 | else | ||
| 123 | { | ||
| 124 | ✗ | const DWORD last_error = GetLastError(); | |
| 125 | ✗ | result_directory_path = L"."; | |
| 126 | std::cerr << "[DMK Filesystem ERROR] Failed to get current working directory." | ||
| 127 | ✗ | << " Using relative path anchor '.'. Error: " << last_error << '\n'; | |
| 128 | } | ||
| 129 | ✗ | } | |
| 130 | 1 | return result_directory_path; | |
| 131 | ✗ | } | |
| 132 | } // anonymous namespace | ||
| 133 | |||
| 134 | 10159 | std::wstring DetourModKit::Filesystem::get_runtime_directory() | |
| 135 | { | ||
| 136 | // C++11 magic statics guarantee thread-safe, one-time initialization. | ||
| 137 | // The module directory never changes at runtime, so caching is safe. | ||
| 138 |
4/8✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 8 taken 10158 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 10 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
|
10159 | static const std::wstring cached_directory = resolve_module_directory(); |
| 139 | 10159 | return cached_directory; | |
| 140 | } | ||
| 141 | |||
| 142 | namespace | ||
| 143 | { | ||
| 144 | 1 | std::string to_utf8(const std::wstring &wide) | |
| 145 | { | ||
| 146 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
|
1 | if (wide.empty()) |
| 147 | { | ||
| 148 | ✗ | return {}; | |
| 149 | } | ||
| 150 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 12 taken 1 time.
|
1 | if (wide.size() > static_cast<size_t>(INT_MAX)) |
| 151 | { | ||
| 152 | ✗ | return "."; | |
| 153 | } | ||
| 154 | 1 | const int wide_len = static_cast<int>(wide.size()); | |
| 155 |
1/2✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 52 not taken.
|
1 | const int needed = WideCharToMultiByte(CP_UTF8, 0, wide.data(), wide_len, |
| 156 | nullptr, 0, nullptr, nullptr); | ||
| 157 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 21 taken 1 time.
|
1 | if (needed <= 0) |
| 158 | { | ||
| 159 | ✗ | return "."; | |
| 160 | } | ||
| 161 |
1/2✓ Branch 23 → 24 taken 1 time.
✗ Branch 23 → 44 not taken.
|
1 | std::string out(static_cast<size_t>(needed), '\0'); |
| 162 |
1/2✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 50 not taken.
|
1 | const int written = WideCharToMultiByte(CP_UTF8, 0, wide.data(), wide_len, |
| 163 | out.data(), needed, nullptr, nullptr); | ||
| 164 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 34 taken 1 time.
|
1 | if (written <= 0) |
| 165 | { | ||
| 166 | ✗ | return "."; | |
| 167 | } | ||
| 168 | 1 | return out; | |
| 169 | 1 | } | |
| 170 | } // anonymous namespace | ||
| 171 | |||
| 172 | 3 | std::string DetourModKit::Filesystem::get_runtime_directory_utf8() | |
| 173 | { | ||
| 174 |
5/10✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 11 taken 2 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 11 not taken.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 15 not taken.
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 13 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
|
3 | static const std::string cached_directory_utf8 = to_utf8(get_runtime_directory()); |
| 175 | 3 | return cached_directory_utf8; | |
| 176 | } | ||
| 177 |