1 //
2 // Copyright (C) 2023 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 <unordered_map>
21 
22 #include <android-base/unique_fd.h>
23 #include <libsnapshot/cow_format.h>
24 
25 namespace android {
26 namespace snapshot {
27 
28 struct TranslatedCowOps {
29     CowHeaderV3 header;
30     std::shared_ptr<std::vector<CowOperationV3>> ops;
31 };
32 
33 class CowParserBase {
34   public:
35     virtual ~CowParserBase() = default;
36 
37     virtual bool Parse(android::base::borrowed_fd fd, const CowHeaderV3& header,
38                        std::optional<uint64_t> label = {}) = 0;
39     virtual bool Translate(TranslatedCowOps* out) = 0;
footer()40     virtual std::optional<CowFooter> footer() const { return std::nullopt; }
xor_data_loc()41     std::shared_ptr<std::unordered_map<uint64_t, uint64_t>> xor_data_loc() {
42         return xor_data_loc_;
43     };
44 
fd_size()45     uint64_t fd_size() const { return fd_size_; }
last_label()46     const std::optional<uint64_t>& last_label() const { return last_label_; }
47 
48   protected:
49     CowHeaderV3 header_ = {};
50     uint64_t fd_size_;
51     std::optional<uint64_t> last_label_;
52     std::shared_ptr<std::unordered_map<uint64_t, uint64_t>> xor_data_loc_ = {};
53 };
54 
55 }  // namespace snapshot
56 }  // namespace android
57