| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/network/sink_mem.cc |
| Date: | 2025-11-02 02:35:35 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 40 | 49 | 81.6% |
| Branches: | 23 | 42 | 54.8% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "sink_mem.h" | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <cstring> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | #include "util/smalloc.h" | ||
| 12 | #include "util/string.h" | ||
| 13 | |||
| 14 | namespace cvmfs { | ||
| 15 | |||
| 16 | ✗ | MemSink::MemSink(size_t size) | |
| 17 | ✗ | : Sink(true), size_(size), pos_(0), max_size_(kMaxMemSize) { | |
| 18 | ✗ | data_ = static_cast<unsigned char *>(smalloc(size)); | |
| 19 | } | ||
| 20 | |||
| 21 | /** | ||
| 22 | * Appends data to the sink | ||
| 23 | * If the sink is too small and | ||
| 24 | * - the sink is the owner of data_: sink size is doubled | ||
| 25 | * - the sink is NOT the owner of data_: fails with -ENOSPC | ||
| 26 | * | ||
| 27 | * @returns on success: number of bytes written | ||
| 28 | * on failure: -errno. | ||
| 29 | */ | ||
| 30 | 3708 | int64_t MemSink::Write(const void *buf, uint64_t sz) { | |
| 31 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 3606 times.
|
3708 | if (pos_ + sz > size_) { |
| 32 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
|
102 | if (is_owner_) { |
| 33 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 34 times.
|
68 | const size_t new_size = pos_ + sz < size_ * 2 ? size_ * 2 : pos_ + sz + 1; |
| 34 | 68 | data_ = static_cast<unsigned char *>(srealloc(data_, new_size)); | |
| 35 | 68 | size_ = new_size; | |
| 36 | } else { | ||
| 37 | 34 | return -ENOSPC; | |
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 3674 | memcpy(data_ + pos_, buf, sz); | |
| 42 | 3674 | pos_ += sz; | |
| 43 | 3674 | return static_cast<int64_t>(sz); | |
| 44 | } | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Truncate all written data and start over at position zero. | ||
| 48 | * | ||
| 49 | * @returns Success = 0 | ||
| 50 | * Failure = -errno | ||
| 51 | */ | ||
| 52 | 487 | int MemSink::Reset() { | |
| 53 |
1/2✓ Branch 0 taken 487 times.
✗ Branch 1 not taken.
|
487 | if (is_owner_) { |
| 54 | 487 | free(data_); | |
| 55 | 487 | data_ = NULL; | |
| 56 | 487 | size_ = 0; | |
| 57 | } | ||
| 58 | |||
| 59 | 487 | pos_ = 0; | |
| 60 | |||
| 61 | 487 | return 0; | |
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @returns true if the object is correctly initialized. | ||
| 66 | */ | ||
| 67 | 2766 | bool MemSink::IsValid() { | |
| 68 |
3/4✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 2630 times.
|
2664 | return (size_ == 0 && pos_ == 0 && data_ == NULL) |
| 69 |
6/6✓ Branch 0 taken 2664 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 102 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 68 times.
✓ Branch 5 taken 34 times.
|
5430 | || (size_ > 0 && pos_ >= 0 && data_ != NULL); |
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Reserves new space in sinks that require reservation (see RequiresReserve) | ||
| 74 | * | ||
| 75 | * Successful if the requested size is smaller than already space reserved, or | ||
| 76 | * if the sink is the owner of the data and can allocate enough new space. | ||
| 77 | * | ||
| 78 | * @note If successful, always resets the current position to 0. | ||
| 79 | * | ||
| 80 | * Fails if | ||
| 81 | * 1) sink is not the owner of the data and more than the current size is | ||
| 82 | * requested | ||
| 83 | * 2) more space is requested than allowed (max_size_) | ||
| 84 | * | ||
| 85 | * @returns success = true | ||
| 86 | * failure = false | ||
| 87 | */ | ||
| 88 | 4583 | bool MemSink::Reserve(size_t size) { | |
| 89 |
2/2✓ Branch 0 taken 2019 times.
✓ Branch 1 taken 2564 times.
|
4583 | if (size <= size_) { |
| 90 | 2019 | pos_ = 0; | |
| 91 | 2019 | return true; | |
| 92 | } | ||
| 93 |
3/4✓ Branch 0 taken 2530 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2530 times.
|
2564 | if (!is_owner_ || size > max_size_) { |
| 94 | 34 | return false; | |
| 95 | } | ||
| 96 | |||
| 97 | 2530 | FreeData(); | |
| 98 | |||
| 99 | 2530 | size_ = size; | |
| 100 | 2530 | pos_ = 0; | |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2530 times.
|
2530 | if (size == 0) { |
| 102 | ✗ | data_ = NULL; | |
| 103 | } else { | ||
| 104 | 2530 | data_ = static_cast<unsigned char *>(smalloc(size)); | |
| 105 | } | ||
| 106 | 2530 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | /** | ||
| 110 | * Return a string representation describing the type of sink and its status | ||
| 111 | */ | ||
| 112 | ✗ | std::string MemSink::Describe() { | |
| 113 | ✗ | std::string result = "Memory sink with "; | |
| 114 | ✗ | result += "size: " + StringifyUint(size_); | |
| 115 | ✗ | result += " - current pos: " + StringifyUint(pos_); | |
| 116 | ✗ | return result; | |
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Allows the sink to adopt data that was initialized outside this class. | ||
| 121 | * The sink can become the new owner of the data, or not. | ||
| 122 | */ | ||
| 123 | 136 | void MemSink::Adopt(size_t size, size_t pos, unsigned char *data, | |
| 124 | bool is_owner) { | ||
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | assert(size >= pos); |
| 126 | |||
| 127 | 136 | FreeData(); | |
| 128 | |||
| 129 | 136 | is_owner_ = is_owner; | |
| 130 | 136 | size_ = size; | |
| 131 | 136 | pos_ = pos; | |
| 132 | 136 | data_ = data; | |
| 133 | 136 | } | |
| 134 | |||
| 135 | } // namespace cvmfs | ||
| 136 |