GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2025-11-23 02:35:30
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 384600 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384600 times.
384600 assert(msg_typed_ != NULL);
35
1/2
✓ Branch 0 taken 384600 times.
✗ Branch 1 not taken.
384600 if (!is_wrapped_)
36 384600 WrapMsg();
37 384792 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 481872 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 482112 times.
481872 assert(msg_rpc_.IsInitialized());
47
2/2
✓ Branch 0 taken 409610 times.
✓ Branch 1 taken 72502 times.
482112 if (msg_typed_ == NULL)
48 409610 UnwrapMsg();
49 481392 return msg_typed_;
50 }
51
52
53 482064 CacheTransport::Frame::Frame()
54 482112 : owns_msg_typed_(false)
55 482112 , msg_typed_(NULL)
56 482112 , attachment_(NULL)
57 482112 , att_size_(0)
58 482112 , is_wrapped_(false)
59 482064 , is_msg_out_of_band_(false) { }
60
61
62 384840 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
63 384792 : owns_msg_typed_(false)
64 384792 , msg_typed_(m)
65 384792 , attachment_(NULL)
66 384792 , att_size_(0)
67 384792 , is_wrapped_(false)
68 384840 , is_msg_out_of_band_(false) { }
69
70
71 866904 CacheTransport::Frame::~Frame() { Reset(0); }
72
73
74 72502 bool CacheTransport::Frame::IsMsgOutOfBand() {
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72502 times.
72502 assert(msg_rpc_.IsInitialized());
76
1/2
✓ Branch 0 taken 72502 times.
✗ Branch 1 not taken.
72502 if (msg_typed_ == NULL)
77 72502 UnwrapMsg();
78 72502 return is_msg_out_of_band_;
79 }
80
81
82 97344 void CacheTransport::Frame::MergeFrom(const Frame &other) {
83 97344 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
84 97344 owns_msg_typed_ = true;
85
2/2
✓ Branch 0 taken 86400 times.
✓ Branch 1 taken 10944 times.
97344 if (other.att_size_ > 0) {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86400 times.
86400 assert(att_size_ >= other.att_size_);
87 86400 memcpy(attachment_, other.attachment_, other.att_size_);
88 86400 att_size_ = other.att_size_;
89 }
90 97344 }
91
92
93 384720 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
94 384720 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384768 times.
384768 if (!retval)
96 return false;
97
98 // Cleanup typed message when Frame leaves scope
99 384768 owns_msg_typed_ = true;
100 384768 return true;
101 }
102
103
104 866952 void CacheTransport::Frame::Release() {
105
2/2
✓ Branch 0 taken 482064 times.
✓ Branch 1 taken 384888 times.
866952 if (owns_msg_typed_)
106 482064 return;
107
108 384888 msg_rpc_.release_msg_refcount_req();
109 384840 msg_rpc_.release_msg_refcount_reply();
110 384840 msg_rpc_.release_msg_read_req();
111 384840 msg_rpc_.release_msg_read_reply();
112 384840 msg_rpc_.release_msg_object_info_req();
113 384840 msg_rpc_.release_msg_object_info_reply();
114 384840 msg_rpc_.release_msg_store_req();
115 384840 msg_rpc_.release_msg_store_abort_req();
116 384840 msg_rpc_.release_msg_store_reply();
117 384840 msg_rpc_.release_msg_handshake();
118 384840 msg_rpc_.release_msg_handshake_ack();
119 384792 msg_rpc_.release_msg_quit();
120 384840 msg_rpc_.release_msg_ioctl();
121 384840 msg_rpc_.release_msg_info_req();
122 384792 msg_rpc_.release_msg_info_reply();
123 384840 msg_rpc_.release_msg_shrink_req();
124 384744 msg_rpc_.release_msg_shrink_reply();
125 384744 msg_rpc_.release_msg_list_req();
126 384696 msg_rpc_.release_msg_list_reply();
127 384696 msg_rpc_.release_msg_detach();
128 384696 msg_rpc_.release_msg_breadcrumb_store_req();
129 384696 msg_rpc_.release_msg_breadcrumb_load_req();
130 384744 msg_rpc_.release_msg_breadcrumb_reply();
131 }
132
133
134 866952 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
135 866952 msg_typed_ = NULL;
136 866952 att_size_ = original_att_size;
137 866952 is_wrapped_ = false;
138 866952 is_msg_out_of_band_ = false;
139 866952 Release();
140 866760 msg_rpc_.Clear();
141 866952 owns_msg_typed_ = false;
142 866952 }
143
144
145 384648 void CacheTransport::Frame::WrapMsg() {
146
2/2
✓ Branch 3 taken 890 times.
✓ Branch 4 taken 383902 times.
384648 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
147 890 msg_rpc_.set_allocated_msg_handshake(
148 890 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
149
2/2
✓ Branch 3 taken 864 times.
✓ Branch 4 taken 382942 times.
383902 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
150 864 msg_rpc_.set_allocated_msg_handshake_ack(
151 864 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
152
2/2
✓ Branch 3 taken 888 times.
✓ Branch 4 taken 382198 times.
382942 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
153 888 msg_rpc_.set_allocated_msg_quit(
154 888 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
155
2/2
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 382006 times.
382198 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
156 192 msg_rpc_.set_allocated_msg_ioctl(
157 192 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
158
2/2
✓ Branch 3 taken 29576 times.
✓ Branch 4 taken 352334 times.
382006 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
159 29576 msg_rpc_.set_allocated_msg_refcount_req(
160 29576 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
161
2/2
✓ Branch 3 taken 27072 times.
✓ Branch 4 taken 325358 times.
352334 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
162 27072 msg_rpc_.set_allocated_msg_refcount_reply(
163 27072 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
164
2/2
✓ Branch 3 taken 792 times.
✓ Branch 4 taken 324566 times.
325358 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
165 792 msg_rpc_.set_allocated_msg_object_info_req(
166 792 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
167
2/2
✓ Branch 3 taken 768 times.
✓ Branch 4 taken 323750 times.
324566 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
168 768 msg_rpc_.set_allocated_msg_object_info_reply(
169 768 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
170
2/2
✓ Branch 3 taken 106826 times.
✓ Branch 4 taken 216972 times.
323750 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
171 106826 msg_rpc_.set_allocated_msg_read_req(
172 106826 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
173
2/2
✓ Branch 3 taken 106128 times.
✓ Branch 4 taken 110844 times.
216972 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
174 106128 msg_rpc_.set_allocated_msg_read_reply(
175 106128 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
176
2/2
✓ Branch 3 taken 31598 times.
✓ Branch 4 taken 79246 times.
110844 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
177 31598 msg_rpc_.set_allocated_msg_store_req(
178 31598 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
179
2/2
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 79198 times.
79246 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
180 48 msg_rpc_.set_allocated_msg_store_abort_req(
181 48 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
182
2/2
✓ Branch 3 taken 29232 times.
✓ Branch 4 taken 49966 times.
79198 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
183 29232 msg_rpc_.set_allocated_msg_store_reply(
184 29232 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
185
2/2
✓ Branch 3 taken 216 times.
✓ Branch 4 taken 49750 times.
49966 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
186 216 msg_rpc_.set_allocated_msg_info_req(
187 216 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
188
2/2
✓ Branch 3 taken 192 times.
✓ Branch 4 taken 49558 times.
49750 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
189 192 msg_rpc_.set_allocated_msg_info_reply(
190 192 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
191
2/2
✓ Branch 3 taken 104 times.
✓ Branch 4 taken 49454 times.
49558 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
192 104 msg_rpc_.set_allocated_msg_shrink_req(
193 104 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
194
2/2
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 49358 times.
49454 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
195 96 msg_rpc_.set_allocated_msg_shrink_reply(
196 96 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
197
2/2
✓ Branch 3 taken 488 times.
✓ Branch 4 taken 48870 times.
49358 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
198 488 msg_rpc_.set_allocated_msg_list_req(
199 488 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
200
2/2
✓ Branch 3 taken 480 times.
✓ Branch 4 taken 48390 times.
48870 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
201 480 msg_rpc_.set_allocated_msg_list_reply(
202 480 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
203
2/2
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 48340 times.
48390 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
204 50 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
205 50 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
206
2/2
✓ Branch 3 taken 100 times.
✓ Branch 4 taken 48240 times.
48340 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
207 100 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
208 100 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
209
2/2
✓ Branch 3 taken 144 times.
✓ Branch 4 taken 48096 times.
48240 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
210 144 msg_rpc_.set_allocated_msg_breadcrumb_reply(
211 144 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
212
1/2
✓ Branch 3 taken 48096 times.
✗ Branch 4 not taken.
48096 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
213 48096 msg_rpc_.set_allocated_msg_detach(
214 48096 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
215 48096 is_msg_out_of_band_ = true;
216 } else {
217 // Unexpected message type, should never happen
218 PANIC(NULL);
219 }
220 384840 is_wrapped_ = true;
221 384840 }
222
223
224 482112 void CacheTransport::Frame::UnwrapMsg() {
225
2/2
✓ Branch 1 taken 864 times.
✓ Branch 2 taken 481200 times.
482112 if (msg_rpc_.has_msg_handshake()) {
226 864 msg_typed_ = msg_rpc_.mutable_msg_handshake();
227
2/2
✓ Branch 1 taken 890 times.
✓ Branch 2 taken 480214 times.
481200 } else if (msg_rpc_.has_msg_handshake_ack()) {
228 890 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
229
2/2
✓ Branch 1 taken 864 times.
✓ Branch 2 taken 479350 times.
480214 } else if (msg_rpc_.has_msg_quit()) {
230 864 msg_typed_ = msg_rpc_.mutable_msg_quit();
231
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 479206 times.
479350 } else if (msg_rpc_.has_msg_ioctl()) {
232 192 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
233
2/2
✓ Branch 1 taken 27072 times.
✓ Branch 2 taken 452182 times.
479206 } else if (msg_rpc_.has_msg_refcount_req()) {
234 27072 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
235
2/2
✓ Branch 1 taken 30488 times.
✓ Branch 2 taken 421646 times.
452182 } else if (msg_rpc_.has_msg_refcount_reply()) {
236 30488 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
237
2/2
✓ Branch 1 taken 768 times.
✓ Branch 2 taken 420782 times.
421646 } else if (msg_rpc_.has_msg_object_info_req()) {
238 768 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
239
2/2
✓ Branch 1 taken 1224 times.
✓ Branch 2 taken 419606 times.
420782 } else if (msg_rpc_.has_msg_object_info_reply()) {
240 1224 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
241
2/2
✓ Branch 1 taken 106128 times.
✓ Branch 2 taken 313478 times.
419606 } else if (msg_rpc_.has_msg_read_req()) {
242 106128 msg_typed_ = msg_rpc_.mutable_msg_read_req();
243
2/2
✓ Branch 1 taken 193226 times.
✓ Branch 2 taken 120396 times.
313478 } else if (msg_rpc_.has_msg_read_reply()) {
244 193226 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
245
2/2
✓ Branch 1 taken 29184 times.
✓ Branch 2 taken 91212 times.
120396 } else if (msg_rpc_.has_msg_store_req()) {
246 29184 msg_typed_ = msg_rpc_.mutable_msg_store_req();
247
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 91164 times.
91212 } else if (msg_rpc_.has_msg_store_abort_req()) {
248 48 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
249
2/2
✓ Branch 1 taken 41246 times.
✓ Branch 2 taken 49918 times.
91164 } else if (msg_rpc_.has_msg_store_reply()) {
250 41246 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
251
2/2
✓ Branch 1 taken 192 times.
✓ Branch 2 taken 49726 times.
49918 } else if (msg_rpc_.has_msg_info_req()) {
252 192 msg_typed_ = msg_rpc_.mutable_msg_info_req();
253
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 49510 times.
49726 } else if (msg_rpc_.has_msg_info_reply()) {
254 216 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
255
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 49414 times.
49510 } else if (msg_rpc_.has_msg_shrink_req()) {
256 96 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
257
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 49310 times.
49414 } else if (msg_rpc_.has_msg_shrink_reply()) {
258 104 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
259
2/2
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 48830 times.
49310 } else if (msg_rpc_.has_msg_list_req()) {
260 480 msg_typed_ = msg_rpc_.mutable_msg_list_req();
261
2/2
✓ Branch 1 taken 488 times.
✓ Branch 2 taken 48342 times.
48830 } else if (msg_rpc_.has_msg_list_reply()) {
262 488 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
263
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 48294 times.
48342 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
264 48 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
265
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 48198 times.
48294 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
266 96 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
267
2/2
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 48048 times.
48198 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
268 150 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
269
1/2
✓ Branch 1 taken 48048 times.
✗ Branch 2 not taken.
48048 } else if (msg_rpc_.has_msg_detach()) {
270 48048 msg_typed_ = msg_rpc_.mutable_msg_detach();
271 48048 is_msg_out_of_band_ = true;
272 } else {
273 // Unexpected message type, should never happen
274 PANIC(NULL);
275 }
276 482064 }
277
278
279 //------------------------------------------------------------------------------
280
281
282 890 CacheTransport::CacheTransport(int fd_connection)
283 890 : fd_connection_(fd_connection), flags_(0) {
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 890 times.
890 assert(fd_connection_ >= 0);
285 890 }
286
287
288 214128 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
289 214128 : fd_connection_(fd_connection), flags_(flags) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214128 times.
214128 assert(fd_connection_ >= 0);
291 214128 }
292
293
294 9663153 void CacheTransport::FillMsgHash(const shash::Any &hash,
295 cvmfs::MsgHash *msg_hash) {
296
3/4
✓ Branch 0 taken 9663031 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
9663153 switch (hash.algorithm) {
297 9663031 case shash::kSha1:
298 9663031 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
299 9663031 break;
300 12 case shash::kRmd160:
301 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
302 12 break;
303 110 case shash::kShake128:
304 110 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
305 110 break;
306 default:
307 PANIC(NULL);
308 }
309 9663153 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
310 9663201 }
311
312
313 31590 void CacheTransport::FillObjectType(int object_flags,
314 cvmfs::EnumObjectType *wire_type) {
315 31590 *wire_type = cvmfs::OBJECT_REGULAR;
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31590 times.
31590 if (object_flags & CacheManager::kLabelCatalog)
317 *wire_type = cvmfs::OBJECT_CATALOG;
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31590 times.
31590 if (object_flags & CacheManager::kLabelVolatile)
319 *wire_type = cvmfs::OBJECT_VOLATILE;
320 31590 }
321
322
323 163250 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
324 shash::Any *hash) {
325
2/4
✓ Branch 1 taken 163152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 98 times.
✗ Branch 4 not taken.
163250 switch (msg_hash.algorithm()) {
326 163152 case cvmfs::HASH_SHA1:
327 163152 hash->algorithm = shash::kSha1;
328 163152 break;
329 case cvmfs::HASH_RIPEMD160:
330 hash->algorithm = shash::kRmd160;
331 break;
332 98 case cvmfs::HASH_SHAKE128:
333 98 hash->algorithm = shash::kShake128;
334 98 break;
335 default:
336 return false;
337 }
338 163250 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
339
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 163250 times.
163250 if (msg_hash.digest().length() != digest_size)
340 return false;
341 163250 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
342 163250 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 384816 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
365 uint32_t size;
366 bool has_attachment;
367
1/2
✓ Branch 1 taken 384816 times.
✗ Branch 2 not taken.
384816 bool retval = RecvHeader(&size, &has_attachment);
368
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 384768 times.
384816 if (!retval)
369 48 return false;
370
371 void *buffer;
372
2/2
✓ Branch 0 taken 249169 times.
✓ Branch 1 taken 135599 times.
384768 if (size <= kMaxStackAlloc)
373 249169 buffer = alloca(size);
374 else
375 135599 buffer = smalloc(size);
376
1/2
✓ Branch 1 taken 384768 times.
✗ Branch 2 not taken.
384768 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
377
2/4
✓ Branch 0 taken 384768 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 384768 times.
384768 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
378 if (size > kMaxStackAlloc) {
379 free(buffer);
380 }
381 return false;
382 }
383
384 384768 uint32_t msg_size = size;
385
2/2
✓ Branch 0 taken 135908 times.
✓ Branch 1 taken 248860 times.
384768 if (has_attachment) {
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135908 times.
135908 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 135908 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
395 135908 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135908 times.
135908 if ((msg_size + kInnerHeaderSize) > size) {
397 if (size > kMaxStackAlloc) {
398 free(buffer);
399 }
400 return false;
401 }
402 }
403
404 384768 void *ptr_msg = has_attachment
405
2/2
✓ Branch 0 taken 135908 times.
✓ Branch 1 taken 248860 times.
384768 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
406 : buffer;
407
1/2
✓ Branch 1 taken 384768 times.
✗ Branch 2 not taken.
384768 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384768 times.
384768 if (!retval) {
409 if (size > kMaxStackAlloc) {
410 free(buffer);
411 }
412 return false;
413 }
414
415
2/2
✓ Branch 0 taken 135908 times.
✓ Branch 1 taken 248860 times.
384768 if (has_attachment) {
416 135908 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 135908 times.
135908 if (frame->att_size() < attachment_size) {
418 if (size > kMaxStackAlloc) {
419 free(buffer);
420 }
421 return false;
422 }
423 135908 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
424 135908 + msg_size;
425 135908 memcpy(frame->attachment(), ptr_attachment, attachment_size);
426 135908 frame->set_att_size(attachment_size);
427 } else {
428 248860 frame->set_att_size(0);
429 }
430
2/2
✓ Branch 0 taken 135599 times.
✓ Branch 1 taken 249169 times.
384768 if (size > kMaxStackAlloc) {
431 135599 free(buffer);
432 }
433 384768 return true;
434 }
435
436
437 384816 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
438 unsigned char header[kHeaderSize];
439
1/2
✓ Branch 1 taken 384816 times.
✗ Branch 2 not taken.
384816 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
440
3/4
✓ Branch 0 taken 384816 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 384768 times.
384816 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
441 48 return false;
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384768 times.
384768 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
443 return false;
444 384768 *has_attachment = header[0] & kFlagHasAttachment;
445 384768 *size = header[1] + (header[2] << 8) + (header[3] << 16);
446
2/4
✓ Branch 0 taken 384768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 384768 times.
✗ Branch 3 not taken.
384768 return (*size > 0) && (*size <= kMaxMsgSize);
447 }
448
449
450 384648 void CacheTransport::SendData(void *message,
451 uint32_t msg_size,
452 void *attachment,
453 uint32_t att_size) {
454 769296 const uint32_t total_size = msg_size + att_size
455
2/2
✓ Branch 0 taken 137628 times.
✓ Branch 1 taken 247020 times.
384648 + ((att_size > 0) ? kInnerHeaderSize : 0);
456
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384648 times.
384648 assert(total_size > 0);
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384648 times.
384648 assert(total_size <= kMaxMsgSize);
459
1/2
✓ Branch 1 taken 379104 times.
✗ Branch 2 not taken.
378912 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 247212 times.
✓ Branch 1 taken 137628 times.
384840 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
464 384840 header[1] = (total_size & 0x000000FF);
465 384840 header[2] = (total_size & 0x0000FF00) >> 8;
466 384840 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 384840 iov[0].iov_base = header;
473 384840 iov[0].iov_len = kHeaderSize;
474
475
2/2
✓ Branch 0 taken 137628 times.
✓ Branch 1 taken 247212 times.
384840 if (att_size > 0) {
476 137628 inner_header[0] = (msg_size & 0x000000FF);
477 137628 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
478 137628 iov[1].iov_base = inner_header;
479 137628 iov[1].iov_len = kInnerHeaderSize;
480 137628 iov[2].iov_base = message;
481 137628 iov[2].iov_len = msg_size;
482 137628 iov[3].iov_base = attachment;
483 137628 iov[3].iov_len = att_size;
484 } else {
485 247212 iov[1].iov_base = message;
486 247212 iov[1].iov_len = msg_size;
487 }
488
2/2
✓ Branch 0 taken 48096 times.
✓ Branch 1 taken 336744 times.
384840 if (flags_ & kFlagSendNonBlocking) {
489
2/4
✓ Branch 0 taken 48096 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 48096 times.
✗ Branch 4 not taken.
48096 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
490 48096 return;
491 }
492
3/4
✓ Branch 0 taken 199116 times.
✓ Branch 1 taken 137628 times.
✓ Branch 3 taken 336696 times.
✗ Branch 4 not taken.
336744 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
493
494
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 336696 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
336696 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
495 PANIC(kLogSyslogErr | kLogDebug,
496 "failed to write to external cache transport (%d), aborting", errno);
497 }
498 }
499
500 48096 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48096 times.
48096 assert(iovcnt > 0);
502 48096 unsigned total_size = 0;
503
2/2
✓ Branch 0 taken 96192 times.
✓ Branch 1 taken 48096 times.
144288 for (unsigned i = 0; i < iovcnt; ++i)
504 96192 total_size += iov[i].iov_len;
505 48096 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
506
507 48096 unsigned pos = 0;
508
2/2
✓ Branch 0 taken 96192 times.
✓ Branch 1 taken 48096 times.
144288 for (unsigned i = 0; i < iovcnt; ++i) {
509 96192 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
510 96192 pos += iov[i].iov_len;
511 }
512
513 48096 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48096 times.
48096 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 48096 }
523
524
525 384792 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
526 384792 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
527 384792 const int32_t size = msg_rpc->ByteSize();
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384648 times.
384648 assert(size > 0);
529 #ifdef __APPLE__
530 void *buffer = smalloc(size);
531 #else
532 384648 void *buffer = alloca(size);
533 #endif
534 384648 const bool retval = msg_rpc->SerializeToArray(buffer, size);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 384696 times.
384696 assert(retval);
536 384696 SendData(buffer, size, frame->attachment(), frame->att_size());
537 #ifdef __APPLE__
538 free(buffer);
539 #endif
540 384744 }
541