blob: 6e48519380d575852aa734d9f0fbd99749298cc3 [file] [log] [blame]
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07001// Copyright (c) 2010 The Chromium 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
23class DeltaDiffGenerator {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070024 public:
25 // Represents a disk block on the install partition.
26 struct Block {
27 // During install, each block on the install partition will be written
28 // and some may be read (in all likelihood, many will be read).
29 // The reading and writing will be performed by InstallOperations,
30 // each of which has a corresponding vertex in a graph.
31 // A Block object tells which vertex will read or write this block
32 // at install time.
33 // Generally, there will be a vector of Block objects whose length
34 // is the number of blocks on the install partition.
35 Block() : reader(Vertex::kInvalidIndex), writer(Vertex::kInvalidIndex) {}
36 Vertex::Index reader;
37 Vertex::Index writer;
38 };
39
40 // This is the only function that external users of the class should call.
41 // old_image and new_image are paths to two image files. They should be
42 // mounted read-only at paths old_root and new_root respectively.
43 // output_path is the filename where the delta update should be written.
44 // Returns true on success.
45 static bool GenerateDeltaUpdateFile(const std::string& old_root,
46 const std::string& old_image,
47 const std::string& new_root,
48 const std::string& new_image,
49 const std::string& output_path);
50
51 // These functions are public so that the unit tests can access them:
52
53 // Reads old_filename (if it exists) and a new_filename and determines
54 // the smallest way to encode this file for the diff. It stores
55 // necessary data in out_data and fills in out_op.
56 // If there's no change in old and new files, it creates a MOVE
57 // operation. If there is a change, or the old file doesn't exist,
58 // the smallest of REPLACE, REPLACE_BZ, or BSDIFF wins.
59 // new_filename must contain at least one byte.
60 // Returns true on success.
61 static bool ReadFileToDiff(const std::string& old_filename,
62 const std::string& new_filename,
63 std::vector<char>* out_data,
64 DeltaArchiveManifest_InstallOperation* out_op);
65
66 // Modifies blocks read by 'op' so that any blocks referred to by
67 // 'remove_extents' are replaced with blocks from 'replace_extents'.
68 // 'remove_extents' and 'replace_extents' must be the same number of blocks.
69 // Blocks will be substituted in the order listed in the vectors.
70 // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents
71 // contains blocks 6, 2, 3, 5, and replace blocks contains
72 // 12, 13, 14, 15, then op will be changed to read from:
73 // 1, 13, 14, 4, 15, 12, 7, 8
74 static void SubstituteBlocks(DeltaArchiveManifest_InstallOperation* op,
75 const std::vector<Extent>& remove_extents,
76 const std::vector<Extent>& replace_extents);
77
78 // Cuts 'edges' from 'graph' according to the AU algorithm. This means
79 // for each edge A->B, remove the dependency that B occur before A.
80 // Do this by creating a new operation X that copies from the blocks
81 // specified by the edge's properties to temp space T. Modify B to read
82 // from T rather than the blocks in the edge. Modify A to depend on X,
83 // but not on B. Free space is found by looking in 'blocks'.
84 // Returns true on success.
85 static bool CutEdges(Graph* graph,
86 const std::vector<Block>& blocks,
87 const std::set<Edge>& edges);
88
89 // Stores all Extents in 'extents' into 'out'.
90 static void StoreExtents(std::vector<Extent>& extents,
91 google::protobuf::RepeatedPtrField<Extent>* out);
92
93 // Creates all the edges for the graph. Writers of a block point to
94 // readers of the same block. This is because for an edge A->B, B
95 // must complete before A executes.
96 static void CreateEdges(Graph* graph, const std::vector<Block>& blocks);
97
98 // Install operations in the manifest may reference data blobs, which
99 // are in data_blobs_path. This function creates a new data blobs file
100 // with the data blobs in the same order as the referencing install
101 // operations in the manifest. E.g. if manifest[0] has a data blob
102 // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
103 // and data_blobs_path's file contains "YX", new_data_blobs_path
104 // will set to be a file that contains "XY".
105 static bool ReorderDataBlobs(DeltaArchiveManifest* manifest,
106 const std::string& data_blobs_path,
107 const std::string& new_data_blobs_path);
108
adlr@google.com3defe6a2009-12-04 20:57:17 +0000109 private:
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 // This should never be constructed
111 DISALLOW_IMPLICIT_CONSTRUCTORS(DeltaDiffGenerator);
112};
113
114}; // namespace chromeos_update_engine
115
116#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__