annotated_operation.cc revision 7d9bd921a8f92338ca44a6ee368c58450da594df
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//
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
23#include "update_engine/utils.h"
24
25using std::string;
26
27namespace chromeos_update_engine {
28
29namespace {
30// Output the list of extents as (start_block, num_blocks) in the passed output
31// stream.
32void 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
40bool AnnotatedOperation::SetOperationBlob(chromeos::Blob* blob,
41                                          BlobFileWriter* blob_file) {
42  off_t data_offset = blob_file->StoreBlob(*blob);
43  TEST_AND_RETURN_FALSE(data_offset != -1);
44  op.set_data_offset(data_offset);
45  op.set_data_length(blob->size());
46  return true;
47}
48
49string InstallOperationTypeName(InstallOperation_Type op_type) {
50  switch (op_type) {
51    case InstallOperation::BSDIFF:
52      return "BSDIFF";
53    case InstallOperation::MOVE:
54      return "MOVE";
55    case InstallOperation::REPLACE:
56      return "REPLACE";
57    case InstallOperation::REPLACE_BZ:
58      return "REPLACE_BZ";
59    case InstallOperation::SOURCE_COPY:
60      return "SOURCE_COPY";
61    case InstallOperation::SOURCE_BSDIFF:
62      return "SOURCE_BSDIFF";
63    case InstallOperation::ZERO:
64      return "ZERO";
65    case InstallOperation::DISCARD:
66      return "DISCARD";
67    case InstallOperation::REPLACE_XZ:
68      return "REPLACE_XZ";
69  }
70  return "UNK";
71}
72
73std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) {
74  // For example, this prints:
75  // REPLACE_BZ 500 @3000
76  //   name: /foo/bar
77  //    dst: (123, 3) (127, 2)
78  os << InstallOperationTypeName(aop.op.type()) << " "  << aop.op.data_length();
79  if (aop.op.data_length() > 0)
80    os << " @" << aop.op.data_offset();
81  if (!aop.name.empty()) {
82    os << std::endl << "  name: " << aop.name;
83  }
84  if (aop.op.src_extents_size() != 0) {
85    os << std::endl << "   src:";
86    OutputExtents(&os, aop.op.src_extents());
87  }
88  if (aop.op.dst_extents_size() != 0) {
89    os << std::endl << "   dst:";
90    OutputExtents(&os, aop.op.dst_extents());
91  }
92  return os;
93}
94
95}  // namespace chromeos_update_engine
96