Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/extent_writer.h" |
| 6 | #include <errno.h> |
| 7 | #include <unistd.h> |
| 8 | #include <algorithm> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 9 | #include "update_engine/graph_types.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 10 | #include "update_engine/utils.h" |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 11 | |
| 12 | using std::min; |
| 13 | |
| 14 | namespace chromeos_update_engine { |
| 15 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 16 | bool DirectExtentWriter::Write(const void* bytes, size_t count) { |
| 17 | if (count == 0) |
| 18 | return true; |
| 19 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
| 20 | size_t bytes_written = 0; |
| 21 | while (count - bytes_written > 0) { |
| 22 | TEST_AND_RETURN_FALSE(next_extent_index_ < extents_.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 23 | uint64_t bytes_remaining_next_extent = |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 24 | extents_[next_extent_index_].num_blocks() * block_size_ - |
| 25 | extent_bytes_written_; |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 26 | CHECK_NE(bytes_remaining_next_extent, static_cast<uint64_t>(0)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 27 | size_t bytes_to_write = |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 28 | static_cast<size_t>(min(static_cast<uint64_t>(count - bytes_written), |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 29 | bytes_remaining_next_extent)); |
| 30 | TEST_AND_RETURN_FALSE(bytes_to_write > 0); |
| 31 | |
| 32 | if (extents_[next_extent_index_].start_block() != kSparseHole) { |
| 33 | const off64_t offset = |
| 34 | extents_[next_extent_index_].start_block() * block_size_ + |
| 35 | extent_bytes_written_; |
| 36 | TEST_AND_RETURN_FALSE_ERRNO(lseek64(fd_, offset, SEEK_SET) != |
| 37 | static_cast<off64_t>(-1)); |
| 38 | TEST_AND_RETURN_FALSE( |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 39 | utils::WriteAll(fd_, c_bytes + bytes_written, bytes_to_write)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 40 | } |
| 41 | bytes_written += bytes_to_write; |
| 42 | extent_bytes_written_ += bytes_to_write; |
| 43 | if (bytes_remaining_next_extent == bytes_to_write) { |
| 44 | // We filled this extent |
| 45 | CHECK_EQ(extent_bytes_written_, |
| 46 | extents_[next_extent_index_].num_blocks() * block_size_); |
| 47 | // move to next extent |
| 48 | extent_bytes_written_ = 0; |
| 49 | next_extent_index_++; |
| 50 | } |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | } // namespace chromeos_update_engine |