• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CompressedSnapshotReader : public chromeos_update_engine::FileDescriptor {
30   public:
31     CompressedSnapshotReader(std::unique_ptr<ICowReader>&& cow,
32                              const std::optional<std::string>& source_device,
33                              std::optional<uint64_t> block_dev_size);
34 
35     bool Open(const char* path, int flags, mode_t mode) override;
36     bool Open(const char* path, int flags) override;
37     ssize_t Write(const void* buf, size_t count) override;
38     bool BlkIoctl(int request, uint64_t start, uint64_t length, int* result) override;
39     ssize_t Read(void* buf, size_t count) override;
40     off64_t Seek(off64_t offset, int whence) override;
41     uint64_t BlockDevSize() override;
42     bool Close() override;
43     bool IsSettingErrno() override;
44     bool IsOpen() override;
45     bool Flush() override;
46 
47   private:
48     ssize_t ReadBlock(uint64_t chunk, size_t start_offset, void* buffer, size_t size);
49     android::base::borrowed_fd GetSourceFd();
50 
51     std::unique_ptr<ICowReader> cow_;
52     std::unique_ptr<ICowOpIter> op_iter_;
53     uint32_t block_size_ = 0;
54 
55     std::optional<std::string> source_device_;
56     android::base::unique_fd source_fd_;
57     uint64_t block_device_size_ = 0;
58     off64_t offset_ = 0;
59 
60     std::vector<const CowOperation*> ops_;
61 };
62 
63 }  // namespace snapshot
64 }  // namespace android
65