From d51f32815e6d89e32288282ac944090ea673e097 Mon Sep 17 00:00:00 2001 From: huaqingsimeng <1004904143@qq.com> Date: Wed, 29 Mar 2023 08:36:12 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BE=9Bjs=E6=8E=A5=E5=8F=A3=E8=83=BD=E5=8A=9B-=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E6=9C=8D=E5=8A=A1=E8=B0=83=E7=94=A8=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- services/backup.cfg | 3 +- .../include/module_external/bms_adapter.h | 46 ++++++++ .../include/module_external/sms_adapter.h | 34 ++++++ .../src/module_external/bms_adapter.cpp | 111 ++++++++++++++++++ .../src/module_external/sms_adapter.cpp | 52 ++++++++ 5 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 services/backup_sa/include/module_external/bms_adapter.h create mode 100644 services/backup_sa/include/module_external/sms_adapter.h create mode 100644 services/backup_sa/src/module_external/bms_adapter.cpp create mode 100644 services/backup_sa/src/module_external/sms_adapter.cpp diff --git a/services/backup.cfg b/services/backup.cfg index 84d391ce6..40c460399 100644 --- a/services/backup.cfg +++ b/services/backup.cfg @@ -5,7 +5,8 @@ "ondemand" : true, "uid" : "backup", "gid" : ["backup"], - "secon" : "u:r:backup_sa:s0" + "secon" : "u:r:backup_sa:s0", + "permission": ["ohos.permission.STORAGE_MANAGER"] } ] } \ No newline at end of file diff --git a/services/backup_sa/include/module_external/bms_adapter.h b/services/backup_sa/include/module_external/bms_adapter.h new file mode 100644 index 000000000..2c8fee0b0 --- /dev/null +++ b/services/backup_sa/include/module_external/bms_adapter.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_BUNDLE_MGR_ADAPTER_H +#define OHOS_FILEMGMT_BACKUP_BUNDLE_MGR_ADAPTER_H + +#include +#include + +#include "b_json/b_json_entity_caps.h" +#include "bundlemgr/bundle_mgr_interface.h" + +namespace OHOS::FileManagement::Backup { +class InnerReceiverImpl; + +class BundleMgrAdapter { +public: + /** + * @brief Get the Bundle Infos object + * + * @return std::vector + */ + static std::vector GetBundleInfos(); + + /** + * @brief Get the bundle infos object + * + * @param bundleNames bundle names + * @return std::vector + */ + static std::vector GetBundleInfos(const std::vector &bundleNames); +}; +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_BACKUP_BUNDLE_MGR_ADAPTER_H \ No newline at end of file diff --git a/services/backup_sa/include/module_external/sms_adapter.h b/services/backup_sa/include/module_external/sms_adapter.h new file mode 100644 index 000000000..ff59b8164 --- /dev/null +++ b/services/backup_sa/include/module_external/sms_adapter.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_STORAGE_MGR_ADAPTER_H +#define OHOS_FILEMGMT_BACKUP_STORAGE_MGR_ADAPTER_H + +#include + +#include "istorage_manager.h" + +namespace OHOS::FileManagement::Backup { +class StorageMgrAdapter { +public: + /** + * @brief Get the bundle stats object + * + * @param bundleName bundle name + */ + static StorageManager::BundleStats GetBundleStats(const std::string &bundleName); +}; +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_BACKUP_STORAGE_MGR_ADAPTER_H \ No newline at end of file diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp new file mode 100644 index 000000000..e9af164e7 --- /dev/null +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "module_external/bms_adapter.h" + +#include +#include +#include +#include + +#include "b_error/b_error.h" +#include "b_file_info.h" +#include "b_json/b_json_entity_extension_config.h" +#include "b_resources/b_constants.h" +#include "bundle_mgr_client.h" +#include "filemgmt_libhilog.h" +#include "install_param.h" +#include "iservice_registry.h" +#include "module_external/sms_adapter.h" +#include "module_ipc/service.h" +#include "module_ipc/svc_session_manager.h" +#include "module_sched/sched_scheduler.h" +#include "status_receiver_host.h" +#include "system_ability_definition.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +static sptr GetBundleManager() +{ + auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager"); + } + + auto bundleObj = saMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleObj == nullptr) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle manager service"); + } + + return iface_cast(bundleObj); +} + +static tuple GetAllowAndExtName(const vector &extensionInfos) +{ + for (auto &&ext : extensionInfos) { + if (ext.type != AppExecFwk::ExtensionAbilityType::BACKUP) { + continue; + } + vector out; + AppExecFwk::BundleMgrClient client; + if (!client.GetResConfigFile(ext, "ohos.extension.backup", out) || out.size() == 0) { + throw BError(BError::Codes::SA_INVAL_ARG, "Failed to get resconfigfile of bundle " + ext.bundleName); + } + BJsonCachedEntity cachedEntity(out[0], ext.bundleName); + auto cache = cachedEntity.Structuralize(); + return {cache.GetAllowToBackupRestore(), ext.name}; + } + return {false, ""}; +} + +vector BundleMgrAdapter::GetBundleInfos() +{ + vector bundleInfos; + vector installedBundles; + auto bms = GetBundleManager(); + if (!bms->GetBundleInfos(AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundles, + AppExecFwk::Constants::START_USERID)) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle infos"); + } + for (auto const &installedBundle : installedBundles) { + auto [allToBackup, extName] = GetAllowAndExtName(installedBundle.extensionInfos); + auto bundleStats = StorageMgrAdapter::GetBundleStats(installedBundle.name); + bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {installedBundle.name, installedBundle.versionCode, + installedBundle.versionName, bundleStats.dataSize_, + allToBackup, extName}); + } + return bundleInfos; +} + +vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames) +{ + vector bundleInfos; + auto bms = GetBundleManager(); + for (auto const &bundleName : bundleNames) { + AppExecFwk::BundleInfo installedBundle; + if (!bms->GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundle, + AppExecFwk::Constants::START_USERID)) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle info"); + } + auto [allToBackup, extName] = GetAllowAndExtName(installedBundle.extensionInfos); + auto bundleStats = StorageMgrAdapter::GetBundleStats(installedBundle.name); + bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {installedBundle.name, installedBundle.versionCode, + installedBundle.versionName, bundleStats.dataSize_, + allToBackup, extName}); + } + return bundleInfos; +} +} // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp new file mode 100644 index 000000000..ce378162b --- /dev/null +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "module_external/sms_adapter.h" + +#include + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +static sptr GetStorageManager() +{ + auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager"); + } + + auto storageObj = saMgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID); + if (storageObj == nullptr) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get storage manager service"); + } + + return iface_cast(storageObj); +} + +StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName) +{ + StorageManager::BundleStats bundleStats; + auto storageMgr = GetStorageManager(); + if (storageMgr->GetBundleStats(bundleName, bundleStats)) { + throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle stats"); + } + return bundleStats; +} +} // namespace OHOS::FileManagement::Backup -- Gitee