Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 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 | // |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/extent_writer.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 22 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/utils.h" |
| 26 | #include "update_engine/payload_consumer/payload_constants.h" |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 27 | |
| 28 | using std::min; |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 32 | bool DirectExtentWriter::Write(const void* bytes, size_t count) { |
| 33 | if (count == 0) |
| 34 | return true; |
| 35 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
| 36 | size_t bytes_written = 0; |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 37 | while (bytes_written < count) { |
| 38 | TEST_AND_RETURN_FALSE(cur_extent_ != extents_.end()); |
| 39 | uint64_t bytes_remaining_cur_extent = |
| 40 | cur_extent_->num_blocks() * block_size_ - extent_bytes_written_; |
| 41 | CHECK_NE(bytes_remaining_cur_extent, static_cast<uint64_t>(0)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 42 | size_t bytes_to_write = |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 43 | static_cast<size_t>(min(static_cast<uint64_t>(count - bytes_written), |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 44 | bytes_remaining_cur_extent)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 45 | TEST_AND_RETURN_FALSE(bytes_to_write > 0); |
| 46 | |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 47 | if (cur_extent_->start_block() != kSparseHole) { |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 48 | const off64_t offset = |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 49 | cur_extent_->start_block() * block_size_ + extent_bytes_written_; |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 50 | TEST_AND_RETURN_FALSE_ERRNO(fd_->Seek(offset, SEEK_SET) != |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 51 | static_cast<off64_t>(-1)); |
| 52 | TEST_AND_RETURN_FALSE( |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 53 | utils::WriteAll(fd_, c_bytes + bytes_written, bytes_to_write)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 54 | } |
| 55 | bytes_written += bytes_to_write; |
| 56 | extent_bytes_written_ += bytes_to_write; |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 57 | if (bytes_remaining_cur_extent == bytes_to_write) { |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 58 | // We filled this extent |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 59 | CHECK_EQ(extent_bytes_written_, cur_extent_->num_blocks() * block_size_); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 60 | // move to next extent |
| 61 | extent_bytes_written_ = 0; |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 62 | cur_extent_++; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | } // namespace chromeos_update_engine |