1 // 2 // Copyright (C) 2021 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 <cstdint> 18 #include <memory> 19 #include <string> 20 21 #include <libsnapshot/cow_writer.h> 22 23 #include "update_engine/payload_consumer/file_descriptor.h" 24 25 namespace chromeos_update_engine { 26 27 // A Readable/Writable FileDescriptor class. This is a simple wrapper around 28 // CowWriter. Only intended to be used by FileSystemVerifierAction for writing 29 // FEC. Writes must be block aligned(4096) or write will fail. 30 class CowWriterFileDescriptor final : public FileDescriptor { 31 public: 32 // |cow_reader| should be obtained by calling 33 // |cow_writer->OpenReader()->OpenFileDescriptor()| 34 CowWriterFileDescriptor( 35 std::unique_ptr<android::snapshot::ICowWriter> cow_writer, 36 std::unique_ptr<FileDescriptor> cow_reader, 37 const std::optional<std::string>& source_device); 38 ~CowWriterFileDescriptor(); 39 40 bool Open(const char* path, int flags, mode_t mode) override; 41 bool Open(const char* path, int flags) override; 42 43 ssize_t Read(void* buf, size_t count) override; 44 45 // |count| must be block aligned, current offset of this fd must also be block 46 // aligned. 47 ssize_t Write(const void* buf, size_t count) override; 48 49 off64_t Seek(off64_t offset, int whence) override; 50 51 uint64_t BlockDevSize() override; 52 53 bool BlkIoctl(int request, 54 uint64_t start, 55 uint64_t length, 56 int* result) override; 57 58 bool Flush() override; 59 60 bool Close() override; 61 62 bool IsSettingErrno() override; 63 64 bool IsOpen() override; 65 66 private: 67 std::unique_ptr<android::snapshot::ICowWriter> cow_writer_; 68 FileDescriptorPtr cow_reader_; 69 std::optional<std::string> source_device_; 70 bool dirty_ = false; 71 }; 72 } // namespace chromeos_update_engine 73