Repository navigation
Expand file tree
/
Copy pathlink_service.cpp
More file actions
146 lines (115 loc) · 4.54 KB
/
Copy pathlink_service.cpp
File metadata and controls
146 lines (115 loc) · 4.54 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
#include "link_service.hpp"
#include <filesystem>
#include <sstream>
#include <system_error>
#if defined(_WIN32)
#include <windows.h>
#endif
namespace dynamic_file_binder {
namespace {
std::wstring formatErrorMessage(unsigned long error_code)
{
#if defined(_WIN32)
if (error_code == 0) {
return L"";
}
wchar_t* buffer = nullptr;
const DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
const DWORD language_id = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
const DWORD size = FormatMessageW(flags, nullptr, error_code, language_id, reinterpret_cast<LPWSTR>(&buffer), 0, nullptr);
std::wstring message;
if (size != 0 && buffer != nullptr) {
message.assign(buffer, buffer + size);
LocalFree(buffer);
while (!message.empty() && (message.back() == L'\r' || message.back() == L'\n' || message.back() == L' ')) {
message.pop_back();
}
}
if (message.empty()) {
std::wostringstream stream;
stream << L"Win32 error " << error_code;
return stream.str();
}
return message;
#else
std::wostringstream stream;
stream << L"Error code " << error_code;
return stream.str();
#endif
}
bool isDirectoryPath(const std::filesystem::path& path)
{
std::error_code error_code;
return std::filesystem::is_directory(path, error_code);
}
std::filesystem::path appendSuffixBeforeExtension(const std::filesystem::path& path, const std::wstring& suffix)
{
const std::filesystem::path parent = path.parent_path();
const std::wstring stem = path.stem().wstring();
const std::wstring extension = path.extension().wstring();
return parent / std::filesystem::path(stem + suffix + extension);
}
} // namespace
LinkResult LinkService::create(const LinkRequest& request) const
{
if (request.source.empty()) {
return {false, ERROR_INVALID_PARAMETER, L"Source path is empty."};
}
if (!std::filesystem::exists(request.source)) {
return {false, ERROR_FILE_NOT_FOUND, L"Source path does not exist."};
}
if (request.link.empty()) {
return {false, ERROR_INVALID_PARAMETER, L"Link path is empty."};
}
#if defined(_WIN32)
if (request.kind == LinkKind::Hard) {
if (isDirectoryPath(request.source)) {
return {false, ERROR_NOT_SUPPORTED, L"Hard links are only supported for files."};
}
if (CreateHardLinkW(request.link.c_str(), request.source.c_str(), nullptr) == FALSE) {
const unsigned long error_code = GetLastError();
return {false, error_code, formatErrorMessage(error_code)};
}
return {true, 0, L"Hard link created successfully."};
}
DWORD flags = 0;
if (isDirectoryPath(request.source)) {
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
}
#if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
#endif
if (CreateSymbolicLinkW(request.link.c_str(), request.source.c_str(), flags) == FALSE) {
const unsigned long error_code = GetLastError();
return {false, error_code, formatErrorMessage(error_code)};
}
return {true, 0, L"Symbolic link created successfully."};
#else
std::error_code errorCode;
if (request.kind == LinkKind::Hard) {
if (std::filesystem::is_directory(request.source, errorCode)) {
return {false, static_cast<unsigned long>(ERROR_NOT_SUPPORTED), L"Hard links are only supported for files."};
}
std::filesystem::create_hard_link(request.source, request.link, errorCode);
if (errorCode) {
return {false, static_cast<unsigned long>(errorCode.value()), std::filesystem::path(errorCode.message()).wstring()};
}
return {true, 0, L"Hard link created successfully."};
}
if (std::filesystem::is_directory(request.source, errorCode)) {
std::filesystem::create_symlink(request.source, request.link, errorCode);
} else {
std::filesystem::create_symlink(request.source, request.link, errorCode);
}
if (errorCode) {
return {false, static_cast<unsigned long>(errorCode.value()), std::filesystem::path(errorCode.message()).wstring()};
}
return {true, 0, L"Symbolic link created successfully."};
#endif
}
std::filesystem::path LinkService::makeDefaultLinkPath(const std::filesystem::path& source, LinkKind kind)
{
const std::wstring suffix = kind == LinkKind::Hard ? L"_hardlink" : L"_symlink";
return appendSuffixBeforeExtension(source, suffix);
}
} // namespace dynamic_file_binder