1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_kvs/alignment.h"
16 
17 #include "pw_status/try.h"
18 
19 namespace pw {
20 
Write(std::span<const std::byte> data)21 StatusWithSize AlignedWriter::Write(std::span<const std::byte> data) {
22   while (!data.empty()) {
23     size_t to_copy = std::min(write_size_ - bytes_in_buffer_, data.size());
24 
25     std::memcpy(&buffer_[bytes_in_buffer_], data.data(), to_copy);
26     PW_TRY_WITH_SIZE(AddBytesToBuffer(to_copy));
27     data = data.subspan(to_copy);
28   }
29 
30   return StatusWithSize(bytes_written_);
31 }
32 
Flush()33 StatusWithSize AlignedWriter::Flush() {
34   Status status;
35 
36   // If data remains in the buffer, pad it to the alignment size and flush the
37   // remaining data.
38   if (bytes_in_buffer_ != 0u) {
39     const size_t remaining_bytes = AlignUp(bytes_in_buffer_, alignment_bytes_);
40     std::memset(&buffer_[bytes_in_buffer_],
41                 int(kPadByte),
42                 remaining_bytes - bytes_in_buffer_);
43     status = output_.Write(buffer_, remaining_bytes).status();
44 
45     bytes_written_ += remaining_bytes;  // Include padding in the total.
46     bytes_in_buffer_ = 0;
47   }
48 
49   const StatusWithSize result(status, bytes_written_);
50   bytes_written_ = 0;
51   return result;
52 }
53 
Write(Input & input,size_t size)54 StatusWithSize AlignedWriter::Write(Input& input, size_t size) {
55   while (size > 0u) {
56     const size_t to_read = std::min(write_size_ - bytes_in_buffer_, size);
57     StatusWithSize result = input.Read(buffer_ + bytes_in_buffer_, to_read);
58     if (!result.ok()) {
59       return StatusWithSize(result.status(), bytes_written_);
60     }
61     PW_TRY_WITH_SIZE(AddBytesToBuffer(to_read));
62     size -= result.size();
63   }
64 
65   return StatusWithSize(bytes_written_);
66 }
67 
AddBytesToBuffer(size_t bytes_added)68 StatusWithSize AlignedWriter::AddBytesToBuffer(size_t bytes_added) {
69   bytes_in_buffer_ += bytes_added;
70 
71   // If the buffer is full, write it out.
72   if (bytes_in_buffer_ == write_size_) {
73     StatusWithSize result = output_.Write(buffer_, write_size_);
74 
75     // Always use write_size_ for the bytes written. If there was an error
76     // assume the space was written or at least disturbed.
77     bytes_written_ += write_size_;
78     bytes_in_buffer_ = 0;
79 
80     if (!result.ok()) {
81       return StatusWithSize(result.status(), bytes_written_);
82     }
83   }
84 
85   return StatusWithSize(bytes_written_);
86 }
87 
88 }  // namespace pw
89