-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathplatformutil.cpp
More file actions
201 lines (191 loc) · 7.05 KB
/
Copy pathplatformutil.cpp
File metadata and controls
201 lines (191 loc) · 7.05 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "platformutil.h"
#include "util.h"
#include "strutil.h"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/mman.h>
#endif
namespace platform {
#ifdef _WIN32
void throw_error(const std::string &msg, DWORD code)
{
LPWSTR pszMsg = 0;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&pszMsg,
0,
0);
std::string ss;
if (pszMsg) {
strutil::squeeze(pszMsg, L"\r\n");
ss = strutil::format("%s: %s", msg.c_str(),
strutil::w2us(pszMsg).c_str());
LocalFree(pszMsg);
}
else if (code < 0xfe00)
ss = strutil::format("%d: %s", code, msg.c_str());
else
ss = strutil::format("%08x: %s", code, msg.c_str());
throw std::runtime_error(ss);
}
FILE *tmpfile(const std::string &prefix)
{
std::wstring sprefix =
strutil::format(L"%s.%d.", strutil::us2w(prefix).c_str(),
GetCurrentProcessId());
wchar_t *tmpname = _wtempnam(0, sprefix.c_str());
std::shared_ptr<wchar_t> tmpname_p(tmpname, std::free);
std::string utmpname = strutil::w2us(tmpname);
HANDLE fh = CreateFileW(prefixed_path(tmpname).c_str(),
GENERIC_READ | GENERIC_WRITE,
0, 0, CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY |
FILE_FLAG_DELETE_ON_CLOSE,
0);
if (fh == INVALID_HANDLE_VALUE)
throw_error(utmpname, GetLastError());
int fd = _open_osfhandle(reinterpret_cast<intptr_t>(fh),
_O_BINARY|_O_RDWR);
if (fd == -1) {
CloseHandle(fh);
util::throw_crt_error("platform::tmpfile: open_osfhandle()");
}
FILE *fp = _fdopen(fd, "w+");
if (!fp) {
_close(fd);
util::throw_crt_error("platform::tmpfile: _fdopen()");
}
return fp;
}
char *load_with_mmap(const std::string &path, uint64_t *size)
{
std::wstring fullpath = prefixed_path(strutil::us2w(path).c_str());
HANDLE hFile = CreateFileW(fullpath.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
throw_error(path, GetLastError());
DWORD sizeHi, sizeLo;
sizeLo = GetFileSize(hFile, &sizeHi);
*size = (static_cast<uint64_t>(sizeHi) << 32) | sizeLo;
HANDLE hMap = CreateFileMappingW(hFile, 0, PAGE_READONLY, 0, 0, 0);
DWORD err = GetLastError();
CloseHandle(hFile);
if (!hMap) throw_error("CreateFileMapping", err);
char *view =
reinterpret_cast<char*>( MapViewOfFile(hMap, FILE_MAP_READ,
0, 0, 0));
CloseHandle(hMap);
return view;
}
int create_named_pipe(const std::string &path)
{
std::wstring wpath = strutil::us2w(path);
HANDLE fh = CreateNamedPipeW(wpath.c_str(),
PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE | PIPE_WAIT,
1, 0, 0, 0, 0);
if (fh == INVALID_HANDLE_VALUE)
throw_error(path, GetLastError());
ConnectNamedPipe(fh, 0);
return _open_osfhandle(reinterpret_cast<intptr_t>(fh),
_O_WRONLY | _O_BINARY);
}
std::string get_dll_version_for_locale(HMODULE hDll, WORD langid)
{
HRSRC hRes = FindResourceExW(hDll,
RT_VERSION,
MAKEINTRESOURCEW(VS_VERSION_INFO),
langid);
if (!hRes)
platform::throw_error("FindResourceExW", GetLastError());
std::string data;
{
DWORD cbres = SizeofResource(hDll, hRes);
HGLOBAL hMem = LoadResource(hDll, hRes);
if (hMem) {
char *pc = static_cast<char*>(LockResource(hMem));
if (cbres && pc)
data.assign(pc, cbres);
FreeResource(hMem);
}
}
// find dwSignature of VS_FIXEDFILEINFO
size_t pos = data.find("\xbd\x04\xef\xfe");
if (pos != std::string::npos) {
VS_FIXEDFILEINFO vfi;
std::memcpy(&vfi, data.c_str() + pos, sizeof vfi);
WORD v[4];
v[0] = HIWORD(vfi.dwFileVersionMS);
v[1] = LOWORD(vfi.dwFileVersionMS);
v[2] = HIWORD(vfi.dwFileVersionLS);
v[3] = LOWORD(vfi.dwFileVersionLS);
return strutil::format("%u.%u.%u.%u", v[0],v[1],v[2],v[3]);
}
return "";
}
bool is_same_file(int fda, int fdb)
{
HANDLE ha = get_handle(fda), hb = get_handle(fdb);
BY_HANDLE_FILE_INFORMATION bhfia, bhfib;
if (!GetFileInformationByHandle(ha, &bhfia)) return false;
if (!GetFileInformationByHandle(hb, &bhfib)) return false;
return bhfia.dwVolumeSerialNumber == bhfib.dwVolumeSerialNumber
&& bhfia.nFileIndexHigh == bhfib.nFileIndexHigh
&& bhfia.nFileIndexLow == bhfib.nFileIndexLow;
}
#else
FILE *tmpfile(const std::string &prefix)
{
std::string dir = getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp";
std::string tmpl = dir + "/" + prefix + ".XXXXXX";
std::vector<char> buf(tmpl.begin(), tmpl.end());
buf.push_back(0);
int fd = mkstemp(buf.data());
if (fd == -1)
util::throw_crt_error("platform::tmpfile: mkstemp()");
unlink(buf.data());
FILE *fp = fdopen(fd, "w+");
if (!fp) {
close(fd);
util::throw_crt_error("platform::tmpfile: fdopen()");
}
return fp;
}
char *load_with_mmap(const std::string &path, uint64_t *size)
{
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1)
util::throw_crt_error(path);
struct stat st;
if (fstat(fd, &st) != 0) {
close(fd);
util::throw_crt_error(path);
}
*size = static_cast<uint64_t>(st.st_size);
void *view = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (view == MAP_FAILED)
util::throw_crt_error(path);
return static_cast<char*>(view);
}
int create_named_pipe(const std::string &)
{
throw std::runtime_error("create_named_pipe: not implemented");
}
bool is_same_file(int fda, int fdb)
{
struct stat sta, stb;
if (fstat(fda, &sta) != 0) return false;
if (fstat(fdb, &stb) != 0) return false;
return sta.st_dev == stb.st_dev && sta.st_ino == stb.st_ino;
}
#endif
}