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