GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/s3fanout.cc
Date: 2026-10-04 02:40:33
Exec Total Coverage
Lines: 649 916 70.9%
Branches: 469 1308 35.9%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Runs a thread using libcurls asynchronous I/O mode to push data to S3
5 */
6
7 #include "s3fanout.h"
8
9 #include <pthread.h>
10
11 #include <algorithm>
12 #include <cassert>
13 #include <cerrno>
14 #include <cstring>
15 #include <utility>
16
17 #include "crypto/hash.h"
18 #include "util/exception.h"
19 #include "util/platform.h"
20 #include "util/posix.h"
21 #include "util/string.h"
22
23 using namespace std; // NOLINT
24
25 namespace s3fanout {
26
27 /**
28 * Escapes characters that are not allowed in XML text content.
29 * Only & and < need escaping; >, ', " are safe in text nodes.
30 */
31 7240 static string XmlEscape(const string &input) {
32 7240 string result;
33
1/2
✓ Branch 2 taken 7240 times.
✗ Branch 3 not taken.
7240 result.reserve(input.size());
34
2/2
✓ Branch 1 taken 165040 times.
✓ Branch 2 taken 7240 times.
172280 for (unsigned i = 0; i < input.size(); ++i) {
35
3/3
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 165032 times.
165040 switch (input[i]) {
36 4 case '&':
37
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 result += "&amp;";
38 4 break;
39 4 case '<':
40
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 result += "&lt;";
41 4 break;
42 165032 default:
43
1/2
✓ Branch 2 taken 165032 times.
✗ Branch 3 not taken.
165032 result += input[i];
44 165032 break;
45 }
46 }
47 7240 return result;
48 }
49
50
51 /**
52 * The CanonicalizedResource of an S3 V2 signature. It must mirror MkUrl():
53 * the server rebuilds the resource from the request path, so any difference,
54 * down to a trailing slash, yields SignatureDoesNotMatch.
55 *
56 * With DNS buckets the bucket lives in the hostname and the path is
57 * "/" + object_key, so an empty key gives "/<bucket>/". In path style the
58 * path already carries the bucket and an empty key gives "/<bucket>".
59 */
60 14760 string MkV2CanonicalResource(const string &bucket, const string &object_key,
61 bool dns_buckets, bool multi_delete) {
62 14760 string resource = "/" + bucket;
63
6/6
✓ Branch 0 taken 14748 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 14656 times.
✓ Branch 4 taken 92 times.
✓ Branch 5 taken 14668 times.
✓ Branch 6 taken 92 times.
14760 if (dns_buckets || !object_key.empty())
64
2/4
✓ Branch 1 taken 14668 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14668 times.
✗ Branch 5 not taken.
14668 resource += "/" + object_key;
65 // V2 requires subresources to be signed; multi-delete posts to "?delete"
66
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 14668 times.
14760 if (multi_delete)
67
1/2
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
92 resource += "?delete";
68 14760 return resource;
69 }
70
71
72 /**
73 * Builds an S3 DeleteObjects XML request body for multi-object delete.
74 * Uses Quiet mode so the response only contains errors, not successes.
75 */
76 100 string ComposeDeleteMultiXml(const vector<string> &keys) {
77 string xml = "<?xml version=\"1.0\" "
78
1/2
✓ Branch 2 taken 100 times.
✗ Branch 3 not taken.
100 "encoding=\"UTF-8\"?><Delete><Quiet>true</Quiet>";
79 // ~70 bytes per <Object><Key>...</Key></Object> entry
80
1/2
✓ Branch 3 taken 100 times.
✗ Branch 4 not taken.
100 xml.reserve(xml.size() + keys.size() * 70 + 10);
81
2/2
✓ Branch 1 taken 7240 times.
✓ Branch 2 taken 100 times.
7340 for (unsigned i = 0; i < keys.size(); ++i) {
82
4/8
✓ Branch 2 taken 7240 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7240 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 7240 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 7240 times.
✗ Branch 12 not taken.
7240 xml += "<Object><Key>" + XmlEscape(keys[i]) + "</Key></Object>";
83 }
84
1/2
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
100 xml += "</Delete>";
85 100 return xml;
86 }
87
88
89 /**
90 * Parses the S3 DeleteObjects error response XML.
91 * In Quiet mode, the response only contains <Error> elements for failed keys.
92 * Returns the number of errors found.
93 */
94 16 unsigned ParseDeleteMultiResponse(const string &response,
95 vector<string> *error_keys,
96 vector<string> *error_codes,
97 vector<string> *error_messages) {
98 16 unsigned num_errors = 0;
99 16 string::size_type pos = 0;
100
101 while (true) {
102 28 const string::size_type err_start = response.find("<Error>", pos);
103
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 16 times.
28 if (err_start == string::npos)
104 12 break;
105 16 const string::size_type err_end = response.find("</Error>", err_start);
106
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (err_end == string::npos)
107 4 break;
108
109
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 const string error_block = response.substr(err_start, err_end - err_start);
110 12 num_errors++;
111
112 // Extract <Key>...</Key>
113 12 const string::size_type key_start = error_block.find("<Key>");
114 12 const string::size_type key_end = error_block.find("</Key>");
115
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (key_start != string::npos && key_end != string::npos) {
116
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 error_keys->push_back(
117
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 error_block.substr(key_start + 5, key_end - key_start - 5));
118 } else {
119 ✗ error_keys->push_back("");
120 }
121
122 // Extract <Code>...</Code>
123 12 const string::size_type code_start = error_block.find("<Code>");
124 12 const string::size_type code_end = error_block.find("</Code>");
125
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (code_start != string::npos && code_end != string::npos) {
126
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 error_codes->push_back(
127
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 error_block.substr(code_start + 6, code_end - code_start - 6));
128 } else {
129 ✗ error_codes->push_back("");
130 }
131
132 // Extract <Message>...</Message>
133 12 const string::size_type msg_start = error_block.find("<Message>");
134 12 const string::size_type msg_end = error_block.find("</Message>");
135
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (msg_start != string::npos && msg_end != string::npos) {
136
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 error_messages->push_back(
137
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 error_block.substr(msg_start + 9, msg_end - msg_start - 9));
138 } else {
139 ✗ error_messages->push_back("");
140 }
141
142 12 pos = err_end + 8; // length of "</Error>"
143 12 }
144
145 16 return num_errors;
146 }
147
148 const char *S3FanoutManager::kCacheControlCas = "Cache-Control: max-age=259200";
149 const unsigned S3FanoutManager::kDefault429ThrottleMs = 250;
150 const unsigned S3FanoutManager::kMax429ThrottleMs = 10000;
151 const unsigned S3FanoutManager::kThrottleReportIntervalSec = 10;
152 const unsigned S3FanoutManager::kDefaultHTTPPort = 80;
153 const unsigned S3FanoutManager::kDefaultHTTPSPort = 443;
154
155 156 std::string S3FanoutManager::MkDotCvmfsCacheControlHeader(
156 unsigned defaultMaxAge, int overrideMaxAge) {
157 const char *var;
158 int max_age_sec;
159 char *at_null_terminator_if_number;
160 156 bool value_determined = false;
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (overrideMaxAge >= 0) {
163 ✗ max_age_sec = overrideMaxAge;
164 ✗ value_determined = true;
165 }
166
167
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (!value_determined) {
168 156 var = getenv("CVMFS_MAX_TTL_SECS");
169
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
156 if (var && var[0]) {
170 ✗ max_age_sec = strtoll(var, &at_null_terminator_if_number, 10);
171 ✗ if (*at_null_terminator_if_number == '\0' && max_age_sec >= 0) {
172 ✗ value_determined = true;
173 }
174 }
175 }
176
177
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (!value_determined) {
178 156 var = getenv("CVMFS_MAX_TTL");
179
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
156 if (var && var[0]) {
180 ✗ max_age_sec = strtoll(var, &at_null_terminator_if_number, 10);
181 ✗ if (*at_null_terminator_if_number == '\0' && max_age_sec >= 0) {
182 ✗ max_age_sec *= 60;
183 ✗ value_determined = true;
184 }
185 }
186 }
187
188
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (!value_determined) {
189 156 max_age_sec = defaultMaxAge;
190 }
191
192
2/4
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 156 times.
✗ Branch 5 not taken.
312 return "Cache-Control: max-age=" + std::to_string(max_age_sec);
193 }
194
195 /**
196 * Parses Retry-After and X-Retry-In headers attached to HTTP 429 responses
197 */
198 200 void S3FanoutManager::DetectThrottleIndicator(const std::string &header,
199 JobInfo *info) {
200 200 std::string value_str;
201
4/6
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 200 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 76 times.
✓ Branch 10 taken 124 times.
200 if (HasPrefix(header, "retry-after:", true))
202
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 value_str = header.substr(12);
203
4/6
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 200 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 180 times.
200 if (HasPrefix(header, "x-retry-in:", true))
204
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 value_str = header.substr(11);
205
206
1/2
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
200 value_str = Trim(value_str, true /* trim_newline */);
207
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 112 times.
200 if (!value_str.empty()) {
208
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 const unsigned value_numeric = String2Uint64(value_str);
209
2/4
✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 88 times.
✗ Branch 6 not taken.
176 const unsigned value_ms = HasSuffix(value_str, "ms", true /* ignore_case */)
210
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 68 times.
88 ? value_numeric
211 88 : (value_numeric * 1000);
212
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
88 if (value_ms > 0)
213 84 info->throttle_ms = std::min(value_ms, kMax429ThrottleMs);
214 }
215 200 }
216
217
218 /**
219 * Called by curl for every HTTP header. Not called for file:// transfers.
220 */
221 44400 static size_t CallbackCurlHeader(void *ptr, size_t size, size_t nmemb,
222 void *info_link) {
223 44400 const size_t num_bytes = size * nmemb;
224
1/2
✓ Branch 2 taken 44400 times.
✗ Branch 3 not taken.
44400 const string header_line(static_cast<const char *>(ptr), num_bytes);
225 44400 JobInfo *info = static_cast<JobInfo *>(info_link);
226
227 // Check for http status code errors
228
4/6
✓ Branch 2 taken 44400 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 44400 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 14784 times.
✓ Branch 10 taken 29616 times.
44400 if (HasPrefix(header_line, "HTTP/1.", false)) {
229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14784 times.
14784 if (header_line.length() < 10)
230 ✗ return 0;
231
232 unsigned i;
233
5/6
✓ Branch 1 taken 29568 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14784 times.
✓ Branch 5 taken 14784 times.
✓ Branch 6 taken 14784 times.
✓ Branch 7 taken 14784 times.
29568 for (i = 8; (i < header_line.length()) && (header_line[i] == ' '); ++i) {
234 }
235
236
2/2
✓ Branch 1 taken 7416 times.
✓ Branch 2 taken 7368 times.
14784 if (header_line[i] == '2') {
237 7416 return num_bytes;
238 } else {
239
1/2
✓ Branch 2 taken 7368 times.
✗ Branch 3 not taken.
7368 LogCvmfs(kLogS3Fanout, kLogDebug, "http status error code [info %p]: %s",
240 info, header_line.c_str());
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7368 times.
7368 if (header_line.length() < i + 3) {
242 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "S3: invalid HTTP response '%s'",
243 header_line.c_str());
244 ✗ info->error_code = kFailOther;
245 ✗ return 0;
246 }
247
2/4
✓ Branch 3 taken 7368 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 7368 times.
✗ Branch 7 not taken.
7368 info->http_error = String2Int64(string(&header_line[i], 3));
248
249
2/7
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7320 times.
✗ Branch 6 not taken.
7368 switch (info->http_error) {
250 48 case 429:
251 48 info->error_code = kFailRetry;
252 48 info->throttle_ms = S3FanoutManager::kDefault429ThrottleMs;
253 48 info->throttle_timestamp = platform_monotonic_time();
254 48 return num_bytes;
255 ✗ case 507: // Insufficient Storage
256 ✗ info->error_code = kFailInsufficientStorage;
257 ✗ break;
258 ✗ case 503:
259 case 502: // Can happen if the S3 gateway-backend connection breaks
260 case 500: // sometimes see this as a transient error from S3
261 ✗ info->error_code = kFailServiceUnavailable;
262 ✗ break;
263 ✗ case 501:
264 case 400:
265 ✗ info->error_code = kFailBadRequest;
266 ✗ break;
267 ✗ case 403:
268 ✗ info->error_code = kFailForbidden;
269 ✗ break;
270 7320 case 404:
271 7320 info->error_code = kFailNotFound;
272 7320 return num_bytes;
273 ✗ default:
274 ✗ info->error_code = kFailOther;
275 }
276 ✗ return 0;
277 }
278 }
279
280
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 29472 times.
29616 if (info->error_code == kFailRetry) {
281
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 S3FanoutManager::DetectThrottleIndicator(header_line, info);
282 }
283
284 29616 return num_bytes;
285 44400 }
286
287
288 /**
289 * Called by curl for every new chunk to upload.
290 */
291 188112 static size_t CallbackCurlData(void *ptr, size_t size, size_t nmemb,
292 void *info_link) {
293 188112 const size_t num_bytes = size * nmemb;
294 188112 JobInfo *info = static_cast<JobInfo *>(info_link);
295
296 188112 LogCvmfs(kLogS3Fanout, kLogDebug, "Data callback with %zu bytes", num_bytes);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 188112 times.
188112 if (num_bytes == 0)
299 ✗ return 0;
300
301 188112 const uint64_t read_bytes = info->origin->Read(ptr, num_bytes);
302
303 188112 LogCvmfs(kLogS3Fanout, kLogDebug, "source buffer pushed out %lu bytes",
304 read_bytes);
305
306 188112 return read_bytes;
307 }
308
309
310 /**
311 * Captures the HTTP response body. For kReqDeleteMulti, the response contains
312 * XML error information. For other requests, the body is ignored.
313 */
314 ✗ static size_t CallbackCurlBody(char *ptr, size_t size, size_t nmemb,
315 void *userdata) {
316 ✗ const size_t num_bytes = size * nmemb;
317 ✗ JobInfo *info = static_cast<JobInfo *>(userdata);
318 ✗ if (info != NULL && info->request == JobInfo::kReqDeleteMulti) {
319 ✗ info->response_body.append(ptr, num_bytes);
320 }
321 ✗ return num_bytes;
322 }
323
324
325 /**
326 * Called when new curl sockets arrive or existing curl sockets depart.
327 */
328 32760 int S3FanoutManager::CallbackCurlSocket(CURL *easy, curl_socket_t s, int action,
329 void *userp, void *socketp) {
330 32760 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(userp);
331 32760 LogCvmfs(kLogS3Fanout, kLogDebug,
332 "CallbackCurlSocket called with easy "
333 "handle %p, socket %d, action %d, up %p, "
334 "sp %p, fds_inuse %d, jobs %d",
335 easy, s, action, userp, socketp, s3fanout_mgr->watch_fds_inuse_,
336 32760 s3fanout_mgr->available_jobs_->Get());
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32760 times.
32760 if (action == CURL_POLL_NONE)
338 ✗ return 0;
339
340 // Find s in watch_fds_
341 // First 2 fds are job and terminate pipes (not curl related)
342 unsigned index;
343
2/2
✓ Branch 0 taken 56244 times.
✓ Branch 1 taken 14772 times.
71016 for (index = 2; index < s3fanout_mgr->watch_fds_inuse_; ++index) {
344
2/2
✓ Branch 0 taken 17988 times.
✓ Branch 1 taken 38256 times.
56244 if (s3fanout_mgr->watch_fds_[index].fd == s)
345 17988 break;
346 }
347 // Or create newly
348
2/2
✓ Branch 0 taken 14772 times.
✓ Branch 1 taken 17988 times.
32760 if (index == s3fanout_mgr->watch_fds_inuse_) {
349 // Extend array if necessary
350
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 14736 times.
14772 if (s3fanout_mgr->watch_fds_inuse_ == s3fanout_mgr->watch_fds_size_) {
351 36 s3fanout_mgr->watch_fds_size_ *= 2;
352 36 s3fanout_mgr->watch_fds_ = static_cast<struct pollfd *>(
353 36 srealloc(s3fanout_mgr->watch_fds_,
354 36 s3fanout_mgr->watch_fds_size_ * sizeof(struct pollfd)));
355 }
356 14772 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].fd = s;
357 14772 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].events = 0;
358 14772 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].revents = 0;
359 14772 s3fanout_mgr->watch_fds_inuse_++;
360 }
361
362
4/5
✓ Branch 0 taken 14772 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 3168 times.
✓ Branch 3 taken 14772 times.
✗ Branch 4 not taken.
32760 switch (action) {
363 14772 case CURL_POLL_IN:
364 14772 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI;
365 14772 break;
366 48 case CURL_POLL_OUT:
367 48 s3fanout_mgr->watch_fds_[index].events = POLLOUT | POLLWRBAND;
368 48 break;
369 3168 case CURL_POLL_INOUT:
370 3168 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI | POLLOUT
371 | POLLWRBAND;
372 3168 break;
373 14772 case CURL_POLL_REMOVE:
374
2/2
✓ Branch 0 taken 2340 times.
✓ Branch 1 taken 12432 times.
14772 if (index < s3fanout_mgr->watch_fds_inuse_ - 1)
375 s3fanout_mgr
376 2340 ->watch_fds_[index] = s3fanout_mgr->watch_fds_
377 2340 [s3fanout_mgr->watch_fds_inuse_ - 1];
378 14772 s3fanout_mgr->watch_fds_inuse_--;
379 // Shrink array if necessary
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14772 times.
14772 if ((s3fanout_mgr->watch_fds_inuse_ > s3fanout_mgr->watch_fds_max_)
381 ✗ && (s3fanout_mgr->watch_fds_inuse_
382 ✗ < s3fanout_mgr->watch_fds_size_ / 2)) {
383 ✗ s3fanout_mgr->watch_fds_size_ /= 2;
384 ✗ s3fanout_mgr->watch_fds_ = static_cast<struct pollfd *>(
385 ✗ srealloc(s3fanout_mgr->watch_fds_,
386 ✗ s3fanout_mgr->watch_fds_size_ * sizeof(struct pollfd)));
387 }
388 14772 break;
389 ✗ default:
390 ✗ PANIC(NULL);
391 }
392
393 32760 return 0;
394 }
395
396
397 /**
398 * Worker thread event loop.
399 */
400 156 void *S3FanoutManager::MainUpload(void *data) {
401
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread started");
402 156 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(data);
403
404 156 s3fanout_mgr->InitPipeWatchFds();
405
406 // Don't schedule more jobs into the multi handle than the maximum number of
407 // parallel connections. This should prevent starvation and thus a timeout
408 // of the authorization header (CVM-1339).
409 156 unsigned jobs_in_flight = 0;
410
411 while (true) {
412 // Check events with 100ms timeout
413 200556 const int timeout_ms = 100;
414
1/2
✓ Branch 1 taken 200556 times.
✗ Branch 2 not taken.
200556 int retval = poll(s3fanout_mgr->watch_fds_, s3fanout_mgr->watch_fds_inuse_,
415 timeout_ms);
416
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 199296 times.
200556 if (retval == 0) {
417 // Handle timeout
418 1260 int still_running = 0;
419
1/2
✓ Branch 1 taken 1260 times.
✗ Branch 2 not taken.
1260 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
420 CURL_SOCKET_TIMEOUT, 0, &still_running);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1260 times.
1260 if (retval != CURLM_OK) {
422 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "Error, timeout due to: %d", retval);
423 ✗ assert(retval == CURLM_OK);
424 }
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199296 times.
199296 } else if (retval < 0) {
426 ✗ assert(errno == EINTR);
427 ✗ continue;
428 }
429
430 // Terminate I/O thread
431
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 200400 times.
200556 if (s3fanout_mgr->watch_fds_[0].revents)
432 156 break;
433
434 // New job incoming
435
2/2
✓ Branch 0 taken 7440 times.
✓ Branch 1 taken 192960 times.
200400 if (s3fanout_mgr->watch_fds_[1].revents) {
436 7440 s3fanout_mgr->watch_fds_[1].revents = 0;
437 JobInfo *info;
438
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 ReadPipe(s3fanout_mgr->pipe_jobs_[0], &info, sizeof(info));
439
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 CURL *handle = s3fanout_mgr->AcquireCurlHandle();
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7440 times.
7440 if (handle == NULL) {
441 ✗ PANIC(kLogStderr, "Failed to acquire CURL handle.");
442 }
443
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 const s3fanout::Failures init_failure = s3fanout_mgr->InitializeRequest(
444 info, handle);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7440 times.
7440 if (init_failure != s3fanout::kFailOk) {
446 ✗ PANIC(kLogStderr,
447 "Failed to initialize CURL handle (error: %d - %s | errno: %d)",
448 init_failure, Code2Ascii(init_failure), errno);
449 }
450
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->SetUrlOptions(info);
451
452
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 curl_multi_add_handle(s3fanout_mgr->curl_multi_, handle);
453
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->active_requests_->insert(info);
454 7440 jobs_in_flight++;
455 7440 int still_running = 0, retval = 0;
456
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
457 CURL_SOCKET_TIMEOUT, 0, &still_running);
458
459
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 LogCvmfs(kLogS3Fanout, kLogDebug, "curl_multi_socket_action: %d - %d",
460 retval, still_running);
461 }
462
463
464 // Activity on curl sockets
465 // Within this loop the curl_multi_socket_action() may cause socket(s)
466 // to be removed from watch_fds_. If a socket is removed it is replaced
467 // by the socket at the end of the array and the inuse count is decreased.
468 // Therefore loop over the array in reverse order.
469 // First 2 fds are job and terminate pipes (not curl related)
470
2/2
✓ Branch 0 taken 446748 times.
✓ Branch 1 taken 200400 times.
647148 for (int32_t i = s3fanout_mgr->watch_fds_inuse_ - 1; i >= 2; --i) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446748 times.
446748 if (static_cast<uint32_t>(i) >= s3fanout_mgr->watch_fds_inuse_) {
472 ✗ continue;
473 }
474
2/2
✓ Branch 0 taken 204948 times.
✓ Branch 1 taken 241800 times.
446748 if (s3fanout_mgr->watch_fds_[i].revents) {
475 204948 int ev_bitmask = 0;
476
2/2
✓ Branch 0 taken 22152 times.
✓ Branch 1 taken 182796 times.
204948 if (s3fanout_mgr->watch_fds_[i].revents & (POLLIN | POLLPRI))
477 22152 ev_bitmask |= CURL_CSELECT_IN;
478
2/2
✓ Branch 0 taken 182796 times.
✓ Branch 1 taken 22152 times.
204948 if (s3fanout_mgr->watch_fds_[i].revents & (POLLOUT | POLLWRBAND))
479 182796 ev_bitmask |= CURL_CSELECT_OUT;
480 204948 if (s3fanout_mgr->watch_fds_[i].revents
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204948 times.
204948 & (POLLERR | POLLHUP | POLLNVAL))
482 ✗ ev_bitmask |= CURL_CSELECT_ERR;
483 204948 s3fanout_mgr->watch_fds_[i].revents = 0;
484
485 204948 int still_running = 0;
486 204948 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
487
1/2
✓ Branch 1 taken 204948 times.
✗ Branch 2 not taken.
204948 s3fanout_mgr->watch_fds_[i].fd,
488 ev_bitmask,
489 &still_running);
490 }
491 }
492
493 // Check if transfers are completed
494 CURLMsg *curl_msg;
495 int msgs_in_queue;
496
3/4
✓ Branch 1 taken 215184 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14784 times.
✓ Branch 4 taken 200400 times.
215184 while ((curl_msg = curl_multi_info_read(s3fanout_mgr->curl_multi_,
497 &msgs_in_queue))) {
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14784 times.
14784 assert(curl_msg->msg == CURLMSG_DONE);
499
500 14784 s3fanout_mgr->statistics_->num_requests++;
501 JobInfo *info;
502 14784 CURL *easy_handle = curl_msg->easy_handle;
503 14784 const int curl_error = curl_msg->data.result;
504
1/2
✓ Branch 1 taken 14784 times.
✗ Branch 2 not taken.
14784 curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &info);
505
506
1/2
✓ Branch 1 taken 14784 times.
✗ Branch 2 not taken.
14784 curl_multi_remove_handle(s3fanout_mgr->curl_multi_, easy_handle);
507
3/4
✓ Branch 1 taken 14784 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7344 times.
✓ Branch 4 taken 7440 times.
14784 if (s3fanout_mgr->VerifyAndFinalize(curl_error, info)) {
508
1/2
✓ Branch 1 taken 7344 times.
✗ Branch 2 not taken.
7344 curl_multi_add_handle(s3fanout_mgr->curl_multi_, easy_handle);
509 7344 int still_running = 0;
510
1/2
✓ Branch 1 taken 7344 times.
✗ Branch 2 not taken.
7344 curl_multi_socket_action(s3fanout_mgr->curl_multi_, CURL_SOCKET_TIMEOUT,
511 0, &still_running);
512 } else {
513 // Return easy handle into pool and write result back
514 7440 jobs_in_flight--;
515
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->active_requests_->erase(info);
516
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->ReleaseCurlHandle(info, easy_handle);
517
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->available_jobs_->Decrement();
518
519 // Add to list of completed jobs
520
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 s3fanout_mgr->PushCompletedJob(info);
521 }
522 }
523 200400 }
524
525 156 set<CURL *>::iterator i = s3fanout_mgr->pool_handles_inuse_->begin();
526 156 const set<CURL *>::const_iterator i_end = s3fanout_mgr->pool_handles_inuse_
527 156 ->end();
528
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 156 times.
156 for (; i != i_end; ++i) {
529 ✗ curl_multi_remove_handle(s3fanout_mgr->curl_multi_, *i);
530 ✗ curl_easy_cleanup(*i);
531 }
532 156 s3fanout_mgr->pool_handles_inuse_->clear();
533 156 free(s3fanout_mgr->watch_fds_);
534
535
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread terminated");
536 156 return NULL;
537 }
538
539
540 /**
541 * Gets an idle CURL handle from the pool. Creates a new one and adds it to
542 * the pool if necessary.
543 */
544 7440 CURL *S3FanoutManager::AcquireCurlHandle() const {
545 CURL *handle;
546
547 7440 const MutexLockGuard guard(curl_handle_lock_);
548
549
2/2
✓ Branch 1 taken 660 times.
✓ Branch 2 taken 6780 times.
7440 if (pool_handles_idle_->empty()) {
550 CURLcode retval;
551
552 // Create a new handle
553
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 handle = curl_easy_init();
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 assert(handle != NULL);
555
556 // Other settings
557
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 retval = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 assert(retval == CURLE_OK);
559
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 retval = curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION,
560 CallbackCurlHeader);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 assert(retval == CURLE_OK);
562
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 retval = curl_easy_setopt(handle, CURLOPT_READFUNCTION, CallbackCurlData);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 assert(retval == CURLE_OK);
564
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 retval = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, CallbackCurlBody);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 660 times.
660 assert(retval == CURLE_OK);
566 // WRITEDATA is set per-request in InitializeRequest
567 } else {
568 6780 handle = *(pool_handles_idle_->begin());
569
1/2
✓ Branch 2 taken 6780 times.
✗ Branch 3 not taken.
6780 pool_handles_idle_->erase(pool_handles_idle_->begin());
570 }
571
572
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 pool_handles_inuse_->insert(handle);
573
574 7440 return handle;
575 7440 }
576
577
578 7440 void S3FanoutManager::ReleaseCurlHandle(JobInfo *info, CURL *handle) const {
579
1/2
✓ Branch 0 taken 7440 times.
✗ Branch 1 not taken.
7440 if (info->http_headers) {
580
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 curl_slist_free_all(info->http_headers);
581 7440 info->http_headers = NULL;
582 }
583
584 7440 const MutexLockGuard guard(curl_handle_lock_);
585
586
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 const set<CURL *>::iterator elem = pool_handles_inuse_->find(handle);
587
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7440 times.
7440 assert(elem != pool_handles_inuse_->end());
588
589
2/2
✓ Branch 1 taken 348 times.
✓ Branch 2 taken 7092 times.
7440 if (pool_handles_idle_->size() > config_.pool_max_handles) {
590
1/2
✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
348 const CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, NULL);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 348 times.
348 assert(retval == CURLE_OK);
592
1/2
✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
348 curl_easy_cleanup(handle);
593 const std::map<CURL *, S3FanOutDnsEntry *>::size_type
594
1/2
✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
348 retitems = curl_sharehandles_->erase(handle);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 348 times.
348 assert(retitems == 1);
596 } else {
597
1/2
✓ Branch 1 taken 7092 times.
✗ Branch 2 not taken.
7092 pool_handles_idle_->insert(handle);
598 }
599
600
1/2
✓ Branch 1 taken 7440 times.
✗ Branch 2 not taken.
7440 pool_handles_inuse_->erase(elem);
601 7440 }
602
603 156 void S3FanoutManager::InitPipeWatchFds() {
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(watch_fds_inuse_ == 0);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(watch_fds_size_ >= 2);
606 156 watch_fds_[0].fd = pipe_terminate_[0];
607 156 watch_fds_[0].events = POLLIN | POLLPRI;
608 156 watch_fds_[0].revents = 0;
609 156 ++watch_fds_inuse_;
610 156 watch_fds_[1].fd = pipe_jobs_[0];
611 156 watch_fds_[1].events = POLLIN | POLLPRI;
612 156 watch_fds_[1].revents = 0;
613 156 ++watch_fds_inuse_;
614 156 }
615
616 /**
617 * The Amazon AWS 2 authorization header according to
618 * http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheAuthenticationHeader
619 */
620 14736 bool S3FanoutManager::MkV2Authz(const JobInfo &info,
621 vector<string> *headers) const {
622 14736 string payload_hash;
623
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 const bool retval = MkPayloadHash(info, &payload_hash);
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 if (!retval)
625 ✗ return false;
626
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 const string content_type = GetContentType(info);
627
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 const string request = GetRequestString(info);
628
629
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 const string timestamp = RfcTimestamp();
630
5/10
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 14736 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 14736 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 14736 times.
✗ Branch 14 not taken.
29472 string to_sign = request + "\n" + payload_hash + "\n" + content_type + "\n"
631
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
29472 + timestamp + "\n";
632
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14736 times.
✗ Branch 4 not taken.
14736 if (config_.x_amz_acl != "")
633
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
29472 to_sign += "x-amz-acl:" + config_.x_amz_acl
634
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
14736 + "\n"; // CanonicalizedAmzHeaders
635 14736 to_sign += MkV2CanonicalResource(config_.bucket, info.object_key,
636 14736 config_.dns_buckets,
637
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
14736 info.request == JobInfo::kReqDeleteMulti);
638
1/2
✓ Branch 3 taken 14736 times.
✗ Branch 4 not taken.
14736 LogCvmfs(kLogS3Fanout, kLogDebug, "%s string to sign: %s", request.c_str(),
639 to_sign.c_str());
640
641
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 shash::Any hmac;
642 14736 hmac.algorithm = shash::kSha1;
643
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 shash::Hmac(config_.secret_key,
644 14736 reinterpret_cast<const unsigned char *>(to_sign.data()),
645 14736 to_sign.length(), &hmac);
646
647
3/6
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 14736 times.
✗ Branch 8 not taken.
44208 headers->push_back("Authorization: AWS " + config_.access_key + ":"
648
3/6
✓ Branch 2 taken 14736 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14736 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 14736 times.
✗ Branch 9 not taken.
73680 + Base64(string(reinterpret_cast<char *>(hmac.digest),
649 14736 hmac.GetDigestSize())));
650
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
14736 headers->push_back("Date: " + timestamp);
651
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
14736 headers->push_back("X-Amz-Acl: " + config_.x_amz_acl);
652
2/2
✓ Branch 1 taken 7380 times.
✓ Branch 2 taken 7356 times.
14736 if (!payload_hash.empty())
653
2/4
✓ Branch 1 taken 7380 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7380 times.
✗ Branch 5 not taken.
7380 headers->push_back("Content-MD5: " + payload_hash);
654
2/2
✓ Branch 1 taken 7380 times.
✓ Branch 2 taken 7356 times.
14736 if (!content_type.empty())
655
2/4
✓ Branch 1 taken 7380 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7380 times.
✗ Branch 5 not taken.
7380 headers->push_back("Content-Type: " + content_type);
656 14736 return true;
657 14736 }
658
659
660 ✗ string S3FanoutManager::GetUriEncode(const string &val,
661 bool encode_slash) const {
662 ✗ string result;
663 ✗ const unsigned len = val.length();
664 ✗ result.reserve(len);
665 ✗ for (unsigned i = 0; i < len; ++i) {
666 ✗ const char c = val[i];
667 ✗ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
668 ✗ || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '~'
669 ✗ || c == '.') {
670 ✗ result.push_back(c);
671 ✗ } else if (c == '/') {
672 ✗ if (encode_slash) {
673 ✗ result += "%2F";
674 } else {
675 ✗ result.push_back(c);
676 }
677 } else {
678 ✗ result.push_back('%');
679 ✗ result.push_back((c / 16) + ((c / 16 <= 9) ? '0' : 'A' - 10));
680 ✗ result.push_back((c % 16) + ((c % 16 <= 9) ? '0' : 'A' - 10));
681 }
682 }
683 ✗ return result;
684 }
685
686
687 ✗ string S3FanoutManager::GetAwsV4SigningKey(const string &date) const {
688 ✗ if (last_signing_key_.first == date)
689 ✗ return last_signing_key_.second;
690
691 ✗ const string date_key = shash::Hmac256("AWS4" + config_.secret_key, date,
692 ✗ true);
693 ✗ const string date_region_key = shash::Hmac256(date_key, config_.region, true);
694 const string date_region_service_key = shash::Hmac256(date_region_key, "s3",
695 ✗ true);
696 string signing_key = shash::Hmac256(date_region_service_key, "aws4_request",
697 ✗ true);
698 ✗ last_signing_key_.first = date;
699 ✗ last_signing_key_.second = signing_key;
700 ✗ return signing_key;
701 }
702
703
704 /**
705 * The Amazon AWS4 authorization header according to
706 * http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
707 */
708 ✗ bool S3FanoutManager::MkV4Authz(const JobInfo &info,
709 vector<string> *headers) const {
710 ✗ string payload_hash;
711 ✗ const bool retval = MkPayloadHash(info, &payload_hash);
712 ✗ if (!retval)
713 ✗ return false;
714 ✗ const string content_type = GetContentType(info);
715 ✗ const string timestamp = IsoTimestamp();
716 ✗ const string date = timestamp.substr(0, 8);
717 ✗ vector<string> tokens = SplitString(complete_hostname_, ':');
718 ✗ assert(tokens.size() <= 2);
719 ✗ string canonical_hostname = tokens[0];
720
721 // if we could split the hostname in two and if the port is *NOT* a default
722 // one
723 ✗ if (tokens.size() == 2
724 ✗ && !((String2Uint64(tokens[1]) == kDefaultHTTPPort)
725 ✗ || (String2Uint64(tokens[1]) == kDefaultHTTPSPort)))
726 ✗ canonical_hostname += ":" + tokens[1];
727
728 ✗ string signed_headers;
729 ✗ string canonical_headers;
730 ✗ if (!content_type.empty()) {
731 ✗ signed_headers += "content-type;";
732 ✗ headers->push_back("Content-Type: " + content_type);
733 ✗ canonical_headers += "content-type:" + content_type + "\n";
734 }
735 ✗ if (config_.x_amz_acl != "") {
736 ✗ signed_headers += "host;x-amz-acl;x-amz-content-sha256;x-amz-date";
737 } else {
738 ✗ signed_headers += "host;x-amz-content-sha256;x-amz-date";
739 }
740 ✗ canonical_headers += "host:" + canonical_hostname + "\n";
741 ✗ if (config_.x_amz_acl != "") {
742 ✗ canonical_headers += "x-amz-acl:" + config_.x_amz_acl + "\n";
743 }
744 ✗ canonical_headers += "x-amz-content-sha256:" + payload_hash + "\n"
745 ✗ + "x-amz-date:" + timestamp + "\n";
746
747 ✗ const string scope = date + "/" + config_.region + "/s3/aws4_request";
748 ✗ string uri;
749 ✗ if (config_.dns_buckets) {
750 ✗ uri = string("/") + info.object_key;
751 ✗ } else if (info.object_key.empty()) {
752 ✗ uri = string("/") + config_.bucket;
753 } else {
754 ✗ uri = string("/") + config_.bucket + "/" + info.object_key;
755 }
756
757 // V4 canonical query string: empty for most requests, "delete=" for
758 // multi-object delete (the S3 ?delete parameter has no value)
759 ✗ const string canonical_query = (info.request == JobInfo::kReqDeleteMulti)
760 ? "delete="
761 ✗ : "";
762
763 ✗ const string canonical_request = GetRequestString(info) + "\n"
764 ✗ + GetUriEncode(uri, false) + "\n"
765 ✗ + canonical_query + "\n" + canonical_headers
766 ✗ + "\n" + signed_headers + "\n"
767 ✗ + payload_hash;
768
769 ✗ const string hash_request = shash::Sha256String(canonical_request.c_str());
770
771 ✗ const string string_to_sign = "AWS4-HMAC-SHA256\n" + timestamp + "\n" + scope
772 ✗ + "\n" + hash_request;
773
774 ✗ const string signing_key = GetAwsV4SigningKey(date);
775 ✗ const string signature = shash::Hmac256(signing_key, string_to_sign);
776
777 ✗ headers->push_back("X-Amz-Acl: " + config_.x_amz_acl);
778 ✗ headers->push_back("X-Amz-Content-Sha256: " + payload_hash);
779 ✗ headers->push_back("X-Amz-Date: " + timestamp);
780 ✗ headers->push_back("Authorization: AWS4-HMAC-SHA256 "
781 "Credential="
782 ✗ + config_.access_key + "/" + scope
783 ✗ + ","
784 "SignedHeaders="
785 ✗ + signed_headers
786 ✗ + ","
787 "Signature="
788 ✗ + signature);
789
790 // S3 requires Content-MD5 for multi-object delete
791 ✗ if (info.request == JobInfo::kReqDeleteMulti) {
792 ✗ shash::Any md5_hash(shash::kMd5);
793 unsigned char *data;
794 ✗ const unsigned int nbytes = info.origin->Data(
795 ✗ reinterpret_cast<void **>(&data), info.origin->GetSize(), 0);
796 ✗ assert(nbytes == info.origin->GetSize());
797 ✗ shash::HashMem(data, nbytes, &md5_hash);
798 ✗ headers->push_back(
799 "Content-MD5: "
800 ✗ + Base64(string(reinterpret_cast<char *>(md5_hash.digest),
801 ✗ md5_hash.GetDigestSize())));
802 }
803 ✗ return true;
804 }
805
806 /**
807 * The Azure Blob authorization header according to
808 * https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key
809 */
810 ✗ bool S3FanoutManager::MkAzureAuthz(const JobInfo &info,
811 vector<string> *headers) const {
812 ✗ const string timestamp = RfcTimestamp();
813 const string canonical_headers = "x-ms-blob-type:BlockBlob\nx-ms-date:"
814 ✗ + timestamp + "\nx-ms-version:2011-08-18";
815 ✗ const string canonical_resource = "/" + config_.access_key + "/"
816 ✗ + config_.bucket + "/" + info.object_key;
817
818 ✗ string string_to_sign;
819 ✗ if ((info.request == JobInfo::kReqHeadOnly)
820 ✗ || (info.request == JobInfo::kReqHeadPut)
821 ✗ || (info.request == JobInfo::kReqDelete)) {
822 ✗ string_to_sign = GetRequestString(info) + string("\n\n\n")
823 ✗ + "\n\n\n\n\n\n\n\n\n" + canonical_headers + "\n"
824 ✗ + canonical_resource;
825 } else {
826 ✗ string_to_sign = GetRequestString(info) + string("\n\n\n")
827 ✗ + string(StringifyInt(info.origin->GetSize()))
828 ✗ + "\n\n\n\n\n\n\n\n\n" + canonical_headers + "\n"
829 ✗ + canonical_resource;
830 }
831
832 ✗ string signing_key;
833 ✗ const int retval = Debase64(config_.secret_key, &signing_key);
834 ✗ if (!retval)
835 ✗ return false;
836
837 ✗ const string signature = shash::Hmac256(signing_key, string_to_sign, true);
838
839 ✗ headers->push_back("x-ms-date: " + timestamp);
840 ✗ headers->push_back("x-ms-version: 2011-08-18");
841 ✗ headers->push_back("Authorization: SharedKey " + config_.access_key + ":"
842 ✗ + Base64(signature));
843 ✗ headers->push_back("x-ms-blob-type: BlockBlob");
844 ✗ return true;
845 }
846
847 14736 void S3FanoutManager::InitializeDnsSettingsCurl(CURL *handle,
848 CURLSH *sharehandle,
849 curl_slist *clist) const {
850 14736 CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, sharehandle);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
852 14736 retval = curl_easy_setopt(handle, CURLOPT_RESOLVE, clist);
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
854 14736 }
855
856
857 14736 int S3FanoutManager::InitializeDnsSettings(CURL *handle,
858 std::string host_with_port) const {
859 // Use existing handle
860 const std::map<CURL *, S3FanOutDnsEntry *>::const_iterator
861
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 it = curl_sharehandles_->find(handle);
862
2/2
✓ Branch 3 taken 14076 times.
✓ Branch 4 taken 660 times.
14736 if (it != curl_sharehandles_->end()) {
863
1/2
✓ Branch 1 taken 14076 times.
✗ Branch 2 not taken.
14076 InitializeDnsSettingsCurl(handle, it->second->sharehandle,
864 14076 it->second->clist);
865 14076 return 0;
866 }
867
868 // Add protocol information for extraction of fields for DNS
869
2/4
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 660 times.
✗ Branch 4 not taken.
660 if (!IsHttpUrl(host_with_port))
870
2/4
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 660 times.
✗ Branch 5 not taken.
660 host_with_port = config_.protocol + "://" + host_with_port;
871
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 const std::string remote_host = dns::ExtractHost(host_with_port);
872
1/2
✓ Branch 1 taken 660 times.
✗ Branch 2 not taken.
660 const std::string remote_port = dns::ExtractPort(host_with_port);
873
874 // If we have the name already resolved, use the least used IP
875 660 S3FanOutDnsEntry *useme = NULL;
876 660 unsigned int usemin = UINT_MAX;
877 660 std::set<S3FanOutDnsEntry *>::iterator its3 = sharehandles_->begin();
878
2/2
✓ Branch 3 taken 528 times.
✓ Branch 4 taken 660 times.
1188 for (; its3 != sharehandles_->end(); ++its3) {
879
1/2
✓ Branch 2 taken 528 times.
✗ Branch 3 not taken.
528 if ((*its3)->dns_name == remote_host) {
880
1/2
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
528 if (usemin >= (*its3)->counter) {
881 528 usemin = (*its3)->counter;
882 528 useme = (*its3);
883 }
884 }
885 }
886
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 132 times.
660 if (useme != NULL) {
887
1/2
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
528 curl_sharehandles_->insert(
888 528 std::pair<CURL *, S3FanOutDnsEntry *>(handle, useme));
889 528 useme->counter++;
890
1/2
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
528 InitializeDnsSettingsCurl(handle, useme->sharehandle, useme->clist);
891 528 return 0;
892 }
893
894 // We need to resolve the hostname
895 // TODO(ssheikki): support ipv6 also... if (opt_ipv4_only_)
896
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 const dns::Host host = resolver_->Resolve(remote_host);
897
1/2
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
132 set<string> const ipv4_addresses = host.ipv4_addresses();
898 132 std::set<string>::iterator its = ipv4_addresses.begin();
899 132 S3FanOutDnsEntry *dnse = NULL;
900
2/2
✓ Branch 3 taken 132 times.
✓ Branch 4 taken 132 times.
264 for (; its != ipv4_addresses.end(); ++its) {
901
2/4
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 132 times.
✗ Branch 5 not taken.
132 dnse = new S3FanOutDnsEntry();
902 132 dnse->counter = 0;
903
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 dnse->dns_name = remote_host;
904
4/12
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 8 taken 132 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 132 times.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 132 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
132 dnse->port = remote_port.size() == 0 ? "80" : remote_port;
905
1/2
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
132 dnse->ip = *its;
906 132 dnse->clist = NULL;
907
1/2
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
132 dnse->clist = curl_slist_append(
908 dnse->clist,
909
4/8
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 132 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 132 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 132 times.
✗ Branch 11 not taken.
264 (dnse->dns_name + ":" + dnse->port + ":" + dnse->ip).c_str());
910
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 dnse->sharehandle = curl_share_init();
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 assert(dnse->sharehandle != NULL);
912
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 const CURLSHcode share_retval = curl_share_setopt(
913 dnse->sharehandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 assert(share_retval == CURLSHE_OK);
915
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 sharehandles_->insert(dnse);
916 }
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (dnse == NULL) {
918 ✗ LogCvmfs(kLogS3Fanout, kLogStderr | kLogSyslogErr,
919 "Error: DNS resolve failed for address '%s'.",
920 remote_host.c_str());
921 ✗ assert(dnse != NULL);
922 ✗ return -1;
923 }
924
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 curl_sharehandles_->insert(
925 132 std::pair<CURL *, S3FanOutDnsEntry *>(handle, dnse));
926 132 dnse->counter++;
927
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 InitializeDnsSettingsCurl(handle, dnse->sharehandle, dnse->clist);
928
929 132 return 0;
930 660 }
931
932
933 14736 bool S3FanoutManager::MkPayloadHash(const JobInfo &info,
934 string *hex_hash) const {
935
2/2
✓ Branch 0 taken 14676 times.
✓ Branch 1 taken 60 times.
14736 if (info.request == JobInfo::kReqHeadOnly
936
2/2
✓ Branch 0 taken 7380 times.
✓ Branch 1 taken 7296 times.
14676 || info.request == JobInfo::kReqHeadPut
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 || info.request == JobInfo::kReqDelete) {
938
1/4
✓ Branch 0 taken 7356 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7356 switch (config_.authz_method) {
939 7356 case kAuthzAwsV2:
940 7356 hex_hash->clear();
941 7356 break;
942 ✗ case kAuthzAwsV4:
943 // Sha256 over empty string
944 *hex_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b78"
945 ✗ "52b855";
946 ✗ break;
947 ✗ case kAuthzAzure:
948 // no payload hash required for Azure signature
949 ✗ hex_hash->clear();
950 ✗ break;
951 ✗ default:
952 ✗ PANIC(NULL);
953 }
954 7356 return true;
955 }
956
957 // kReqDeleteMulti is a POST with a body (XML payload), same hashing as PUT
958 // falls through intentionally.
959
960 // PUT or POST with payload
961
1/2
✓ Branch 1 taken 7380 times.
✗ Branch 2 not taken.
7380 shash::Any payload_hash(shash::kMd5);
962
963 unsigned char *data;
964 7380 const unsigned int nbytes = info.origin->Data(
965
2/4
✓ Branch 2 taken 7380 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7380 times.
✗ Branch 6 not taken.
7380 reinterpret_cast<void **>(&data), info.origin->GetSize(), 0);
966
2/4
✓ Branch 2 taken 7380 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7380 times.
7380 assert(nbytes == info.origin->GetSize());
967
968
1/4
✓ Branch 0 taken 7380 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7380 switch (config_.authz_method) {
969 7380 case kAuthzAwsV2:
970
1/2
✓ Branch 1 taken 7380 times.
✗ Branch 2 not taken.
7380 shash::HashMem(data, nbytes, &payload_hash);
971
2/4
✓ Branch 2 taken 7380 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7380 times.
✗ Branch 6 not taken.
22140 *hex_hash = Base64(string(reinterpret_cast<char *>(payload_hash.digest),
972 14760 payload_hash.GetDigestSize()));
973 7380 return true;
974 ✗ case kAuthzAwsV4:
975 ✗ *hex_hash = shash::Sha256Mem(data, nbytes);
976 ✗ return true;
977 ✗ case kAuthzAzure:
978 // no payload hash required for Azure signature
979 ✗ hex_hash->clear();
980 ✗ return true;
981 ✗ default:
982 ✗ PANIC(NULL);
983 }
984 }
985
986 14736 string S3FanoutManager::GetRequestString(const JobInfo &info) const {
987
3/5
✓ Branch 0 taken 7356 times.
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✗ Branch 4 not taken.
14736 switch (info.request) {
988 7356 case JobInfo::kReqHeadOnly:
989 case JobInfo::kReqHeadPut:
990
1/2
✓ Branch 2 taken 7356 times.
✗ Branch 3 not taken.
7356 return "HEAD";
991 7296 case JobInfo::kReqPutCas:
992 case JobInfo::kReqPutDotCvmfs:
993 case JobInfo::kReqPutHtml:
994 case JobInfo::kReqPutBucket:
995
1/2
✓ Branch 2 taken 7296 times.
✗ Branch 3 not taken.
7296 return "PUT";
996 ✗ case JobInfo::kReqDelete:
997 ✗ return "DELETE";
998 84 case JobInfo::kReqDeleteMulti:
999
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 return "POST";
1000 ✗ default:
1001 ✗ PANIC(NULL);
1002 }
1003 }
1004
1005
1006 14736 string S3FanoutManager::GetContentType(const JobInfo &info) const {
1007
3/6
✓ Branch 0 taken 7356 times.
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
14736 switch (info.request) {
1008 7356 case JobInfo::kReqHeadOnly:
1009 case JobInfo::kReqHeadPut:
1010 case JobInfo::kReqDelete:
1011
1/2
✓ Branch 2 taken 7356 times.
✗ Branch 3 not taken.
7356 return "";
1012 7296 case JobInfo::kReqPutCas:
1013
1/2
✓ Branch 2 taken 7296 times.
✗ Branch 3 not taken.
7296 return "application/octet-stream";
1014 ✗ case JobInfo::kReqPutDotCvmfs:
1015 ✗ return "application/x-cvmfs";
1016 ✗ case JobInfo::kReqPutHtml:
1017 ✗ return "text/html";
1018 84 case JobInfo::kReqPutBucket:
1019 case JobInfo::kReqDeleteMulti:
1020
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 return "text/xml";
1021 ✗ default:
1022 ✗ PANIC(NULL);
1023 }
1024 }
1025
1026
1027 /**
1028 * Request parameters set the URL and other options such as timeout and
1029 * proxy.
1030 */
1031 14736 Failures S3FanoutManager::InitializeRequest(JobInfo *info, CURL *handle) const {
1032 // Initialize internal download state
1033 14736 info->curl_handle = handle;
1034 14736 info->error_code = kFailOk;
1035 14736 info->http_error = 0;
1036 14736 info->num_retries = 0;
1037 14736 info->backoff_ms = 0;
1038 14736 info->throttle_ms = 0;
1039 14736 info->throttle_timestamp = 0;
1040 14736 info->http_headers = NULL;
1041 // info->payload_size is needed in S3Uploader::MainCollectResults,
1042 // where info->origin is already destroyed.
1043
1/2
✓ Branch 2 taken 14736 times.
✗ Branch 3 not taken.
14736 info->payload_size = info->origin->GetSize();
1044
1045
2/4
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14736 times.
✗ Branch 5 not taken.
14736 InitializeDnsSettings(handle, complete_hostname_);
1046
1047 CURLcode retval;
1048
2/2
✓ Branch 0 taken 14676 times.
✓ Branch 1 taken 60 times.
14736 if (info->request == JobInfo::kReqHeadOnly
1049
2/2
✓ Branch 0 taken 7380 times.
✓ Branch 1 taken 7296 times.
14676 || info->request == JobInfo::kReqHeadPut
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 || info->request == JobInfo::kReqDelete) {
1051
1/2
✓ Branch 1 taken 7356 times.
✗ Branch 2 not taken.
7356 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 0);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7356 times.
7356 assert(retval == CURLE_OK);
1053
1/2
✓ Branch 1 taken 7356 times.
✗ Branch 2 not taken.
7356 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 1);
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7356 times.
7356 assert(retval == CURLE_OK);
1055
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7356 times.
7356 if (info->request == JobInfo::kReqDelete) {
1057 ✗ retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST,
1058 GetRequestString(*info).c_str());
1059 ✗ assert(retval == CURLE_OK);
1060 } else {
1061
1/2
✓ Branch 1 taken 7356 times.
✗ Branch 2 not taken.
7356 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7356 times.
7356 assert(retval == CURLE_OK);
1063 }
1064
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7296 times.
7380 } else if (info->request == JobInfo::kReqDeleteMulti) {
1065 // POST request with XML body read from origin buffer
1066
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(retval == CURLE_OK);
1068
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(retval == CURLE_OK);
1070
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "POST");
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(retval == CURLE_OK);
1072
2/4
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 84 times.
✗ Branch 6 not taken.
84 retval = curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE,
1073 static_cast<curl_off_t>(info->origin->GetSize()));
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(retval == CURLE_OK);
1075 84 info->response_body.clear();
1076 } else {
1077
1/2
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
7296 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 assert(retval == CURLE_OK);
1079
1/2
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
7296 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 assert(retval == CURLE_OK);
1081
1/2
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
7296 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 assert(retval == CURLE_OK);
1083
2/4
✓ Branch 2 taken 7296 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7296 times.
✗ Branch 6 not taken.
7296 retval = curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE,
1084 static_cast<curl_off_t>(info->origin->GetSize()));
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 assert(retval == CURLE_OK);
1086
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 if (info->request == JobInfo::kReqPutDotCvmfs) {
1088 ✗ info->http_headers = curl_slist_append(
1089 info->http_headers, dot_cvmfs_cache_control_header.c_str());
1090
1/2
✓ Branch 0 taken 7296 times.
✗ Branch 1 not taken.
7296 } else if (info->request == JobInfo::kReqPutCas) {
1091
1/2
✓ Branch 1 taken 7296 times.
✗ Branch 2 not taken.
7296 info->http_headers = curl_slist_append(info->http_headers,
1092 kCacheControlCas);
1093 }
1094 }
1095
1096 bool retval_b;
1097
1098 // Authorization
1099 14736 vector<string> authz_headers;
1100
1/4
✓ Branch 0 taken 14736 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14736 switch (config_.authz_method) {
1101 14736 case kAuthzAwsV2:
1102
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval_b = MkV2Authz(*info, &authz_headers);
1103 14736 break;
1104 ✗ case kAuthzAwsV4:
1105 ✗ retval_b = MkV4Authz(*info, &authz_headers);
1106 ✗ break;
1107 ✗ case kAuthzAzure:
1108 ✗ retval_b = MkAzureAuthz(*info, &authz_headers);
1109 ✗ break;
1110 ✗ default:
1111 ✗ PANIC(NULL);
1112 }
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 if (!retval_b)
1114 ✗ return kFailLocalIO;
1115
2/2
✓ Branch 1 taken 58968 times.
✓ Branch 2 taken 14736 times.
73704 for (unsigned i = 0; i < authz_headers.size(); ++i) {
1116
1/2
✓ Branch 2 taken 58968 times.
✗ Branch 3 not taken.
58968 info->http_headers = curl_slist_append(info->http_headers,
1117 58968 authz_headers[i].c_str());
1118 }
1119
1120 // Common headers
1121
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 info->http_headers = curl_slist_append(info->http_headers,
1122 "Connection: Keep-Alive");
1123
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 info->http_headers = curl_slist_append(info->http_headers, "Pragma:");
1124 // No 100-continue
1125
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 info->http_headers = curl_slist_append(info->http_headers, "Expect:");
1126 // Strip unnecessary header
1127
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 info->http_headers = curl_slist_append(info->http_headers, "Accept:");
1128
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 info->http_headers = curl_slist_append(info->http_headers,
1129 14736 user_agent_->c_str());
1130
1131 // Set curl parameters
1132
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_PRIVATE, static_cast<void *>(info));
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1134
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_HEADERDATA,
1135 static_cast<void *>(info));
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1137
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_READDATA,
1138 static_cast<void *>(info));
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1140
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_WRITEDATA,
1141 static_cast<void *>(info));
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1143
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, info->http_headers);
1144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 if (opt_ipv4_only_) {
1146 ✗ retval = curl_easy_setopt(handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1147 ✗ assert(retval == CURLE_OK);
1148 }
1149 // Follow HTTP redirects
1150
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1152
1153
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, info->errorbuffer);
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1155
1156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14736 times.
14736 if (config_.protocol == "https") {
1157 ✗ retval = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
1158 ✗ assert(retval == CURLE_OK);
1159 ✗ retval = curl_easy_setopt(handle, CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
1160 ✗ assert(retval == CURLE_OK);
1161 ✗ const bool add_cert = ssl_certificate_store_.ApplySslCertificatePath(
1162 handle);
1163 ✗ assert(add_cert);
1164 }
1165
1166 14736 return kFailOk;
1167 14736 }
1168
1169
1170 /**
1171 * Sets the URL specific options such as host to use and timeout.
1172 */
1173 14736 void S3FanoutManager::SetUrlOptions(JobInfo *info) const {
1174 14736 CURL *curl_handle = info->curl_handle;
1175 CURLcode retval;
1176
1177
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT,
1178 config_.opt_timeout_sec);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1180
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_LIMIT,
1181 kLowSpeedLimit);
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1183
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 retval = curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_TIME,
1184 config_.opt_timeout_sec);
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 if (is_curl_debug_) {
1188 ✗ retval = curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
1189 ✗ assert(retval == CURLE_OK);
1190 }
1191
1192
1/2
✓ Branch 1 taken 14736 times.
✗ Branch 2 not taken.
14736 string url = MkUrl(info->object_key);
1193
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 14652 times.
14736 if (info->request == JobInfo::kReqDeleteMulti)
1194
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 url += "?delete";
1195
1/2
✓ Branch 2 taken 14736 times.
✗ Branch 3 not taken.
14736 retval = curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1197
1198
1/2
✓ Branch 2 taken 14736 times.
✗ Branch 3 not taken.
14736 retval = curl_easy_setopt(curl_handle, CURLOPT_PROXY, config_.proxy.c_str());
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14736 times.
14736 assert(retval == CURLE_OK);
1200 14736 }
1201
1202
1203 /**
1204 * Adds transfer time and uploaded bytes to the global counters.
1205 */
1206 14784 void S3FanoutManager::UpdateStatistics(CURL *handle) {
1207 curl_off_t val;
1208
1209
2/4
✓ Branch 1 taken 14784 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14784 times.
✗ Branch 4 not taken.
14784 if (curl_easy_getinfo(handle, CURLINFO_SIZE_UPLOAD_T, &val) == CURLE_OK)
1210 14784 statistics_->transferred_bytes += val;
1211 14784 }
1212
1213
1214 /**
1215 * Retry if possible and if not already done too often.
1216 */
1217 72 bool S3FanoutManager::CanRetry(const JobInfo *info) {
1218 72 return (info->error_code == kFailHostConnection
1219
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 || info->error_code == kFailHostResolve
1220
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 || info->error_code == kFailServiceUnavailable
1221
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 || info->error_code == kFailRetry)
1222
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
144 && (info->num_retries < config_.opt_max_retries);
1223 }
1224
1225
1226 /**
1227 * Backoff for retry to introduce a jitter into a upload sequence.
1228 *
1229 * \return true if backoff has been performed, false otherwise
1230 */
1231 48 void S3FanoutManager::Backoff(JobInfo *info) {
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (info->error_code != kFailRetry)
1233 ✗ info->num_retries++;
1234 48 statistics_->num_retries++;
1235
1236
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (info->throttle_ms > 0) {
1237 48 LogCvmfs(kLogS3Fanout, kLogDebug, "throttling for %d ms",
1238 info->throttle_ms);
1239 48 const uint64_t now = platform_monotonic_time();
1240
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if ((info->throttle_timestamp + (info->throttle_ms / 1000)) >= now) {
1241
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 36 times.
48 if ((now - timestamp_last_throttle_report_)
1242 > kThrottleReportIntervalSec) {
1243 12 LogCvmfs(kLogS3Fanout, kLogStdout,
1244 "Warning: S3 backend throttling %ums "
1245 "(total backoff time so far %lums)",
1246 12 info->throttle_ms, statistics_->ms_throttled);
1247 12 timestamp_last_throttle_report_ = now;
1248 }
1249 48 statistics_->ms_throttled += info->throttle_ms;
1250 48 SafeSleepMs(info->throttle_ms);
1251 }
1252 } else {
1253 ✗ if (info->backoff_ms == 0) {
1254 // Must be != 0
1255 ✗ info->backoff_ms = prng_.Next(config_.opt_backoff_init_ms + 1);
1256 } else {
1257 ✗ info->backoff_ms *= 2;
1258 }
1259 ✗ if (info->backoff_ms > config_.opt_backoff_max_ms)
1260 ✗ info->backoff_ms = config_.opt_backoff_max_ms;
1261
1262 ✗ LogCvmfs(kLogS3Fanout, kLogDebug, "backing off for %d ms",
1263 info->backoff_ms);
1264 ✗ SafeSleepMs(info->backoff_ms);
1265 }
1266 48 }
1267
1268
1269 /**
1270 * Checks the result of a curl request and implements the failure logic
1271 * and takes care of cleanup.
1272 *
1273 * @return true if request should be repeated, false otherwise
1274 */
1275 14784 bool S3FanoutManager::VerifyAndFinalize(const int curl_error, JobInfo *info) {
1276 14784 LogCvmfs(kLogS3Fanout, kLogDebug,
1277 "Verify uploaded/tested object %s "
1278 "(curl error %d, info error %d, info request %d)",
1279 14784 info->object_key.c_str(), curl_error, info->error_code,
1280 14784 info->request);
1281 14784 UpdateStatistics(info->curl_handle);
1282
1283 // Verification and error classification
1284
1/6
✓ Branch 0 taken 14784 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
14784 switch (curl_error) {
1285 14784 case CURLE_OK:
1286
2/2
✓ Branch 0 taken 14736 times.
✓ Branch 1 taken 48 times.
14784 if ((info->error_code != kFailRetry)
1287
2/2
✓ Branch 0 taken 7416 times.
✓ Branch 1 taken 7320 times.
14736 && (info->error_code != kFailNotFound)) {
1288 7416 info->error_code = kFailOk;
1289 }
1290 14784 break;
1291 ✗ case CURLE_UNSUPPORTED_PROTOCOL:
1292 case CURLE_URL_MALFORMAT:
1293 ✗ info->error_code = kFailBadRequest;
1294 ✗ break;
1295 ✗ case CURLE_COULDNT_RESOLVE_HOST:
1296 ✗ info->error_code = kFailHostResolve;
1297 ✗ break;
1298 ✗ case CURLE_COULDNT_CONNECT:
1299 case CURLE_OPERATION_TIMEDOUT:
1300 case CURLE_SEND_ERROR:
1301 case CURLE_RECV_ERROR:
1302 ✗ info->error_code = kFailHostConnection;
1303 ✗ break;
1304 ✗ case CURLE_ABORTED_BY_CALLBACK:
1305 case CURLE_WRITE_ERROR:
1306 // Error set by callback
1307 ✗ break;
1308 ✗ default:
1309 ✗ LogCvmfs(kLogS3Fanout, kLogStderr | kLogSyslogErr,
1310 "unexpected curl error (%d) while trying to upload %s: %s",
1311 curl_error, info->object_key.c_str(), info->errorbuffer);
1312 ✗ info->error_code = kFailOther;
1313 ✗ break;
1314 }
1315
1316 // Transform HEAD to PUT request
1317
2/2
✓ Branch 0 taken 7320 times.
✓ Branch 1 taken 7464 times.
14784 if ((info->error_code == kFailNotFound)
1318
2/2
✓ Branch 0 taken 7296 times.
✓ Branch 1 taken 24 times.
7320 && (info->request == JobInfo::kReqHeadPut)) {
1319 7296 LogCvmfs(kLogS3Fanout, kLogDebug, "not found: %s, uploading",
1320 info->object_key.c_str());
1321 7296 info->request = JobInfo::kReqPutCas;
1322 7296 curl_slist_free_all(info->http_headers);
1323 7296 info->http_headers = NULL;
1324 7296 const s3fanout::Failures init_failure = InitializeRequest(
1325 info, info->curl_handle);
1326
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7296 times.
7296 if (init_failure != s3fanout::kFailOk) {
1328 ✗ PANIC(kLogStderr,
1329 "Failed to initialize CURL handle "
1330 "(error: %d - %s | errno: %d)",
1331 init_failure, Code2Ascii(init_failure), errno);
1332 }
1333 7296 SetUrlOptions(info);
1334 // Reset origin
1335 7296 info->origin->Rewind();
1336 7296 return true; // Again, Put
1337 }
1338
1339 // Determination if failed request should be repeated
1340 7488 bool try_again = false;
1341
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 7416 times.
7488 if (info->error_code != kFailOk) {
1342 72 try_again = CanRetry(info);
1343 }
1344
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7440 times.
7488 if (try_again) {
1345
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (info->request == JobInfo::kReqPutCas
1346
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 || info->request == JobInfo::kReqPutDotCvmfs
1347
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 || info->request == JobInfo::kReqPutHtml
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 || info->request == JobInfo::kReqDeleteMulti) {
1349 ✗ LogCvmfs(kLogS3Fanout, kLogDebug, "Trying again to upload %s",
1350 info->object_key.c_str());
1351 // Reset origin
1352 ✗ info->origin->Rewind();
1353 ✗ if (info->request == JobInfo::kReqDeleteMulti)
1354 ✗ info->response_body.clear();
1355 }
1356 48 Backoff(info);
1357 48 info->error_code = kFailOk;
1358 48 info->http_error = 0;
1359 48 info->throttle_ms = 0;
1360 48 info->backoff_ms = 0;
1361 48 info->throttle_timestamp = 0;
1362 48 return true; // try again
1363 }
1364
1365 // Cleanup opened resources
1366 7440 info->origin.reset();
1367
1368
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 7416 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
7440 if ((info->error_code != kFailOk) && (info->http_error != 0)
1369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 && (info->http_error != 404)) {
1370 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "S3: HTTP failure %d", info->http_error);
1371 }
1372 7440 return false; // stop transfer
1373 }
1374
1375
2/4
✓ Branch 3 taken 156 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 156 times.
✗ Branch 10 not taken.
156 S3FanoutManager::S3FanoutManager(const S3Config &config) : config_(config) {
1376 156 atomic_init32(&multi_threaded_);
1377
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 MakePipe(pipe_terminate_);
1378
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 MakePipe(pipe_jobs_);
1379
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 MakePipe(pipe_completed_);
1380
1381 int retval;
1382 156 jobs_todo_lock_ = reinterpret_cast<pthread_mutex_t *>(
1383 156 smalloc(sizeof(pthread_mutex_t)));
1384 156 retval = pthread_mutex_init(jobs_todo_lock_, NULL);
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(retval == 0);
1386 156 curl_handle_lock_ = reinterpret_cast<pthread_mutex_t *>(
1387 156 smalloc(sizeof(pthread_mutex_t)));
1388 156 retval = pthread_mutex_init(curl_handle_lock_, NULL);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(retval == 0);
1390
1391
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 active_requests_ = new set<JobInfo *>;
1392
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 pool_handles_idle_ = new set<CURL *>;
1393
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 pool_handles_inuse_ = new set<CURL *>;
1394
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 curl_sharehandles_ = new map<CURL *, S3FanOutDnsEntry *>;
1395
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 sharehandles_ = new set<S3FanOutDnsEntry *>;
1396 156 watch_fds_max_ = 4 * config_.pool_max_handles;
1397 156 max_available_jobs_ = 4 * config_.pool_max_handles;
1398
2/4
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 156 times.
✗ Branch 5 not taken.
156 available_jobs_ = new Semaphore(max_available_jobs_);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(NULL != available_jobs_);
1400
1401
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 statistics_ = new Statistics();
1402
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 user_agent_ = new string();
1403
2/4
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 156 times.
✗ Branch 6 not taken.
156 *user_agent_ = "User-Agent: cvmfs " + string(CVMFS_VERSION);
1404
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 complete_hostname_ = MkCompleteHostname();
1405
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 dot_cvmfs_cache_control_header = MkDotCvmfsCacheControlHeader();
1406
1407
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 const CURLcode cretval = curl_global_init(CURL_GLOBAL_ALL);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(cretval == CURLE_OK);
1409
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 curl_multi_ = curl_multi_init();
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(curl_multi_ != NULL);
1411 CURLMcode mretval;
1412
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETFUNCTION,
1413 CallbackCurlSocket);
1414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(mretval == CURLM_OK);
1415
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETDATA,
1416 static_cast<void *>(this));
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(mretval == CURLM_OK);
1418
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_MAX_TOTAL_CONNECTIONS,
1419 config_.pool_max_handles);
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(mretval == CURLM_OK);
1421
1422 156 prng_.InitLocaltime();
1423
1424 156 thread_upload_ = 0;
1425 156 timestamp_last_throttle_report_ = 0;
1426 156 is_curl_debug_ = (getenv("_CVMFS_CURL_DEBUG") != NULL);
1427
1428 // Parsing environment variables
1429 156 if ((getenv("CVMFS_IPV4_ONLY") != NULL)
1430
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 156 times.
156 && (strlen(getenv("CVMFS_IPV4_ONLY")) > 0)) {
1431 ✗ opt_ipv4_only_ = true;
1432 } else {
1433 156 opt_ipv4_only_ = false;
1434 }
1435
1436
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 resolver_ = dns::CaresResolver::Create(opt_ipv4_only_, 2, 2000);
1437
1438 156 watch_fds_ = static_cast<struct pollfd *>(smalloc(4 * sizeof(struct pollfd)));
1439 156 watch_fds_size_ = 4;
1440 156 watch_fds_inuse_ = 0;
1441
1442
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 ssl_certificate_store_.UseSystemCertificatePath();
1443 156 }
1444
1445 312 S3FanoutManager::~S3FanoutManager() {
1446 156 pthread_mutex_destroy(jobs_todo_lock_);
1447 156 free(jobs_todo_lock_);
1448 156 pthread_mutex_destroy(curl_handle_lock_);
1449 156 free(curl_handle_lock_);
1450
1451
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 if (atomic_xadd32(&multi_threaded_, 0) == 1) {
1452 // Shutdown I/O thread
1453 156 char buf = 'T';
1454 156 WritePipe(pipe_terminate_[1], &buf, 1);
1455 156 pthread_join(thread_upload_, NULL);
1456 }
1457 156 ClosePipe(pipe_terminate_);
1458 156 ClosePipe(pipe_jobs_);
1459 156 ClosePipe(pipe_completed_);
1460
1461 156 set<CURL *>::iterator i = pool_handles_idle_->begin();
1462 156 const set<CURL *>::const_iterator iEnd = pool_handles_idle_->end();
1463
2/2
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 156 times.
468 for (; i != iEnd; ++i) {
1464 312 curl_easy_cleanup(*i);
1465 }
1466
1467 156 set<S3FanOutDnsEntry *>::iterator is = sharehandles_->begin();
1468 156 const set<S3FanOutDnsEntry *>::const_iterator isEnd = sharehandles_->end();
1469
2/2
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 156 times.
288 for (; is != isEnd; ++is) {
1470 132 curl_share_cleanup((*is)->sharehandle);
1471 132 curl_slist_free_all((*is)->clist);
1472
1/2
✓ Branch 1 taken 132 times.
✗ Branch 2 not taken.
132 delete *is;
1473 }
1474 156 pool_handles_idle_->clear();
1475 156 curl_sharehandles_->clear();
1476 156 sharehandles_->clear();
1477
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete active_requests_;
1478
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete pool_handles_idle_;
1479
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete pool_handles_inuse_;
1480
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete curl_sharehandles_;
1481
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete sharehandles_;
1482
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete user_agent_;
1483 156 curl_multi_cleanup(curl_multi_);
1484
1485
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete statistics_;
1486
1487
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 delete available_jobs_;
1488
1489 156 curl_global_cleanup();
1490 156 }
1491
1492 /**
1493 * Spawns the I/O worker thread. No way back except ~S3FanoutManager.
1494 */
1495 156 void S3FanoutManager::Spawn() {
1496 156 LogCvmfs(kLogS3Fanout, kLogDebug, "S3FanoutManager spawned");
1497
1498 156 const int retval = pthread_create(&thread_upload_, NULL, MainUpload,
1499 static_cast<void *>(this));
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 assert(retval == 0);
1501
1502 156 atomic_inc32(&multi_threaded_);
1503 156 }
1504
1505 36 const Statistics &S3FanoutManager::GetStatistics() { return *statistics_; }
1506
1507 /**
1508 * Push new job to be uploaded to the S3 cloud storage.
1509 */
1510 7440 void S3FanoutManager::PushNewJob(JobInfo *info) {
1511 7440 available_jobs_->Increment();
1512 7440 WritePipe(pipe_jobs_[1], &info, sizeof(info));
1513 7440 }
1514
1515 /**
1516 * Push completed job to list of completed jobs
1517 */
1518 7596 void S3FanoutManager::PushCompletedJob(JobInfo *info) {
1519 7596 WritePipe(pipe_completed_[1], &info, sizeof(info));
1520 7596 }
1521
1522 /**
1523 * Pop completed job
1524 */
1525 7596 JobInfo *S3FanoutManager::PopCompletedJob() {
1526 JobInfo *info;
1527
1/2
✓ Branch 1 taken 7596 times.
✗ Branch 2 not taken.
7596 ReadPipe(pipe_completed_[0], &info, sizeof(info));
1528 7596 return info;
1529 }
1530
1531 //------------------------------------------------------------------------------
1532
1533
1534 ✗ string Statistics::Print() const {
1535 ✗ return "Transferred Bytes: " + StringifyInt(uint64_t(transferred_bytes))
1536 ✗ + "\n" + "Transfer duration: " + StringifyInt(uint64_t(transfer_time))
1537 ✗ + " s\n" + "Number of requests: " + StringifyInt(num_requests) + "\n"
1538 ✗ + "Number of retries: " + StringifyInt(num_retries) + "\n";
1539 }
1540
1541 } // namespace s3fanout
1542