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 "cloud_disk_data_convertor.h"
17
18 #include <regex>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include "cloud_disk_data_const.h"
23 #include "mimetype_utils.h"
24 #include "cloud_pref_impl.h"
25 #include "cloud_file_utils.h"
26
27 namespace OHOS {
28 namespace FileManagement {
29 namespace CloudSync {
30 using namespace std;
31 using namespace NativeRdb;
32 using namespace CloudDisk;
33 using DriveKit::DKLocalErrorCode;
34
35 string CloudDiskDataConvertor::recordType_ = "file";
36
CloudDiskDataConvertor(int32_t userId,string & bundleName,OperationType type,const function<void (int32_t,NativeRdb::ResultSet & resultSet)> & func)37 CloudDiskDataConvertor::CloudDiskDataConvertor(int32_t userId,
38 string &bundleName,
39 OperationType type,
40 const function<void(int32_t, NativeRdb::ResultSet &resultSet)> &func)
41 : userId_(userId), bundleName_(bundleName), type_(type), errHandler_(func)
42 {
43 Media::MimeTypeUtils::InitMimeTypeMap();
44 }
HandleErr(int32_t err,NativeRdb::ResultSet & resultSet)45 void CloudDiskDataConvertor::HandleErr(int32_t err, NativeRdb::ResultSet &resultSet)
46 {
47 if (errHandler_ != nullptr) {
48 errHandler_(err, resultSet);
49 }
50 }
51
SetRootId(string rootId)52 void CloudDiskDataConvertor::SetRootId(string rootId)
53 {
54 rootId_ = rootId;
55 }
56
InitRootId()57 int32_t CloudDiskDataConvertor::InitRootId()
58 {
59 if (!rootId_.empty()) {
60 return E_OK;
61 }
62 CloudPrefImpl cloudPrefImpl(userId_, bundleName_, FileColumn::FILES_TABLE);
63 cloudPrefImpl.GetString("rootId", rootId_);
64 LOGD("local rootId is %{public}s", rootId_.c_str());
65 if (rootId_.empty()) {
66 auto driveKit = DriveKit::DriveKitNative::GetInstance(userId_);
67 if (driveKit == nullptr) {
68 LOGE("sdk helper get drive kit instance fail");
69 return E_INVAL_ARG;
70 }
71 auto container = driveKit->GetDefaultContainer(bundleName_);
72 if (container == nullptr) {
73 LOGE("sdk helper get drive kit container fail");
74 return E_INVAL_ARG;
75 }
76 shared_ptr<DriveKit::DKDatabase> database = container->GetPrivateDatabase();
77 if (database == nullptr) {
78 LOGE("sdk helper get drive kit database fail");
79 return E_INVAL_ARG;
80 }
81 DriveKit::DKError dkErr = database->GetRootId(rootId_);
82 LOGD("database rootId is %{public}s", rootId_.c_str());
83 if (dkErr.dkErrorCode != DriveKit::DKLocalErrorCode::NO_ERROR || rootId_.empty()) {
84 LOGE("get root id failed, err %{public}d", dkErr.dkErrorCode);
85 return E_INVAL_ARG;
86 }
87 cloudPrefImpl.SetString("rootId", rootId_);
88 }
89 return E_OK;
90 }
GetParentCloudId(const DriveKit::DKRecordData & data)91 std::string CloudDiskDataConvertor::GetParentCloudId(const DriveKit::DKRecordData &data)
92 {
93 string parentFolder;
94 if (data.find(DK_PARENT_CLOUD_ID) == data.end() ||
95 data.at(DK_PARENT_CLOUD_ID).GetString(parentFolder) != DKLocalErrorCode::NO_ERROR) {
96 LOGE("extract parent cloudId error");
97 return "";
98 }
99 if (parentFolder == rootId_) {
100 parentFolder = "rootId";
101 }
102 return parentFolder;
103 }
104
Convert(DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)105 int32_t CloudDiskDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ValuesBucket &valueBucket)
106 {
107 RETURN_ON_ERR(InitRootId());
108 DriveKit::DKRecordData data;
109 record.GetRecordData(data);
110 ExtractCompatibleValue(record, data, valueBucket);
111 return E_OK;
112 }
Convert(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)113 int32_t CloudDiskDataConvertor::Convert(DriveKit::DKRecord &record, NativeRdb::ResultSet &resultSet)
114 {
115 RETURN_ON_ERR(InitRootId());
116 DriveKit::DKRecordData data;
117 RETURN_ON_ERR(FillRecordId(record, resultSet));
118 RETURN_ON_ERR(FillCreatedTime(record, resultSet));
119 RETURN_ON_ERR(FillMetaEditedTime(record, resultSet));
120 RETURN_ON_ERR(FillVersion(record, resultSet));
121 RETURN_ON_ERR(HandleCompatibleFileds(data, resultSet));
122 RETURN_ON_ERR(HandleAttributes(data, resultSet));
123 record.SetRecordData(data);
124 record.SetRecordType(recordType_);
125 if (type_ == FILE_CREATE) {
126 record.SetNewCreate(true);
127 }
128 return E_OK;
129 }
ExtractCompatibleValue(const DriveKit::DKRecord & record,const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)130 int32_t CloudDiskDataConvertor::ExtractCompatibleValue(const DriveKit::DKRecord &record,
131 const DriveKit::DKRecordData &data,
132 NativeRdb::ValuesBucket &valueBucket)
133 {
134 RETURN_ON_ERR(ExtractCloudId(record, valueBucket));
135 if (record.GetIsDelete()) {
136 return E_OK;
137 }
138 RETURN_ON_ERR(ExtractFileName(data, valueBucket));
139 RETURN_ON_ERR(ExtractFileParentCloudId(data, valueBucket));
140 RETURN_ON_ERR(ExtractDirectlyRecycled(data, valueBucket));
141 RETURN_ON_ERR(ExtractIsDirectory(data, valueBucket));
142 RETURN_ON_ERR(ExtractVersion(record, valueBucket));
143 CompensateAttributes(data, record, valueBucket);
144 ExtractFileTimeRecycled(data, valueBucket);
145 return E_OK;
146 }
CompensateAttributes(const DriveKit::DKRecordData & data,const DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)147 int32_t CloudDiskDataConvertor::CompensateAttributes(const DriveKit::DKRecordData &data,
148 const DriveKit::DKRecord &record,
149 NativeRdb::ValuesBucket &valueBucket)
150 {
151 int64_t fileTimeAdded = 0;
152 int64_t fileTimeEdited = 0;
153 int64_t metaTimeEdited = 0;
154 DriveKit::DKRecordFieldMap attributes;
155 if (data.find(DK_FILE_ATTRIBUTES) == data.end()) {
156 fileTimeAdded = static_cast<int64_t>(record.GetCreateTime());
157 fileTimeEdited = static_cast<int64_t>(record.GetEditedTime());
158 metaTimeEdited = static_cast<int64_t>(record.GetEditedTime());
159 } else {
160 data.at(DK_FILE_ATTRIBUTES).GetRecordMap(attributes);
161 if (attributes.find(DK_FILE_TIME_ADDED) == attributes.end() ||
162 attributes[DK_FILE_TIME_ADDED].GetLong(fileTimeAdded) != DKLocalErrorCode::NO_ERROR) {
163 fileTimeAdded = static_cast<int64_t>(record.GetCreateTime());
164 }
165 if (attributes.find(DK_FILE_TIME_EDITED) == attributes.end() ||
166 attributes[DK_FILE_TIME_EDITED].GetLong(fileTimeEdited) != DKLocalErrorCode::NO_ERROR) {
167 fileTimeEdited = static_cast<int64_t>(record.GetEditedTime());
168 }
169 if (attributes.find(DK_META_TIME_EDITED) == attributes.end() ||
170 attributes[DK_META_TIME_EDITED].GetLong(metaTimeEdited) != DKLocalErrorCode::NO_ERROR) {
171 metaTimeEdited = static_cast<int64_t>(record.GetEditedTime());
172 }
173 }
174 valueBucket.PutLong(FileColumn::FILE_TIME_ADDED, fileTimeAdded);
175 valueBucket.PutLong(FileColumn::FILE_TIME_EDITED, fileTimeEdited);
176 valueBucket.PutLong(FileColumn::META_TIME_EDITED, metaTimeEdited);
177 return E_OK;
178 }
ExtractCloudId(const DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)179 int32_t CloudDiskDataConvertor::ExtractCloudId(const DriveKit::DKRecord &record,
180 NativeRdb::ValuesBucket &valueBucket)
181 {
182 std::string cloudId = record.GetRecordId();
183 valueBucket.PutString(FileColumn::CLOUD_ID, cloudId);
184 return E_OK;
185 }
ExtractFileName(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)186 int32_t CloudDiskDataConvertor::ExtractFileName(const DriveKit::DKRecordData &data,
187 NativeRdb::ValuesBucket &valueBucket)
188 {
189 string fileName;
190 if (data.find(DK_FILE_NAME) == data.end() ||
191 data.at(DK_FILE_NAME).GetString(fileName) != DKLocalErrorCode::NO_ERROR) {
192 LOGE("extract file name error");
193 return E_INVAL_ARG;
194 }
195 size_t pos = fileName.find_last_of('.');
196 if (pos != string::npos) {
197 string extension = fileName.substr(fileName.find_last_of('.') + 1, fileName.length());
198 auto mimeType = Media::MimeTypeUtils::GetMimeTypeFromExtension(extension);
199 auto mediaType = Media::MimeTypeUtils::GetMediaTypeFromMimeType(mimeType);
200 valueBucket.PutString(FileColumn::FILE_CATEGORY, extension);
201 valueBucket.PutString(FileColumn::MIME_TYPE, mimeType);
202 valueBucket.PutInt(FileColumn::FILE_TYPE, static_cast<int32_t>(mediaType));
203 }
204 valueBucket.PutString(FileColumn::FILE_NAME, fileName);
205 return E_OK;
206 }
ExtractFileParentCloudId(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)207 int32_t CloudDiskDataConvertor::ExtractFileParentCloudId(const DriveKit::DKRecordData &data,
208 NativeRdb::ValuesBucket &valueBucket)
209 {
210 string parentFolder = GetParentCloudId(data);
211 if (parentFolder.empty()) {
212 LOGE("extract parent cloudId error");
213 return E_INVAL_ARG;
214 }
215 valueBucket.PutString(FileColumn::PARENT_CLOUD_ID, parentFolder);
216 return E_OK;
217 }
ExtractFileSize(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)218 int32_t CloudDiskDataConvertor::ExtractFileSize(const DriveKit::DKRecordData &data,
219 NativeRdb::ValuesBucket &valueBucket)
220 {
221 int64_t fileSize;
222 if (data.find(DK_FILE_SIZE) == data.end() ||
223 data.at(DK_FILE_SIZE).GetLong(fileSize) != DKLocalErrorCode::NO_ERROR) {
224 LOGE("extract fileSize cloudId error");
225 return E_INVAL_ARG;
226 }
227 valueBucket.PutLong(FileColumn::FILE_SIZE, fileSize);
228 return E_OK;
229 }
230
ExtractSha256(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)231 int32_t CloudDiskDataConvertor::ExtractSha256(const DriveKit::DKRecordData &data,
232 NativeRdb::ValuesBucket &valueBucket)
233 {
234 string fileSha256;
235 if (data.find(DK_FILE_SHA256) == data.end() ||
236 data.at(DK_FILE_SHA256).GetString(fileSha256) != DKLocalErrorCode::NO_ERROR) {
237 LOGW("extract sha256 error");
238 return E_INVAL_ARG;
239 }
240 valueBucket.PutString(FileColumn::FILE_SHA256, fileSha256);
241 return E_OK;
242 }
ExtractFileTimeRecycled(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)243 int32_t CloudDiskDataConvertor::ExtractFileTimeRecycled(const DriveKit::DKRecordData &data,
244 NativeRdb::ValuesBucket &valueBucket)
245 {
246 int64_t fileTimeRecycled = 0;
247 bool isRecyled = false;
248 if (data.find(DK_IS_RECYCLED) == data.end()
249 || data.at(DK_IS_RECYCLED).GetBool(isRecyled) != DKLocalErrorCode::NO_ERROR) {
250 LOGE("extract DK_IS_RECYCLED error");
251 return E_INVAL_ARG;
252 }
253 if (isRecyled) {
254 if (data.find(DK_FILE_TIME_RECYCLED) == data.end() ||
255 data.at(DK_FILE_TIME_RECYCLED).GetLong(fileTimeRecycled) != DKLocalErrorCode::NO_ERROR) {
256 LOGE("extract fileTimeRecycled error");
257 return E_INVAL_ARG;
258 }
259 }
260 valueBucket.PutLong(FileColumn::FILE_TIME_RECYCLED, fileTimeRecycled);
261 return E_OK;
262 }
ExtractDirectlyRecycled(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)263 int32_t CloudDiskDataConvertor::ExtractDirectlyRecycled(const DriveKit::DKRecordData &data,
264 NativeRdb::ValuesBucket &valueBucket)
265 {
266 bool directoryRecycled;
267 if (data.find(DK_DIRECTLY_RECYCLED) == data.end() ||
268 data.at(DK_DIRECTLY_RECYCLED).GetBool(directoryRecycled) != DKLocalErrorCode::NO_ERROR) {
269 LOGE("extract directoryRecycled error");
270 return E_INVAL_ARG;
271 }
272 valueBucket.PutInt(FileColumn::DIRECTLY_RECYCLED, directoryRecycled);
273 return E_OK;
274 }
275
ExtractVersion(const DriveKit::DKRecord & record,NativeRdb::ValuesBucket & valueBucket)276 int32_t CloudDiskDataConvertor::ExtractVersion(const DriveKit::DKRecord &record,
277 NativeRdb::ValuesBucket &valueBucket)
278 {
279 unsigned long version = record.GetVersion();
280 valueBucket.PutLong(FileColumn::VERSION, version);
281 return E_OK;
282 }
ExtractIsDirectory(const DriveKit::DKRecordData & data,NativeRdb::ValuesBucket & valueBucket)283 int32_t CloudDiskDataConvertor::ExtractIsDirectory(const DriveKit::DKRecordData &data,
284 NativeRdb::ValuesBucket &valueBucket)
285 {
286 string fileType;
287 if (data.find(DK_IS_DIRECTORY) == data.end() ||
288 data.at(DK_IS_DIRECTORY).GetString(fileType) != DKLocalErrorCode::NO_ERROR) {
289 LOGE("extract type error");
290 return E_INVAL_ARG;
291 }
292 int32_t type = DIRECTORY;
293 if (fileType == "file") {
294 type = FILE;
295 ExtractSha256(data, valueBucket);
296 RETURN_ON_ERR(ExtractFileSize(data, valueBucket));
297 }
298 valueBucket.PutInt(FileColumn::IS_DIRECTORY, type);
299 return E_OK;
300 }
HandleCompatibleFileds(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)301 int32_t CloudDiskDataConvertor::HandleCompatibleFileds(DriveKit::DKRecordData &data,
302 NativeRdb::ResultSet &resultSet)
303 {
304 RETURN_ON_ERR(HandleFileName(data, resultSet));
305 RETURN_ON_ERR(HandleParentId(data, resultSet));
306 RETURN_ON_ERR(HandleDirectlyRecycled(data, resultSet));
307 RETURN_ON_ERR(HandleRecycleTime(data, resultSet));
308 RETURN_ON_ERR(HandleType(data, resultSet));
309 RETURN_ON_ERR(HandleOperateType(data, resultSet));
310 RETURN_ON_ERR(HandleFileSize(data, resultSet));
311 RETURN_ON_ERR(HandleAttachments(data, resultSet));
312 return E_OK;
313 }
HandleAttachments(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)314 int32_t CloudDiskDataConvertor::HandleAttachments(DriveKit::DKRecordData &data,
315 NativeRdb::ResultSet &resultSet)
316 {
317 if (type_ != FILE_CREATE && type_ != FILE_DATA_MODIFY) {
318 return E_OK;
319 }
320 int32_t isDirectory;
321 if (GetInt(FileColumn::IS_DIRECTORY, isDirectory, resultSet)) {
322 LOGE("Get File Type is failed");
323 return E_RDB;
324 }
325 if (isDirectory == FILE) {
326 string cloudId;
327 string filePath;
328 if (!GetString(FileColumn::CLOUD_ID, cloudId, resultSet)) {
329 filePath = CloudFileUtils::GetLocalFilePath(cloudId, bundleName_, userId_);
330 } else {
331 LOGE("Get File Path is failed");
332 return E_RDB;
333 }
334 int32_t ret = HandleContent(data, filePath);
335 if (ret != E_OK) {
336 LOGE("handle content err %{public}d", ret);
337 return ret;
338 }
339 }
340 return E_OK;
341 }
HandleAttributes(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)342 int32_t CloudDiskDataConvertor::HandleAttributes(DriveKit::DKRecordData &data,
343 NativeRdb::ResultSet &resultSet)
344 {
345 if (type_ == FILE_DELETE) {
346 return E_OK;
347 }
348 DriveKit::DKRecordFieldMap map;
349 RETURN_ON_ERR(HandleCreateTime(map, resultSet));
350 RETURN_ON_ERR(HandleMetaEditedTime(map, resultSet));
351 RETURN_ON_ERR(HandleEditedTime(map, resultSet));
352 data[DK_FILE_ATTRIBUTES] = DriveKit::DKRecordField(map);
353 return E_OK;
354 }
FillRecordId(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)355 int32_t CloudDiskDataConvertor::FillRecordId(DriveKit::DKRecord &record,
356 NativeRdb::ResultSet &resultSet)
357 {
358 string val;
359 int32_t ret = GetString(FileColumn::CLOUD_ID, val, resultSet);
360 if (ret != E_OK) {
361 LOGE("Fill record id failed, ret %{public}d", ret);
362 return ret;
363 }
364 record.SetRecordId(val);
365 return E_OK;
366 }
FillCreatedTime(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)367 int32_t CloudDiskDataConvertor::FillCreatedTime(DriveKit::DKRecord &record,
368 NativeRdb::ResultSet &resultSet)
369 {
370 if (type_ == FILE_DELETE) {
371 return E_OK;
372 }
373 int64_t createdTime;
374 int32_t ret = GetLong(FileColumn::FILE_TIME_ADDED, createdTime, resultSet);
375 if (ret != E_OK) {
376 LOGE("Fill CreatedTime failed, ret = %{public}d", ret);
377 return ret;
378 }
379 record.SetCreateTime(static_cast<uint64_t>(createdTime));
380 return E_OK;
381 }
FillMetaEditedTime(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)382 int32_t CloudDiskDataConvertor::FillMetaEditedTime(DriveKit::DKRecord &record,
383 NativeRdb::ResultSet &resultSet)
384 {
385 if (type_ == FILE_DELETE) {
386 return E_OK;
387 }
388 int64_t metaEditedTime;
389 int32_t ret = GetLong(FileColumn::META_TIME_EDITED, metaEditedTime, resultSet);
390 if (ret != E_OK) {
391 LOGE("Fill MetaEditedTime failed, ret = %{public}d", ret);
392 return ret;
393 }
394 record.SetEditedTime(static_cast<uint64_t>(metaEditedTime));
395 return E_OK;
396 }
FillVersion(DriveKit::DKRecord & record,NativeRdb::ResultSet & resultSet)397 int32_t CloudDiskDataConvertor::FillVersion(DriveKit::DKRecord &record,
398 NativeRdb::ResultSet &resultSet)
399 {
400 int64_t version;
401 int32_t ret = GetLong(FileColumn::VERSION, version, resultSet);
402 if (ret != E_OK) {
403 LOGE("Fill Version failed, ret = %{public}d", ret);
404 return ret;
405 }
406 record.SetVersion(static_cast<unsigned long>(version));
407 return E_OK;
408 }
HandleFileName(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)409 int32_t CloudDiskDataConvertor::HandleFileName(DriveKit::DKRecordData &data,
410 NativeRdb::ResultSet &resultSet)
411 {
412 if (type_ == FILE_DELETE) {
413 return E_OK;
414 }
415 std::string displayName;
416 int32_t ret = GetString(FileColumn::FILE_NAME, displayName, resultSet);
417 if (ret != E_OK) {
418 LOGE("handler FileName failed, ret = %{public}d", ret);
419 return ret;
420 }
421 data[DK_FILE_NAME] = DriveKit::DKRecordField(displayName);
422 return E_OK;
423 }
HandleParentId(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)424 int32_t CloudDiskDataConvertor::HandleParentId(DriveKit::DKRecordData &data,
425 NativeRdb::ResultSet &resultSet)
426 {
427 if (type_ == FILE_DELETE) {
428 return E_OK;
429 }
430 std::string parentFolder;
431 int32_t ret = GetString(FileColumn::PARENT_CLOUD_ID, parentFolder, resultSet);
432 if (ret != E_OK) {
433 LOGE("handler ParentId failed, ret = %{public}d", ret);
434 return ret;
435 }
436 if (parentFolder == "rootId") {
437 parentFolder = rootId_;
438 }
439 data[DK_PARENT_CLOUD_ID] = DriveKit::DKRecordField(parentFolder);
440 return E_OK;
441 }
HandleDirectlyRecycled(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)442 int32_t CloudDiskDataConvertor::HandleDirectlyRecycled(DriveKit::DKRecordData &data,
443 NativeRdb::ResultSet &resultSet)
444 {
445 if (type_ == FILE_DELETE) {
446 return E_OK;
447 }
448 int32_t directlyRecycled;
449 int32_t ret = GetInt(FileColumn::DIRECTLY_RECYCLED, directlyRecycled, resultSet);
450 if (ret != E_OK) {
451 LOGE("handler DirectlyRecycled failed, ret = %{public}d", ret);
452 return ret;
453 }
454 data[DK_DIRECTLY_RECYCLED] = DriveKit::DKRecordField((directlyRecycled != 0));
455 return E_OK;
456 }
HandleRecycleTime(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)457 int32_t CloudDiskDataConvertor::HandleRecycleTime(DriveKit::DKRecordData &data,
458 NativeRdb::ResultSet &resultSet)
459 {
460 if (type_ == FILE_DELETE) {
461 return E_OK;
462 }
463 int64_t recycleTime;
464 int32_t ret = GetLong(FileColumn::FILE_TIME_RECYCLED, recycleTime, resultSet);
465 if (ret != E_OK) {
466 LOGE("handler RecycleTime failed, ret = %{public}d", ret);
467 return ret;
468 }
469 data[DK_IS_RECYCLED] = DriveKit::DKRecordField((recycleTime != 0));
470 if (recycleTime != 0) {
471 data[DK_FILE_TIME_RECYCLED] = DriveKit::DKRecordField(recycleTime);
472 }
473 return E_OK;
474 }
HandleType(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)475 int32_t CloudDiskDataConvertor::HandleType(DriveKit::DKRecordData &data,
476 NativeRdb::ResultSet &resultSet)
477 {
478 int32_t type;
479 int32_t ret = GetInt(FileColumn::IS_DIRECTORY, type, resultSet);
480 if (ret != E_OK) {
481 LOGE("handler type failed, ret = %{public}d", ret);
482 return ret;
483 }
484 data[DK_IS_DIRECTORY] = DriveKit::DKRecordField(type == DIRECTORY ? "directory" : "file");
485 return E_OK;
486 }
HandleOperateType(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)487 int32_t CloudDiskDataConvertor::HandleOperateType(DriveKit::DKRecordData &data,
488 NativeRdb::ResultSet &resultSet)
489 {
490 if (type_ == FILE_DELETE) {
491 return E_OK;
492 }
493 int64_t operateType;
494 int32_t ret = GetLong(FileColumn::OPERATE_TYPE, operateType, resultSet);
495 if (ret != E_OK) {
496 LOGE("handler operateType failed, ret = %{public}d", ret);
497 return ret;
498 }
499 data[DK_FILE_OPERATE_TYPE] = DriveKit::DKRecordField(operateType);
500 return E_OK;
501 }
HandleFileSize(DriveKit::DKRecordData & data,NativeRdb::ResultSet & resultSet)502 int32_t CloudDiskDataConvertor::HandleFileSize(DriveKit::DKRecordData &data,
503 NativeRdb::ResultSet &resultSet)
504 {
505 if (type_ == FILE_DELETE) {
506 return E_OK;
507 }
508 int64_t fileSize;
509 int32_t ret = GetLong(FileColumn::FILE_SIZE, fileSize, resultSet);
510 if (ret != E_OK) {
511 LOGE("handler fileSize failed, ret = %{public}d", ret);
512 return ret;
513 }
514 data[DK_FILE_SIZE] = DriveKit::DKRecordField(fileSize);
515 return E_OK;
516 }
HandleCreateTime(DriveKit::DKRecordFieldMap & map,NativeRdb::ResultSet & resultSet)517 int32_t CloudDiskDataConvertor::HandleCreateTime(DriveKit::DKRecordFieldMap &map,
518 NativeRdb::ResultSet &resultSet)
519 {
520 int64_t createTime;
521 int32_t ret = GetLong(FileColumn::FILE_TIME_ADDED, createTime, resultSet);
522 if (ret != E_OK) {
523 LOGE("handler CreateTime failed, ret = %{public}d", ret);
524 return ret;
525 }
526 map[DK_FILE_TIME_ADDED] = DriveKit::DKRecordField(createTime);
527 return E_OK;
528 }
HandleMetaEditedTime(DriveKit::DKRecordFieldMap & map,NativeRdb::ResultSet & resultSet)529 int32_t CloudDiskDataConvertor::HandleMetaEditedTime(DriveKit::DKRecordFieldMap &map,
530 NativeRdb::ResultSet &resultSet)
531 {
532 int64_t metaEditedTime;
533 int32_t ret = GetLong(FileColumn::META_TIME_EDITED, metaEditedTime, resultSet);
534 if (ret != E_OK) {
535 LOGE("handler MetaEditedTime failed, ret = %{public}d", ret);
536 return ret;
537 }
538 map[DK_META_TIME_EDITED] = DriveKit::DKRecordField(metaEditedTime);
539 return E_OK;
540 }
HandleEditedTime(DriveKit::DKRecordFieldMap & map,NativeRdb::ResultSet & resultSet)541 int32_t CloudDiskDataConvertor::HandleEditedTime(DriveKit::DKRecordFieldMap &map,
542 NativeRdb::ResultSet &resultSet)
543 {
544 int64_t editedTime;
545 int32_t ret = GetLong(FileColumn::FILE_TIME_EDITED, editedTime, resultSet);
546 if (ret != E_OK) {
547 LOGE("handler EditedTime failed, ret = %{public}d", ret);
548 return ret;
549 }
550 map[DK_FILE_TIME_EDITED] = DriveKit::DKRecordField(editedTime);
551 return E_OK;
552 }
HandleContent(DriveKit::DKRecordData & data,string & path)553 int32_t CloudDiskDataConvertor::HandleContent(DriveKit::DKRecordData &data,
554 string &path)
555 {
556 if (access(path.c_str(), F_OK)) {
557 LOGE("content %{private}s doesn't exist", path.c_str());
558 return E_PATH;
559 }
560 DriveKit::DKAsset content;
561 content.uri = move(path);
562 content.assetName = DK_FILE_CONTENT;
563 content.operationType = DriveKit::DKAssetOperType::DK_ASSET_ADD;
564 data[DK_FILE_CONTENT] = DriveKit::DKRecordField(content);
565 return E_OK;
566 }
567 } // namespace CloudSync
568 } // namespace FileManagement
569 } // namespace OHOS