• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #define LOG_TAG "Upgrade"
16 #include "upgrade.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 
21 #include "accesstoken_kit.h"
22 #include "device_manager_adapter.h"
23 #include "directory/directory_manager.h"
24 #include "kvdb_general_store.h"
25 #include "log_print.h"
26 #include "metadata/meta_data_manager.h"
27 #include "metadata/secret_key_meta_data.h"
28 #include "utils/anonymous.h"
29 namespace OHOS::DistributedKv {
30 using namespace OHOS::DistributedData;
31 using system_clock = std::chrono::system_clock;
32 using DMAdapter = DistributedData::DeviceManagerAdapter;
33 using DBKey = DistributedDB::Key;
34 
GetInstance()35 Upgrade &Upgrade::GetInstance()
36 {
37     static Upgrade upgrade;
38     return upgrade;
39 }
40 
UpdateStore(const StoreMeta & old,const StoreMeta & meta,const std::vector<uint8_t> & pwd)41 Upgrade::DBStatus Upgrade::UpdateStore(const StoreMeta &old, const StoreMeta &meta, const std::vector<uint8_t> &pwd)
42 {
43     if (old.isNeedUpdateDeviceId && !old.isEncrypt) {
44         auto store = GetDBStore(meta, pwd);
45         if (store == nullptr) {
46             ZLOGI("get store failed, appId:%{public}s storeId:%{public}s", old.appId.c_str(),
47                 Anonymous::Change(old.storeId).c_str());
48             return DBStatus::DB_ERROR;
49         }
50         store->OperateDataStatus(static_cast<uint32_t>(DistributedDB::DataOperator::UPDATE_TIME) |
51             static_cast<uint32_t>(DistributedDB::DataOperator::RESET_UPLOAD_CLOUD));
52     }
53 
54     if ((old.version < StoreMeta::UUID_CHANGED_TAG || (old.isNeedUpdateDeviceId && !old.isEncrypt)) &&
55         old.storeType == DEVICE_COLLABORATION) {
56         auto upStatus = Upgrade::GetInstance().UpdateUuid(old, meta, pwd);
57         if (upStatus != DBStatus::OK) {
58             return DBStatus::DB_ERROR;
59         }
60     }
61 
62     if (old.dataDir == meta.dataDir) {
63         return DBStatus::OK;
64     }
65 
66     if (!exporter_ || !cleaner_) {
67         return DBStatus::NOT_SUPPORT;
68     }
69 
70     DBPassword password;
71     auto backupFile = exporter_(old, password);
72     if (backupFile.empty()) {
73         return DBStatus::NOT_FOUND;
74     }
75 
76     auto kvStore = GetDBStore(meta, pwd);
77     if (kvStore == nullptr) {
78         return DBStatus::DB_ERROR;
79     }
80 
81     cleaner_(old);
82     return DBStatus::OK;
83 }
84 
ExportStore(const StoreMeta & old,const StoreMeta & meta)85 Upgrade::DBStatus Upgrade::ExportStore(const StoreMeta &old, const StoreMeta &meta)
86 {
87     if (old.dataDir == meta.dataDir) {
88         return DBStatus::OK;
89     }
90 
91     if (!exporter_) {
92         return DBStatus::NOT_SUPPORT;
93     }
94 
95     DBPassword password;
96     auto backupFile = exporter_(old, password);
97     if (backupFile.empty()) {
98         return DBStatus::NOT_FOUND;
99     }
100     return DBStatus::OK;
101 }
102 
UpdateUuid(const StoreMeta & old,const StoreMeta & meta,const std::vector<uint8_t> & pwd)103 Upgrade::DBStatus Upgrade::UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector<uint8_t> &pwd)
104 {
105     auto kvStore = GetDBStore(meta, pwd);
106     if (kvStore == nullptr) {
107         return DBStatus::DB_ERROR;
108     }
109     kvStore->RemoveDeviceData();
110     auto uuid = GetEncryptedUuidByMeta(meta);
111     auto dbStatus = kvStore->UpdateKey([uuid](const DBKey &originKey, DBKey &newKey) {
112         newKey = originKey;
113         errno_t err = EOK;
114         err = memcpy_s(newKey.data(), newKey.size(), uuid.data(), uuid.size());
115         if (err != EOK) {
116             ZLOGE("memcpy_s failed, err:%{public}d", err);
117         }
118     });
119     if (dbStatus != DBStatus::OK) {
120         ZLOGE("fail to update Uuid, status:%{public}d", dbStatus);
121     }
122     return dbStatus;
123 }
124 
RegisterExporter(uint32_t version,Exporter exporter)125 bool Upgrade::RegisterExporter(uint32_t version, Exporter exporter)
126 {
127     (void)version;
128     exporter_ = std::move(exporter);
129     return exporter_ != nullptr;
130 }
131 
RegisterCleaner(uint32_t version,Cleaner cleaner)132 bool Upgrade::RegisterCleaner(uint32_t version, Cleaner cleaner)
133 {
134     (void)version;
135     cleaner_ = std::move(cleaner);
136     return cleaner_ != nullptr;
137 }
138 
GetDBStore(const StoreMeta & meta,const std::vector<uint8_t> & pwd)139 Upgrade::AutoStore Upgrade::GetDBStore(const StoreMeta &meta, const std::vector<uint8_t> &pwd)
140 {
141     DBManager manager(meta.appId, meta.user, meta.instanceId);
142     manager.SetKvStoreConfig({ DirectoryManager::GetInstance().GetStorePath(meta) });
143     auto release = [&manager](DBStore *store) { manager.CloseKvStore(store); };
144     DBPassword password;
145     password.SetValue(pwd.data(), pwd.size());
146     AutoStore dbStore(nullptr, release);
147     manager.GetKvStore(meta.storeId, KVDBGeneralStore::GetDBOption(meta, password),
148         [&dbStore](auto dbStatus, auto *tmpStore) {
149             dbStore.reset(tmpStore);
150         });
151     return dbStore;
152 }
153 
GetEncryptedUuidByMeta(const StoreMeta & meta)154 std::string Upgrade::GetEncryptedUuidByMeta(const StoreMeta &meta)
155 {
156     std::string keyUuid = meta.appId + meta.deviceId;
157     auto pair = calcUuid_.Find(keyUuid);
158     if (pair.first) {
159         return pair.second;
160     }
161     std::string uuid;
162     if (OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(meta.tokenId) ==
163         OHOS::Security::AccessToken::TOKEN_HAP) {
164         uuid = DMAdapter::GetInstance().CalcClientUuid(meta.appId, meta.deviceId);
165         calcUuid_.Insert(keyUuid, uuid);
166         return uuid;
167     }
168     uuid = DMAdapter::GetInstance().CalcClientUuid(" ", meta.deviceId);
169     calcUuid_.Insert(keyUuid, uuid);
170     return uuid;
171 }
172 } // namespace OHOS::DistributedKv