• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <libsnapshot/snapshot_writer.h>
21 
22 #include "update_engine/payload_consumer/file_descriptor.h"
23 
24 namespace chromeos_update_engine {
25 
26 // A Readable/Writable FileDescriptor class. This is a simple wrapper around
27 // CowWriter. Only intended to be used by FileSystemVerifierAction for writing
28 // FEC. Writes must be block aligned(4096) or write will fail.
29 class CowWriterFileDescriptor final : public FileDescriptor {
30  public:
31   explicit CowWriterFileDescriptor(
32       std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer);
33 
34   // |cow_reader| should be obtained by calling |cow_writer->OpenReader()|
35   CowWriterFileDescriptor(
36       std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer,
37       std::unique_ptr<FileDescriptor> cow_reader);
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::ISnapshotWriter> cow_writer_;
68   FileDescriptorPtr cow_reader_;
69   bool dirty_ = false;
70 };
71 }  // namespace chromeos_update_engine
72