blob: 70350ed996ab5b5cdd71a70fdf85f1562d3f4510 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Alex Deymo477aec22015-03-24 23:40:48 -070016
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
Gilad Arnold41e34742015-05-11 11:31:50 -070023#include "update_engine/utils.h"
24
Alex Deymo477aec22015-03-24 23:40:48 -070025using 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
Sen Jiang8cc502d2015-08-10 10:04:54 -070040bool AnnotatedOperation::SetOperationBlob(chromeos::Blob* blob,
41 BlobFileWriter* blob_file) {
Gilad Arnold41e34742015-05-11 11:31:50 -070042 op.set_data_length(blob->size());
Sen Jiang8cc502d2015-08-10 10:04:54 -070043 off_t data_offset = blob_file->StoreBlob(*blob);
44 if (data_offset == -1)
45 return false;
46 op.set_data_offset(data_offset);
Gilad Arnold41e34742015-05-11 11:31:50 -070047 return true;
48}
49
Alex Deymoa12ee112015-08-12 22:19:32 -070050string InstallOperationTypeName(InstallOperation_Type op_type) {
Alex Deymo477aec22015-03-24 23:40:48 -070051 switch (op_type) {
Alex Deymoa12ee112015-08-12 22:19:32 -070052 case InstallOperation::BSDIFF:
Alex Deymo477aec22015-03-24 23:40:48 -070053 return "BSDIFF";
Alex Deymoa12ee112015-08-12 22:19:32 -070054 case InstallOperation::MOVE:
Alex Deymo477aec22015-03-24 23:40:48 -070055 return "MOVE";
Alex Deymoa12ee112015-08-12 22:19:32 -070056 case InstallOperation::REPLACE:
Alex Deymo477aec22015-03-24 23:40:48 -070057 return "REPLACE";
Alex Deymoa12ee112015-08-12 22:19:32 -070058 case InstallOperation::REPLACE_BZ:
Alex Deymo477aec22015-03-24 23:40:48 -070059 return "REPLACE_BZ";
Alex Deymoa12ee112015-08-12 22:19:32 -070060 case InstallOperation::SOURCE_COPY:
Alex Deymo477aec22015-03-24 23:40:48 -070061 return "SOURCE_COPY";
Alex Deymoa12ee112015-08-12 22:19:32 -070062 case InstallOperation::SOURCE_BSDIFF:
Alex Deymo477aec22015-03-24 23:40:48 -070063 return "SOURCE_BSDIFF";
Alex Deymoac6246a2015-08-13 14:00:22 -070064 case InstallOperation::ZERO:
65 return "ZERO";
66 case InstallOperation::DISCARD:
67 return "DISCARD";
Alex Deymo477aec22015-03-24 23:40:48 -070068 }
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