GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/file_backed_buffer.cc
Date: 2026-06-21 02:37:04
Exec Total Coverage
Lines: 102 110 92.7%
Branches: 53 78 67.9%

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 10686 FileBackedBuffer *FileBackedBuffer::Create(uint64_t in_memory_threshold,
21 const std::string &tmp_dir) {
22
1/2
✓ Branch 2 taken 10686 times.
✗ Branch 3 not taken.
10686 return new FileBackedBuffer(in_memory_threshold, tmp_dir);
23 }
24
25 10686 FileBackedBuffer::FileBackedBuffer(uint64_t in_memory_threshold,
26 10686 const std::string &tmp_dir)
27 10686 : in_memory_threshold_(in_memory_threshold)
28 10686 , tmp_dir_(tmp_dir)
29 10686 , state_(kWriteState)
30 10686 , mode_(kMemoryMode)
31 10686 , size_(0)
32 10686 , buf_(NULL)
33 10686 , pos_(0)
34 10686 , fp_(NULL)
35
1/2
✓ Branch 2 taken 10686 times.
✗ Branch 3 not taken.
10686 , file_path_("")
36 21372 , mmapped_(NULL) { }
37
38 21372 FileBackedBuffer::~FileBackedBuffer() {
39 10686 free(buf_);
40
2/2
✓ Branch 0 taken 6345 times.
✓ Branch 1 taken 4341 times.
10686 if (mode_ != kFileMode)
41 6345 return;
42
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 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 4341 mmapped_->Unmap();
50
1/2
✓ Branch 0 taken 4341 times.
✗ Branch 1 not taken.
4341 delete mmapped_;
51 }
52 4341 const int retval = unlink(file_path_.c_str());
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 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 4341 times.
✓ Branch 2 taken 6345 times.
✓ Branch 4 taken 4341 times.
✓ Branch 5 taken 6345 times.
17031 }
57
58 3394140 void FileBackedBuffer::Append(const void *source, uint64_t len) {
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3394140 times.
3394140 assert(source != NULL);
60
61 // Cannot write after Commit()
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3394140 times.
3394140 assert(state_ == kWriteState);
63
64
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 3394120 times.
3394140 if (len == 0)
65 20 return;
66
67 // check the size and eventually save to file
68
4/4
✓ Branch 0 taken 402403 times.
✓ Branch 1 taken 2991717 times.
✓ Branch 2 taken 4341 times.
✓ Branch 3 taken 398062 times.
3394120 if (mode_ == kMemoryMode && pos_ + len > in_memory_threshold_) {
69 // changes mode_ to kFileMode
70 4341 SaveToFile();
71 }
72
73
2/2
✓ Branch 0 taken 398062 times.
✓ Branch 1 taken 2996058 times.
3394120 if (mode_ == kMemoryMode) {
74
2/2
✓ Branch 0 taken 7319 times.
✓ Branch 1 taken 390743 times.
398062 if (buf_ == NULL) {
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7319 times.
7319 assert(size_ == 0);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7319 times.
7319 assert(pos_ == 0);
77 7319 buf_ = reinterpret_cast<unsigned char *>(smalloc(len));
78 7319 size_ = len;
79
2/2
✓ Branch 0 taken 33043 times.
✓ Branch 1 taken 357700 times.
390743 } else if (size_ < pos_ + len) {
80 33043 const uint64_t newsize = (size_ * 2 < pos_ + len) ? (pos_ + len)
81 : (size_ * 2);
82 33043 buf_ = reinterpret_cast<unsigned char *>(srealloc(buf_, newsize));
83 33043 size_ = newsize;
84 }
85
86 398062 memcpy(buf_ + pos_, source, len);
87 398062 pos_ += len;
88 } else {
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2996058 times.
2996058 assert(fp_ != NULL);
90 2996058 const uint64_t bytes_written = fwrite(source, 1, len, fp_);
91
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2996041 times.
2996058 if (bytes_written != len) {
92 17 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 2996041 pos_ += len;
98 2996041 size_ += len;
99 }
100 }
101
102 10575 void FileBackedBuffer::Commit() {
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10575 times.
10575 assert(state_ == kWriteState);
104
105
2/2
✓ Branch 0 taken 4341 times.
✓ Branch 1 taken 6234 times.
10575 if (mode_ == kFileMode) {
106 4341 const int retval = fclose(fp_);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 if (retval != 0)
108 PANIC(kLogStderr, "could not close file after writing finished: %s",
109 file_path_.c_str());
110 4341 fp_ = NULL;
111
112
1/2
✓ Branch 2 taken 4341 times.
✗ Branch 3 not taken.
4341 mmapped_ = new MemoryMappedFile(file_path_);
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4341 times.
4341 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 6234 buf_ = reinterpret_cast<unsigned char *>(srealloc(buf_, pos_));
118 6234 size_ = pos_;
119 }
120
121 10575 pos_ = 0;
122 10575 state_ = kReadState;
123 10575 }
124
125 277187 int64_t FileBackedBuffer::Data(void **ptr, int64_t len, uint64_t pos) {
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277187 times.
277187 assert(state_ == kReadState);
127
128 554374 const int64_t actual_len = (pos + len <= size_)
129
2/2
✓ Branch 0 taken 4963 times.
✓ Branch 1 taken 272224 times.
277187 ? len
130 4963 : static_cast<int64_t>(size_)
131 4963 - static_cast<int64_t>(pos);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277187 times.
277187 assert(actual_len >= 0);
133
134
2/2
✓ Branch 0 taken 10462 times.
✓ Branch 1 taken 266725 times.
277187 if (mode_ == kMemoryMode) {
135 10462 *ptr = buf_ + pos;
136 } else {
137 266725 *ptr = mmapped_->buffer() + pos;
138 }
139 277187 return actual_len;
140 }
141
142 266632 int64_t FileBackedBuffer::Read(void *ptr, int64_t len) {
143 266632 const int64_t bytes_read = ReadP(ptr, len, pos_);
144 266632 pos_ += bytes_read;
145 266632 return bytes_read;
146 }
147
148 266632 int64_t FileBackedBuffer::ReadP(void *ptr, int64_t len, uint64_t pos) {
149 void *source;
150
1/2
✓ Branch 1 taken 266632 times.
✗ Branch 2 not taken.
266632 const int64_t bytes_read = Data(&source, len, pos);
151 266632 memcpy(ptr, source, bytes_read);
152 266632 return bytes_read;
153 }
154
155 10336 void FileBackedBuffer::Rewind() {
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10336 times.
10336 assert(state_ == kReadState);
157 10336 pos_ = 0;
158 10336 }
159
160 54078 uint64_t FileBackedBuffer::GetSize() const {
161
4/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 53933 times.
✓ Branch 2 taken 125 times.
✓ Branch 3 taken 20 times.
54078 if (state_ == kWriteState && mode_ == kMemoryMode)
162 125 return pos_;
163 53953 return size_;
164 }
165
166
167 4341 void FileBackedBuffer::SaveToFile() {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 assert(state_ == kWriteState);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 assert(mode_ == kMemoryMode);
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 assert(fp_ == NULL);
172 4341 fp_ = CreateTempFile(tmp_dir_, 0644, "w", &file_path_);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 if (fp_ == NULL)
174 PANIC(kLogStderr, "could not create temporary file");
175
176 4341 const uint64_t bytes_written = fwrite(buf_, 1, pos_, fp_);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4341 times.
4341 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 4341 free(buf_);
185 4341 buf_ = NULL;
186 4341 size_ = pos_;
187 4341 mode_ = kFileMode;
188 4341 }
189