GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 45.6% 36 / 0 / 79
Functions: 100.0% 4 / 0 / 4
Branches: 19.4% 30 / 0 / 155

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