| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/file_backed_buffer.cc |
| Date: | 2026-08-16 02:40:26 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 101 | 110 | 91.8% |
| Branches: | 52 | 78 | 66.7% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "file_backed_buffer.h" | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <cstdio> | ||
| 10 | #include <cstdlib> | ||
| 11 | #include <cstring> | ||
| 12 | #include <string> | ||
| 13 | #include <unistd.h> | ||
| 14 | |||
| 15 | #include "util/exception.h" | ||
| 16 | #include "util/logging.h" | ||
| 17 | #include "util/posix.h" | ||
| 18 | #include "util/smalloc.h" | ||
| 19 | |||
| 20 | 29311 | FileBackedBuffer *FileBackedBuffer::Create(uint64_t in_memory_threshold, | |
| 21 | const std::string &tmp_dir) { | ||
| 22 |
1/2✓ Branch 2 taken 29311 times.
✗ Branch 3 not taken.
|
29311 | return new FileBackedBuffer(in_memory_threshold, tmp_dir); |
| 23 | } | ||
| 24 | |||
| 25 | 29311 | FileBackedBuffer::FileBackedBuffer(uint64_t in_memory_threshold, | |
| 26 | 29311 | const std::string &tmp_dir) | |
| 27 | 29311 | : in_memory_threshold_(in_memory_threshold) | |
| 28 | 29311 | , tmp_dir_(tmp_dir) | |
| 29 | 29311 | , state_(kWriteState) | |
| 30 | 29311 | , mode_(kMemoryMode) | |
| 31 | 29311 | , size_(0) | |
| 32 | 29311 | , buf_(NULL) | |
| 33 | 29311 | , pos_(0) | |
| 34 | 29311 | , fp_(NULL) | |
| 35 |
1/2✓ Branch 2 taken 29311 times.
✗ Branch 3 not taken.
|
29311 | , file_path_("") |
| 36 | 58622 | , mmapped_(NULL) { } | |
| 37 | |||
| 38 | 58622 | FileBackedBuffer::~FileBackedBuffer() { | |
| 39 | 29311 | free(buf_); | |
| 40 |
2/2✓ Branch 0 taken 17376 times.
✓ Branch 1 taken 11935 times.
|
29311 | if (mode_ != kFileMode) |
| 41 | 17376 | return; | |
| 42 | |||
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | if (state_ == kWriteState) { |
| 44 | ✗ | const int retval = fclose(fp_); | |
| 45 | ✗ | if (retval != 0) | |
| 46 | ✗ | PANIC(kLogStderr, "could not close temporary file %s: error %d", | |
| 47 | file_path_.c_str(), retval); | ||
| 48 | } else { | ||
| 49 | 11935 | mmapped_->Unmap(); | |
| 50 |
1/2✓ Branch 0 taken 11935 times.
✗ Branch 1 not taken.
|
11935 | delete mmapped_; |
| 51 | } | ||
| 52 | 11935 | const int retval = unlink(file_path_.c_str()); | |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | if (retval != 0) |
| 54 | ✗ | PANIC(kLogStderr, "could not delete temporary file %s: error %d", | |
| 55 | file_path_.c_str(), retval); | ||
| 56 |
4/4✓ Branch 1 taken 11935 times.
✓ Branch 2 taken 17376 times.
✓ Branch 4 taken 11935 times.
✓ Branch 5 taken 17376 times.
|
46687 | } |
| 57 | |||
| 58 | 9383366 | void FileBackedBuffer::Append(const void *source, uint64_t len) { | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9383366 times.
|
9383366 | assert(source != NULL); |
| 60 | |||
| 61 | // Cannot write after Commit() | ||
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9383366 times.
|
9383366 | assert(state_ == kWriteState); |
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 9383344 times.
|
9383366 | if (len == 0) |
| 65 | 22 | return; | |
| 66 | |||
| 67 | // check the size and eventually save to file | ||
| 68 |
4/4✓ Branch 0 taken 1112193 times.
✓ Branch 1 taken 8271151 times.
✓ Branch 2 taken 11935 times.
✓ Branch 3 taken 1100258 times.
|
9383344 | if (mode_ == kMemoryMode && pos_ + len > in_memory_threshold_) { |
| 69 | // changes mode_ to kFileMode | ||
| 70 | 11935 | SaveToFile(); | |
| 71 | } | ||
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 1100258 times.
✓ Branch 1 taken 8283086 times.
|
9383344 | if (mode_ == kMemoryMode) { |
| 74 |
2/2✓ Branch 0 taken 20135 times.
✓ Branch 1 taken 1080123 times.
|
1100258 | if (buf_ == NULL) { |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20135 times.
|
20135 | assert(size_ == 0); |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20135 times.
|
20135 | assert(pos_ == 0); |
| 77 | 20135 | buf_ = reinterpret_cast<unsigned char *>(smalloc(len)); | |
| 78 | 20135 | size_ = len; | |
| 79 |
2/2✓ Branch 0 taken 91221 times.
✓ Branch 1 taken 988902 times.
|
1080123 | } else if (size_ < pos_ + len) { |
| 80 | 91221 | const uint64_t newsize = (size_ * 2 < pos_ + len) ? (pos_ + len) | |
| 81 | : (size_ * 2); | ||
| 82 | 91221 | buf_ = reinterpret_cast<unsigned char *>(srealloc(buf_, newsize)); | |
| 83 | 91221 | size_ = newsize; | |
| 84 | } | ||
| 85 | |||
| 86 | 1100258 | memcpy(buf_ + pos_, source, len); | |
| 87 | 1100258 | pos_ += len; | |
| 88 | } else { | ||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8283086 times.
|
8283086 | assert(fp_ != NULL); |
| 90 | 8283086 | const uint64_t bytes_written = fwrite(source, 1, len, fp_); | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8283086 times.
|
8283086 | if (bytes_written != len) { |
| 92 | ✗ | PANIC(kLogStderr, | |
| 93 | "could not append to temporary file %s: length %lu, " | ||
| 94 | "actually written %lu, error %d", | ||
| 95 | file_path_.c_str(), len, bytes_written, ferror(fp_)); | ||
| 96 | } | ||
| 97 | 8283086 | pos_ += len; | |
| 98 | 8283086 | size_ += len; | |
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | 29037 | void FileBackedBuffer::Commit() { | |
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29037 times.
|
29037 | assert(state_ == kWriteState); |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 11935 times.
✓ Branch 1 taken 17102 times.
|
29037 | if (mode_ == kFileMode) { |
| 106 | 11935 | const int retval = fclose(fp_); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | if (retval != 0) |
| 108 | ✗ | PANIC(kLogStderr, "could not close file after writing finished: %s", | |
| 109 | file_path_.c_str()); | ||
| 110 | 11935 | fp_ = NULL; | |
| 111 | |||
| 112 |
1/2✓ Branch 2 taken 11935 times.
✗ Branch 3 not taken.
|
11935 | mmapped_ = new MemoryMappedFile(file_path_); |
| 113 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11935 times.
|
11935 | if (!mmapped_->Map()) |
| 114 | ✗ | PANIC(kLogStderr, "could not memory-map file %s", file_path_.c_str()); | |
| 115 | } else { | ||
| 116 | // Trim memory chunk size to actual data size | ||
| 117 | 17102 | buf_ = reinterpret_cast<unsigned char *>(srealloc(buf_, pos_)); | |
| 118 | 17102 | size_ = pos_; | |
| 119 | } | ||
| 120 | |||
| 121 | 29037 | pos_ = 0; | |
| 122 | 29037 | state_ = kReadState; | |
| 123 | 29037 | } | |
| 124 | |||
| 125 | 765941 | int64_t FileBackedBuffer::Data(void **ptr, int64_t len, uint64_t pos) { | |
| 126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765941 times.
|
765941 | assert(state_ == kReadState); |
| 127 | |||
| 128 | 1531882 | const int64_t actual_len = (pos + len <= size_) | |
| 129 |
2/2✓ Branch 0 taken 13355 times.
✓ Branch 1 taken 752586 times.
|
765941 | ? len |
| 130 | 13355 | : static_cast<int64_t>(size_) | |
| 131 | 13355 | - static_cast<int64_t>(pos); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765941 times.
|
765941 | assert(actual_len >= 0); |
| 133 | |||
| 134 |
2/2✓ Branch 0 taken 28658 times.
✓ Branch 1 taken 737283 times.
|
765941 | if (mode_ == kMemoryMode) { |
| 135 | 28658 | *ptr = buf_ + pos; | |
| 136 | } else { | ||
| 137 | 737283 | *ptr = mmapped_->buffer() + pos; | |
| 138 | } | ||
| 139 | 765941 | return actual_len; | |
| 140 | } | ||
| 141 | |||
| 142 | 736926 | int64_t FileBackedBuffer::Read(void *ptr, int64_t len) { | |
| 143 | 736926 | const int64_t bytes_read = ReadP(ptr, len, pos_); | |
| 144 | 736926 | pos_ += bytes_read; | |
| 145 | 736926 | return bytes_read; | |
| 146 | } | ||
| 147 | |||
| 148 | 736926 | int64_t FileBackedBuffer::ReadP(void *ptr, int64_t len, uint64_t pos) { | |
| 149 | void *source; | ||
| 150 |
1/2✓ Branch 1 taken 736926 times.
✗ Branch 2 not taken.
|
736926 | const int64_t bytes_read = Data(&source, len, pos); |
| 151 | 736926 | memcpy(ptr, source, bytes_read); | |
| 152 | 736926 | return bytes_read; | |
| 153 | } | ||
| 154 | |||
| 155 | 28576 | void FileBackedBuffer::Rewind() { | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28576 times.
|
28576 | assert(state_ == kReadState); |
| 157 | 28576 | pos_ = 0; | |
| 158 | 28576 | } | |
| 159 | |||
| 160 | 149310 | uint64_t FileBackedBuffer::GetSize() const { | |
| 161 |
4/4✓ Branch 0 taken 301 times.
✓ Branch 1 taken 149009 times.
✓ Branch 2 taken 279 times.
✓ Branch 3 taken 22 times.
|
149310 | if (state_ == kWriteState && mode_ == kMemoryMode) |
| 162 | 279 | return pos_; | |
| 163 | 149031 | return size_; | |
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | 11935 | void FileBackedBuffer::SaveToFile() { | |
| 168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | assert(state_ == kWriteState); |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | assert(mode_ == kMemoryMode); |
| 170 | |||
| 171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | assert(fp_ == NULL); |
| 172 | 11935 | fp_ = CreateTempFile(tmp_dir_, 0644, "w", &file_path_); | |
| 173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | if (fp_ == NULL) |
| 174 | ✗ | PANIC(kLogStderr, "could not create temporary file"); | |
| 175 | |||
| 176 | 11935 | const uint64_t bytes_written = fwrite(buf_, 1, pos_, fp_); | |
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11935 times.
|
11935 | if (bytes_written != pos_) { |
| 178 | ✗ | PANIC(kLogStderr, | |
| 179 | "could not write to temporary file %s: length %lu, " | ||
| 180 | "actually written %lu, error %d", | ||
| 181 | file_path_.c_str(), pos_, bytes_written, ferror(fp_)); | ||
| 182 | } | ||
| 183 | |||
| 184 | 11935 | free(buf_); | |
| 185 | 11935 | buf_ = NULL; | |
| 186 | 11935 | size_ = pos_; | |
| 187 | 11935 | mode_ = kFileMode; | |
| 188 | 11935 | } | |
| 189 |