• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_download_callback_client.h"
17 #include "cloud_sync_manager_impl.h"
18 #include "cloud_sync_callback_client.h"
19 #include "downgrade_download_callback_client.h"
20 #include "service_proxy.h"
21 #include "dfs_error.h"
22 #include "system_ability_definition.h"
23 #include "iservice_registry.h"
24 #include "utils_directory.h"
25 #include "utils_log.h"
26 
27 namespace OHOS::FileManagement::CloudSync {
28 using namespace std;
29 constexpr int32_t MIN_USER_ID = 100;
30 constexpr int32_t MAX_FILE_CACHE_NUM = 400;
31 constexpr int32_t MAX_DENTRY_FILE_SIZE = 500;
GetInstance()32 CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance()
33 {
34     static CloudSyncManagerImpl instance;
35     return instance;
36 }
37 
RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback,const std::string & bundleName)38 int32_t CloudSyncManagerImpl::RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback,
39                                                const std::string &bundleName)
40 {
41     if (!callback) {
42         LOGE("callback is null");
43         return E_INVAL_ARG;
44     }
45     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
46     if (!CloudSyncServiceProxy) {
47         LOGE("proxy is null");
48         return E_SA_LOAD_FAILED;
49     }
50     auto ret = CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)),
51                                                             bundleName);
52     {
53         unique_lock<mutex> lock(callbackMutex_);
54         callback_ = callback;
55     }
56     SubscribeListener(bundleName);
57     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
58     LOGI("RegisterCallback ret %{public}d", ret);
59     return ret;
60 }
61 
RegisterFileSyncCallback(const std::shared_ptr<CloudSyncCallback> callback,const std::string & bundleName)62 int32_t CloudSyncManagerImpl::RegisterFileSyncCallback(const std::shared_ptr<CloudSyncCallback> callback,
63     const std::string &bundleName)
64 {
65     if (!callback) {
66         LOGE("callback is null");
67         return E_INVAL_ARG;
68     }
69     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
70     if (!CloudSyncServiceProxy) {
71         LOGE("proxy is null");
72         return E_SA_LOAD_FAILED;
73     }
74     auto ret = CloudSyncServiceProxy->RegisterFileSyncCallbackInner(
75         sptr(new (std::nothrow) CloudSyncCallbackClient(callback)), bundleName);
76     {
77         unique_lock<mutex> lock(callbackMutex_);
78         callback_ = callback;
79     }
80     SubscribeListener(bundleName);
81     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
82     LOGI("RegisterFileSyncCallback ret %{public}d", ret);
83     return ret;
84 }
85 
UnRegisterCallback(const std::string & bundleName)86 int32_t CloudSyncManagerImpl::UnRegisterCallback(const std::string &bundleName)
87 {
88     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
89     if (!CloudSyncServiceProxy) {
90         LOGE("proxy is null");
91         return E_SA_LOAD_FAILED;
92     }
93 
94     auto ret = CloudSyncServiceProxy->UnRegisterCallbackInner(bundleName);
95     if (!ret) {
96         {
97             unique_lock<mutex> lock(callbackMutex_);
98             callback_ = nullptr;
99         }
100         SubscribeListener();
101     }
102     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
103     LOGI("UnRegisterCallback ret %{public}d", ret);
104     return ret;
105 }
106 
UnRegisterFileSyncCallback(const std::string & bundleName)107 int32_t CloudSyncManagerImpl::UnRegisterFileSyncCallback(const std::string &bundleName)
108 {
109     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
110     if (!CloudSyncServiceProxy) {
111         LOGE("proxy is null");
112         return E_SA_LOAD_FAILED;
113     }
114 
115     auto ret = CloudSyncServiceProxy->UnRegisterFileSyncCallbackInner(bundleName);
116     if (!ret) {
117         {
118             unique_lock<mutex> lock(callbackMutex_);
119             callback_ = nullptr;
120         }
121         SubscribeListener();
122     }
123     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
124     LOGI("UnRegisterFileSyncCallback ret %{public}d", ret);
125     return ret;
126 }
127 
StartSync(const std::string & bundleName)128 int32_t CloudSyncManagerImpl::StartSync(const std::string &bundleName)
129 {
130     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
131     if (!CloudSyncServiceProxy) {
132         LOGE("proxy is null");
133         return E_SA_LOAD_FAILED;
134     }
135     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
136     return CloudSyncServiceProxy->StartSyncInner(true, bundleName);
137 }
138 
StartFileSync(const std::string & bundleName)139 int32_t CloudSyncManagerImpl::StartFileSync(const std::string &bundleName)
140 {
141     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
142     if (!CloudSyncServiceProxy) {
143         LOGE("proxy is null");
144         return E_SA_LOAD_FAILED;
145     }
146     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
147     return CloudSyncServiceProxy->StartFileSyncInner(true, bundleName);
148 }
149 
GetSyncTime(int64_t & syncTime,const std::string & bundleName)150 int32_t CloudSyncManagerImpl::GetSyncTime(int64_t &syncTime, const std::string &bundleName)
151 {
152     LOGI("GetSyncTime Start");
153     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
154     if (!CloudSyncServiceProxy) {
155         LOGE("proxy is null");
156         return E_SA_LOAD_FAILED;
157     }
158     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
159     return CloudSyncServiceProxy->GetSyncTimeInner(syncTime, bundleName);
160 }
161 
OptimizeStorage(const OptimizeSpaceOptions & optimizeOptions,const std::shared_ptr<CloudOptimizeCallback> optimizeCallback)162 int32_t CloudSyncManagerImpl::OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
163     const std::shared_ptr<CloudOptimizeCallback> optimizeCallback)
164 {
165     LOGI("OptimizeStorage Start");
166     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
167     if (!CloudSyncServiceProxy) {
168         LOGE("proxy is null");
169         return E_SA_LOAD_FAILED;
170     }
171     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
172 
173     bool isCallbackValid = false;
174     sptr<CloudOptimizeCallbackClient> opCallback = sptr(new (std::nothrow) CloudOptimizeCallbackClient(nullptr));
175     if (optimizeCallback != nullptr) {
176         opCallback = sptr(new (std::nothrow) CloudOptimizeCallbackClient(optimizeCallback));
177         isCallbackValid = true;
178         if (opCallback == nullptr) {
179             isCallbackValid = false;
180             LOGE("OptimizeStorage callback failed");
181         }
182     }
183 
184     return CloudSyncServiceProxy->OptimizeStorage(optimizeOptions, isCallbackValid, opCallback);
185 }
186 
StopOptimizeStorage()187 int32_t CloudSyncManagerImpl::StopOptimizeStorage()
188 {
189     LOGI("StopOptimizeStorage Start");
190     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
191     if (!CloudSyncServiceProxy) {
192         LOGE("proxy is null");
193         return E_SA_LOAD_FAILED;
194     }
195     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
196 
197     return CloudSyncServiceProxy->StopOptimizeStorage();
198 }
199 
StartSync(bool forceFlag,const std::shared_ptr<CloudSyncCallback> callback)200 int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr<CloudSyncCallback> callback)
201 {
202     if (!callback) {
203         LOGE("callback is null");
204         return E_INVAL_ARG;
205     }
206     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
207     if (!CloudSyncServiceProxy) {
208         LOGE("proxy is null");
209         return E_SA_LOAD_FAILED;
210     }
211 
212     if (!isFirstCall_.test()) {
213         LOGI("Register callback");
214         auto ret = CloudSyncServiceProxy->RegisterCallbackInner(
215             sptr(new (std::nothrow) CloudSyncCallbackClient(callback)), "");
216         if (ret) {
217             LOGE("Register callback failed");
218             isFirstCall_.clear();
219             return ret;
220         }
221         callback_ = callback;
222         SubscribeListener();
223         SetDeathRecipient(CloudSyncServiceProxy->AsObject());
224     }
225 
226     return CloudSyncServiceProxy->StartSyncInner(forceFlag, "");
227 }
228 
TriggerSync(const std::string & bundleName,const int32_t & userId)229 int32_t CloudSyncManagerImpl::TriggerSync(const std::string &bundleName, const int32_t &userId)
230 {
231     if (bundleName.empty() || userId < MIN_USER_ID) {
232         LOGE("Trigger Sync parameter is invalid");
233         return E_INVAL_ARG;
234     }
235     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
236     if (!CloudSyncServiceProxy) {
237         LOGE("proxy is null");
238         return E_SA_LOAD_FAILED;
239     }
240     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
241     return CloudSyncServiceProxy->TriggerSyncInner(bundleName, userId);
242 }
243 
StopSync(const std::string & bundleName,bool forceFlag)244 int32_t CloudSyncManagerImpl::StopSync(const std::string &bundleName, bool forceFlag)
245 {
246     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
247     if (!CloudSyncServiceProxy) {
248         LOGE("proxy is null");
249         return E_SA_LOAD_FAILED;
250     }
251     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
252     return CloudSyncServiceProxy->StopSyncInner(bundleName, forceFlag);
253 }
254 
StopFileSync(const std::string & bundleName,bool forceFlag)255 int32_t CloudSyncManagerImpl::StopFileSync(const std::string &bundleName, bool forceFlag)
256 {
257     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
258     if (!CloudSyncServiceProxy) {
259         LOGE("proxy is null");
260         return E_SA_LOAD_FAILED;
261     }
262     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
263     return CloudSyncServiceProxy->StopFileSyncInner(bundleName, forceFlag);
264 }
265 
ResetCursor(const std::string & bundleName)266 int32_t CloudSyncManagerImpl::ResetCursor(const std::string &bundleName)
267 {
268     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
269     if (!CloudSyncServiceProxy) {
270         LOGE("proxy is null");
271         return E_SA_LOAD_FAILED;
272     }
273     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
274     return CloudSyncServiceProxy->ResetCursor(bundleName);
275 }
276 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)277 int32_t CloudSyncManagerImpl::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
278 {
279     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
280     if (!CloudSyncServiceProxy) {
281         LOGE("proxy is null");
282         return E_SA_LOAD_FAILED;
283     }
284 
285     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
286 
287     int32_t ret = CloudSyncServiceProxy->ChangeAppSwitch(accoutId, bundleName, status);
288     LOGI("ChangeAppSwitch ret %{public}d", ret);
289     return ret;
290 }
291 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)292 int32_t CloudSyncManagerImpl::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
293 {
294     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
295     if (!CloudSyncServiceProxy) {
296         LOGE("proxy is null");
297         return E_SA_LOAD_FAILED;
298     }
299 
300     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
301 
302     int32_t ret = CloudSyncServiceProxy->NotifyDataChange(accoutId, bundleName);
303     LOGI("NotifyDataChange ret %{public}d", ret);
304     return ret;
305 }
306 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)307 int32_t CloudSyncManagerImpl::NotifyEventChange(
308     int32_t userId, const std::string &eventId, const std::string &extraData)
309 {
310     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
311     if (!CloudSyncServiceProxy) {
312         LOGE("proxy is null");
313         return E_SA_LOAD_FAILED;
314     }
315 
316     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
317 
318     int32_t ret = CloudSyncServiceProxy->NotifyEventChange(userId, eventId, extraData);
319     LOGI("NotifyDataChange ret %{public}d", ret);
320     return ret;
321 }
322 
StartDownloadFile(const std::string & uri,const std::shared_ptr<CloudDownloadCallback> downloadCallback,int64_t & downloadId)323 int32_t CloudSyncManagerImpl::StartDownloadFile(const std::string &uri,
324                                                 const std::shared_ptr<CloudDownloadCallback> downloadCallback,
325                                                 int64_t &downloadId)
326 {
327     LOGI("StartDownloadFile start");
328     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
329     if (!CloudSyncServiceProxy) {
330         LOGE("proxy is null");
331         return E_SA_LOAD_FAILED;
332     }
333     if (downloadCallback == nullptr) {
334         LOGE("The callback is null");
335         return E_INVAL_ARG;
336     }
337     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
338     auto dlCallback = sptr<CloudDownloadCallbackClient>::MakeSptr(downloadCallback);
339     int32_t ret = CloudSyncServiceProxy->StartDownloadFile(uri, dlCallback, downloadId);
340     LOGI("StartDownloadFile ret %{public}d", ret);
341     return ret;
342 }
343 
BatchCleanFile(const std::vector<CleanFileInfo> & fileInfo,std::vector<std::string> & failCloudId)344 int32_t CloudSyncManagerImpl::BatchCleanFile(const std::vector<CleanFileInfo> &fileInfo,
345     std::vector<std::string> &failCloudId)
346 {
347     if (fileInfo.size() > CLEAN_FILE_MAX_SIZE) {
348         LOGE("BatchCleanFile size over max limit");
349         return E_INVAL_ARG;
350     }
351     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
352     if (!CloudSyncServiceProxy) {
353         LOGE("proxy is null");
354         return E_SA_LOAD_FAILED;
355     }
356 
357     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
358     std::vector<CleanFileInfoObj> fileInfoObj;
359     for (const auto &info : fileInfo) {
360         CleanFileInfoObj obj(info);
361         fileInfoObj.emplace_back(obj);
362     }
363 
364     return CloudSyncServiceProxy->BatchCleanFile(fileInfoObj, failCloudId);
365 }
366 
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,int32_t fieldkey,const std::shared_ptr<CloudDownloadCallback> downloadCallback,int32_t timeout)367 int32_t CloudSyncManagerImpl::StartFileCache(const std::vector<std::string> &uriVec,
368                                              int64_t &downloadId,
369                                              int32_t fieldkey,
370                                              const std::shared_ptr<CloudDownloadCallback> downloadCallback,
371                                              int32_t timeout)
372 {
373     LOGI("StartFileCache start, uriVec size: %{public}zu, fieldKey: %{public}d", uriVec.size(), fieldkey);
374     if (uriVec.empty()) {
375         LOGE("The uri list is empty");
376         return E_INVAL_ARG;
377     }
378     if (uriVec.size() > MAX_FILE_CACHE_NUM) {
379         LOGE("The size of uri list exceeded the maximum limit, size: %{public}zu", uriVec.size());
380         return E_EXCEED_MAX_SIZE;
381     }
382     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
383     if (!CloudSyncServiceProxy) {
384         LOGE("proxy is null");
385         return E_SA_LOAD_FAILED;
386     }
387     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
388 
389     if (downloadCallback == nullptr) {
390         LOGW("The callback is null, download progress may not be received");
391     }
392     auto dlCallback = sptr<CloudDownloadCallbackClient>::MakeSptr(downloadCallback);
393     int32_t ret = CloudSyncServiceProxy->StartFileCache(uriVec, downloadId, fieldkey, dlCallback, timeout);
394     LOGI("StartFileCache end, ret %{public}d", ret);
395     return ret;
396 }
397 
StopDownloadFile(int64_t downloadId,bool needClean)398 int32_t CloudSyncManagerImpl::StopDownloadFile(int64_t downloadId, bool needClean)
399 {
400     LOGI("StopDownloadFile start");
401     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
402     if (!CloudSyncServiceProxy) {
403         LOGE("proxy is null");
404         return E_SA_LOAD_FAILED;
405     }
406     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
407     int32_t ret = CloudSyncServiceProxy->StopDownloadFile(downloadId, needClean);
408     LOGI("StopDownloadFile end, ret %{public}d", ret);
409     return ret;
410 }
411 
StopFileCache(int64_t downloadId,bool needClean,int32_t timeout)412 int32_t CloudSyncManagerImpl::StopFileCache(int64_t downloadId, bool needClean, int32_t timeout)
413 {
414     LOGI("StopFileCache start");
415     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
416     if (!CloudSyncServiceProxy) {
417         LOGE("proxy is null");
418         return E_SA_LOAD_FAILED;
419     }
420     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
421     int32_t ret = CloudSyncServiceProxy->StopFileCache(downloadId, needClean, timeout);
422     LOGI("StopFileCache end, ret %{public}d", ret);
423     return ret;
424 }
425 
StartDowngrade(const std::string & bundleName,const std::shared_ptr<DowngradeDlCallback> downloadCallback)426 int32_t CloudSyncManagerImpl::StartDowngrade(const std::string &bundleName,
427                                              const std::shared_ptr<DowngradeDlCallback> downloadCallback)
428 {
429     LOGI("StartDowngrade start");
430     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
431     if (!CloudSyncServiceProxy) {
432         LOGE("proxy is null");
433         return E_SA_LOAD_FAILED;
434     }
435     if (bundleName.empty() || downloadCallback == nullptr) {
436         LOGE("Invalid argument");
437         return E_INVAL_ARG;
438     }
439     sptr<DowngradeDownloadCallbackClient> dlCallback =
440         sptr(new (std::nothrow) DowngradeDownloadCallbackClient(downloadCallback));
441     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
442     int32_t ret = CloudSyncServiceProxy->StartDowngrade(bundleName, dlCallback);
443     LOGI("StartDowngrade ret %{public}d", ret);
444     return ret;
445 }
446 
StopDowngrade(const std::string & bundleName)447 int32_t CloudSyncManagerImpl::StopDowngrade(const std::string &bundleName)
448 {
449     LOGI("StartDowngrade start");
450     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
451     if (!CloudSyncServiceProxy) {
452         LOGE("proxy is null");
453         return E_SA_LOAD_FAILED;
454     }
455     if (bundleName.empty()) {
456         LOGE("Invalid argument");
457         return E_INVAL_ARG;
458     }
459 
460     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
461     int32_t ret = CloudSyncServiceProxy->StopDowngrade(bundleName);
462     LOGI("StopDowngrade ret %{public}d", ret);
463     return ret;
464 }
465 
GetCloudFileInfo(const std::string & bundleName,CloudFileInfo & cloudFileInfo)466 int32_t CloudSyncManagerImpl::GetCloudFileInfo(const std::string &bundleName, CloudFileInfo &cloudFileInfo)
467 {
468     LOGI("StartDowngrade start");
469     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
470     if (!CloudSyncServiceProxy) {
471         LOGE("proxy is null");
472         return E_SA_LOAD_FAILED;
473     }
474     if (bundleName.empty()) {
475         LOGE("Invalid argument");
476         return E_INVAL_ARG;
477     }
478 
479     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
480     int32_t ret = CloudSyncServiceProxy->GetCloudFileInfo(bundleName, cloudFileInfo);
481     LOGI("GetCloudFileInfo ret %{public}d", ret);
482     return ret;
483 }
484 
GetHistoryVersionList(const std::string & uri,const int32_t versionNumLimit,std::vector<CloudSync::HistoryVersion> & historyVersionList)485 int32_t CloudSyncManagerImpl::GetHistoryVersionList(const std::string &uri, const int32_t versionNumLimit,
486     std::vector<CloudSync::HistoryVersion> &historyVersionList)
487 {
488     LOGI("GetHistoryVersionList start, versionNumLimit is %{public}d", versionNumLimit);
489     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
490     if (!CloudSyncServiceProxy) {
491         LOGE("proxy is null");
492         return E_SA_LOAD_FAILED;
493     }
494     if (uri.empty()) {
495         LOGE("Invalid argument");
496         return E_ILLEGAL_URI;
497     }
498 
499     if (versionNumLimit <= 0) {
500         LOGE("Invalid argument");
501         return E_INVAL_ARG;
502     }
503 
504     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
505     int32_t ret = CloudSyncServiceProxy->GetHistoryVersionList(uri, versionNumLimit, historyVersionList);
506     LOGI("GetHistoryVersionList ret %{public}d", ret);
507     return ret;
508 }
509 
DownloadHistoryVersion(const std::string & uri,int64_t & downloadId,const uint64_t versionId,const std::shared_ptr<CloudDownloadCallback> downloadCallback,std::string & versionUri)510 int32_t CloudSyncManagerImpl::DownloadHistoryVersion(const std::string &uri, int64_t &downloadId,
511     const uint64_t versionId, const std::shared_ptr<CloudDownloadCallback> downloadCallback, std::string &versionUri)
512 {
513     LOGI("DownloadHistoryVersion start, versionId is %{public}lld, Callback is null: %{public}d",
514          static_cast<long long>(versionId), (downloadCallback == nullptr));
515     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
516     if (!CloudSyncServiceProxy) {
517         LOGE("proxy is null");
518         return E_SA_LOAD_FAILED;
519     }
520     if (uri.empty()) {
521         LOGE("Invalid argument");
522         return E_ILLEGAL_URI;
523     }
524 
525     if (downloadCallback == nullptr) {
526         LOGE("Invalid argument");
527         return E_INVAL_ARG;
528     }
529 
530     sptr<CloudDownloadCallbackClient> dlCallback =
531         sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback));
532     if (dlCallback == nullptr) {
533         LOGE("DownloadHistoryVersion register download callback failed");
534         return E_SERVICE_INNER_ERROR;
535     }
536 
537     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
538     int32_t ret = CloudSyncServiceProxy->DownloadHistoryVersion(uri, downloadId, versionId, dlCallback, versionUri);
539     LOGI("DownloadHistoryVersion ret %{public}d, downloadId %{public}lld",
540          ret, static_cast<long long>(downloadId));
541     return ret;
542 }
543 
ReplaceFileWithHistoryVersion(const std::string & uri,const std::string & versionUri)544 int32_t CloudSyncManagerImpl::ReplaceFileWithHistoryVersion(const std::string &uri, const std::string &versionUri)
545 {
546     LOGI("ReplaceFileWithHistoryVersion start");
547     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
548     if (!CloudSyncServiceProxy) {
549         LOGE("proxy is null");
550         return E_SA_LOAD_FAILED;
551     }
552     if (uri.empty() || versionUri.empty()) {
553         LOGE("Invalid argument");
554         return E_ILLEGAL_URI;
555     }
556 
557     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
558     int32_t ret = CloudSyncServiceProxy->ReplaceFileWithHistoryVersion(uri, versionUri);
559     return ret;
560 }
561 
IsFileConflict(const std::string & uri,bool & isConflict)562 int32_t CloudSyncManagerImpl::IsFileConflict(const std::string &uri, bool &isConflict)
563 {
564     LOGI("IsFileConflict start");
565     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
566     if (!CloudSyncServiceProxy) {
567         LOGE("proxy is null");
568         return E_SA_LOAD_FAILED;
569     }
570     if (uri.empty()) {
571         LOGE("Invalid argument");
572         return E_ILLEGAL_URI;
573     }
574 
575     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
576     int32_t ret = CloudSyncServiceProxy->IsFileConflict(uri, isConflict);
577     return ret;
578 }
579 
ClearFileConflict(const std::string & uri)580 int32_t CloudSyncManagerImpl::ClearFileConflict(const std::string &uri)
581 {
582     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
583     if (!CloudSyncServiceProxy) {
584         LOGE("proxy is null");
585         return E_SA_LOAD_FAILED;
586     }
587     if (uri.empty()) {
588         LOGE("Invalid argument");
589         return E_ILLEGAL_URI;
590     }
591 
592     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
593     int32_t ret = CloudSyncServiceProxy->ClearFileConflict(uri);
594     return ret;
595 }
596 
DownloadThumb()597 int32_t CloudSyncManagerImpl::DownloadThumb()
598 {
599     LOGI("DownloadThumb start");
600     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
601     if (!CloudSyncServiceProxy) {
602         LOGE("proxy is null");
603         return E_SA_LOAD_FAILED;
604     }
605     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
606     int32_t ret = CloudSyncServiceProxy->DownloadThumb();
607     LOGI("DownloadThumb ret %{public}d", ret);
608     return ret;
609 }
610 
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)611 void CloudSyncManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
612 {
613     if (!isFirstCall_.test_and_set()) {
614         auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
615             LOGE("service died.");
616             ServiceProxy::InvaildInstance();
617             if (callback_) {
618                 callback_->OnDeathRecipient();
619             }
620             isFirstCall_.clear();
621         };
622         deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
623         if (!remoteObject->AddDeathRecipient(deathRecipient_)) {
624             LOGE("add death recipient failed");
625             isFirstCall_.clear();
626         }
627     }
628 }
629 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)630 int32_t CloudSyncManagerImpl::EnableCloud(const std::string &accoutId,
631                                           const SwitchDataObj &switchData)
632 {
633     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
634     if (!CloudSyncServiceProxy) {
635         LOGE("proxy is null");
636         return E_SA_LOAD_FAILED;
637     }
638 
639     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
640 
641     return CloudSyncServiceProxy->EnableCloud(accoutId, switchData);
642 }
643 
DisableCloud(const std::string & accoutId)644 int32_t CloudSyncManagerImpl::DisableCloud(const std::string &accoutId)
645 {
646     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
647     if (!CloudSyncServiceProxy) {
648         LOGE("proxy is null");
649         return E_SA_LOAD_FAILED;
650     }
651 
652     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
653     return CloudSyncServiceProxy->DisableCloud(accoutId);
654 }
655 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)656 int32_t CloudSyncManagerImpl::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
657 {
658     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
659     if (!CloudSyncServiceProxy) {
660         LOGE("proxy is null");
661         return E_SA_LOAD_FAILED;
662     }
663 
664     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
665 
666     return CloudSyncServiceProxy->Clean(accountId, cleanOptions);
667 }
668 
CleanCache(const std::string & uri)669 int32_t CloudSyncManagerImpl::CleanCache(const std::string &uri)
670 {
671     LOGI("CleanCache Start");
672     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
673     if (!CloudSyncServiceProxy) {
674         LOGE("proxy is null");
675         return E_SA_LOAD_FAILED;
676     }
677     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
678     return CloudSyncServiceProxy->CleanCacheInner(uri);
679 }
680 
CleanFileCache(const std::string & uri)681 int32_t CloudSyncManagerImpl::CleanFileCache(const std::string &uri)
682 {
683     LOGI("CleanFileCache Start");
684     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
685     if (!CloudSyncServiceProxy) {
686         LOGE("proxy is null");
687         return E_SA_LOAD_FAILED;
688     }
689     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
690     int32_t ret = CloudSyncServiceProxy->CleanFileCacheInner(uri);
691     LOGI("CleanFileCache end, ret: %{public}d", ret);
692     return ret;
693 }
694 
BatchDentryFileInsert(const std::vector<DentryFileInfo> & fileInfo,std::vector<std::string> & failCloudId)695 int32_t CloudSyncManagerImpl::BatchDentryFileInsert(const std::vector<DentryFileInfo> &fileInfo,
696     std::vector<std::string> &failCloudId)
697 {
698     if (fileInfo.size() > MAX_DENTRY_FILE_SIZE) {
699         LOGE("BatchDentryFileInsert parameter is invalid");
700         return E_INVAL_ARG;
701     }
702     auto CloudSyncServiceProxy = ServiceProxy::GetInstance();
703     if (!CloudSyncServiceProxy) {
704         LOGE("proxy is null");
705         return E_SA_LOAD_FAILED;
706     }
707 
708     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
709     std::vector<DentryFileInfoObj> fileInfoObj;
710     for (const auto &info : fileInfo) {
711         DentryFileInfoObj obj(info);
712         fileInfoObj.emplace_back(obj);
713     }
714 
715     return CloudSyncServiceProxy->BatchDentryFileInsert(fileInfoObj, failCloudId);
716 }
717 
SubscribeListener(std::string bundleName)718 void CloudSyncManagerImpl::SubscribeListener(std::string bundleName)
719 {
720     unique_lock<mutex> lock(subscribeMutex_);
721     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
722     if (samgr == nullptr) {
723         LOGE("Samgr is nullptr");
724         return;
725     }
726     if (listener_ != nullptr) {
727         auto ret = samgr->UnSubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
728         LOGI("unsubscribed to systemAbility ret %{public}d", ret);
729     }
730     if (callback_ != nullptr) {
731         listener_ = new SystemAbilityStatusChange(bundleName);
732         auto ret = samgr->SubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
733         LOGI("subscribed to systemAbility ret %{public}d", ret);
734     } else {
735         listener_ = nullptr;
736     }
737 }
738 
ResetProxyCallback(uint32_t retryCount,const string & bundleName)739 bool CloudSyncManagerImpl::ResetProxyCallback(uint32_t retryCount, const string &bundleName)
740 {
741     auto cloudSyncServiceProxy = ServiceProxy::GetInstance();
742     if (cloudSyncServiceProxy == nullptr) {
743         LOGE("proxy is null");
744         return false;
745     }
746     bool hasCallback = false;
747     if (callback_ != nullptr) {
748         auto callback = sptr(new (std::nothrow) CloudSyncCallbackClient(callback_));
749         if (callback == nullptr ||
750             cloudSyncServiceProxy->RegisterCallbackInner(callback, bundleName) != E_OK) {
751             LOGW("register callback failed, try time is %{public}d", retryCount);
752         } else {
753             hasCallback = true;
754         }
755     }
756     if (hasCallback) {
757         CloudSyncManagerImpl::GetInstance().SetDeathRecipient(cloudSyncServiceProxy->AsObject());
758     }
759     return true;
760 }
761 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)762 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId,
763     const std::string &deviceId)
764 {
765     const uint32_t RETRY_TIMES = 3;
766     const uint32_t SLEEP_TIME = 20 * 1000;
767     uint32_t retryCount = 0;
768     LOGI("saId %{public}d loaded", systemAbilityId);
769     do {
770         usleep(SLEEP_TIME);
771         if (!CloudSyncManagerImpl::GetInstance().ResetProxyCallback(retryCount, bundleName_)) {
772             continue;
773         }
774         return;
775     } while (++retryCount < RETRY_TIMES);
776     LOGE("register callback failed, try too many times");
777 }
778 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)779 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId,
780     const std::string &deviceId)
781 {
782     return;
783 }
784 
CleanGalleryDentryFile()785 void CloudSyncManagerImpl::CleanGalleryDentryFile()
786 {
787     LOGI("CleanGalleryDentryFile start");
788     const std::string photoDir = "/storage/media/cloud/files/Photo";
789     const std::string thumbsDir = "/storage/media/cloud/files/.thumbs/Photo";
790     if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(photoDir)) {
791         LOGW("remove photo dentry dir failed, errno: %{public}d", errno);
792     }
793     if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(thumbsDir)) {
794         LOGW("remove thumbs dentry dir failed, errno: %{public}d", errno);
795     }
796 }
797 
798 template <typename RemoveFn>
LogAndDelete(const std::string & path,RemoveFn fn,const std::string & message)799 void LogAndDelete(const std::string& path, RemoveFn fn, const std::string& message)
800 {
801     int ret = fn(path.c_str());
802     if (ret != 0) {
803         LOGE("%{public}s, errno %{public}d", message.c_str(), errno);
804     }
805 }
806 
IsPhotoPath(const std::string & path)807 static bool IsPhotoPath(const std::string& path)
808 {
809     static const std::string prefix = "/storage/cloud/files/Photo/";
810     return path.length() >= prefix.length() && path.compare(0, prefix.length(), prefix) == 0;
811 }
812 
GetThumbsPath(const std::string & path)813 static std::string GetThumbsPath(const std::string& path)
814 {
815     const string str = "files/";
816     size_t len = str.size() - 1;
817     std::string newPath = "/storage/media/cloud/files/.thumbs" + path.substr(path.find(str) + len); // 待删除的文件路径
818     return newPath;
819 }
820 
GetMediaPath(const std::string & path)821 static std::string GetMediaPath(const std::string& path)
822 {
823     const string str = "storage/";
824     size_t len = str.size() - 1;
825     std::string newPath = "/storage/media" + path.substr(path.find(str) + len);
826     return newPath;
827 }
828 
CleanGalleryDentryFile(const std::string path)829 void CloudSyncManagerImpl::CleanGalleryDentryFile(const std::string path)
830 {
831     if (!IsPhotoPath(path)) {
832         LOGE("CleanGalleryDentryFile path is not photo");
833         return;
834     }
835 
836     const std::string mediaDir = GetMediaPath(path);
837     LogAndDelete(mediaDir, unlink, "fail to delete path");
838     const std::string thumbsDir = GetThumbsPath(mediaDir);
839     LogAndDelete(thumbsDir, ::remove, "fail to remove thumbsDir");
840 }
841 } // namespace OHOS::FileManagement::CloudSync
842