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 #include <base/threading/simple_thread.h>
33 
34 #include "update_engine/common/utils.h"
35 #include "update_engine/payload_consumer/delta_performer.h"
36 #include "update_engine/payload_consumer/payload_constants.h"
37 #include "update_engine/payload_generator/ab_generator.h"
38 #include "update_engine/payload_generator/annotated_operation.h"
39 #include "update_engine/payload_generator/blob_file_writer.h"
40 #include "update_engine/payload_generator/delta_diff_utils.h"
41 #include "update_engine/payload_generator/full_update_generator.h"
42 #include "update_engine/payload_generator/payload_file.h"
43 
44 using std::string;
45 using std::unique_ptr;
46 using std::vector;
47 
48 namespace chromeos_update_engine {
49 
50 // bytes
51 const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
52 const size_t kBlockSize = 4096;  // bytes
53 
54 class PartitionProcessor : public base::DelegateSimpleThread::Delegate {
55  public:
PartitionProcessor(const PayloadGenerationConfig & config,const PartitionConfig & old_part,const PartitionConfig & new_part,BlobFileWriter * file_writer,std::vector<AnnotatedOperation> * aops,std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy)56   explicit PartitionProcessor(
57       const PayloadGenerationConfig& config,
58       const PartitionConfig& old_part,
59       const PartitionConfig& new_part,
60       BlobFileWriter* file_writer,
61       std::vector<AnnotatedOperation>* aops,
62       std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy)
63       : config_(config),
64         old_part_(old_part),
65         new_part_(new_part),
66         file_writer_(file_writer),
67         aops_(aops),
68         strategy_(std::move(strategy)) {}
69   PartitionProcessor(PartitionProcessor&&) noexcept = default;
Run()70   void Run() override {
71     LOG(INFO) << "Started an async task to process partition "
72               << old_part_.name;
73     bool success = strategy_->GenerateOperations(
74         config_, old_part_, new_part_, file_writer_, aops_);
75     if (!success) {
76       // ABORT the entire process, so that developer can look
77       // at recent logs and diagnose what happened
78       LOG(FATAL) << "GenerateOperations(" << old_part_.name << ", "
79                  << new_part_.name << ") failed";
80     }
81   }
82 
83  private:
84   const PayloadGenerationConfig& config_;
85   const PartitionConfig& old_part_;
86   const PartitionConfig& new_part_;
87   BlobFileWriter* file_writer_;
88   std::vector<AnnotatedOperation>* aops_;
89   std::unique_ptr<chromeos_update_engine::OperationsGenerator> strategy_;
90   DISALLOW_COPY_AND_ASSIGN(PartitionProcessor);
91 };
92 
GenerateUpdatePayloadFile(const PayloadGenerationConfig & config,const string & output_path,const string & private_key_path,uint64_t * metadata_size)93 bool GenerateUpdatePayloadFile(const PayloadGenerationConfig& config,
94                                const string& output_path,
95                                const string& private_key_path,
96                                uint64_t* metadata_size) {
97   if (!config.version.Validate()) {
98     LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
99                << "." << config.version.minor;
100     return false;
101   }
102 
103   // Create empty payload file object.
104   PayloadFile payload;
105   TEST_AND_RETURN_FALSE(payload.Init(config));
106 
107   const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
108   string temp_file_path;
109   int data_file_fd;
110   TEST_AND_RETURN_FALSE(
111       utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
112   ScopedPathUnlinker temp_file_unlinker(temp_file_path);
113   TEST_AND_RETURN_FALSE(data_file_fd >= 0);
114 
115   {
116     off_t data_file_size = 0;
117     ScopedFdCloser data_file_fd_closer(&data_file_fd);
118     BlobFileWriter blob_file(data_file_fd, &data_file_size);
119     if (config.is_delta) {
120       TEST_AND_RETURN_FALSE(config.source.partitions.size() ==
121                             config.target.partitions.size());
122     }
123     PartitionConfig empty_part("");
124     std::vector<std::vector<AnnotatedOperation>> all_aops;
125     all_aops.resize(config.target.partitions.size());
126     std::vector<PartitionProcessor> partition_tasks{};
127     auto thread_count = std::min<int>(diff_utils::GetMaxThreads(),
128                                       config.target.partitions.size());
129     base::DelegateSimpleThreadPool thread_pool{"partition-thread-pool",
130                                                thread_count};
131     for (size_t i = 0; i < config.target.partitions.size(); i++) {
132       const PartitionConfig& old_part =
133           config.is_delta ? config.source.partitions[i] : empty_part;
134       const PartitionConfig& new_part = config.target.partitions[i];
135       LOG(INFO) << "Partition name: " << new_part.name;
136       LOG(INFO) << "Partition size: " << new_part.size;
137       LOG(INFO) << "Block count: " << new_part.size / config.block_size;
138 
139       // Select payload generation strategy based on the config.
140       unique_ptr<OperationsGenerator> strategy;
141       if (!old_part.path.empty()) {
142         // Delta update.
143         LOG(INFO) << "Using generator ABGenerator().";
144         strategy.reset(new ABGenerator());
145       } else {
146         LOG(INFO) << "Using generator FullUpdateGenerator().";
147         strategy.reset(new FullUpdateGenerator());
148       }
149 
150       // Generate the operations using the strategy we selected above.
151       partition_tasks.push_back(PartitionProcessor(config,
152                                                    old_part,
153                                                    new_part,
154                                                    &blob_file,
155                                                    &all_aops[i],
156                                                    std::move(strategy)));
157     }
158     thread_pool.Start();
159     for (auto& processor : partition_tasks) {
160       thread_pool.AddWork(&processor);
161     }
162     thread_pool.JoinAll();
163 
164     for (size_t i = 0; i < config.target.partitions.size(); i++) {
165       const PartitionConfig& old_part =
166           config.is_delta ? config.source.partitions[i] : empty_part;
167       const PartitionConfig& new_part = config.target.partitions[i];
168       TEST_AND_RETURN_FALSE(
169           payload.AddPartition(old_part, new_part, std::move(all_aops[i])));
170     }
171   }
172 
173   LOG(INFO) << "Writing payload file...";
174   // Write payload file to disk.
175   TEST_AND_RETURN_FALSE(payload.WritePayload(
176       output_path, temp_file_path, private_key_path, metadata_size));
177 
178   LOG(INFO) << "All done. Successfully created delta file with "
179             << "metadata size = " << *metadata_size;
180   return true;
181 }
182 
183 };  // namespace chromeos_update_engine
184