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