delta_diff_utils.h revision f0061358b5f741baeeb9177b838b289d2ce318f3
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 skip_block_0,
35                        bool src_ops_allowed);
36
37// Create operations in |aops| for identical blocks that moved around in the old
38// and new partition and also handle zeroed blocks. The old and new partition
39// are stored in the |old_part| and |new_part| files and have |old_num_blocks|
40// and |new_num_blocks| respectively. The maximum operation size is
41// |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the
42// produced operations are stored in the |data_fd| file whose size is updated
43// in the value pointed by |data_file_size|.
44// The collections |old_visited_blocks| and |new_visited_blocks| state what
45// blocks already have operations reading or writing them and only operations
46// for unvisited blocks are produced by this function updating both collections
47// with the used blocks.
48bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops,
49                             const std::string& old_part,
50                             const std::string& new_part,
51                             size_t old_num_blocks,
52                             size_t new_num_blocks,
53                             off_t chunk_blocks,
54                             bool src_ops_allowed,
55                             int data_fd,
56                             off_t* data_file_size,
57                             ExtentRanges* old_visited_blocks,
58                             ExtentRanges* new_visited_blocks);
59
60// For a given file |name| append operations to |aops| to produce it in the
61// |new_part|. The file will be split in chunks of |chunk_blocks| blocks each
62// or treated as a single chunk if |chunk_blocks| is -1. The file data is
63// stored in |new_part| in the blocks described by |new_extents| and, if it
64// exists, the old version exists in |old_part| in the blocks described by
65// |old_extents|. The operations added to |aops| reference the data blob
66// in the file |data_fd|, which has length *data_file_size. *data_file_size is
67// updated appropriately. Returns true on success.
68bool DeltaReadFile(std::vector<AnnotatedOperation>* aops,
69                   const std::string& old_part,
70                   const std::string& new_part,
71                   const std::vector<Extent>& old_extents,
72                   const std::vector<Extent>& new_extents,
73                   const std::string& name,
74                   off_t chunk_blocks,
75                   int data_fd,
76                   off_t* data_file_size,
77                   bool src_ops_allowed);
78
79// Reads the blocks |old_extents| from |old_part| (if it exists) and the
80// |new_extents| from |new_part| and determines the smallest way to encode
81// this |new_extents| for the diff. It stores necessary data in |out_data| and
82// fills in |out_op|. If there's no change in old and new files, it creates a
83// MOVE operation. If there is a change, the smallest of REPLACE, REPLACE_BZ,
84// or BSDIFF wins. |new_extents| must not be empty.
85// If |src_ops_allowed| is true, it will emit SOURCE_COPY and SOURCE_BSDIFF
86// operations instead of MOVE and BSDIFF, respectively.
87// Returns true on success.
88bool ReadExtentsToDiff(const std::string& old_part,
89                       const std::string& new_part,
90                       const std::vector<Extent>& old_extents,
91                       const std::vector<Extent>& new_extents,
92                       bool bsdiff_allowed,
93                       chromeos::Blob* out_data,
94                       DeltaArchiveManifest_InstallOperation* out_op,
95                       bool src_ops_allowed);
96
97// Runs the bsdiff tool on two files and returns the resulting delta in
98// |out|. Returns true on success.
99bool BsdiffFiles(const std::string& old_file,
100                 const std::string& new_file,
101                 chromeos::Blob* out);
102
103// Returns true if |op| is a no-op operation that doesn't do any useful work
104// (e.g., a move operation that copies blocks onto themselves).
105bool IsNoopOperation(const DeltaArchiveManifest_InstallOperation& op);
106
107// Filters all the operations that are no-op, maintaining the relative order
108// of the rest of the operations.
109void FilterNoopOperations(std::vector<AnnotatedOperation>* ops);
110
111bool InitializePartitionInfo(const PartitionConfig& partition,
112                             PartitionInfo* info);
113
114}  // namespace diff_utils
115
116}  // namespace chromeos_update_engine
117
118#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
119