xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 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 | #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> |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/strings.h> |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 32 | |
| 33 | #include "fuse_sideload.h" |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 34 | #include "otautil/sysutil.h" |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 35 | |
| 36 | FuseFileDataProvider::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 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 52 | std::unique_ptr<FuseDataProvider> FuseFileDataProvider::CreateFromFile(const std::string& path, |
| 53 | uint32_t block_size) { |
| 54 | return std::make_unique<FuseFileDataProvider>(path, block_size); |
| 55 | } |
| 56 | |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 57 | bool FuseFileDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 58 | uint32_t start_block) const { |
| 59 | uint64_t offset = static_cast<uint64_t>(start_block) * fuse_block_size_; |
| 60 | if (fetch_size > file_size_ || offset > file_size_ - fetch_size) { |
| 61 | fprintf(stderr, |
| 62 | "Out of bound read, start block: %" PRIu32 ", fetch size: %" PRIu32 |
| 63 | ", file size %" PRIu64 "\n", |
| 64 | start_block, fetch_size, file_size_); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if (!android::base::ReadFullyAtOffset(fd_, buffer, fetch_size, offset)) { |
| 69 | fprintf(stderr, "Failed to read fetch size: %" PRIu32 " bytes data at offset %" PRIu64 ": %s\n", |
| 70 | fetch_size, offset, strerror(errno)); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void FuseFileDataProvider::Close() { |
| 78 | fd_.reset(); |
| 79 | } |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 80 | |
| 81 | FuseBlockDataProvider::FuseBlockDataProvider(uint64_t file_size, uint32_t fuse_block_size, |
| 82 | android::base::unique_fd&& fd, |
| 83 | uint32_t source_block_size, RangeSet ranges) |
| 84 | : FuseDataProvider(file_size, fuse_block_size), |
| 85 | fd_(std::move(fd)), |
| 86 | source_block_size_(source_block_size), |
| 87 | ranges_(std::move(ranges)) { |
| 88 | // Make sure the offset is also aligned with the blocks on the block device when we call |
| 89 | // ReadBlockAlignedData(). |
| 90 | CHECK_EQ(0, fuse_block_size_ % source_block_size_); |
| 91 | } |
| 92 | |
| 93 | bool FuseBlockDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size, |
| 94 | uint32_t start_block) const { |
| 95 | uint64_t offset = static_cast<uint64_t>(start_block) * fuse_block_size_; |
| 96 | if (fetch_size > file_size_ || offset > file_size_ - fetch_size) { |
| 97 | LOG(ERROR) << "Out of bound read, offset: " << offset << ", fetch size: " << fetch_size |
| 98 | << ", file size " << file_size_; |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | auto read_ranges = |
| 103 | ranges_.GetSubRanges(offset / source_block_size_, fetch_size / source_block_size_); |
| 104 | if (!read_ranges) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | uint8_t* next_out = buffer; |
| 109 | for (const auto& [range_start, range_end] : read_ranges.value()) { |
| 110 | uint64_t bytes_start = static_cast<uint64_t>(range_start) * source_block_size_; |
| 111 | uint64_t bytes_to_read = static_cast<uint64_t>(range_end - range_start) * source_block_size_; |
| 112 | if (!android::base::ReadFullyAtOffset(fd_, next_out, bytes_to_read, bytes_start)) { |
| 113 | PLOG(ERROR) << "Failed to read " << bytes_to_read << " bytes at offset " << bytes_start; |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | next_out += bytes_to_read; |
| 118 | } |
| 119 | |
| 120 | if (uint64_t tailing_bytes = fetch_size % source_block_size_; tailing_bytes != 0) { |
| 121 | // Calculate the offset to last partial block. |
| 122 | uint64_t tailing_offset = |
| 123 | read_ranges.value() |
| 124 | ? static_cast<uint64_t>((read_ranges->cend() - 1)->second) * source_block_size_ |
| 125 | : static_cast<uint64_t>(start_block) * source_block_size_; |
| 126 | if (!android::base::ReadFullyAtOffset(fd_, next_out, tailing_bytes, tailing_offset)) { |
| 127 | PLOG(ERROR) << "Failed to read tailing " << tailing_bytes << " bytes at offset " |
| 128 | << tailing_offset; |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
Tianjie Xu | f6158eb | 2019-06-11 16:09:07 -0700 | [diff] [blame] | 135 | std::unique_ptr<FuseDataProvider> FuseBlockDataProvider::CreateFromBlockMap( |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 136 | const std::string& block_map_path, uint32_t fuse_block_size) { |
| 137 | auto block_map = BlockMapData::ParseBlockMapFile(block_map_path); |
| 138 | if (!block_map) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map.path().c_str(), O_RDONLY))); |
| 143 | if (fd == -1) { |
| 144 | PLOG(ERROR) << "Failed to open " << block_map.path(); |
| 145 | return nullptr; |
| 146 | } |
| 147 | |
| 148 | return std::unique_ptr<FuseBlockDataProvider>( |
| 149 | new FuseBlockDataProvider(block_map.file_size(), fuse_block_size, std::move(fd), |
| 150 | block_map.block_size(), block_map.block_ranges())); |
| 151 | } |
| 152 | |
| 153 | void FuseBlockDataProvider::Close() { |
| 154 | fd_.reset(); |
| 155 | } |