-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathopen_request.cpp
More file actions
238 lines (213 loc) · 7.61 KB
/
Copy pathopen_request.cpp
File metadata and controls
238 lines (213 loc) · 7.61 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "open_request.hpp"
#include "../config/config.hpp"
#include "../utils/atomic_file.hpp"
#include "../utils/utf8_path.hpp"
#include "../utils/uuid.hpp"
#include <nlohmann/json.hpp>
#include <chrono>
#include <cctype>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <system_error>
#include <utility>
namespace fs = std::filesystem;
namespace acecode::desktop {
namespace {
constexpr const char* kWorkspaceArgument = "--open-workspace";
constexpr const char* kSessionArgument = "--open-session";
constexpr std::size_t kMaxCwdBytes = 32768;
constexpr std::size_t kMaxSessionIdBytes = 512;
bool has_non_whitespace(const std::string& text) {
for (unsigned char ch : text) {
if (std::isspace(ch) == 0) return true;
}
return false;
}
bool valid_text_field(const std::string& value, std::size_t max_bytes) {
return !value.empty() &&
value.size() <= max_bytes &&
value.find('\0') == std::string::npos &&
has_non_whitespace(value);
}
std::string request_path(const std::string& path_override) {
return path_override.empty() ? desktop_open_request_path() : path_override;
}
std::int64_t now_unix_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
}
} // namespace
bool valid_desktop_open_request(const DesktopOpenRequest& request,
std::string* error) {
if (!valid_text_field(request.cwd, kMaxCwdBytes)) {
if (error) *error = "Desktop open request has an invalid workspace path";
return false;
}
if (!valid_text_field(request.session_id, kMaxSessionIdBytes)) {
if (error) *error = "Desktop open request has an invalid session id";
return false;
}
if (error) error->clear();
return true;
}
std::vector<std::string> desktop_open_request_arguments(
const DesktopOpenRequest& request) {
if (!valid_desktop_open_request(request)) return {};
return {
kWorkspaceArgument,
request.cwd,
kSessionArgument,
request.session_id,
};
}
DesktopOpenRequestParseResult parse_desktop_open_request_arguments(
const std::vector<std::string>& argv) {
DesktopOpenRequestParseResult result;
DesktopOpenRequest request;
bool saw_workspace = false;
bool saw_session = false;
for (std::size_t index = argv.empty() ? 0 : 1; index < argv.size(); ++index) {
const std::string& argument = argv[index];
const bool is_workspace = argument == kWorkspaceArgument;
const bool is_session = argument == kSessionArgument;
if (!is_workspace && !is_session) continue;
if (index + 1 >= argv.size()) {
result.error = argument + " requires a value";
return result;
}
const std::string& value = argv[++index];
if (value == kWorkspaceArgument || value == kSessionArgument) {
result.error = argument + " requires a value";
return result;
}
if (is_workspace) {
if (saw_workspace) {
result.error = "duplicate " + std::string(kWorkspaceArgument);
return result;
}
saw_workspace = true;
request.cwd = value;
} else {
if (saw_session) {
result.error = "duplicate " + std::string(kSessionArgument);
return result;
}
saw_session = true;
request.session_id = value;
}
}
if (!saw_workspace && !saw_session) return result;
if (!saw_workspace || !saw_session) {
result.error =
"Desktop open request requires both --open-workspace and --open-session";
return result;
}
if (!valid_desktop_open_request(request, &result.error)) return result;
result.request = std::move(request);
return result;
}
std::string desktop_open_request_path() {
return path_to_utf8(
path_from_utf8(get_run_dir()) / "desktop-open-request.json");
}
bool publish_pending_desktop_open_request(
const DesktopOpenRequest& request,
std::string* error,
const std::string& path_override) {
std::string validation_error;
if (!valid_desktop_open_request(request, &validation_error)) {
if (error) *error = std::move(validation_error);
return false;
}
const nlohmann::json payload{
{"version", 2},
{"created_at_unix_ms", now_unix_ms()},
{"cwd", request.cwd},
{"session_id", request.session_id},
};
const std::string path = request_path(path_override);
if (!atomic_write_file(path, payload.dump(), true)) {
if (error) *error = "failed to write pending Desktop open request";
return false;
}
if (error) error->clear();
return true;
}
std::optional<DesktopOpenRequest> take_pending_desktop_open_request(
std::string* error,
const std::string& path_override,
std::int64_t minimum_created_at_unix_ms) {
const fs::path source = path_from_utf8(request_path(path_override));
std::error_code ec;
if (!fs::exists(source, ec)) {
if (ec && error) {
*error = "failed to inspect pending Desktop open request: " +
ec.message();
} else if (error) {
error->clear();
}
return std::nullopt;
}
fs::path claimed = source;
claimed += ".processing-" + generate_uuid();
fs::rename(source, claimed, ec);
if (ec) {
if (error) {
*error = "failed to claim pending Desktop open request: " +
ec.message();
}
return std::nullopt;
}
std::optional<DesktopOpenRequest> result;
std::string local_error;
try {
std::ifstream input(claimed, std::ios::binary);
if (!input) {
local_error = "failed to read pending Desktop open request";
} else {
std::ostringstream buffer;
buffer << input.rdbuf();
const auto payload = nlohmann::json::parse(buffer.str());
if (!payload.is_object() ||
payload.value("version", 0) != 2 ||
!payload.contains("created_at_unix_ms") ||
!payload["created_at_unix_ms"].is_number_integer() ||
!payload.contains("cwd") ||
!payload["cwd"].is_string() ||
!payload.contains("session_id") ||
!payload["session_id"].is_string()) {
local_error = "pending Desktop open request has an invalid schema";
} else if (
payload["created_at_unix_ms"].get<std::int64_t>() <
minimum_created_at_unix_ms) {
local_error =
"ignored stale pending Desktop open request";
} else {
DesktopOpenRequest request{
payload["cwd"].get<std::string>(),
payload["session_id"].get<std::string>(),
};
if (valid_desktop_open_request(request, &local_error)) {
result = std::move(request);
}
}
}
} catch (const std::exception& exception) {
local_error =
"failed to parse pending Desktop open request: " +
std::string(exception.what());
}
std::error_code remove_error;
fs::remove(claimed, remove_error);
if (local_error.empty() && remove_error) {
local_error =
"failed to remove consumed Desktop open request: " +
remove_error.message();
}
if (error) *error = std::move(local_error);
return result;
}
} // namespace acecode::desktop