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 <memory>
22 #include <string>
23 
24 #include <android-base/unique_fd.h>
25 
26 #include "otautil/rangeset.h"
27 
28 // This is the base class to read data from source and provide the data to FUSE.
29 class FuseDataProvider {
30  public:
FuseDataProvider(uint64_t file_size,uint32_t block_size)31   FuseDataProvider(uint64_t file_size, uint32_t block_size)
32       : file_size_(file_size), fuse_block_size_(block_size) {}
33 
34   virtual ~FuseDataProvider() = default;
35 
file_size()36   uint64_t file_size() const {
37     return file_size_;
38   }
fuse_block_size()39   uint32_t fuse_block_size() const {
40     return fuse_block_size_;
41   }
42 
43   // 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 
47   virtual bool Valid() const = 0;
48 
Close()49   virtual void Close() {}
50 
51  protected:
52   FuseDataProvider() = default;
53 
54   // 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.
61 class FuseFileDataProvider : public FuseDataProvider {
62  public:
63   FuseFileDataProvider(const std::string& path, uint32_t block_size);
64 
65   static std::unique_ptr<FuseDataProvider> CreateFromFile(const std::string& path,
66                                                           uint32_t block_size);
67 
68   bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
69                             uint32_t start_block) const override;
70 
Valid()71   bool Valid() const override {
72     return fd_ != -1;
73   }
74 
75   void Close() override;
76 
77  private:
78   // The underlying source to read data from.
79   android::base::unique_fd fd_;
80 };
81 
82 // This class parses a block map and reads data from the underlying block device.
83 class FuseBlockDataProvider : public FuseDataProvider {
84  public:
85   // Constructs the fuse provider from the block map.
86   static std::unique_ptr<FuseDataProvider> CreateFromBlockMap(const std::string& block_map_path,
87                                                               uint32_t fuse_block_size);
88 
ranges()89   RangeSet ranges() const {
90     return ranges_;
91   }
92 
93   bool ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
94                             uint32_t start_block) const override;
95 
Valid()96   bool Valid() const override {
97     return fd_ != -1;
98   }
99 
100   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 };
112