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 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 47 | virtual bool Valid() const = 0; |
| 48 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 49 | virtual void Close() {} |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 50 | |
| 51 | protected: |
| 52 | FuseDataProvider() = default; |
| 53 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 54 | // Size in bytes of the file to read. |
| 55 | uint64_t file_size_ = 0; |
| 56 | // Block size passed to the fuse, this is different from the block size of the block device. |
| 57 | uint32_t fuse_block_size_ = 0; |
| 58 | }; |
| 59 | |
| 60 | // This class reads data from a file. |
| 61 | class FuseFileDataProvider : public FuseDataProvider { |
| 62 | public: |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 63 | FuseFileDataProvider(const std::string& path, uint32_t block_size); |
| 64 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 65 | static std::unique_ptr<FuseDataProvider> CreateFromFile(const std::string& path, |
| 66 | uint32_t block_size); |
| 67 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 68 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 69 | uint32_t start_block) const override; |
| 70 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 71 | bool Valid() const override { |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 72 | return fd_ != -1; |
| 73 | } |
| 74 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 75 | void Close() override; |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | // The underlying source to read data from. |
| 79 | android::base::unique_fd fd_; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 80 | }; |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 81 | |
| 82 | // This class parses a block map and reads data from the underlying block device. |
| 83 | class FuseBlockDataProvider : public FuseDataProvider { |
| 84 | public: |
| 85 | // Constructs the fuse provider from the block map. |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 86 | static std::unique_ptr<FuseDataProvider> CreateFromBlockMap(const std::string& block_map_path, |
| 87 | uint32_t fuse_block_size); |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 88 | |
| 89 | RangeSet ranges() const { |
| 90 | return ranges_; |
| 91 | } |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 92 | |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 93 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 94 | uint32_t start_block) const override; |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 95 | |
| 96 | bool Valid() const override { |
| 97 | return fd_ != -1; |
| 98 | } |
| 99 | |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 100 | void Close() override; |
| 101 | |
| 102 | private: |
| 103 | FuseBlockDataProvider(uint64_t file_size, uint32_t fuse_block_size, android::base::unique_fd&& fd, |
| 104 | uint32_t source_block_size, RangeSet ranges); |
| 105 | // The underlying block device to read data from. |
| 106 | android::base::unique_fd fd_; |
| 107 | // The block size of the source block device. |
| 108 | uint32_t source_block_size_; |
| 109 | // The block ranges from the source block device that consist of the file |
| 110 | RangeSet ranges_; |
| 111 | }; |