!1348 应用状态从kv db拆分到json db

Merge pull request !1348 from dujingcheng/bmsState
This commit is contained in:
openharmony_ci
2022-05-09 01:08:12 +00:00
committed by Gitee
28 changed files with 585 additions and 51 deletions
@@ -66,6 +66,8 @@ const std::string BUNDLE_APP_DATA_BASE_DIR = "/data/app/";
const std::string BASE = "/base/";
const std::string DATABASE = "/database/";
const std::string HAPS = "/haps/";
const std::string BUNDLE_MANAGER_SERVICE_PATH = "/data/service/el1/public/bms/bundle_manager_service";
const std::string BUNDLE_USER_INFO_PATH = BUNDLE_MANAGER_SERVICE_PATH + "/bundle_user_info.json";
const std::string DISTRIBUTED_FILE = "/data/service/el2/%/hmdfs/account/data/";
const std::string DISTRIBUTED_FILE_NON_ACCOUNT = "/data/service/el2/%/hmdfs/non_account/data/";
const std::string DISTRIBUTED_FILE_PROPERTY = "const.distributed_file_property.enabled";
@@ -29,11 +29,11 @@ struct BundleUserInfo : public Parcelable {
// Indicates whether the bundle is disabled.
bool enabled = true;
std::vector<std::string> abilities;
// disabled abilities of the user.
std::vector<std::string> disabledAbilities;
bool IsInitialState() const;
void Reset();
bool ReadFromParcel(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
static BundleUserInfo *Unmarshalling(Parcel &parcel);
@@ -30,7 +30,6 @@ namespace AppExecFwk {
namespace {
const std::string BUNDLE_USER_INFO_USER_ID = "userId";
const std::string BUNDLE_USER_INFO_ENABLE = "enabled";
const std::string BUNDLE_USER_INFO_ABILITIES = "abilities";
const std::string BUNDLE_USER_INFO_DISABLE_ABILITIES = "disabledAbilities";
} // namespace
@@ -38,12 +37,6 @@ bool BundleUserInfo::ReadFromParcel(Parcel &parcel)
{
userId = parcel.ReadInt32();
enabled = parcel.ReadBool();
int32_t abilitiesSize;
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilitiesSize);
for (int32_t i = 0; i < abilitiesSize; i++) {
abilities.emplace_back(Str16ToStr8(parcel.ReadString16()));
}
int32_t disabledAbilitiesSize;
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, disabledAbilitiesSize);
for (int32_t i = 0; i < disabledAbilitiesSize; i++) {
@@ -69,11 +62,6 @@ bool BundleUserInfo::Marshalling(Parcel &parcel) const
{
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilities.size());
for (auto &ability : abilities) {
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(ability));
}
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, disabledAbilities.size());
for (auto &disabledAbility : disabledAbilities) {
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(disabledAbility));
@@ -108,12 +96,22 @@ void BundleUserInfo::Dump(const std::string &prefix, int fd)
}
}
bool BundleUserInfo::IsInitialState() const
{
return enabled && disabledAbilities.empty();
}
void BundleUserInfo::Reset()
{
enabled = true;
disabledAbilities.clear();
}
void to_json(nlohmann::json& jsonObject, const BundleUserInfo& bundleUserInfo)
{
jsonObject = nlohmann::json {
{BUNDLE_USER_INFO_USER_ID, bundleUserInfo.userId},
{BUNDLE_USER_INFO_ENABLE, bundleUserInfo.enabled},
{BUNDLE_USER_INFO_ABILITIES, bundleUserInfo.abilities},
{BUNDLE_USER_INFO_DISABLE_ABILITIES, bundleUserInfo.disabledAbilities},
};
}
@@ -138,14 +136,6 @@ void from_json(const nlohmann::json& jsonObject, BundleUserInfo& bundleUserInfo)
false,
parseResult,
ArrayType::NOT_ARRAY);
GetValueIfFindKey<std::vector<std::string>>(jsonObject,
jsonObjectEnd,
BUNDLE_USER_INFO_ABILITIES,
bundleUserInfo.abilities,
JsonType::ARRAY,
false,
parseResult,
ArrayType::STRING);
GetValueIfFindKey<std::vector<std::string>>(jsonObject,
jsonObjectEnd,
BUNDLE_USER_INFO_DISABLE_ABILITIES,
+1
View File
@@ -144,6 +144,7 @@ ohos_shared_library("libbms") {
"src/bundle_mgr_service.cpp",
"src/bundle_mgr_service_event_handler.cpp",
"src/bundle_scanner.cpp",
"src/bundle_state_storage.cpp",
"src/bundle_status_callback_death_recipient.cpp",
"src/bundle_user_mgr_host_impl.cpp",
"src/distributed_data_storage.cpp",
@@ -31,6 +31,7 @@
#include "bundle_data_storage_interface.h"
#include "bundle_promise.h"
#include "bundle_sandbox_data_mgr.h"
#include "bundle_state_storage.h"
#include "bundle_status_callback_interface.h"
#include "common_event_manager.h"
#include "distributed_data_storage.h"
@@ -826,6 +827,9 @@ private:
void FilterExtensionAbilityInfosByModuleName(const std::string &moduleName,
std::vector<ExtensionAbilityInfo> &extensionInfos) const;
void CompatibleOldBundleStateInKvDb();
void ResetBundleStateData();
void LoadAllBundleStateDataFromJsonDb();
private:
mutable std::mutex bundleInfoMutex_;
@@ -859,6 +863,7 @@ private:
std::shared_ptr<IBundleDataStorage> dataStorage_;
std::shared_ptr<PreInstallDataStorage> preInstallDataStorage_;
std::shared_ptr<DistributedDataStorage> distributedDataStorage_;
std::shared_ptr<BundleStateStorage> bundleStateStorage_;
std::set<sptr<OnPermissionChangedCallback>> allPermissionsCallbacks_;
std::vector<PreInstallBundleInfo> preInstallBundleInfos_;
// map<uid, callback>.
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2022 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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
#include <map>
#include <mutex>
#include "bundle_user_info.h"
#include "json_serializer.h"
namespace OHOS {
namespace AppExecFwk {
class BundleStateStorage : public std::enable_shared_from_this<BundleStateStorage> {
public:
BundleStateStorage() = default;
~BundleStateStorage() = default;
/**
* @brief Judge whether json db exists. If it does not exist, create it and return the judgment result.
* @return Returns true if the json db exists; returns false otherwise.
*/
bool HasBundleUserInfoJsonDb();
/**
* @brief Load all bundles state from json db to BundleUserInfo.
* @param infos Indicates the map to save all installed bundles.
* @return Returns true if load the data successfully; returns false otherwise.
*/
bool LoadAllBundleStateData(std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos);
/**
* @brief Save the bundles state to json db.
* @param bundleName Indicates the bundleName object to be save.
* @param userId Indicates the userId object to be save.
* @param bundleUserInfo Indicates the bundleUserInfo object to be save.
* @return Returns true if save the data successfully saved; returns false otherwise.
*/
bool SaveBundleStateStorage(
const std::string bundleName, int32_t userId, const BundleUserInfo &bundleUserInfo);
/**
* @brief Save the bundles state from json db.
* @param bundleName Indicates the bundleName object to be save.
* @param userId Indicates the userId object to be save.
* @param bundleUserInfo Indicates the bundleUserInfo object to be save.
* @return Returns true if get the data successfully; returns false otherwise.
*/
bool GetBundleStateStorage(
const std::string bundleName, int32_t userId, BundleUserInfo &bundleUserInfo);
/**
* @brief Delete the bundles state from json db.
* @param bundleName Indicates the bundleName object to be save.
* @param userId Indicates the userId object to be save.
* @return Returns true if delete the data successfully; returns false otherwise.
*/
bool DeleteBundleState(const std::string bundleName, int32_t userId);
private:
bool LoadAllBundleStateDataFromJson(
nlohmann::json &jParse, std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos);
mutable std::mutex bundleStateMutex_;
};
} // namespace AppExecFwk
} // namespace OHOS
#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
+12
View File
@@ -104,6 +104,18 @@ public:
* @return Returns userId.
*/
static int32_t GetUserIdByUid(int32_t uid);
/**
* @brief Is file exist.
* @param path Indicates path.
* @return Returns result.
*/
static bool IsExistFile(const std::string &path);
/**
* @brief Is dir exist.
* @param path Indicates path.
* @return Returns result.
*/
static bool IsExistDir(const std::string &path);
static void MakeHmdfsConfig(const std::string &bundleName, int32_t bundleId);
@@ -1220,6 +1220,11 @@ public:
{
return innerBundleUserInfos_;
}
/**
* @brief Reset bundle state.
* @param userId Indicates the userId to set.
*/
void ResetBundleState(int32_t userId);
/**
* @brief Set userId to remove userinfo.
* @param userId Indicates the userId to set.
+120 -27
View File
@@ -49,6 +49,7 @@ BundleDataMgr::BundleDataMgr()
preInstallDataStorage_ = std::make_shared<PreInstallDataStorage>();
distributedDataStorage_ = DistributedDataStorage::GetInstance();
sandboxDataMgr_ = std::make_shared<BundleSandboxDataMgr>();
bundleStateStorage_ = std::make_shared<BundleStateStorage>();
APP_LOGI("BundleDataMgr instance is created");
}
@@ -63,22 +64,89 @@ BundleDataMgr::~BundleDataMgr()
bool BundleDataMgr::LoadDataFromPersistentStorage()
{
std::lock_guard<std::mutex> lock(bundleInfoMutex_);
bool ret = dataStorage_->LoadAllData(bundleInfos_);
if (ret) {
if (bundleInfos_.empty()) {
APP_LOGW("persistent data is empty");
return false;
// Judge whether bundleState json db exists.
// If it does not exist, create it and return the judgment result.
bool bundleStateDbExist = bundleStateStorage_->HasBundleUserInfoJsonDb();
if (!dataStorage_->LoadAllData(bundleInfos_)) {
APP_LOGE("LoadAllData failed");
return false;
}
if (bundleInfos_.empty()) {
APP_LOGW("persistent data is empty");
return false;
}
for (const auto &item : bundleInfos_) {
std::lock_guard<std::mutex> lock(stateMutex_);
installStates_.emplace(item.first, InstallState::INSTALL_SUCCESS);
}
LoadAllPreInstallBundleInfos(preInstallBundleInfos_);
RestoreUidAndGid();
if (!bundleStateDbExist) {
// Compatible old bundle status in kV db
CompatibleOldBundleStateInKvDb();
} else {
ResetBundleStateData();
// Load all bundle status from json db.
LoadAllBundleStateDataFromJsonDb();
}
SetInitialUserFlag(true);
return true;
}
void BundleDataMgr::CompatibleOldBundleStateInKvDb()
{
for (auto& bundleInfoItem : bundleInfos_) {
for (auto& innerBundleUserInfoItem : bundleInfoItem.second.GetInnerBundleUserInfos()) {
auto& bundleUserInfo = innerBundleUserInfoItem.second.bundleUserInfo;
if (bundleUserInfo.IsInitialState()) {
continue;
}
// save old bundle state to json db
bundleStateStorage_->SaveBundleStateStorage(
bundleInfoItem.first, bundleUserInfo.userId, bundleUserInfo);
}
for (const auto &item : bundleInfos_) {
std::lock_guard<std::mutex> lock(stateMutex_);
installStates_.emplace(item.first, InstallState::INSTALL_SUCCESS);
}
}
void BundleDataMgr::LoadAllBundleStateDataFromJsonDb()
{
APP_LOGD("Load all bundle state start");
std::map<std::string, std::map<int32_t, BundleUserInfo>> bundleStateInfos;
if (!bundleStateStorage_->LoadAllBundleStateData(bundleStateInfos) || bundleStateInfos.empty()) {
APP_LOGE("Load all bundle state failed");
return;
}
for (auto& bundleState : bundleStateInfos) {
auto infoItem = bundleInfos_.find(bundleState.first);
if (infoItem == bundleInfos_.end()) {
APP_LOGE("BundleName(%{public}s) not exist in cache", bundleState.first.c_str());
continue;
}
LoadAllPreInstallBundleInfos(preInstallBundleInfos_);
RestoreUidAndGid();
SetInitialUserFlag(true);
InnerBundleInfo& newInfo = infoItem->second;
for (auto& bundleUserState : bundleState.second) {
auto& tempUserInfo = bundleUserState.second;
newInfo.SetApplicationEnabled(tempUserInfo.enabled, bundleUserState.first);
for (auto& disabledAbility : tempUserInfo.disabledAbilities) {
newInfo.SetAbilityEnabled(bundleState.first, "", disabledAbility, false, bundleUserState.first);
}
}
}
APP_LOGD("Load all bundle state end");
}
void BundleDataMgr::ResetBundleStateData()
{
for (auto& bundleInfoItem : bundleInfos_) {
bundleInfoItem.second.ResetBundleState(Constants::ALL_USERID);
}
return ret;
}
bool BundleDataMgr::UpdateBundleInstallState(const std::string &bundleName, const InstallState state)
@@ -286,6 +354,8 @@ bool BundleDataMgr::RemoveInnerBundleUserInfo(
APP_LOGE("update storage failed bundle:%{public}s", bundleName.c_str());
return false;
}
bundleStateStorage_->DeleteBundleState(bundleName, userId);
return true;
}
@@ -1525,21 +1595,30 @@ bool BundleDataMgr::SetApplicationEnabled(const std::string &bundleName, bool is
APP_LOGE("bundleName empty");
return false;
}
std::lock_guard<std::mutex> lock(bundleInfoMutex_);
auto infoItem = bundleInfos_.find(bundleName);
if (infoItem == bundleInfos_.end()) {
APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
return false;
}
InnerBundleInfo newInfo = infoItem->second;
InnerBundleInfo& newInfo = infoItem->second;
newInfo.SetApplicationEnabled(isEnable, GetUserId(userId));
if (dataStorage_->SaveStorageBundleInfo(newInfo)) {
infoItem->second.SetApplicationEnabled(isEnable, GetUserId(userId));
return true;
} else {
APP_LOGE("bundle:%{private}s SetApplicationEnabled failed", bundleName.c_str());
InnerBundleUserInfo innerBundleUserInfo;
if (!newInfo.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
APP_LOGE("can not find userId %{public}d when get applicationInfo", userId);
return false;
}
if (innerBundleUserInfo.bundleUserInfo.IsInitialState()) {
bundleStateStorage_->DeleteBundleState(bundleName, userId);
} else {
bundleStateStorage_->SaveBundleStateStorage(
bundleName, userId, innerBundleUserInfo.bundleUserInfo);
}
return true;
}
bool BundleDataMgr::SetModuleRemovable(const std::string &bundleName, const std::string &moduleName, bool isEnable)
@@ -1621,20 +1700,34 @@ bool BundleDataMgr::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEna
APP_LOGE("bundleInfos_ data is empty");
return false;
}
APP_LOGD("SetAbilityEnabled %{public}s", abilityInfo.bundleName.c_str());
auto infoItem = bundleInfos_.find(abilityInfo.bundleName);
if (infoItem == bundleInfos_.end()) {
APP_LOGE("can not find bundle %{public}s", abilityInfo.bundleName.c_str());
return false;
}
InnerBundleInfo newInfo = infoItem->second;
newInfo.SetAbilityEnabled(abilityInfo.bundleName, abilityInfo.moduleName, abilityInfo.name,
isEnabled, GetUserId(userId));
if (dataStorage_->SaveStorageBundleInfo(newInfo)) {
return infoItem->second.SetAbilityEnabled(
abilityInfo.bundleName, abilityInfo.moduleName, abilityInfo.name, isEnabled, GetUserId(userId));
InnerBundleInfo& newInfo = infoItem->second;
if (!newInfo.SetAbilityEnabled(
abilityInfo.bundleName, abilityInfo.moduleName, abilityInfo.name, isEnabled, GetUserId(userId))) {
APP_LOGE("SetAbilityEnabled %{public}s failed", abilityInfo.bundleName.c_str());
return false;
}
APP_LOGD("dataStorage SetAbilityEnabled %{public}s failed", abilityInfo.bundleName.c_str());
return false;
InnerBundleUserInfo innerBundleUserInfo;
if (!newInfo.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
APP_LOGE("can not find userId %{public}d when get applicationInfo", userId);
return false;
}
if (innerBundleUserInfo.bundleUserInfo.IsInitialState()) {
bundleStateStorage_->DeleteBundleState(abilityInfo.bundleName, userId);
} else {
bundleStateStorage_->SaveBundleStateStorage(
abilityInfo.bundleName, userId, innerBundleUserInfo.bundleUserInfo);
}
return true;
}
#ifdef BUNDLE_FRAMEWORK_GRAPHICS
@@ -0,0 +1,288 @@
/*
* Copyright (c) 2022 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 "bundle_state_storage.h"
#include <fstream>
#include "app_log_wrapper.h"
#include "bundle_constants.h"
#include "bundle_util.h"
#include "installd_client.h"
#include "string_ex.h"
namespace OHOS {
namespace AppExecFwk {
namespace {
const std::string::size_type EXPECT_SPLIT_SIZE = 2;
void NameAndUserIdToKey(
const std::string &bundleName, int32_t userId, std::string &key)
{
key.append(bundleName);
key.append(Constants::FILE_UNDERLINE);
key.append(std::to_string(userId));
APP_LOGD("bundleName = %{public}s", bundleName.c_str());
}
bool KeyToNameAndUserId(
const std::string &key, std::string &bundleName, int32_t &userId)
{
bool ret = false;
std::vector<std::string> splitStrs;
OHOS::SplitStr(key, Constants::FILE_UNDERLINE, splitStrs);
if (splitStrs.size() == EXPECT_SPLIT_SIZE) {
bundleName = splitStrs[0];
userId = atoi(splitStrs[1].c_str());
ret = true;
}
APP_LOGD("bundleName = %{public}s", bundleName.c_str());
return ret;
}
}
bool BundleStateStorage::HasBundleUserInfoJsonDb()
{
APP_LOGD("HasBundleUserInfoJsonDb start");
if (BundleUtil::IsExistFile(Constants::BUNDLE_USER_INFO_PATH)) {
APP_LOGD("Json db exist");
return true;
}
APP_LOGD("Json db not exist, and create it");
bool isDirExist = BundleUtil::IsExistDir(Constants::BUNDLE_MANAGER_SERVICE_PATH);
if (!isDirExist) {
ErrCode result = InstalldClient::GetInstance()->CreateBundleDir(
Constants::BUNDLE_MANAGER_SERVICE_PATH);
if (result != ERR_OK) {
APP_LOGE("fail to create dir, error is %{public}d", result);
return false;
}
}
auto file = fopen(Constants::BUNDLE_USER_INFO_PATH.c_str(), "at++");
if (file == nullptr) {
APP_LOGE("create json db failed");
return false;
}
auto ret = fclose(file);
if (ret != 0) {
APP_LOGE("ret: %{public}d", ret);
}
return false;
}
bool BundleStateStorage::LoadAllBundleStateData(
std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos)
{
APP_LOGD("load all bundle state data to map");
std::lock_guard<std::mutex> lock(bundleStateMutex_);
std::fstream i(Constants::BUNDLE_USER_INFO_PATH);
nlohmann::json jParse;
if (!i.is_open()) {
APP_LOGE("failed to open bundle database file");
return false;
}
APP_LOGD("Open bundle state db file success");
i.seekg(0, std::ios::end);
int len = static_cast<int>(i.tellg());
if (len <= 0) {
APP_LOGD("Tellg failed.");
i.close();
return false;
}
i.seekg(0, std::ios::beg);
jParse = nlohmann::json::parse(i, nullptr, false);
i.close();
return LoadAllBundleStateDataFromJson(jParse, infos);
}
bool BundleStateStorage::LoadAllBundleStateDataFromJson(
nlohmann::json &jParse, std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos)
{
if (jParse.is_discarded()) {
APP_LOGE("Bad json due to jParse is discarded");
return false;
}
for (auto &item : jParse.items()) {
std::string bundleName;
int32_t userId;
if (!KeyToNameAndUserId(item.key(), bundleName, userId)) {
continue;
}
BundleUserInfo bundleUserInfo;
nlohmann::json& jsonObject = item.value();
if (jsonObject.is_discarded()) {
APP_LOGE("Load failed due to data is discarded");
continue;
}
bundleUserInfo = jsonObject.get<BundleUserInfo>();
if (infos.find(bundleName) == infos.end()) {
std::map<int32_t, BundleUserInfo> tempUser;
tempUser.try_emplace(userId, bundleUserInfo);
infos.try_emplace(bundleName, tempUser);
continue;
}
auto& bundleUserInfoMaps = infos.at(bundleName);
if (bundleUserInfoMaps.find(userId) == bundleUserInfoMaps.end()) {
bundleUserInfoMaps.try_emplace(userId, bundleUserInfo);
continue;
}
bundleUserInfoMaps.at(userId) = bundleUserInfo;
}
return !infos.empty();
}
bool BundleStateStorage::SaveBundleStateStorage(
const std::string bundleName, int32_t userId, const BundleUserInfo &bundleUserInfo)
{
APP_LOGD("Save bundle state to json db");
if (bundleName.empty() || userId < 0) {
APP_LOGE("Save bundle state failed due to param invalid.");
return false;
}
std::lock_guard<std::mutex> lock(bundleStateMutex_);
std::string appName;
NameAndUserIdToKey(bundleName, userId, appName);
std::fstream f(Constants::BUNDLE_USER_INFO_PATH);
if (!f.is_open()) {
APP_LOGE("failed to open bundle database file");
return false;
}
f.seekg(0, std::ios::end);
int len = static_cast<int>(f.tellg());
if (len == 0) {
nlohmann::json appRoot;
appRoot[appName] = bundleUserInfo;
f << std::setw(Constants::DUMP_INDENT) << appRoot << std::endl;
} else {
f.seekg(0, std::ios::beg);
nlohmann::json jsonFile;
f >> jsonFile;
jsonFile[appName] = bundleUserInfo;
f.seekp(0, std::ios::beg);
f << std::setw(Constants::DUMP_INDENT) << jsonFile << std::endl;
}
f.close();
return true;
}
bool BundleStateStorage::GetBundleStateStorage(
const std::string bundleName, int32_t userId, BundleUserInfo &bundleUserInfo)
{
if (bundleName.empty() || userId < 0) {
APP_LOGE("Get bundle state data failed due to param invalid.");
return false;
}
std::lock_guard<std::mutex> lock(bundleStateMutex_);
std::string appName;
NameAndUserIdToKey(bundleName, userId, appName);
std::fstream f(Constants::BUNDLE_USER_INFO_PATH);
if (!f.is_open()) {
APP_LOGE("failed to open bundle database file");
return false;
}
f.seekg(0, std::ios::end);
int len = static_cast<int>(f.tellg());
if (len == 0) {
f.close();
APP_LOGE("failed to open bundle database file due to tellg invalid");
return false;
}
f.seekg(0, std::ios::beg);
nlohmann::json jsonFile;
f >> jsonFile;
if (jsonFile.is_discarded()) {
f.close();
APP_LOGE("Get failed due to data is discarded");
return false;
}
bundleUserInfo = jsonFile.get<BundleUserInfo>();
f.close();
return true;
}
bool BundleStateStorage::DeleteBundleState(
const std::string bundleName, int32_t userId)
{
APP_LOGD("Delete bundle state data");
if (bundleName.empty() || userId < 0) {
APP_LOGE("Delete bundle state data failed due to param invalid.");
return false;
}
std::lock_guard<std::mutex> lock(bundleStateMutex_);
bool isEmpty = false;
std::string appName;
NameAndUserIdToKey(bundleName, userId, appName);
std::ifstream i(Constants::BUNDLE_USER_INFO_PATH);
nlohmann::json jParse;
if (!i.is_open()) {
APP_LOGE("failed to open bundle database file");
return false;
}
i.seekg(0, std::ios::end);
int len = static_cast<int>(i.tellg());
if (len == 0) {
i.close();
APP_LOGE("file is empty appName = %{private}s", appName.c_str());
return true;
}
i.seekg(0, std::ios::beg);
i >> jParse;
if (jParse.find(appName) == jParse.end()) {
i.close();
APP_LOGD("not find appName = %{public}s", appName.c_str());
return true;
}
jParse.erase(appName);
isEmpty = (jParse.size() == 0) ? true : false;
i.close();
std::ofstream o(Constants::BUNDLE_USER_INFO_PATH);
if (!o.is_open()) {
APP_LOGE("failed to open bundle database file");
return false;
}
if (!isEmpty) {
o << std::setw(Constants::DUMP_INDENT) << jParse;
}
o.close();
return true;
}
} // namespace AppExecFwk
} // namespace OHOS
+28
View File
@@ -353,5 +353,33 @@ int32_t BundleUtil::CreateFileDescriptor(const std::string &bundlePath, long lon
}
return fd;
}
bool BundleUtil::IsExistFile(const std::string &path)
{
if (path.empty()) {
return false;
}
struct stat buf = {};
if (stat(path.c_str(), &buf) != 0) {
return false;
}
return S_ISREG(buf.st_mode);
}
bool BundleUtil::IsExistDir(const std::string &path)
{
if (path.empty()) {
return false;
}
struct stat buf = {};
if (stat(path.c_str(), &buf) != 0) {
return false;
}
return S_ISDIR(buf.st_mode);
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -1907,6 +1907,25 @@ void InnerBundleInfo::GetModuleNames(std::vector<std::string> &moduleNames) cons
}
}
void InnerBundleInfo::ResetBundleState(int32_t userId)
{
if (userId == Constants::ALL_USERID) {
for (auto& innerBundleUserInfo : innerBundleUserInfos_) {
innerBundleUserInfo.second.bundleUserInfo.Reset();
}
return;
}
auto& key = NameAndUserIdToKey(GetBundleName(), userId);
if (innerBundleUserInfos_.find(key) == innerBundleUserInfos_.end()) {
APP_LOGD("no this user %{public}s", key.c_str());
return;
}
innerBundleUserInfos_.at(key).bundleUserInfo.Reset();
}
void InnerBundleInfo::RemoveInnerBundleUserInfo(int32_t userId)
{
auto& key = NameAndUserIdToKey(GetBundleName(), userId);
@@ -33,6 +33,7 @@ ohos_unittest("BmsBundleAccessTokenIdTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -34,6 +34,7 @@ ohos_unittest("BmsBundleCloneTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
"${services_path}/bundlemgr/src/hidump_helper.cpp",
@@ -33,6 +33,7 @@ ohos_unittest("BmsBundleDependenciesTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -33,6 +33,7 @@ ohos_unittest("BmsBundleInstallerTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -158,6 +159,7 @@ ohos_unittest("BmsMultipleBundleInstallerTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -33,6 +33,7 @@ ohos_unittest("BmsBundleKitServiceTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -36,6 +36,7 @@ ohos_unittest("BmsBundlePermissionGrantTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -33,6 +33,7 @@ ohos_unittest("BmsSandboxAppTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -37,6 +37,7 @@ ohos_unittest("BmsBundleUninstallerTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -40,6 +40,7 @@ ohos_unittest("BmsBundleUpdaterTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -34,6 +34,7 @@ ohos_unittest("BmsDataMgrTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -30,6 +30,7 @@ ohos_unittest("BmsServiceBundleScanTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -30,6 +30,7 @@ ohos_unittest("BmsServiceStartupTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/distributed_data_storage.cpp",
@@ -44,6 +44,7 @@ ohos_moduletest("BmsBundleInstallerModuleTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/hidump_helper.cpp",
@@ -42,6 +42,7 @@ ohos_moduletest("BmsBundleUninstallerModuleTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/hidump_helper.cpp",
@@ -35,6 +35,7 @@ ohos_moduletest("BmsServiceStartModuleTest") {
"${services_path}/bundlemgr/src/bundle_mgr_service.cpp",
"${services_path}/bundlemgr/src/bundle_mgr_service_event_handler.cpp",
"${services_path}/bundlemgr/src/bundle_scanner.cpp",
"${services_path}/bundlemgr/src/bundle_state_storage.cpp",
"${services_path}/bundlemgr/src/bundle_status_callback_death_recipient.cpp",
"${services_path}/bundlemgr/src/bundle_user_mgr_host_impl.cpp",
"${services_path}/bundlemgr/src/hidump_helper.cpp",
@@ -450,7 +450,6 @@ namespace {
{
nlohmann::json jsonObject;
BundleUserInfo bundleUserInfo;
bundleUserInfo.abilities = {"ohos.global.systemres.MainAbility"};
bundleUserInfo.disabledAbilities = {"ohos.global.systemres.MainAbility"};
for (auto _ : state) {
/* @tc.steps: step1.call to_json in loop */