1 /*
2 * Copyright (c) 2021 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 "storage/storage_status_service.h"
17 #include "storage_service_errno.h"
18 #include "storage_service_log.h"
19 #include "installd_client.h"
20 #include "os_account_manager.h"
21 #include "os_account_constants.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace StorageManager {
StorageStatusService()27 StorageStatusService::StorageStatusService() {}
~StorageStatusService()28 StorageStatusService::~StorageStatusService() {}
29
GetCurrentUserId()30 int StorageStatusService::GetCurrentUserId()
31 {
32 vector<int> osAccountInfos;
33 if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountInfos) == E_OK) {
34 if (osAccountInfos.size() > 0) {
35 return osAccountInfos[0];
36 }
37 }
38 LOGE("StorageStatusService::An error occurred in querying current os account.");
39 return DEFAULT_USER_ID;
40 }
41
GetBundleStats(std::string pkgName)42 vector<int64_t> StorageStatusService::GetBundleStats(std::string pkgName)
43 {
44 vector<int64_t> result = {0, 0, 0};
45 int userId = GetCurrentUserId();
46 LOGI("StorageStatusService::userId is:%d", userId);
47 if (userId < 0 || userId > AccountSA::Constants::MAX_USER_ID) {
48 LOGI("StorageStatusService::Invaild userId.");
49 return result;
50 }
51 vector<int64_t> bundleStats;
52 int errorcode = AppExecFwk::InstalldClient::GetInstance()->GetBundleStats(pkgName, userId, bundleStats);
53 if (bundleStats.size() != dataDir.size() || errorcode != E_OK) {
54 LOGE("StorageStatusService::An error occurred in querying bundle stats.");
55 return result;
56 }
57 for (uint i = 0; i < bundleStats.size(); i++) {
58 if (bundleStats[i] == E_ERR) {
59 LOGE("StorageStatusService::Failed to query %s data.", dataDir[i].c_str());
60 bundleStats[i] = 0;
61 }
62 }
63 result[APPSIZE] = bundleStats[APP];
64 result[CACHESIZE] = bundleStats[CACHE];
65 result[DATASIZE] = bundleStats[LOCAL] + bundleStats[DISTRIBUTED] + bundleStats[DATABASE];
66 return result;
67 }
68 } // StorageManager
69 } // OHOS
70