diff --git a/services/backup.cfg b/services/backup.cfg index 84d391ce619ed8f98cf2852ca25b7b349902923a..40c460399464eca84ba8b3836c007e4622ccd907 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 0000000000000000000000000000000000000000..2c8fee0b03a706e472032498783b8146ed4d1df0 --- /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 0000000000000000000000000000000000000000..ff59b816412865535aefd14b8a8eed7a6855117a --- /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 0000000000000000000000000000000000000000..e9af164e722ef6dedccccf413a5b91ffabc8e9e1 --- /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 0000000000000000000000000000000000000000..ce378162b219a5bfbb80c571547d2a940bc880ca --- /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