blob: 7f8a36c3f723054014cf34ceb005bc46261a571f [file] [log] [blame]
Alex Deymo477aec22015-03-24 23:40:48 -07001// 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 Arnold41e34742015-05-11 11:31:50 -070011#include "update_engine/utils.h"
12
Alex Deymo477aec22015-03-24 23:40:48 -070013using 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
28void AnnotatedOperation::SetNameFromFileAndChunk(
29 const string& filename, off_t chunk_offset, off_t chunk_size) {
30 name = filename;
31 if (chunk_offset != 0 || chunk_size != -1) {
32 if (chunk_size != -1) {
33 base::StringAppendF(&name, " [%" PRId64 ", %" PRId64 ")",
34 chunk_offset, chunk_offset + chunk_size);
35 } else {
36 base::StringAppendF(&name, " [%" PRId64 ", end)", chunk_offset);
37 }
38 }
39}
40
Gilad Arnold41e34742015-05-11 11:31:50 -070041bool AnnotatedOperation::SetOperationBlob(chromeos::Blob* blob, int data_fd,
42 off_t* data_file_size) {
43 TEST_AND_RETURN_FALSE(utils::PWriteAll(data_fd,
44 blob->data(),
45 blob->size(),
46 *data_file_size));
47 op.set_data_length(blob->size());
48 op.set_data_offset(*data_file_size);
49 *data_file_size += blob->size();
50 return true;
51}
52
Alex Deymo477aec22015-03-24 23:40:48 -070053string InstallOperationTypeName(
54 DeltaArchiveManifest_InstallOperation_Type op_type) {
55 switch (op_type) {
56 case DeltaArchiveManifest_InstallOperation_Type_BSDIFF:
57 return "BSDIFF";
58 case DeltaArchiveManifest_InstallOperation_Type_MOVE:
59 return "MOVE";
60 case DeltaArchiveManifest_InstallOperation_Type_REPLACE:
61 return "REPLACE";
62 case DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ:
63 return "REPLACE_BZ";
64 case DeltaArchiveManifest_InstallOperation_Type_SOURCE_COPY:
65 return "SOURCE_COPY";
66 case DeltaArchiveManifest_InstallOperation_Type_SOURCE_BSDIFF:
67 return "SOURCE_BSDIFF";
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