• 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   ~CowWriterFileDescriptor();
34 
35   bool Open(const char* path, int flags, mode_t mode) override;
36   bool Open(const char* path, int flags) override;
37 
38   ssize_t Read(void* buf, size_t count) override;
39 
40   // |count| must be block aligned, current offset of this fd must also be block
41   // aligned.
42   ssize_t Write(const void* buf, size_t count) override;
43 
44   off64_t Seek(off64_t offset, int whence) override;
45 
46   uint64_t BlockDevSize() override;
47 
48   bool BlkIoctl(int request,
49                 uint64_t start,
50                 uint64_t length,
51                 int* result) override;
52 
53   bool Flush() override;
54 
55   bool Close() override;
56 
57   bool IsSettingErrno() override;
58 
59   bool IsOpen() override;
60 
61  private:
62   std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer_;
63   FileDescriptorPtr cow_reader_;
64   bool dirty_ = false;
65 };
66 }  // namespace chromeos_update_engine
67