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: |
| 28 | FuseDataProvider(android::base::unique_fd&& fd, uint64_t file_size, uint32_t block_size) |
| 29 | : fd_(std::move(fd)), file_size_(file_size), fuse_block_size_(block_size) {} |
| 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 | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 40 | bool Valid() const { |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 41 | return fd_ != -1; |
| 42 | } |
| 43 | |
| 44 | // Reads |fetch_size| bytes data starting from |start_block|. Puts the result in |buffer|. |
| 45 | virtual bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 46 | uint32_t start_block) const = 0; |
| 47 | |
| 48 | virtual void Close() = 0; |
| 49 | |
| 50 | protected: |
| 51 | FuseDataProvider() = default; |
| 52 | |
| 53 | // The underlying source to read data from. |
| 54 | android::base::unique_fd fd_; |
| 55 | // Size in bytes of the file to read. |
| 56 | uint64_t file_size_ = 0; |
| 57 | // Block size passed to the fuse, this is different from the block size of the block device. |
| 58 | uint32_t fuse_block_size_ = 0; |
| 59 | }; |
| 60 | |
| 61 | // This class reads data from a file. |
| 62 | class FuseFileDataProvider : public FuseDataProvider { |
| 63 | public: |
| 64 | FuseFileDataProvider(android::base::unique_fd&& fd, uint64_t file_size, uint32_t block_size) |
| 65 | : FuseDataProvider(std::move(fd), file_size, block_size) {} |
| 66 | |
| 67 | FuseFileDataProvider(const std::string& path, uint32_t block_size); |
| 68 | |
| 69 | bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 70 | uint32_t start_block) const override; |
| 71 | |
| 72 | void Close() override; |
| 73 | }; |