1 /*
2 * Copyright (c) 2022 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 "single_ver_data_sync_utils.h"
16
17 #include <mutex>
18 #include "db_common.h"
19 #include "version.h"
20 #include "log_print.h"
21 #include "message.h"
22 namespace DistributedDB {
23 namespace {
FillPermissionCheckParam(const SyncGenericInterface * storage,int mode,PermissionCheckParam & param,uint8_t & flag)24 void FillPermissionCheckParam(const SyncGenericInterface* storage, int mode, PermissionCheckParam ¶m, uint8_t &flag)
25 {
26 param.appId = storage->GetDbProperties().GetStringProp(DBProperties::APP_ID, "");
27 param.userId = storage->GetDbProperties().GetStringProp(DBProperties::USER_ID, "");
28 param.storeId = storage->GetDbProperties().GetStringProp(DBProperties::STORE_ID, "");
29 param.instanceId = storage->GetDbProperties().GetIntProp(DBProperties::INSTANCE_ID, 0);
30 switch (mode) {
31 case SyncModeType::PUSH:
32 flag = CHECK_FLAG_RECEIVE;
33 break;
34 case SyncModeType::PULL:
35 flag = CHECK_FLAG_SEND;
36 break;
37 case SyncModeType::PUSH_AND_PULL:
38 flag = CHECK_FLAG_SEND | CHECK_FLAG_RECEIVE;
39 break;
40 default:
41 flag = CHECK_FLAG_RECEIVE;
42 break;
43 }
44 }
45 }
46
QuerySyncCheck(const SingleVerSyncTaskContext * context,bool & isCheckStatus)47 int SingleVerDataSyncUtils::QuerySyncCheck(const SingleVerSyncTaskContext *context, bool &isCheckStatus)
48 {
49 if (context == nullptr) {
50 isCheckStatus = false;
51 return -E_INVALID_ARGS;
52 }
53 if (!context->IsQuerySync()) {
54 isCheckStatus = true;
55 return E_OK;
56 }
57 uint32_t version = std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT);
58 // for 101 version, no need to do abilitySync, just send request to remote
59 if (version <= SOFTWARE_VERSION_RELEASE_1_0) {
60 isCheckStatus = true;
61 return E_OK;
62 }
63 if (version < SOFTWARE_VERSION_RELEASE_4_0) {
64 LOGE("[SingleVerDataSync] not support query sync when remote ver lower than 104");
65 isCheckStatus = false;
66 return E_OK;
67 }
68 if (version < SOFTWARE_VERSION_RELEASE_5_0 && !(context->GetQuery().IsQueryOnlyByKey())) {
69 LOGE("[SingleVerDataSync] remote version only support prefix key");
70 isCheckStatus = false;
71 return E_OK;
72 }
73 if (context->GetQuery().HasInKeys() && context->IsNotSupportAbility(SyncConfig::INKEYS_QUERY)) {
74 isCheckStatus = false;
75 return E_OK;
76 }
77 isCheckStatus = true;
78 return E_OK;
79 }
80
AckMsgErrnoCheck(const SingleVerSyncTaskContext * context,const Message * message)81 int SingleVerDataSyncUtils::AckMsgErrnoCheck(const SingleVerSyncTaskContext *context, const Message *message)
82 {
83 if (context == nullptr || message == nullptr) {
84 return -E_INVALID_ARGS;
85 }
86 if (message->IsFeedbackError()) {
87 LOGE("[DataSync][AckMsgErrnoCheck] message errNo=%d", message->GetErrorNo());
88 return -static_cast<int>(message->GetErrorNo());
89 }
90 return E_OK;
91 }
92
RequestQueryCheck(const DataRequestPacket * packet,SyncGenericInterface * storage)93 int SingleVerDataSyncUtils::RequestQueryCheck(const DataRequestPacket *packet, SyncGenericInterface *storage)
94 {
95 if (storage == nullptr || packet == nullptr) {
96 return -E_INVALID_ARGS;
97 }
98 if (SyncOperation::GetSyncType(packet->GetMode()) != SyncType::QUERY_SYNC_TYPE) {
99 return E_OK;
100 }
101 QuerySyncObject syncQuery = packet->GetQuery();
102 int errCode = storage->CheckAndInitQueryCondition(syncQuery);
103 if (errCode != E_OK) {
104 LOGE("[SingleVerDataSync] check sync query failed,errCode=%d", errCode);
105 return errCode;
106 }
107 return E_OK;
108 }
109
IsPermitLocalDeviceRecvData(const std::string & deviceId,const SecurityOption & remoteSecOption)110 bool SingleVerDataSyncUtils::IsPermitLocalDeviceRecvData(const std::string &deviceId,
111 const SecurityOption &remoteSecOption)
112 {
113 return RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(deviceId, remoteSecOption);
114 }
115
IsPermitRemoteDeviceRecvData(const std::string & deviceId,const SecurityOption & remoteSecOption,SyncGenericInterface * storage)116 bool SingleVerDataSyncUtils::IsPermitRemoteDeviceRecvData(const std::string &deviceId,
117 const SecurityOption &remoteSecOption, SyncGenericInterface *storage)
118 {
119 if (storage == nullptr) {
120 return false;
121 }
122 SecurityOption localSecOption;
123 if (remoteSecOption.securityLabel == NOT_SUPPORT_SEC_CLASSIFICATION) {
124 return true;
125 }
126 int errCode = storage->GetSecurityOption(localSecOption);
127 if (errCode == -E_NOT_SUPPORT) {
128 return true;
129 }
130 if (errCode != E_OK) {
131 LOGE("[SingleVerDataSyncUtils] get security option error %d", errCode);
132 return false;
133 }
134 if (localSecOption.securityLabel == NOT_SET) {
135 LOGE("[SingleVerDataSyncUtils] local label is not set!");
136 return false;
137 }
138 return RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(deviceId, localSecOption);
139 }
140
TransDbDataItemToSendDataItem(const std::string & localHashName,std::vector<SendDataItem> & outData)141 void SingleVerDataSyncUtils::TransDbDataItemToSendDataItem(const std::string &localHashName,
142 std::vector<SendDataItem> &outData)
143 {
144 for (size_t i = 0; i < outData.size(); i++) {
145 if (outData[i] == nullptr) {
146 continue;
147 }
148 outData[i]->SetOrigDevice(outData[i]->GetOrigDevice().empty() ? localHashName : outData[i]->GetOrigDevice());
149 if (i == 0 || i == (outData.size() - 1)) {
150 LOGD("[DataSync][TransToSendItem] printData packet=%zu,timestamp=%" PRIu64 ",flag=%" PRIu64, i,
151 outData[i]->GetTimestamp(), outData[i]->GetFlag());
152 }
153 }
154 }
155
TransferForeignOrigDevName(const std::string & deviceName,const std::string & localHashName)156 std::string SingleVerDataSyncUtils::TransferForeignOrigDevName(const std::string &deviceName,
157 const std::string &localHashName)
158 {
159 if (localHashName == deviceName) {
160 return "";
161 }
162 return deviceName;
163 }
164
TransSendDataItemToLocal(const SingleVerSyncTaskContext * context,const std::string & localHashName,const std::vector<SendDataItem> & data)165 void SingleVerDataSyncUtils::TransSendDataItemToLocal(const SingleVerSyncTaskContext *context,
166 const std::string &localHashName, const std::vector<SendDataItem> &data)
167 {
168 TimeOffset offset = context->GetTimeOffset();
169 for (auto &item : data) {
170 if (item == nullptr) {
171 continue;
172 }
173 item->SetOrigDevice(TransferForeignOrigDevName(item->GetOrigDevice(), localHashName));
174 Timestamp tempTimestamp = item->GetTimestamp();
175 Timestamp tempWriteTimestamp = item->GetWriteTimestamp();
176 item->SetTimestamp(tempTimestamp - static_cast<Timestamp>(offset));
177 if (tempWriteTimestamp != 0) {
178 item->SetWriteTimestamp(tempWriteTimestamp - static_cast<Timestamp>(offset));
179 }
180
181 Timestamp currentLocalTime = context->GetCurrentLocalTime();
182 if (item->GetTimestamp() > currentLocalTime) {
183 item->SetTimestamp(currentLocalTime);
184 }
185 if (item->GetWriteTimestamp() > currentLocalTime) {
186 item->SetWriteTimestamp(currentLocalTime);
187 }
188 }
189 }
190
TranslateErrCodeIfNeed(int mode,uint32_t version,int & errCode)191 void SingleVerDataSyncUtils::TranslateErrCodeIfNeed(int mode, uint32_t version, int &errCode)
192 {
193 // once get data occur E_EKEYREVOKED error, should also send request to remote dev to pull data.
194 if (SyncOperation::TransferSyncMode(mode) == SyncModeType::PUSH_AND_PULL &&
195 version > SOFTWARE_VERSION_RELEASE_2_0 && errCode == -E_EKEYREVOKED) {
196 errCode = E_OK;
197 }
198 }
199
RunPermissionCheck(SingleVerSyncTaskContext * context,const SyncGenericInterface * storage,const std::string & label,const DataRequestPacket * packet)200 int SingleVerDataSyncUtils::RunPermissionCheck(SingleVerSyncTaskContext *context, const SyncGenericInterface* storage,
201 const std::string &label, const DataRequestPacket *packet)
202 {
203 int mode = SyncOperation::TransferSyncMode(packet->GetMode());
204 return RunPermissionCheckInner(context, storage, label, packet, mode);
205 }
206
RunPermissionCheck(SingleVerSyncTaskContext * context,const SyncGenericInterface * storage,const std::string & label,int mode)207 int SingleVerDataSyncUtils::RunPermissionCheck(SingleVerSyncTaskContext *context, const SyncGenericInterface* storage,
208 const std::string &label, int mode)
209 {
210 return RunPermissionCheckInner(context, storage, label, nullptr, mode);
211 }
212
CheckPermitReceiveData(const SingleVerSyncTaskContext * context,const ICommunicator * communicator,const SyncGenericInterface * storage)213 bool SingleVerDataSyncUtils::CheckPermitReceiveData(const SingleVerSyncTaskContext *context,
214 const ICommunicator *communicator, const SyncGenericInterface *storage)
215 {
216 if (storage == nullptr) {
217 LOGE("[SingleVerDataSyncUtils] storage is nullptr when check receive data");
218 return false;
219 }
220 // check memory db here because remote maybe low version
221 // it will send option with not set rather than not support when remote is memory db
222 bool memory = storage->GetDbProperties().GetBoolProp(KvDBProperties::MEMORY_MODE, false);
223 if (memory) {
224 LOGI("[SingleVerDataSyncUtils] skip check receive data because local is memory db");
225 return true;
226 }
227 SecurityOption remoteSecOption = context->GetRemoteSeccurityOption();
228 std::string localDeviceId;
229 if (communicator == nullptr || remoteSecOption.securityLabel == NOT_SUPPORT_SEC_CLASSIFICATION) {
230 return true;
231 }
232 communicator->GetLocalIdentity(localDeviceId);
233 SecurityOption localOption;
234 int errCode = storage->GetSecurityOption(localOption);
235 if (errCode == -E_NOT_SUPPORT) {
236 LOGD("[SingleVerDataSyncUtils] local not support get security label");
237 return true;
238 }
239 if (errCode == E_OK && localOption.securityLabel == SecurityLabel::NOT_SET) {
240 LOGE("[SingleVerDataSyncUtils] local label is not set!");
241 return false;
242 }
243 if (remoteSecOption.securityLabel == SecurityLabel::S0 && localOption.securityLabel == SecurityLabel::S1) {
244 remoteSecOption.securityLabel = SecurityLabel::S1;
245 LOGI("[SingleVerDataSyncUtils] Transform Remote SecLabel From S0 To S1 When Receive Data");
246 }
247 if (remoteSecOption.securityLabel == FAILED_GET_SEC_CLASSIFICATION) {
248 LOGE("[SingleVerDataSyncUtils] remote label get failed!");
249 return false;
250 }
251 if (remoteSecOption.securityLabel != SecurityLabel::NOT_SET &&
252 remoteSecOption.securityLabel != localOption.securityLabel) {
253 LOGE("[SingleVerDataSyncUtils] label is not equal remote %d local %d", remoteSecOption.securityLabel,
254 localOption.securityLabel);
255 return false;
256 }
257 bool isPermitSync = SingleVerDataSyncUtils::IsPermitLocalDeviceRecvData(localDeviceId, remoteSecOption);
258 if (isPermitSync) {
259 return isPermitSync;
260 }
261 LOGE("[SingleVerDataSyncUtils][PermitReceiveData] check failed: permitReceive=%d, localDev=%s, seclabel=%d,"
262 " secflag=%d", isPermitSync, STR_MASK(localDeviceId), remoteSecOption.securityLabel,
263 remoteSecOption.securityFlag);
264 return isPermitSync;
265 }
266
SetPacketId(DataRequestPacket * packet,SingleVerSyncTaskContext * context,uint32_t version)267 void SingleVerDataSyncUtils::SetPacketId(DataRequestPacket *packet, SingleVerSyncTaskContext *context, uint32_t version)
268 {
269 if (version > SOFTWARE_VERSION_RELEASE_2_0) {
270 context->IncPacketId(); // begin from 1
271 std::vector<uint64_t> reserved {context->GetPacketId()};
272 packet->SetReserved(reserved);
273 }
274 }
275
GetMessageId(SyncType syncType)276 int SingleVerDataSyncUtils::GetMessageId(SyncType syncType)
277 {
278 if (syncType == SyncType::QUERY_SYNC_TYPE) {
279 return QUERY_SYNC_MESSAGE;
280 }
281 return DATA_SYNC_MESSAGE;
282 }
283
PushAndPullKeyRevokHandle(SingleVerSyncTaskContext * context)284 void SingleVerDataSyncUtils::PushAndPullKeyRevokHandle(SingleVerSyncTaskContext *context)
285 {
286 // for push_and_pull mode it may be EKEYREVOKED error before receive watermarkexception
287 // should clear errCode and restart pushpull request.
288 int mode = SyncOperation::TransferSyncMode(context->GetMode());
289 if (context->GetRemoteSoftwareVersion() > SOFTWARE_VERSION_RELEASE_2_0 && mode == SyncModeType::PUSH_AND_PULL &&
290 context->GetTaskErrCode() == -E_EKEYREVOKED) {
291 context->SetTaskErrCode(E_OK);
292 }
293 }
294
GetReSendMode(int mode,uint32_t sequenceId,SyncType syncType)295 int SingleVerDataSyncUtils::GetReSendMode(int mode, uint32_t sequenceId, SyncType syncType)
296 {
297 int curMode = SyncOperation::TransferSyncMode(mode);
298 if (curMode == SyncModeType::PUSH || curMode == SyncModeType::PULL) {
299 return mode;
300 }
301 if (curMode == SyncModeType::RESPONSE_PULL) {
302 return (syncType == SyncType::QUERY_SYNC_TYPE) ? SyncModeType::QUERY_PUSH : SyncModeType::PUSH;
303 }
304 // set push_and_pull mode when resend first sequenceId to inform remote to run RESPONSE_PULL task
305 // for sequenceId which is larger than first, only need to send data, means to set push or query_push mode
306 if (sequenceId == 1) {
307 return (syncType == SyncType::QUERY_SYNC_TYPE) ? SyncModeType::QUERY_PUSH_PULL : SyncModeType::PUSH_AND_PULL;
308 }
309 return (syncType == SyncType::QUERY_SYNC_TYPE) ? SyncModeType::QUERY_PUSH : SyncModeType::PUSH;
310 }
311
FillControlRequestPacket(ControlRequestPacket * packet,SingleVerSyncTaskContext * context)312 void SingleVerDataSyncUtils::FillControlRequestPacket(ControlRequestPacket *packet, SingleVerSyncTaskContext *context)
313 {
314 uint32_t version = std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT);
315 uint32_t flag = 0;
316 if (context->GetMode() == SyncModeType::SUBSCRIBE_QUERY && context->IsAutoSubscribe()) {
317 flag = SubscribeRequest::IS_AUTO_SUBSCRIBE;
318 }
319 packet->SetPacketHead(E_OK, version, GetControlCmdType(context->GetMode()), flag);
320 packet->SetQuery(context->GetQuery());
321 }
322
GetControlCmdType(int mode)323 ControlCmdType SingleVerDataSyncUtils::GetControlCmdType(int mode)
324 {
325 if (mode == SyncModeType::SUBSCRIBE_QUERY) {
326 return ControlCmdType::SUBSCRIBE_QUERY_CMD;
327 } else if (mode == SyncModeType::UNSUBSCRIBE_QUERY) {
328 return ControlCmdType::UNSUBSCRIBE_QUERY_CMD;
329 }
330 return ControlCmdType::INVALID_CONTROL_CMD;
331 }
332
GetModeByControlCmdType(ControlCmdType controlCmd)333 int SingleVerDataSyncUtils::GetModeByControlCmdType(ControlCmdType controlCmd)
334 {
335 if (controlCmd == ControlCmdType::SUBSCRIBE_QUERY_CMD) {
336 return SyncModeType::SUBSCRIBE_QUERY;
337 } else if (controlCmd == ControlCmdType::UNSUBSCRIBE_QUERY_CMD) {
338 return SyncModeType::UNSUBSCRIBE_QUERY;
339 }
340 return SyncModeType::INVALID_MODE;
341 }
342
IsNeedTriggerQueryAutoSync(Message * inMsg,QuerySyncObject & query)343 bool SingleVerDataSyncUtils::IsNeedTriggerQueryAutoSync(Message *inMsg, QuerySyncObject &query)
344 {
345 if (inMsg == nullptr) {
346 return false;
347 }
348 if (inMsg->GetMessageId() != CONTROL_SYNC_MESSAGE) {
349 return false;
350 }
351 const ControlRequestPacket *packet = inMsg->GetObject<ControlRequestPacket>();
352 if (packet == nullptr) {
353 return false;
354 }
355 uint32_t controlCmdType = packet->GetcontrolCmdType();
356 if (controlCmdType == ControlCmdType::SUBSCRIBE_QUERY_CMD && inMsg->GetMessageType() == TYPE_REQUEST) {
357 const SubscribeRequest *subPacket = inMsg->GetObject<SubscribeRequest>();
358 if (subPacket == nullptr) {
359 return false;
360 }
361 query = subPacket->GetQuery();
362 LOGI("[SingleVerDataSync] receive sub scribe query cmd,begin to trigger query auto sync");
363 return true;
364 }
365 return false;
366 }
367
ControlAckErrorHandle(const SingleVerSyncTaskContext * context,const std::shared_ptr<SubscribeManager> & subManager)368 void SingleVerDataSyncUtils::ControlAckErrorHandle(const SingleVerSyncTaskContext *context,
369 const std::shared_ptr<SubscribeManager> &subManager)
370 {
371 if (context->GetMode() == SyncModeType::SUBSCRIBE_QUERY) {
372 // reserve before need clear
373 subManager->DeleteLocalSubscribeQuery(context->GetDeviceId(), context->GetQuery());
374 }
375 }
376
SetMessageHeadInfo(Message & message,uint16_t inMsgType,const std::string & inTarget,uint32_t inSequenceId,uint32_t inSessionId)377 void SingleVerDataSyncUtils::SetMessageHeadInfo(Message &message, uint16_t inMsgType, const std::string &inTarget,
378 uint32_t inSequenceId, uint32_t inSessionId)
379 {
380 message.SetMessageType(inMsgType);
381 message.SetTarget(inTarget);
382 message.SetSequenceId(inSequenceId);
383 message.SetSessionId(inSessionId);
384 }
385
IsGetDataSuccessfully(int errCode)386 bool SingleVerDataSyncUtils::IsGetDataSuccessfully(int errCode)
387 {
388 return (errCode == E_OK || errCode == -E_UNFINISHED);
389 }
390
GetMaxSendDataTime(const std::vector<SendDataItem> & inData)391 Timestamp SingleVerDataSyncUtils::GetMaxSendDataTime(const std::vector<SendDataItem> &inData)
392 {
393 Timestamp stamp = 0;
394 for (size_t i = 0; i < inData.size(); i++) {
395 if (inData[i] == nullptr) {
396 continue;
397 }
398 Timestamp tempStamp = inData[i]->GetTimestamp();
399 if (stamp < tempStamp) {
400 stamp = tempStamp;
401 }
402 }
403 return stamp;
404 }
405
GetFullSyncDataTimeRange(const std::vector<SendDataItem> & inData,WaterMark localMark,UpdateWaterMark & isUpdate)406 SyncTimeRange SingleVerDataSyncUtils::GetFullSyncDataTimeRange(const std::vector<SendDataItem> &inData,
407 WaterMark localMark, UpdateWaterMark &isUpdate)
408 {
409 Timestamp maxTimestamp = localMark;
410 Timestamp minTimestamp = localMark;
411 for (size_t i = 0; i < inData.size(); i++) {
412 if (inData[i] == nullptr) {
413 continue;
414 }
415 Timestamp tempStamp = inData[i]->GetTimestamp();
416 if (maxTimestamp < tempStamp) {
417 maxTimestamp = tempStamp;
418 }
419 if (minTimestamp > tempStamp) {
420 minTimestamp = tempStamp;
421 }
422 isUpdate.normalUpdateMark = true;
423 }
424 return {minTimestamp, 0, maxTimestamp, 0};
425 }
426
GetQuerySyncDataTimeRange(const std::vector<SendDataItem> & inData,WaterMark localMark,WaterMark deleteLocalMark,UpdateWaterMark & isUpdate)427 SyncTimeRange SingleVerDataSyncUtils::GetQuerySyncDataTimeRange(const std::vector<SendDataItem> &inData,
428 WaterMark localMark, WaterMark deleteLocalMark, UpdateWaterMark &isUpdate)
429 {
430 SyncTimeRange dataTimeRange = {localMark, deleteLocalMark, localMark, deleteLocalMark};
431 for (size_t i = 0; i < inData.size(); i++) {
432 if (inData[i] == nullptr) {
433 continue;
434 }
435 Timestamp tempStamp = inData[i]->GetTimestamp();
436 if ((inData[i]->GetFlag() & DataItem::DELETE_FLAG) == 0) { // query data
437 if (dataTimeRange.endTime < tempStamp) {
438 dataTimeRange.endTime = tempStamp;
439 }
440 if (dataTimeRange.beginTime > tempStamp) {
441 dataTimeRange.beginTime = tempStamp;
442 }
443 isUpdate.normalUpdateMark = true;
444 }
445 if ((inData[i]->GetFlag() & DataItem::DELETE_FLAG) != 0) { // delete data
446 if (dataTimeRange.deleteEndTime < tempStamp) {
447 dataTimeRange.deleteEndTime = tempStamp;
448 }
449 if (dataTimeRange.deleteBeginTime > tempStamp) {
450 dataTimeRange.deleteBeginTime = tempStamp;
451 }
452 isUpdate.deleteUpdateMark = true;
453 }
454 }
455 return dataTimeRange;
456 }
457
ReviseLocalMark(SyncType syncType,const SyncTimeRange & dataTimeRange,UpdateWaterMark updateMark)458 SyncTimeRange SingleVerDataSyncUtils::ReviseLocalMark(SyncType syncType, const SyncTimeRange &dataTimeRange,
459 UpdateWaterMark updateMark)
460 {
461 SyncTimeRange tmpDataTime = dataTimeRange;
462 if (updateMark.deleteUpdateMark && syncType == SyncType::QUERY_SYNC_TYPE) {
463 tmpDataTime.deleteEndTime += 1;
464 }
465 if (updateMark.normalUpdateMark) {
466 tmpDataTime.endTime += 1;
467 }
468 return tmpDataTime;
469 }
470
GetRecvDataTimeRange(SyncType syncType,const std::vector<SendDataItem> & data,UpdateWaterMark & isUpdate)471 SyncTimeRange SingleVerDataSyncUtils::GetRecvDataTimeRange(SyncType syncType,
472 const std::vector<SendDataItem> &data, UpdateWaterMark &isUpdate)
473 {
474 if (syncType != SyncType::QUERY_SYNC_TYPE) {
475 return SingleVerDataSyncUtils::GetFullSyncDataTimeRange(data, 0, isUpdate);
476 }
477 return SingleVerDataSyncUtils::GetQuerySyncDataTimeRange(data, 0, 0, isUpdate);
478 }
479
GetSyncDataTimeRange(SyncType syncType,WaterMark localMark,WaterMark deleteMark,const std::vector<SendDataItem> & inData,UpdateWaterMark & isUpdate)480 SyncTimeRange SingleVerDataSyncUtils::GetSyncDataTimeRange(SyncType syncType, WaterMark localMark, WaterMark deleteMark,
481 const std::vector<SendDataItem> &inData, UpdateWaterMark &isUpdate)
482 {
483 if (syncType != SyncType::QUERY_SYNC_TYPE) {
484 return SingleVerDataSyncUtils::GetFullSyncDataTimeRange(inData, localMark, isUpdate);
485 }
486 return SingleVerDataSyncUtils::GetQuerySyncDataTimeRange(inData, localMark, deleteMark, isUpdate);
487 }
488
RunPermissionCheckInner(const SingleVerSyncTaskContext * context,const SyncGenericInterface * storage,const std::string & label,const DataRequestPacket * packet,int mode)489 int SingleVerDataSyncUtils::RunPermissionCheckInner(const SingleVerSyncTaskContext *context,
490 const SyncGenericInterface* storage, const std::string &label, const DataRequestPacket *packet, int mode)
491 {
492 PermissionCheckParam param;
493 uint8_t flag = 0u;
494 FillPermissionCheckParam(storage, mode, param, flag);
495 param.deviceId = context->GetDeviceId();
496 if (packet != nullptr) {
497 param.extraConditions = packet->GetExtraConditions();
498 }
499 int errCode = RuntimeContext::GetInstance()->RunPermissionCheck(param, flag);
500 if (errCode != E_OK) {
501 LOGE("[DataSync][RunPermissionCheck] check failed flag=%" PRIu8 ",Label=%s,dev=%s", flag, label.c_str(),
502 STR_MASK(context->GetDeviceId()));
503 }
504 return errCode;
505 }
506
GetTimeOffsetFromRequestMsg(const Message * message)507 std::pair<TimeOffset, TimeOffset> SingleVerDataSyncUtils::GetTimeOffsetFromRequestMsg(const Message *message)
508 {
509 std::pair<TimeOffset, TimeOffset> res;
510 auto &[systemOffset, senderLocalOffset] = res;
511 const DataRequestPacket *packet = message->GetObject<DataRequestPacket>();
512 if (packet == nullptr) {
513 systemOffset = 0;
514 senderLocalOffset = 0;
515 return res;
516 }
517 systemOffset = packet->GetSystemTimeOffset();
518 senderLocalOffset = packet->GetSenderTimeOffset();
519 return res;
520 }
521
RecordClientId(const SingleVerSyncTaskContext & context,const SyncGenericInterface & storage,std::shared_ptr<Metadata> & metadata)522 void SingleVerDataSyncUtils::RecordClientId(const SingleVerSyncTaskContext &context,
523 const SyncGenericInterface &storage, std::shared_ptr<Metadata> &metadata)
524 {
525 StoreInfo info = {
526 storage.GetDbProperties().GetStringProp(DBProperties::USER_ID, ""),
527 storage.GetDbProperties().GetStringProp(DBProperties::APP_ID, ""),
528 storage.GetDbProperties().GetStringProp(DBProperties::STORE_ID, "")
529 };
530 std::string clientId;
531 if (RuntimeContext::GetInstance()->TranslateDeviceId(context.GetDeviceId(), info, clientId) == E_OK) {
532 int errCode = metadata->SaveClientId(context.GetDeviceId(), clientId);
533 if (errCode != E_OK) {
534 LOGW("[DataSync] record clientId failed %d", errCode);
535 }
536 }
537 }
538
SetDataRequestCommonInfo(const SingleVerSyncTaskContext & context,const SyncGenericInterface & storage,DataRequestPacket & packet,std::shared_ptr<Metadata> & metadata)539 void SingleVerDataSyncUtils::SetDataRequestCommonInfo(const SingleVerSyncTaskContext &context,
540 const SyncGenericInterface &storage, DataRequestPacket &packet, std::shared_ptr<Metadata> &metadata)
541 {
542 packet.SetSenderTimeOffset(metadata->GetLocalTimeOffset());
543 packet.SetSystemTimeOffset(metadata->GetSystemTimeOffset(context.GetDeviceId()));
544 if (context.GetRemoteSoftwareVersion() < SOFTWARE_VERSION_RELEASE_9_0) {
545 return;
546 }
547 auto [err, localSchemaVer] = metadata->GetLocalSchemaVersion();
548 if (err != E_OK) {
549 LOGW("[DataSync] get local schema version failed:%d", err);
550 return;
551 }
552 packet.SetSchemaVersion(localSchemaVer);
553 SecurityOption localOption;
554 err = storage.GetSecurityOption(localOption);
555 if (err == -E_NOT_SUPPORT) {
556 LOGW("[DataSync] local not support sec classification");
557 localOption.securityLabel = NOT_SUPPORT_SEC_CLASSIFICATION;
558 } else if (err != E_OK) {
559 LOGE("[DataSync] get local security option errCode:%d", err);
560 localOption.securityLabel = FAILED_GET_SEC_CLASSIFICATION;
561 }
562 packet.SetSecurityOption(localOption);
563 }
564
SchemaVersionMatchCheck(const SingleVerSyncTaskContext & context,const DataRequestPacket & packet,std::shared_ptr<Metadata> & metadata)565 int SingleVerDataSyncUtils::SchemaVersionMatchCheck(const SingleVerSyncTaskContext &context,
566 const DataRequestPacket &packet, std::shared_ptr<Metadata> &metadata)
567 {
568 if (context.GetRemoteSoftwareVersion() < SOFTWARE_VERSION_RELEASE_9_0) {
569 return E_OK;
570 }
571 auto remoteSchemaVersion = metadata->GetRemoteSchemaVersion(context.GetDeviceId());
572 if (remoteSchemaVersion != packet.GetSchemaVersion()) {
573 LOGE("[DataSync] remote schema version misMatch, need ability sync again, packet %" PRIu64 " cache %" PRIu64,
574 packet.GetSchemaVersion(), remoteSchemaVersion);
575 return -E_NEED_ABILITY_SYNC;
576 }
577 return E_OK;
578 }
579 }