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