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_syncer.h"
17 #include "clouddisk_rdbstore.h"
18 #include "dfs_error.h"
19 #include "utils_log.h"
20
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudSync {
24 using namespace std;
25 using namespace OHOS::FileManagement::CloudDisk;
CloudDiskDataSyncer(const std::string bundleName,const int32_t userId)26 CloudDiskDataSyncer::CloudDiskDataSyncer(const std::string bundleName, const int32_t userId)
27 : DataSyncer(bundleName, userId) {}
28
Init(const std::string bundleName,const int32_t userId)29 int32_t CloudDiskDataSyncer::Init(const std::string bundleName, const int32_t userId)
30 {
31 CloudDisk::CloudDiskRdbStore cloudDiskRdbStore(bundleName_, userId_);
32 auto rdb = cloudDiskRdbStore.GetRaw();
33 if (!rdb) {
34 LOGE("clouddisk rdb init fail");
35 return E_RDB;
36 }
37 /* init handler */
38 cloudDiskHandler_ = make_shared<CloudDiskDataHandler>(userId_, bundleName_, rdb, stopFlag_);
39 return E_OK;
40 }
41
Clean(const int action)42 int32_t CloudDiskDataSyncer::Clean(const int action)
43 {
44 LOGD("cloud disk data sycner Clean");
45 BeginClean();
46 int32_t ret = CancelDownload(cloudDiskHandler_);
47 if (ret != E_OK) {
48 LOGE("CloudDisk data syncer file cancel download err %{public}d", ret);
49 }
50 ret = CleanInner(cloudDiskHandler_, action);
51 if (ret != E_OK) {
52 LOGE("disk data syncer file clean err %{public}d", ret);
53 }
54 CompleteClean();
55 return ret;
56 }
57
StartDownloadFile(const std::string path,const int32_t userId)58 int32_t CloudDiskDataSyncer::StartDownloadFile(const std::string path, const int32_t userId)
59 {
60 return DownloadInner(cloudDiskHandler_, path, userId);
61 }
62
StopDownloadFile(const std::string path,const int32_t userId)63 int32_t CloudDiskDataSyncer::StopDownloadFile(const std::string path, const int32_t userId)
64 {
65 return DataSyncer::StopDownloadFile(path, userId);
66 }
67
CleanCache(const string & uri)68 int32_t CloudDiskDataSyncer::CleanCache(const string &uri)
69 {
70 return cloudDiskHandler_->CleanCache(uri);
71 }
72
Schedule()73 void CloudDiskDataSyncer::Schedule()
74 {
75 LOGI("schedule to stage %{public}d", ++stage_);
76 int32_t ret = E_OK;
77 switch (stage_) {
78 case DOWNLOADFILE: {
79 ret = DownloadFile();
80 break;
81 }
82 case COMPLETEPULL: {
83 ret = CompletePull();
84 break;
85 }
86 case UPLOADFILE: {
87 ret = UploadFile();
88 break;
89 }
90 case COMPLETEPUSH: {
91 ret = CompletePush();
92 break;
93 }
94 case END: {
95 ret = Complete();
96 break;
97 }
98 default: {
99 ret = E_SCHEDULE;
100 LOGE("schedule fail %{public}d", ret);
101 break;
102 }
103 }
104 /* if error takes place while schedule, just abort it */
105 if (ret != E_OK) {
106 Abort();
107 }
108 }
109
Reset()110 void CloudDiskDataSyncer::Reset()
111 {
112 stage_ = BEGIN;
113 cloudDiskHandler_->Reset();
114 }
115
DownloadFile()116 int32_t CloudDiskDataSyncer::DownloadFile()
117 {
118 LOGI("cloud disk data sycner download file");
119 int ret = Pull(cloudDiskHandler_);
120 if (ret != E_OK) {
121 LOGE("disk data syncer pull file err %{public}d", ret);
122 }
123 return ret;
124 }
125
UploadFile()126 int32_t CloudDiskDataSyncer::UploadFile()
127 {
128 LOGI("cloud disk data sycner upload file");
129 int32_t ret = Push(cloudDiskHandler_);
130 if (ret != E_OK) {
131 LOGE("disk data syncer push file err %{public}d", ret);
132 }
133 return ret;
134 }
135
Complete(bool isNeedNotify)136 int32_t CloudDiskDataSyncer::Complete(bool isNeedNotify)
137 {
138 LOGI("cloud disk data syncer complete all");
139 Unlock();
140 CompleteAll(isNeedNotify);
141 if (isDataChanged_) {
142 ChangesNotify();
143 isDataChanged_ = false;
144 }
145 return E_OK;
146 }
147
ChangesNotify()148 void CloudDiskDataSyncer::ChangesNotify()
149 {
150 sdkHelper_->ChangesNotify([] (auto, DriveKit::DKError err) {
151 if (err.HasError()) {
152 LOGE("drivekit changes notify server err %{public}d and dk error %{public}d", err.serverErrorCode,
153 err.dkErrorCode);
154 }
155 });
156 }
157
ScheduleByType(SyncTriggerType syncTriggerType)158 void CloudDiskDataSyncer::ScheduleByType(SyncTriggerType syncTriggerType)
159 {
160 Schedule();
161 return;
162 }
163 } // namespace CloudSync
164 } // namespace FileManagement
165 } // namespace OHOS