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 * Logger::get_instance() here would deadlock on the magic-static guard that is already held by the
27 * in-progress Logger singleton init.
28 */
29 990 std::wstring resolve_module_directory()
30 {
31 990 HMODULE h_self_module = nullptr;
32 990 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 990 times.
✗ Branch 3 → 127 not taken.
990 if (!GetModuleHandleExW(
38 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
39
2/4
✓ Branch 4 → 5 taken 990 times.
✗ Branch 4 → 6 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 17 taken 990 times.
1980 reinterpret_cast<LPCWSTR>(&Filesystem::get_runtime_directory), &h_self_module) ||
40
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 990 times.
990 h_self_module == nullptr)
41 {
42 const DWORD last_error = GetLastError();
43 throw std::runtime_error("GetModuleHandleExW failed to retrieve module handle. Error: " +
44 std::to_string(last_error));
45 }
46
47 // Dynamic buffer to support paths longer than MAX_PATH
48 990 DWORD buf_size = MAX_PATH;
49
1/2
✓ Branch 19 → 20 taken 990 times.
✗ Branch 19 → 106 not taken.
990 std::wstring module_path_buffer(buf_size, L'\0');
50
51 990 constexpr DWORD MAX_MODULE_PATH = 32768;
52
53 for (;;)
54 {
55
1/2
✓ Branch 23 → 24 taken 990 times.
✗ Branch 23 → 125 not taken.
990 const DWORD path_length = GetModuleFileNameW(h_self_module, module_path_buffer.data(), buf_size);
56
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 33 taken 990 times.
990 if (path_length == 0)
57 {
58 const DWORD last_error = GetLastError();
59 throw std::runtime_error("GetModuleFileNameW failed to retrieve module path. Error: " +
60 std::to_string(last_error));
61 }
62
1/2
✓ Branch 33 → 34 taken 990 times.
✗ Branch 33 → 36 not taken.
990 if (path_length < buf_size)
63 {
64
1/2
✓ Branch 34 → 35 taken 990 times.
✗ Branch 34 → 125 not taken.
990 module_path_buffer.resize(path_length);
65 990 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 990 times.
✗ Branch 45 → 125 not taken.
990 const std::filesystem::path module_full_path(module_path_buffer);
77
2/4
✓ Branch 46 → 47 taken 990 times.
✗ Branch 46 → 121 not taken.
✓ Branch 47 → 48 taken 990 times.
✗ Branch 47 → 119 not taken.
990 result_directory_path = module_full_path.parent_path().wstring();
78 990 }
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 990 times.
990 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 990 return result_directory_path;
131 }
132 } // anonymous namespace
133
134 11162 std::wstring DetourModKit::Filesystem::get_runtime_directory()
135 {
136 // C++11 magic statics guarantee thread-safe, one-time initialization. The module directory never changes at
137 // runtime, so caching is safe.
138
5/8
✓ Branch 2 → 3 taken 992 times.
✓ Branch 2 → 8 taken 10170 times.
✓ Branch 4 → 5 taken 990 times.
✓ Branch 4 → 8 taken 2 times.
✓ Branch 5 → 6 taken 990 times.
✗ Branch 5 → 10 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
11162 static const std::wstring cached_directory = resolve_module_directory();
139 11162 return cached_directory;
140 }
141
142 namespace
143 {
144 2 std::string to_utf8(const std::wstring &wide)
145 {
146
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
2 if (wide.empty())
147 {
148 return {};
149 }
150
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 12 taken 2 times.
2 if (wide.size() > static_cast<size_t>(INT_MAX))
151 {
152 return ".";
153 }
154 2 const int wide_len = static_cast<int>(wide.size());
155
1/2
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 52 not taken.
2 const int needed = WideCharToMultiByte(CP_UTF8, 0, wide.data(), wide_len, nullptr, 0, nullptr, nullptr);
156
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 21 taken 2 times.
2 if (needed <= 0)
157 {
158 return ".";
159 }
160
1/2
✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 44 not taken.
2 std::string out(static_cast<size_t>(needed), '\0');
161 const int written =
162
1/2
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 50 not taken.
2 WideCharToMultiByte(CP_UTF8, 0, wide.data(), wide_len, out.data(), needed, nullptr, nullptr);
163
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 34 taken 2 times.
2 if (written <= 0)
164 {
165 return ".";
166 }
167 2 return out;
168 2 }
169 } // anonymous namespace
170
171 3 std::string DetourModKit::Filesystem::get_runtime_directory_utf8()
172 {
173
5/10
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 11 taken 1 time.
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 11 not taken.
✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 15 not taken.
✓ Branch 6 → 7 taken 2 times.
✗ 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());
174 3 return cached_directory_utf8;
175 }
176 } // namespace DetourModKit
177