| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/pack.cc |
| Date: | 2026-07-05 02:36:18 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 283 | 307 | 92.2% |
| Branches: | 202 | 332 | 60.8% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "pack.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <cassert> | ||
| 9 | #include <cstring> | ||
| 10 | #include <map> | ||
| 11 | |||
| 12 | #include "util/exception.h" | ||
| 13 | #include "util/platform.h" | ||
| 14 | #include "util/smalloc.h" | ||
| 15 | #include "util/string.h" | ||
| 16 | |||
| 17 | using namespace std; // NOLINT | ||
| 18 | |||
| 19 | namespace { // some private utility functions used by ObjectPackProducer | ||
| 20 | |||
| 21 | 2592 | void InitializeHeader(const int version, const int num_objects, | |
| 22 | const size_t pack_size, std::string *header) { | ||
| 23 |
1/2✓ Branch 0 taken 2592 times.
✗ Branch 1 not taken.
|
2592 | if (header) { |
| 24 |
2/4✓ Branch 2 taken 2592 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2592 times.
✗ Branch 6 not taken.
|
2592 | *header = "V" + StringifyInt(version) + "\n"; |
| 25 |
3/6✓ Branch 2 taken 2592 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2592 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2592 times.
✗ Branch 9 not taken.
|
2592 | *header += "S" + StringifyInt(pack_size) + "\n"; |
| 26 |
3/6✓ Branch 2 taken 2592 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2592 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2592 times.
✗ Branch 9 not taken.
|
2592 | *header += "N" + StringifyInt(num_objects) + "\n"; |
| 27 | 2592 | *header += "--\n"; | |
| 28 | } | ||
| 29 | 2592 | } | |
| 30 | |||
| 31 | 222900 | void AppendItemToHeader(ObjectPack::BucketContentType object_type, | |
| 32 | const std::string &hash_str, const size_t object_size, | ||
| 33 | const std::string &object_name, std::string *header) { | ||
| 34 | // If the item type is kName, the "item_name" parameter should not be empty | ||
| 35 |
4/6✓ Branch 0 taken 99 times.
✓ Branch 1 taken 222801 times.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 99 times.
✗ Branch 6 not taken.
|
222900 | assert((object_type == ObjectPack::kCas) |
| 36 | || ((object_type == ObjectPack::kNamed) && (!object_name.empty()))); | ||
| 37 |
1/2✓ Branch 2 taken 222900 times.
✗ Branch 3 not taken.
|
222900 | std::string line_prefix = ""; |
| 38 |
1/2✓ Branch 2 taken 222900 times.
✗ Branch 3 not taken.
|
222900 | std::string line_suffix = ""; |
| 39 |
2/3✓ Branch 0 taken 99 times.
✓ Branch 1 taken 222801 times.
✗ Branch 2 not taken.
|
222900 | switch (object_type) { |
| 40 | 99 | case ObjectPack::kNamed: | |
| 41 |
1/2✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
|
99 | line_prefix = "N "; |
| 42 |
3/6✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 99 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 99 times.
✗ Branch 9 not taken.
|
99 | line_suffix = std::string(" ") + Base64Url(object_name); |
| 43 | 99 | break; | |
| 44 | 222801 | case ObjectPack::kCas: | |
| 45 |
1/2✓ Branch 1 taken 222801 times.
✗ Branch 2 not taken.
|
222801 | line_prefix = "C "; |
| 46 | 222801 | break; | |
| 47 | ✗ | default: | |
| 48 | ✗ | PANIC(kLogStderr, "Unknown object pack type to be added to header."); | |
| 49 | } | ||
| 50 |
1/2✓ Branch 0 taken 222900 times.
✗ Branch 1 not taken.
|
222900 | if (header) { |
| 51 |
4/8✓ Branch 1 taken 222900 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 222900 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 222900 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 222900 times.
✗ Branch 11 not taken.
|
445800 | *header += line_prefix + hash_str + " " + StringifyInt(object_size) |
| 52 |
3/6✓ Branch 1 taken 222900 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 222900 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 222900 times.
✗ Branch 8 not taken.
|
222900 | + line_suffix + "\n"; |
| 53 | } | ||
| 54 | 222900 | } | |
| 55 | |||
| 56 | } // namespace | ||
| 57 | |||
| 58 | 3312174 | ObjectPack::Bucket::Bucket() | |
| 59 | 3312174 | : content(reinterpret_cast<unsigned char *>(smalloc(kInitialSize))) | |
| 60 | 3312174 | , size(0) | |
| 61 | 3312174 | , capacity(kInitialSize) | |
| 62 | 3312174 | , content_type(kEmpty) | |
| 63 | 6624348 | , name() { } | |
| 64 | |||
| 65 | 11811 | void ObjectPack::Bucket::Add(const void *buf, const uint64_t buf_size) { | |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11811 times.
|
11811 | if (buf_size == 0) |
| 67 | ✗ | return; | |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 139014 times.
✓ Branch 1 taken 11811 times.
|
150825 | while (size + buf_size > capacity) { |
| 70 | 139014 | capacity *= 2; | |
| 71 | 139014 | content = reinterpret_cast<unsigned char *>(srealloc(content, capacity)); | |
| 72 | } | ||
| 73 | 11811 | memcpy(content + size, buf, buf_size); | |
| 74 | 11811 | size += buf_size; | |
| 75 | } | ||
| 76 | |||
| 77 | 3312174 | ObjectPack::Bucket::~Bucket() { free(content); } | |
| 78 | |||
| 79 | //------------------------------------------------------------------------------ | ||
| 80 | |||
| 81 | 1729 | ObjectPack::ObjectPack(const uint64_t limit) : limit_(limit), size_(0) { | |
| 82 | 1729 | InitLock(); | |
| 83 | 1729 | } | |
| 84 | |||
| 85 | 1710 | ObjectPack::~ObjectPack() { | |
| 86 | 3420 | for (std::set<BucketHandle>::const_iterator i = open_buckets_.begin(), | |
| 87 | 1710 | iEnd = open_buckets_.end(); | |
| 88 |
2/2✓ Branch 1 taken 2178 times.
✓ Branch 2 taken 1710 times.
|
3888 | i != iEnd; |
| 89 | 2178 | ++i) { | |
| 90 |
1/2✓ Branch 1 taken 2178 times.
✗ Branch 2 not taken.
|
2178 | delete *i; |
| 91 | } | ||
| 92 | |||
| 93 |
2/2✓ Branch 1 taken 3309864 times.
✓ Branch 2 taken 1710 times.
|
3311574 | for (unsigned i = 0; i < buckets_.size(); ++i) |
| 94 |
1/2✓ Branch 1 taken 3309864 times.
✗ Branch 2 not taken.
|
3309864 | delete buckets_[i]; |
| 95 | 1710 | pthread_mutex_destroy(lock_); | |
| 96 | 1710 | free(lock_); | |
| 97 | 1710 | } | |
| 98 | |||
| 99 | 2307 | void ObjectPack::AddToBucket(const void *buf, const uint64_t size, | |
| 100 | const ObjectPack::BucketHandle handle) { | ||
| 101 | 2307 | handle->Add(buf, size); | |
| 102 | 2307 | } | |
| 103 | |||
| 104 | 3312141 | ObjectPack::BucketHandle ObjectPack::NewBucket() { | |
| 105 |
2/4✓ Branch 1 taken 3312141 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3312141 times.
✗ Branch 5 not taken.
|
3312141 | BucketHandle handle = new Bucket(); |
| 106 | |||
| 107 | 3312141 | const MutexLockGuard mutex_guard(lock_); | |
| 108 |
1/2✓ Branch 1 taken 3312141 times.
✗ Branch 2 not taken.
|
3312141 | open_buckets_.insert(handle); |
| 109 | 3312141 | return handle; | |
| 110 | 3312141 | } | |
| 111 | |||
| 112 | /** | ||
| 113 | * Can only fail due to insufficient remaining space in the ObjectPack. | ||
| 114 | */ | ||
| 115 | 3312379 | bool ObjectPack::CommitBucket(const BucketContentType type, | |
| 116 | const shash::Any &id, | ||
| 117 | const ObjectPack::BucketHandle handle, | ||
| 118 | const std::string &name) { | ||
| 119 | 3312379 | handle->id = id; | |
| 120 | |||
| 121 | 3312379 | handle->content_type = type; | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3312379 times.
|
3312379 | if (type == kNamed) { |
| 123 | ✗ | handle->name = name; | |
| 124 | } | ||
| 125 | |||
| 126 | 3312379 | const MutexLockGuard mutex_guard(lock_); | |
| 127 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 2 taken 3312346 times.
|
3312379 | if (buckets_.size() >= kMaxObjects) |
| 128 | 33 | return false; | |
| 129 |
2/2✓ Branch 0 taken 2482 times.
✓ Branch 1 taken 3309864 times.
|
3312346 | if (size_ + handle->size > limit_) |
| 130 | 2482 | return false; | |
| 131 |
1/2✓ Branch 1 taken 3309864 times.
✗ Branch 2 not taken.
|
3309864 | open_buckets_.erase(handle); |
| 132 |
1/2✓ Branch 1 taken 3309864 times.
✗ Branch 2 not taken.
|
3309864 | buckets_.push_back(handle); |
| 133 | 3309864 | size_ += handle->size; | |
| 134 | 3309864 | return true; | |
| 135 | 3312379 | } | |
| 136 | |||
| 137 | 99 | void ObjectPack::DiscardBucket(const BucketHandle handle) { | |
| 138 | 99 | const MutexLockGuard mutex_guard(lock_); | |
| 139 |
1/2✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
|
99 | open_buckets_.erase(handle); |
| 140 |
1/2✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
|
99 | delete handle; |
| 141 | 99 | } | |
| 142 | |||
| 143 | 1729 | void ObjectPack::InitLock() { | |
| 144 | 1729 | lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 145 | 1729 | const int retval = pthread_mutex_init(lock_, NULL); | |
| 146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1729 times.
|
1729 | assert(retval == 0); |
| 147 | 1729 | } | |
| 148 | |||
| 149 | /** | ||
| 150 | * If a commit failed, an open Bucket can be transferred to another ObjectPack | ||
| 151 | * with more space. | ||
| 152 | */ | ||
| 153 | 451 | void ObjectPack::TransferBucket(const ObjectPack::BucketHandle handle, | |
| 154 | ObjectPack *other) { | ||
| 155 | 451 | const MutexLockGuard mutex_guard(lock_); | |
| 156 |
1/2✓ Branch 1 taken 451 times.
✗ Branch 2 not taken.
|
451 | open_buckets_.erase(handle); |
| 157 |
1/2✓ Branch 1 taken 451 times.
✗ Branch 2 not taken.
|
451 | other->open_buckets_.insert(handle); |
| 158 | 451 | } | |
| 159 | |||
| 160 | 1248912 | unsigned char *ObjectPack::BucketContent(size_t idx) const { | |
| 161 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1248912 times.
|
1248912 | assert(idx < buckets_.size()); |
| 162 | 1248912 | return buckets_[idx]->content; | |
| 163 | } | ||
| 164 | |||
| 165 | 1471713 | uint64_t ObjectPack::BucketSize(size_t idx) const { | |
| 166 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1471713 times.
|
1471713 | assert(idx < buckets_.size()); |
| 167 | 1471713 | return buckets_[idx]->size; | |
| 168 | } | ||
| 169 | |||
| 170 | 222801 | const shash::Any &ObjectPack::BucketId(size_t idx) const { | |
| 171 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 222801 times.
|
222801 | assert(idx < buckets_.size()); |
| 172 | 222801 | return buckets_[idx]->id; | |
| 173 | } | ||
| 174 | |||
| 175 | //------------------------------------------------------------------------------ | ||
| 176 | |||
| 177 | /** | ||
| 178 | * Hash over the header. The hash algorithm needs to be provided by hash. | ||
| 179 | */ | ||
| 180 | 2389 | void ObjectPackProducer::GetDigest(shash::Any *hash) { | |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2389 times.
|
2389 | assert(hash); |
| 182 | 2389 | shash::HashString(header_, hash); | |
| 183 | 2389 | } | |
| 184 | |||
| 185 | 2493 | ObjectPackProducer::ObjectPackProducer(ObjectPack *pack) | |
| 186 | 2493 | : pack_(pack), big_file_(NULL), pos_(0), idx_(0), pos_in_bucket_(0) { | |
| 187 | 2493 | const unsigned N = pack->GetNoObjects(); | |
| 188 | // rough guess, most likely a little too much | ||
| 189 |
1/2✓ Branch 1 taken 2493 times.
✗ Branch 2 not taken.
|
2493 | header_.reserve(30 + N * (2 * shash::kMaxDigestSize + 5)); |
| 190 | |||
| 191 |
1/2✓ Branch 2 taken 2493 times.
✗ Branch 3 not taken.
|
2493 | InitializeHeader(2, N, pack->size(), &header_); |
| 192 | |||
| 193 |
2/2✓ Branch 0 taken 222801 times.
✓ Branch 1 taken 2493 times.
|
225294 | for (unsigned i = 0; i < N; ++i) { |
| 194 |
3/6✓ Branch 2 taken 222801 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 222801 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 222801 times.
✗ Branch 11 not taken.
|
222801 | AppendItemToHeader(ObjectPack::kCas, pack->BucketId(i).ToString(true), |
| 195 | pack->BucketSize(i), "", &header_); | ||
| 196 | } | ||
| 197 | 2493 | } | |
| 198 | |||
| 199 | 99 | ObjectPackProducer::ObjectPackProducer(const shash::Any &id, FILE *big_file, | |
| 200 | 99 | const std::string &file_name) | |
| 201 | 99 | : pack_(NULL), big_file_(big_file), pos_(0), idx_(0), pos_in_bucket_(0) { | |
| 202 | 99 | const int fd = fileno(big_file_); | |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
|
99 | assert(fd >= 0); |
| 204 | platform_stat64 info; | ||
| 205 | 99 | const int retval = platform_fstat(fd, &info); | |
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
|
99 | assert(retval == 0); |
| 207 | |||
| 208 |
1/2✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
|
99 | InitializeHeader(2, 1, info.st_size, &header_); |
| 209 | |||
| 210 |
2/4✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 99 times.
✗ Branch 5 not taken.
|
99 | AppendItemToHeader(ObjectPack::kNamed, id.ToString(true), info.st_size, |
| 211 | file_name, &header_); | ||
| 212 | |||
| 213 |
1/2✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
|
99 | rewind(big_file); |
| 214 | 99 | } | |
| 215 | |||
| 216 | /** | ||
| 217 | * Copies as many bytes as possible into buf. If the returned number of bytes | ||
| 218 | * is shorter than buf_size, everything has been produced. | ||
| 219 | */ | ||
| 220 | 1037749 | unsigned ObjectPackProducer::ProduceNext(const unsigned buf_size, | |
| 221 | unsigned char *buf) { | ||
| 222 | 1037749 | const unsigned remaining_in_header = (pos_ < header_.size()) | |
| 223 | 10842 | ? (header_.size() - pos_) | |
| 224 |
2/2✓ Branch 0 taken 10842 times.
✓ Branch 1 taken 1026907 times.
|
1048591 | : 0; |
| 225 | 1037749 | const unsigned nbytes_header = std::min(remaining_in_header, buf_size); | |
| 226 |
2/2✓ Branch 0 taken 10842 times.
✓ Branch 1 taken 1026907 times.
|
1037749 | if (nbytes_header) { |
| 227 | 10842 | memcpy(buf, header_.data() + pos_, nbytes_header); | |
| 228 | 10842 | pos_ += nbytes_header; | |
| 229 | } | ||
| 230 | |||
| 231 | 1037749 | unsigned remaining_in_buf = buf_size - nbytes_header; | |
| 232 |
2/2✓ Branch 0 taken 8349 times.
✓ Branch 1 taken 1029400 times.
|
1037749 | if (remaining_in_buf == 0) |
| 233 | 8349 | return nbytes_header; | |
| 234 | 1029400 | unsigned nbytes_payload = 0; | |
| 235 | |||
| 236 |
2/2✓ Branch 0 taken 627 times.
✓ Branch 1 taken 1028773 times.
|
1029400 | if (big_file_) { |
| 237 |
1/2✓ Branch 1 taken 627 times.
✗ Branch 2 not taken.
|
627 | const size_t nbytes = fread(buf + nbytes_header, 1, remaining_in_buf, |
| 238 | big_file_); | ||
| 239 | 627 | nbytes_payload = nbytes; | |
| 240 | 627 | pos_ += nbytes_payload; | |
| 241 |
2/2✓ Branch 1 taken 1028538 times.
✓ Branch 2 taken 235 times.
|
1028773 | } else if (idx_ < pack_->GetNoObjects()) { |
| 242 | // Copy a few buckets more | ||
| 243 |
6/6✓ Branch 0 taken 1251273 times.
✓ Branch 1 taken 1026177 times.
✓ Branch 3 taken 1248912 times.
✓ Branch 4 taken 2361 times.
✓ Branch 5 taken 1248912 times.
✓ Branch 6 taken 1028538 times.
|
2277450 | while ((remaining_in_buf) > 0 && (idx_ < pack_->GetNoObjects())) { |
| 244 | 1248912 | const unsigned remaining_in_bucket = pack_->BucketSize(idx_) | |
| 245 | 1248912 | - pos_in_bucket_; | |
| 246 | 1248912 | const unsigned nbytes = std::min(remaining_in_buf, remaining_in_bucket); | |
| 247 | 2497824 | memcpy(buf + nbytes_header + nbytes_payload, | |
| 248 | 1248912 | pack_->BucketContent(idx_) + pos_in_bucket_, nbytes); | |
| 249 | |||
| 250 | 1248912 | pos_in_bucket_ += nbytes; | |
| 251 | 1248912 | nbytes_payload += nbytes; | |
| 252 | 1248912 | remaining_in_buf -= nbytes; | |
| 253 |
2/2✓ Branch 0 taken 222801 times.
✓ Branch 1 taken 1026111 times.
|
1248912 | if (nbytes == remaining_in_bucket) { |
| 254 | 222801 | pos_in_bucket_ = 0; | |
| 255 | 222801 | idx_++; | |
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | 1029400 | return nbytes_header + nbytes_payload; | |
| 261 | } | ||
| 262 | |||
| 263 | //------------------------------------------------------------------------------ | ||
| 264 | |||
| 265 | 2422 | ObjectPackConsumer::ObjectPackConsumer(const shash::Any &expected_digest, | |
| 266 | 2422 | const unsigned expected_header_size) | |
| 267 | 2422 | : expected_digest_(expected_digest) | |
| 268 | 2422 | , expected_header_size_(expected_header_size) | |
| 269 | 2422 | , pos_(0) | |
| 270 | 2422 | , idx_(0) | |
| 271 | 2422 | , pos_in_object_(0) | |
| 272 | 2422 | , pos_in_accu_(0) | |
| 273 | 2422 | , state_(ObjectPackBuild::kStateContinue) | |
| 274 | 2422 | , size_(0) { | |
| 275 | // Upper limit of 100B per entry | ||
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2422 times.
|
2422 | if (expected_header_size > (100 * ObjectPack::kMaxObjects)) { |
| 277 | ✗ | state_ = ObjectPackBuild::kStateHeaderTooBig; | |
| 278 | ✗ | return; | |
| 279 | } | ||
| 280 | |||
| 281 |
1/2✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
|
2422 | raw_header_.reserve(expected_header_size); |
| 282 | } | ||
| 283 | |||
| 284 | /** | ||
| 285 | * At the end of the function, pos_ will have progressed by buf_size (unless | ||
| 286 | * the buffer contains trailing garbage bytes. | ||
| 287 | */ | ||
| 288 | 1036312 | ObjectPackBuild::State ObjectPackConsumer::ConsumeNext( | |
| 289 | const unsigned buf_size, const unsigned char *buf) { | ||
| 290 |
2/2✓ Branch 0 taken 2211 times.
✓ Branch 1 taken 1034101 times.
|
1036312 | if (buf_size == 0) |
| 291 | 2211 | return state_; | |
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1034101 times.
|
1034101 | if (state_ == ObjectPackBuild::kStateDone) { |
| 293 | ✗ | state_ = ObjectPackBuild::kStateTrailingBytes; | |
| 294 | ✗ | return state_; | |
| 295 | } | ||
| 296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1034101 times.
|
1034101 | if (state_ != ObjectPackBuild::kStateContinue) |
| 297 | ✗ | return state_; | |
| 298 | |||
| 299 |
2/2✓ Branch 0 taken 8065 times.
✓ Branch 1 taken 1026036 times.
|
1034101 | const unsigned remaining_in_header = (pos_ < expected_header_size_) |
| 300 | 8065 | ? (expected_header_size_ - pos_) | |
| 301 | : 0; | ||
| 302 | 1034101 | const unsigned nbytes_header = std::min(remaining_in_header, buf_size); | |
| 303 |
2/2✓ Branch 0 taken 8065 times.
✓ Branch 1 taken 1026036 times.
|
1034101 | if (nbytes_header) { |
| 304 |
2/4✓ Branch 2 taken 8065 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8065 times.
✗ Branch 6 not taken.
|
8065 | raw_header_ += string(reinterpret_cast<const char *>(buf), nbytes_header); |
| 305 | 8065 | pos_ += nbytes_header; | |
| 306 | } | ||
| 307 | |||
| 308 |
2/2✓ Branch 0 taken 5643 times.
✓ Branch 1 taken 1028458 times.
|
1034101 | if (pos_ < expected_header_size_) |
| 309 | 5643 | return ObjectPackBuild::kStateContinue; | |
| 310 | |||
| 311 | // This condition can only be true once through the lifetime of the | ||
| 312 | // Consumer. | ||
| 313 |
3/4✓ Branch 0 taken 2422 times.
✓ Branch 1 taken 1026036 times.
✓ Branch 2 taken 2422 times.
✗ Branch 3 not taken.
|
1028458 | if (nbytes_header && (pos_ == expected_header_size_)) { |
| 314 |
1/2✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
|
2422 | shash::Any digest(expected_digest_.algorithm); |
| 315 |
1/2✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
|
2422 | shash::HashString(raw_header_, &digest); |
| 316 |
2/4✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2422 times.
|
2422 | if (digest != expected_digest_) { |
| 317 | ✗ | state_ = ObjectPackBuild::kStateCorrupt; | |
| 318 | 66 | return state_; | |
| 319 | } else { | ||
| 320 |
1/2✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
|
2422 | const bool retval = ParseHeader(); |
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2422 times.
|
2422 | if (!retval) { |
| 322 | ✗ | state_ = ObjectPackBuild::kStateBadFormat; | |
| 323 | ✗ | return state_; | |
| 324 | } | ||
| 325 | // We don't need the raw string anymore | ||
| 326 | 2422 | raw_header_.clear(); | |
| 327 | } | ||
| 328 | |||
| 329 | // Empty pack? | ||
| 330 |
6/6✓ Branch 0 taken 99 times.
✓ Branch 1 taken 2323 times.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 66 times.
✓ Branch 6 taken 2356 times.
|
2422 | if ((buf_size == nbytes_header) && (index_.size() == 0)) { |
| 331 | 66 | state_ = ObjectPackBuild::kStateDone; | |
| 332 | 66 | return state_; | |
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | 1028392 | const unsigned remaining_in_buf = buf_size - nbytes_header; | |
| 337 | 1028392 | const unsigned char *payload = buf + nbytes_header; | |
| 338 |
1/2✓ Branch 1 taken 1028392 times.
✗ Branch 2 not taken.
|
1028392 | return ConsumePayload(remaining_in_buf, payload); |
| 339 | } | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Informs listeners for small complete objects. For large objects, buffers | ||
| 343 | * the | ||
| 344 | * input into reasonably sized chunks. buf can contain both a chunk of data | ||
| 345 | * that needs to be added to the consumer's accumulator and a bunch of | ||
| 346 | * complete small objects. We use the accumulator only if necessary to avoid | ||
| 347 | * unnecessary memory copies. | ||
| 348 | */ | ||
| 349 | 1028392 | ObjectPackBuild::State ObjectPackConsumer::ConsumePayload( | |
| 350 | const unsigned buf_size, const unsigned char *buf) { | ||
| 351 | 1028392 | uint64_t pos_in_buf = 0; | |
| 352 | 2428562 | while ((idx_ < index_.size()) | |
| 353 |
8/8✓ Branch 0 taken 2426206 times.
✓ Branch 1 taken 2356 times.
✓ Branch 2 taken 1026069 times.
✓ Branch 3 taken 1400137 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 1026036 times.
✓ Branch 7 taken 1400170 times.
✓ Branch 8 taken 1028392 times.
|
2428562 | && ((pos_in_buf < buf_size) || (index_[idx_].size == 0))) { |
| 354 | // Fill the accumulator or process next small object | ||
| 355 | uint64_t nbytes; // How many bytes are consumed in this iteration | ||
| 356 | 1400170 | const uint64_t remaining_in_buf = buf_size - pos_in_buf; | |
| 357 | 1400170 | const uint64_t remaining_in_object = index_[idx_].size - pos_in_object_; | |
| 358 | 1400170 | const bool is_small_rest = remaining_in_buf < kAccuSize; | |
| 359 | |||
| 360 | // We use the accumulator if there is already something in or if we have a | ||
| 361 | // small piece of data of a larger object. | ||
| 362 | 1400170 | nbytes = std::min(remaining_in_object, remaining_in_buf); | |
| 363 |
2/2✓ Branch 0 taken 1065946 times.
✓ Branch 1 taken 334224 times.
|
1400170 | if ((pos_in_accu_ > 0) |
| 364 |
4/4✓ Branch 0 taken 848034 times.
✓ Branch 1 taken 217912 times.
✓ Branch 2 taken 156288 times.
✓ Branch 3 taken 691746 times.
|
1065946 | || ((remaining_in_buf < remaining_in_object) && is_small_rest)) { |
| 365 | 490512 | const uint64_t remaining_in_accu = kAccuSize - pos_in_accu_; | |
| 366 | 490512 | nbytes = std::min(remaining_in_accu, nbytes); | |
| 367 | 490512 | memcpy(accumulator_ + pos_in_accu_, buf + pos_in_buf, nbytes); | |
| 368 | 490512 | pos_in_accu_ += nbytes; | |
| 369 |
4/4✓ Branch 0 taken 338976 times.
✓ Branch 1 taken 151536 times.
✓ Branch 2 taken 4752 times.
✓ Branch 3 taken 334224 times.
|
490512 | if ((pos_in_accu_ == kAccuSize) || (nbytes == remaining_in_object)) { |
| 370 |
1/2✓ Branch 1 taken 156288 times.
✗ Branch 2 not taken.
|
156288 | NotifyListeners(ObjectPackBuild::Event( |
| 371 |
1/2✓ Branch 3 taken 156288 times.
✗ Branch 4 not taken.
|
156288 | index_[idx_].id, index_[idx_].size, pos_in_accu_, accumulator_, |
| 372 | 156288 | index_[idx_].entry_type, index_[idx_].entry_name)); | |
| 373 | 156288 | pos_in_accu_ = 0; | |
| 374 | } | ||
| 375 | 490512 | } else { // directly trigger listeners using buf | |
| 376 |
1/2✓ Branch 1 taken 909658 times.
✗ Branch 2 not taken.
|
909658 | NotifyListeners(ObjectPackBuild::Event( |
| 377 |
1/2✓ Branch 3 taken 909658 times.
✗ Branch 4 not taken.
|
909658 | index_[idx_].id, index_[idx_].size, nbytes, buf + pos_in_buf, |
| 378 | 909658 | index_[idx_].entry_type, index_[idx_].entry_name)); | |
| 379 | } | ||
| 380 | |||
| 381 | 1400170 | pos_in_buf += nbytes; | |
| 382 | 1400170 | pos_in_object_ += nbytes; | |
| 383 |
2/2✓ Branch 0 taken 222697 times.
✓ Branch 1 taken 1177473 times.
|
1400170 | if (nbytes == remaining_in_object) { |
| 384 | 222697 | idx_++; | |
| 385 | 222697 | pos_in_object_ = 0; | |
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | 1028392 | pos_ += buf_size; | |
| 390 | |||
| 391 |
2/2✓ Branch 1 taken 2356 times.
✓ Branch 2 taken 1026036 times.
|
1028392 | if (idx_ == index_.size()) |
| 392 |
1/2✓ Branch 0 taken 2356 times.
✗ Branch 1 not taken.
|
2356 | state_ = (pos_in_buf == buf_size) ? ObjectPackBuild::kStateDone |
| 393 | : ObjectPackBuild::kStateTrailingBytes; | ||
| 394 | else | ||
| 395 | 1026036 | state_ = ObjectPackBuild::kStateContinue; | |
| 396 | 1028392 | return state_; | |
| 397 | } | ||
| 398 | |||
| 399 | 2422 | bool ObjectPackConsumer::ParseHeader() { | |
| 400 | 2422 | map<char, string> header; | |
| 401 | const unsigned char *data = reinterpret_cast<const unsigned char *>( | ||
| 402 | 2422 | raw_header_.data()); | |
| 403 |
1/2✓ Branch 2 taken 2422 times.
✗ Branch 3 not taken.
|
2422 | ParseKeyvalMem(data, raw_header_.size(), &header); |
| 404 |
2/5✓ Branch 2 taken 2422 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2422 times.
|
2422 | if (header.find('V') == header.end()) |
| 405 | ✗ | return false; | |
| 406 |
3/7✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2422 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2422 times.
|
2422 | if (header['V'] != "2") |
| 407 | ✗ | return false; | |
| 408 |
2/4✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2422 times.
✗ Branch 5 not taken.
|
2422 | size_ = String2Uint64(header['S']); |
| 409 |
2/4✓ Branch 1 taken 2422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2422 times.
✗ Branch 5 not taken.
|
2422 | const unsigned nobjects = String2Uint64(header['N']); |
| 410 | |||
| 411 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2356 times.
|
2422 | if (nobjects == 0) |
| 412 | 66 | return true; | |
| 413 | |||
| 414 | // Build the object index | ||
| 415 | 2356 | const size_t separator_idx = raw_header_.find("--\n"); | |
| 416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2356 times.
|
2356 | if (separator_idx == string::npos) |
| 417 | ✗ | return false; | |
| 418 | 2356 | unsigned index_idx = separator_idx + 3; | |
| 419 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2356 times.
|
2356 | if (index_idx >= raw_header_.size()) |
| 420 | ✗ | return false; | |
| 421 | |||
| 422 | 2356 | uint64_t sum_size = 0; | |
| 423 | do { | ||
| 424 | 222697 | const unsigned remaining_in_header = raw_header_.size() - index_idx; | |
| 425 | 445394 | const string line = GetLineMem(raw_header_.data() + index_idx, | |
| 426 |
1/2✓ Branch 2 taken 222697 times.
✗ Branch 3 not taken.
|
222697 | remaining_in_header); |
| 427 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 222697 times.
|
222697 | if (line == "") |
| 428 | ✗ | break; | |
| 429 | |||
| 430 |
1/2✓ Branch 1 taken 222697 times.
✗ Branch 2 not taken.
|
222697 | IndexEntry entry; |
| 431 |
2/4✓ Branch 1 taken 222697 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 222697 times.
|
222697 | if (!ParseItem(line, &entry, &sum_size)) { |
| 432 | ✗ | break; | |
| 433 | } | ||
| 434 | |||
| 435 |
1/2✓ Branch 1 taken 222697 times.
✗ Branch 2 not taken.
|
222697 | index_.push_back(entry); |
| 436 | 222697 | index_idx += line.size() + 1; | |
| 437 |
4/6✓ Branch 1 taken 222697 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 222697 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 220341 times.
✓ Branch 8 taken 2356 times.
|
445394 | } while (index_idx < raw_header_.size()); |
| 438 | |||
| 439 |
2/4✓ Branch 1 taken 2356 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2356 times.
✗ Branch 4 not taken.
|
2356 | return (nobjects == index_.size()) && (size_ == sum_size); |
| 440 | 2422 | } | |
| 441 | |||
| 442 | 222697 | bool ObjectPackConsumer::ParseItem(const std::string &line, | |
| 443 | ObjectPackConsumer::IndexEntry *entry, | ||
| 444 | uint64_t *sum_size) { | ||
| 445 |
2/4✓ Branch 0 taken 222697 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 222697 times.
|
222697 | if (!entry || !sum_size) { |
| 446 | ✗ | return false; | |
| 447 | } | ||
| 448 | |||
| 449 |
2/2✓ Branch 1 taken 222664 times.
✓ Branch 2 taken 33 times.
|
222697 | if (line[0] == 'C') { // CAS blob |
| 450 | 222664 | const ObjectPack::BucketContentType entry_type = ObjectPack::kCas; | |
| 451 | |||
| 452 | // We could use SplitString but we can have many lines so we do something | ||
| 453 | // more efficient here | ||
| 454 | 222664 | const size_t separator = line.find(' ', 2); | |
| 455 |
3/6✓ Branch 0 taken 222664 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 222664 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 222664 times.
|
222664 | if ((separator == string::npos) || (separator == (line.size() - 1))) { |
| 456 | ✗ | return false; | |
| 457 | } | ||
| 458 | |||
| 459 |
2/4✓ Branch 1 taken 222664 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 222664 times.
✗ Branch 5 not taken.
|
222664 | const uint64_t size = String2Uint64(line.substr(separator + 1)); |
| 460 | 222664 | *sum_size += size; | |
| 461 | |||
| 462 | // Warning do not construct a HexPtr with an rvalue! | ||
| 463 | // The constructor takes the address of its argument. | ||
| 464 |
1/2✓ Branch 1 taken 222664 times.
✗ Branch 2 not taken.
|
222664 | const std::string hash_string = line.substr(2, separator - 2); |
| 465 | 222664 | const shash::HexPtr hex_ptr(hash_string); | |
| 466 | |||
| 467 |
1/2✓ Branch 1 taken 222664 times.
✗ Branch 2 not taken.
|
222664 | entry->id = shash::MkFromSuffixedHexPtr(hex_ptr); |
| 468 | 222664 | entry->size = size; | |
| 469 | 222664 | entry->entry_type = entry_type; | |
| 470 |
1/2✓ Branch 1 taken 222664 times.
✗ Branch 2 not taken.
|
222664 | entry->entry_name = ""; |
| 471 |
1/2✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
222697 | } else if (line[0] == 'N') { // Named file |
| 472 | 33 | const ObjectPack::BucketContentType entry_type = ObjectPack::kNamed; | |
| 473 | |||
| 474 | // First separator, before the size field | ||
| 475 | 33 | const size_t separator1 = line.find(' ', 2); | |
| 476 |
3/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
|
33 | if ((separator1 == string::npos) || (separator1 == (line.size() - 1))) { |
| 477 | ✗ | return false; | |
| 478 | } | ||
| 479 | |||
| 480 | // Second separator, before the name field | ||
| 481 | 33 | const size_t separator2 = line.find(' ', separator1 + 1); | |
| 482 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | if ((separator1 == 0) || (separator1 == string::npos) |
| 483 |
3/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
|
66 | || (separator1 == (line.size() - 1))) { |
| 484 | ✗ | return false; | |
| 485 | } | ||
| 486 | |||
| 487 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | const uint64_t size = String2Uint64( |
| 488 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
66 | line.substr(separator1 + 1, separator2 - separator1 - 1)); |
| 489 | |||
| 490 | 33 | std::string name; | |
| 491 |
3/7✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 33 times.
|
33 | if (!Debase64(line.substr(separator2 + 1), &name)) { |
| 492 | ✗ | return false; | |
| 493 | } | ||
| 494 | |||
| 495 | 33 | *sum_size += size; | |
| 496 | |||
| 497 | // Warning do not construct a HexPtr with an rvalue! | ||
| 498 | // The constructor takes the address of its argument. | ||
| 499 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | const std::string hash_string = line.substr(2, separator1 - 2); |
| 500 | 33 | const shash::HexPtr hex_ptr(hash_string); | |
| 501 | |||
| 502 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | entry->id = shash::MkFromSuffixedHexPtr(hex_ptr); |
| 503 | 33 | entry->size = size; | |
| 504 | 33 | entry->entry_type = entry_type; | |
| 505 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | entry->entry_name = name; |
| 506 |
1/2✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | } else { // Error |
| 507 | ✗ | return false; | |
| 508 | } | ||
| 509 | |||
| 510 | 222697 | return true; | |
| 511 | } | ||
| 512 |