GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 46.2% 18 / 0 / 39
Functions: 100.0% 2 / 0 / 2
Branches: 16.5% 15 / 0 / 91

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 * Diagnostics are written to stderr rather than Logger because this
26 * function executes during Logger construction (via generate_log_file_path),
27 * and calling Logger::get_instance() here would deadlock on the magic-static
28 * guard that is already held by the in-progress Logger singleton init.
29 */
30 1 std::string resolve_module_directory()
31 {
32 1 HMODULE h_self_module = nullptr;
33 1 wchar_t module_path_buffer[MAX_PATH] = {0};
34 1 std::string 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 → 84 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
1/2
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 84 not taken.
1 const DWORD path_length = GetModuleFileNameW(h_self_module, module_path_buffer, MAX_PATH);
49
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 27 taken 1 time.
1 if (path_length == 0)
50 {
51 const DWORD last_error = GetLastError();
52 throw std::runtime_error("GetModuleFileNameW failed to retrieve module path. Error: " + std::to_string(last_error));
53 }
54
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 31 taken 1 time.
1 if (path_length >= MAX_PATH)
55 {
56 throw std::runtime_error("GetModuleFileNameW failed: Path buffer was too small.");
57 }
58
59
1/2
✓ Branch 31 → 32 taken 1 time.
✗ Branch 31 → 84 not taken.
1 const std::filesystem::path module_full_path(module_path_buffer);
60
2/4
✓ Branch 32 → 33 taken 1 time.
✗ Branch 32 → 80 not taken.
✓ Branch 33 → 34 taken 1 time.
✗ Branch 33 → 78 not taken.
1 result_directory_path = module_full_path.parent_path().string();
61 1 }
62 catch (const std::filesystem::filesystem_error &fs_err)
63 {
64 std::cerr << "[DMK Filesystem WARNING] Filesystem error: " << fs_err.what()
65 << ". Attempting to fall back to current working directory." << '\n';
66 }
67 catch (const std::exception &e)
68 {
69 std::cerr << "[DMK Filesystem WARNING] Failed to determine module directory: " << e.what()
70 << ". Attempting to fall back to current working directory." << '\n';
71 }
72
73
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 58 taken 1 time.
1 if (result_directory_path.empty())
74 {
75 wchar_t current_dir_buffer[MAX_PATH] = {0};
76 if (GetCurrentDirectoryW(MAX_PATH, current_dir_buffer) > 0)
77 {
78 result_directory_path = std::filesystem::path(current_dir_buffer).string();
79 std::cerr << "[DMK Filesystem WARNING] Using current working directory as fallback: "
80 << result_directory_path << '\n';
81 }
82 else
83 {
84 const DWORD last_error = GetLastError();
85 result_directory_path = ".";
86 std::cerr << "[DMK Filesystem ERROR] Failed to get current working directory."
87 << " Using relative path anchor '.'. Error: " << last_error << '\n';
88 }
89 }
90 1 return result_directory_path;
91 }
92 } // anonymous namespace
93
94 10083 std::string DetourModKit::Filesystem::get_runtime_directory()
95 {
96 // C++11 magic statics guarantee thread-safe, one-time initialization.
97 // The module directory never changes at runtime, so caching is safe.
98
4/8
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 8 taken 10082 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.
10083 static const std::string cached_directory = resolve_module_directory();
99 10083 return cached_directory;
100 }
101