1 //
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 //
16
17 #include "update_engine/payload_generator/delta_diff_generator.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 #include <base/logging.h>
32
33 #include "update_engine/common/utils.h"
34 #include "update_engine/payload_consumer/delta_performer.h"
35 #include "update_engine/payload_consumer/payload_constants.h"
36 #include "update_engine/payload_generator/ab_generator.h"
37 #include "update_engine/payload_generator/blob_file_writer.h"
38 #include "update_engine/payload_generator/delta_diff_utils.h"
39 #include "update_engine/payload_generator/full_update_generator.h"
40 #include "update_engine/payload_generator/inplace_generator.h"
41 #include "update_engine/payload_generator/payload_file.h"
42
43 using std::string;
44 using std::unique_ptr;
45 using std::vector;
46
47 namespace chromeos_update_engine {
48
49 // bytes
50 const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
51 const size_t kBlockSize = 4096; // bytes
52
GenerateUpdatePayloadFile(const PayloadGenerationConfig & config,const string & output_path,const string & private_key_path,uint64_t * metadata_size)53 bool GenerateUpdatePayloadFile(
54 const PayloadGenerationConfig& config,
55 const string& output_path,
56 const string& private_key_path,
57 uint64_t* metadata_size) {
58 if (!config.version.Validate()) {
59 LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
60 << "." << config.version.minor;
61 return false;
62 }
63
64 // Create empty payload file object.
65 PayloadFile payload;
66 TEST_AND_RETURN_FALSE(payload.Init(config));
67
68 const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
69 string temp_file_path;
70 int data_file_fd;
71 TEST_AND_RETURN_FALSE(
72 utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
73 ScopedPathUnlinker temp_file_unlinker(temp_file_path);
74 TEST_AND_RETURN_FALSE(data_file_fd >= 0);
75
76 {
77 off_t data_file_size = 0;
78 ScopedFdCloser data_file_fd_closer(&data_file_fd);
79 BlobFileWriter blob_file(data_file_fd, &data_file_size);
80 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 if (!old_part.path.empty()) {
96 // Delta update.
97 if (config.version.minor == kInPlaceMinorPayloadVersion) {
98 LOG(INFO) << "Using generator InplaceGenerator().";
99 strategy.reset(new InplaceGenerator());
100 } else {
101 LOG(INFO) << "Using generator ABGenerator().";
102 strategy.reset(new ABGenerator());
103 }
104 } else {
105 LOG(INFO) << "Using generator FullUpdateGenerator().";
106 strategy.reset(new FullUpdateGenerator());
107 }
108
109 vector<AnnotatedOperation> aops;
110 // Generate the operations using the strategy we selected above.
111 TEST_AND_RETURN_FALSE(strategy->GenerateOperations(config,
112 old_part,
113 new_part,
114 &blob_file,
115 &aops));
116
117 // Filter the no-operations. OperationsGenerators should not output this
118 // kind of operations normally, but this is an extra step to fix that if
119 // happened.
120 diff_utils::FilterNoopOperations(&aops);
121
122 TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops));
123 }
124 }
125
126 LOG(INFO) << "Writing payload file...";
127 // Write payload file to disk.
128 TEST_AND_RETURN_FALSE(payload.WritePayload(output_path, temp_file_path,
129 private_key_path, metadata_size));
130
131 LOG(INFO) << "All done. Successfully created delta file with "
132 << "metadata size = " << *metadata_size;
133 return true;
134 }
135
136 }; // namespace chromeos_update_engine
137