forked from OpenCppCoverage/OpenCppCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandleInformation.cpp
154 lines (126 loc) · 4.71 KB
/
HandleInformation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// OpenCppCoverage is an open source code coverage for C++.
// Copyright (C) 2014 OpenCppCoverage
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
#include "HandleInformation.hpp"
#include <Psapi.h>
#include <boost/algorithm/string.hpp>
#include "CppCoverageException.hpp"
#include "Handle.hpp"
namespace CppCoverage
{
namespace
{
//-------------------------------------------------------------------------
std::vector<std::wstring> GetLogicalDrives()
{
std::vector<wchar_t> logicalDriveStrings(PathBufferSize);
std::vector<std::wstring> logicalDrives;
auto size = GetLogicalDriveStrings(
static_cast<int>(logicalDriveStrings.size()),
&logicalDriveStrings[0]);
if (!size)
THROW(L"Cannot GetLogicalDriveStrings");
size_t startIndex = 0;
for (size_t i = 0; i < size; ++i)
{
if (!logicalDriveStrings[i])
{
std::wstring localDrive(&logicalDriveStrings[startIndex], &logicalDriveStrings[i]);
logicalDrives.push_back(localDrive);
startIndex = i + 1;
}
}
return logicalDrives;
}
//-------------------------------------------------------------------------
std::vector<std::pair<std::wstring, std::wstring>> GetQueryDosDevicesMapping()
{
std::vector<wchar_t> dosDevice(PathBufferSize);
std::vector<std::pair<std::wstring, std::wstring>> queryDosDevicesMapping;
for (const auto& logicalDrive : GetLogicalDrives())
{
auto pos = logicalDrive.find('\\');
std::wstring drive = (pos != std::string::npos) ? logicalDrive.substr(0, pos) : logicalDrive;
if (QueryDosDevice(drive.c_str(), &dosDevice[0], static_cast<int>(dosDevice.size())))
{
std::wstring dosDeviceName{ &dosDevice[0] };
queryDosDevicesMapping.emplace_back(dosDeviceName, drive);
}
}
// Handle network drive. We just remove prefix.
queryDosDevicesMapping.emplace_back(L"\\Device\\Mup", L"\\");
return queryDosDevicesMapping;
}
//-------------------------------------------------------------------------
std::wstring GetMappedFileNameStr(HANDLE hfile)
{
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hfile, &dwFileSizeHi);
if (dwFileSizeLo == 0 && dwFileSizeHi == 0)
THROW(L"Cannot map a file with a length of zero.");
auto fileMappingHandle = CreateHandle(
CreateFileMapping(hfile, NULL, PAGE_READONLY, 0, 1, NULL),
CloseHandle);
auto mapViewOfFile = CreateHandle(
MapViewOfFile(fileMappingHandle.GetValue(), FILE_MAP_READ, 0, 0, 1),
UnmapViewOfFile);
TCHAR pszFilename[MAX_PATH + 1];
if (!GetMappedFileName(GetCurrentProcess(),
mapViewOfFile.GetValue(),
pszFilename,
MAX_PATH))
{
THROW(L"Cannot GetMappedFileName");
}
return pszFilename;
}
//-------------------------------------------------------------------------
std::wstring GetFinalPathName(HANDLE hfile)
{
std::vector<wchar_t> buffer(PathBufferSize);
if (GetFinalPathNameByHandle(hfile, &buffer[0], static_cast<int>(buffer.size()) - 1, VOLUME_NAME_NT))
return &buffer[0];
return GetMappedFileNameStr(hfile);
}
}
//-------------------------------------------------------------------------
HandleInformation::HandleInformation() = default;
//-------------------------------------------------------------------------
std::wstring HandleInformation::ComputeFilename(HANDLE hfile) const
{
if (hfile == nullptr)
{
THROW(L"OpenCppCoverage cannot find the name of the module.\n"
L"See "
L"https://github.com/OpenCppCoverage/OpenCppCoverage/wiki/"
L"FAQ#error-opencppcoverage-cannot-find-the-name-of-the-"
L"module for more information.");
}
auto mappedFileName = GetFinalPathName(hfile);
auto queryDosDevicesMapping = GetQueryDosDevicesMapping();
for (const auto& queryDosDeviceMapping : queryDosDevicesMapping)
{
const auto& deviceName = queryDosDeviceMapping.first;
const auto& logicalDrive = queryDosDeviceMapping.second;
if (boost::algorithm::istarts_with(mappedFileName, deviceName))
{
boost::algorithm::ireplace_first(mappedFileName, deviceName, logicalDrive);
return mappedFileName;
}
}
THROW(L"Cannot find path for the handle: " << mappedFileName);
}
}