• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const int64_t ERR_SIZE = -1;
35 } // namespace
36 
GetStorageManager()37 static sptr<StorageManager::IStorageManager> GetStorageManager()
38 {
39     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40     if (saMgr == nullptr) {
41         throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager");
42     }
43 
44     auto storageObj = saMgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
45     if (storageObj == nullptr) {
46         throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get storage manager service");
47     }
48 
49     return iface_cast<StorageManager::IStorageManager>(storageObj);
50 }
51 
GetBundleStats(const string & bundleName)52 StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName)
53 {
54     StorageManager::BundleStats bundleStats;
55     auto storageMgr = GetStorageManager();
56     if (storageMgr->GetBundleStats(bundleName, bundleStats)) {
57         throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle stats");
58     }
59     return bundleStats;
60 }
61 
GetUserStorageStats(const std::string & bundleName,int32_t userId)62 int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, int32_t userId)
63 {
64     StorageManager::StorageStats bundleStats;
65     auto storageMgr = GetStorageManager();
66     if (bundleName == MEDIA_LIBRARY_HAP) {
67         if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) {
68             HILOGE("Failed to get user media storage stats");
69             return ERR_SIZE;
70         }
71         return bundleStats.image_ + bundleStats.video_;
72     } else if (bundleName == EXTERNAL_FILE_HAP) {
73         if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) {
74             HILOGE("Failed to get user file storage stats");
75             return ERR_SIZE;
76         }
77         return bundleStats.file_;
78     }
79     return 0;
80 }
81 
UpdateMemPara(int32_t size)82 int32_t StorageMgrAdapter::UpdateMemPara(int32_t size)
83 {
84     auto storageMgr = GetStorageManager();
85     int32_t oldSize = BConstants::DEFAULT_VFS_CACHE_PRESSURE;
86     if (storageMgr->UpdateMemoryPara(size, oldSize)) {
87         HILOGE("An error occured StorageMgrAdapter UpdateMemPara");
88         return BConstants::DEFAULT_VFS_CACHE_PRESSURE;
89     }
90     return oldSize;
91 }
92 
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)93 int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames,
94     const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes,
95     std::vector<int64_t> &incPkgFileSizes)
96 {
97     auto storageMgr = GetStorageManager();
98     if (storageMgr->GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes,
99         incPkgFileSizes)) {
100         throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user storage stats");
101     }
102     return 0;
103 }
104 } // namespace OHOS::FileManagement::Backup
105