blob: 3f28c8dd077f9abd5dc2ffd8f3a99b90228f5ad8 [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) {
Sen Jiang8cc502d2015-08-10 10:04:54 -070042 off_t data_offset = blob_file->StoreBlob(*blob);
Sen Jiang5456c192015-08-19 15:01:16 -070043 TEST_AND_RETURN_FALSE(data_offset != -1);
Sen Jiang8cc502d2015-08-10 10:04:54 -070044 op.set_data_offset(data_offset);
Sen Jiang5456c192015-08-19 15:01:16 -070045 op.set_data_length(blob->size());
Gilad Arnold41e34742015-05-11 11:31:50 -070046 return true;
47}
48
Alex Deymoa12ee112015-08-12 22:19:32 -070049string InstallOperationTypeName(InstallOperation_Type op_type) {
Alex Deymo477aec22015-03-24 23:40:48 -070050 switch (op_type) {
Alex Deymoa12ee112015-08-12 22:19:32 -070051 case InstallOperation::BSDIFF:
Alex Deymo477aec22015-03-24 23:40:48 -070052 return "BSDIFF";
Alex Deymoa12ee112015-08-12 22:19:32 -070053 case InstallOperation::MOVE:
Alex Deymo477aec22015-03-24 23:40:48 -070054 return "MOVE";
Alex Deymoa12ee112015-08-12 22:19:32 -070055 case InstallOperation::REPLACE:
Alex Deymo477aec22015-03-24 23:40:48 -070056 return "REPLACE";
Alex Deymoa12ee112015-08-12 22:19:32 -070057 case InstallOperation::REPLACE_BZ:
Alex Deymo477aec22015-03-24 23:40:48 -070058 return "REPLACE_BZ";
Alex Deymoa12ee112015-08-12 22:19:32 -070059 case InstallOperation::SOURCE_COPY:
Alex Deymo477aec22015-03-24 23:40:48 -070060 return "SOURCE_COPY";
Alex Deymoa12ee112015-08-12 22:19:32 -070061 case InstallOperation::SOURCE_BSDIFF:
Alex Deymo477aec22015-03-24 23:40:48 -070062 return "SOURCE_BSDIFF";
Alex Deymoac6246a2015-08-13 14:00:22 -070063 case InstallOperation::ZERO:
64 return "ZERO";
65 case InstallOperation::DISCARD:
66 return "DISCARD";
Alex Deymo477aec22015-03-24 23:40:48 -070067 }
68 return "UNK";
69}
70
71std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) {
72 // For example, this prints:
73 // REPLACE_BZ 500 @3000
74 // name: /foo/bar
75 // dst: (123, 3) (127, 2)
76 os << InstallOperationTypeName(aop.op.type()) << " " << aop.op.data_length();
77 if (aop.op.data_length() > 0)
78 os << " @" << aop.op.data_offset();
79 if (!aop.name.empty()) {
80 os << std::endl << " name: " << aop.name;
81 }
82 if (aop.op.src_extents_size() != 0) {
83 os << std::endl << " src:";
84 OutputExtents(&os, aop.op.src_extents());
85 }
86 if (aop.op.dst_extents_size() != 0) {
87 os << std::endl << " dst:";
88 OutputExtents(&os, aop.op.dst_extents());
89 }
90 return os;
91}
92
93} // namespace chromeos_update_engine