1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "fs_manager/cmp_partition.h"
16#include "log/log.h"
17
18using namespace std;
19
20namespace Updater {
21static PartitonList g_updaterPlist;
22static PartitionChangeType ComparePartition(const PartitonList &plist, struct Partition &newp)
23{
24    PartitionChange cg = PARTITION_NEW;
25    struct Partition *oldp = nullptr;
26    for (auto& p : plist) {
27        if (!strcmp(p->partName.c_str(), newp.partName.c_str())) {
28            LOG(INFO) << "compare_partition old " << p->partName;
29            cg = PARTITION_OLD;
30            oldp = p;
31            break;
32        }
33    }
34    PartitionChangeType ct = NOT_CHANGE;
35    switch (cg) {
36        case PARTITION_NEW:
37            ct = NEW_PARTITION;
38            newp.changeType = NEW_PARTITION;
39            break;
40        case PARTITION_OLD:
41            if (oldp->start != newp.start) {
42                LOG(INFO) << "newp.start " << newp.start;
43                ct = START_CHANGE;
44                oldp->changeType = START_CHANGE;
45                newp.changeType = START_CHANGE;
46            } else if (oldp->length != newp.length) {
47                LOG(INFO) << "newp.length " << newp.length;
48                ct =  LENGTH_CHANGE;
49                oldp->changeType = LENGTH_CHANGE;
50                newp.changeType = LENGTH_CHANGE;
51            } else {
52                ct =  NOT_CHANGE;
53                oldp->changeType = NOT_CHANGE;
54            }
55            break;
56        default:
57            break;
58    }
59    return ct;
60}
61
62static int TraversePartitionList(const PartitonList &nlist, const PartitonList &olist, PartitonList &ulist)
63{
64    if (nlist.empty() || olist.empty()) {
65        return 0;
66    }
67
68    ulist.clear();
69    PartitionChangeType changeType = NOT_CHANGE;
70    for (auto& p : nlist) {
71        changeType = ComparePartition(olist, *p);
72        if (changeType != NOT_CHANGE) {
73            LOG(INFO) << "change p->partName " << p->partName;
74            ulist.push_back(p);
75        }
76    }
77    return 1;
78}
79
80int RegisterUpdaterPartitionList(const PartitonList &nlist, const PartitonList &olist)
81{
82    if (nlist.empty() || olist.empty()) {
83        return 0;
84    }
85
86    g_updaterPlist.clear();
87    int ret = TraversePartitionList(nlist, olist, g_updaterPlist);
88
89    return ret;
90}
91
92int GetRegisterUpdaterPartitionList(PartitonList &ulist)
93{
94    if (g_updaterPlist.empty()) {
95        return 1;
96    }
97
98    ulist.clear();
99    ulist.assign(g_updaterPlist.begin(), g_updaterPlist.end());
100    if (ulist.empty()) {
101        return 0;
102    }
103    return 1;
104}
105} // namespace Updater
106