1 // 2 // Copyright (C) 2020 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 <optional> 20 #include <vector> 21 22 #include <android-base/file.h> 23 #include <libsnapshot/cow_reader.h> 24 #include <payload_consumer/file_descriptor.h> 25 26 namespace android { 27 namespace snapshot { 28 29 class ReadOnlyFileDescriptor : public chromeos_update_engine::FileDescriptor { 30 public: 31 bool Open(const char* path, int flags, mode_t mode) override; 32 bool Open(const char* path, int flags) override; 33 ssize_t Write(const void* buf, size_t count) override; 34 bool BlkIoctl(int request, uint64_t start, uint64_t length, int* result) override; 35 }; 36 37 class ReadFdFileDescriptor : public ReadOnlyFileDescriptor { 38 public: 39 explicit ReadFdFileDescriptor(android::base::unique_fd&& fd); 40 41 ssize_t Read(void* buf, size_t count) override; 42 off64_t Seek(off64_t offset, int whence) override; 43 uint64_t BlockDevSize() override; 44 bool Close() override; 45 bool IsSettingErrno() override; 46 bool IsOpen() override; 47 bool Flush() override; 48 49 private: 50 android::base::unique_fd fd_; 51 }; 52 53 class CompressedSnapshotReader : public ReadOnlyFileDescriptor { 54 public: 55 bool SetCow(std::unique_ptr<CowReader>&& cow); 56 void SetSourceDevice(const std::string& source_device); 57 void SetBlockDeviceSize(uint64_t block_device_size); 58 59 ssize_t Read(void* buf, size_t count) override; 60 off64_t Seek(off64_t offset, int whence) override; 61 uint64_t BlockDevSize() override; 62 bool Close() override; 63 bool IsSettingErrno() override; 64 bool IsOpen() override; 65 bool Flush() override; 66 67 private: 68 ssize_t ReadBlock(uint64_t chunk, IByteSink* sink, size_t start_offset, 69 const std::optional<uint64_t>& max_bytes = {}); 70 android::base::borrowed_fd GetSourceFd(); 71 72 std::unique_ptr<CowReader> cow_; 73 std::unique_ptr<ICowOpIter> op_iter_; 74 uint32_t block_size_ = 0; 75 76 std::optional<std::string> source_device_; 77 android::base::unique_fd source_fd_; 78 uint64_t block_device_size_ = 0; 79 off64_t offset_ = 0; 80 81 std::vector<const CowOperation*> ops_; 82 }; 83 84 } // namespace snapshot 85 } // namespace android 86