./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
30 / 70
42.9%
Branches:
28 / 151
18.5%
Functions:
4 / 4
100.0%

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