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 "storage_manager_impl.h"
17
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20
21 #include "storage_service_errno.h"
22 #include "storage_service_log.h"
23
24 using namespace OHOS::StorageManager;
25
26 namespace OHOS {
27 namespace CJStorageManager {
CjStorageStatusService()28 CjStorageStatusService::CjStorageStatusService() {}
~CjStorageStatusService()29 CjStorageStatusService::~CjStorageStatusService() {}
30
Connect()31 int32_t CjStorageStatusService::Connect()
32 {
33 LOGD("CjStorageManagerConnect::Connect start");
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (storageManager_ == nullptr) {
36 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (sam == nullptr) {
38 LOGE("CjStorageManagerConnect::Connect samgr == nullptr");
39 return E_SA_IS_NULLPTR;
40 }
41 auto object = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
42 if (object == nullptr) {
43 LOGE("CjStorageManagerConnect::Connect object == nullptr");
44 return E_REMOTE_IS_NULLPTR;
45 }
46 storageManager_ = iface_cast<StorageManager::IStorageManager>(object);
47 if (storageManager_ == nullptr) {
48 LOGE("CjStorageManagerConnect::Connect service == nullptr");
49 return E_SERVICE_IS_NULLPTR;
50 }
51 deathRecipient_ = new (std::nothrow) CjSmDeathRecipient();
52 if (!deathRecipient_) {
53 LOGE("CjStorageManagerConnect::Connect failed to create death recipient");
54 storageManager_ = nullptr;
55 return E_SERVICE_IS_NULLPTR;
56 }
57 if (storageManager_->AsObject() != nullptr) {
58 storageManager_->AsObject()->AddDeathRecipient(deathRecipient_);
59 }
60 }
61 LOGD("CjStorageManagerConnect::Connect end");
62 return E_OK;
63 }
64
GetCurrentBundleStats(BundleStats & bundleStats,uint32_t statFlag)65 int32_t CjStorageStatusService::GetCurrentBundleStats(BundleStats &bundleStats, uint32_t statFlag)
66 {
67 int32_t err = Connect();
68 if (err != E_OK) {
69 LOGE("CjStorageManagerConnect::GetCurrentBundleStats:Connect error");
70 return err;
71 }
72 if (storageManager_ == nullptr) {
73 LOGE("CjStorageManagerConnect::GetCurrentBundleStats service == nullptr");
74 return E_SERVICE_IS_NULLPTR;
75 }
76 return storageManager_->GetCurrentBundleStats(bundleStats, statFlag);
77 }
78
GetTotalSize(int64_t & totalSize)79 int32_t CjStorageStatusService::GetTotalSize(int64_t &totalSize)
80 {
81 int32_t err = Connect();
82 if (err != E_OK) {
83 LOGE("CjStorageManagerConnect::GetTotalSize:Connect error");
84 return err;
85 }
86 if (storageManager_ == nullptr) {
87 LOGE("CjStorageManagerConnect::GetTotalSize service == nullptr");
88 return E_SERVICE_IS_NULLPTR;
89 }
90 return storageManager_->GetTotalSize(totalSize);
91 }
92
GetFreeSize(int64_t & freeSize)93 int32_t CjStorageStatusService::GetFreeSize(int64_t &freeSize)
94 {
95 int32_t err = Connect();
96 if (err != E_OK) {
97 LOGE("CjStorageManagerConnect::GetFreeSize:Connect error");
98 return err;
99 }
100 if (storageManager_ == nullptr) {
101 LOGE("CjStorageManagerConnect::GetFreeSize service == nullptr");
102 return E_SERVICE_IS_NULLPTR;
103 }
104 return storageManager_->GetFreeSize(freeSize);
105 }
106
ResetProxy()107 int32_t CjStorageStatusService::ResetProxy()
108 {
109 LOGI("CjStorageManagerConnect::ResetProxy start");
110 std::lock_guard<std::mutex> lock(mutex_);
111 if ((storageManager_ != nullptr) && (storageManager_->AsObject() != nullptr)) {
112 storageManager_->AsObject()->RemoveDeathRecipient(deathRecipient_);
113 }
114 storageManager_ = nullptr;
115 return E_OK;
116 }
117
OnRemoteDied(const wptr<IRemoteObject> & remote)118 void CjSmDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
119 {
120 LOGI("SmDeathRecipient::OnRemoteDied reset proxy.");
121 auto service = DelayedSingleton<CjStorageStatusService>::GetInstance();
122 if (service == nullptr) {
123 return;
124 }
125 service->ResetProxy();
126 }
127
Convert2CjErrNum(int32_t errNum)128 int32_t Convert2CjErrNum(int32_t errNum)
129 {
130 static std::unordered_map<int32_t, int32_t> errCodeTable {
131 { E_PERMISSION_DENIED, E_PERMISSION },
132 { E_WRITE_DESCRIPTOR_ERR, E_IPCSS},
133 { E_NON_EXIST, E_NOOBJECT},
134 { E_PREPARE_DIR, E_PREPARE_DIR},
135 { E_DESTROY_DIR, E_DESTROY_DIR},
136 { E_VOL_MOUNT_ERR, E_MOUNT_ERR},
137 { E_VOL_UMOUNT_ERR, E_UNMOUNT},
138 { E_SET_POLICY, E_SET_POLICY},
139 { E_USERID_RANGE, E_OUTOFRANGE},
140 { E_VOL_STATE, E_VOLUMESTATE},
141 { E_UMOUNT_BUSY, E_VOLUMESTATE},
142 { E_NOT_SUPPORT, E_SUPPORTEDFS},
143 { E_SYS_KERNEL_ERR, E_UNMOUNT},
144 { E_NO_CHILD, E_NO_CHILD},
145 { E_WRITE_PARCEL_ERR, E_IPCSS},
146 { E_WRITE_REPLY_ERR, E_IPCSS},
147 { E_SA_IS_NULLPTR, E_IPCSS},
148 { E_REMOTE_IS_NULLPTR, E_IPCSS},
149 { E_SERVICE_IS_NULLPTR, E_IPCSS},
150 { E_BUNDLEMGR_ERROR, E_IPCSS},
151 { E_MEDIALIBRARY_ERROR, E_IPCSS},
152 };
153
154 if (errCodeTable.find(errNum) != errCodeTable.end()) {
155 return errCodeTable.at(errNum);
156 } else {
157 return errNum;
158 }
159 };
160
161 } // namespace CJStorageManager
162 } // namespace OHOS