annotated_operation.cc revision aea4c1cea20dda7ae7e85fc8924a2d784f70d806
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  op.set_data_length(blob->size());
43  off_t data_offset = blob_file->StoreBlob(*blob);
44  if (data_offset == -1)
45    return false;
46  op.set_data_offset(data_offset);
47  return true;
48}
49
50string InstallOperationTypeName(InstallOperation_Type op_type) {
51  switch (op_type) {
52    case InstallOperation::BSDIFF:
53      return "BSDIFF";
54    case InstallOperation::MOVE:
55      return "MOVE";
56    case InstallOperation::REPLACE:
57      return "REPLACE";
58    case InstallOperation::REPLACE_BZ:
59      return "REPLACE_BZ";
60    case InstallOperation::SOURCE_COPY:
61      return "SOURCE_COPY";
62    case InstallOperation::SOURCE_BSDIFF:
63      return "SOURCE_BSDIFF";
64    case InstallOperation::ZERO:
65      return "ZERO";
66    case InstallOperation::DISCARD:
67      return "DISCARD";
68  }
69  return "UNK";
70}
71
72std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) {
73  // For example, this prints:
74  // REPLACE_BZ 500 @3000
75  //   name: /foo/bar
76  //    dst: (123, 3) (127, 2)
77  os << InstallOperationTypeName(aop.op.type()) << " "  << aop.op.data_length();
78  if (aop.op.data_length() > 0)
79    os << " @" << aop.op.data_offset();
80  if (!aop.name.empty()) {
81    os << std::endl << "  name: " << aop.name;
82  }
83  if (aop.op.src_extents_size() != 0) {
84    os << std::endl << "   src:";
85    OutputExtents(&os, aop.op.src_extents());
86  }
87  if (aop.op.dst_extents_size() != 0) {
88    os << std::endl << "   dst:";
89    OutputExtents(&os, aop.op.dst_extents());
90  }
91  return os;
92}
93
94}  // namespace chromeos_update_engine
95