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
|
#include <vcpkg-test/catch.h>
#include <vcpkg-test/util.h>
#include <vcpkg/base/checks.h>
#include <vcpkg/base/files.h>
#include <vcpkg/statusparagraph.h>
// used to get the implementation specific compiler flags (i.e., __cpp_lib_filesystem)
#include <ciso646>
#include <iostream>
#include <memory>
#if defined(_WIN32)
#include <windows.h>
#endif
#define FILESYSTEM_SYMLINK_STD 0
#define FILESYSTEM_SYMLINK_UNIX 1
#define FILESYSTEM_SYMLINK_NONE 2
#if defined(__cpp_lib_filesystem)
#define FILESYSTEM_SYMLINK FILESYSTEM_SYMLINK_STD
#include <filesystem> // required for filesystem::create_{directory_}symlink
#elif !defined(_MSC_VER)
#define FILESYSTEM_SYMLINK FILESYSTEM_SYMLINK_UNIX
#include <unistd.h>
#else
#define FILESYSTEM_SYMLINK FILESYSTEM_SYMLINK_NONE
#endif
namespace vcpkg::Test
{
std::unique_ptr<vcpkg::StatusParagraph> make_status_pgh(const char* name,
const char* depends,
const char* default_features,
const char* triplet)
{
using Pgh = std::unordered_map<std::string, std::string>;
return std::make_unique<StatusParagraph>(Pgh{{"Package", name},
{"Version", "1"},
{"Architecture", triplet},
{"Multi-Arch", "same"},
{"Depends", depends},
{"Default-Features", default_features},
{"Status", "install ok installed"}});
}
std::unique_ptr<StatusParagraph> make_status_feature_pgh(const char* name,
const char* feature,
const char* depends,
const char* triplet)
{
using Pgh = std::unordered_map<std::string, std::string>;
return std::make_unique<StatusParagraph>(Pgh{{"Package", name},
{"Version", "1"},
{"Feature", feature},
{"Architecture", triplet},
{"Multi-Arch", "same"},
{"Depends", depends},
{"Status", "install ok installed"}});
}
PackageSpec unsafe_pspec(std::string name, Triplet t)
{
auto m_ret = PackageSpec::from_name_and_triplet(name, t);
REQUIRE(m_ret.has_value());
return m_ret.value_or_exit(VCPKG_LINE_INFO);
}
static AllowSymlinks internal_can_create_symlinks() noexcept
{
#if FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_NONE
return AllowSymlinks::No;
#elif FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_UNIX
return AllowSymlinks::Yes;
#elif !defined(_WIN32) // FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_STD
return AllowSymlinks::Yes;
#else
HKEY key;
bool allow_symlinks = true;
const auto status = RegOpenKeyExW(
HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock)", 0, 0, &key);
if (status == ERROR_FILE_NOT_FOUND)
{
allow_symlinks = false;
std::clog << "Symlinks are not allowed on this system\n";
}
if (status == ERROR_SUCCESS) RegCloseKey(key);
return allow_symlinks ? AllowSymlinks::Yes : AllowSymlinks::No;
#endif
}
const static AllowSymlinks CAN_CREATE_SYMLINKS = internal_can_create_symlinks();
AllowSymlinks can_create_symlinks() noexcept { return CAN_CREATE_SYMLINKS; }
static fs::path internal_base_temporary_directory()
{
#if defined(_WIN32)
wchar_t* tmp = static_cast<wchar_t*>(std::calloc(32'767, 2));
if (!GetEnvironmentVariableW(L"TEMP", tmp, 32'767))
{
std::cerr << "No temporary directory found.\n";
std::abort();
}
fs::path result = tmp;
std::free(tmp);
return result / L"vcpkg-test";
#else
return "/tmp/vcpkg-test";
#endif
}
const static fs::path BASE_TEMPORARY_DIRECTORY = internal_base_temporary_directory();
const fs::path& base_temporary_directory() noexcept { return BASE_TEMPORARY_DIRECTORY; }
#if FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_NONE
constexpr char no_filesystem_message[] =
"<filesystem> doesn't exist; on windows, we don't attempt to use the win32 calls to create symlinks";
#endif
void create_symlink(const fs::path& target, const fs::path& file, std::error_code& ec)
{
#if FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_STD
if (can_create_symlinks())
{
std::filesystem::path targetp = target.native();
std::filesystem::path filep = file.native();
std::filesystem::create_symlink(targetp, filep);
}
else
{
vcpkg::Checks::exit_with_message(VCPKG_LINE_INFO, "Symlinks are not allowed on this system");
}
#elif FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_UNIX
if (symlink(target.c_str(), file.c_str()) != 0)
{
ec.assign(errno, std::system_category());
}
#else
vcpkg::Checks::exit_with_message(VCPKG_LINE_INFO, no_filesystem_message);
#endif
}
void create_directory_symlink(const fs::path& target, const fs::path& file, std::error_code& ec)
{
#if FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_STD
if (can_create_symlinks())
{
std::filesystem::path targetp = target.native();
std::filesystem::path filep = file.native();
std::filesystem::create_directory_symlink(targetp, filep);
}
else
{
vcpkg::Checks::exit_with_message(VCPKG_LINE_INFO, "Symlinks are not allowed on this system");
}
#elif FILESYSTEM_SYMLINK == FILESYSTEM_SYMLINK_UNIX
::vcpkg::Test::create_symlink(target, file, ec);
#else
vcpkg::Checks::exit_with_message(VCPKG_LINE_INFO, no_filesystem_message);
#endif
}
}
|