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 | |
| 21 | #include <string> |
| 22 | |
| 23 | #include <android-base/unique_fd.h> |
| 24 | |
| 25 | // This is the base class to read data from source and provide the data to FUSE. |
| 26 | class FuseDataProvider { |
| 27 | public: |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 28 | FuseDataProvider(uint64_t file_size, uint32_t block_size) |
| 29 | : file_size_(file_size), fuse_block_size_(block_size) {} |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 30 | |
| 31 | virtual ~FuseDataProvider() = default; |
| 32 | |
| 33 | uint64_t file_size() const { |
| 34 | return file_size_; |
| 35 | } |
| 36 | uint32_t fuse_block_size() const { |
| 37 | return fuse_block_size_; |
| 38 | } |
| 39 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 40 | // Reads |fetch_size| bytes data starting from |start_block|. Puts the result in |buffer|. |
| 41 | virtual bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 42 | uint32_t start_block) const = 0; |
| 43 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 44 | virtual void Close() {} |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 45 | |
| 46 | protected: |
| 47 | FuseDataProvider() = default; |
| 48 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 49 | // Size in bytes of the file to read. |
| 50 | uint64_t file_size_ = 0; |
| 51 | // Block size passed to the fuse, this is different from the block size of the block device. |
| 52 | uint32_t fuse_block_size_ = 0; |
| 53 | }; |
| 54 | |
| 55 | // This class reads data from a file. |
| 56 | class FuseFileDataProvider : public FuseDataProvider { |
| 57 | public: |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 58 | FuseFileDataProvider(const std::string& path, uint32_t block_size); |
| 59 | |
| 60 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 61 | uint32_t start_block) const override; |
| 62 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 63 | bool Valid() const { |
| 64 | return fd_ != -1; |
| 65 | } |
| 66 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 67 | void Close() override; |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | // The underlying source to read data from. |
| 71 | android::base::unique_fd fd_; |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 72 | }; |