Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
| 7 | |
| 8 | #include <inttypes.h> |
| 9 | #include <vector> |
| 10 | #include <google/protobuf/repeated_field.h> |
| 11 | #include "update_engine/file_writer.h" |
| 12 | #include "update_engine/update_metadata.pb.h" |
| 13 | |
| 14 | namespace chromeos_update_engine { |
| 15 | |
| 16 | // This class performs the actions in a delta update synchronously. The delta |
| 17 | // update itself should be passed in in chunks as it is received. |
| 18 | |
| 19 | class DeltaPerformer : public FileWriter { |
| 20 | public: |
| 21 | DeltaPerformer() |
| 22 | : fd_(-1), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 23 | kernel_fd_(-1), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 24 | manifest_valid_(false), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 25 | next_operation_num_(0), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 26 | buffer_offset_(0), |
| 27 | block_size_(0) {} |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 28 | |
| 29 | // Opens the kernel. Should be called before or after Open(), but before |
| 30 | // Write(). The kernel file will be close()d when Close() is called. |
| 31 | bool OpenKernel(const char* kernel_path); |
| 32 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 33 | // flags and mode ignored. Once Close()d, a DeltaPerformer can't be |
| 34 | // Open()ed again. |
| 35 | int Open(const char* path, int flags, mode_t mode); |
| 36 | |
| 37 | // Wrapper around write. Returns bytes written on success or |
| 38 | // -errno on error. |
Andrew de los Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame] | 39 | ssize_t Write(const void* bytes, size_t count); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | |
| 41 | // Wrapper around close. Returns 0 on success or -errno on error. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 42 | // Closes both 'path' given to Open() and the kernel path. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 43 | int Close(); |
| 44 | |
| 45 | // Converts an ordered collection of Extent objects which contain data of |
| 46 | // length full_length to a comma-separated string. For each Extent, the |
| 47 | // string will have the start offset and then the length in bytes. |
| 48 | // The length value of the last extent in the string may be short, since |
| 49 | // the full length of all extents in the string is capped to full_length. |
| 50 | // Also, an extent starting at kSparseHole, appears as -1 in the string. |
| 51 | // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
| 52 | // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
| 53 | // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
| 54 | static bool ExtentsToBsdiffPositionsString( |
| 55 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 56 | uint64_t block_size, |
| 57 | uint64_t full_length, |
| 58 | std::string* positions_string); |
| 59 | |
| 60 | private: |
| 61 | // Returns true if enough of the delta file has been passed via Write() |
| 62 | // to be able to perform a given install operation. |
| 63 | bool CanPerformInstallOperation( |
| 64 | const DeltaArchiveManifest_InstallOperation& operation); |
| 65 | |
| 66 | // Returns true on success. |
| 67 | bool PerformInstallOperation( |
| 68 | const DeltaArchiveManifest_InstallOperation& operation); |
| 69 | |
| 70 | // These perform a specific type of operation and return true on success. |
| 71 | bool PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 72 | const DeltaArchiveManifest_InstallOperation& operation, |
| 73 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 74 | bool PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 75 | const DeltaArchiveManifest_InstallOperation& operation, |
| 76 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 77 | bool PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 78 | const DeltaArchiveManifest_InstallOperation& operation, |
| 79 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 80 | |
| 81 | // File descriptor of open device. |
| 82 | int fd_; |
| 83 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 84 | // File descriptor of the kernel device |
| 85 | int kernel_fd_; |
| 86 | |
| 87 | std::string path_; // Path that fd_ refers to. |
| 88 | std::string kernel_path_; // Path that kernel_fd_ refers to. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 89 | |
| 90 | DeltaArchiveManifest manifest_; |
| 91 | bool manifest_valid_; |
| 92 | |
| 93 | // Index of the next operation to perform in the manifest. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame^] | 94 | int next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 95 | |
| 96 | // buffer_ is a window of the data that's been downloaded. At first, |
| 97 | // it contains the beginning of the download, but after the protobuf |
| 98 | // has been downloaded and parsed, it contains a sliding window of |
| 99 | // data blobs. |
| 100 | std::vector<char> buffer_; |
| 101 | // Offset of buffer_ in the binary blobs section of the update. |
| 102 | uint64_t buffer_offset_; |
| 103 | |
| 104 | // The block size (parsed from the manifest). |
| 105 | uint32_t block_size_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 108 | }; |
| 109 | |
| 110 | } // namespace chromeos_update_engine |
| 111 | |
| 112 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |