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
16 #include "optimize_storage_task.h"
17 #include "cloud_file_kit.h"
18 #include "settings_data_manager.h"
19 #include "system_load.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace FileManagement {
24 namespace CloudSync {
OptimizeStorageTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)25 OptimizeStorageTask::OptimizeStorageTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
26 : CycleTask("optimize_storage_task", {GALLERY_BUNDLE_NAME, HDC_BUNDLE_NAME}, ONE_DAY, dataSyncManager)
27 {
28 }
29
RunTaskForBundle(int32_t userId,std::string bundleName)30 int32_t OptimizeStorageTask::RunTaskForBundle(int32_t userId, std::string bundleName)
31 {
32 if (!SystemLoadStatus::IsLoadStatusUnderHot()) {
33 LOGE("OptimizeStorageTask::RunTaskForBundle system load is over hot");
34 return E_STOP;
35 }
36 auto instance = CloudFile::CloudFileKit::GetInstance();
37 if (instance == nullptr) {
38 LOGE("get cloud file helper instance failed");
39 return E_NULLPTR;
40 }
41 auto dataSyncManager_ = GetDataSyncManager();
42 if (dataSyncManager_->CleanRemainFile(bundleName, userId) != E_OK) {
43 LOGW(" clean reamin file fail");
44 }
45
46 int32_t agingDays = -1;
47 int32_t agingPolicy = -1;
48 if (bundleName == HDC_BUNDLE_NAME) {
49 agingDays = SettingsDataManager::GetLocalSpaceFreeDays();
50 agingPolicy = SettingsDataManager::GetLocalSpaceFreeStatus();
51 } else {
52 std::map<std::string, std::string> param;
53 auto ret = instance->GetAppConfigParams(userId, bundleName, param);
54 if (ret != E_OK || param.empty()) {
55 LOGE("GetAppConfigParams failed");
56 return ret;
57 }
58 agingDays = std::stoi(param["validDays"]);
59 agingPolicy = std::stoi(param["dataAgingPolicy"]);
60 }
61
62 if (agingPolicy == 0) {
63 return dataSyncManager_->OptimizeStorage(bundleName, userId, agingDays);
64 }
65 return E_OK;
66 }
67 } // namespace CloudSync
68 } // namespace FileManagement
69 } // namespace OHOS