blob: 8d4ea4073b3dcc80db35689ae6506d9f2532159e [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
Tao Bao2be97372019-04-15 12:45:50 -070047 virtual void Close() {}
xunchangea2912f2019-03-17 16:45:12 -070048
49 protected:
50 FuseDataProvider() = default;
51
xunchangea2912f2019-03-17 16:45:12 -070052 // Size in bytes of the file to read.
53 uint64_t file_size_ = 0;
54 // Block size passed to the fuse, this is different from the block size of the block device.
55 uint32_t fuse_block_size_ = 0;
56};
57
58// This class reads data from a file.
59class FuseFileDataProvider : public FuseDataProvider {
60 public:
xunchangea2912f2019-03-17 16:45:12 -070061 FuseFileDataProvider(const std::string& path, uint32_t block_size);
62
63 bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
64 uint32_t start_block) const override;
65
Tao Bao2be97372019-04-15 12:45:50 -070066 bool Valid() const {
67 return fd_ != -1;
68 }
69
xunchangea2912f2019-03-17 16:45:12 -070070 void Close() override;
Tao Bao2be97372019-04-15 12:45:50 -070071
72 private:
73 // The underlying source to read data from.
74 android::base::unique_fd fd_;
xunchangea2912f2019-03-17 16:45:12 -070075};
xunchang311e6ca2019-03-22 08:54:35 -070076
77// This class parses a block map and reads data from the underlying block device.
78class FuseBlockDataProvider : public FuseDataProvider {
79 public:
80 // Constructs the fuse provider from the block map.
81 static std::unique_ptr<FuseBlockDataProvider> CreateFromBlockMap(
82 const std::string& block_map_path, uint32_t fuse_block_size);
83
84 RangeSet ranges() const {
85 return ranges_;
86 }
87 bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
88 uint32_t start_block) const override;
89 void Close() override;
90
91 private:
92 FuseBlockDataProvider(uint64_t file_size, uint32_t fuse_block_size, android::base::unique_fd&& fd,
93 uint32_t source_block_size, RangeSet ranges);
94 // The underlying block device to read data from.
95 android::base::unique_fd fd_;
96 // The block size of the source block device.
97 uint32_t source_block_size_;
98 // The block ranges from the source block device that consist of the file
99 RangeSet ranges_;
100};