1 /*
2 * Copyright (c) 2023-2024 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 "module_external/sms_adapter.h"
17
18 #include <refbase.h>
19
20 #include "b_error/b_error.h"
21 #include "b_resources/b_constants.h"
22 #include "filemgmt_libhilog.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28
29 namespace {
30 const string MEDIA_LIBRARY_HAP = "com.ohos.medialibrary.medialibrarydata";
31 const string EXTERNAL_FILE_HAP = "com.ohos.UserFile.ExternalFileManager";
32 const string MEDIA_TYPE = "media";
33 const string FILE_TYPE = "file";
34 } // namespace
35
GetStorageManager()36 static sptr<StorageManager::IStorageManager> GetStorageManager()
37 {
38 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 if (saMgr == nullptr) {
40 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager");
41 }
42
43 auto storageObj = saMgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
44 if (storageObj == nullptr) {
45 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get storage manager service");
46 }
47
48 return iface_cast<StorageManager::IStorageManager>(storageObj);
49 }
50
GetBundleStats(const string & bundleName)51 StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName)
52 {
53 StorageManager::BundleStats bundleStats;
54 auto storageMgr = GetStorageManager();
55 if (storageMgr->GetBundleStats(bundleName, bundleStats)) {
56 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle stats");
57 }
58 return bundleStats;
59 }
60
GetUserStorageStats(const std::string & bundleName,int32_t userId)61 int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, int32_t userId)
62 {
63 StorageManager::StorageStats bundleStats;
64 auto storageMgr = GetStorageManager();
65 if (bundleName == MEDIA_LIBRARY_HAP) {
66 if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) {
67 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user media storage stats");
68 }
69 return bundleStats.image_ + bundleStats.video_;
70 } else if (bundleName == EXTERNAL_FILE_HAP) {
71 if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) {
72 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user file storage stats");
73 }
74 return bundleStats.file_;
75 }
76 return 0;
77 }
78
UpdateMemPara(int32_t size)79 int32_t StorageMgrAdapter::UpdateMemPara(int32_t size)
80 {
81 auto storageMgr = GetStorageManager();
82 int32_t oldSize = BConstants::DEFAULT_VFS_CACHE_PRESSURE;
83 if (storageMgr->UpdateMemoryPara(size, oldSize)) {
84 HILOGE("An error occured StorageMgrAdapter UpdateMemPara");
85 return BConstants::DEFAULT_VFS_CACHE_PRESSURE;
86 }
87 return oldSize;
88 }
89
GetBundleStatsForIncrease(uint32_t userId,const std::vector<std::string> & bundleNames,const std::vector<int64_t> & incrementalBackTimes,std::vector<int64_t> & pkgFileSizes,std::vector<int64_t> & incPkgFileSizes)90 int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames,
91 const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes,
92 std::vector<int64_t> &incPkgFileSizes)
93 {
94 auto storageMgr = GetStorageManager();
95 if (storageMgr->GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes,
96 incPkgFileSizes)) {
97 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user storage stats");
98 }
99 return 0;
100 }
101 } // namespace OHOS::FileManagement::Backup
102