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 #include "ipc/cloud_sync_service.h"
16
17 #include <cstdint>
18 #include <memory>
19
20 #include "battery_status.h"
21 #include "cloud_file_kit.h"
22 #include "cloud_status.h"
23 #include "clouddisk_rdb_utils.h"
24 #include "cycle_task/cycle_task_runner.h"
25 #include "data_sync_const.h"
26 #include "data_syncer_rdb_store.h"
27 #include "dfs_error.h"
28 #include "dfsu_access_token_helper.h"
29 #include "directory_ex.h"
30 #include "ipc_skeleton.h"
31 #include "ipc/download_asset_callback_manager.h"
32 #include "meta_file.h"
33 #include "mem_mgr_client.h"
34 #include "net_conn_callback_observer.h"
35 #include "network_status.h"
36 #include "parameters.h"
37 #include "periodic_check_task.h"
38 #include "plugin_loader.h"
39 #include "sandbox_helper.h"
40 #include "screen_status.h"
41 #include "session_manager.h"
42 #include "settings_data_manager.h"
43 #include "system_ability_definition.h"
44 #include "system_load.h"
45 #include "task_state_manager.h"
46 #include "utils_log.h"
47
48 namespace OHOS::FileManagement::CloudSync {
49 using namespace std;
50 using namespace OHOS;
51 using namespace CloudFile;
52 constexpr int32_t MIN_USER_ID = 100;
53 constexpr int LOAD_SA_TIMEOUT_MS = 4000;
54 const std::string CLOUDDRIVE_KEY = "persist.kernel.bundle_name.clouddrive";
55 REGISTER_SYSTEM_ABILITY_BY_ID(CloudSyncService, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, false);
56
CloudSyncService(int32_t saID,bool runOnCreate)57 CloudSyncService::CloudSyncService(int32_t saID, bool runOnCreate) : SystemAbility(saID, runOnCreate)
58 {
59 }
60
PublishSA()61 void CloudSyncService::PublishSA()
62 {
63 LOGI("Begin to init");
64 if (!SystemAbility::Publish(this)) {
65 throw runtime_error("Failed to publish the daemon");
66 }
67 LOGI("Init finished successfully");
68 }
69
PreInit()70 void CloudSyncService::PreInit()
71 {
72 /* load cloud file ext plugin */
73 CloudFile::PluginLoader::GetInstance().LoadCloudKitPlugin(true);
74 auto instance = CloudFile::CloudFileKit::GetInstance();
75 if (instance == nullptr) {
76 LOGE("get cloud file helper instance failed");
77 dataSyncManager_ = make_shared<DataSyncManager>();
78 } else {
79 dataSyncManager_ = instance->GetDataSyncManager();
80 }
81
82 batteryStatusListener_ = make_shared<BatteryStatusListener>(dataSyncManager_);
83 screenStatusListener_ = make_shared<ScreenStatusListener>(dataSyncManager_);
84 userStatusListener_ = make_shared<UserStatusListener>(dataSyncManager_);
85 packageStatusListener_ = make_shared<PackageStatusListener>(dataSyncManager_);
86 }
87
Init()88 void CloudSyncService::Init()
89 {
90 /* Get Init Charging status */
91 BatteryStatus::GetInitChargingStatus();
92 ScreenStatus::InitScreenStatus();
93 }
94
95 constexpr int TEST_MAIN_USR_ID = 100;
GetBundleNameUserInfo(BundleNameUserInfo & bundleNameUserInfo)96 int32_t CloudSyncService::GetBundleNameUserInfo(BundleNameUserInfo &bundleNameUserInfo)
97 {
98 string bundleName;
99 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
100 return E_INVAL_ARG;
101 }
102 bundleNameUserInfo.bundleName = bundleName;
103
104 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
105 if (callerUserId == 0) {
106 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
107 }
108 bundleNameUserInfo.userId = callerUserId;
109
110 auto callerPid = DfsuAccessTokenHelper::GetPid();
111 bundleNameUserInfo.pid = callerPid;
112
113 return E_OK;
114 }
115
CovertBundleName(std::string & bundleName)116 void CloudSyncService::CovertBundleName(std::string &bundleName)
117 {
118 auto clouddriveBundleName = system::GetParameter(CLOUDDRIVE_KEY, "");
119 if (bundleName == clouddriveBundleName) {
120 bundleName = GALLERY_BUNDLE_NAME;
121 }
122 }
123
GetBundleNameUserInfo(const std::vector<std::string> & uriVec,BundleNameUserInfo & bundleNameUserInfo)124 void CloudSyncService::GetBundleNameUserInfo(const std::vector<std::string> &uriVec,
125 BundleNameUserInfo &bundleNameUserInfo)
126 {
127 Uri uri(uriVec[0]);
128 string bundleName = uri.GetAuthority();
129 bundleNameUserInfo.bundleName = bundleName;
130
131 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
132 if (callerUserId == 0) {
133 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
134 }
135 bundleNameUserInfo.userId = callerUserId;
136
137 auto callerPid = DfsuAccessTokenHelper::GetPid();
138 bundleNameUserInfo.pid = callerPid;
139 }
140
GetHmdfsPath(const std::string & uri,int32_t userId)141 std::string CloudSyncService::GetHmdfsPath(const std::string &uri, int32_t userId)
142 {
143 const std::string HMDFS_DIR = "/mnt/hmdfs/";
144 const std::string DATA_DIR = "/account/device_view/local/data/";
145 const std::string FILE_DIR = "data/storage/el2/distributedfiles/";
146 const std::string URI_PREFIX = "://";
147 if (uri.empty() || uri.find("/../") != std::string::npos) {
148 return "";
149 }
150
151 std::string bundleName;
152 size_t uriPrefixPos = uri.find(URI_PREFIX);
153 if (uriPrefixPos == std::string::npos) {
154 return "";
155 }
156 uriPrefixPos += URI_PREFIX.length();
157 size_t bundleNameEndPos = uri.find('/', uriPrefixPos);
158 if (bundleNameEndPos == std::string::npos) {
159 return "";
160 }
161 bundleName = uri.substr(uriPrefixPos, bundleNameEndPos - uriPrefixPos);
162
163 std::string relativePath;
164 size_t fileDirPos = uri.find(FILE_DIR);
165 if (fileDirPos == std::string::npos) {
166 return "";
167 }
168 fileDirPos += FILE_DIR.length();
169 relativePath = uri.substr(fileDirPos);
170
171 std::string outputPath = HMDFS_DIR + std::to_string(userId) + DATA_DIR + bundleName + "/" + relativePath;
172 std::string dir = outputPath.substr(0, outputPath.find_last_of('/'));
173
174 ForceCreateDirectory(dir);
175 return outputPath;
176 }
177
OnStart(const SystemAbilityOnDemandReason & startReason)178 void CloudSyncService::OnStart(const SystemAbilityOnDemandReason& startReason)
179 {
180 PreInit();
181 try {
182 PublishSA();
183 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
184 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
185 AddSystemAbilityListener(RES_SCHED_SYS_ABILITY_ID);
186 AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
187 AddSystemAbilityListener(MEMORY_MANAGER_SA_ID);
188 } catch (const exception &e) {
189 LOGE("%{public}s", e.what());
190 }
191 LOGI("Start service successfully");
192 Init();
193 LOGI("init service successfully");
194 system::SetParameter(CLOUD_FILE_SERVICE_SA_STATUS_FLAG, CLOUD_FILE_SERVICE_SA_START);
195 TaskStateManager::GetInstance().StartTask();
196 // 跟随进程生命周期
197 ffrt::submit([startReason, this]() {
198 SettingsDataManager::InitSettingsDataManager();
199 this->HandleStartReason(startReason);
200 int32_t userId = 0;
201 if (dataSyncManager_->GetUserId(userId) != E_OK) {
202 return;
203 }
204 string oldPath = "/data/service/el2/" + to_string(userId) + "/hmdfs/cache/cloud_cache/pread_cache";
205 if (access(oldPath.c_str(), F_OK) == 0) {
206 if (!ForceRemoveDirectory(oldPath)) {
207 LOGE("rm old video cache path fail, err: %{public}d", errno);
208 }
209 }
210 });
211 }
212
OnActive(const SystemAbilityOnDemandReason & startReason)213 void CloudSyncService::OnActive(const SystemAbilityOnDemandReason& startReason)
214 {
215 LOGI("active service successfully");
216 system::SetParameter(CLOUD_FILE_SERVICE_SA_STATUS_FLAG, CLOUD_FILE_SERVICE_SA_START);
217 TaskStateManager::GetInstance().StartTask();
218 ffrt::submit([startReason, this]() {
219 this->HandleStartReason(startReason);
220 });
221 }
222
OnStop()223 void CloudSyncService::OnStop()
224 {
225 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(), 1, 0, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID);
226 LOGI("Stop finished successfully");
227 }
228
HandleStartReason(const SystemAbilityOnDemandReason & startReason)229 void CloudSyncService::HandleStartReason(const SystemAbilityOnDemandReason& startReason)
230 {
231 string reason = startReason.GetName();
232 int32_t userId = 0;
233
234 LOGI("Begin to start service reason: %{public}s", reason.c_str());
235
236 if (reason == "usual.event.USER_UNLOCKED") {
237 return;
238 }
239
240 if (dataSyncManager_->GetUserId(userId) != E_OK) {
241 return;
242 }
243
244 if (reason == "usual.event.wifi.CONN_STATE") {
245 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::NETWORK_AVAIL_TRIGGER);
246 dataSyncManager_->DownloadThumb();
247 dataSyncManager_->CacheVideo();
248 } else if (reason == "usual.event.BATTERY_OKAY") {
249 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::BATTERY_OK_TRIGGER);
250 dataSyncManager_->DownloadThumb();
251 } else if (reason == "usual.event.SCREEN_OFF") {
252 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::SCREEN_OFF_TRIGGER);
253 dataSyncManager_->DownloadThumb();
254 dataSyncManager_->CacheVideo();
255 } else if (reason == "usual.event.POWER_CONNECTED") {
256 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::POWER_CONNECT_TRIGGER);
257 dataSyncManager_->DownloadThumb();
258 dataSyncManager_->CacheVideo();
259 } else if (reason == "usual.event.PACKAGE_REMOVED") {
260 HandlePackageRemoved(startReason);
261 }
262
263 if (reason != "load") {
264 shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(dataSyncManager_);
265 taskRunner->StartTask();
266 }
267 }
268
HandlePackageRemoved(const SystemAbilityOnDemandReason & startReason)269 void CloudSyncService::HandlePackageRemoved(const SystemAbilityOnDemandReason& startReason)
270 {
271 std::string bundleName;
272 std::string userId;
273 auto extraData = startReason.GetExtraData().GetWant();
274 auto iter = extraData.find("bundleName");
275 if (iter != extraData.end()) {
276 bundleName = iter->second;
277 } else {
278 LOGE("Cant find bundleName");
279 return;
280 }
281 iter = extraData.find("userId");
282 if (iter != extraData.end()) {
283 userId = iter->second;
284 } else {
285 LOGE("Cant find userId");
286 return;
287 }
288 int32_t userIdNum = std::atoi(userId.c_str());
289 if (userIdNum < 0 || (userIdNum == 0 && userId != "0")) {
290 LOGE("Get UserId Failed!");
291 return;
292 }
293 packageStatusListener_->RemovedClean(bundleName, userIdNum);
294 }
295
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)296 void CloudSyncService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
297 {
298 LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
299 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
300 userStatusListener_->Start();
301 batteryStatusListener_->Start();
302 screenStatusListener_->Start();
303 packageStatusListener_->Start();
304 } else if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
305 auto sessionManager = make_shared<SessionManager>();
306 sessionManager->Init();
307 userStatusListener_->AddObserver(sessionManager);
308 fileTransferManager_ = make_shared<FileTransferManager>(sessionManager);
309 fileTransferManager_->Init();
310 } else if (systemAbilityId == RES_SCHED_SYS_ABILITY_ID) {
311 SystemLoadStatus::InitSystemload(dataSyncManager_);
312 } else if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
313 NetworkStatus::InitNetwork(dataSyncManager_);
314 } else if (systemAbilityId == MEMORY_MANAGER_SA_ID) {
315 pid_t pid = getpid();
316 Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 1, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID);
317 Memory::MemMgrClient::GetInstance().SetCritical(pid, true, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID);
318 } else {
319 LOGE("unexpected");
320 }
321 }
322
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)323 void CloudSyncService::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
324 {
325 LOGD("set death recipient");
326 auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
327 unique_lock<mutex> lock(loadRemoteSAMutex_);
328 for (auto it = remoteObjectMap_.begin(); it != remoteObjectMap_.end();) {
329 if (it->second.GetRefPtr() == obj.GetRefPtr()) {
330 it = remoteObjectMap_.erase(it);
331 LOGD("remote sa died");
332 } else {
333 ++it;
334 }
335 }
336 };
337 deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
338 remoteObject->AddDeathRecipient(deathRecipient_);
339 }
340
LoadRemoteSA(const std::string & deviceId)341 int32_t CloudSyncService::LoadRemoteSA(const std::string &deviceId)
342 {
343 LOGI("Load remote CloudSync SA start");
344 if (deviceId.empty()) {
345 LOGE("Failed to load remote SA, deviceId is empty");
346 return E_SA_LOAD_FAILED;
347 }
348 unique_lock<mutex> lock(loadRemoteSAMutex_);
349 auto iter = remoteObjectMap_.find(deviceId);
350 if (iter != remoteObjectMap_.end() && iter->second != nullptr) {
351 return E_OK;
352 }
353
354 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
355 if (samgr == nullptr) {
356 LOGE("Samgr is nullptr");
357 return E_SA_LOAD_FAILED;
358 }
359 auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, deviceId);
360 if (object == nullptr) {
361 LOGE("Failed to Load systemAbility, object is nullptr");
362 return E_SA_LOAD_FAILED;
363 }
364 remoteObjectMap_[deviceId] = object;
365 SetDeathRecipient(remoteObjectMap_[deviceId]);
366 LOGI("Load remote CloudSync SA end");
367 return E_OK;
368 }
369
GetTargetBundleName(string & targetBundleName,string & callerBundleName)370 static int32_t GetTargetBundleName(string &targetBundleName, string &callerBundleName)
371 {
372 if (DfsuAccessTokenHelper::GetCallerBundleName(callerBundleName)) {
373 return E_INVAL_ARG;
374 }
375 if (targetBundleName == "") {
376 targetBundleName = callerBundleName;
377 }
378 if (targetBundleName != callerBundleName &&
379 !DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
380 LOGE("permission denied: cloudfile_sync_manager");
381 return E_PERMISSION_DENIED;
382 }
383 return E_OK;
384 }
385
CheckPermissions(const string & permission,bool isSystemApp)386 static int32_t CheckPermissions(const string &permission, bool isSystemApp)
387 {
388 if (!permission.empty() && !DfsuAccessTokenHelper::CheckCallerPermission(permission)) {
389 LOGE("permission denied");
390 return E_PERMISSION_DENIED;
391 }
392 if (isSystemApp && !DfsuAccessTokenHelper::IsSystemApp()) {
393 LOGE("caller hap is not system hap");
394 return E_PERMISSION_SYSTEM;
395 }
396 return E_OK;
397 }
398
UnRegisterCallbackInner(const string & bundleName)399 int32_t CloudSyncService::UnRegisterCallbackInner(const string &bundleName)
400 {
401 LOGI("Begin UnRegisterCallbackInner");
402 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
403
404 string targetBundleName = bundleName;
405 string callerBundleName = "";
406 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
407 if (ret != E_OK) {
408 LOGE("get bundle name failed: %{public}d", ret);
409 return ret;
410 }
411 dataSyncManager_->UnRegisterCloudSyncCallback(targetBundleName, callerBundleName);
412 LOGI("End UnRegisterCallbackInner");
413 return E_OK;
414 }
415
UnRegisterFileSyncCallbackInner(const string & bundleName)416 int32_t CloudSyncService::UnRegisterFileSyncCallbackInner(const string &bundleName)
417 {
418 LOGI("Begin UnRegisterFileSyncCallbackInner");
419 string targetBundleName = bundleName;
420 string callerBundleName = "";
421 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
422 if (ret != E_OK) {
423 LOGE("get bundle name failed: %{public}d", ret);
424 return ret;
425 }
426 dataSyncManager_->UnRegisterCloudSyncCallback(targetBundleName, callerBundleName);
427 LOGI("End UnRegisterFileSyncCallbackInner");
428 return E_OK;
429 }
430
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject,const string & bundleName)431 int32_t CloudSyncService::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject, const string &bundleName)
432 {
433 LOGI("Begin RegisterCallbackInner");
434 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
435
436 if (remoteObject == nullptr) {
437 LOGE("remoteObject is nullptr");
438 return E_INVAL_ARG;
439 }
440
441 string targetBundleName = bundleName;
442 string callerBundleName = "";
443 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
444 if (ret != E_OK) {
445 LOGE("get bundle name failed: %{public}d", ret);
446 return ret;
447 }
448
449 auto callback = iface_cast<ICloudSyncCallback>(remoteObject);
450 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
451 dataSyncManager_->RegisterCloudSyncCallback(targetBundleName, callerBundleName, callerUserId, callback);
452 LOGI("End RegisterCallbackInner");
453 return E_OK;
454 }
455
RegisterFileSyncCallbackInner(const sptr<IRemoteObject> & remoteObject,const string & bundleName)456 int32_t CloudSyncService::RegisterFileSyncCallbackInner(const sptr<IRemoteObject> &remoteObject,
457 const string &bundleName)
458 {
459 LOGI("Begin RegisterFileSyncCallbackInner");
460 if (remoteObject == nullptr) {
461 LOGE("remoteObject is nullptr");
462 return E_INVAL_ARG;
463 }
464
465 string targetBundleName = bundleName;
466 string callerBundleName = "";
467 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
468 if (ret != E_OK) {
469 LOGE("get bundle name failed: %{public}d", ret);
470 return ret;
471 }
472
473 auto callback = iface_cast<ICloudSyncCallback>(remoteObject);
474 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
475 dataSyncManager_->RegisterCloudSyncCallback(targetBundleName, callerBundleName, callerUserId, callback);
476 LOGI("End RegisterFileSyncCallbackInner");
477 return E_OK;
478 }
479
StartSyncInner(bool forceFlag,const string & bundleName)480 int32_t CloudSyncService::StartSyncInner(bool forceFlag, const string &bundleName)
481 {
482 LOGI("Begin StartSyncInner");
483 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
484
485 string targetBundleName = bundleName;
486 string callerBundleName = "";
487 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
488 if (ret != E_OK) {
489 LOGE("get bundle name failed: %{public}d", ret);
490 return ret;
491 }
492 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
493 ret = dataSyncManager_->TriggerStartSync(targetBundleName, callerUserId, forceFlag, SyncTriggerType::APP_TRIGGER);
494 LOGI("End StartSyncInner");
495 return ret;
496 }
497
StartFileSyncInner(bool forceFlag,const string & bundleName)498 int32_t CloudSyncService::StartFileSyncInner(bool forceFlag, const string &bundleName)
499 {
500 LOGI("Begin StartFileSyncInner");
501 string targetBundleName = bundleName;
502 string callerBundleName = "";
503 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
504 if (ret != E_OK) {
505 LOGE("get bundle name failed: %{public}d", ret);
506 return ret;
507 }
508 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
509 ret = dataSyncManager_->TriggerStartSync(targetBundleName, callerUserId, forceFlag, SyncTriggerType::APP_TRIGGER);
510 LOGI("End StartFileSyncInner");
511 return ret;
512 }
513
TriggerSyncInner(const std::string & bundleName,int32_t userId)514 int32_t CloudSyncService::TriggerSyncInner(const std::string &bundleName, int32_t userId)
515 {
516 LOGI("Begin TriggerSyncInner");
517 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
518
519 if (bundleName.empty() || userId < MIN_USER_ID) {
520 LOGE("Trigger sync parameter is invalid");
521 return E_INVAL_ARG;
522 }
523 int32_t ret = dataSyncManager_->TriggerStartSync(bundleName, userId, false, SyncTriggerType::APP_TRIGGER);
524 LOGI("End StartSyncInner");
525 return ret;
526 }
527
StopSyncInner(const string & bundleName,bool forceFlag)528 int32_t CloudSyncService::StopSyncInner(const string &bundleName, bool forceFlag)
529 {
530 LOGI("Begin StopSyncInner");
531 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
532
533 string targetBundleName = bundleName;
534 string callerBundleName = "";
535 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
536 if (ret != E_OK) {
537 LOGE("get bundle name failed: %{public}d", ret);
538 return ret;
539 }
540 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
541 ret = dataSyncManager_->TriggerStopSync(targetBundleName, callerUserId, forceFlag, SyncTriggerType::APP_TRIGGER);
542 LOGI("End StopSyncInner");
543 return ret;
544 }
545
StopFileSyncInner(const string & bundleName,bool forceFlag)546 int32_t CloudSyncService::StopFileSyncInner(const string &bundleName, bool forceFlag)
547 {
548 LOGI("Begin StopFileSyncInner");
549 string targetBundleName = bundleName;
550 string callerBundleName = "";
551 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
552 if (ret != E_OK) {
553 LOGE("get bundle name failed: %{public}d", ret);
554 return ret;
555 }
556 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
557 ret = dataSyncManager_->TriggerStopSync(targetBundleName, callerUserId, forceFlag, SyncTriggerType::APP_TRIGGER);
558 LOGI("End StopFileSyncInner");
559 return ret;
560 }
561
ResetCursor(const string & bundleName)562 int32_t CloudSyncService::ResetCursor(const string &bundleName)
563 {
564 LOGI("Begin ResetCursor");
565 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
566
567 string targetBundleName = bundleName;
568 string callerBundleName = "";
569 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
570 if (ret != E_OK) {
571 LOGE("get bundle name failed: %{public}d", ret);
572 return ret;
573 }
574 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
575 ret = dataSyncManager_->ResetCursor(targetBundleName, callerUserId);
576 LOGI("End ResetCursor");
577 return ret;
578 }
579
GetSyncTimeInner(int64_t & syncTime,const string & bundleName)580 int32_t CloudSyncService::GetSyncTimeInner(int64_t &syncTime, const string &bundleName)
581 {
582 LOGI("Begin GetSyncTimeInner");
583 string targetBundleName = bundleName;
584 string callerBundleName = "";
585 int32_t ret = GetTargetBundleName(targetBundleName, callerBundleName);
586 if (ret != E_OK) {
587 LOGE("get bundle name failed: %{public}d", ret);
588 return ret;
589 }
590 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
591 ret = DataSyncerRdbStore::GetInstance().GetLastSyncTime(callerUserId, targetBundleName, syncTime);
592 LOGI("End GetSyncTimeInner");
593 return ret;
594 }
595
BatchDentryFileInsert(const std::vector<DentryFileInfoObj> & fileInfo,std::vector<std::string> & failCloudId)596 int32_t CloudSyncService::BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
597 std::vector<std::string> &failCloudId)
598 {
599 LOGI("Begin BatchDentryFileInsert");
600 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
601
602 std::vector<DentryFileInfo> dentryFileInfo;
603 for (const auto &obj : fileInfo) {
604 DentryFileInfo tmpFileInfo{obj.cloudId, obj.size, obj.modifiedTime, obj.path, obj.fileName, obj.fileType};
605 dentryFileInfo.emplace_back(tmpFileInfo);
606 }
607
608 int32_t ret = dataSyncManager_->BatchDentryFileInsert(dentryFileInfo, failCloudId);
609 LOGI("End BatchDentryFileInsert");
610 return ret;
611 }
612
CleanCacheInner(const std::string & uri)613 int32_t CloudSyncService::CleanCacheInner(const std::string &uri)
614 {
615 LOGI("Begin CleanCacheInner");
616 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
617
618 string bundleName;
619 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
620 return E_INVAL_ARG;
621 }
622 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
623 int32_t ret = dataSyncManager_->CleanCache(bundleName, callerUserId, uri);
624 LOGI("End CleanCacheInner");
625 return E_OK;
626 }
627
CleanFileCacheInner(const std::string & uri)628 int32_t CloudSyncService::CleanFileCacheInner(const std::string &uri)
629 {
630 LOGI("Begin CleanFileCacheInner");
631
632 string bundleName;
633 if (!DfsuAccessTokenHelper::CheckUriPermission(uri)) {
634 LOGE("Not support uri");
635 return E_ILLEGAL_URI;
636 }
637 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
638 return E_SERVICE_INNER_ERROR;
639 }
640 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
641 int32_t ret = dataSyncManager_->CleanCache(bundleName, callerUserId, uri);
642 LOGI("End CleanFileCacheInner");
643 return ret;
644 }
645
OptimizeStorage(const OptimizeSpaceOptions & optimizeOptions,bool isCallbackValid,const sptr<IRemoteObject> & optimizeCallback)646 int32_t CloudSyncService::OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
647 bool isCallbackValid,
648 const sptr<IRemoteObject> &optimizeCallback)
649 {
650 LOGI("Begin OptimizeStorage");
651 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
652
653 BundleNameUserInfo bundleNameUserInfo;
654 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
655 if (ret != E_OK) {
656 return ret;
657 }
658
659 CovertBundleName(bundleNameUserInfo.bundleName);
660 LOGI("OptimizeStorage, bundleName: %{private}s, agingDays: %{public}d, callerUserId: %{public}d",
661 bundleNameUserInfo.bundleName.c_str(), optimizeOptions.agingDays, bundleNameUserInfo.userId);
662 if (!isCallbackValid) {
663 return dataSyncManager_->OptimizeStorage(bundleNameUserInfo.bundleName, bundleNameUserInfo.userId,
664 optimizeOptions.agingDays);
665 }
666
667 auto optimizeCb = iface_cast<ICloudOptimizeCallback>(optimizeCallback);
668 ret = dataSyncManager_->StartOptimizeStorage(bundleNameUserInfo, optimizeOptions, optimizeCb);
669 LOGI("End OptimizeStorage");
670 return ret;
671 }
672
StopOptimizeStorage()673 int32_t CloudSyncService::StopOptimizeStorage()
674 {
675 LOGI("Begin StopOptimizeStorage");
676 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
677
678 BundleNameUserInfo bundleNameUserInfo;
679 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
680 if (ret != E_OK) {
681 return ret;
682 }
683 CovertBundleName(bundleNameUserInfo.bundleName);
684 ret = dataSyncManager_->StopOptimizeStorage(bundleNameUserInfo);
685 LOGI("End StopOptimizeStorage");
686 return ret;
687 }
688
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)689 int32_t CloudSyncService::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
690 {
691 LOGI("Begin ChangeAppSwitch");
692 RETURN_ON_ERR(CheckPermissions("", true));
693
694 int32_t callerUserId = DfsuAccessTokenHelper::GetUserId();
695 /* SA is 0 */
696 if (callerUserId == 0) {
697 DfsuAccessTokenHelper::GetAccountId(callerUserId);
698 }
699 LOGI("ChangeAppSwitch, bundleName: %{private}s, status: %{public}d, callerUserId: %{public}d", bundleName.c_str(),
700 status, callerUserId);
701
702 /* update app switch status */
703 int32_t ret = CloudStatus::ChangeAppSwitch(bundleName, callerUserId, status);
704 if (ret != E_OK) {
705 LOGE("CloudStatus::ChangeAppSwitch failed, ret: %{public}d", ret);
706 return ret;
707 }
708 if (status) {
709 ret = dataSyncManager_->TriggerStartSync(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER);
710 if (ret != E_OK) {
711 LOGE("dataSyncManager Trigger failed, status: %{public}d", status);
712 return ret;
713 }
714 } else {
715 system::SetParameter(CLOUDSYNC_STATUS_KEY, CLOUDSYNC_STATUS_SWITCHOFF);
716 ret = dataSyncManager_->StopSyncSynced(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER);
717 if (ret != E_OK) {
718 LOGE("StopSyncSynced failed, ret: %{public}d", ret);
719 }
720 }
721
722 ret = dataSyncManager_->ChangeAppSwitch(bundleName, callerUserId, status);
723 LOGI("End ChangeAppSwitch");
724 return ret;
725 }
726
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)727 int32_t CloudSyncService::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
728 {
729 LOGI("Begin NotifyDataChange");
730 RETURN_ON_ERR(CheckPermissions("", true));
731
732 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
733 int32_t ret = dataSyncManager_->TriggerStartSync(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER);
734 LOGI("End NotifyDataChange");
735 return E_OK;
736 }
737
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)738 int32_t CloudSyncService::NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData)
739 {
740 LOGI("Begin NotifyEventChange");
741 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
742
743 auto instance = CloudFile::CloudFileKit::GetInstance();
744 if (instance == nullptr) {
745 LOGE("get cloud file helper instance failed");
746 return E_NULLPTR;
747 }
748
749 string appBundleName;
750 string prepareTraceId;
751 auto ret = instance->ResolveNotificationEvent(userId, extraData, appBundleName, prepareTraceId);
752 if (ret != E_OK) {
753 LOGE("ResolveNotificationEvent failed, ret:%{public}d", ret);
754 return E_CLOUD_SDK;
755 }
756
757 std::thread([this, appBundleName, userId, prepareTraceId]() {
758 dataSyncManager_->TriggerStartSync(appBundleName, userId, false, SyncTriggerType::CLOUD_TRIGGER,
759 prepareTraceId);
760 }).detach();
761 LOGI("End NotifyEventChange");
762 return E_OK;
763 }
764
DisableCloud(const std::string & accoutId)765 int32_t CloudSyncService::DisableCloud(const std::string &accoutId)
766 {
767 LOGI("Begin DisableCloud");
768 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
769
770 int32_t callerUserId = DfsuAccessTokenHelper::GetUserId();
771 /* SA is 0 */
772 if (callerUserId == 0) {
773 DfsuAccessTokenHelper::GetAccountId(callerUserId);
774 }
775 int32_t ret = dataSyncManager_->DisableCloud(callerUserId);
776 LOGI("End DisableCloud, ret: %{public}d", ret);
777 return ret;
778 }
779
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)780 int32_t CloudSyncService::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
781 {
782 LOGI("Begin EnableCloud");
783 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
784 LOGI("End EnableCloud");
785 return E_OK;
786 }
787
Clean(const std::string & accountId,const CleanOptions & cleanOptions)788 int32_t CloudSyncService::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
789 {
790 LOGI("Begin Clean");
791 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
792
793 for (auto &iter : cleanOptions.appActionsData) {
794 LOGD("Clean key is: %s, value is: %d", iter.first.c_str(), iter.second);
795 }
796
797 MetaFileMgr::GetInstance().ClearAll();
798 MetaFileMgr::GetInstance().CloudDiskClearAll();
799 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
800 LOGI("Clean callerUserId is: %{public}d", callerUserId);
801 for (auto iter = cleanOptions.appActionsData.begin(); iter != cleanOptions.appActionsData.end(); ++iter) {
802 dataSyncManager_->CleanCloudFile(callerUserId, iter->first, iter->second);
803 }
804 LOGI("End Clean");
805 return E_OK;
806 }
807
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,int32_t fieldkey,const sptr<IRemoteObject> & downloadCallback,int32_t timeout)808 int32_t CloudSyncService::StartFileCache(const std::vector<std::string> &uriVec,
809 int64_t &downloadId,
810 int32_t fieldkey,
811 const sptr<IRemoteObject> &downloadCallback,
812 int32_t timeout)
813 {
814 LOGI("Begin StartFileCache");
815 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_AUTH_URI)) {
816 for (auto &uri : uriVec) {
817 if (!DfsuAccessTokenHelper::CheckUriPermission(uri)) {
818 LOGE("permission denied");
819 return E_ILLEGAL_URI;
820 }
821 }
822 }
823 BundleNameUserInfo bundleNameUserInfo;
824 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
825 if (ret != E_OK) {
826 LOGE("GetBundleNameUserInfo failed.");
827 return ret;
828 }
829
830 sptr<ICloudDownloadCallback> downloadCb = iface_cast<ICloudDownloadCallback>(downloadCallback);
831 if (downloadCb == nullptr) {
832 LOGE("Invalid downloadCallback, not a valid ICloudDownloadCallback.");
833 // Common error code for single and batch download task.
834 return E_BROKEN_IPC;
835 }
836 ret = dataSyncManager_->StartDownloadFile(bundleNameUserInfo, uriVec, downloadId, fieldkey, downloadCb, timeout);
837 LOGI("End StartFileCache, ret: %{public}d", ret);
838 return ret;
839 }
840
StartDownloadFile(const std::string & uri,const sptr<IRemoteObject> & downloadCallback,int64_t & downloadId)841 int32_t CloudSyncService::StartDownloadFile(const std::string &uri,
842 const sptr<IRemoteObject> &downloadCallback,
843 int64_t &downloadId)
844 {
845 LOGI("Begin StartDownloadFile");
846 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
847
848 BundleNameUserInfo bundleNameUserInfo;
849 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
850 if (ret != E_OK) {
851 return ret;
852 }
853 sptr<ICloudDownloadCallback> downloadCb = iface_cast<ICloudDownloadCallback>(downloadCallback);
854 if (downloadCb == nullptr) {
855 LOGE("Invalid downloadCallback, not a valid ICloudDownloadCallback.");
856 return E_INVAL_ARG;
857 }
858 ret = dataSyncManager_->StartDownloadFile(bundleNameUserInfo, {uri}, downloadId, FieldKey::FIELDKEY_CONTENT,
859 downloadCb);
860 LOGI("End StartDownloadFile");
861 return ret;
862 }
863
StopDownloadFile(int64_t downloadId,bool needClean)864 int32_t CloudSyncService::StopDownloadFile(int64_t downloadId, bool needClean)
865 {
866 LOGI("Begin StopDownloadFile");
867 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
868
869 BundleNameUserInfo bundleNameUserInfo;
870 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
871 if (ret != E_OK) {
872 return ret;
873 }
874
875 ret = dataSyncManager_->StopDownloadFile(bundleNameUserInfo, downloadId, needClean);
876 LOGI("End StopDownloadFile");
877 return ret;
878 }
879
StopFileCache(int64_t downloadId,bool needClean,int32_t timeout)880 int32_t CloudSyncService::StopFileCache(int64_t downloadId, bool needClean, int32_t timeout)
881 {
882 LOGI("Begin StopFileCache");
883 RETURN_ON_ERR(CheckPermissions(PERM_AUTH_URI, false));
884
885 BundleNameUserInfo bundleNameUserInfo;
886 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
887 if (ret != E_OK) {
888 return ret;
889 }
890
891 ret = dataSyncManager_->StopDownloadFile(bundleNameUserInfo, downloadId, needClean, timeout);
892 LOGI("End StopFileCache, ret: %{public}d", ret);
893 return ret;
894 }
895
DownloadThumb()896 int32_t CloudSyncService::DownloadThumb()
897 {
898 LOGI("Begin DownloadThumb");
899 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
900
901 int32_t ret = dataSyncManager_->TriggerDownloadThumb();
902 LOGI("End DownloadThumb");
903 return ret;
904 }
905
UploadAsset(const int32_t userId,const std::string & request,std::string & result)906 int32_t CloudSyncService::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
907 {
908 LOGI("Begin UploadAsset");
909 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
910
911 auto instance = CloudFile::CloudFileKit::GetInstance();
912 if (instance == nullptr) {
913 LOGE("get cloud file helper instance failed");
914 return E_NULLPTR;
915 }
916
917 string bundleName("distributeddata");
918 TaskStateManager::GetInstance().StartTask(bundleName, TaskType::UPLOAD_ASSET_TASK);
919 auto ret = instance->OnUploadAsset(userId, request, result);
920 TaskStateManager::GetInstance().CompleteTask(bundleName, TaskType::UPLOAD_ASSET_TASK);
921 LOGI("End UploadAsset");
922 return ret;
923 }
924
DownloadFile(const int32_t userId,const std::string & bundleName,const AssetInfoObj & assetInfoObj)925 int32_t CloudSyncService::DownloadFile(const int32_t userId,
926 const std::string &bundleName,
927 const AssetInfoObj &assetInfoObj)
928 {
929 LOGI("Begin DownloadFile");
930 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
931
932 auto instance = CloudFile::CloudFileKit::GetInstance();
933 if (instance == nullptr) {
934 LOGE("get cloud file helper instance failed");
935 return E_NULLPTR;
936 }
937
938 auto assetsDownloader = instance->GetCloudAssetsDownloader(userId, bundleName);
939 if (assetsDownloader == nullptr) {
940 LOGE("get asset downloader failed");
941 return E_NULLPTR;
942 }
943
944 Asset asset;
945 asset.assetName = assetInfoObj.assetName;
946
947 asset.uri = GetHmdfsPath(assetInfoObj.uri, userId);
948 if (asset.uri.empty()) {
949 LOGE("fail to get download path from %{public}s", GetAnonyString(assetInfoObj.uri).c_str());
950 return E_INVAL_ARG;
951 }
952
953 // Not to pass the assetinfo.fieldkey
954 DownloadAssetInfo assetsToDownload{assetInfoObj.recordType, assetInfoObj.recordId, {}, asset, {}};
955 TaskStateManager::GetInstance().StartTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
956 auto ret = assetsDownloader->DownloadAssets(assetsToDownload);
957 TaskStateManager::GetInstance().CompleteTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
958 LOGI("End DownloadFile");
959 return ret;
960 }
961
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap,int32_t connectTime)962 int32_t CloudSyncService::DownloadFiles(const int32_t userId,
963 const std::string &bundleName,
964 const std::vector<AssetInfoObj> &assetInfoObj,
965 std::vector<bool> &assetResultMap,
966 int32_t connectTime)
967 {
968 LOGI("Begin DownloadFiles");
969 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
970
971 auto instance = CloudFile::CloudFileKit::GetInstance();
972 if (instance == nullptr) {
973 LOGE("get cloud file helper instance failed");
974 return E_NULLPTR;
975 }
976
977 auto assetsDownloader = instance->GetCloudAssetsDownloader(userId, bundleName);
978 if (assetsDownloader == nullptr) {
979 LOGE("get asset downloader failed");
980 return E_NULLPTR;
981 }
982
983 std::vector<DownloadAssetInfo> assetsToDownload;
984 for (const auto &obj : assetInfoObj) {
985 Asset asset;
986 asset.assetName = obj.assetName;
987 asset.uri = GetHmdfsPath(obj.uri, userId);
988 if (asset.uri.empty()) {
989 LOGE("fail to get download path from %{private}s", GetAnonyString(obj.uri).c_str());
990 return E_INVAL_ARG;
991 }
992 DownloadAssetInfo assetToDownload{obj.recordType, obj.recordId, {}, asset, {}};
993 assetsToDownload.emplace_back(assetToDownload);
994 }
995
996 TaskStateManager::GetInstance().StartTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
997 auto ret = assetsDownloader->DownloadAssets(assetsToDownload, assetResultMap, connectTime);
998 TaskStateManager::GetInstance().CompleteTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
999 LOGI("End DownloadFiles");
1000 return ret;
1001 }
1002
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,const AssetInfoObj & assetInfoObj)1003 int32_t CloudSyncService::DownloadAsset(const uint64_t taskId,
1004 const int32_t userId,
1005 const std::string &bundleName,
1006 const std::string &networkId,
1007 const AssetInfoObj &assetInfoObj)
1008 {
1009 LOGI("Begin DownloadAsset");
1010 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
1011
1012 if (networkId == "edge2cloud") {
1013 LOGE("now not support");
1014 return E_INVAL_ARG;
1015 }
1016 // Load sa for remote device
1017 if (LoadRemoteSA(networkId) != E_OK) { // maybe need to convert deviceId
1018 return E_SA_LOAD_FAILED;
1019 }
1020
1021 string uri = assetInfoObj.uri;
1022 fileTransferManager_->DownloadFileFromRemoteDevice(networkId, userId, taskId, uri);
1023 LOGI("End DownloadAsset");
1024 return E_OK;
1025 }
1026
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)1027 int32_t CloudSyncService::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
1028 {
1029 LOGI("Begin RegisterDownloadAssetCallback");
1030 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
1031
1032 if (remoteObject == nullptr) {
1033 LOGE("remoteObject is nullptr");
1034 return E_INVAL_ARG;
1035 }
1036 auto callback = iface_cast<IDownloadAssetCallback>(remoteObject);
1037 DownloadAssetCallbackManager::GetInstance().AddCallback(callback);
1038 LOGI("End RegisterDownloadAssetCallback");
1039 return E_OK;
1040 }
1041
DeleteAsset(const int32_t userId,const std::string & uri)1042 int32_t CloudSyncService::DeleteAsset(const int32_t userId, const std::string &uri)
1043 {
1044 LOGI("Begin DeleteAsset");
1045 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
1046
1047 std::string physicalPath = "";
1048 int ret = AppFileService::SandboxHelper::GetPhysicalPath(uri, std::to_string(userId), physicalPath);
1049 if (ret != 0 || !AppFileService::SandboxHelper::IsValidPath(physicalPath)) {
1050 LOGE("Get physical path failed with %{public}d", ret);
1051 return E_GET_PHYSICAL_PATH_FAILED;
1052 }
1053
1054 LOGD("delete assert, path %{public}s", GetAnonyString(physicalPath).c_str());
1055
1056 ret = unlink(physicalPath.c_str());
1057 if (ret != 0) {
1058 LOGE("fail to delete asset, errno %{public}d", errno);
1059 return E_DELETE_FAILED;
1060 }
1061 LOGI("End DeleteAsset");
1062 return E_OK;
1063 }
1064
BatchCleanFile(const std::vector<CleanFileInfoObj> & fileInfo,std::vector<std::string> & failCloudId)1065 int32_t CloudSyncService::BatchCleanFile(const std::vector<CleanFileInfoObj> &fileInfo,
1066 std::vector<std::string> &failCloudId)
1067 {
1068 LOGI("Begin BatchCleanFile");
1069 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC, true));
1070
1071 std::vector<CleanFileInfo> cleanFilesInfo;
1072 for (const auto &obj : fileInfo) {
1073 CleanFileInfo tmpFileInfo{obj.cloudId, obj.size, obj.modifiedTime, obj.path, obj.fileName, obj.attachment};
1074 cleanFilesInfo.emplace_back(tmpFileInfo);
1075 }
1076
1077 int32_t ret = dataSyncManager_->BatchCleanFile(cleanFilesInfo, failCloudId);
1078 LOGI("End BatchCleanFile");
1079 return ret;
1080 }
1081
CallbackEnter(uint32_t code)1082 int32_t CloudSyncService::CallbackEnter(uint32_t code)
1083 {
1084 if (!IPCSkeleton::IsLocalCalling()) {
1085 LOGE("remote requeset is not allowed, cmd:%{public}u", code);
1086 return ERR_TRANSACTION_FAILED;
1087 }
1088
1089 return ERR_NONE;
1090 }
1091
CallbackExit(uint32_t code,int32_t result)1092 int32_t CloudSyncService::CallbackExit(uint32_t code, int32_t result)
1093 {
1094 return ERR_NONE;
1095 }
1096
StartDowngrade(const std::string & bundleName,const sptr<IRemoteObject> & downloadCallback)1097 int32_t CloudSyncService::StartDowngrade(const std::string &bundleName, const sptr<IRemoteObject> &downloadCallback)
1098 {
1099 LOGI("Begin StartDowngrade");
1100 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
1101
1102 sptr<IDowngradeDlCallback> downloadCb = iface_cast<IDowngradeDlCallback>(downloadCallback);
1103 int32_t ret = dataSyncManager_->StartDowngrade(bundleName, downloadCb);
1104 LOGI("End StartDowngrade");
1105 return ret;
1106 }
1107
StopDowngrade(const std::string & bundleName)1108 int32_t CloudSyncService::StopDowngrade(const std::string &bundleName)
1109 {
1110 LOGI("Begin StopDowngrade");
1111 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
1112
1113 int32_t ret = dataSyncManager_->StopDowngrade(bundleName);
1114 LOGI("End StopDowngrade");
1115 return ret;
1116 }
1117
GetCloudFileInfo(const std::string & bundleName,CloudFileInfo & cloudFileInfo)1118 int32_t CloudSyncService::GetCloudFileInfo(const std::string &bundleName, CloudFileInfo &cloudFileInfo)
1119 {
1120 LOGI("Begin GetCloudFileInfo");
1121 RETURN_ON_ERR(CheckPermissions(PERM_CLOUD_SYNC_MANAGER, true));
1122
1123 int32_t ret = dataSyncManager_->GetCloudFileInfo(bundleName, cloudFileInfo);
1124 LOGI("End GetCloudFileInfo");
1125 return ret;
1126 }
1127
GetHistoryVersionList(const std::string & uri,const int32_t versionNumLimit,std::vector<CloudSync::HistoryVersion> & historyVersionList)1128 int32_t CloudSyncService::GetHistoryVersionList(const std::string &uri, const int32_t versionNumLimit,
1129 std::vector<CloudSync::HistoryVersion> &historyVersionList)
1130 {
1131 LOGI("Begin GetHistoryVersionList");
1132
1133 BundleNameUserInfo bundleNameUserInfo;
1134 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
1135 if (ret != E_OK) {
1136 LOGE("GetBundleNameUserInfo failed.");
1137 return ret;
1138 }
1139
1140 ret = dataSyncManager_->GetHistoryVersionList(bundleNameUserInfo, uri, versionNumLimit, historyVersionList);
1141 LOGI("End GetHistoryVersionList, ret %{public}d", ret);
1142 return ret;
1143 }
1144
DownloadHistoryVersion(const std::string & uri,int64_t & downloadId,const uint64_t versionId,const sptr<IRemoteObject> & downloadCallback,std::string & versionUri)1145 int32_t CloudSyncService::DownloadHistoryVersion(const std::string &uri, int64_t &downloadId, const uint64_t versionId,
1146 const sptr<IRemoteObject> &downloadCallback, std::string &versionUri)
1147 {
1148 LOGI("Begin DownloadHistoryVersion");
1149
1150 BundleNameUserInfo bundleNameUserInfo;
1151 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
1152 if (ret != E_OK) {
1153 LOGE("GetBundleNameUserInfo failed.");
1154 return ret;
1155 }
1156
1157 sptr<ICloudDownloadCallback> downloadCb = iface_cast<ICloudDownloadCallback>(downloadCallback);
1158 ret = dataSyncManager_->DownloadHistoryVersion(bundleNameUserInfo, uri, downloadId, versionId,
1159 downloadCb, versionUri);
1160 LOGI("End DownloadHistoryVersion, ret %{public}d", ret);
1161 return ret;
1162 }
1163
ReplaceFileWithHistoryVersion(const std::string & uri,const std::string & versionUri)1164 int32_t CloudSyncService::ReplaceFileWithHistoryVersion(const std::string &uri, const std::string &versionUri)
1165 {
1166 LOGI("Begin ReplaceFileWithHistoryVersion");
1167
1168 BundleNameUserInfo bundleNameUserInfo;
1169 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
1170 if (ret != E_OK) {
1171 LOGE("GetBundleNameUserInfo failed.");
1172 return ret;
1173 }
1174
1175 ret = dataSyncManager_->ReplaceFileWithHistoryVersion(bundleNameUserInfo, uri, versionUri);
1176 LOGI("End ReplaceFileWithHistoryVersion, ret %{public}d", ret);
1177 return ret;
1178 }
1179
IsFileConflict(const std::string & uri,bool & isConflict)1180 int32_t CloudSyncService::IsFileConflict(const std::string &uri, bool &isConflict)
1181 {
1182 LOGI("Begin IsFileConflict");
1183
1184 BundleNameUserInfo bundleNameUserInfo;
1185 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
1186 if (ret != E_OK) {
1187 LOGE("GetBundleNameUserInfo failed.");
1188 return ret;
1189 }
1190
1191 ret = dataSyncManager_->IsFileConflict(bundleNameUserInfo, uri, isConflict);
1192 LOGI("End IsFileConflict, ret %{public}d", ret);
1193 return ret;
1194 }
1195
ClearFileConflict(const std::string & uri)1196 int32_t CloudSyncService::ClearFileConflict(const std::string &uri)
1197 {
1198 LOGI("Begin ClearFileConflict");
1199
1200 BundleNameUserInfo bundleNameUserInfo;
1201 int ret = GetBundleNameUserInfo(bundleNameUserInfo);
1202 if (ret != E_OK) {
1203 LOGE("GetBundleNameUserInfo failed.");
1204 return ret;
1205 }
1206
1207 ret = dataSyncManager_->ClearFileConflict(bundleNameUserInfo, uri);
1208 LOGI("End ClearFileConflict, ret %{public}d", ret);
1209 return ret;
1210 }
1211 } // namespace OHOS::FileManagement::CloudSync
1212