xref: /aosp_15_r20/system/update_engine/payload_consumer/extent_reader.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2017 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/extent_reader.h"
18*5a923131SAndroid Build Coastguard Worker 
19*5a923131SAndroid Build Coastguard Worker #include <algorithm>
20*5a923131SAndroid Build Coastguard Worker 
21*5a923131SAndroid Build Coastguard Worker #include <sys/types.h>
22*5a923131SAndroid Build Coastguard Worker #include <unistd.h>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
25*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/payload_constants.h"
26*5a923131SAndroid Build Coastguard Worker 
27*5a923131SAndroid Build Coastguard Worker using google::protobuf::RepeatedPtrField;
28*5a923131SAndroid Build Coastguard Worker 
29*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
30*5a923131SAndroid Build Coastguard Worker 
Init(FileDescriptorPtr fd,const RepeatedPtrField<Extent> & extents,uint32_t block_size)31*5a923131SAndroid Build Coastguard Worker bool DirectExtentReader::Init(FileDescriptorPtr fd,
32*5a923131SAndroid Build Coastguard Worker                               const RepeatedPtrField<Extent>& extents,
33*5a923131SAndroid Build Coastguard Worker                               uint32_t block_size) {
34*5a923131SAndroid Build Coastguard Worker   fd_ = fd;
35*5a923131SAndroid Build Coastguard Worker   extents_ = extents;
36*5a923131SAndroid Build Coastguard Worker   block_size_ = block_size;
37*5a923131SAndroid Build Coastguard Worker   cur_extent_ = extents_.begin();
38*5a923131SAndroid Build Coastguard Worker 
39*5a923131SAndroid Build Coastguard Worker   extents_upper_bounds_.reserve(extents_.size() + 1);
40*5a923131SAndroid Build Coastguard Worker   // We add this pad as the first element to not bother with boundary checks
41*5a923131SAndroid Build Coastguard Worker   // later.
42*5a923131SAndroid Build Coastguard Worker   extents_upper_bounds_.emplace_back(0);
43*5a923131SAndroid Build Coastguard Worker   for (const auto& extent : extents_) {
44*5a923131SAndroid Build Coastguard Worker     total_size_ += extent.num_blocks() * block_size_;
45*5a923131SAndroid Build Coastguard Worker     extents_upper_bounds_.emplace_back(total_size_);
46*5a923131SAndroid Build Coastguard Worker   }
47*5a923131SAndroid Build Coastguard Worker   return true;
48*5a923131SAndroid Build Coastguard Worker }
49*5a923131SAndroid Build Coastguard Worker 
Seek(uint64_t offset)50*5a923131SAndroid Build Coastguard Worker bool DirectExtentReader::Seek(uint64_t offset) {
51*5a923131SAndroid Build Coastguard Worker   TEST_AND_RETURN_FALSE(offset <= total_size_);
52*5a923131SAndroid Build Coastguard Worker   if (offset_ == offset) {
53*5a923131SAndroid Build Coastguard Worker     return true;
54*5a923131SAndroid Build Coastguard Worker   }
55*5a923131SAndroid Build Coastguard Worker   // The first item is zero and upper_bound never returns it because it always
56*5a923131SAndroid Build Coastguard Worker   // return the item which is greater than the given value.
57*5a923131SAndroid Build Coastguard Worker   auto extent_idx =
58*5a923131SAndroid Build Coastguard Worker       std::upper_bound(
59*5a923131SAndroid Build Coastguard Worker           extents_upper_bounds_.begin(), extents_upper_bounds_.end(), offset) -
60*5a923131SAndroid Build Coastguard Worker       extents_upper_bounds_.begin() - 1;
61*5a923131SAndroid Build Coastguard Worker   cur_extent_ = std::next(extents_.begin(), extent_idx);
62*5a923131SAndroid Build Coastguard Worker   offset_ = offset;
63*5a923131SAndroid Build Coastguard Worker   cur_extent_bytes_read_ = offset_ - extents_upper_bounds_[extent_idx];
64*5a923131SAndroid Build Coastguard Worker   return true;
65*5a923131SAndroid Build Coastguard Worker }
66*5a923131SAndroid Build Coastguard Worker 
Read(void * buffer,size_t count)67*5a923131SAndroid Build Coastguard Worker bool DirectExtentReader::Read(void* buffer, size_t count) {
68*5a923131SAndroid Build Coastguard Worker   auto bytes = reinterpret_cast<uint8_t*>(buffer);
69*5a923131SAndroid Build Coastguard Worker   uint64_t bytes_read = 0;
70*5a923131SAndroid Build Coastguard Worker   while (bytes_read < count) {
71*5a923131SAndroid Build Coastguard Worker     if (cur_extent_ == extents_.end()) {
72*5a923131SAndroid Build Coastguard Worker       TEST_AND_RETURN_FALSE(bytes_read == count);
73*5a923131SAndroid Build Coastguard Worker     }
74*5a923131SAndroid Build Coastguard Worker     uint64_t cur_extent_bytes_left =
75*5a923131SAndroid Build Coastguard Worker         cur_extent_->num_blocks() * block_size_ - cur_extent_bytes_read_;
76*5a923131SAndroid Build Coastguard Worker     uint64_t bytes_to_read =
77*5a923131SAndroid Build Coastguard Worker         std::min(count - bytes_read, cur_extent_bytes_left);
78*5a923131SAndroid Build Coastguard Worker 
79*5a923131SAndroid Build Coastguard Worker     ssize_t out_bytes_read;
80*5a923131SAndroid Build Coastguard Worker     TEST_AND_RETURN_FALSE(utils::ReadAll(
81*5a923131SAndroid Build Coastguard Worker         fd_,
82*5a923131SAndroid Build Coastguard Worker         bytes + bytes_read,
83*5a923131SAndroid Build Coastguard Worker         bytes_to_read,
84*5a923131SAndroid Build Coastguard Worker         cur_extent_->start_block() * block_size_ + cur_extent_bytes_read_,
85*5a923131SAndroid Build Coastguard Worker         &out_bytes_read));
86*5a923131SAndroid Build Coastguard Worker     TEST_AND_RETURN_FALSE(out_bytes_read ==
87*5a923131SAndroid Build Coastguard Worker                           static_cast<ssize_t>(bytes_to_read));
88*5a923131SAndroid Build Coastguard Worker 
89*5a923131SAndroid Build Coastguard Worker     bytes_read += bytes_to_read;
90*5a923131SAndroid Build Coastguard Worker     cur_extent_bytes_read_ += bytes_to_read;
91*5a923131SAndroid Build Coastguard Worker     offset_ += bytes_to_read;
92*5a923131SAndroid Build Coastguard Worker     if (cur_extent_bytes_read_ == cur_extent_->num_blocks() * block_size_) {
93*5a923131SAndroid Build Coastguard Worker       // We have to advance the cur_extent_;
94*5a923131SAndroid Build Coastguard Worker       cur_extent_++;
95*5a923131SAndroid Build Coastguard Worker       cur_extent_bytes_read_ = 0;
96*5a923131SAndroid Build Coastguard Worker     }
97*5a923131SAndroid Build Coastguard Worker   }
98*5a923131SAndroid Build Coastguard Worker   return true;
99*5a923131SAndroid Build Coastguard Worker }
100*5a923131SAndroid Build Coastguard Worker 
101*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
102