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