ab_generator.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (C) 2015 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. #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_AB_GENERATOR_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_AB_GENERATOR_H_
  18. #include <string>
  19. #include <vector>
  20. #include <base/macros.h>
  21. #include <brillo/secure_blob.h>
  22. #include "update_engine/payload_consumer/payload_constants.h"
  23. #include "update_engine/payload_generator/blob_file_writer.h"
  24. #include "update_engine/payload_generator/extent_utils.h"
  25. #include "update_engine/payload_generator/filesystem_interface.h"
  26. #include "update_engine/payload_generator/operations_generator.h"
  27. #include "update_engine/payload_generator/payload_generation_config.h"
  28. #include "update_engine/update_metadata.pb.h"
  29. namespace chromeos_update_engine {
  30. // The ABGenerator is an operations generator that generates payloads using the
  31. // A-to-B operations SOURCE_COPY and SOURCE_BSDIFF introduced in the payload
  32. // minor version 2 format.
  33. class ABGenerator : public OperationsGenerator {
  34. public:
  35. ABGenerator() = default;
  36. // Generate the update payload operations for the given partition using
  37. // SOURCE_* operations, used for generating deltas for the minor version
  38. // kSourceMinorPayloadVersion. This function will generate operations in the
  39. // partition that will read blocks from the source partition in random order
  40. // and write the new image on the target partition, also possibly in random
  41. // order. The operations are stored in |aops| and should be executed in that
  42. // order. All the offsets in the operations reference the data written to
  43. // |blob_file|.
  44. bool GenerateOperations(const PayloadGenerationConfig& config,
  45. const PartitionConfig& old_part,
  46. const PartitionConfig& new_part,
  47. BlobFileWriter* blob_file,
  48. std::vector<AnnotatedOperation>* aops) override;
  49. // Split the operations in the vector of AnnotatedOperations |aops| such that
  50. // for every operation there is only one dst extent and updates |aops| with
  51. // the new list of operations. All kinds of operations are fragmented except
  52. // BSDIFF and SOURCE_BSDIFF, PUFFDIFF and BROTLI_BSDIFF operations. The
  53. // |target_part_path| is the filename of the new image, where the destination
  54. // extents refer to. The blobs of the operations in |aops| should reference
  55. // |blob_file|. |blob_file| are updated if needed.
  56. static bool FragmentOperations(const PayloadVersion& version,
  57. std::vector<AnnotatedOperation>* aops,
  58. const std::string& target_part_path,
  59. BlobFileWriter* blob_file);
  60. // Takes a vector of AnnotatedOperations |aops| and sorts them by the first
  61. // start block in their destination extents. Sets |aops| to a vector of the
  62. // sorted operations.
  63. static void SortOperationsByDestination(
  64. std::vector<AnnotatedOperation>* aops);
  65. // Takes an SOURCE_COPY install operation, |aop|, and adds one operation for
  66. // each dst extent in |aop| to |ops|. The new operations added to |ops| will
  67. // have only one dst extent. The src extents are split so the number of blocks
  68. // in the src and dst extents are equal.
  69. // E.g. we have a SOURCE_COPY operation:
  70. // src extents: [(1, 3), (5, 1), (7, 1)], dst extents: [(2, 2), (6, 3)]
  71. // Then we will get 2 new operations:
  72. // 1. src extents: [(1, 2)], dst extents: [(2, 2)]
  73. // 2. src extents: [(3, 1),(5, 1),(7, 1)], dst extents: [(6, 3)]
  74. static bool SplitSourceCopy(const AnnotatedOperation& original_aop,
  75. std::vector<AnnotatedOperation>* result_aops);
  76. // Takes a REPLACE, REPLACE_BZ or REPLACE_XZ operation |aop|, and adds one
  77. // operation for each dst extent in |aop| to |ops|. The new operations added
  78. // to |ops| will have only one dst extent each, and may be of a different
  79. // type depending on whether compression is advantageous.
  80. static bool SplitAReplaceOp(const PayloadVersion& version,
  81. const AnnotatedOperation& original_aop,
  82. const std::string& target_part,
  83. std::vector<AnnotatedOperation>* result_aops,
  84. BlobFileWriter* blob_file);
  85. // Takes a sorted (by first destination extent) vector of operations |aops|
  86. // and merges SOURCE_COPY, REPLACE, REPLACE_BZ and REPLACE_XZ, operations in
  87. // that vector.
  88. // It will merge two operations if:
  89. // - They are both REPLACE_*, or they are both SOURCE_COPY,
  90. // - Their destination blocks are contiguous.
  91. // - Their combined blocks do not exceed |chunk_blocks| blocks.
  92. // Note that unlike other methods, you can't pass a negative number in
  93. // |chunk_blocks|.
  94. static bool MergeOperations(std::vector<AnnotatedOperation>* aops,
  95. const PayloadVersion& version,
  96. size_t chunk_blocks,
  97. const std::string& target_part,
  98. BlobFileWriter* blob_file);
  99. // Takes a vector of AnnotatedOperations |aops|, adds source hash to all
  100. // operations that have src_extents.
  101. static bool AddSourceHash(std::vector<AnnotatedOperation>* aops,
  102. const std::string& source_part_path);
  103. private:
  104. // Adds the data payload for a REPLACE/REPLACE_BZ/REPLACE_XZ operation |aop|
  105. // by reading its output extents from |target_part_path| and appending a
  106. // corresponding data blob to |blob_file|. The blob will be compressed if this
  107. // is smaller than the uncompressed form, and the operation type will be set
  108. // accordingly. |*blob_file| will be updated as well. If the operation happens
  109. // to have the right type and already points to a data blob, nothing is
  110. // written. Caller should only set type and data blob if it's valid.
  111. static bool AddDataAndSetType(AnnotatedOperation* aop,
  112. const PayloadVersion& version,
  113. const std::string& target_part_path,
  114. BlobFileWriter* blob_file);
  115. DISALLOW_COPY_AND_ASSIGN(ABGenerator);
  116. };
  117. } // namespace chromeos_update_engine
  118. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_AB_GENERATOR_H_