MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / Write

Method Write

lib/mdflib/mdflib/src/at4block.cpp:166–228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164}
165
166uint64_t At4Block::Write(std::streambuf& buffer) {
167 const bool update = FilePosition() > 0;
168 if (update) {
169 return block_size_;
170 }
171 ByteArray data_buffer;
172 try {
173 path filename = u8path(filename_);
174 if (!fs::exists(filename) && IsEmbedded()) {
175 MDF_ERROR() << "Attachment File doesn't exist. File: " << filename_;
176 }
177 if (fs::exists(filename)) {
178 if (const auto md5 = CreateMd5FileChecksum(filename_, md5_); md5) {
179 flags_ |= At4Flags::kUsingMd5;
180 }
181 original_size_ = file_size(filename);
182 if (IsEmbedded() && IsCompressed()) {
183 if (const bool compress = Deflate(filename_, data_buffer); !compress) {
184 MDF_ERROR() << "Compress failure. File: " << filename;
185 return 0;
186 }
187 } else if (IsEmbedded()) {
188 if (const auto read = FileToBuffer(filename_, data_buffer); !read) {
189 MDF_ERROR() << "File to buffer failure. File: " << filename;
190 return 0;
191 }
192 }
193 }
194 } catch (const std::exception& err) {
195 MDF_ERROR() << "Attachment File error. Error: " << err.what()
196 << ", File: " << filename_;
197 return 0;
198 }
199
200 nof_bytes_ = data_buffer.size();
201
202 block_type_ = "##AT";
203 block_length_ = 24 + (4 * 8) + 2 + 2 + 4 + 16 + 8 + 8 + nof_bytes_;
204 link_list_.resize(4, 0);
205
206 WriteTx4(buffer, kIndexFilename, filename_);
207 WriteTx4(buffer, kIndexType, file_type_);
208 WriteMdComment(buffer, kIndexMd);
209
210 uint64_t bytes = MdfBlock::Write(buffer);
211 bytes += WriteNumber(buffer, flags_);
212 bytes += WriteNumber(buffer, creator_index_);
213 bytes += WriteBytes(buffer, 4);
214 if (md5_.size() == 16) {
215 bytes += WriteByte(buffer, md5_);
216 } else {
217 bytes += WriteBytes(buffer, 16);
218 }
219 bytes += WriteNumber(buffer, original_size_);
220 bytes += WriteNumber(buffer, nof_bytes_);
221 data_position_ = FilePosition();
222 if (nof_bytes_ > 0) {
223 bytes += WriteByte(buffer, data_buffer);

Callers

nothing calls this directly

Calls 7

CreateMd5FileChecksumFunction · 0.85
DeflateFunction · 0.85
FileToBufferFunction · 0.85
WriteBytesFunction · 0.85
WriteByteFunction · 0.85
sizeMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected