• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "metadata/store_meta_data.h"
17 
18 #include "metadata/auto_launch_meta_data.h"
19 #include "metadata/secret_key_meta_data.h"
20 #include "metadata/store_debug_info.h"
21 #include "metadata/store_meta_data_local.h"
22 #include "metadata/strategy_meta_data.h"
23 #include "utils/anonymous.h"
24 #include "utils/constant.h"
25 #include "utils/crypto.h"
26 namespace OHOS {
27 namespace DistributedData {
28 constexpr uint32_t StoreMetaData::CURRENT_VERSION;
29 constexpr uint32_t StoreMetaData::FIELD_CHANGED_TAG;
30 constexpr const char *StoreMetaData::KEY_PREFIX;
Marshal(json & node) const31 bool StoreMetaData::Marshal(json &node) const
32 {
33     SetValue(node[GET_NAME(version)], version);
34     SetValue(node[GET_NAME(isAutoSync)], isAutoSync);
35     SetValue(node[GET_NAME(isBackup)], isBackup);
36     SetValue(node[GET_NAME(isEncrypt)], isEncrypt);
37     SetValue(node[GET_NAME(isManualClean)], isManualClean);
38     SetValue(node[GET_NAME(isDirty)], isDirty);
39     SetValue(node[GET_NAME(isSearchable)], isSearchable);
40     SetValue(node[GET_NAME(isNeedCompress)], isNeedCompress);
41     SetValue(node[GET_NAME(storeType)], storeType);
42     SetValue(node[GET_NAME(securityLevel)], securityLevel);
43     SetValue(node[GET_NAME(area)], area);
44     SetValue(node[GET_NAME(uid)], uid);
45     SetValue(node[GET_NAME(tokenId)], tokenId);
46     SetValue(node[GET_NAME(instanceId)], instanceId);
47     SetValue(node[GET_NAME(appId)], appId);
48     SetValue(node[GET_NAME(appType)], appType);
49     SetValue(node[GET_NAME(bundleName)], bundleName);
50     SetValue(node[GET_NAME(hapName)], hapName);
51     SetValue(node[GET_NAME(dataDir)], dataDir);
52     SetValue(node[GET_NAME(deviceId)], deviceId);
53     SetValue(node[GET_NAME(schema)], schema);
54     SetValue(node[GET_NAME(storeId)], storeId);
55     SetValue(node[GET_NAME(user)], user);
56     SetValue(node[GET_NAME(account)], account);
57     SetValue(node[GET_NAME(dataType)], dataType);
58     SetValue(node[GET_NAME(enableCloud)], enableCloud);
59     SetValue(node[GET_NAME(cloudAutoSync)], cloudAutoSync);
60     SetValue(node[GET_NAME(asyncDownloadAsset)], asyncDownloadAsset);
61     SetValue(node[GET_NAME(isNeedUpdateDeviceId)], isNeedUpdateDeviceId);
62     // compatible with the versions which lower than VERSION_TAG_0000
63     SetValue(node[GET_NAME(kvStoreType)], storeType);
64     SetValue(node[GET_NAME(deviceAccountID)], user);
65     SetValue(node[GET_NAME(userId)], account);
66     SetValue(node[GET_NAME(UID)], uid);
67     SetValue(node[GET_NAME(customDir)], customDir);
68     SetValue(node[GET_NAME(authType)], authType);
69     SetValue(node[GET_NAME(haMode)], haMode);
70     return true;
71 }
72 
Unmarshal(const json & node)73 bool StoreMetaData::Unmarshal(const json &node)
74 {
75     GetValue(node, GET_NAME(version), version);
76     GetValue(node, GET_NAME(isAutoSync), isAutoSync);
77     GetValue(node, GET_NAME(isBackup), isBackup);
78     GetValue(node, GET_NAME(isDirty), isDirty);
79     GetValue(node, GET_NAME(isEncrypt), isEncrypt);
80     GetValue(node, GET_NAME(isManualClean), isManualClean);
81     GetValue(node, GET_NAME(isSearchable), isSearchable);
82     GetValue(node, GET_NAME(isNeedCompress), isNeedCompress);
83     GetValue(node, GET_NAME(storeType), storeType);
84     GetValue(node, GET_NAME(securityLevel), securityLevel);
85     GetValue(node, GET_NAME(area), area);
86     GetValue(node, GET_NAME(uid), uid);
87     GetValue(node, GET_NAME(tokenId), tokenId);
88     GetValue(node, GET_NAME(instanceId), instanceId);
89     GetValue(node, GET_NAME(appId), appId);
90     GetValue(node, GET_NAME(appType), appType);
91     GetValue(node, GET_NAME(bundleName), bundleName);
92     GetValue(node, GET_NAME(hapName), hapName);
93     GetValue(node, GET_NAME(dataDir), dataDir);
94     GetValue(node, GET_NAME(deviceId), deviceId);
95     GetValue(node, GET_NAME(schema), schema);
96     GetValue(node, GET_NAME(storeId), storeId);
97     GetValue(node, GET_NAME(user), user);
98     GetValue(node, GET_NAME(account), account);
99     GetValue(node, GET_NAME(dataType), dataType);
100     GetValue(node, GET_NAME(enableCloud), enableCloud);
101     GetValue(node, GET_NAME(cloudAutoSync), cloudAutoSync);
102     GetValue(node, GET_NAME(asyncDownloadAsset), asyncDownloadAsset);
103     GetValue(node, GET_NAME(isNeedUpdateDeviceId), isNeedUpdateDeviceId);
104     // compatible with the older versions
105     if (version < FIELD_CHANGED_TAG) {
106         GetValue(node, GET_NAME(kvStoreType), storeType);
107         GetValue(node, GET_NAME(UID), uid);
108         GetValue(node, GET_NAME(deviceAccountID), user);
109         GetValue(node, GET_NAME(userId), account);
110     }
111     GetValue(node, GET_NAME(customDir), customDir);
112     GetValue(node, GET_NAME(authType), authType);
113     GetValue(node, GET_NAME(haMode), haMode);
114     return true;
115 }
116 
StoreMetaData()117 StoreMetaData::StoreMetaData()
118 {
119 }
120 
~StoreMetaData()121 StoreMetaData::~StoreMetaData()
122 {
123 }
124 
StoreMetaData(const std::string & userId,const std::string & appId,const std::string & storeId)125 StoreMetaData::StoreMetaData(const std::string &userId, const std::string &appId, const std::string &storeId)
126     : appId(appId), storeId(storeId), user(userId)
127 {
128 }
129 
StoreMetaData(const StoreInfo & storeInfo)130 StoreMetaData::StoreMetaData(const StoreInfo &storeInfo)
131     : instanceId(storeInfo.instanceId), bundleName(storeInfo.bundleName), dataDir(storeInfo.path),
132       storeId(storeInfo.storeName), user(std::to_string(storeInfo.user))
133 {
134 }
135 
operator ==(const StoreMetaData & metaData) const136 bool StoreMetaData::operator==(const StoreMetaData &metaData) const
137 {
138     if (Constant::NotEqual(isAutoSync, metaData.isAutoSync) || Constant::NotEqual(isBackup, metaData.isBackup) ||
139         Constant::NotEqual(isDirty, metaData.isDirty) || Constant::NotEqual(isEncrypt, metaData.isEncrypt) ||
140         Constant::NotEqual(isSearchable, metaData.isSearchable) ||
141         Constant::NotEqual(isNeedCompress, metaData.isNeedCompress) ||
142         Constant::NotEqual(enableCloud, metaData.enableCloud) ||
143         Constant::NotEqual(cloudAutoSync, metaData.cloudAutoSync) ||
144         Constant::NotEqual(isManualClean, metaData.isManualClean) ||
145         Constant::NotEqual(isNeedUpdateDeviceId, metaData.isNeedUpdateDeviceId)) {
146         return false;
147     }
148     return (version == metaData.version && storeType == metaData.storeType && dataType == metaData.dataType &&
149             securityLevel == metaData.securityLevel && area == metaData.area && uid == metaData.uid &&
150             tokenId == metaData.tokenId && instanceId == metaData.instanceId && appId == metaData.appId &&
151             appType == metaData.appType && bundleName == metaData.bundleName && dataDir == metaData.dataDir &&
152             storeId == metaData.storeId && user == metaData.user && deviceId == metaData.deviceId &&
153             account == metaData.account && authType == metaData.authType && haMode == metaData.haMode);
154 }
155 
operator !=(const StoreMetaData & metaData) const156 bool StoreMetaData::operator!=(const StoreMetaData &metaData) const
157 {
158     return !(*this == metaData);
159 }
160 
GetKey() const161 std::string StoreMetaData::GetKey() const
162 {
163     if (instanceId == 0) {
164         return GetKey({ deviceId, user, "default", bundleName, storeId, dataDir });
165     }
166     return GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId), dataDir });
167 }
168 
GetKeyLocal() const169 std::string StoreMetaData::GetKeyLocal() const
170 {
171     if (instanceId == 0) {
172         return StoreMetaDataLocal::GetKey({ deviceId, user, "default", bundleName, storeId, dataDir });
173     }
174     return StoreMetaDataLocal::GetKey(
175         { deviceId, user, "default", bundleName, storeId, std::to_string(instanceId), dataDir });
176 }
177 
GetSecretKey() const178 std::string StoreMetaData::GetSecretKey() const
179 {
180     if (version < UUID_CHANGED_TAG) {
181         return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId, dataDir });
182     }
183     return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId, std::to_string(instanceId), dataDir });
184 }
185 
GetBackupSecretKey() const186 std::string StoreMetaData::GetBackupSecretKey() const
187 {
188     if (version < UUID_CHANGED_TAG) {
189         return SecretKeyMetaData::GetBackupKey({ user, "default", bundleName, storeId });
190     }
191     return SecretKeyMetaData::GetBackupKey({ user, "default", bundleName, storeId, std::to_string(instanceId) });
192 }
193 
GetAutoLaunchKey() const194 std::string StoreMetaData::GetAutoLaunchKey() const
195 {
196     return AutoLaunchMetaData::GetPrefix({ deviceId, user, "default", bundleName, storeId });
197 }
198 
GetDebugInfoKey() const199 std::string StoreMetaData::GetDebugInfoKey() const
200 {
201     return StoreDebugInfo::GetPrefix(
202         { deviceId, user, "default", bundleName, storeId, std::to_string(instanceId), dataDir });
203 }
204 
GetDebugInfoKeyWithoutPath() const205 std::string StoreMetaData::GetDebugInfoKeyWithoutPath() const
206 {
207     return StoreDebugInfo::GetPrefix({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
208 }
209 
GetDfxInfoKey() const210 std::string StoreMetaData::GetDfxInfoKey() const
211 {
212     return StoreDfxInfo::GetPrefix(
213         { deviceId, user, "default", bundleName, storeId, std::to_string(instanceId), dataDir });
214 }
215 
GetDfxInfoKeyWithoutPath() const216 std::string StoreMetaData::GetDfxInfoKeyWithoutPath() const
217 {
218     return StoreDfxInfo::GetPrefix({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
219 }
220 
GetStoreAlias() const221 std::string StoreMetaData::GetStoreAlias() const
222 {
223     return Anonymous::Change(storeId);
224 }
225 
GetStrategyKey() const226 std::string StoreMetaData::GetStrategyKey() const
227 {
228     if (instanceId == 0) {
229         return StrategyMeta::GetPrefix({ deviceId, user, "default", bundleName, storeId });
230     }
231     return StrategyMeta::GetPrefix({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
232 }
233 
GetKey(const std::initializer_list<std::string> & fields)234 std::string StoreMetaData::GetKey(const std::initializer_list<std::string> &fields)
235 {
236     std::string prefix = KEY_PREFIX;
237     for (const auto &field : fields) {
238         prefix.append(Constant::KEY_SEPARATOR).append(field);
239     }
240     return prefix;
241 }
242 
GetPrefix(const std::initializer_list<std::string> & fields)243 std::string StoreMetaData::GetPrefix(const std::initializer_list<std::string> &fields)
244 {
245     return GetKey(fields).append(Constant::KEY_SEPARATOR);
246 }
247 
GetStoreInfo() const248 StoreInfo StoreMetaData::GetStoreInfo() const
249 {
250     StoreInfo info;
251     info.instanceId = instanceId;
252     info.bundleName = bundleName;
253     info.storeName = storeId;
254     info.user = atoi(user.c_str());
255     return info;
256 }
257 
GetCloneSecretKey() const258 std::string StoreMetaData::GetCloneSecretKey() const
259 {
260     return SecretKeyMetaData::GetCloneKey({ user, "default", bundleName, storeId, std::to_string(instanceId) });
261 }
262 
GetKeyWithoutPath() const263 std::string StoreMetaData::GetKeyWithoutPath() const
264 {
265     if (instanceId == 0) {
266         return GetKey({ deviceId, user, "default", bundleName, storeId });
267     }
268     return GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
269 }
270 
GetKeyLocalWithoutPath() const271 std::string StoreMetaData::GetKeyLocalWithoutPath() const
272 {
273     if (instanceId == 0) {
274         return StoreMetaDataLocal::GetKey({ deviceId, user, "default", bundleName, storeId });
275     }
276     return StoreMetaDataLocal::GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
277 }
278 
GetSecretKeyWithoutPath() const279 std::string StoreMetaData::GetSecretKeyWithoutPath() const
280 {
281     if (version < UUID_CHANGED_TAG) {
282         return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId });
283     }
284     return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId, std::to_string(instanceId) });
285 }
286 
StoreMetaMapping()287 StoreMetaMapping::StoreMetaMapping() {}
StoreMetaMapping(const StoreMetaData & storeMeta)288 StoreMetaMapping::StoreMetaMapping(const StoreMetaData &storeMeta) : StoreMetaData(storeMeta) {}
~StoreMetaMapping()289 StoreMetaMapping::~StoreMetaMapping() {}
290 
Marshal(json & node) const291 bool StoreMetaMapping::Marshal(json &node) const
292 {
293     StoreMetaData::Marshal(node);
294     SetValue(node[GET_NAME(cloudPath)], cloudPath);
295     SetValue(node[GET_NAME(devicePath)], devicePath);
296     SetValue(node[GET_NAME(searchPath)], searchPath);
297     return true;
298 }
Unmarshal(const Serializable::json & node)299 bool StoreMetaMapping::Unmarshal(const Serializable::json &node)
300 {
301     StoreMetaData::Unmarshal(node);
302     GetValue(node, GET_NAME(cloudPath), cloudPath);
303     GetValue(node, GET_NAME(devicePath), devicePath);
304     GetValue(node, GET_NAME(searchPath), searchPath);
305     return true;
306 }
307 
GetKey() const308 std::string StoreMetaMapping::GetKey() const
309 {
310     if (instanceId == 0) {
311         return GetKey({ deviceId, user, "default", bundleName, storeId });
312     }
313     return GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
314 }
315 
GetCloudStoreMetaKey() const316 std::string StoreMetaMapping::GetCloudStoreMetaKey() const
317 {
318     return GetStoreMetaKeyWithPath(cloudPath);
319 }
320 
GetDeviceStoreMetaKey() const321 std::string StoreMetaMapping::GetDeviceStoreMetaKey() const
322 {
323     return GetStoreMetaKeyWithPath(devicePath);
324 }
325 
GetPrefix(const std::initializer_list<std::string> & fields)326 std::string StoreMetaMapping::GetPrefix(const std::initializer_list<std::string> &fields)
327 {
328     return GetKey(fields).append(Constant::KEY_SEPARATOR);
329 }
330 
operator =(const StoreMetaData & meta)331 StoreMetaMapping& StoreMetaMapping::operator=(const StoreMetaData &meta)
332 {
333     if (this == &meta) {
334         return *this;
335     }
336     this->StoreMetaData::operator=(meta);
337     return *this;
338 }
339 
GetStoreMetaKeyWithPath(const std::string & path) const340 std::string StoreMetaMapping::GetStoreMetaKeyWithPath(const std::string &path) const
341 {
342     if (instanceId == 0) {
343         return StoreMetaData::GetKey({ deviceId, user, "default", bundleName, storeId, path });
344     }
345     return StoreMetaData::GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId), path });
346 }
347 
GetKey(const std::initializer_list<std::string> & fields)348 std::string StoreMetaMapping::GetKey(const std::initializer_list<std::string> &fields)
349 {
350     std::string prefix = KEY_PREFIX;
351     for (const auto &field : fields) {
352         prefix.append(Constant::KEY_SEPARATOR).append(field);
353     }
354     return prefix;
355 }
StoreMetaMapping(const StoreInfo & storeInfo)356 StoreMetaMapping::StoreMetaMapping(const StoreInfo &storeInfo) : StoreMetaData(storeInfo) {}
357 
358 } // namespace DistributedData
359 } // namespace OHOS