blob: efc47478fe4b518867a0a4a3124bebd0fed7a5df [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.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070043 // {old,new}_kernel_part are paths to the old and new kernel partition
44 // images, respectively.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070045 // output_path is the filename where the delta update should be written.
46 // Returns true on success.
47 static bool GenerateDeltaUpdateFile(const std::string& old_root,
48 const std::string& old_image,
49 const std::string& new_root,
50 const std::string& new_image,
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070051 const std::string& old_kernel_part,
52 const std::string& new_kernel_part,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070053 const std::string& output_path);
54
55 // These functions are public so that the unit tests can access them:
56
57 // Reads old_filename (if it exists) and a new_filename and determines
58 // the smallest way to encode this file for the diff. It stores
59 // necessary data in out_data and fills in out_op.
60 // If there's no change in old and new files, it creates a MOVE
61 // operation. If there is a change, or the old file doesn't exist,
62 // the smallest of REPLACE, REPLACE_BZ, or BSDIFF wins.
63 // new_filename must contain at least one byte.
64 // Returns true on success.
65 static bool ReadFileToDiff(const std::string& old_filename,
66 const std::string& new_filename,
67 std::vector<char>* out_data,
68 DeltaArchiveManifest_InstallOperation* out_op);
69
70 // Modifies blocks read by 'op' so that any blocks referred to by
71 // 'remove_extents' are replaced with blocks from 'replace_extents'.
72 // 'remove_extents' and 'replace_extents' must be the same number of blocks.
73 // Blocks will be substituted in the order listed in the vectors.
74 // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents
75 // contains blocks 6, 2, 3, 5, and replace blocks contains
76 // 12, 13, 14, 15, then op will be changed to read from:
77 // 1, 13, 14, 4, 15, 12, 7, 8
78 static void SubstituteBlocks(DeltaArchiveManifest_InstallOperation* op,
79 const std::vector<Extent>& remove_extents,
80 const std::vector<Extent>& replace_extents);
81
82 // Cuts 'edges' from 'graph' according to the AU algorithm. This means
83 // for each edge A->B, remove the dependency that B occur before A.
84 // Do this by creating a new operation X that copies from the blocks
85 // specified by the edge's properties to temp space T. Modify B to read
86 // from T rather than the blocks in the edge. Modify A to depend on X,
87 // but not on B. Free space is found by looking in 'blocks'.
88 // Returns true on success.
89 static bool CutEdges(Graph* graph,
90 const std::vector<Block>& blocks,
91 const std::set<Edge>& edges);
92
93 // Stores all Extents in 'extents' into 'out'.
94 static void StoreExtents(std::vector<Extent>& extents,
95 google::protobuf::RepeatedPtrField<Extent>* out);
96
97 // Creates all the edges for the graph. Writers of a block point to
98 // readers of the same block. This is because for an edge A->B, B
99 // must complete before A executes.
100 static void CreateEdges(Graph* graph, const std::vector<Block>& blocks);
101
102 // Install operations in the manifest may reference data blobs, which
103 // are in data_blobs_path. This function creates a new data blobs file
104 // with the data blobs in the same order as the referencing install
105 // operations in the manifest. E.g. if manifest[0] has a data blob
106 // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
107 // and data_blobs_path's file contains "YX", new_data_blobs_path
108 // will set to be a file that contains "XY".
109 static bool ReorderDataBlobs(DeltaArchiveManifest* manifest,
110 const std::string& data_blobs_path,
111 const std::string& new_data_blobs_path);
112
adlr@google.com3defe6a2009-12-04 20:57:17 +0000113 private:
adlr@google.com3defe6a2009-12-04 20:57:17 +0000114 // This should never be constructed
115 DISALLOW_IMPLICIT_CONSTRUCTORS(DeltaDiffGenerator);
116};
117
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700118extern const char* const kBsdiffPath;
119extern const char* const kBspatchPath;
120extern const char* const kDeltaMagic;
121
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122}; // namespace chromeos_update_engine
123
124#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__