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 #include <libsnapshot/cow_format.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <android-base/logging.h>
22
23 namespace android {
24 namespace snapshot {
25
operator <<(std::ostream & os,CowOperation const & op)26 std::ostream& operator<<(std::ostream& os, CowOperation const& op) {
27 os << "CowOperation(type:";
28 if (op.type == kCowCopyOp)
29 os << "kCowCopyOp, ";
30 else if (op.type == kCowReplaceOp)
31 os << "kCowReplaceOp, ";
32 else if (op.type == kCowZeroOp)
33 os << "kZeroOp, ";
34 else if (op.type == kCowFooterOp)
35 os << "kCowFooterOp, ";
36 else if (op.type == kCowLabelOp)
37 os << "kCowLabelOp, ";
38 else if (op.type == kCowClusterOp)
39 os << "kCowClusterOp ";
40 else if (op.type == kCowFooterOp)
41 os << "kCowFooterOp ";
42 else
43 os << (int)op.type << "?,";
44 os << "compression:";
45 if (op.compression == kCowCompressNone)
46 os << "kCowCompressNone, ";
47 else if (op.compression == kCowCompressGz)
48 os << "kCowCompressGz, ";
49 else if (op.compression == kCowCompressBrotli)
50 os << "kCowCompressBrotli, ";
51 else
52 os << (int)op.compression << "?, ";
53 os << "data_length:" << op.data_length << ",\t";
54 os << "new_block:" << op.new_block << ",\t";
55 os << "source:" << op.source << ")";
56 return os;
57 }
58
GetNextOpOffset(const CowOperation & op,uint32_t cluster_ops)59 int64_t GetNextOpOffset(const CowOperation& op, uint32_t cluster_ops) {
60 if (op.type == kCowClusterOp) {
61 return op.source;
62 } else if (op.type == kCowReplaceOp && cluster_ops == 0) {
63 return op.data_length;
64 } else {
65 return 0;
66 }
67 }
68
GetNextDataOffset(const CowOperation & op,uint32_t cluster_ops)69 int64_t GetNextDataOffset(const CowOperation& op, uint32_t cluster_ops) {
70 if (op.type == kCowClusterOp) {
71 return cluster_ops * sizeof(CowOperation);
72 } else if (cluster_ops == 0) {
73 return sizeof(CowOperation);
74 } else {
75 return 0;
76 }
77 }
78
IsMetadataOp(const CowOperation & op)79 bool IsMetadataOp(const CowOperation& op) {
80 switch (op.type) {
81 case kCowLabelOp:
82 case kCowClusterOp:
83 case kCowFooterOp:
84 return true;
85 default:
86 return false;
87 }
88 }
89
90 } // namespace snapshot
91 } // namespace android
92