delta_diff_utils.h revision 896fdbaa1cc9a422ac36ef455d02c3cef1f27816
1// Copyright 2015 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
6#define UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
7
8#include <string>
9#include <vector>
10
11#include <chromeos/secure_blob.h>
12
13#include "update_engine/payload_generator/annotated_operation.h"
14#include "update_engine/payload_generator/extent_ranges.h"
15#include "update_engine/payload_generator/payload_generation_config.h"
16#include "update_engine/update_metadata.pb.h"
17
18namespace chromeos_update_engine {
19
20namespace diff_utils {
21
22// Create operations in |aops| to produce all the blocks in the |new_part|
23// partition using the filesystem opened in that PartitionConfig.
24// It uses the files reported by the filesystem in |old_part| and the data
25// blocks in that partition (if available) to determine the best way to compress
26// the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary
27// data to the end of |data_fd| updating |data_file_size| accordingly.
28bool DeltaReadPartition(std::vector<AnnotatedOperation>* aops,
29                        const PartitionConfig& old_part,
30                        const PartitionConfig& new_part,
31                        off_t chunk_blocks,
32                        int data_fd,
33                        off_t* data_file_size,
34                        bool src_ops_allowed);
35
36// Create operations in |aops| for identical blocks that moved around in the old
37// and new partition and also handle zeroed blocks. The old and new partition
38// are stored in the |old_part| and |new_part| files and have |old_num_blocks|
39// and |new_num_blocks| respectively. The maximum operation size is
40// |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the
41// produced operations are stored in the |data_fd| file whose size is updated
42// in the value pointed by |data_file_size|.
43// The collections |old_visited_blocks| and |new_visited_blocks| state what
44// blocks already have operations reading or writing them and only operations
45// for unvisited blocks are produced by this function updating both collections
46// with the used blocks.
47bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops,
48                             const std::string& old_part,
49                             const std::string& new_part,
50                             size_t old_num_blocks,
51                             size_t new_num_blocks,
52                             off_t chunk_blocks,
53                             bool src_ops_allowed,
54                             int data_fd,
55                             off_t* data_file_size,
56                             ExtentRanges* old_visited_blocks,
57                             ExtentRanges* new_visited_blocks);
58
59// For a given file |name| append operations to |aops| to produce it in the
60// |new_part|. The file will be split in chunks of |chunk_blocks| blocks each
61// or treated as a single chunk if |chunk_blocks| is -1. The file data is
62// stored in |new_part| in the blocks described by |new_extents| and, if it
63// exists, the old version exists in |old_part| in the blocks described by
64// |old_extents|. The operations added to |aops| reference the data blob
65// in the file |data_fd|, which has length *data_file_size. *data_file_size is
66// updated appropriately. Returns true on success.
67bool DeltaReadFile(std::vector<AnnotatedOperation>* aops,
68                   const std::string& old_part,
69                   const std::string& new_part,
70                   const std::vector<Extent>& old_extents,
71                   const std::vector<Extent>& new_extents,
72                   const std::string& name,
73                   off_t chunk_blocks,
74                   int data_fd,
75                   off_t* data_file_size,
76                   bool src_ops_allowed);
77
78// Reads the blocks |old_extents| from |old_part| (if it exists) and the
79// |new_extents| from |new_part| and determines the smallest way to encode
80// this |new_extents| for the diff. It stores necessary data in |out_data| and
81// fills in |out_op|. If there's no change in old and new files, it creates a
82// MOVE operation. If there is a change, the smallest of REPLACE, REPLACE_BZ,
83// or BSDIFF wins. |new_extents| must not be empty.
84// If |src_ops_allowed| is true, it will emit SOURCE_COPY and SOURCE_BSDIFF
85// operations instead of MOVE and BSDIFF, respectively.
86// Returns true on success.
87bool ReadExtentsToDiff(const std::string& old_part,
88                       const std::string& new_part,
89                       const std::vector<Extent>& old_extents,
90                       const std::vector<Extent>& new_extents,
91                       bool bsdiff_allowed,
92                       chromeos::Blob* out_data,
93                       DeltaArchiveManifest_InstallOperation* out_op,
94                       bool src_ops_allowed);
95
96// Runs the bsdiff tool on two files and returns the resulting delta in
97// |out|. Returns true on success.
98bool BsdiffFiles(const std::string& old_file,
99                 const std::string& new_file,
100                 chromeos::Blob* out);
101
102// Returns true if |op| is a no-op operation that doesn't do any useful work
103// (e.g., a move operation that copies blocks onto themselves).
104bool IsNoopOperation(const DeltaArchiveManifest_InstallOperation& op);
105
106// Filters all the operations that are no-op, maintaining the relative order
107// of the rest of the operations.
108void FilterNoopOperations(std::vector<AnnotatedOperation>* ops);
109
110bool InitializePartitionInfo(const PartitionConfig& partition,
111                             PartitionInfo* info);
112
113// Compare two AnnotatedOperations by the start block of the first Extent in
114// their destination extents.
115bool CompareAopsByDestination(AnnotatedOperation first_aop,
116                              AnnotatedOperation second_aop);
117
118}  // namespace diff_utils
119
120}  // namespace chromeos_update_engine
121
122#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
123