blob: e28fe851d3426339b3712bd2ec2ad92ea9d63d9f [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
Alex Deymo39910dc2015-11-09 17:04:30 -080023#include "update_engine/common/utils.h"
24#include "update_engine/payload_consumer/payload_constants.h"
Gilad Arnold41e34742015-05-11 11:31:50 -070025
Alex Deymo477aec22015-03-24 23:40:48 -070026namespace chromeos_update_engine {
27
28namespace {
29// Output the list of extents as (start_block, num_blocks) in the passed output
30// stream.
31void OutputExtents(std::ostream* os,
32 const google::protobuf::RepeatedPtrField<Extent>& extents) {
33 for (const auto& extent : extents) {
34 *os << " (" << extent.start_block() << ", " << extent.num_blocks() << ")";
35 }
36}
37} // namespace
38
Alex Deymo726aeca2016-04-05 19:20:46 -070039bool AnnotatedOperation::SetOperationBlob(const brillo::Blob& blob,
Sen Jiang8cc502d2015-08-10 10:04:54 -070040 BlobFileWriter* blob_file) {
Sen Jiang4108c322016-04-12 13:18:33 -070041 if (blob.empty()) {
42 op.clear_data_offset();
43 op.clear_data_length();
44 return true;
45 }
Alex Deymo726aeca2016-04-05 19:20:46 -070046 off_t data_offset = blob_file->StoreBlob(blob);
Sen Jiang5456c192015-08-19 15:01:16 -070047 TEST_AND_RETURN_FALSE(data_offset != -1);
Sen Jiang8cc502d2015-08-10 10:04:54 -070048 op.set_data_offset(data_offset);
Alex Deymo726aeca2016-04-05 19:20:46 -070049 op.set_data_length(blob.size());
Gilad Arnold41e34742015-05-11 11:31:50 -070050 return true;
51}
52
Alex Deymo477aec22015-03-24 23:40:48 -070053std::ostream& operator<<(std::ostream& os, const AnnotatedOperation& aop) {
54 // For example, this prints:
55 // REPLACE_BZ 500 @3000
56 // name: /foo/bar
57 // dst: (123, 3) (127, 2)
58 os << InstallOperationTypeName(aop.op.type()) << " " << aop.op.data_length();
59 if (aop.op.data_length() > 0)
60 os << " @" << aop.op.data_offset();
61 if (!aop.name.empty()) {
62 os << std::endl << " name: " << aop.name;
63 }
64 if (aop.op.src_extents_size() != 0) {
65 os << std::endl << " src:";
66 OutputExtents(&os, aop.op.src_extents());
67 }
68 if (aop.op.dst_extents_size() != 0) {
69 os << std::endl << " dst:";
70 OutputExtents(&os, aop.op.dst_extents());
71 }
72 return os;
73}
74
75} // namespace chromeos_update_engine