blob: 59059cf9b3f746a42ecff995daf5f30970c4ba88 [file] [log] [blame]
xunchangea2912f2019-03-17 16:45:12 -07001/*
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.
26class FuseDataProvider {
27 public:
Tao Bao2be97372019-04-15 12:45:50 -070028 FuseDataProvider(uint64_t file_size, uint32_t block_size)
29 : file_size_(file_size), fuse_block_size_(block_size) {}
xunchangea2912f2019-03-17 16:45:12 -070030
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
xunchangea2912f2019-03-17 16:45:12 -070040 // 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 Bao2be97372019-04-15 12:45:50 -070044 virtual void Close() {}
xunchangea2912f2019-03-17 16:45:12 -070045
46 protected:
47 FuseDataProvider() = default;
48
xunchangea2912f2019-03-17 16:45:12 -070049 // 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.
56class FuseFileDataProvider : public FuseDataProvider {
57 public:
xunchangea2912f2019-03-17 16:45:12 -070058 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 Bao2be97372019-04-15 12:45:50 -070063 bool Valid() const {
64 return fd_ != -1;
65 }
66
xunchangea2912f2019-03-17 16:45:12 -070067 void Close() override;
Tao Bao2be97372019-04-15 12:45:50 -070068
69 private:
70 // The underlying source to read data from.
71 android::base::unique_fd fd_;
xunchangea2912f2019-03-17 16:45:12 -070072};