blob: a140d21cf3cd279eaa20a6cc099320b82dbcbd4f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo161c4a12014-05-16 15:56:21 -070017#include "update_engine/payload_generator/delta_diff_generator.h"
Darin Petkov880335c2010-10-01 15:52:53 -070018
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070019#include <errno.h>
20#include <fcntl.h>
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070021#include <inttypes.h>
Darin Petkov880335c2010-10-01 15:52:53 -070022#include <sys/stat.h>
23#include <sys/types.h>
24
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070025#include <algorithm>
Ben Chan02f7c1d2014-10-18 15:18:02 -070026#include <memory>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070027#include <string>
28#include <utility>
29#include <vector>
Darin Petkov880335c2010-10-01 15:52:53 -070030
31#include <base/logging.h>
Darin Petkov880335c2010-10-01 15:52:53 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/utils.h"
34#include "update_engine/payload_consumer/delta_performer.h"
35#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo14158572015-06-13 03:37:08 -070036#include "update_engine/payload_generator/ab_generator.h"
Sen Jiang8cc502d2015-08-10 10:04:54 -070037#include "update_engine/payload_generator/blob_file_writer.h"
Alex Deymo14158572015-06-13 03:37:08 -070038#include "update_engine/payload_generator/delta_diff_utils.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070039#include "update_engine/payload_generator/full_update_generator.h"
Allie Woodcd514b52015-02-19 13:56:07 -080040#include "update_engine/payload_generator/inplace_generator.h"
Alex Deymo14158572015-06-13 03:37:08 -070041#include "update_engine/payload_generator/payload_file.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070042
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070043using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070044using std::unique_ptr;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045using std::vector;
46
47namespace chromeos_update_engine {
48
Chris Sosaf586b012013-05-21 13:33:42 -070049// bytes
50const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
Allie Woodcd514b52015-02-19 13:56:07 -080051const size_t kBlockSize = 4096; // bytes
Alex Deymo9b244df2015-03-11 21:51:18 -070052
Alex Deymo477aec22015-03-24 23:40:48 -070053bool GenerateUpdatePayloadFile(
Alex Deymof1cbe172015-03-05 15:58:37 -080054 const PayloadGenerationConfig& config,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070055 const string& output_path,
Jay Srinivasan738fdf32012-12-07 17:40:54 -080056 const string& private_key_path,
57 uint64_t* metadata_size) {
Alex Deymoa4073ef2016-03-22 23:40:53 -070058 if (!config.version.Validate()) {
59 LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
60 << "." << config.version.minor;
61 return false;
62 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070063
Alex Deymo14158572015-06-13 03:37:08 -070064 // Create empty payload file object.
65 PayloadFile payload;
66 TEST_AND_RETURN_FALSE(payload.Init(config));
Don Garrettb8dd1d92013-11-22 17:40:02 -080067
Sen Jiang981eb112015-08-25 17:03:18 -070068 const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
69 string temp_file_path;
Alex Deymo14158572015-06-13 03:37:08 -070070 int data_file_fd;
71 TEST_AND_RETURN_FALSE(
72 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
Sen Jiang981eb112015-08-25 17:03:18 -070073 ScopedPathUnlinker temp_file_unlinker(temp_file_path);
Alex Deymo14158572015-06-13 03:37:08 -070074 TEST_AND_RETURN_FALSE(data_file_fd >= 0);
75
76 {
Sen Jiang981eb112015-08-25 17:03:18 -070077 off_t data_file_size = 0;
Alex Deymo14158572015-06-13 03:37:08 -070078 ScopedFdCloser data_file_fd_closer(&data_file_fd);
Sen Jiang8cc502d2015-08-10 10:04:54 -070079 BlobFileWriter blob_file(data_file_fd, &data_file_size);
Sen Jiang981eb112015-08-25 17:03:18 -070080 if (config.is_delta) {
81 TEST_AND_RETURN_FALSE(config.source.partitions.size() ==
82 config.target.partitions.size());
83 }
84 PartitionConfig empty_part("");
85 for (size_t i = 0; i < config.target.partitions.size(); i++) {
86 const PartitionConfig& old_part =
87 config.is_delta ? config.source.partitions[i] : empty_part;
88 const PartitionConfig& new_part = config.target.partitions[i];
89 LOG(INFO) << "Partition name: " << new_part.name;
90 LOG(INFO) << "Partition size: " << new_part.size;
91 LOG(INFO) << "Block count: " << new_part.size / config.block_size;
92
93 // Select payload generation strategy based on the config.
94 unique_ptr<OperationsGenerator> strategy;
95 // We don't efficiently support deltas on squashfs. For now, we will
96 // produce full operations in that case.
97 if (!old_part.path.empty() &&
Sen Jiangdcbc0ae2016-03-18 15:33:19 -070098 !diff_utils::IsSquashfs4Filesystem(new_part.path)) {
Sen Jiang981eb112015-08-25 17:03:18 -070099 // Delta update.
Alex Deymoa4073ef2016-03-22 23:40:53 -0700100 if (config.version.minor == kInPlaceMinorPayloadVersion) {
Sen Jiang981eb112015-08-25 17:03:18 -0700101 LOG(INFO) << "Using generator InplaceGenerator().";
102 strategy.reset(new InplaceGenerator());
Alex Deymoa4073ef2016-03-22 23:40:53 -0700103 } else {
Sen Jiang981eb112015-08-25 17:03:18 -0700104 LOG(INFO) << "Using generator ABGenerator().";
105 strategy.reset(new ABGenerator());
Sen Jiang981eb112015-08-25 17:03:18 -0700106 }
107 } else {
108 LOG(INFO) << "Using generator FullUpdateGenerator().";
109 strategy.reset(new FullUpdateGenerator());
110 }
111
112 vector<AnnotatedOperation> aops;
113 // Generate the operations using the strategy we selected above.
114 TEST_AND_RETURN_FALSE(strategy->GenerateOperations(config,
115 old_part,
116 new_part,
117 &blob_file,
118 &aops));
119
120 // Filter the no-operations. OperationsGenerators should not output this
121 // kind of operations normally, but this is an extra step to fix that if
122 // happened.
123 diff_utils::FilterNoopOperations(&aops);
124
125 TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops));
126 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700127 }
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700128
Sen Jiang981eb112015-08-25 17:03:18 -0700129 LOG(INFO) << "Writing payload file...";
Alex Deymo14158572015-06-13 03:37:08 -0700130 // Write payload file to disk.
Sen Jiang70a6ab02015-08-28 13:23:27 -0700131 TEST_AND_RETURN_FALSE(payload.WritePayload(output_path, temp_file_path,
132 private_key_path, metadata_size));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700133
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800134 LOG(INFO) << "All done. Successfully created delta file with "
135 << "metadata size = " << *metadata_size;
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700136 return true;
137}
138
139}; // namespace chromeos_update_engine