GCC Code Coverage Report


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