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 | |
| 11 | using std::string; |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
| 15 | namespace { |
| 16 | // Output the list of extents as (start_block, num_blocks) in the passed output |
| 17 | // stream. |
| 18 | void OutputExtents(std::ostream* os, |
| 19 | const google::protobuf::RepeatedPtrField<Extent>& extents) { |
| 20 | for (const auto& extent : extents) { |
| 21 | *os << " (" << extent.start_block() << ", " << extent.num_blocks() << ")"; |
| 22 | } |
| 23 | } |
| 24 | } // namespace |
| 25 | |
| 26 | void AnnotatedOperation::SetNameFromFileAndChunk( |
| 27 | const string& filename, off_t chunk_offset, off_t chunk_size) { |
| 28 | name = filename; |
| 29 | if (chunk_offset != 0 || chunk_size != -1) { |
| 30 | if (chunk_size != -1) { |
| 31 | base::StringAppendF(&name, " [%" PRId64 ", %" PRId64 ")", |
| 32 | chunk_offset, chunk_offset + chunk_size); |
| 33 | } else { |
| 34 | base::StringAppendF(&name, " [%" PRId64 ", end)", chunk_offset); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | string InstallOperationTypeName( |
| 40 | DeltaArchiveManifest_InstallOperation_Type op_type) { |
| 41 | switch (op_type) { |
| 42 | case DeltaArchiveManifest_InstallOperation_Type_BSDIFF: |
| 43 | return "BSDIFF"; |
| 44 | case DeltaArchiveManifest_InstallOperation_Type_MOVE: |
| 45 | return "MOVE"; |
| 46 | case DeltaArchiveManifest_InstallOperation_Type_REPLACE: |
| 47 | return "REPLACE"; |
| 48 | case DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ: |
| 49 | return "REPLACE_BZ"; |
| 50 | case DeltaArchiveManifest_InstallOperation_Type_SOURCE_COPY: |
| 51 | return "SOURCE_COPY"; |
| 52 | case DeltaArchiveManifest_InstallOperation_Type_SOURCE_BSDIFF: |
| 53 | return "SOURCE_BSDIFF"; |
| 54 | } |
| 55 | return "UNK"; |
| 56 | } |
| 57 | |
| 58 | std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) { |
| 59 | // For example, this prints: |
| 60 | // REPLACE_BZ 500 @3000 |
| 61 | // name: /foo/bar |
| 62 | // dst: (123, 3) (127, 2) |
| 63 | os << InstallOperationTypeName(aop.op.type()) << " " << aop.op.data_length(); |
| 64 | if (aop.op.data_length() > 0) |
| 65 | os << " @" << aop.op.data_offset(); |
| 66 | if (!aop.name.empty()) { |
| 67 | os << std::endl << " name: " << aop.name; |
| 68 | } |
| 69 | if (aop.op.src_extents_size() != 0) { |
| 70 | os << std::endl << " src:"; |
| 71 | OutputExtents(&os, aop.op.src_extents()); |
| 72 | } |
| 73 | if (aop.op.dst_extents_size() != 0) { |
| 74 | os << std::endl << " dst:"; |
| 75 | OutputExtents(&os, aop.op.dst_extents()); |
| 76 | } |
| 77 | return os; |
| 78 | } |
| 79 | |
| 80 | } // namespace chromeos_update_engine |