Alex Deymo | f006135 | 2015-07-01 14:59:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOCK_MAPPING_H_ |
| 6 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOCK_MAPPING_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <chromeos/secure_blob.h> |
| 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 | |
| 15 | #include "update_engine/payload_generator/payload_generation_config.h" |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | // BlockMapping allows to map data blocks (chromeos::Blobs of block_size size) |
| 20 | // into unique integer values called "block ids". This mapping differs from a |
| 21 | // hash function in that two blocks with the same data will have the same id but |
| 22 | // also two blocks with the same id will have the same data. This is only valid |
| 23 | // in the context of the same BlockMapping instance. |
| 24 | class BlockMapping { |
| 25 | public: |
| 26 | using BlockId = int64_t; |
| 27 | |
| 28 | explicit BlockMapping(size_t block_size) : block_size_(block_size) {} |
| 29 | |
| 30 | // Add a single data block to the mapping. Returns its unique block id. |
| 31 | // In case of error returns -1. |
| 32 | BlockId AddBlock(const chromeos::Blob& block_data); |
| 33 | |
| 34 | // Add a block from disk reading it from the file descriptor |fd| from the |
| 35 | // offset in bytes |byte_offset|. The data block may or may not be cached, so |
| 36 | // the file descriptor must be available until the BlockMapping is destroyed. |
| 37 | // Returns the unique block id of the added block or -1 in case of error. |
| 38 | BlockId AddDiskBlock(int fd, off_t byte_offset); |
| 39 | |
| 40 | // This is a helper method to add |num_blocks| contiguous blocks reading them |
| 41 | // from the file descriptor |fd| starting at offset |initial_byte_offset|. |
| 42 | // Returns whether it succeeded to add all the disk blocks and stores in |
| 43 | // |block_ids| the block id for each one of the added blocks. |
| 44 | bool AddManyDiskBlocks(int fd, off_t initial_byte_offset, size_t num_blocks, |
| 45 | std::vector<BlockId>* block_ids); |
| 46 | |
| 47 | private: |
| 48 | FRIEND_TEST(BlockMappingTest, BlocksAreNotKeptInMemory); |
| 49 | |
| 50 | // Add a single block passed in |block_data|. If |fd| is not -1, the block |
| 51 | // can be discarded to save RAM and retrieved later from |fd| at the position |
| 52 | // |byte_offset|. |
| 53 | BlockId AddBlock(int fd, off_t byte_offset, const chromeos::Blob& block_data); |
| 54 | |
| 55 | size_t block_size_; |
| 56 | |
| 57 | BlockId used_block_ids{0}; |
| 58 | |
| 59 | // The UniqueBlock represents the data of a block associated to a unique |
| 60 | // block id. |
| 61 | struct UniqueBlock { |
| 62 | chromeos::Blob block_data; |
| 63 | |
| 64 | // The block id assigned to this unique block. |
| 65 | BlockId block_id; |
| 66 | |
| 67 | // The location on this unique block on disk (if not cached in block_data). |
| 68 | int fd{-1}; |
| 69 | off_t byte_offset{0}; |
| 70 | |
| 71 | // Number of times we have seen this data block. Used for caching. |
| 72 | uint32_t times_read{0}; |
| 73 | |
| 74 | // Compares the UniqueBlock data with the other_block data and stores if |
| 75 | // they are equal in |equals|. Returns whether there was an error reading |
| 76 | // the block from disk while comparing it. |
| 77 | bool CompareData(const chromeos::Blob& other_block, bool* equals); |
| 78 | }; |
| 79 | |
| 80 | // A mapping from hash values to possible block ids. |
| 81 | std::map<size_t, std::vector<UniqueBlock>> mapping_; |
| 82 | }; |
| 83 | |
| 84 | // Maps the blocks of the old and new partitions |old_part| and |new_part| whose |
| 85 | // size in bytes are |old_size| and |new_size| into block ids where two blocks |
| 86 | // with the same data will have the same block id and vice versa, regardless of |
| 87 | // the partition they are on. |
| 88 | // The block ids number 0 corresponds to the block with all zeros, but any |
| 89 | // other block id number is assigned randomly. |
| 90 | bool MapPartitionBlocks(const std::string& old_part, |
| 91 | const std::string& new_part, |
| 92 | size_t old_size, |
| 93 | size_t new_size, |
| 94 | size_t block_size, |
| 95 | std::vector<BlockMapping::BlockId>* old_block_ids, |
| 96 | std::vector<BlockMapping::BlockId>* new_block_ids); |
| 97 | |
| 98 | } // namespace chromeos_update_engine |
| 99 | |
| 100 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOCK_MAPPING_H_ |