GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/receiver/reactor.cc
Date: 2026-04-05 02:35:23
Exec Total Coverage
Lines: 160 319 50.2%
Branches: 137 586 23.4%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "reactor.h"
6
7 #include <stdint.h>
8 #include <unistd.h>
9
10 #include <cstdlib>
11 #include <cstring>
12 #include <utility>
13 #include <vector>
14
15 #include "commit_processor.h"
16 #include "json_document_write.h"
17 #include "payload_processor.h"
18 #include "repository_tag.h"
19 #include "session_token.h"
20 #include "upload_facility.h"
21 #include "util/exception.h"
22 #include "util/logging.h"
23 #include "util/pointer.h"
24 #include "util/posix.h"
25 #include "util/string.h"
26
27 namespace receiver {
28
29 // NOTE, during the handling of the messages between the gateway and the
30 // receiver, we keep reading `4` bytes instead of the more common
31 // `sizeof(req_id)` or `sizeof(int32_t)`.
32 // This mirror well the behaviour of the gateway code.
33 // It would be possible on both codebase to ask the size of the type, but then
34 // we would need to make sure that the types are actually the same.
35 // It is simpler to send `4` bytes.
36
37 36 Reactor::Request Reactor::ReadRequest(int fd, std::string *data) {
38 using namespace receiver; // NOLINT
39
40 // First, read the command identifier
41 36 int32_t req_id = kQuit;
42
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 int nb = SafeRead(fd, &req_id, 4);
43
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (nb != 4) {
45 return kError;
46 }
47
48 // Then, read message size
49 36 int32_t msg_size = 0;
50
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 nb = SafeRead(fd, &msg_size, 4);
51
52
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if (req_id == kError || nb != 4) {
53 return kError;
54 }
55
56 // Finally read the message body
57
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 if (msg_size > 0) {
58
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 std::vector<char> buffer(msg_size);
59
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 nb = SafeRead(fd, &buffer[0], msg_size);
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (nb != msg_size) {
62 return kError;
63 }
64
65
1/2
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
24 *data = std::string(&buffer[0], msg_size);
66
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 }
67
68 36 return static_cast<Request>(req_id);
69 }
70
71 36 bool Reactor::WriteRequest(int fd, Request req, const std::string &data) {
72 36 const int32_t msg_size = data.size();
73 36 const int32_t total_size = 8 + data.size(); // req + msg_size + data
74
75
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 std::vector<char> buffer(total_size);
76
77 36 memcpy(&buffer[0], &req, 4);
78 36 memcpy(&buffer[4], &msg_size, 4);
79
80
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 12 times.
36 if (!data.empty()) {
81 24 memcpy(&buffer[8], &data[0], data.size());
82 }
83
84
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
72 return SafeWrite(fd, &buffer[0], total_size);
85 36 }
86
87 36 bool Reactor::ReadReply(int fd, std::string *data) {
88 36 int32_t msg_size(0);
89
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 int nb = SafeRead(fd, &msg_size, 4);
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (nb != 4) {
92 return false;
93 }
94
95
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 std::vector<char> buffer(msg_size);
96
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 nb = SafeRead(fd, &buffer[0], msg_size);
97
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (nb != msg_size) {
99 return false;
100 }
101
102
1/2
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
36 *data = std::string(&buffer[0], msg_size);
103
104 36 return true;
105 36 }
106
107 36 bool Reactor::WriteReply(int fd, const std::string &data) {
108 36 const int32_t msg_size = data.size();
109 36 const int32_t total_size = 4 + data.size();
110
111
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 std::vector<char> buffer(total_size);
112
113 36 memcpy(&buffer[0], &msg_size, 4);
114
115
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 if (!data.empty()) {
116 36 memcpy(&buffer[4], &data[0], data.size());
117 }
118
119
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
72 return SafeWrite(fd, &buffer[0], total_size);
120 36 }
121
122 bool Reactor::ExtractStatsFromReq(JsonDocument *req, perf::Statistics *stats,
123 std::string *start_time) {
124 const perf::StatisticsTemplate stats_tmpl("publish", stats);
125 const upload::UploadCounters counters(stats_tmpl);
126
127 const JSON *statistics = JsonDocument::SearchInObject(
128 req->root(), "statistics", JSON_OBJECT);
129 if (statistics == NULL) {
130 LogCvmfs(kLogReceiver, kLogSyslogErr,
131 "Could not find 'statistics' field in request");
132 return false;
133 }
134
135 const JSON *publish_ctrs = JsonDocument::SearchInObject(statistics, "publish",
136 JSON_OBJECT);
137
138 if (publish_ctrs == NULL) {
139 LogCvmfs(kLogReceiver, kLogSyslogErr,
140 "Could not find 'statistics.publish' field in request");
141 return false;
142 }
143
144 const JSON *n_chunks_added = JsonDocument::SearchInObject(
145 publish_ctrs, "n_chunks_added", JSON_INT);
146 const JSON *n_chunks_duplicated = JsonDocument::SearchInObject(
147 publish_ctrs, "n_chunks_duplicated", JSON_INT);
148 const JSON *n_catalogs_added = JsonDocument::SearchInObject(
149 publish_ctrs, "n_catalogs_added", JSON_INT);
150 const JSON *sz_uploaded_bytes = JsonDocument::SearchInObject(
151 publish_ctrs, "sz_uploaded_bytes", JSON_INT);
152 const JSON *sz_uploaded_catalog_bytes = JsonDocument::SearchInObject(
153 publish_ctrs, "sz_uploaded_catalog_bytes", JSON_INT);
154
155 const JSON *start_time_json = JsonDocument::SearchInObject(
156 statistics, "start_time", JSON_STRING);
157
158 if (n_chunks_added == NULL || n_chunks_duplicated == NULL
159 || n_catalogs_added == NULL || sz_uploaded_bytes == NULL
160 || sz_uploaded_catalog_bytes == NULL || start_time_json == NULL) {
161 return false;
162 }
163
164 perf::Xadd(counters.n_chunks_added, n_chunks_added->get<int>());
165 perf::Xadd(counters.n_chunks_duplicated, n_chunks_duplicated->get<int>());
166 perf::Xadd(counters.n_catalogs_added, n_catalogs_added->get<int>());
167 perf::Xadd(counters.sz_uploaded_bytes, sz_uploaded_bytes->get<int>());
168 perf::Xadd(counters.sz_uploaded_catalog_bytes,
169 sz_uploaded_catalog_bytes->get<int>());
170
171 *start_time = start_time_json->get<std::string>();
172
173 return true;
174 }
175
176 12 Reactor::Reactor(int fdin, int fdout) : fdin_(fdin), fdout_(fdout) { }
177
178 24 Reactor::~Reactor() { }
179
180 12 bool Reactor::Run() {
181 12 std::string msg_body;
182 12 Request req = kQuit;
183 do {
184 36 msg_body.clear();
185
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 req = ReadRequest(fdin_, &msg_body);
186
2/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
36 if (!HandleRequest(req, msg_body)) {
187 LogCvmfs(kLogReceiver, kLogSyslogErr,
188 "Reactor: could not handle request %d. Exiting", req);
189 return false;
190 }
191
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 } while (req != kQuit);
192
193 12 return true;
194 12 }
195
196 8 bool Reactor::HandleGenerateToken(const std::string &req, std::string *reply) {
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (reply == NULL) {
198 PANIC(kLogSyslogErr, "HandleGenerateToken: Invalid reply pointer.");
199 }
200
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 const UniquePtr<JsonDocument> req_json(JsonDocument::Create(req));
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (!req_json.IsValid()) {
202 LogCvmfs(kLogReceiver, kLogSyslogErr,
203 "HandleGenerateToken: Invalid JSON request.");
204 return false;
205 }
206
207
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
8 const JSON *key_id = JsonDocument::SearchInObject(req_json->root(), "key_id",
208 JSON_STRING);
209
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
8 const JSON *path = JsonDocument::SearchInObject(req_json->root(), "path",
210 JSON_STRING);
211
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
8 const JSON *max_lease_time = JsonDocument::SearchInObject(
212 req_json->root(), "max_lease_time", JSON_INT);
213
214
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 if (key_id == NULL || path == NULL || max_lease_time == NULL) {
215 LogCvmfs(kLogReceiver, kLogSyslogErr,
216 "HandleGenerateToken: Missing fields in request.");
217 return false;
218 }
219
220 8 std::string session_token;
221 8 std::string public_token_id;
222 8 std::string token_secret;
223
224
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 if (!GenerateSessionToken(key_id->get<std::string>(),
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 path->get<std::string>(),
226
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 max_lease_time->get<int>(), &session_token,
227 &public_token_id, &token_secret)) {
228 LogCvmfs(kLogReceiver, kLogSyslogErr,
229 "HandleGenerateToken: Could not generate session token.");
230 return false;
231 }
232
233 8 JsonStringGenerator input;
234
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
8 input.Add("token", session_token);
235
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
8 input.Add("id", public_token_id);
236
2/4
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
8 input.Add("secret", token_secret);
237
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 const std::string json = input.GenerateString();
238
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 *reply = json;
239
240 8 return true;
241 8 }
242
243 4 bool Reactor::HandleGetTokenId(const std::string &req, std::string *reply) {
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (reply == NULL) {
245 PANIC(kLogSyslogErr, "HandleGetTokenId: Invalid reply pointer.");
246 }
247
248 4 std::string token_id;
249 4 JsonStringGenerator input;
250
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (!GetTokenPublicId(req, &token_id)) {
251 input.Add("status", "error");
252 input.Add("reason", "invalid_token");
253 } else {
254
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
4 input.Add("status", "ok");
255
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 input.Add("id", token_id);
256 }
257
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 const std::string json = input.GenerateString();
258
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 *reply = json;
259
260 4 return true;
261 4 }
262
263 4 bool Reactor::HandleCheckToken(const std::string &req, std::string *reply) {
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (reply == NULL) {
265 PANIC(kLogSyslogErr, "HandleCheckToken: Invalid reply pointer.");
266 }
267
268
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 const UniquePtr<JsonDocument> req_json(JsonDocument::Create(req));
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!req_json.IsValid()) {
270 LogCvmfs(kLogReceiver, kLogSyslogErr,
271 "HandleCheckToken: Invalid JSON request.");
272 return false;
273 }
274
275
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 const JSON *token = JsonDocument::SearchInObject(req_json->root(), "token",
276 JSON_STRING);
277
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 const JSON *secret = JsonDocument::SearchInObject(req_json->root(), "secret",
278 JSON_STRING);
279
280
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (token == NULL || secret == NULL) {
281 LogCvmfs(kLogReceiver, kLogSyslogErr,
282 "HandleCheckToken: Missing fields in request.");
283 return false;
284 }
285
286 4 std::string path;
287 4 JsonStringGenerator input;
288
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 const TokenCheckResult ret = CheckToken(token->get<std::string>(),
289
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 secret->get<std::string>(), &path);
290
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 switch (ret) {
291 case kExpired:
292 // Expired token
293 input.Add("status", "error");
294 input.Add("reason", "expired_token");
295 break;
296 case kInvalid:
297 // Invalid token
298 input.Add("status", "error");
299 input.Add("reason", "invalid_token");
300 break;
301 4 case kValid:
302 // All ok
303
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
4 input.Add("status", "ok");
304
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 input.Add("path", path);
305 4 break;
306 default:
307 // Should not be reached
308 PANIC(kLogSyslogErr,
309 "HandleCheckToken: Unknown value received. Exiting.");
310 }
311
312
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 const std::string json = input.GenerateString();
313
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 *reply = json;
314
315 4 return true;
316 4 }
317
318 // This is a special handler. We need to continue reading the payload from the
319 // fdin_
320 4 bool Reactor::HandleSubmitPayload(int fdin, const std::string &req,
321 std::string *reply) {
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!reply) {
323 PANIC(kLogSyslogErr, "HandleSubmitPayload: Invalid reply pointer.");
324 }
325
326 // Extract the Path (used for verification), Digest and DigestSize from the
327 // request JSON.
328
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 const UniquePtr<JsonDocument> req_json(JsonDocument::Create(req));
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!req_json.IsValid()) {
330 LogCvmfs(kLogReceiver, kLogSyslogErr,
331 "HandleSubmitPayload: Invalid JSON request.");
332 return false;
333 }
334
335
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 const JSON *path_json = JsonDocument::SearchInObject(req_json->root(), "path",
336 JSON_STRING);
337
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 const JSON *digest_json = JsonDocument::SearchInObject(req_json->root(),
338 "digest", JSON_STRING);
339
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
4 const JSON *header_size_json = JsonDocument::SearchInObject(
340 req_json->root(), "header_size", JSON_INT);
341
342
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (path_json == NULL || digest_json == NULL || header_size_json == NULL) {
343 LogCvmfs(kLogReceiver, kLogSyslogErr,
344 "HandleSubmitPayload: Missing fields in request.");
345 return false;
346 }
347
348
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 perf::Statistics statistics;
349
350
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 const UniquePtr<PayloadProcessor> proc(MakePayloadProcessor());
351
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 proc->SetStatistics(&statistics);
352 4 JsonStringGenerator reply_input;
353
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 const PayloadProcessor::Result res = proc->Process(
354
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 fdin, digest_json->get<std::string>(), path_json->get<std::string>(),
355
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 header_size_json->get<int>());
356
357
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 switch (res) {
358 case PayloadProcessor::kPathViolation:
359 reply_input.Add("status", "error");
360 reply_input.Add("reason", "path_violation");
361 break;
362 case PayloadProcessor::kOtherError:
363 reply_input.Add("status", "error");
364 reply_input.Add("reason", "other_error");
365 break;
366 case PayloadProcessor::kUploaderError:
367 reply_input.Add("status", "error");
368 reply_input.Add("reason", "uploader_error");
369 break;
370 4 case PayloadProcessor::kSuccess:
371
3/6
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
4 reply_input.Add("status", "ok");
372 4 break;
373 default:
374 PANIC(kLogSyslogErr,
375 "HandleSubmitPayload: Unknown value of PayloadProcessor::Result "
376 "encountered.");
377 break;
378 }
379
380 // HandleSubmitPayload sends partial statistics back to the gateway
381
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 const std::string stats_json = statistics.PrintJSON();
382
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 reply_input.AddJsonObject("statistics", stats_json);
383
384
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 const std::string json = reply_input.GenerateString();
385
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 *reply = json;
386
387 4 return true;
388 4 }
389
390 bool Reactor::HandleCommit(const std::string &req, std::string *reply) {
391 if (!reply) {
392 PANIC(kLogSyslogErr, "HandleCommit: Invalid reply pointer.");
393 }
394 // Extract the Path from the request JSON.
395 const UniquePtr<JsonDocument> req_json(JsonDocument::Create(req));
396 if (!req_json.IsValid()) {
397 LogCvmfs(kLogReceiver, kLogSyslogErr,
398 "HandleCommit: Invalid JSON request.");
399 return false;
400 }
401
402 const JSON *lease_path_json = JsonDocument::SearchInObject(
403 req_json->root(), "lease_path", JSON_STRING);
404 const JSON *old_root_hash_json = JsonDocument::SearchInObject(
405 req_json->root(), "old_root_hash", JSON_STRING);
406 const JSON *new_root_hash_json = JsonDocument::SearchInObject(
407 req_json->root(), "new_root_hash", JSON_STRING);
408 const JSON *tag_name_json = JsonDocument::SearchInObject(
409 req_json->root(), "tag_name", JSON_STRING);
410 const JSON *tag_description_json = JsonDocument::SearchInObject(
411 req_json->root(), "tag_description", JSON_STRING);
412
413 if (lease_path_json == NULL || old_root_hash_json == NULL
414 || new_root_hash_json == NULL) {
415 LogCvmfs(kLogReceiver, kLogSyslogErr,
416 "HandleCommit: Missing fields in request.");
417 return false;
418 }
419
420 perf::Statistics statistics;
421 std::string start_time;
422 if (!Reactor::ExtractStatsFromReq(req_json.weak_ref(), &statistics,
423 &start_time)) {
424 LogCvmfs(
425 kLogReceiver, kLogSyslogErr,
426 "HandleCommit: Could not extract statistics counters from request");
427 }
428 uint64_t final_revision;
429
430 // Here we use the path to commit the changes!
431 const UniquePtr<CommitProcessor> proc(MakeCommitProcessor());
432 proc->SetStatistics(&statistics, start_time);
433 const shash::Any old_root_hash = shash::MkFromSuffixedHexPtr(
434 shash::HexPtr(old_root_hash_json->get<std::string>()));
435 const shash::Any new_root_hash = shash::MkFromSuffixedHexPtr(
436 shash::HexPtr(new_root_hash_json->get<std::string>()));
437 const RepositoryTag repo_tag(tag_name_json->get<std::string>(),
438 tag_description_json->get<std::string>());
439 const CommitProcessor::Result res = proc->Process(
440 lease_path_json->get<std::string>(), old_root_hash, new_root_hash,
441 repo_tag, &final_revision);
442
443 JsonStringGenerator reply_input;
444 switch (res) {
445 case CommitProcessor::kSuccess:
446 reply_input.Add("status", "ok");
447 reply_input.Add("final_revision", static_cast<int64_t>(final_revision));
448 break;
449 case CommitProcessor::kError:
450 reply_input.Add("status", "error");
451 reply_input.Add("reason", "miscellaneous");
452 break;
453 case CommitProcessor::kMergeFailure:
454 reply_input.Add("status", "error");
455 reply_input.Add("reason", "merge_error");
456 break;
457 case CommitProcessor::kMissingReflog:
458 reply_input.Add("status", "error");
459 reply_input.Add("reason", "missing_reflog");
460 break;
461 default:
462 PANIC(kLogSyslogErr,
463 "Unknown value of CommitProcessor::Result encountered.");
464 break;
465 }
466
467 const std::string json = reply_input.GenerateString();
468 *reply = json;
469
470 return true;
471 }
472
473 PayloadProcessor *Reactor::MakePayloadProcessor() {
474 return new PayloadProcessor();
475 }
476
477 CommitProcessor *Reactor::MakeCommitProcessor() {
478 return new CommitProcessor();
479 }
480
481 36 bool Reactor::HandleRequest(Request req, const std::string &data) {
482 36 bool ok = true;
483 36 std::string reply;
484 try {
485
6/10
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
36 switch (req) {
486 12 case kQuit:
487
2/4
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
12 ok = WriteReply(fdout_, "ok");
488 12 break;
489 4 case kEcho:
490
4/8
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
4 ok = WriteReply(fdout_, std::string("PID: ") + StringifyUint(getpid()));
491 4 break;
492 8 case kGenerateToken:
493
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 ok &= HandleGenerateToken(data, &reply);
494
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 ok &= WriteReply(fdout_, reply);
495 8 break;
496 4 case kGetTokenId:
497
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= HandleGetTokenId(data, &reply);
498
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= WriteReply(fdout_, reply);
499 4 break;
500 4 case kCheckToken:
501
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= HandleCheckToken(data, &reply);
502
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= WriteReply(fdout_, reply);
503 4 break;
504 4 case kSubmitPayload:
505
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= HandleSubmitPayload(fdin_, data, &reply);
506
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ok &= WriteReply(fdout_, reply);
507 4 break;
508 case kCommit:
509 ok &= HandleCommit(data, &reply);
510 ok &= WriteReply(fdout_, reply);
511 break;
512 case kTestCrash:
513 PANIC(kLogSyslogErr,
514 "Crash for test purposes. Should never happen in production "
515 "environment.");
516 break;
517 case kError:
518 LogCvmfs(kLogReceiver, kLogSyslogErr,
519 "Reactor: unknown command received.");
520 ok = false;
521 break;
522 default:
523 break;
524 }
525 } catch (const ECvmfsException &e) {
526 reply.clear();
527
528 std::string error("runtime error: ");
529 error += e.what();
530
531 JsonStringGenerator input;
532 input.Add("status", "error");
533 input.Add("reason", error);
534
535 reply = input.GenerateString();
536 WriteReply(fdout_, reply);
537 throw e;
538 }
539
540 36 return ok;
541 36 }
542
543 } // namespace receiver
544