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