delta_diff_utils.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_DELTA_DIFF_UTILS_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. #include <brillo/secure_blob.h>
  22. #include <puffin/puffdiff.h>
  23. #include "update_engine/payload_generator/annotated_operation.h"
  24. #include "update_engine/payload_generator/extent_ranges.h"
  25. #include "update_engine/payload_generator/payload_generation_config.h"
  26. #include "update_engine/update_metadata.pb.h"
  27. namespace chromeos_update_engine {
  28. namespace diff_utils {
  29. // Create operations in |aops| to produce all the blocks in the |new_part|
  30. // partition using the filesystem opened in that PartitionConfig.
  31. // It uses the files reported by the filesystem in |old_part| and the data
  32. // blocks in that partition (if available) to determine the best way to compress
  33. // the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary
  34. // data to |blob_file|. |hard_chunk_blocks| and |soft_chunk_blocks| are the hard
  35. // and soft chunk limits in number of blocks respectively. The soft chunk limit
  36. // is used to split MOVE and SOURCE_COPY operations and REPLACE_BZ of zeroed
  37. // blocks, while the hard limit is used to split a file when generating other
  38. // operations. A value of -1 in |hard_chunk_blocks| means whole files.
  39. bool DeltaReadPartition(std::vector<AnnotatedOperation>* aops,
  40. const PartitionConfig& old_part,
  41. const PartitionConfig& new_part,
  42. ssize_t hard_chunk_blocks,
  43. size_t soft_chunk_blocks,
  44. const PayloadVersion& version,
  45. BlobFileWriter* blob_file);
  46. // Create operations in |aops| for identical blocks that moved around in the old
  47. // and new partition and also handle zeroed blocks. The old and new partition
  48. // are stored in the |old_part| and |new_part| files and have |old_num_blocks|
  49. // and |new_num_blocks| respectively. The maximum operation size is
  50. // |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the
  51. // produced operations are stored in the |blob_file|.
  52. // The collections |old_visited_blocks| and |new_visited_blocks| state what
  53. // blocks already have operations reading or writing them and only operations
  54. // for unvisited blocks are produced by this function updating both collections
  55. // with the used blocks.
  56. bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops,
  57. const std::string& old_part,
  58. const std::string& new_part,
  59. size_t old_num_blocks,
  60. size_t new_num_blocks,
  61. ssize_t chunk_blocks,
  62. const PayloadVersion& version,
  63. BlobFileWriter* blob_file,
  64. ExtentRanges* old_visited_blocks,
  65. ExtentRanges* new_visited_blocks,
  66. ExtentRanges* old_zero_blocks);
  67. // For a given file |name| append operations to |aops| to produce it in the
  68. // |new_part|. The file will be split in chunks of |chunk_blocks| blocks each
  69. // or treated as a single chunk if |chunk_blocks| is -1. The file data is
  70. // stored in |new_part| in the blocks described by |new_extents| and, if it
  71. // exists, the old version exists in |old_part| in the blocks described by
  72. // |old_extents|. The operations added to |aops| reference the data blob
  73. // in the |blob_file|. |old_deflates| and |new_deflates| are all deflate
  74. // locations in |old_part| and |new_part|. Returns true on success.
  75. bool DeltaReadFile(std::vector<AnnotatedOperation>* aops,
  76. const std::string& old_part,
  77. const std::string& new_part,
  78. const std::vector<Extent>& old_extents,
  79. const std::vector<Extent>& new_extents,
  80. const std::vector<puffin::BitExtent>& old_deflates,
  81. const std::vector<puffin::BitExtent>& new_deflates,
  82. const std::string& name,
  83. ssize_t chunk_blocks,
  84. const PayloadVersion& version,
  85. BlobFileWriter* blob_file);
  86. // Reads the blocks |old_extents| from |old_part| (if it exists) and the
  87. // |new_extents| from |new_part| and determines the smallest way to encode
  88. // this |new_extents| for the diff. It stores necessary data in |out_data| and
  89. // fills in |out_op|. If there's no change in old and new files, it creates a
  90. // MOVE or SOURCE_COPY operation. If there is a change, the smallest of the
  91. // operations allowed in the given |version| (REPLACE, REPLACE_BZ, BSDIFF,
  92. // SOURCE_BSDIFF, or PUFFDIFF) wins.
  93. // |new_extents| must not be empty. |old_deflates| and |new_deflates| are all
  94. // the deflate locations in |old_part| and |new_part|. Returns true on success.
  95. bool ReadExtentsToDiff(const std::string& old_part,
  96. const std::string& new_part,
  97. const std::vector<Extent>& old_extents,
  98. const std::vector<Extent>& new_extents,
  99. const std::vector<puffin::BitExtent>& old_deflates,
  100. const std::vector<puffin::BitExtent>& new_deflates,
  101. const PayloadVersion& version,
  102. brillo::Blob* out_data,
  103. InstallOperation* out_op);
  104. // Generates the best allowed full operation to produce |new_data|. The allowed
  105. // operations are based on |payload_version|. The operation blob will be stored
  106. // in |out_blob| and the resulting operation type in |out_type|. Returns whether
  107. // a valid full operation was generated.
  108. bool GenerateBestFullOperation(const brillo::Blob& new_data,
  109. const PayloadVersion& version,
  110. brillo::Blob* out_blob,
  111. InstallOperation::Type* out_type);
  112. // Returns whether |op_type| is one of the REPLACE full operations.
  113. bool IsAReplaceOperation(InstallOperation::Type op_type);
  114. // Returns true if an operation with type |op_type| has no |src_extents|.
  115. bool IsNoSourceOperation(InstallOperation::Type op_type);
  116. // Returns true if |op| is a no-op operation that doesn't do any useful work
  117. // (e.g., a move operation that copies blocks onto themselves).
  118. bool IsNoopOperation(const InstallOperation& op);
  119. // Filters all the operations that are no-op, maintaining the relative order
  120. // of the rest of the operations.
  121. void FilterNoopOperations(std::vector<AnnotatedOperation>* ops);
  122. bool InitializePartitionInfo(const PartitionConfig& partition,
  123. PartitionInfo* info);
  124. // Compare two AnnotatedOperations by the start block of the first Extent in
  125. // their destination extents.
  126. bool CompareAopsByDestination(AnnotatedOperation first_aop,
  127. AnnotatedOperation second_aop);
  128. // Returns whether the filesystem is an ext[234] filesystem. In case of failure,
  129. // such as if the file |device| doesn't exists or can't be read, it returns
  130. // false.
  131. bool IsExtFilesystem(const std::string& device);
  132. // Returns the max number of threads to process the files(chunks) in parallel.
  133. size_t GetMaxThreads();
  134. // Returns the old file which file name has the shortest levenshtein distance to
  135. // |new_file_name|.
  136. FilesystemInterface::File GetOldFile(
  137. const std::map<std::string, FilesystemInterface::File>& old_files_map,
  138. const std::string& new_file_name);
  139. } // namespace diff_utils
  140. } // namespace chromeos_update_engine
  141. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_