• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const PayloadGenerationConfig& config,
54                                const string& output_path,
55                                const string& private_key_path,
56                                uint64_t* metadata_size) {
57   if (!config.version.Validate()) {
58     LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
59                << "." << config.version.minor;
60     return false;
61   }
62 
63   // Create empty payload file object.
64   PayloadFile payload;
65   TEST_AND_RETURN_FALSE(payload.Init(config));
66 
67   const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
68   string temp_file_path;
69   int data_file_fd;
70   TEST_AND_RETURN_FALSE(
71       utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
72   ScopedPathUnlinker temp_file_unlinker(temp_file_path);
73   TEST_AND_RETURN_FALSE(data_file_fd >= 0);
74 
75   {
76     off_t data_file_size = 0;
77     ScopedFdCloser data_file_fd_closer(&data_file_fd);
78     BlobFileWriter blob_file(data_file_fd, &data_file_size);
79     if (config.is_delta) {
80       TEST_AND_RETURN_FALSE(config.source.partitions.size() ==
81                             config.target.partitions.size());
82     }
83     PartitionConfig empty_part("");
84     for (size_t i = 0; i < config.target.partitions.size(); i++) {
85       const PartitionConfig& old_part =
86           config.is_delta ? config.source.partitions[i] : empty_part;
87       const PartitionConfig& new_part = config.target.partitions[i];
88       LOG(INFO) << "Partition name: " << new_part.name;
89       LOG(INFO) << "Partition size: " << new_part.size;
90       LOG(INFO) << "Block count: " << new_part.size / config.block_size;
91 
92       // Select payload generation strategy based on the config.
93       unique_ptr<OperationsGenerator> strategy;
94       if (!old_part.path.empty()) {
95         // Delta update.
96         if (config.version.minor == kInPlaceMinorPayloadVersion) {
97           LOG(INFO) << "Using generator InplaceGenerator().";
98           strategy.reset(new InplaceGenerator());
99         } else {
100           LOG(INFO) << "Using generator ABGenerator().";
101           strategy.reset(new ABGenerator());
102         }
103       } else {
104         LOG(INFO) << "Using generator FullUpdateGenerator().";
105         strategy.reset(new FullUpdateGenerator());
106       }
107 
108       vector<AnnotatedOperation> aops;
109       // Generate the operations using the strategy we selected above.
110       TEST_AND_RETURN_FALSE(strategy->GenerateOperations(
111           config, old_part, new_part, &blob_file, &aops));
112 
113       // Filter the no-operations. OperationsGenerators should not output this
114       // kind of operations normally, but this is an extra step to fix that if
115       // happened.
116       diff_utils::FilterNoopOperations(&aops);
117 
118       TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops));
119     }
120   }
121 
122   LOG(INFO) << "Writing payload file...";
123   // Write payload file to disk.
124   TEST_AND_RETURN_FALSE(payload.WritePayload(
125       output_path, temp_file_path, private_key_path, metadata_size));
126 
127   LOG(INFO) << "All done. Successfully created delta file with "
128             << "metadata size = " << *metadata_size;
129   return true;
130 }
131 
132 };  // namespace chromeos_update_engine
133