delta_diff_generator.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "update_engine/payload_generator/delta_diff_generator.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <inttypes.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <algorithm>
  23. #include <memory>
  24. #include <string>
  25. #include <utility>
  26. #include <vector>
  27. #include <base/logging.h>
  28. #include "update_engine/common/utils.h"
  29. #include "update_engine/payload_consumer/delta_performer.h"
  30. #include "update_engine/payload_consumer/payload_constants.h"
  31. #include "update_engine/payload_generator/ab_generator.h"
  32. #include "update_engine/payload_generator/blob_file_writer.h"
  33. #include "update_engine/payload_generator/delta_diff_utils.h"
  34. #include "update_engine/payload_generator/full_update_generator.h"
  35. #include "update_engine/payload_generator/inplace_generator.h"
  36. #include "update_engine/payload_generator/payload_file.h"
  37. using std::string;
  38. using std::unique_ptr;
  39. using std::vector;
  40. namespace chromeos_update_engine {
  41. // bytes
  42. const size_t kRootFSPartitionSize = static_cast<size_t>(2) * 1024 * 1024 * 1024;
  43. const size_t kBlockSize = 4096; // bytes
  44. bool GenerateUpdatePayloadFile(const PayloadGenerationConfig& config,
  45. const string& output_path,
  46. const string& private_key_path,
  47. uint64_t* metadata_size) {
  48. if (!config.version.Validate()) {
  49. LOG(ERROR) << "Unsupported major.minor version: " << config.version.major
  50. << "." << config.version.minor;
  51. return false;
  52. }
  53. // Create empty payload file object.
  54. PayloadFile payload;
  55. TEST_AND_RETURN_FALSE(payload.Init(config));
  56. const string kTempFileTemplate("CrAU_temp_data.XXXXXX");
  57. string temp_file_path;
  58. int data_file_fd;
  59. TEST_AND_RETURN_FALSE(
  60. utils::MakeTempFile(kTempFileTemplate, &temp_file_path, &data_file_fd));
  61. ScopedPathUnlinker temp_file_unlinker(temp_file_path);
  62. TEST_AND_RETURN_FALSE(data_file_fd >= 0);
  63. {
  64. off_t data_file_size = 0;
  65. ScopedFdCloser data_file_fd_closer(&data_file_fd);
  66. BlobFileWriter blob_file(data_file_fd, &data_file_size);
  67. if (config.is_delta) {
  68. TEST_AND_RETURN_FALSE(config.source.partitions.size() ==
  69. config.target.partitions.size());
  70. }
  71. PartitionConfig empty_part("");
  72. for (size_t i = 0; i < config.target.partitions.size(); i++) {
  73. const PartitionConfig& old_part =
  74. config.is_delta ? config.source.partitions[i] : empty_part;
  75. const PartitionConfig& new_part = config.target.partitions[i];
  76. LOG(INFO) << "Partition name: " << new_part.name;
  77. LOG(INFO) << "Partition size: " << new_part.size;
  78. LOG(INFO) << "Block count: " << new_part.size / config.block_size;
  79. // Select payload generation strategy based on the config.
  80. unique_ptr<OperationsGenerator> strategy;
  81. if (!old_part.path.empty()) {
  82. // Delta update.
  83. if (config.version.minor == kInPlaceMinorPayloadVersion) {
  84. LOG(INFO) << "Using generator InplaceGenerator().";
  85. strategy.reset(new InplaceGenerator());
  86. } else {
  87. LOG(INFO) << "Using generator ABGenerator().";
  88. strategy.reset(new ABGenerator());
  89. }
  90. } else {
  91. LOG(INFO) << "Using generator FullUpdateGenerator().";
  92. strategy.reset(new FullUpdateGenerator());
  93. }
  94. vector<AnnotatedOperation> aops;
  95. // Generate the operations using the strategy we selected above.
  96. TEST_AND_RETURN_FALSE(strategy->GenerateOperations(
  97. config, old_part, new_part, &blob_file, &aops));
  98. // Filter the no-operations. OperationsGenerators should not output this
  99. // kind of operations normally, but this is an extra step to fix that if
  100. // happened.
  101. diff_utils::FilterNoopOperations(&aops);
  102. TEST_AND_RETURN_FALSE(payload.AddPartition(old_part, new_part, aops));
  103. }
  104. }
  105. LOG(INFO) << "Writing payload file...";
  106. // Write payload file to disk.
  107. TEST_AND_RETURN_FALSE(payload.WritePayload(
  108. output_path, temp_file_path, private_key_path, metadata_size));
  109. LOG(INFO) << "All done. Successfully created delta file with "
  110. << "metadata size = " << *metadata_size;
  111. return true;
  112. }
  113. }; // namespace chromeos_update_engine