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/secret_key_meta_data.h"
19 #include "metadata/store_meta_data_local.h"
20 #include "metadata/strategy_meta_data.h"
21 #include "utils/anonymous.h"
22 #include "utils/constant.h"
23 namespace OHOS {
24 namespace DistributedData {
25 constexpr uint32_t StoreMetaData::CURRENT_VERSION;
26 constexpr uint32_t StoreMetaData::FIELD_CHANGED_TAG;
27 constexpr const char *StoreMetaData::KEY_PREFIX;
Marshal(json & node) const28 bool StoreMetaData::Marshal(json &node) const
29 {
30 SetValue(node[GET_NAME(version)], version);
31 SetValue(node[GET_NAME(isAutoSync)], isAutoSync);
32 SetValue(node[GET_NAME(isBackup)], isBackup);
33 SetValue(node[GET_NAME(isEncrypt)], isEncrypt);
34 SetValue(node[GET_NAME(isDirty)], isDirty);
35 SetValue(node[GET_NAME(storeType)], storeType);
36 SetValue(node[GET_NAME(securityLevel)], securityLevel);
37 SetValue(node[GET_NAME(area)], area);
38 SetValue(node[GET_NAME(uid)], uid);
39 SetValue(node[GET_NAME(tokenId)], tokenId);
40 SetValue(node[GET_NAME(instanceId)], instanceId);
41 SetValue(node[GET_NAME(appId)], appId);
42 SetValue(node[GET_NAME(appType)], appType);
43 SetValue(node[GET_NAME(bundleName)], bundleName);
44 SetValue(node[GET_NAME(hapName)], hapName);
45 SetValue(node[GET_NAME(dataDir)], dataDir);
46 SetValue(node[GET_NAME(deviceId)], deviceId);
47 SetValue(node[GET_NAME(schema)], schema);
48 SetValue(node[GET_NAME(storeId)], storeId);
49 SetValue(node[GET_NAME(user)], user);
50 SetValue(node[GET_NAME(account)], account);
51
52 // compatible with the versions which lower than VERSION_TAG_0000
53 SetValue(node[GET_NAME(kvStoreType)], storeType);
54 SetValue(node[GET_NAME(deviceAccountID)], user);
55 SetValue(node[GET_NAME(userId)], account);
56 SetValue(node[GET_NAME(UID)], uid);
57
58 return true;
59 }
60
Unmarshal(const json & node)61 bool StoreMetaData::Unmarshal(const json &node)
62 {
63 GetValue(node, GET_NAME(version), version);
64 GetValue(node, GET_NAME(isAutoSync), isAutoSync);
65 GetValue(node, GET_NAME(isBackup), isBackup);
66 GetValue(node, GET_NAME(isDirty), isDirty);
67 GetValue(node, GET_NAME(isEncrypt), isEncrypt);
68 GetValue(node, GET_NAME(storeType), storeType);
69 GetValue(node, GET_NAME(securityLevel), securityLevel);
70 GetValue(node, GET_NAME(area), area);
71 GetValue(node, GET_NAME(uid), uid);
72 GetValue(node, GET_NAME(tokenId), tokenId);
73 GetValue(node, GET_NAME(instanceId), instanceId);
74 GetValue(node, GET_NAME(appId), appId);
75 GetValue(node, GET_NAME(appType), appType);
76 GetValue(node, GET_NAME(bundleName), bundleName);
77 GetValue(node, GET_NAME(hapName), hapName);
78 GetValue(node, GET_NAME(dataDir), dataDir);
79 GetValue(node, GET_NAME(deviceId), deviceId);
80 GetValue(node, GET_NAME(schema), schema);
81 GetValue(node, GET_NAME(storeId), storeId);
82 GetValue(node, GET_NAME(user), user);
83 GetValue(node, GET_NAME(account), account);
84
85 // compatible with the older versions
86 if (version < FIELD_CHANGED_TAG) {
87 GetValue(node, GET_NAME(kvStoreType), storeType);
88 GetValue(node, GET_NAME(UID), uid);
89 GetValue(node, GET_NAME(deviceAccountID), user);
90 GetValue(node, GET_NAME(userId), account);
91 }
92 return true;
93 }
94
StoreMetaData()95 StoreMetaData::StoreMetaData()
96 {
97 }
98
~StoreMetaData()99 StoreMetaData::~StoreMetaData()
100 {
101 }
102
StoreMetaData(const std::string & userId,const std::string & appId,const std::string & storeId)103 StoreMetaData::StoreMetaData(const std::string &userId, const std::string &appId, const std::string &storeId)
104 : appId(appId), storeId(storeId), user(userId)
105 {
106 }
107
operator ==(const StoreMetaData & metaData) const108 bool StoreMetaData::operator==(const StoreMetaData &metaData) const
109 {
110 if (Constant::NotEqual(isAutoSync, metaData.isAutoSync) || Constant::NotEqual(isBackup, metaData.isBackup) ||
111 Constant::NotEqual(isDirty, metaData.isDirty) || Constant::NotEqual(isEncrypt, metaData.isEncrypt)) {
112 return false;
113 }
114 return (version == metaData.version && storeType == metaData.storeType &&
115 securityLevel == metaData.securityLevel && area == metaData.area && uid == metaData.uid &&
116 tokenId == metaData.tokenId && instanceId == metaData.instanceId && appId == metaData.appId &&
117 appType == metaData.appType && bundleName == metaData.bundleName && dataDir == metaData.dataDir
118 );
119 }
120
operator !=(const StoreMetaData & metaData) const121 bool StoreMetaData::operator!=(const StoreMetaData &metaData) const
122 {
123 return !(*this == metaData);
124 }
125
GetKey() const126 std::string StoreMetaData::GetKey() const
127 {
128 if (instanceId == 0) {
129 return GetKey({ deviceId, user, "default", bundleName, storeId });
130 }
131 return GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
132 }
133
GetKeyLocal() const134 std::string StoreMetaData::GetKeyLocal() const
135 {
136 if (instanceId == 0) {
137 return StoreMetaDataLocal::GetKey({ deviceId, user, "default", bundleName, storeId });
138 }
139 return StoreMetaDataLocal::GetKey({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
140 }
141
GetSecretKey() const142 std::string StoreMetaData::GetSecretKey() const
143 {
144 if (version < CURRENT_VERSION) {
145 return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId });
146 }
147 return SecretKeyMetaData::GetKey({ user, "default", bundleName, storeId, std::to_string(instanceId) });
148 }
149
GetBackupSecretKey() const150 std::string StoreMetaData::GetBackupSecretKey() const
151 {
152 if (version < CURRENT_VERSION) {
153 return SecretKeyMetaData::GetBackupKey({ user, "default", bundleName, storeId });
154 }
155 return SecretKeyMetaData::GetBackupKey({ user, "default", bundleName, storeId, std::to_string(instanceId) });
156 }
157
GetStoreAlias() const158 std::string StoreMetaData::GetStoreAlias() const
159 {
160 return Anonymous::Change(storeId);
161 }
162
GetStrategyKey() const163 std::string StoreMetaData::GetStrategyKey() const
164 {
165 if (instanceId == 0) {
166 return StrategyMeta::GetPrefix({ deviceId, user, "default", bundleName, storeId });
167 }
168 return StrategyMeta::GetPrefix({ deviceId, user, "default", bundleName, storeId, std::to_string(instanceId) });
169 }
170
GetKey(const std::initializer_list<std::string> & fields)171 std::string StoreMetaData::GetKey(const std::initializer_list<std::string> &fields)
172 {
173 std::string prefix = KEY_PREFIX;
174 for (const auto &field : fields) {
175 prefix.append(Constant::KEY_SEPARATOR).append(field);
176 }
177 return prefix;
178 }
179
GetPrefix(const std::initializer_list<std::string> & fields)180 std::string StoreMetaData::GetPrefix(const std::initializer_list<std::string> &fields)
181 {
182 return GetKey(fields).append(Constant::KEY_SEPARATOR);
183 }
184 } // namespace DistributedData
185 } // namespace OHOS
186