1 /*
2 * Copyright (c) 2023 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 "data_handler.h"
17
18 #include "dfs_error.h"
19 #include "utils_log.h"
20 namespace OHOS {
21 namespace FileManagement {
22 namespace CloudSync {
23 using namespace std;
24 using namespace DriveKit;
DataHandler(int32_t userId,const string & bundleName,const std::string & table)25 DataHandler::DataHandler(int32_t userId, const string &bundleName, const std::string &table)
26 : cloudPrefImpl_(userId, bundleName, table)
27 {
28 cloudPrefImpl_.GetString(START_CURSOR, startCursor_);
29 cloudPrefImpl_.GetString(NEXT_CURSOR, nextCursor_);
30 cloudPrefImpl_.GetString(NEXT_CURSOR, tempNextCursor_);
31 cloudPrefImpl_.GetInt(BATCH_NO, batchNo_);
32 cloudPrefImpl_.GetInt(RECORD_SIZE, recordSize_);
33 cloudPrefImpl_.GetBool(CHECKING_FLAG, isChecking_);
34 }
35
GetStartCursor(DriveKit::DKQueryCursor & cursor)36 void DataHandler::GetStartCursor(DriveKit::DKQueryCursor &cursor)
37 {
38 cursor = startCursor_;
39 }
40
GetNextCursor(DriveKit::DKQueryCursor & cursor)41 void DataHandler::GetNextCursor(DriveKit::DKQueryCursor &cursor)
42 {
43 if (tempNextCursor_.empty()) {
44 cursor = startCursor_;
45 return;
46 }
47 cursor = tempNextCursor_;
48 }
49
SetTempStartCursor(const DriveKit::DKQueryCursor & cursor)50 void DataHandler::SetTempStartCursor(const DriveKit::DKQueryCursor &cursor)
51 {
52 tempStartCursor_ = cursor;
53 cloudPrefImpl_.SetString(TEMP_START_CURSOR, tempStartCursor_);
54 }
55
GetTempStartCursor(DriveKit::DKQueryCursor & cursor)56 void DataHandler::GetTempStartCursor(DriveKit::DKQueryCursor &cursor)
57 {
58 cursor = tempStartCursor_;
59 }
60
SetTempNextCursor(const DriveKit::DKQueryCursor & cursor,bool isFinish)61 void DataHandler::SetTempNextCursor(const DriveKit::DKQueryCursor &cursor, bool isFinish)
62 {
63 LOGI("batchNo_ %{public}d set temp next cursor %{public}s, isFinish %{public}d",
64 batchNo_, cursor.c_str(), isFinish);
65 tempNextCursor_ = cursor;
66 cursorMap_.insert(std::pair<int32_t, DriveKit::DKQueryCursor>(batchNo_, cursor));
67 cursorFinishMap_.insert(std::pair<int32_t, bool>(batchNo_, false));
68 if (!isFinish) {
69 batchNo_ ++;
70 } else {
71 isFinish_ = true;
72 }
73 }
74
GetBatchNo()75 int32_t DataHandler::GetBatchNo()
76 {
77 return batchNo_;
78 }
79
IsPullRecords()80 bool DataHandler::IsPullRecords()
81 {
82 return startCursor_.empty();
83 }
84
ClearCursor()85 void DataHandler::ClearCursor()
86 {
87 startCursor_.clear();
88 nextCursor_.clear();
89 tempStartCursor_.clear();
90 tempNextCursor_.clear();
91 batchNo_ = 0;
92 recordSize_ = 0;
93 isFinish_ = false;
94 isChecking_ = false;
95 cursorMap_.clear();
96 cursorFinishMap_.clear();
97 cloudPrefImpl_.SetString(START_CURSOR, startCursor_);
98 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
99 cloudPrefImpl_.SetInt(BATCH_NO, batchNo_);
100 cloudPrefImpl_.Delete(TEMP_START_CURSOR);
101 cloudPrefImpl_.Delete(RECORD_SIZE);
102 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
103 }
104
FinishPull(const int32_t batchNo)105 void DataHandler::FinishPull(const int32_t batchNo)
106 {
107 std::lock_guard<std::mutex> lock(mutex_);
108 LOGI("batchNo %{public}d finish pull", batchNo);
109 cursorFinishMap_[batchNo] = true;
110 if (cursorFinishMap_.begin()->first == batchNo) {
111 while (!cursorFinishMap_.empty() && cursorFinishMap_.begin()->second) {
112 nextCursor_ = cursorMap_.begin()->second;
113 cloudPrefImpl_.SetInt(BATCH_NO, cursorFinishMap_.begin()->first);
114 cursorMap_.erase(cursorMap_.begin()->first);
115 cursorFinishMap_.erase(cursorFinishMap_.begin()->first);
116 }
117 }
118 LOGD("The next cursor is %{public}s", nextCursor_.c_str());
119 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
120
121 if (cursorMap_.empty() && isFinish_) {
122 if (IsPullRecords()) {
123 LOGD("PullRecords finish all");
124 startCursor_ = tempStartCursor_;
125 tempStartCursor_.clear();
126 recordSize_ = 0;
127 isChecking_ = false;
128 cloudPrefImpl_.Delete(TEMP_START_CURSOR);
129 cloudPrefImpl_.Delete(RECORD_SIZE);
130 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
131 } else {
132 LOGD("PullDatabaseChanged finish all");
133 startCursor_ = nextCursor_;
134 }
135 nextCursor_.clear();
136 tempNextCursor_.clear();
137 cloudPrefImpl_.SetString(START_CURSOR, startCursor_);
138 cloudPrefImpl_.SetString(NEXT_CURSOR, nextCursor_);
139 cloudPrefImpl_.Delete(BATCH_NO);
140 batchNo_ = 0;
141 isFinish_ = false;
142 }
143 LOGD("When batchNo finish pull startCursor %{public}s, nextCursor %{public}s, tempStartCursor %{public}s,",
144 startCursor_.c_str(), nextCursor_.c_str(), tempStartCursor_.c_str());
145 LOGD("tempNextCursor %{public}s, batchNo_ %{public}d", tempNextCursor_.c_str(), batchNo_);
146 }
147
SetRecordSize(const int32_t recordSize)148 void DataHandler::SetRecordSize(const int32_t recordSize)
149 {
150 recordSize_ = recordSize;
151 cloudPrefImpl_.SetInt(RECORD_SIZE, recordSize_);
152 }
153
GetRecordSize()154 int32_t DataHandler::GetRecordSize()
155 {
156 return recordSize_;
157 }
158
GetCheckFlag()159 bool DataHandler::GetCheckFlag()
160 {
161 return isChecking_;
162 }
163
SetDownloadType(const int32_t type)164 void DataHandler::SetDownloadType(const int32_t type)
165 {
166 downloadType_ = type;
167 }
168
GetDownloadType()169 int32_t DataHandler::GetDownloadType()
170 {
171 return downloadType_;
172 }
SetChecking()173 void DataHandler::SetChecking()
174 {
175 ClearCursor();
176 isChecking_ = true;
177 cloudPrefImpl_.SetBool(CHECKING_FLAG, isChecking_);
178 }
179
GetCheckRecords(std::vector<DriveKit::DKRecordId> & checkRecords,const std::shared_ptr<std::vector<DriveKit::DKRecord>> & records)180 int32_t DataHandler::GetCheckRecords(std::vector<DriveKit::DKRecordId> &checkRecords,
181 const std::shared_ptr<std::vector<DriveKit::DKRecord>> &records)
182 {
183 return E_OK;
184 }
185
GetFileModifiedRecords(vector<DriveKit::DKRecord> & records)186 int32_t DataHandler::GetFileModifiedRecords(vector<DriveKit::DKRecord> &records)
187 {
188 return E_OK;
189 }
190
OnModifyFdirtyRecords(const map<DriveKit::DKRecordId,DriveKit::DKRecordOperResult> & map)191 int32_t DataHandler::OnModifyFdirtyRecords(const map<DriveKit::DKRecordId,
192 DriveKit::DKRecordOperResult> &map)
193 {
194 return E_OK;
195 }
196
GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> & outAssetsToDownload)197 int32_t DataHandler::GetAssetsToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload)
198 {
199 return E_OK;
200 }
201
GetThumbToDownload(std::vector<DriveKit::DKDownloadAsset> & outAssetsToDownload)202 int32_t DataHandler::GetThumbToDownload(std::vector<DriveKit::DKDownloadAsset> &outAssetsToDownload)
203 {
204 return E_OK;
205 }
206
GetDownloadAsset(string cloudId,vector<DriveKit::DKDownloadAsset> & outAssetsToDownload)207 int32_t DataHandler::GetDownloadAsset(string cloudId,
208 vector<DriveKit::DKDownloadAsset> &outAssetsToDownload)
209 {
210 return E_OK;
211 }
212
OnDownloadSuccess(const DriveKit::DKDownloadAsset & asset)213 int32_t DataHandler::OnDownloadSuccess(const DriveKit::DKDownloadAsset &asset)
214 {
215 return E_OK;
216 }
217
OnDownloadAssets(const std::map<DriveKit::DKDownloadAsset,DriveKit::DKDownloadResult> & resultMap)218 int32_t DataHandler::OnDownloadAssets(const std::map<DriveKit::DKDownloadAsset, DriveKit::DKDownloadResult> &resultMap)
219 {
220 return E_OK;
221 }
222
OnDownloadAssets(const DriveKit::DKDownloadAsset & asset)223 int32_t DataHandler::OnDownloadAssets(const DriveKit::DKDownloadAsset &asset)
224 {
225 return E_OK;
226 }
227
OnDownloadAssetsFailure(const std::vector<DriveKit::DKDownloadAsset> & assets)228 int32_t DataHandler::OnDownloadAssetsFailure(const std::vector<DriveKit::DKDownloadAsset> &assets)
229 {
230 return E_OK;
231 }
232
Clean(const int action)233 int32_t DataHandler::Clean(const int action)
234 {
235 return E_OK;
236 }
237
OnRecordFailed(const std::pair<DKRecordId,DKRecordOperResult> & entry)238 int32_t DataHandler::OnRecordFailed(const std::pair<DKRecordId, DKRecordOperResult> &entry)
239 {
240 const DKRecordOperResult &result = entry.second;
241 int32_t serverErrorCode = INT32_MAX;
242 serverErrorCode = result.GetDKError().serverErrorCode;
243 LOGE("serverErrorCode %{public}d", serverErrorCode);
244 if (static_cast<DKServerErrorCode>(serverErrorCode) == DKServerErrorCode::NETWORK_ERROR) {
245 return HandleNetworkErr();
246 } else if ((static_cast<DKServerErrorCode>(serverErrorCode) == DKServerErrorCode::UID_EMPTY) ||
247 (static_cast<DKServerErrorCode>(serverErrorCode) == DKServerErrorCode::SWITCH_OFF)) {
248 return HandleNotSupportSync();
249 }
250 DKErrorType errorType = result.GetDKError().errorType;
251 if (result.GetDKError().errorDetails.size() == 0 && errorType != DKErrorType::TYPE_NOT_NEED_RETRY) {
252 LOGE("errorDetails is empty and errorType is invalid");
253 return E_INVAL_ARG;
254 } else if (result.GetDKError().errorDetails.size() != 0) {
255 auto errorDetailcode = static_cast<DKDetailErrorCode>(result.GetDKError().errorDetails[0].detailCode);
256 if (errorDetailcode == DKDetailErrorCode::SPACE_FULL) {
257 return HandleCloudSpaceNotEnough();
258 }
259 if (errorType != DKErrorType::TYPE_NOT_NEED_RETRY) {
260 LOGE("unknown error code record failed, serverErrorCode = %{public}d, errorDetailcode = %{public}d",
261 serverErrorCode, errorDetailcode);
262 return HandleDetailcode(errorDetailcode);
263 }
264 LOGE("errorDetailcode = %{public}d, errorType = %{public}d, no need retry",
265 errorDetailcode, static_cast<int32_t>(errorType));
266 return E_STOP;
267 } else {
268 LOGE("errorType = %{public}d, no need retry", static_cast<int32_t>(errorType));
269 return E_STOP;
270 }
271 return E_UNKNOWN;
272 }
273
HandleCloudSpaceNotEnough()274 int32_t DataHandler::HandleCloudSpaceNotEnough()
275 {
276 LOGE("Cloud Space Not Enough");
277 /* Stop sync */
278 return E_CLOUD_STORAGE_FULL;
279 }
280
HandleNotSupportSync()281 int32_t DataHandler::HandleNotSupportSync()
282 {
283 LOGE("switch off or uid empty");
284 return E_STOP;
285 }
286
HandleNetworkErr()287 int32_t DataHandler::HandleNetworkErr()
288 {
289 LOGE("Network Error");
290 return E_SYNC_FAILED_NETWORK_NOT_AVAILABLE;
291 }
292
HandleDetailcode(DKDetailErrorCode detailCode)293 int32_t DataHandler::HandleDetailcode(DKDetailErrorCode detailCode)
294 {
295 /* Only one record failed, not stop sync */
296 return E_UNKNOWN;
297 }
298
GetReturn(const int32_t error,int32_t & retCode)299 void DataHandler::GetReturn(const int32_t error, int32_t &retCode)
300 {
301 if ((error == E_STOP) || (error == E_CLOUD_STORAGE_FULL) || (error == E_SYNC_FAILED_NETWORK_NOT_AVAILABLE)) {
302 retCode = error;
303 }
304 }
305 } // namespace CloudSync
306 } // namespace FileManagement
307 } // namespace OHOS
308