• 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 "cloud_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 const int32_t AGING_DAYS = 30;
30 
GetBundleName() const31 const string &CloudSyncCore::GetBundleName() const
32 {
33     static const string emptyString = "";
34     return (bundleEntity) ? bundleEntity->bundleName_ : emptyString;
35 }
36 
Constructor()37 FsResult<CloudSyncCore *> CloudSyncCore::Constructor()
38 {
39     CloudSyncCore *cloudSyncPtr = new CloudSyncCore();
40 
41     if (cloudSyncPtr == nullptr) {
42         LOGE("Failed to create CloudSyncCore object on heap.");
43         return FsResult<CloudSyncCore *>::Error(ENOMEM);
44     }
45 
46     return FsResult<CloudSyncCore *>::Success(move(cloudSyncPtr));
47 }
48 
CloudSyncCore()49 CloudSyncCore::CloudSyncCore()
50 {
51     LOGI("init without bundle name");
52     bundleEntity = nullptr;
53 }
54 
DoOn(const string & event,const shared_ptr<CloudSyncCallbackMiddle> callback)55 FsResult<void> CloudSyncCore::DoOn(const string &event, const shared_ptr<CloudSyncCallbackMiddle> callback)
56 {
57     if (event != "progress") {
58         LOGE("event is not progress.");
59         return FsResult<void>::Error(E_PARAMS);
60     }
61 
62     if (callback_ != nullptr) {
63         LOGI("callback already exist");
64         return FsResult<void>::Success();
65     }
66 
67     string bundleName = GetBundleName();
68     callback_ = callback;
69     int32_t ret = CloudSyncManager::GetInstance().RegisterCallback(callback_, bundleName);
70     if (ret != E_OK) {
71         LOGE("DoOn Register error, result: %{public}d", ret);
72         return FsResult<void>::Error(Convert2ErrNum(ret));
73     }
74 
75     return FsResult<void>::Success();
76 }
77 
DoOff(const string & event,const optional<shared_ptr<CloudSyncCallbackMiddle>> & callback)78 FsResult<void> CloudSyncCore::DoOff(const string &event, const optional<shared_ptr<CloudSyncCallbackMiddle>> &callback)
79 {
80     if (event != "progress") {
81         LOGE("event is not progress");
82         return FsResult<void>::Error(E_PARAMS);
83     }
84 
85     string bundleName = GetBundleName();
86     int32_t ret = CloudSyncManager::GetInstance().UnRegisterCallback(bundleName);
87     if (ret != E_OK) {
88         LOGE("DoOff UnRegister error, result: %{public}d", ret);
89         return FsResult<void>::Error(Convert2ErrNum(ret));
90     }
91 
92     if (callback_ != nullptr) {
93         /* delete callback */
94         callback_->DeleteReference();
95         callback_ = nullptr;
96     }
97 
98     return FsResult<void>::Success();
99 }
100 
DoStart()101 FsResult<void> CloudSyncCore::DoStart()
102 {
103     string bundleName = GetBundleName();
104     int32_t ret = CloudSyncManager::GetInstance().StartSync(bundleName);
105     if (ret != E_OK) {
106         LOGE("Start Sync error, result: %{public}d", ret);
107         return FsResult<void>::Error(Convert2ErrNum(ret));
108     }
109 
110     return FsResult<void>::Success();
111 }
112 
DoStop()113 FsResult<void> CloudSyncCore::DoStop()
114 {
115     string bundleName = GetBundleName();
116     int32_t ret = CloudSyncManager::GetInstance().StopSync(bundleName);
117     if (ret != E_OK) {
118         LOGE("Stop Sync error, result: %{public}d", ret);
119         return FsResult<void>::Error(Convert2ErrNum(ret));
120     }
121 
122     return FsResult<void>::Success();
123 }
124 
DoOptimizeStorage()125 FsResult<void> CloudSyncCore::DoOptimizeStorage()
126 {
127     LOGI("DoOptimizeStorage enter");
128     OptimizeSpaceOptions optimizeOptions {};
129     optimizeOptions.totalSize = 0;
130     optimizeOptions.agingDays = AGING_DAYS;
131 
132     int32_t ret = CloudSyncManager::GetInstance().OptimizeStorage(optimizeOptions);
133     if (ret != E_OK) {
134         LOGE("DoOptimizeStorage error, result: %{public}d", ret);
135         return FsResult<void>::Error(Convert2ErrNum(ret));
136     }
137 
138     return FsResult<void>::Success();
139 }
140 
DoStartOptimizeStorage(const OptimizeSpaceOptions & optimizeOptions,const std::shared_ptr<CloudOptimizeCallbackMiddle> callback)141 FsResult<void> CloudSyncCore::DoStartOptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
142     const std::shared_ptr<CloudOptimizeCallbackMiddle> callback)
143 {
144     LOGI("DoStartOptimizeStorage enter");
145     int32_t ret = CloudSyncManager::GetInstance().OptimizeStorage(optimizeOptions, callback);
146     if (ret != E_OK) {
147         LOGE("DoStartOptimizeStorage error, result: %{public}d", ret);
148         return FsResult<void>::Error(Convert2ErrNum(ret));
149     }
150 
151     return FsResult<void>::Success();
152 }
153 
DoStopOptimizeStorage()154 FsResult<void> CloudSyncCore::DoStopOptimizeStorage()
155 {
156     LOGI("DoStopOptimizeStorage enter");
157     int32_t ret = CloudSyncManager::GetInstance().StopOptimizeStorage();
158     if (ret != E_OK) {
159         LOGE("DoStopOptimizeStorage error, result: %{public}d", ret);
160         return FsResult<void>::Error(Convert2ErrNum(ret));
161     }
162 
163     return FsResult<void>::Success();
164 }
165 
DoGetFileSyncState(string path)166 FsResult<int32_t> CloudSyncCore::DoGetFileSyncState(string path)
167 {
168     Uri uri(path);
169     string sandBoxPath = uri.GetPath();
170     string xattrKey = "user.cloud.filestatus";
171 
172     auto xattrValueSize = getxattr(sandBoxPath.c_str(), xattrKey.c_str(), nullptr, 0);
173     if (xattrValueSize < 0) {
174         return FsResult<int32_t>::Error(EINVAL);
175     }
176     unique_ptr<char[]> xattrValue = std::make_unique<char[]>((long)xattrValueSize + 1);
177     if (xattrValue == nullptr) {
178         return FsResult<int32_t>::Error(EINVAL);
179     }
180     xattrValueSize = getxattr(sandBoxPath.c_str(), xattrKey.c_str(), xattrValue.get(), xattrValueSize);
181     if (xattrValueSize <= 0) {
182         return FsResult<int32_t>::Error(EINVAL);
183     }
184     int32_t fileStatus = atoi(xattrValue.get());
185     int32_t val;
186     if (fileStatus == FileSync::FILESYNC_TO_BE_UPLOADED || fileStatus == FileSync::FILESYNC_UPLOADING ||
187         fileStatus == FileSync::FILESYNC_UPLOAD_FAILURE || fileStatus == FileSync::FILESYNC_UPLOAD_SUCCESS) {
188         val = statusMap[fileStatus];
189     } else {
190         val = FileSyncState::FILESYNCSTATE_COMPLETED;
191     }
192 
193     return FsResult<int32_t>::Success(val);
194 }
195 } // namespace OHOS::FileManagement::CloudSync
196