blob: 3cdaef33d8784d4a90b84e40c07f6f4b69c6c76d [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
xunchang311e6ca2019-03-22 08:54:35 -070021#include <memory>
xunchangea2912f2019-03-17 16:45:12 -070022#include <string>
23
24#include <android-base/unique_fd.h>
25
xunchang311e6ca2019-03-22 08:54:35 -070026#include "otautil/rangeset.h"
27
xunchangea2912f2019-03-17 16:45:12 -070028// This is the base class to read data from source and provide the data to FUSE.
29class FuseDataProvider {
30 public:
Tao Bao2be97372019-04-15 12:45:50 -070031 FuseDataProvider(uint64_t file_size, uint32_t block_size)
32 : file_size_(file_size), fuse_block_size_(block_size) {}
xunchangea2912f2019-03-17 16:45:12 -070033
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
xunchangea2912f2019-03-17 16:45:12 -070043 // 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 Xuf6158eb2019-06-11 16:09:07 -070047 virtual bool Valid() const = 0;
48
Tao Bao2be97372019-04-15 12:45:50 -070049 virtual void Close() {}
xunchangea2912f2019-03-17 16:45:12 -070050
51 protected:
52 FuseDataProvider() = default;
53
xunchangea2912f2019-03-17 16:45:12 -070054 // 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.
61class FuseFileDataProvider : public FuseDataProvider {
62 public:
xunchangea2912f2019-03-17 16:45:12 -070063 FuseFileDataProvider(const std::string& path, uint32_t block_size);
64
Tianjie Xuf6158eb2019-06-11 16:09:07 -070065 static std::unique_ptr<FuseDataProvider> CreateFromFile(const std::string& path,
66 uint32_t block_size);
67
xunchangea2912f2019-03-17 16:45:12 -070068 bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
69 uint32_t start_block) const override;
70
Tianjie Xuf6158eb2019-06-11 16:09:07 -070071 bool Valid() const override {
Tao Bao2be97372019-04-15 12:45:50 -070072 return fd_ != -1;
73 }
74
xunchangea2912f2019-03-17 16:45:12 -070075 void Close() override;
Tao Bao2be97372019-04-15 12:45:50 -070076
77 private:
78 // The underlying source to read data from.
79 android::base::unique_fd fd_;
xunchangea2912f2019-03-17 16:45:12 -070080};
xunchang311e6ca2019-03-22 08:54:35 -070081
82// This class parses a block map and reads data from the underlying block device.
83class FuseBlockDataProvider : public FuseDataProvider {
84 public:
85 // Constructs the fuse provider from the block map.
Tianjie Xuf6158eb2019-06-11 16:09:07 -070086 static std::unique_ptr<FuseDataProvider> CreateFromBlockMap(const std::string& block_map_path,
87 uint32_t fuse_block_size);
xunchang311e6ca2019-03-22 08:54:35 -070088
89 RangeSet ranges() const {
90 return ranges_;
91 }
Tianjie Xuf6158eb2019-06-11 16:09:07 -070092
xunchang311e6ca2019-03-22 08:54:35 -070093 bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
94 uint32_t start_block) const override;
Tianjie Xuf6158eb2019-06-11 16:09:07 -070095
96 bool Valid() const override {
97 return fd_ != -1;
98 }
99
xunchang311e6ca2019-03-22 08:54:35 -0700100 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};