blob: 3156fd6cd8794b68afe5ac9935e9e4512b439a66 [file] [log] [blame]
Darin Petkov7ea32332010-10-13 10:46:11 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__
7
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07008#include <string>
9#include <vector>
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include "base/basictypes.h"
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070011#include "update_engine/graph_types.h"
12#include "update_engine/update_metadata.pb.h"
13
14// There is one function in DeltaDiffGenerator of importance to users
15// of the class: GenerateDeltaUpdateFile(). Before calling it,
16// the old and new images must be mounted. Call GenerateDeltaUpdateFile()
17// with both the mount-points of the images in addition to the paths of
18// the images (both old and new). A delta from old to new will be
19// generated and stored in output_path.
adlr@google.com3defe6a2009-12-04 20:57:17 +000020
21namespace chromeos_update_engine {
22
Andrew de los Reyesef017552010-10-06 17:57:52 -070023// This struct stores all relevant info for an edge that is cut between
24// nodes old_src -> old_dst by creating new vertex new_vertex. The new
25// relationship is:
26// old_src -(read before)-> new_vertex <-(write before)- old_dst
27// new_vertex is a MOVE operation that moves some existing blocks into
28// temp space. The temp extents are, by necessity, stored in new_vertex
29// (as dst extents) and old_dst (as src extents), but they are also broken
30// out into tmp_extents, as the nodes themselves may contain many more
31// extents.
32struct CutEdgeVertexes {
33 Vertex::Index new_vertex;
34 Vertex::Index old_src;
35 Vertex::Index old_dst;
36 std::vector<Extent> tmp_extents;
37};
38
adlr@google.com3defe6a2009-12-04 20:57:17 +000039class DeltaDiffGenerator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070040 public:
41 // Represents a disk block on the install partition.
42 struct Block {
43 // During install, each block on the install partition will be written
44 // and some may be read (in all likelihood, many will be read).
45 // The reading and writing will be performed by InstallOperations,
46 // each of which has a corresponding vertex in a graph.
47 // A Block object tells which vertex will read or write this block
48 // at install time.
49 // Generally, there will be a vector of Block objects whose length
50 // is the number of blocks on the install partition.
51 Block() : reader(Vertex::kInvalidIndex), writer(Vertex::kInvalidIndex) {}
52 Vertex::Index reader;
53 Vertex::Index writer;
54 };
55
56 // This is the only function that external users of the class should call.
57 // old_image and new_image are paths to two image files. They should be
58 // mounted read-only at paths old_root and new_root respectively.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070059 // {old,new}_kernel_part are paths to the old and new kernel partition
60 // images, respectively.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070061 // private_key_path points to a private key used to sign the update.
62 // Pass empty string to not sign the update.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070063 // output_path is the filename where the delta update should be written.
64 // Returns true on success.
65 static bool GenerateDeltaUpdateFile(const std::string& old_root,
66 const std::string& old_image,
67 const std::string& new_root,
68 const std::string& new_image,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070069 const std::string& old_kernel_part,
70 const std::string& new_kernel_part,
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070071 const std::string& output_path,
72 const std::string& private_key_path);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070073
74 // These functions are public so that the unit tests can access them:
75
Andrew de los Reyesef017552010-10-06 17:57:52 -070076 // Takes a graph, which is not a DAG, which represents the files just
77 // read from disk, and converts it into a DAG by breaking all cycles
78 // and finding temp space to resolve broken edges.
79 // The final order of the nodes is given in |final_order|
80 // Some files may need to be reread from disk, thus |fd| and
81 // |data_file_size| are be passed.
82 // Returns true on success.
83 static bool ConvertGraphToDag(Graph* graph,
84 const std::string& new_root,
85 int fd,
86 off_t* data_file_size,
87 std::vector<Vertex::Index>* final_order);
88
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070089 // Reads old_filename (if it exists) and a new_filename and determines
90 // the smallest way to encode this file for the diff. It stores
91 // necessary data in out_data and fills in out_op.
92 // If there's no change in old and new files, it creates a MOVE
93 // operation. If there is a change, or the old file doesn't exist,
94 // the smallest of REPLACE, REPLACE_BZ, or BSDIFF wins.
95 // new_filename must contain at least one byte.
96 // Returns true on success.
97 static bool ReadFileToDiff(const std::string& old_filename,
98 const std::string& new_filename,
99 std::vector<char>* out_data,
Darin Petkov68c10d12010-10-14 09:24:37 -0700100 DeltaArchiveManifest_InstallOperation* out_op,
101 bool gather_extents);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700102
103 // Modifies blocks read by 'op' so that any blocks referred to by
104 // 'remove_extents' are replaced with blocks from 'replace_extents'.
105 // 'remove_extents' and 'replace_extents' must be the same number of blocks.
106 // Blocks will be substituted in the order listed in the vectors.
107 // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents
108 // contains blocks 6, 2, 3, 5, and replace blocks contains
109 // 12, 13, 14, 15, then op will be changed to read from:
110 // 1, 13, 14, 4, 15, 12, 7, 8
Andrew de los Reyesef017552010-10-06 17:57:52 -0700111 static void SubstituteBlocks(Vertex* vertex,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700112 const std::vector<Extent>& remove_extents,
113 const std::vector<Extent>& replace_extents);
114
115 // Cuts 'edges' from 'graph' according to the AU algorithm. This means
116 // for each edge A->B, remove the dependency that B occur before A.
117 // Do this by creating a new operation X that copies from the blocks
118 // specified by the edge's properties to temp space T. Modify B to read
119 // from T rather than the blocks in the edge. Modify A to depend on X,
120 // but not on B. Free space is found by looking in 'blocks'.
121 // Returns true on success.
122 static bool CutEdges(Graph* graph,
Andrew de los Reyesef017552010-10-06 17:57:52 -0700123 const std::set<Edge>& edges,
124 std::vector<CutEdgeVertexes>* out_cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700125
126 // Stores all Extents in 'extents' into 'out'.
Andrew de los Reyesef017552010-10-06 17:57:52 -0700127 static void StoreExtents(const std::vector<Extent>& extents,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700128 google::protobuf::RepeatedPtrField<Extent>* out);
Darin Petkov7ea32332010-10-13 10:46:11 -0700129
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700130 // Creates all the edges for the graph. Writers of a block point to
131 // readers of the same block. This is because for an edge A->B, B
132 // must complete before A executes.
133 static void CreateEdges(Graph* graph, const std::vector<Block>& blocks);
Andrew de los Reyesef017552010-10-06 17:57:52 -0700134
135 // Given a topologically sorted graph |op_indexes| and |graph|, alters
136 // |op_indexes| to move all the full operations to the end of the vector.
137 // Full operations should not be depended on, so this is safe.
138 static void MoveFullOpsToBack(Graph* graph,
139 std::vector<Vertex::Index>* op_indexes);
140
141 // Sorts the vector |cuts| by its |cuts[].old_dest| member. Order is
142 // determined by the order of elements in op_indexes.
143 static void SortCutsByTopoOrder(std::vector<Vertex::Index>& op_indexes,
144 std::vector<CutEdgeVertexes>* cuts);
145
146 // Returns true iff there are no extents in the graph that refer to temp
147 // blocks. Temp blocks are in the range [kTempBlockStart, kSparseHole).
148 static bool NoTempBlocksRemain(const Graph& graph);
Darin Petkov7ea32332010-10-13 10:46:11 -0700149
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700150 // Install operations in the manifest may reference data blobs, which
151 // are in data_blobs_path. This function creates a new data blobs file
152 // with the data blobs in the same order as the referencing install
153 // operations in the manifest. E.g. if manifest[0] has a data blob
154 // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
155 // and data_blobs_path's file contains "YX", new_data_blobs_path
156 // will set to be a file that contains "XY".
157 static bool ReorderDataBlobs(DeltaArchiveManifest* manifest,
158 const std::string& data_blobs_path,
159 const std::string& new_data_blobs_path);
Darin Petkov7ea32332010-10-13 10:46:11 -0700160
Andrew de los Reyesef017552010-10-06 17:57:52 -0700161 // Handles allocation of temp blocks to a cut edge by converting the
162 // dest node to a full op. This removes the need for temp blocks, but
163 // comes at the cost of a worse compression ratio.
164 // For example, say we have A->B->A. It would first be cut to form:
165 // A->B->N<-A, where N copies blocks to temp space. If there are no
166 // temp blocks, this function can be called to convert it to the form:
167 // A->B. Now, A is a full operation.
168 static bool ConvertCutToFullOp(Graph* graph,
169 const CutEdgeVertexes& cut,
170 const std::string& new_root,
171 int data_fd,
172 off_t* data_file_size);
Darin Petkov7ea32332010-10-13 10:46:11 -0700173
Andrew de los Reyesef017552010-10-06 17:57:52 -0700174 // Takes |op_indexes|, which is effectively a mapping from order in
175 // which the op is performed -> graph vertex index, and produces the
176 // reverse: a mapping from graph vertex index -> op_indexes index.
177 static void GenerateReverseTopoOrderMap(
178 std::vector<Vertex::Index>& op_indexes,
179 std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes);
Darin Petkov7ea32332010-10-13 10:46:11 -0700180
Andrew de los Reyesef017552010-10-06 17:57:52 -0700181 // Takes a |graph|, which has edges that must be cut, as listed in
182 // |cuts|. Cuts the edges. Maintains a list in which the operations
183 // will be performed (in |op_indexes|) and the reverse (in
184 // |reverse_op_indexes|). Cutting edges requires scratch space, and
185 // if insufficient scratch is found, the file is reread and will be
186 // send down (either as REPLACE or REPLACE_BZ). Returns true on
187 // success.
188 static bool AssignTempBlocks(
189 Graph* graph,
190 const std::string& new_root,
191 int data_fd,
192 off_t* data_file_size,
193 std::vector<Vertex::Index>* op_indexes,
194 std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes,
195 std::vector<CutEdgeVertexes>& cuts);
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700196
Darin Petkov7ea32332010-10-13 10:46:11 -0700197 // Given a new rootfs and kernel (|new_image|, |new_kernel_part|), Reads them
198 // sequentially, creating a full update of chunk_size chunks. Populates
199 // |graph|, |kernel_ops|, and |final_order|, with data about the update
200 // operations, and writes relevant data to |fd|, updating |data_file_size| as
201 // it does. Only the first |image_size| bytes are read from |new_image|
202 // assuming that this is the actual file system.
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700203 static bool ReadFullUpdateFromDisk(
204 Graph* graph,
205 const std::string& new_kernel_part,
206 const std::string& new_image,
Darin Petkov7ea32332010-10-13 10:46:11 -0700207 off_t image_size,
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700208 int fd,
209 off_t* data_file_size,
210 off_t chunk_size,
211 std::vector<DeltaArchiveManifest_InstallOperation>* kernel_ops,
212 std::vector<Vertex::Index>* final_order);
213
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700214 // Returns true if |op| is a no-op operation that doesn't do any useful work
215 // (e.g., a move operation that copies blocks onto themselves).
216 static bool IsNoopOperation(const DeltaArchiveManifest_InstallOperation& op);
217
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700218 static bool InitializePartitionInfo(bool is_kernel,
219 const std::string& partition,
220 PartitionInfo* info);
221
adlr@google.com3defe6a2009-12-04 20:57:17 +0000222 private:
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223 // This should never be constructed
224 DISALLOW_IMPLICIT_CONSTRUCTORS(DeltaDiffGenerator);
225};
226
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700227extern const char* const kBsdiffPath;
228extern const char* const kBspatchPath;
229extern const char* const kDeltaMagic;
230
adlr@google.com3defe6a2009-12-04 20:57:17 +0000231}; // namespace chromeos_update_engine
232
233#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__