OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 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/split_file_writer.h" | 5 #include "update_engine/split_file_writer.h" |
6 #include <algorithm> | 6 #include <algorithm> |
7 | 7 |
8 using std::min; | 8 using std::min; |
9 | 9 |
10 namespace chromeos_update_engine { | 10 namespace chromeos_update_engine { |
(...skipping 25 matching lines...) Expand all Loading... |
36 if (rc != static_cast<int>(count)) { | 36 if (rc != static_cast<int>(count)) { |
37 LOG(ERROR) << "Not all bytes successfully written to file."; | 37 LOG(ERROR) << "Not all bytes successfully written to file."; |
38 return -EIO; | 38 return -EIO; |
39 } | 39 } |
40 return rc; | 40 return rc; |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 ssize_t SplitFileWriter::Write(const void* bytes, size_t count) { | 44 ssize_t SplitFileWriter::Write(const void* bytes, size_t count) { |
45 const size_t original_count = count; | 45 const size_t original_count = count; |
46 | 46 |
47 // This first block is trying to read the first sizeof(uint64_t) | 47 // This first block is trying to read the first sizeof(uint64_t) |
48 // bytes, which are the number of bytes that should be written | 48 // bytes, which are the number of bytes that should be written |
49 // to the first FileWriter. | 49 // to the first FileWriter. |
50 if (bytes_received_ < static_cast<off_t>(sizeof(uint64_t))) { | 50 if (bytes_received_ < static_cast<off_t>(sizeof(uint64_t))) { |
51 // Write more to the initial buffer | 51 // Write more to the initial buffer |
52 size_t bytes_to_copy = min(static_cast<off_t>(count), | 52 size_t bytes_to_copy = min(static_cast<off_t>(count), |
53 static_cast<off_t>(sizeof(first_length_buf_)) - | 53 static_cast<off_t>(sizeof(first_length_buf_)) - |
54 bytes_received_); | 54 bytes_received_); |
55 memcpy(&first_length_buf_[bytes_received_], bytes, bytes_to_copy); | 55 memcpy(&first_length_buf_[bytes_received_], bytes, bytes_to_copy); |
56 bytes_received_ += bytes_to_copy; | 56 bytes_received_ += bytes_to_copy; |
(...skipping 14 matching lines...) Expand all Loading... |
71 } | 71 } |
72 CHECK_GE(bytes_received_, static_cast<off_t>(sizeof(uint64_t))); | 72 CHECK_GE(bytes_received_, static_cast<off_t>(sizeof(uint64_t))); |
73 | 73 |
74 // This block of code is writing to the first FileWriter. | 74 // This block of code is writing to the first FileWriter. |
75 if (bytes_received_ - static_cast<off_t>(sizeof(uint64_t)) < first_length_) { | 75 if (bytes_received_ - static_cast<off_t>(sizeof(uint64_t)) < first_length_) { |
76 // Write to first FileWriter | 76 // Write to first FileWriter |
77 size_t bytes_to_write = min( | 77 size_t bytes_to_write = min( |
78 first_length_ - | 78 first_length_ - |
79 (bytes_received_ - static_cast<off_t>(sizeof(uint64_t))), | 79 (bytes_received_ - static_cast<off_t>(sizeof(uint64_t))), |
80 static_cast<off_t>(count)); | 80 static_cast<off_t>(count)); |
81 | 81 |
82 int rc = PerformWrite(first_file_writer_, bytes, bytes_to_write); | 82 int rc = PerformWrite(first_file_writer_, bytes, bytes_to_write); |
83 if (rc != static_cast<int>(bytes_to_write)) | 83 if (rc != static_cast<int>(bytes_to_write)) |
84 return rc; | 84 return rc; |
85 | 85 |
86 bytes_received_ += bytes_to_write; | 86 bytes_received_ += bytes_to_write; |
87 count -= bytes_to_write; | 87 count -= bytes_to_write; |
88 bytes = static_cast<const void*>( | 88 bytes = static_cast<const void*>( |
89 static_cast<const char*>(bytes) + bytes_to_write); | 89 static_cast<const char*>(bytes) + bytes_to_write); |
90 if (count == 0) | 90 if (count == 0) |
91 return original_count; | 91 return original_count; |
92 } | 92 } |
93 | 93 |
94 CHECK_GE(static_cast<off_t>(bytes_received_), | 94 CHECK_GE(static_cast<off_t>(bytes_received_), |
95 first_length_ + static_cast<off_t>(sizeof(uint64_t))); | 95 first_length_ + static_cast<off_t>(sizeof(uint64_t))); |
96 // Write to second FileWriter | 96 // Write to second FileWriter |
97 int rc = PerformWrite(second_file_writer_, bytes, count); | 97 int rc = PerformWrite(second_file_writer_, bytes, count); |
98 if (rc != static_cast<int>(count)) | 98 if (rc != static_cast<int>(count)) |
99 return rc; | 99 return rc; |
100 return original_count; | 100 return original_count; |
101 } | 101 } |
102 | 102 |
103 int SplitFileWriter::Close() { | 103 int SplitFileWriter::Close() { |
104 int first_result = first_file_writer_->Close(); | 104 int first_result = first_file_writer_->Close(); |
105 if (first_result < 0) | 105 if (first_result < 0) |
106 LOG(ERROR) << "Error Close()ing first file."; | 106 LOG(ERROR) << "Error Close()ing first file."; |
107 int second_result = second_file_writer_->Close(); | 107 int second_result = second_file_writer_->Close(); |
108 if (second_result < 0) | 108 if (second_result < 0) |
109 LOG(ERROR) << "Error Close()ing second file."; | 109 LOG(ERROR) << "Error Close()ing second file."; |
110 // Return error if either had returned error. | 110 // Return error if either had returned error. |
111 return second_result < 0 ? second_result : first_result; | 111 return second_result < 0 ? second_result : first_result; |
112 } | 112 } |
113 | 113 |
114 } // namespace chromeos_update_engine | 114 } // namespace chromeos_update_engine |
OLD | NEW |