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