Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 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 | // |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/payload_generator/annotated_operation.h" |
| 18 | |
| 19 | #include <base/format_macros.h> |
| 20 | #include <base/strings/string_number_conversions.h> |
| 21 | #include <base/strings/stringprintf.h> |
| 22 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 23 | #include "update_engine/utils.h" |
| 24 | |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | namespace { |
| 30 | // Output the list of extents as (start_block, num_blocks) in the passed output |
| 31 | // stream. |
| 32 | void OutputExtents(std::ostream* os, |
| 33 | const google::protobuf::RepeatedPtrField<Extent>& extents) { |
| 34 | for (const auto& extent : extents) { |
| 35 | *os << " (" << extent.start_block() << ", " << extent.num_blocks() << ")"; |
| 36 | } |
| 37 | } |
| 38 | } // namespace |
| 39 | |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 40 | bool AnnotatedOperation::SetOperationBlob(chromeos::Blob* blob, |
| 41 | BlobFileWriter* blob_file) { |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 42 | off_t data_offset = blob_file->StoreBlob(*blob); |
Sen Jiang | 5456c19 | 2015-08-19 15:01:16 -0700 | [diff] [blame^] | 43 | TEST_AND_RETURN_FALSE(data_offset != -1); |
Sen Jiang | 8cc502d | 2015-08-10 10:04:54 -0700 | [diff] [blame] | 44 | op.set_data_offset(data_offset); |
Sen Jiang | 5456c19 | 2015-08-19 15:01:16 -0700 | [diff] [blame^] | 45 | op.set_data_length(blob->size()); |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 49 | string InstallOperationTypeName(InstallOperation_Type op_type) { |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 50 | switch (op_type) { |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 51 | case InstallOperation::BSDIFF: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 52 | return "BSDIFF"; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 53 | case InstallOperation::MOVE: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 54 | return "MOVE"; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 55 | case InstallOperation::REPLACE: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 56 | return "REPLACE"; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 57 | case InstallOperation::REPLACE_BZ: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 58 | return "REPLACE_BZ"; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 59 | case InstallOperation::SOURCE_COPY: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 60 | return "SOURCE_COPY"; |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 61 | case InstallOperation::SOURCE_BSDIFF: |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 62 | return "SOURCE_BSDIFF"; |
Alex Deymo | ac6246a | 2015-08-13 14:00:22 -0700 | [diff] [blame] | 63 | case InstallOperation::ZERO: |
| 64 | return "ZERO"; |
| 65 | case InstallOperation::DISCARD: |
| 66 | return "DISCARD"; |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame] | 67 | } |
| 68 | return "UNK"; |
| 69 | } |
| 70 | |
| 71 | std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) { |
| 72 | // For example, this prints: |
| 73 | // REPLACE_BZ 500 @3000 |
| 74 | // name: /foo/bar |
| 75 | // dst: (123, 3) (127, 2) |
| 76 | os << InstallOperationTypeName(aop.op.type()) << " " << aop.op.data_length(); |
| 77 | if (aop.op.data_length() > 0) |
| 78 | os << " @" << aop.op.data_offset(); |
| 79 | if (!aop.name.empty()) { |
| 80 | os << std::endl << " name: " << aop.name; |
| 81 | } |
| 82 | if (aop.op.src_extents_size() != 0) { |
| 83 | os << std::endl << " src:"; |
| 84 | OutputExtents(&os, aop.op.src_extents()); |
| 85 | } |
| 86 | if (aop.op.dst_extents_size() != 0) { |
| 87 | os << std::endl << " dst:"; |
| 88 | OutputExtents(&os, aop.op.dst_extents()); |
| 89 | } |
| 90 | return os; |
| 91 | } |
| 92 | |
| 93 | } // namespace chromeos_update_engine |