GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_transport.cc
Date: 2026-06-28 02:36:10
Exec Total Coverage
Lines: 328 372 88.2%
Branches: 168 228 73.7%

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 "cache.h"
16 #include "crypto/hash.h"
17 #include "util/exception.h"
18 #include "util/logging.h"
19 #include "util/posix.h"
20 #include "util/smalloc.h"
21
22 // TODO(jblomer): Check for possible starvation of plugin by dying clients
23 // (blocking read). Probably only relevant for TCP sockets.
24
25 using namespace std; // NOLINT
26
27 const char
28 *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__";
29
30 /**
31 * Called on the sender side to wrap a message into a MsgRpc message for wire
32 * transfer.
33 */
34 353260 cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353260 times.
353260 assert(msg_typed_ != NULL);
36
1/2
✓ Branch 0 taken 353260 times.
✗ Branch 1 not taken.
353260 if (!is_wrapped_)
37 353260 WrapMsg();
38 353260 return &msg_rpc_;
39 }
40
41
42 /**
43 * Called on the receiving end of an RPC to extract the actual message from the
44 * MsgRpc.
45 */
46 442424 google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() {
47
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 442380 times.
442424 assert(msg_rpc_.IsInitialized());
48
2/2
✓ Branch 0 taken 375434 times.
✓ Branch 1 taken 66946 times.
442380 if (msg_typed_ == NULL)
49 375434 UnwrapMsg();
50 442424 return msg_typed_;
51 }
52
53
54 442380 CacheTransport::Frame::Frame()
55 442424 : owns_msg_typed_(false)
56 442424 , msg_typed_(NULL)
57 442424 , attachment_(NULL)
58 442424 , att_size_(0)
59 442424 , is_wrapped_(false)
60 442380 , is_msg_out_of_band_(false) { }
61
62
63 353216 CacheTransport::Frame::Frame(google::protobuf::MessageLite *m)
64 353216 : owns_msg_typed_(false)
65 353216 , msg_typed_(m)
66 353216 , attachment_(NULL)
67 353216 , att_size_(0)
68 353216 , is_wrapped_(false)
69 353216 , is_msg_out_of_band_(false) { }
70
71
72 795596 CacheTransport::Frame::~Frame() { Reset(0); }
73
74
75 66946 bool CacheTransport::Frame::IsMsgOutOfBand() {
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 66946 times.
66946 assert(msg_rpc_.IsInitialized());
77
1/2
✓ Branch 0 taken 66946 times.
✗ Branch 1 not taken.
66946 if (msg_typed_ == NULL)
78 66946 UnwrapMsg();
79 66946 return is_msg_out_of_band_;
80 }
81
82
83 89232 void CacheTransport::Frame::MergeFrom(const Frame &other) {
84 89232 msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_);
85 89232 owns_msg_typed_ = true;
86
2/2
✓ Branch 0 taken 79200 times.
✓ Branch 1 taken 10032 times.
89232 if (other.att_size_ > 0) {
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79200 times.
79200 assert(att_size_ >= other.att_size_);
88 79200 memcpy(attachment_, other.attachment_, other.att_size_);
89 79200 att_size_ = other.att_size_;
90 }
91 89232 }
92
93
94 353148 bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) {
95 353148 const bool retval = msg_rpc_.ParseFromArray(buffer, size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 352664 times.
352664 if (!retval)
97 return false;
98
99 // Cleanup typed message when Frame leaves scope
100 352664 owns_msg_typed_ = true;
101 352664 return true;
102 }
103
104
105 795728 void CacheTransport::Frame::Release() {
106
2/2
✓ Branch 0 taken 442424 times.
✓ Branch 1 taken 353304 times.
795728 if (owns_msg_typed_)
107 442424 return;
108
109 353304 msg_rpc_.release_msg_refcount_req();
110 353304 msg_rpc_.release_msg_refcount_reply();
111 353304 msg_rpc_.release_msg_read_req();
112 353304 msg_rpc_.release_msg_read_reply();
113 353304 msg_rpc_.release_msg_object_info_req();
114 353260 msg_rpc_.release_msg_object_info_reply();
115 353260 msg_rpc_.release_msg_store_req();
116 353304 msg_rpc_.release_msg_store_abort_req();
117 353304 msg_rpc_.release_msg_store_reply();
118 353304 msg_rpc_.release_msg_handshake();
119 353304 msg_rpc_.release_msg_handshake_ack();
120 353304 msg_rpc_.release_msg_quit();
121 353304 msg_rpc_.release_msg_ioctl();
122 353304 msg_rpc_.release_msg_info_req();
123 353304 msg_rpc_.release_msg_info_reply();
124 353304 msg_rpc_.release_msg_shrink_req();
125 353304 msg_rpc_.release_msg_shrink_reply();
126 353260 msg_rpc_.release_msg_list_req();
127 353260 msg_rpc_.release_msg_list_reply();
128 353260 msg_rpc_.release_msg_detach();
129 353304 msg_rpc_.release_msg_breadcrumb_store_req();
130 353304 msg_rpc_.release_msg_breadcrumb_load_req();
131 353304 msg_rpc_.release_msg_breadcrumb_reply();
132 }
133
134
135 795728 void CacheTransport::Frame::Reset(uint32_t original_att_size) {
136 795728 msg_typed_ = NULL;
137 795728 att_size_ = original_att_size;
138 795728 is_wrapped_ = false;
139 795728 is_msg_out_of_band_ = false;
140 795728 Release();
141 795596 msg_rpc_.Clear();
142 795640 owns_msg_typed_ = false;
143 795640 }
144
145
146 353216 void CacheTransport::Frame::WrapMsg() {
147
2/2
✓ Branch 3 taken 818 times.
✓ Branch 4 taken 352442 times.
353216 if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") {
148 818 msg_rpc_.set_allocated_msg_handshake(
149 818 reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_));
150
2/2
✓ Branch 3 taken 792 times.
✓ Branch 4 taken 351650 times.
352442 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") {
151 792 msg_rpc_.set_allocated_msg_handshake_ack(
152 792 reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_));
153
2/2
✓ Branch 3 taken 816 times.
✓ Branch 4 taken 350834 times.
351650 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") {
154 816 msg_rpc_.set_allocated_msg_quit(
155 816 reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_));
156
2/2
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 350658 times.
350834 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") {
157 176 msg_rpc_.set_allocated_msg_ioctl(
158 176 reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_));
159
2/2
✓ Branch 3 taken 27320 times.
✓ Branch 4 taken 323294 times.
350658 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") {
160 27320 msg_rpc_.set_allocated_msg_refcount_req(
161 27320 reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_));
162
2/2
✓ Branch 3 taken 24816 times.
✓ Branch 4 taken 298522 times.
323294 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") {
163 24816 msg_rpc_.set_allocated_msg_refcount_reply(
164 24816 reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_));
165
2/2
✓ Branch 3 taken 728 times.
✓ Branch 4 taken 297794 times.
298522 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") {
166 728 msg_rpc_.set_allocated_msg_object_info_req(
167 728 reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_));
168
2/2
✓ Branch 3 taken 704 times.
✓ Branch 4 taken 297090 times.
297794 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") {
169 704 msg_rpc_.set_allocated_msg_object_info_reply(
170 704 reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_));
171
2/2
✓ Branch 3 taken 97994 times.
✓ Branch 4 taken 199096 times.
297090 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") {
172 97994 msg_rpc_.set_allocated_msg_read_req(
173 97994 reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_));
174
2/2
✓ Branch 3 taken 97284 times.
✓ Branch 4 taken 101812 times.
199096 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") {
175 97284 msg_rpc_.set_allocated_msg_read_reply(
176 97284 reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_));
177
2/2
✓ Branch 3 taken 29166 times.
✓ Branch 4 taken 72646 times.
101812 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") {
178 29166 msg_rpc_.set_allocated_msg_store_req(
179 29166 reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_));
180
2/2
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 72602 times.
72646 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") {
181 44 msg_rpc_.set_allocated_msg_store_abort_req(
182 44 reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_));
183
2/2
✓ Branch 3 taken 26796 times.
✓ Branch 4 taken 45806 times.
72602 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") {
184 26796 msg_rpc_.set_allocated_msg_store_reply(
185 26796 reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_));
186
2/2
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 45606 times.
45806 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") {
187 200 msg_rpc_.set_allocated_msg_info_req(
188 200 reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_));
189
2/2
✓ Branch 3 taken 176 times.
✓ Branch 4 taken 45430 times.
45606 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") {
190 176 msg_rpc_.set_allocated_msg_info_reply(
191 176 reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_));
192
2/2
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 45334 times.
45430 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") {
193 96 msg_rpc_.set_allocated_msg_shrink_req(
194 96 reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_));
195
2/2
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 45246 times.
45334 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") {
196 88 msg_rpc_.set_allocated_msg_shrink_reply(
197 88 reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_));
198
2/2
✓ Branch 3 taken 448 times.
✓ Branch 4 taken 44798 times.
45246 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") {
199 448 msg_rpc_.set_allocated_msg_list_req(
200 448 reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_));
201
2/2
✓ Branch 3 taken 440 times.
✓ Branch 4 taken 44358 times.
44798 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") {
202 440 msg_rpc_.set_allocated_msg_list_reply(
203 440 reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_));
204
2/2
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 44312 times.
44358 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") {
205 46 msg_rpc_.set_allocated_msg_breadcrumb_store_req(
206 46 reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_));
207
2/2
✓ Branch 3 taken 92 times.
✓ Branch 4 taken 44220 times.
44312 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") {
208 92 msg_rpc_.set_allocated_msg_breadcrumb_load_req(
209 92 reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_));
210
2/2
✓ Branch 3 taken 132 times.
✓ Branch 4 taken 44088 times.
44220 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") {
211 132 msg_rpc_.set_allocated_msg_breadcrumb_reply(
212 132 reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_));
213
1/2
✓ Branch 3 taken 44088 times.
✗ Branch 4 not taken.
44088 } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") {
214 44088 msg_rpc_.set_allocated_msg_detach(
215 44088 reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_));
216 44088 is_msg_out_of_band_ = true;
217 } else {
218 // Unexpected message type, should never happen
219 PANIC(NULL);
220 }
221 353260 is_wrapped_ = true;
222 353260 }
223
224
225 442380 void CacheTransport::Frame::UnwrapMsg() {
226
2/2
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 441456 times.
442380 if (msg_rpc_.has_msg_handshake()) {
227 792 msg_typed_ = msg_rpc_.mutable_msg_handshake();
228
2/2
✓ Branch 1 taken 818 times.
✓ Branch 2 taken 440550 times.
441456 } else if (msg_rpc_.has_msg_handshake_ack()) {
229 818 msg_typed_ = msg_rpc_.mutable_msg_handshake_ack();
230
2/2
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 439978 times.
440550 } else if (msg_rpc_.has_msg_quit()) {
231 792 msg_typed_ = msg_rpc_.mutable_msg_quit();
232
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 439626 times.
439978 } else if (msg_rpc_.has_msg_ioctl()) {
233 176 msg_typed_ = msg_rpc_.mutable_msg_ioctl();
234
2/2
✓ Branch 1 taken 24816 times.
✓ Branch 2 taken 414854 times.
439626 } else if (msg_rpc_.has_msg_refcount_req()) {
235 24816 msg_typed_ = msg_rpc_.mutable_msg_refcount_req();
236
2/2
✓ Branch 1 taken 28156 times.
✓ Branch 2 taken 386698 times.
414854 } else if (msg_rpc_.has_msg_refcount_reply()) {
237 28156 msg_typed_ = msg_rpc_.mutable_msg_refcount_reply();
238
2/2
✓ Branch 1 taken 704 times.
✓ Branch 2 taken 385994 times.
386698 } else if (msg_rpc_.has_msg_object_info_req()) {
239 704 msg_typed_ = msg_rpc_.mutable_msg_object_info_req();
240
2/2
✓ Branch 1 taken 1124 times.
✓ Branch 2 taken 385002 times.
385994 } else if (msg_rpc_.has_msg_object_info_reply()) {
241 1124 msg_typed_ = msg_rpc_.mutable_msg_object_info_reply();
242
2/2
✓ Branch 1 taken 97284 times.
✓ Branch 2 taken 287762 times.
385002 } else if (msg_rpc_.has_msg_read_req()) {
243 97284 msg_typed_ = msg_rpc_.mutable_msg_read_req();
244
2/2
✓ Branch 1 taken 177194 times.
✓ Branch 2 taken 110568 times.
287762 } else if (msg_rpc_.has_msg_read_reply()) {
245 177194 msg_typed_ = msg_rpc_.mutable_msg_read_reply();
246
2/2
✓ Branch 1 taken 26752 times.
✓ Branch 2 taken 83816 times.
110568 } else if (msg_rpc_.has_msg_store_req()) {
247 26752 msg_typed_ = msg_rpc_.mutable_msg_store_req();
248
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 83772 times.
83816 } else if (msg_rpc_.has_msg_store_abort_req()) {
249 44 msg_typed_ = msg_rpc_.mutable_msg_store_abort_req();
250
2/2
✓ Branch 1 taken 38010 times.
✓ Branch 2 taken 45762 times.
83772 } else if (msg_rpc_.has_msg_store_reply()) {
251 38010 msg_typed_ = msg_rpc_.mutable_msg_store_reply();
252
2/2
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 45586 times.
45762 } else if (msg_rpc_.has_msg_info_req()) {
253 176 msg_typed_ = msg_rpc_.mutable_msg_info_req();
254
2/2
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 45386 times.
45586 } else if (msg_rpc_.has_msg_info_reply()) {
255 200 msg_typed_ = msg_rpc_.mutable_msg_info_reply();
256
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 45298 times.
45386 } else if (msg_rpc_.has_msg_shrink_req()) {
257 88 msg_typed_ = msg_rpc_.mutable_msg_shrink_req();
258
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 45202 times.
45298 } else if (msg_rpc_.has_msg_shrink_reply()) {
259 96 msg_typed_ = msg_rpc_.mutable_msg_shrink_reply();
260
2/2
✓ Branch 1 taken 440 times.
✓ Branch 2 taken 44762 times.
45202 } else if (msg_rpc_.has_msg_list_req()) {
261 440 msg_typed_ = msg_rpc_.mutable_msg_list_req();
262
2/2
✓ Branch 1 taken 448 times.
✓ Branch 2 taken 44314 times.
44762 } else if (msg_rpc_.has_msg_list_reply()) {
263 448 msg_typed_ = msg_rpc_.mutable_msg_list_reply();
264
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 44270 times.
44314 } else if (msg_rpc_.has_msg_breadcrumb_store_req()) {
265 44 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req();
266
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 44182 times.
44270 } else if (msg_rpc_.has_msg_breadcrumb_load_req()) {
267 88 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req();
268
2/2
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 44044 times.
44182 } else if (msg_rpc_.has_msg_breadcrumb_reply()) {
269 138 msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply();
270
1/2
✓ Branch 1 taken 44044 times.
✗ Branch 2 not taken.
44044 } else if (msg_rpc_.has_msg_detach()) {
271 44044 msg_typed_ = msg_rpc_.mutable_msg_detach();
272 44044 is_msg_out_of_band_ = true;
273 } else {
274 // Unexpected message type, should never happen
275 PANIC(NULL);
276 }
277 442424 }
278
279
280 //------------------------------------------------------------------------------
281
282
283 818 CacheTransport::CacheTransport(int fd_connection)
284 818 : fd_connection_(fd_connection), flags_(0) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 818 times.
818 assert(fd_connection_ >= 0);
286 818 }
287
288
289 196284 CacheTransport::CacheTransport(int fd_connection, uint32_t flags)
290 196284 : fd_connection_(fd_connection), flags_(flags) {
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196284 times.
196284 assert(fd_connection_ >= 0);
292 196284 }
293
294
295 8858376 void CacheTransport::FillMsgHash(const shash::Any &hash,
296 cvmfs::MsgHash *msg_hash) {
297
3/4
✓ Branch 0 taken 8858262 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
8858376 switch (hash.algorithm) {
298 8858262 case shash::kSha1:
299 8858262 msg_hash->set_algorithm(cvmfs::HASH_SHA1);
300 8858262 break;
301 12 case shash::kRmd160:
302 12 msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160);
303 12 break;
304 102 case shash::kShake128:
305 102 msg_hash->set_algorithm(cvmfs::HASH_SHAKE128);
306 102 break;
307 default:
308 PANIC(NULL);
309 }
310 8858376 msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]);
311 8858376 }
312
313
314 29158 void CacheTransport::FillObjectType(int object_flags,
315 cvmfs::EnumObjectType *wire_type) {
316 29158 *wire_type = cvmfs::OBJECT_REGULAR;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29158 times.
29158 if (object_flags & CacheManager::kLabelCatalog)
318 *wire_type = cvmfs::OBJECT_CATALOG;
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29158 times.
29158 if (object_flags & CacheManager::kLabelVolatile)
320 *wire_type = cvmfs::OBJECT_VOLATILE;
321 29158 }
322
323
324 149646 bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash,
325 shash::Any *hash) {
326
2/4
✓ Branch 1 taken 149556 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
✗ Branch 4 not taken.
149646 switch (msg_hash.algorithm()) {
327 149556 case cvmfs::HASH_SHA1:
328 149556 hash->algorithm = shash::kSha1;
329 149556 break;
330 case cvmfs::HASH_RIPEMD160:
331 hash->algorithm = shash::kRmd160;
332 break;
333 90 case cvmfs::HASH_SHAKE128:
334 90 hash->algorithm = shash::kShake128;
335 90 break;
336 default:
337 return false;
338 }
339 149646 const unsigned digest_size = shash::kDigestSizes[hash->algorithm];
340
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 149646 times.
149646 if (msg_hash.digest().length() != digest_size)
341 return false;
342 149646 memcpy(hash->digest, msg_hash.digest().data(), digest_size);
343 149646 return true;
344 }
345
346
347 bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type,
348 int *object_flags) {
349 *object_flags = 0;
350 switch (wire_type) {
351 case cvmfs::OBJECT_REGULAR:
352 return true;
353 case cvmfs::OBJECT_CATALOG:
354 *object_flags |= CacheManager::kLabelCatalog;
355 return true;
356 case cvmfs::OBJECT_VOLATILE:
357 *object_flags |= CacheManager::kLabelVolatile;
358 return true;
359 default:
360 return false;
361 }
362 }
363
364
365 353236 bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) {
366 uint32_t size;
367 bool has_attachment;
368
1/2
✓ Branch 1 taken 353236 times.
✗ Branch 2 not taken.
353236 bool retval = RecvHeader(&size, &has_attachment);
369
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 353192 times.
353236 if (!retval)
370 44 return false;
371
372 void *buffer;
373
2/2
✓ Branch 0 taken 228852 times.
✓ Branch 1 taken 124340 times.
353192 if (size <= kMaxStackAlloc)
374 228852 buffer = alloca(size);
375 else
376 124340 buffer = smalloc(size);
377
1/2
✓ Branch 1 taken 353192 times.
✗ Branch 2 not taken.
353192 const ssize_t nbytes = SafeRead(fd_connection_, buffer, size);
378
3/4
✓ Branch 0 taken 353148 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 353148 times.
353192 if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) {
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (size > kMaxStackAlloc) {
380 free(buffer);
381 }
382 44 return false;
383 }
384
385 353148 uint32_t msg_size = size;
386
2/2
✓ Branch 0 taken 124652 times.
✓ Branch 1 taken 228496 times.
353148 if (has_attachment) {
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124652 times.
124652 if (size < 2) {
388 // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here
389 // for consistency.
390 if (size > kMaxStackAlloc) {
391 free(buffer);
392 }
393 return false;
394 }
395 124652 msg_size = (*reinterpret_cast<unsigned char *>(buffer))
396 124652 + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124652 times.
124652 if ((msg_size + kInnerHeaderSize) > size) {
398 if (size > kMaxStackAlloc) {
399 free(buffer);
400 }
401 return false;
402 }
403 }
404
405 353148 void *ptr_msg = has_attachment
406
2/2
✓ Branch 0 taken 124652 times.
✓ Branch 1 taken 228496 times.
353148 ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize)
407 : buffer;
408
1/2
✓ Branch 1 taken 352708 times.
✗ Branch 2 not taken.
353148 retval = frame->ParseMsgRpc(ptr_msg, msg_size);
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 352708 times.
352708 if (!retval) {
410 if (size > kMaxStackAlloc) {
411 free(buffer);
412 }
413 return false;
414 }
415
416
2/2
✓ Branch 0 taken 124652 times.
✓ Branch 1 taken 228056 times.
352708 if (has_attachment) {
417 124652 const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize);
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124652 times.
124652 if (frame->att_size() < attachment_size) {
419 if (size > kMaxStackAlloc) {
420 free(buffer);
421 }
422 return false;
423 }
424 124652 void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize
425 124652 + msg_size;
426 124652 memcpy(frame->attachment(), ptr_attachment, attachment_size);
427 124652 frame->set_att_size(attachment_size);
428 } else {
429 228056 frame->set_att_size(0);
430 }
431
2/2
✓ Branch 0 taken 124340 times.
✓ Branch 1 taken 228808 times.
353148 if (size > kMaxStackAlloc) {
432 124340 free(buffer);
433 }
434 353148 return true;
435 }
436
437
438 353236 bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) {
439 unsigned char header[kHeaderSize];
440
1/2
✓ Branch 1 taken 353236 times.
✗ Branch 2 not taken.
353236 const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize);
441
3/4
✓ Branch 0 taken 353236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 353192 times.
353236 if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize))
442 44 return false;
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353192 times.
353192 if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion)
444 return false;
445 353192 *has_attachment = header[0] & kFlagHasAttachment;
446 353192 *size = header[1] + (header[2] << 8) + (header[3] << 16);
447
2/4
✓ Branch 0 taken 353192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 353192 times.
✗ Branch 3 not taken.
353192 return (*size > 0) && (*size <= kMaxMsgSize);
448 }
449
450
451 353260 void CacheTransport::SendData(void *message,
452 uint32_t msg_size,
453 void *attachment,
454 uint32_t att_size) {
455 706520 const uint32_t total_size = msg_size + att_size
456
2/2
✓ Branch 0 taken 126360 times.
✓ Branch 1 taken 226900 times.
353260 + ((att_size > 0) ? kInnerHeaderSize : 0);
457
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353260 times.
353260 assert(total_size > 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353260 times.
353260 assert(total_size <= kMaxMsgSize);
460
1/2
✓ Branch 1 taken 347512 times.
✗ Branch 2 not taken.
347512 LogCvmfs(kLogCache, kLogDebug,
461 "sending message of size %u to cache transport", total_size);
462
463 unsigned char header[kHeaderSize];
464
2/2
✓ Branch 0 taken 226900 times.
✓ Branch 1 taken 126360 times.
353260 header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment);
465 353260 header[1] = (total_size & 0x000000FF);
466 353260 header[2] = (total_size & 0x0000FF00) >> 8;
467 353260 header[3] = (total_size & 0x00FF0000) >> 16;
468 // Only transferred if an attachment is present. Otherwise the overall size
469 // is also the size of the protobuf message.
470 unsigned char inner_header[kInnerHeaderSize];
471
472 struct iovec iov[4];
473 353260 iov[0].iov_base = header;
474 353260 iov[0].iov_len = kHeaderSize;
475
476
2/2
✓ Branch 0 taken 126360 times.
✓ Branch 1 taken 226900 times.
353260 if (att_size > 0) {
477 126360 inner_header[0] = (msg_size & 0x000000FF);
478 126360 inner_header[1] = (msg_size & 0x0000FF00) >> 8;
479 126360 iov[1].iov_base = inner_header;
480 126360 iov[1].iov_len = kInnerHeaderSize;
481 126360 iov[2].iov_base = message;
482 126360 iov[2].iov_len = msg_size;
483 126360 iov[3].iov_base = attachment;
484 126360 iov[3].iov_len = att_size;
485 } else {
486 226900 iov[1].iov_base = message;
487 226900 iov[1].iov_len = msg_size;
488 }
489
2/2
✓ Branch 0 taken 44088 times.
✓ Branch 1 taken 309172 times.
353260 if (flags_ & kFlagSendNonBlocking) {
490
2/4
✓ Branch 0 taken 44088 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 44088 times.
✗ Branch 4 not taken.
44088 SendNonBlocking(iov, (att_size == 0) ? 2 : 4);
491 44088 return;
492 }
493
3/4
✓ Branch 0 taken 182812 times.
✓ Branch 1 taken 126360 times.
✓ Branch 3 taken 309172 times.
✗ Branch 4 not taken.
309172 const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4);
494
495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 309172 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
309172 if (!retval && !(flags_ & kFlagSendIgnoreFailure)) {
496 PANIC(kLogSyslogErr | kLogDebug,
497 "failed to write to external cache transport (%d), aborting", errno);
498 }
499 }
500
501 44088 void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44088 times.
44088 assert(iovcnt > 0);
503 44088 unsigned total_size = 0;
504
2/2
✓ Branch 0 taken 88176 times.
✓ Branch 1 taken 44088 times.
132264 for (unsigned i = 0; i < iovcnt; ++i)
505 88176 total_size += iov[i].iov_len;
506 44088 unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size));
507
508 44088 unsigned pos = 0;
509
2/2
✓ Branch 0 taken 88176 times.
✓ Branch 1 taken 44088 times.
132264 for (unsigned i = 0; i < iovcnt; ++i) {
510 88176 memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len);
511 88176 pos += iov[i].iov_len;
512 }
513
514 44088 const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44088 times.
44088 if (retval < 0) {
516 assert(errno != EMSGSIZE);
517 if (!(flags_ & kFlagSendIgnoreFailure)) {
518 PANIC(kLogSyslogErr | kLogDebug,
519 "failed to write to external cache transport (%d), aborting",
520 errno);
521 }
522 }
523 44088 }
524
525
526 353260 void CacheTransport::SendFrame(CacheTransport::Frame *frame) {
527 353260 cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc();
528 353260 const int32_t size = msg_rpc->ByteSize();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353260 times.
353260 assert(size > 0);
530 #ifdef __APPLE__
531 void *buffer = smalloc(size);
532 #else
533 353260 void *buffer = alloca(size);
534 #endif
535 353260 const bool retval = msg_rpc->SerializeToArray(buffer, size);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 353216 times.
353216 assert(retval);
537 353216 SendData(buffer, size, frame->attachment(), frame->att_size());
538 #ifdef __APPLE__
539 free(buffer);
540 #endif
541 353128 }
542