Repository navigation
Expand file tree
/
Copy pathenvironment.cpp
More file actions
220 lines (179 loc) · 7.41 KB
/
Copy pathenvironment.cpp
File metadata and controls
220 lines (179 loc) · 7.41 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) 2024 The RefValue Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
module;
#include <essence/compat.hpp>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOGDI
#define NOGDI
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#include <shellapi.h>
#else
#include <dlfcn.h>
#include <unistd.h>
#endif
module essence.basic;
import std;
import :abi.string;
import :abi.vector;
import :encoding;
import lib.spdlog;
namespace essence {
namespace {
std::mutex deleting_mutex;
std::vector<std::filesystem::path> directories_to_delete;
#ifdef _WIN32
const auto command_line_args{[] {
abi::vector<abi::string> result;
if (std::int32_t size{}; std::unique_ptr<wchar_t* [], decltype(&LocalFree)> args {
CommandLineToArgvW(GetCommandLineW(), &size), &LocalFree
}) {
result.reserve(size);
for (std::size_t i = 0; i < static_cast<std::size_t>(size); i++) {
result.emplace_back(to_utf8_string(args[i]));
}
}
return result;
}()};
void create_lazy_rmdir_process() {
static constexpr std::string_view rmdir_pattern{R"(rmdir /s /q "{}" & )"};
static constexpr std::string_view prefix{R"(cmd.exe /c timeout /t 3 /nobreak > nul & )"};
std::string command_line{prefix};
{
std::scoped_lock lock{deleting_mutex};
for (auto&& item : directories_to_delete) {
command_line.append(
format(rmdir_pattern, item.generic_u8string() | std::ranges::to<std::string>()));
}
}
command_line.erase(command_line.size() - 3);
PROCESS_INFORMATION pi{};
STARTUPINFOW si{
.cb = sizeof(STARTUPINFOW),
};
auto native_command_line = to_native_string(command_line);
if (CreateProcessW(nullptr, native_command_line.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr,
nullptr, &si, &pi)) {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
abi::string get_module_path(std::uint32_t flags, const void* name_or_address) {
if (HMODULE module; GetModuleHandleExW(flags, static_cast<const wchar_t*>(name_or_address), &module)) {
std::wstring path(MAX_PATH, L'\0');
path.resize(GetModuleFileNameW(module, path.data(), static_cast<DWORD>(path.size())));
path.shrink_to_fit();
return to_utf8_string(path);
}
return {};
}
#else
char** raw_argv;
std::size_t raw_argc;
abi::vector<abi::string> command_line_args;
// http://www.dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html
// What is this __init_array? I thought you'd never ask. You can have code run at this stage as well.
// Since this is just after returning from running _init which ran our constructors,
// that means anything in this array will run after constructors are done.
// You can tell the compiler you want a function to run at this phase. The function will receive the same
// arguments as main.
[[maybe_unused]] __attribute__((used, section(".init_array"))) void (*get_argc_argv)(
int, char**, char**) = [](int argc, char** argv, [[maybe_unused]] char** envp) {
raw_argc = static_cast<std::size_t>(argc);
raw_argv = argv;
};
[[maybe_unused]] ES_KEEP_ALIVE struct init {
init() {
command_line_args.reserve(raw_argc);
for (std::size_t i = 0; i < raw_argc; i++) {
command_line_args.emplace_back(raw_argv[i]);
}
}
} force_init;
std::filesystem::path get_module_full_path(std::string_view filename) {
std::ifstream stream{format("/proc/{}/maps", getpid()), std::ios_base::in};
std::string line;
while (std::getline(stream, line)) {
const auto components = line | std::views::split(' ') | std::views::transform([](const auto& inner) {
return std::filesystem::path{inner.begin(), inner.end()};
}) | std::views::filter([](const auto& inner) { return inner.is_absolute(); })
| std::ranges::to<std::vector>();
if (!components.empty() && components.back().filename() == filename) {
return components.back();
}
}
return {};
}
#endif
} // namespace
abi::string get_executable_path() {
#ifdef _WIN32
return get_module_path(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nullptr);
#else
return {};
#endif
}
abi::string get_module_path(const void* address) {
#ifdef _WIN32
return get_module_path(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, address);
#else
if (Dl_info info{}; dladdr(address, &info)) {
if (std::filesystem::path path{info.dli_fname}; path.is_relative()) {
return abi::to_abi_string(
get_module_full_path(path.generic_u8string() | std::ranges::to<std::string>()).generic_u8string()
| std::ranges::to<std::string>());
}
return info.dli_fname;
}
#endif
return {};
}
abi::vector<abi::string> get_command_line_args() {
return command_line_args;
}
void delete_directory_at_exit(std::string_view path) {
static std::once_flag flag;
std::call_once(flag, [] {
static_cast<void>(std::atexit([] {
spdlog::info("Starting to delete scheduled directories...");
#ifdef _WIN32
create_lazy_rmdir_process();
#else
std::error_code code;
std::scoped_lock lock{deleting_mutex};
for (auto&& item : directories_to_delete) {
std::filesystem::remove_all(item, code);
}
#endif
}));
});
std::scoped_lock lock{deleting_mutex};
directories_to_delete.emplace_back(path | std::ranges::to<std::u8string>());
}
} // namespace essence