blob: 5ee6e247fe9a79b799d913b39af64ad5244b20d7 [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#include "fuse_provider.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <inttypes.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27#include <functional>
28
29#include <android-base/file.h>
xunchang311e6ca2019-03-22 08:54:35 -070030#include <android-base/logging.h>
31#include <android-base/strings.h>
xunchangea2912f2019-03-17 16:45:12 -070032
33#include "fuse_sideload.h"
xunchang311e6ca2019-03-22 08:54:35 -070034#include "otautil/sysutil.h"
xunchangea2912f2019-03-17 16:45:12 -070035
36FuseFileDataProvider::FuseFileDataProvider(const std::string& path, uint32_t block_size) {
37 struct stat sb;
38 if (stat(path.c_str(), &sb) == -1) {
39 fprintf(stderr, "failed to stat %s: %s\n", path.c_str(), strerror(errno));
40 return;
41 }
42
43 fd_.reset(open(path.c_str(), O_RDONLY));
44 if (fd_ == -1) {
45 fprintf(stderr, "failed to open %s: %s\n", path.c_str(), strerror(errno));
46 return;
47 }
48 file_size_ = sb.st_size;
49 fuse_block_size_ = block_size;
50}
51
52bool FuseFileDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
53 uint32_t start_block) const {
54 uint64_t offset = static_cast<uint64_t>(start_block) * fuse_block_size_;
55 if (fetch_size > file_size_ || offset > file_size_ - fetch_size) {
56 fprintf(stderr,
57 "Out of bound read, start block: %" PRIu32 ", fetch size: %" PRIu32
58 ", file size %" PRIu64 "\n",
59 start_block, fetch_size, file_size_);
60 return false;
61 }
62
63 if (!android::base::ReadFullyAtOffset(fd_, buffer, fetch_size, offset)) {
64 fprintf(stderr, "Failed to read fetch size: %" PRIu32 " bytes data at offset %" PRIu64 ": %s\n",
65 fetch_size, offset, strerror(errno));
66 return false;
67 }
68
69 return true;
70}
71
72void FuseFileDataProvider::Close() {
73 fd_.reset();
74}
xunchang311e6ca2019-03-22 08:54:35 -070075
76FuseBlockDataProvider::FuseBlockDataProvider(uint64_t file_size, uint32_t fuse_block_size,
77 android::base::unique_fd&& fd,
78 uint32_t source_block_size, RangeSet ranges)
79 : FuseDataProvider(file_size, fuse_block_size),
80 fd_(std::move(fd)),
81 source_block_size_(source_block_size),
82 ranges_(std::move(ranges)) {
83 // Make sure the offset is also aligned with the blocks on the block device when we call
84 // ReadBlockAlignedData().
85 CHECK_EQ(0, fuse_block_size_ % source_block_size_);
86}
87
88bool FuseBlockDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
89 uint32_t start_block) const {
90 uint64_t offset = static_cast<uint64_t>(start_block) * fuse_block_size_;
91 if (fetch_size > file_size_ || offset > file_size_ - fetch_size) {
92 LOG(ERROR) << "Out of bound read, offset: " << offset << ", fetch size: " << fetch_size
93 << ", file size " << file_size_;
94 return false;
95 }
96
97 auto read_ranges =
98 ranges_.GetSubRanges(offset / source_block_size_, fetch_size / source_block_size_);
99 if (!read_ranges) {
100 return false;
101 }
102
103 uint8_t* next_out = buffer;
104 for (const auto& [range_start, range_end] : read_ranges.value()) {
105 uint64_t bytes_start = static_cast<uint64_t>(range_start) * source_block_size_;
106 uint64_t bytes_to_read = static_cast<uint64_t>(range_end - range_start) * source_block_size_;
107 if (!android::base::ReadFullyAtOffset(fd_, next_out, bytes_to_read, bytes_start)) {
108 PLOG(ERROR) << "Failed to read " << bytes_to_read << " bytes at offset " << bytes_start;
109 return false;
110 }
111
112 next_out += bytes_to_read;
113 }
114
115 if (uint64_t tailing_bytes = fetch_size % source_block_size_; tailing_bytes != 0) {
116 // Calculate the offset to last partial block.
117 uint64_t tailing_offset =
118 read_ranges.value()
119 ? static_cast<uint64_t>((read_ranges->cend() - 1)->second) * source_block_size_
120 : static_cast<uint64_t>(start_block) * source_block_size_;
121 if (!android::base::ReadFullyAtOffset(fd_, next_out, tailing_bytes, tailing_offset)) {
122 PLOG(ERROR) << "Failed to read tailing " << tailing_bytes << " bytes at offset "
123 << tailing_offset;
124 return false;
125 }
126 }
127 return true;
128}
129
130std::unique_ptr<FuseBlockDataProvider> FuseBlockDataProvider::CreateFromBlockMap(
131 const std::string& block_map_path, uint32_t fuse_block_size) {
132 auto block_map = BlockMapData::ParseBlockMapFile(block_map_path);
133 if (!block_map) {
134 return nullptr;
135 }
136
137 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map.path().c_str(), O_RDONLY)));
138 if (fd == -1) {
139 PLOG(ERROR) << "Failed to open " << block_map.path();
140 return nullptr;
141 }
142
143 return std::unique_ptr<FuseBlockDataProvider>(
144 new FuseBlockDataProvider(block_map.file_size(), fuse_block_size, std::move(fd),
145 block_map.block_size(), block_map.block_ranges()));
146}
147
148void FuseBlockDataProvider::Close() {
149 fd_.reset();
150}