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 "cycle_task/cycle_task_runner.h"
21 #include "dfs_error.h"
22 #include "data_sync_const.h"
23 #include "dfsu_access_token_helper.h"
24 #include "directory_ex.h"
25 #include "ipc/cloud_sync_callback_manager.h"
26 #include "ipc/download_asset_callback_manager.h"
27 #include "meta_file.h"
28 #include "periodic_check_task.h"
29 #include "sandbox_helper.h"
30 #include "session_manager.h"
31 #include "sdk_helper.h"
32 #include "sync_rule/battery_status.h"
33 #include "sync_rule/cloud_status.h"
34 #include "sync_rule/net_conn_callback_observer.h"
35 #include "sync_rule/network_status.h"
36 #include "system_ability_definition.h"
37 #include "sync_rule/screen_status.h"
38 #include "task_state_manager.h"
39 #include "utils_log.h"
40
41 namespace OHOS::FileManagement::CloudSync {
42 using namespace std;
43 using namespace OHOS;
44 constexpr int32_t MIN_USER_ID = 100;
45 constexpr int LOAD_SA_TIMEOUT_MS = 4000;
46
47 REGISTER_SYSTEM_ABILITY_BY_ID(CloudSyncService, FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, false);
48
CloudSyncService(int32_t saID,bool runOnCreate)49 CloudSyncService::CloudSyncService(int32_t saID, bool runOnCreate) : SystemAbility(saID, runOnCreate)
50 {
51 dataSyncManager_ = make_shared<DataSyncManager>();
52 batteryStatusListener_ = make_shared<BatteryStatusListener>(dataSyncManager_);
53 screenStatusListener_ = make_shared<ScreenStatusListener>(dataSyncManager_);
54 }
55
PublishSA()56 void CloudSyncService::PublishSA()
57 {
58 LOGI("Begin to init");
59 if (!SystemAbility::Publish(this)) {
60 throw runtime_error("Failed to publish the daemon");
61 }
62 LOGI("Init finished successfully");
63 }
64
Init()65 void CloudSyncService::Init()
66 {
67 NetworkStatus::InitNetwork(dataSyncManager_);
68 /* Get Init Charging status */
69 BatteryStatus::GetInitChargingStatus();
70 ScreenStatus::InitScreenStatus();
71 }
72
GetHmdfsPath(const std::string & uri,int32_t userId)73 std::string CloudSyncService::GetHmdfsPath(const std::string &uri, int32_t userId)
74 {
75 const std::string HMDFS_DIR = "/mnt/hmdfs/";
76 const std::string DATA_DIR = "/account/device_view/local/data/";
77 const std::string FILE_DIR = "data/storage/el2/distributedfiles/";
78 const std::string URI_PREFIX = "://";
79 if (uri.empty()) {
80 return "";
81 }
82
83 std::string bundleName;
84 size_t uriPrefixPos = uri.find(URI_PREFIX);
85 if (uriPrefixPos == std::string::npos) {
86 return "";
87 }
88 uriPrefixPos += URI_PREFIX.length();
89 size_t bundleNameEndPos = uri.find('/', uriPrefixPos);
90 if (bundleNameEndPos == std::string::npos) {
91 return "";
92 }
93 bundleName = uri.substr(uriPrefixPos, bundleNameEndPos - uriPrefixPos);
94
95 std::string relativePath;
96 size_t fileDirPos = uri.find(FILE_DIR);
97 if (fileDirPos == std::string::npos) {
98 return "";
99 }
100 fileDirPos += FILE_DIR.length();
101 relativePath = uri.substr(fileDirPos);
102
103 std::string outputPath = HMDFS_DIR + std::to_string(userId) + DATA_DIR + bundleName + "/" + relativePath;
104 std::string dir = outputPath.substr(0, outputPath.find_last_of('/'));
105
106 ForceCreateDirectory(dir);
107 return outputPath;
108 }
109
OnStart(const SystemAbilityOnDemandReason & startReason)110 void CloudSyncService::OnStart(const SystemAbilityOnDemandReason& startReason)
111 {
112 try {
113 PublishSA();
114 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
115 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
116 } catch (const exception &e) {
117 LOGE("%{public}s", e.what());
118 }
119 LOGI("Start service successfully");
120 Init();
121 LOGI("init service successfully");
122 TaskStateManager::GetInstance().StartTask();
123 HandleStartReason(startReason);
124 }
125
OnStop()126 void CloudSyncService::OnStop()
127 {
128 LOGI("Stop finished successfully");
129 }
130
HandleStartReason(const SystemAbilityOnDemandReason & startReason)131 void CloudSyncService::HandleStartReason(const SystemAbilityOnDemandReason& startReason)
132 {
133 string reason = startReason.GetName();
134 int32_t userId = 0;
135
136 LOGI("Begin to start service reason: %{public}s", reason.c_str());
137
138 if (reason == "usual.event.USER_UNLOCKED") {
139 shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(dataSyncManager_);
140 taskRunner->SetPref(ForcePeriodicCheck, true);
141 return;
142 }
143
144 if (dataSyncManager_->GetUserId(userId) != E_OK) {
145 return;
146 }
147
148 if (reason == "usual.event.wifi.SCAN_FINISHED") {
149 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::NETWORK_AVAIL_TRIGGER);
150 } else if (reason == "usual.event.BATTERY_OKAY") {
151 dataSyncManager_->TriggerRecoverySync(SyncTriggerType::BATTERY_OK_TRIGGER);
152 } else if (reason == "usual.event.SCREEN_OFF") {
153 dataSyncManager_->DownloadThumb();
154 }
155
156 if (reason != "load") {
157 shared_ptr<CycleTaskRunner> taskRunner = make_shared<CycleTaskRunner>(dataSyncManager_);
158 taskRunner->StartTask(reason);
159 }
160 }
161
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)162 void CloudSyncService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
163 {
164 LOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
165 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
166 batteryStatusListener_->Start();
167 screenStatusListener_->Start();
168 } else if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
169 auto sessionManager = make_shared<SessionManager>();
170 sessionManager->Init();
171 fileTransferManager_ = make_shared<FileTransferManager>(sessionManager);
172 fileTransferManager_->Init();
173 } else {
174 LOGE("unexpected");
175 }
176 }
177
OnLoadSACompleteForRemote(const std::string & deviceId,int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)178 void CloudSyncService::LoadRemoteSACallback::OnLoadSACompleteForRemote(const std::string &deviceId,
179 int32_t systemAbilityId,
180 const sptr<IRemoteObject> &remoteObject)
181 {
182 LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{public}s", systemAbilityId,
183 (remoteObject == nullptr ? "false" : "true"));
184 unique_lock<mutex> lock(loadRemoteSAMutex_);
185 if (remoteObject == nullptr) {
186 isLoadSuccess_.store(false);
187 } else {
188 isLoadSuccess_.store(true);
189 remoteObjectMap_[deviceId] = remoteObject;
190 }
191 proxyConVar_.notify_one();
192 }
193
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)194 void CloudSyncService::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
195 {
196 LOGD("set death recipient");
197 auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
198 unique_lock<mutex> lock(loadRemoteSAMutex_);
199 for (auto it = remoteObjectMap_.begin(); it != remoteObjectMap_.end();) {
200 if (it->second.GetRefPtr() == obj.GetRefPtr()) {
201 it = remoteObjectMap_.erase(it);
202 LOGD("remote sa died");
203 } else {
204 ++it;
205 }
206 }
207 };
208 deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
209 remoteObject->AddDeathRecipient(deathRecipient_);
210 }
211
LoadRemoteSA(const std::string & deviceId)212 int32_t CloudSyncService::LoadRemoteSA(const std::string &deviceId)
213 {
214 unique_lock<mutex> lock(loadRemoteSAMutex_);
215 auto iter = remoteObjectMap_.find(deviceId);
216 if (iter != remoteObjectMap_.end()) {
217 return E_OK;
218 }
219 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
220 if (samgr == nullptr) {
221 LOGE("Samgr is nullptr");
222 return E_SA_LOAD_FAILED;
223 }
224 sptr<LoadRemoteSACallback> cloudSyncLoadCallback = new LoadRemoteSACallback();
225 if (cloudSyncLoadCallback == nullptr) {
226 LOGE("cloudSyncLoadCallback is nullptr");
227 return E_SA_LOAD_FAILED;
228 }
229 int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, deviceId, cloudSyncLoadCallback);
230 if (ret != E_OK) {
231 LOGE("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d",
232 FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
233 return E_SA_LOAD_FAILED;
234 }
235
236 auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
237 lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
238 [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
239 if (!waitStatus) {
240 LOGE("Load CloudSynd SA timeout");
241 return E_SA_LOAD_FAILED;
242 }
243 SetDeathRecipient(remoteObjectMap_[deviceId]);
244 return E_OK;
245 }
246
UnRegisterCallbackInner()247 int32_t CloudSyncService::UnRegisterCallbackInner()
248 {
249 string bundleName;
250 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
251 return E_INVAL_ARG;
252 }
253
254 CloudSyncCallbackManager::GetInstance().RemoveCallback(bundleName);
255 return E_OK;
256 }
257
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject)258 int32_t CloudSyncService::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject)
259 {
260 if (remoteObject == nullptr) {
261 LOGE("remoteObject is nullptr");
262 return E_INVAL_ARG;
263 }
264
265 string bundleName;
266 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
267 return E_INVAL_ARG;
268 }
269
270 auto callback = iface_cast<ICloudSyncCallback>(remoteObject);
271 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
272 CloudSyncCallbackManager::GetInstance().AddCallback(bundleName, callerUserId, callback);
273 dataSyncManager_->RegisterCloudSyncCallback(bundleName, callerUserId);
274 return E_OK;
275 }
276
StartSyncInner(bool forceFlag)277 int32_t CloudSyncService::StartSyncInner(bool forceFlag)
278 {
279 string bundleName;
280 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
281 return E_INVAL_ARG;
282 }
283 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
284 return dataSyncManager_->TriggerStartSync(bundleName, callerUserId, forceFlag,
285 SyncTriggerType::APP_TRIGGER);
286 }
287
TriggerSyncInner(const std::string & bundleName,const int32_t & userId)288 int32_t CloudSyncService::TriggerSyncInner(const std::string &bundleName, const int32_t &userId)
289 {
290 if (bundleName.empty() || userId < MIN_USER_ID) {
291 LOGE("Trigger sync parameter is invalid");
292 return E_INVAL_ARG;
293 }
294 return dataSyncManager_->TriggerStartSync(bundleName, userId, false,
295 SyncTriggerType::APP_TRIGGER);
296 }
297
StopSyncInner()298 int32_t CloudSyncService::StopSyncInner()
299 {
300 string bundleName;
301 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
302 return E_INVAL_ARG;
303 }
304 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
305 return dataSyncManager_->TriggerStopSync(bundleName, callerUserId, SyncTriggerType::APP_TRIGGER);
306 }
307
GetSyncTimeInner(int64_t & syncTime)308 int32_t CloudSyncService::GetSyncTimeInner(int64_t &syncTime)
309 {
310 syncTime = 0;
311 LOGI("GetLasySyncTime(service) is ok");
312 return E_OK;
313 }
314
CleanCacheInner(const std::string & uri)315 int32_t CloudSyncService::CleanCacheInner(const std::string &uri)
316 {
317 string bundleName;
318 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
319 return E_INVAL_ARG;
320 }
321 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
322 return dataSyncManager_->CleanCache(bundleName, callerUserId, uri);
323 }
324
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)325 int32_t CloudSyncService::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
326 {
327 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
328
329 /* update app switch status */
330 auto ret = CloudStatus::ChangeAppSwitch(bundleName, callerUserId, status);
331 if (ret != E_OK) {
332 return ret;
333 }
334 if (status) {
335 return dataSyncManager_->TriggerStartSync(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER);
336 } else {
337 return dataSyncManager_->TriggerStopSync(bundleName, callerUserId, SyncTriggerType::CLOUD_TRIGGER);
338 }
339 }
340
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)341 int32_t CloudSyncService::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
342 {
343 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
344 return dataSyncManager_->TriggerStartSync(bundleName, callerUserId, false, SyncTriggerType::CLOUD_TRIGGER);
345 }
346
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)347 int32_t CloudSyncService::NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData)
348 {
349 auto driveKit = DriveKit::DriveKitNative::GetInstance(userId);
350 if (driveKit == nullptr) {
351 LOGE("sdk helper get drive kit instance fail");
352 return E_CLOUD_SDK;
353 }
354
355 DriveKit::DKUserInfo userInfo;
356 auto err = driveKit->GetCloudUserInfo(userInfo);
357 if (err.HasError()) {
358 LOGE("GetCloudUserInfo failed, server err:%{public}d and dk err:%{public}d", err.serverErrorCode,
359 err.dkErrorCode);
360 return E_CLOUD_SDK;
361 }
362
363 DriveKit::DKRecordChangeEvent event;
364 auto eventErr = driveKit->ResolveNotificationEvent(extraData, event);
365 if (eventErr.HasError()) {
366 LOGE("ResolveNotificationEvent failed, server err:%{public}d and dk err:%{public}d", eventErr.serverErrorCode,
367 eventErr.dkErrorCode);
368 return E_CLOUD_SDK;
369 }
370 return dataSyncManager_->TriggerStartSync(event.appBundleName, userId, false, SyncTriggerType::CLOUD_TRIGGER);
371 }
372
DisableCloud(const std::string & accoutId)373 int32_t CloudSyncService::DisableCloud(const std::string &accoutId)
374 {
375 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
376 return dataSyncManager_->DisableCloud(callerUserId);
377 }
378
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)379 int32_t CloudSyncService::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
380 {
381 return E_OK;
382 }
383
Clean(const std::string & accountId,const CleanOptions & cleanOptions)384 int32_t CloudSyncService::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
385 {
386 LOGI("Clean accountId is: %{public}s", accountId.c_str());
387 for (auto &iter : cleanOptions.appActionsData) {
388 LOGD("Clean key is: %s, value is: %d", iter.first.c_str(), iter.second);
389 }
390
391 MetaFileMgr::GetInstance().ClearAll();
392 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
393 LOGI("Clean callerUserId is: %{public}d", callerUserId);
394 for (auto iter = cleanOptions.appActionsData.begin(); iter != cleanOptions.appActionsData.end(); ++iter) {
395 dataSyncManager_->CleanCloudFile(callerUserId, iter->first, iter->second);
396 }
397
398 return E_OK;
399 }
StartFileCache(const std::string & uriStr)400 int32_t CloudSyncService::StartFileCache(const std::string &uriStr)
401 {
402 Uri uri(uriStr);
403 string bundleName = uri.GetAuthority();
404 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
405 return dataSyncManager_->StartDownloadFile(bundleName, callerUserId, uriStr);
406 }
407
408 constexpr int TEST_MAIN_USR_ID = 100;
StartDownloadFile(const std::string & path)409 int32_t CloudSyncService::StartDownloadFile(const std::string &path)
410 {
411 string bundleName;
412 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
413 return E_INVAL_ARG;
414 }
415 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
416 LOGI("start StartDownloadFile");
417 if (callerUserId == 0) {
418 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
419 }
420 return dataSyncManager_->StartDownloadFile(bundleName, callerUserId, path);
421 }
422
StopDownloadFile(const std::string & path)423 int32_t CloudSyncService::StopDownloadFile(const std::string &path)
424 {
425 string bundleName;
426 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
427 return E_INVAL_ARG;
428 }
429 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
430 LOGI("start StopDownloadFile");
431 if (callerUserId == 0) {
432 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
433 }
434 return dataSyncManager_->StopDownloadFile(bundleName, callerUserId, path);
435 }
436
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)437 int32_t CloudSyncService::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
438 {
439 string bundleName;
440 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
441 return E_INVAL_ARG;
442 }
443 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
444 auto downloadCb = iface_cast<ICloudDownloadCallback>(downloadCallback);
445 LOGI("start RegisterDownloadFileCallback");
446 if (callerUserId == 0) {
447 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
448 }
449 return dataSyncManager_->RegisterDownloadFileCallback(bundleName, callerUserId, downloadCb);
450 }
451
UnregisterDownloadFileCallback()452 int32_t CloudSyncService::UnregisterDownloadFileCallback()
453 {
454 string bundleName;
455 if (DfsuAccessTokenHelper::GetCallerBundleName(bundleName)) {
456 return E_INVAL_ARG;
457 }
458 auto callerUserId = DfsuAccessTokenHelper::GetUserId();
459 LOGI("start UnregisterDownloadFileCallback");
460 if (callerUserId == 0) {
461 callerUserId = TEST_MAIN_USR_ID; // for root user change id to main user for test
462 }
463 return dataSyncManager_->UnregisterDownloadFileCallback(bundleName, callerUserId);
464 }
465
UploadAsset(const int32_t userId,const std::string & request,std::string & result)466 int32_t CloudSyncService::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
467 {
468 auto driveKit = DriveKit::DriveKitNative::GetInstance(userId);
469 if (driveKit == nullptr) {
470 LOGE("uploadAsset get drive kit instance err");
471 return E_CLOUD_SDK;
472 }
473 string bundleName("distributeddata");
474 TaskStateManager::GetInstance().StartTask(bundleName, TaskType::UPLOAD_ASSET_TASK);
475 auto ret = driveKit->OnUploadAsset(request, result);
476 TaskStateManager::GetInstance().CompleteTask(bundleName, TaskType::UPLOAD_ASSET_TASK);
477 return ret;
478 }
479
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)480 int32_t CloudSyncService::DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj)
481 {
482 auto sdkHelper = std::make_shared<SdkHelper>();
483 auto ret = sdkHelper->Init(userId, bundleName);
484 if (ret != E_OK) {
485 LOGE("get sdk helper err %{public}d", ret);
486 return ret;
487 }
488
489 DriveKit::DKAsset asset;
490 asset.assetName = assetInfoObj.assetName;
491
492 asset.uri = GetHmdfsPath(assetInfoObj.uri, userId);
493 if (asset.uri.empty()) {
494 LOGE("fail to get download path from %{public}s", assetInfoObj.uri.c_str());
495 return E_INVAL_ARG;
496 }
497
498 // Not to pass the assetinfo.fieldkey
499 DriveKit::DKDownloadAsset assetsToDownload{assetInfoObj.recordType, assetInfoObj.recordId, {}, asset, {}};
500 TaskStateManager::GetInstance().StartTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
501 ret = sdkHelper->DownloadAssets(assetsToDownload);
502 TaskStateManager::GetInstance().CompleteTask(bundleName, TaskType::DOWNLOAD_ASSET_TASK);
503 return ret;
504 }
505
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)506 int32_t CloudSyncService::DownloadAsset(const uint64_t taskId,
507 const int32_t userId,
508 const std::string &bundleName,
509 const std::string &networkId,
510 AssetInfoObj &assetInfoObj)
511 {
512 if (networkId == "edge2cloud") {
513 LOGE("now not support");
514 return E_INVAL_ARG;
515 }
516 // Load sa for remote device
517 if (LoadRemoteSA(networkId) != E_OK) { // maybe need to convert deviceId
518 return E_SA_LOAD_FAILED;
519 }
520
521 string uri = assetInfoObj.uri;
522 fileTransferManager_->DownloadFileFromRemoteDevice(networkId, userId, taskId, uri);
523 return E_OK;
524 }
525
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)526 int32_t CloudSyncService::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
527 {
528 if (remoteObject == nullptr) {
529 LOGE("remoteObject is nullptr");
530 return E_INVAL_ARG;
531 }
532 auto callback = iface_cast<IDownloadAssetCallback>(remoteObject);
533 DownloadAssetCallbackManager::GetInstance().AddCallback(callback);
534 return E_OK;
535 }
536
DeleteAsset(const int32_t userId,const std::string & uri)537 int32_t CloudSyncService::DeleteAsset(const int32_t userId, const std::string &uri)
538 {
539 std::string physicalPath = "";
540 int ret = AppFileService::SandboxHelper::GetPhysicalPath(uri, std::to_string(userId), physicalPath);
541 if (ret != 0) {
542 LOGE("Get physical path failed with %{public}d", ret);
543 return E_GET_PHYSICAL_PATH_FAILED;
544 }
545
546 LOGD("delete assert, path %{public}s", physicalPath.c_str());
547
548 ret = unlink(physicalPath.c_str());
549 if (ret != 0) {
550 LOGE("fail to delete asset, errno %{public}d", errno);
551 return E_DELETE_FAILED;
552 }
553 return E_OK;
554 }
555 } // namespace OHOS::FileManagement::CloudSync
556