GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-02-15 02:35:50
Exec Total Coverage
Lines: 326 372 87.6%
Branches: 166 228 72.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "cache_transport.h"
6
7 #include <alloca.h>
8 #include <errno.h>
9 #include <sys/socket.h>
10
11 #include <cassert>
12 #include <cstdlib>
13 #include <cstring>
14
15 #include "crypto/hash.h"
16 #include "util/exception.h"
17 #include "util/logging.h"
18 #include "util/posix.h"
19 #include "util/smalloc.h"
20
21 // TODO(jblomer): Check for possible starvation of plugin by dying clients
22 // (blocking read). Probably only relevant for TCP sockets.
23
24 using namespace std; // NOLINT
25
26 const char
27 *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__";
28
29 /**
30 * Called on the sender side to wrap a message into a MsgRpc message for wire
31 * transfer.
32 */
33 392737 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392737 times.
392737 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 392737 times.
✗ Branch 1 not taken.
392737 if (!is_wrapped_)
36 392737 WrapMsg();
37 392639 return &msg_rpc_;
38 }
39
40
41 /**
42 * Called on the receiving end of an RPC to extract the actual message from the
43 * MsgRpc.
44 */
45 492036 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 492036 times.
492036 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 418143 times.
✓ Branch 1 taken 73893 times.
492036 if (msg_typed_ == NULL)
48 418143 UnwrapMsg();
49 491644 return msg_typed_;
50 }
51
52
53 491987 CacheTransport::Frame::Frame()
54 491987 : owns_msg_typed_(false)
55 491987 , msg_typed_(NULL)
56 491987 , attachment_(NULL)
57 491987 , att_size_(0)
58 491987 , is_wrapped_(false)
59 491987 , is_msg_out_of_band_(false) { }
60
61
62 392737 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 392737 : owns_msg_typed_(false)
64 392737 , msg_typed_(m)
65 392737 , attachment_(NULL)
66 392737 , att_size_(0)
67 392737 , is_wrapped_(false)
68 392737 , is_msg_out_of_band_(false) { }
69
70
71 884528 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 73893 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73893 times.
73893 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 73893 times.
✗ Branch 1 not taken.
73893 if (msg_typed_ == NULL)
77 73893 UnwrapMsg();
78 73893 return is_msg_out_of_band_;
79 }
80
81
82 99372 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 99372 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 99372 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 88200 times.
✓ Branch 1 taken 11172 times.
99372 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88200 times.
88200 assert(att_size_ >= other.att_size_);
87 88200 memcpy(attachment_, other.attachment_, other.att_size_);
88 88200 att_size_ = other.att_size_;
89 }
90 99372 }
91
92
93 392664 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 392664 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392517 times.
392517 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 392517 owns_msg_typed_ = true;
100 392517 return true;
101 }
102
103
104 884626 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 491938 times.
✓ Branch 1 taken 392688 times.
884626 if (owns_msg_typed_)
106 491938 return;
107
108 392688 msg_rpc_.release_msg_refcount_req();
109 392786 msg_rpc_.release_msg_refcount_reply();
110 392737 msg_rpc_.release_msg_read_req();
111 392688 msg_rpc_.release_msg_read_reply();
112 392688 msg_rpc_.release_msg_object_info_req();
113 392737 msg_rpc_.release_msg_object_info_reply();
114 392737 msg_rpc_.release_msg_store_req();
115 392737 msg_rpc_.release_msg_store_abort_req();
116 392786 msg_rpc_.release_msg_store_reply();
117 392786 msg_rpc_.release_msg_handshake();
118 392786 msg_rpc_.release_msg_handshake_ack();
119 392786 msg_rpc_.release_msg_quit();
120 392786 msg_rpc_.release_msg_ioctl();
121 392786 msg_rpc_.release_msg_info_req();
122 392786 msg_rpc_.release_msg_info_reply();
123 392786 msg_rpc_.release_msg_shrink_req();
124 392786 msg_rpc_.release_msg_shrink_reply();
125 392786 msg_rpc_.release_msg_list_req();
126 392786 msg_rpc_.release_msg_list_reply();
127 392737 msg_rpc_.release_msg_detach();
128 392639 msg_rpc_.release_msg_breadcrumb_store_req();
129 392590 msg_rpc_.release_msg_breadcrumb_load_req();
130 392590 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 884577 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 884577 msg_typed_ = NULL;
136 884577 att_size_ = original_att_size;
137 884577 is_wrapped_ = false;
138 884577 is_msg_out_of_band_ = false;
139 884577 Release();
140 884479 msg_rpc_.Clear();
141 884528 owns_msg_typed_ = false;
142 884528 }
143
144
145 392737 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 908 times.
✓ Branch 4 taken 391633 times.
392737 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 908 msg_rpc_.set_allocated_msg_handshake(
148 908 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 882 times.
✓ Branch 4 taken 390800 times.
391633 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 882 msg_rpc_.set_allocated_msg_handshake_ack(
151 882 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 906 times.
✓ Branch 4 taken 390041 times.
390800 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 906 msg_rpc_.set_allocated_msg_quit(
154 906 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 196 times.
✓ Branch 4 taken 389845 times.
390041 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 196 msg_rpc_.set_allocated_msg_ioctl(
157 196 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 30140 times.
✓ Branch 4 taken 359509 times.
389845 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 30140 msg_rpc_.set_allocated_msg_refcount_req(
160 30140 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 27636 times.
✓ Branch 4 taken 332069 times.
359509 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 27636 msg_rpc_.set_allocated_msg_refcount_reply(
163 27636 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 808 times.
✓ Branch 4 taken 331261 times.
332069 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 808 msg_rpc_.set_allocated_msg_object_info_req(
166 808 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 784 times.
✓ Branch 4 taken 330477 times.
331261 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 784 msg_rpc_.set_allocated_msg_object_info_reply(
169 784 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 109036 times.
✓ Branch 4 taken 221441 times.
330477 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 109036 msg_rpc_.set_allocated_msg_read_req(
172 109036 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 108339 times.
✓ Branch 4 taken 113102 times.
221441 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 108339 msg_rpc_.set_allocated_msg_read_reply(
175 108339 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 32206 times.
✓ Branch 4 taken 80896 times.
113102 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 32206 msg_rpc_.set_allocated_msg_store_req(
178 32206 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 80847 times.
80896 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 49 msg_rpc_.set_allocated_msg_store_abort_req(
181 49 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 29841 times.
✓ Branch 4 taken 51006 times.
80847 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 29841 msg_rpc_.set_allocated_msg_store_reply(
184 29841 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 220 times.
✓ Branch 4 taken 50786 times.
51006 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 220 msg_rpc_.set_allocated_msg_info_req(
187 220 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 196 times.
✓ Branch 4 taken 50590 times.
50786 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 196 msg_rpc_.set_allocated_msg_info_reply(
190 196 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 106 times.
✓ Branch 4 taken 50484 times.
50590 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 106 msg_rpc_.set_allocated_msg_shrink_req(
193 106 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 98 times.
✓ Branch 4 taken 50386 times.
50484 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 98 msg_rpc_.set_allocated_msg_shrink_reply(
196 98 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 498 times.
✓ Branch 4 taken 49888 times.
50386 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 498 msg_rpc_.set_allocated_msg_list_req(
199 498 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 490 times.
✓ Branch 4 taken 49398 times.
49888 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 490 msg_rpc_.set_allocated_msg_list_reply(
202 490 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 49347 times.
49398 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 51 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 51 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 49245 times.
49347 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 102 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 102 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 147 times.
✓ Branch 4 taken 49098 times.
49245 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 147 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 147 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 49098 times.
✗ Branch 4 not taken.
49098 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 49098 msg_rpc_.set_allocated_msg_detach(
214 49098 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 49098 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 392737 is_wrapped_ = true;
221 392737 }
222
223
224 492036 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 882 times.
✓ Branch 2 taken 491056 times.
492036 if (msg_rpc_.has_msg_handshake()) {
226 882 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 908 times.
✓ Branch 2 taken 490099 times.
491056 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 908 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 882 times.
✓ Branch 2 taken 489217 times.
490099 } else if (msg_rpc_.has_msg_quit()) {
230 882 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 489070 times.
489217 } else if (msg_rpc_.has_msg_ioctl()) {
232 196 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 27636 times.
✓ Branch 2 taken 461189 times.
489070 } else if (msg_rpc_.has_msg_refcount_req()) {
234 27636 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 31022 times.
✓ Branch 2 taken 430069 times.
461189 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 31022 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 784 times.
✓ Branch 2 taken 429432 times.
430069 } else if (msg_rpc_.has_msg_object_info_req()) {
238 784 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 1249 times.
✓ Branch 2 taken 428330 times.
429432 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 1249 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 108339 times.
✓ Branch 2 taken 319942 times.
428330 } else if (msg_rpc_.has_msg_read_req()) {
242 108339 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 197236 times.
✓ Branch 2 taken 122853 times.
319942 } else if (msg_rpc_.has_msg_read_reply()) {
244 197236 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 29792 times.
✓ Branch 2 taken 93061 times.
122853 } else if (msg_rpc_.has_msg_store_req()) {
246 29792 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 93012 times.
93061 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 49 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 42055 times.
✓ Branch 2 taken 50957 times.
93012 } else if (msg_rpc_.has_msg_store_reply()) {
250 42055 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 50761 times.
50957 } else if (msg_rpc_.has_msg_info_req()) {
252 196 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 220 times.
✓ Branch 2 taken 50541 times.
50761 } else if (msg_rpc_.has_msg_info_reply()) {
254 220 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 50443 times.
50541 } else if (msg_rpc_.has_msg_shrink_req()) {
256 98 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 50337 times.
50443 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 106 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 490 times.
✓ Branch 2 taken 49847 times.
50337 } else if (msg_rpc_.has_msg_list_req()) {
260 490 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 498 times.
✓ Branch 2 taken 49349 times.
49847 } else if (msg_rpc_.has_msg_list_reply()) {
262 498 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49300 times.
49349 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 49 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 49202 times.
49300 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 98 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 49049 times.
49202 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 153 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 49049 times.
✗ Branch 2 not taken.
49049 } else if (msg_rpc_.has_msg_detach()) {
270 49049 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 49049 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 492036 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 908 CacheTransport::CacheTransport(int fd_connection)
283 908 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
908 assert(fd_connection_ >= 0);
285 908 }
286
287
288 218589 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 218589 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218589 times.
218589 assert(fd_connection_ >= 0);
291 218589 }
292
293
294 9864406 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 9864282 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
9864406 switch (hash.algorithm) {
297 9864282 case shash::kSha1:
298 9864282 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 9864282 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 112 case shash::kShake128:
304 112 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 112 break;
306 default:
307 PANIC(NULL);
308 }
309 9864406 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 9864406 }
311
312
313 32198 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 32198 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32198 times.
32198 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32198 times.
32198 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 32198 }
321
322
323 166651 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 166551 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 100 times.
✗ Branch 4 not taken.
166651 switch (msg_hash.algorithm()) {
326 166551 case cvmfs::HASH_SHA1:
327 166551 hash->algorithm = shash::kSha1;
328 166551 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 100 case cvmfs::HASH_SHAKE128:
333 100 hash->algorithm = shash::kShake128;
334 100 break;
335 default:
336 return false;
337 }
338 166651 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 166651 times.
166651 if (msg_hash.digest().length() != digest_size)
340 return false;
341 166651 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 166651 return true;
343 }
344
345
346 bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type,
347 int *object_flags) {
348 *object_flags = 0;
349 switch (wire_type) {
350 case cvmfs::OBJECT_REGULAR:
351 return true;
352 case cvmfs::OBJECT_CATALOG:
353 *object_flags |= CacheManager::kLabelCatalog;
354 return true;
355 case cvmfs::OBJECT_VOLATILE:
356 *object_flags |= CacheManager::kLabelVolatile;
357 return true;
358 default:
359 return false;
360 }
361 }
362
363
364 392664 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 392713 times.
✗ Branch 2 not taken.
392664 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 392664 times.
392713 if (!retval)
369 49 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 254247 times.
✓ Branch 1 taken 138417 times.
392664 if (size <= kMaxStackAlloc)
373 254247 buffer = alloca(size);
374 else
375 138417 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 392664 times.
✗ Branch 2 not taken.
392664 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
2/4
✓ Branch 0 taken 392664 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 392664 times.
392664 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 return false;
382 }
383
384 392664 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 138724 times.
✓ Branch 1 taken 253940 times.
392664 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138724 times.
138724 if (size < 2) {
387 // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here
388 // for consistency.
389 if (size > kMaxStackAlloc) {
390 free(buffer);
391 }
392 return false;
393 }
394 138724 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 138724 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138724 times.
138724 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 392664 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 138724 times.
✓ Branch 1 taken 253940 times.
392664 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 392517 times.
✗ Branch 2 not taken.
392664 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392517 times.
392517 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 138724 times.
✓ Branch 1 taken 253793 times.
392517 if (has_attachment) {
416 138724 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 138724 times.
138724 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 138724 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 138724 + msg_size;
425 138724 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 138724 frame->set_att_size(attachment_size);
427 } else {
428 253793 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 138417 times.
✓ Branch 1 taken 254198 times.
392615 if (size > kMaxStackAlloc) {
431 138417 free(buffer);
432 }
433 392615 return true;
434 }
435
436
437 392664 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 392713 times.
✗ Branch 2 not taken.
392664 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 392713 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 392664 times.
392713 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 49 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392664 times.
392664 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 392664 *has_attachment = header[0] & kFlagHasAttachment;
445 392664 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 392664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 392664 times.
✗ Branch 3 not taken.
392664 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 392688 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 785376 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 140445 times.
✓ Branch 1 taken 252243 times.
392688 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392688 times.
392688 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392688 times.
392688 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 387002 times.
✗ Branch 2 not taken.
386953 LogCvmfs(kLogCache, kLogDebug,
460 "sending message of size %u to cache transport", total_size);
461
462 unsigned char header[kHeaderSize];
463
2/2
✓ Branch 0 taken 252292 times.
✓ Branch 1 taken 140445 times.
392737 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 392737 header[1] = (total_size & 0x000000FF);
465 392737 header[2] = (total_size & 0x0000FF00) >> 8;
466 392737 header[3] = (total_size & 0x00FF0000) >> 16;
467 // Only transferred if an attachment is present. Otherwise the overall size
468 // is also the size of the protobuf message.
469 unsigned char inner_header[kInnerHeaderSize];
470
471 struct iovec iov[4];
472 392737 iov[0].iov_base = header;
473 392737 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 140445 times.
✓ Branch 1 taken 252292 times.
392737 if (att_size > 0) {
476 140445 inner_header[0] = (msg_size & 0x000000FF);
477 140445 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 140445 iov[1].iov_base = inner_header;
479 140445 iov[1].iov_len = kInnerHeaderSize;
480 140445 iov[2].iov_base = message;
481 140445 iov[2].iov_len = msg_size;
482 140445 iov[3].iov_base = attachment;
483 140445 iov[3].iov_len = att_size;
484 } else {
485 252292 iov[1].iov_base = message;
486 252292 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 49098 times.
✓ Branch 1 taken 343639 times.
392737 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 49098 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 49098 times.
✗ Branch 4 not taken.
49098 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 49098 return;
491 }
492
3/4
✓ Branch 0 taken 203194 times.
✓ Branch 1 taken 140445 times.
✓ Branch 3 taken 343541 times.
✗ Branch 4 not taken.
343639 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 343541 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
343541 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 49098 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49098 times.
49098 assert(iovcnt > 0);
502 49098 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 98196 times.
✓ Branch 1 taken 49098 times.
147294 for (unsigned i = 0; i < iovcnt; ++i)
504 98196 total_size += iov[i].iov_len;
505 49098 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 49098 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 98196 times.
✓ Branch 1 taken 49098 times.
147294 for (unsigned i = 0; i < iovcnt; ++i) {
509 98196 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 98196 pos += iov[i].iov_len;
511 }
512
513 49098 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49098 times.
49098 if (retval < 0) {
515 assert(errno != EMSGSIZE);
516 if (!(flags_ & kFlagSendIgnoreFailure)) {
517 PANIC(kLogSyslogErr | kLogDebug,
518 "failed to write to external cache transport (%d), aborting",
519 errno);
520 }
521 }
522 49098 }
523
524
525 392737 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 392737 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 392590 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392492 times.
392492 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 392492 void *buffer = alloca(size);
533 #endif
534 392492 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 392688 times.
392688 assert(retval);
536 392688 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 392492 }
541