• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "file_sync_core.h"
17 
18 #include <sys/types.h>
19 #include <sys/xattr.h>
20 
21 #include "cloud_sync_manager.h"
22 #include "dfs_error.h"
23 #include "uri.h"
24 #include "utils_log.h"
25 
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace std;
28 const int32_t E_PARAMS = 401;
29 
GetBundleName() const30 const string &FileSyncCore::GetBundleName() const
31 {
32     static const string emptyString = "";
33     return (bundleEntity) ? bundleEntity->bundleName_ : emptyString;
34 }
35 
Constructor(const optional<string> & bundleName)36 FsResult<FileSyncCore *> FileSyncCore::Constructor(const optional<string> &bundleName)
37 {
38     FileSyncCore *fileSyncPtr = nullptr;
39     if (bundleName.has_value()) {
40         const string &name = *bundleName;
41         if (name == "") {
42             LOGE("Failed to get bundle name");
43             return FsResult<FileSyncCore *>::Error(E_PARAMS);
44         }
45         fileSyncPtr = new FileSyncCore(name);
46     } else {
47         fileSyncPtr = new FileSyncCore();
48     }
49 
50     if (fileSyncPtr == nullptr) {
51         LOGE("Failed to create CloudSyncCore object on heap.");
52         return FsResult<FileSyncCore *>::Error(ENOMEM);
53     }
54 
55     return FsResult<FileSyncCore *>::Success(move(fileSyncPtr));
56 }
57 
FileSyncCore(const string & bundleName)58 FileSyncCore::FileSyncCore(const string &bundleName)
59 {
60     LOGI("init with bundle name");
61     bundleEntity = make_unique<BundleEntity>(bundleName);
62 }
63 
FileSyncCore()64 FileSyncCore::FileSyncCore()
65 {
66     LOGI("init without bundle name");
67     bundleEntity = nullptr;
68 }
69 
DoOn(const string & event,const shared_ptr<CloudSyncCallbackMiddle> callback)70 FsResult<void> FileSyncCore::DoOn(const string &event, const shared_ptr<CloudSyncCallbackMiddle> callback)
71 {
72     if (event != "progress") {
73         LOGE("event is not progress.");
74         return FsResult<void>::Error(E_PARAMS);
75     }
76 
77     if (callback_ != nullptr) {
78         LOGI("callback already exist");
79         return FsResult<void>::Success();
80     }
81 
82     string bundleName = GetBundleName();
83     callback_ = callback;
84     int32_t ret = CloudSyncManager::GetInstance().RegisterFileSyncCallback(callback_, bundleName);
85     if (ret != E_OK) {
86         LOGE("DoOn Register error, result: %{public}d", ret);
87         return FsResult<void>::Error(Convert2ErrNum(ret));
88     }
89 
90     return FsResult<void>::Success();
91 }
92 
DoOff(const string & event,const optional<shared_ptr<CloudSyncCallbackMiddle>> & callback)93 FsResult<void> FileSyncCore::DoOff(const string &event, const optional<shared_ptr<CloudSyncCallbackMiddle>> &callback)
94 {
95     if (event != "progress") {
96         LOGE("event is not progress");
97         return FsResult<void>::Error(E_PARAMS);
98     }
99 
100     string bundleName = GetBundleName();
101     int32_t ret = CloudSyncManager::GetInstance().UnRegisterFileSyncCallback(bundleName);
102     if (ret != E_OK) {
103         LOGE("DoOff UnRegister error, result: %{public}d", ret);
104         return FsResult<void>::Error(Convert2ErrNum(ret));
105     }
106 
107     if (callback_ != nullptr) {
108         /* delete callback */
109         callback_->DeleteReference();
110         callback_ = nullptr;
111     }
112 
113     return FsResult<void>::Success();
114 }
115 
DoStart()116 FsResult<void> FileSyncCore::DoStart()
117 {
118     string bundleName = GetBundleName();
119     int32_t ret = CloudSyncManager::GetInstance().StartFileSync(bundleName);
120     if (ret != E_OK) {
121         LOGE("Start Sync error, result: %{public}d", ret);
122         return FsResult<void>::Error(Convert2ErrNum(ret));
123     }
124 
125     return FsResult<void>::Success();
126 }
127 
DoStop()128 FsResult<void> FileSyncCore::DoStop()
129 {
130     string bundleName = GetBundleName();
131     int32_t ret = CloudSyncManager::GetInstance().StopFileSync(bundleName);
132     if (ret != E_OK) {
133         LOGE("Stop Sync error, result: %{public}d", ret);
134         return FsResult<void>::Error(Convert2ErrNum(ret));
135     }
136 
137     return FsResult<void>::Success();
138 }
139 
DoGetLastSyncTime()140 FsResult<int64_t> FileSyncCore::DoGetLastSyncTime()
141 {
142     LOGI("Start begin");
143     int64_t time = 0;
144     string bundleName = GetBundleName();
145     int32_t ret = CloudSyncManager::GetInstance().GetSyncTime(time, bundleName);
146     if (ret != E_OK) {
147         LOGE("GetLastSyncTime error, result: %{public}d", ret);
148         return FsResult<int64_t>::Error(Convert2ErrNum(ret));
149     }
150     LOGI("Start GetLastSyncTime Success!");
151     return FsResult<int64_t>::Success(time);
152 }
153 } // namespace OHOS::FileManagement::CloudSync