xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 21 | #include <memory> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | #include <android-base/unique_fd.h> |
| 25 | |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 26 | #include "otautil/rangeset.h" |
| 27 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 28 | // This is the base class to read data from source and provide the data to FUSE. |
| 29 | class FuseDataProvider { |
| 30 | public: |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 31 | FuseDataProvider(uint64_t file_size, uint32_t block_size) |
| 32 | : file_size_(file_size), fuse_block_size_(block_size) {} |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 33 | |
| 34 | virtual ~FuseDataProvider() = default; |
| 35 | |
| 36 | uint64_t file_size() const { |
| 37 | return file_size_; |
| 38 | } |
| 39 | uint32_t fuse_block_size() const { |
| 40 | return fuse_block_size_; |
| 41 | } |
| 42 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 43 | // Reads |fetch_size| bytes data starting from |start_block|. Puts the result in |buffer|. |
| 44 | virtual bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 45 | uint32_t start_block) const = 0; |
| 46 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 47 | virtual void Close() {} |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 48 | |
| 49 | protected: |
| 50 | FuseDataProvider() = default; |
| 51 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 52 | // Size in bytes of the file to read. |
| 53 | uint64_t file_size_ = 0; |
| 54 | // Block size passed to the fuse, this is different from the block size of the block device. |
| 55 | uint32_t fuse_block_size_ = 0; |
| 56 | }; |
| 57 | |
| 58 | // This class reads data from a file. |
| 59 | class FuseFileDataProvider : public FuseDataProvider { |
| 60 | public: |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 61 | FuseFileDataProvider(const std::string& path, uint32_t block_size); |
| 62 | |
| 63 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 64 | uint32_t start_block) const override; |
| 65 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 66 | bool Valid() const { |
| 67 | return fd_ != -1; |
| 68 | } |
| 69 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 70 | void Close() override; |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | // The underlying source to read data from. |
| 74 | android::base::unique_fd fd_; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 75 | }; |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 76 | |
| 77 | // This class parses a block map and reads data from the underlying block device. |
| 78 | class FuseBlockDataProvider : public FuseDataProvider { |
| 79 | public: |
| 80 | // Constructs the fuse provider from the block map. |
| 81 | static std::unique_ptr<FuseBlockDataProvider> CreateFromBlockMap( |
| 82 | const std::string& block_map_path, uint32_t fuse_block_size); |
| 83 | |
| 84 | RangeSet ranges() const { |
| 85 | return ranges_; |
| 86 | } |
| 87 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 88 | uint32_t start_block) const override; |
| 89 | void Close() override; |
| 90 | |
| 91 | private: |
| 92 | FuseBlockDataProvider(uint64_t file_size, uint32_t fuse_block_size, android::base::unique_fd&& fd, |
| 93 | uint32_t source_block_size, RangeSet ranges); |
| 94 | // The underlying block device to read data from. |
| 95 | android::base::unique_fd fd_; |
| 96 | // The block size of the source block device. |
| 97 | uint32_t source_block_size_; |
| 98 | // The block ranges from the source block device that consist of the file |
| 99 | RangeSet ranges_; |
| 100 | }; |