1 //
2 // Copyright (C) 2017 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 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FEC_FILE_DESCRIPTOR_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FEC_FILE_DESCRIPTOR_H_
19 
20 #include <fec/io.h>
21 
22 #include "update_engine/payload_consumer/file_descriptor.h"
23 
24 // A FileDescriptor implementation with error correction based on the "libfec"
25 // library. The libfec on the running system allows to parse the error
26 // correction blocks stored in partitions that have verity and error correction
27 // enabled. This information is present in the raw block device, but of course
28 // not available via the dm-verity block device.
29 
30 namespace chromeos_update_engine {
31 
32 // An error corrected file based on FEC.
33 class FecFileDescriptor : public FileDescriptor {
34  public:
35   FecFileDescriptor() = default;
36   ~FecFileDescriptor() = default;
37 
38   // Interface methods.
39   bool Open(const char* path, int flags, mode_t mode) override;
40   bool Open(const char* path, int flags) override;
41   ssize_t Read(void* buf, size_t count) override;
42   ssize_t Write(const void* buf, size_t count) override;
43   off64_t Seek(off64_t offset, int whence) override;
44   uint64_t BlockDevSize() override;
45   bool BlkIoctl(int request,
46                 uint64_t start,
47                 uint64_t length,
48                 int* result) override;
49   bool Flush() override { return true; }
50   bool Close() override;
51   bool IsSettingErrno() override { return true; }
52   bool IsOpen() override {
53     // The bool operator on the fec::io class tells whether the internal
54     // handle is open.
55     return static_cast<bool>(fh_);
56   }
57 
58  protected:
59   fec::io fh_;
60   uint64_t dev_size_{0};
61 };
62 
63 }  // namespace chromeos_update_engine
64 
65 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_FEC_FILE_DESCRIPTOR_H_
66