From c741fc9b0a5feb1773f2edb3f0ae021c15efbedb Mon Sep 17 00:00:00 2001 From: zhoulong Date: Tue, 1 Aug 2023 10:52:01 +0800 Subject: [PATCH 1/5] database Signed-off-by: zhoulong Change-Id: Ic3ee709ecfd341b062e77baad6d018058add8183 --- .../drv_bundle_state_callback.h | 2 +- .../drivers_pkg_manager/pkg_database.h | 79 ++++++ .../drivers_pkg_manager/pkg_db_helper.h | 87 ++++++ .../src/drivers_pkg_manager/BUILD.gn | 13 + .../driver_pkg_manager.cpp | 40 +-- .../drv_bundle_state_callback.cpp | 96 ++++--- .../src/drivers_pkg_manager/pkg_database.cpp | 194 +++++++++++++ .../src/drivers_pkg_manager/pkg_db.cfg | 9 + .../src/drivers_pkg_manager/pkg_db_helper.cpp | 262 ++++++++++++++++++ 9 files changed, 722 insertions(+), 60 deletions(-) create mode 100644 services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h create mode 100644 services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h index 13fff43..b63b523 100644 --- a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h @@ -78,7 +78,7 @@ public: virtual sptr AsObject() override; - bool GetAllDriverInfos(std::map &driverInfos); + bool GetAllDriverInfos(); bool CheckBundleMgrProxyPermission(); diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h new file mode 100644 index 0000000..01f005b --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h @@ -0,0 +1,79 @@ +/* + * 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 PKG_DATABASE_H +#define PKG_DATABASE_H + +#include + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +static std::string PKG_DB_PATH = "/test/"; + +constexpr const char *PKG_DB_NAME = "pkg.db"; +constexpr const char *PKG_TABLE_NAME = "pkgInfoTable"; +constexpr int32_t DATABASE_OPEN_VERSION = 1; +constexpr int32_t DATABASE_NEW_VERSION = 2; + +constexpr const char *CREATE_PKG_TABLE = "CREATE TABLE IF NOT EXISTS [pkgInfoTable](" + "[id] INTEGER PRIMARY KEY AUTOINCREMENT, " + "[bundleName] TEXT," + "[bundleAbility] TEXT," + "[driverInfo] TEXT );"; + +class PkgDataBase { +public: + static std::shared_ptr GetInstance(); + int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const OHOS::NativeRdb::RdbPredicates &predicates); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause, + const std::vector &whereArgs); + int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs); + std::shared_ptr Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns); + int32_t BeginTransaction(); + int32_t Commit(); + int32_t RollBack(); + +private: + PkgDataBase(); + DISALLOW_COPY_AND_MOVE(PkgDataBase); + + static std::shared_ptr instance_; + std::shared_ptr store_; +}; + +class PkgDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { +public: + int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; + int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; + int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USB_RIGHT_DATABASE_H diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h new file mode 100644 index 0000000..bd04ead --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h @@ -0,0 +1,87 @@ +/* + * 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 PKG_DB_HELPER_H +#define PKG_DB_HELPER_H + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "pkg_database.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +class PkgDbHelper { +public: + static std::shared_ptr GetInstance(); + /* query (user) record */ + /* query (user, device) record */ + /* query (user, device, app) record */ + /* query users */ + int32_t QueryRightRecordUids(std::vector &uids); + /* query apps */ + int32_t QueryRightRecordApps(int32_t uid, std::vector &apps); + + /* update (user, device, app) record */ + int32_t QueryAllBundleInfoNames(const std::string &bundleAbility, std::vector &bundleInfoNames); + /* add or update (user, device, app) record */ + int32_t AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo); + std::string QueryBundleInfoNames(const std::string &driverInfo); + int32_t QueryAllBundleAbilityNames(const std::string &bundleName,std::vector &bundleAbilityNames); + /* delete (user, device, app) record */ + int32_t DeleteRightRecord(const std::string &bundleName); + /* delete (user, device) record */ + int32_t DeleteDeviceRightRecord(int32_t uid, const std::string &deviceName); + /* delete (user, app) record */ + int32_t DeleteAppRightRecord(int32_t uid, const std::string &bundleName); + /* delete (user, apps) record */ + int32_t DeleteAppsRightRecord(int32_t uid, const std::vector &bundleNames); + /* delete (user) record */ + int32_t DeleteUidRightRecord(int32_t uid); + /* delete (user, time) expired record */ + int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime); + /* delete (validTime, device) record */ + int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string &deviceName); + int32_t CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleName); + int32_t QueryAllSize(std::vector &allBundleAbility); +private: + PkgDbHelper(); + DISALLOW_COPY_AND_MOVE(PkgDbHelper); + + + int32_t AddOrUpdateRightRecordEx(bool isUpdate, const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo); + int32_t QueryRightRecordCount(void); + int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues); + int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector &whereArgs); + int32_t DeleteAndNoOtherOperation(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + + static std::shared_ptr instance_; + std::mutex databaseMutex_; + std::shared_ptr rightDatabase_; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USB_RIGHT_DB_HELPER_H diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn index a5b424f..80efbcc 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn @@ -26,6 +26,8 @@ ohos_shared_library("drivers_pkg_manager") { "driver_info.cpp", "driver_pkg_manager.cpp", "drv_bundle_state_callback.cpp", + "pkg_database.cpp", + "pkg_db_helper.cpp", ] include_dirs = [ "../../include/drivers_pkg_manager", @@ -56,7 +58,18 @@ ohos_shared_library("drivers_pkg_manager") { "ipc:ipc_core", "os_account:os_account_innerkits", "samgr:samgr_proxy", + "relational_store:native_appdatafwk", + "relational_store:native_dataability", + "relational_store:native_rdb", + "init:libbegetutil", ] subsystem_name = "hdf" part_name = "external_device_manager" } + +ohos_prebuilt_etc("pkg_db.init") { + source = "pkg_db.cfg" + relative_install_dir = "init" + subsystem_name = "hdf" + part_name = "external_device_manager" +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp index ec27858..76d5c6a 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp @@ -24,6 +24,7 @@ #include "common_event_subscribe_info.h" #include "bus_extension_core.h" #include "driver_pkg_manager.h" +#include "pkg_db_helper.h" namespace OHOS { namespace ExternalDeviceManager { @@ -55,6 +56,7 @@ void DriverPkgManager::PrintTest() int32_t DriverPkgManager::Init() { + EDM_LOGE(MODULE_PKG_MGR, "begin Init 111111111"); EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); @@ -74,8 +76,7 @@ int32_t DriverPkgManager::Init() return EDM_ERR_INVALID_OBJECT; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ GetAllDriverInfos Err"); return EDM_ERR_NOT_SUPPORT; } @@ -90,39 +91,42 @@ shared_ptr DriverPkgManager::QueryMatchDriver(shared_ptr(); ret->bundleName.clear(); ret->abilityName.clear(); - if (bundleStateCallback_ == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null"); return nullptr; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err"); return nullptr; } - if (drvInfos_.empty()) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver drvInfos_ Empty"); + std::vector apps; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleInfoNames("uid", apps); + if (retRdb <= 0) { + /* error or empty record */ return nullptr; } - - for (auto [key, val] : drvInfos_) { - extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(val.GetBusName()); + int32_t totalApps = static_cast(apps.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalApps: %{public}d", totalApps); + for (int32_t i = 0; i < totalApps; i++) { + std::string app = apps.at(i); + DriverInfo driverInfo; + driverInfo.UnSerialize(app); + + extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(driverInfo.GetBusName()); if (extInstance == nullptr) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetInstance at bus:%{public}s", val.GetBusName().c_str()); - continue; + return nullptr; } - if (extInstance->MatchDriver(val, *devInfo)) { - string bundleName = key; + if (extInstance->MatchDriver(driverInfo, *devInfo)) { + std::shared_ptr helper = PkgDbHelper::GetInstance(); + + string bundleName = helper->QueryBundleInfoNames(app); ret->bundleName = bundleName.substr(0, bundleName.find_first_of(bundleStateCallback_->GetStiching())); ret->abilityName = bundleName.substr(bundleName.find_last_of(bundleStateCallback_->GetStiching()) + 1); return ret; - } else { - string drvInfoStr; - val.Serialize(drvInfoStr); - EDM_LOGI(MODULE_PKG_MGR, "PKG Serialize:%{public}s", drvInfoStr.c_str()); } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp index f3ff7dc..e520d49 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp @@ -23,6 +23,7 @@ #include "iservice_registry.h" #include "bundle_constants.h" #include "os_account_manager.h" +#include "pkg_db_helper.h" #include "hdf_log.h" #include "edm_errors.h" @@ -44,12 +45,10 @@ const string DRV_INFO_VERSION = "version"; DrvBundleStateCallback::DrvBundleStateCallback() { - allDrvInfos_.clear(); stiching.clear(); stiching += "########"; - std::map drvInfos_; - if (GetAllDriverInfos(drvInfos_)) { + if (GetAllDriverInfos()) { EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback OK"); } else { EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback ERR"); @@ -63,7 +62,14 @@ DrvBundleStateCallback::~DrvBundleStateCallback() void DrvBundleStateCallback::PrintTest() { - cout << "allDrvInfos_ size = " << allDrvInfos_.size() << endl; + std::vector allBundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t ret = helper->QueryAllSize(allBundleAbilityNames); + if (ret <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed"); + return; + } + cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl; } void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode, @@ -87,7 +93,7 @@ void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } FinishTrace(LABEL); } @@ -105,7 +111,7 @@ void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, cons } if (ParseBaseDriverInfo()) { - OnBundleDrvUpdated(BUNDLE_UPDATED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error"); } FinishTrace(LABEL); } @@ -128,10 +134,9 @@ sptr DrvBundleStateCallback::AsObject() return nullptr; } -bool DrvBundleStateCallback::GetAllDriverInfos(std::map &driverInfos) +bool DrvBundleStateCallback::GetAllDriverInfos() { if (initOnce) { - driverInfos = allDrvInfos_; return true; } @@ -139,7 +144,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri auto iBundleMgr = GetBundleMgrProxy(); if (iBundleMgr == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "Can not get iBundleMgr"); - driverInfos = allDrvInfos_; return false; } std::vector bundleInfos; @@ -149,7 +153,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA); if (!(iBundleMgr->GetBundleInfos(flags, bundleInfos, userId))) { EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfos err"); - driverInfos = allDrvInfos_; return false; } @@ -239,9 +242,6 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() string abilityName; bool ret = false; - // clear DriverInfos vector - innerDrvInfos_.clear(); - // parase infos to innerDrvInfos_ while (!extensionInfos_.empty()) { tmpDrvInfo.bus_.clear(); @@ -272,9 +272,29 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null"); continue; } + string driverInfo; + tmpDrvInfo.Serialize(driverInfo); + string tempbundleName = bundleName; bundleName += stiching + abilityName; - innerDrvInfos_[bundleName] = tmpDrvInfo; + + std::shared_ptr helper = PkgDbHelper::GetInstance(); + bool flag = true; + helper->CheckIfNeedUpdateEx(flag, bundleName); + int32_t addOrUpdate = helper->AddOrUpdateRightRecord(tempbundleName, bundleName, driverInfo); + + if (addOrUpdate < 0) { + EDM_LOGE(MODULE_PKG_MGR, "add or update failed: %{public}s, addOrUpdate=%{public}d", bundleName.c_str(), addOrUpdate); + continue; + } + + if(m_pFun != nullptr) { + if(flag) { + m_pFun(BUNDLE_UPDATED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } else { + m_pFun(BUNDLE_ADDED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } + } ret = true; } return ret; @@ -322,7 +342,6 @@ sptr DrvBundleStateCallback::GetBundleMgrProxy() void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bundleInfos) { - allDrvInfos_.clear(); while (!bundleInfos.empty()) { extensionInfos_.clear(); extensionInfos_ = bundleInfos.back().extensionInfos; @@ -332,41 +351,36 @@ void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bund } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } } } -void DrvBundleStateCallback::OnBundleDrvAdded(int bundleStatus) +void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) { - for (auto ele : innerDrvInfos_) { - allDrvInfos_[ele.first] = innerDrvInfos_[ele.first]; + std::vector bundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleAbilityNames(bundleName, bundleAbilityNames); + if (retRdb <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllBundleAbilityNames failed: %{public}s", bundleName.c_str()); + return; + } + int32_t totalNames = static_cast(bundleAbilityNames.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalbundleAbilityNames: %{public}d", totalNames); + for (int32_t i = 0; i < totalNames; i++) { if (m_pFun != nullptr) { - string bundleName = ele.first.substr(0, ele.first.find_first_of(GetStiching())); - string abilityName = ele.first.substr(ele.first.find_last_of(GetStiching()) + 1); - m_pFun(bundleStatus, BusType::BUS_TYPE_USB, bundleName, abilityName); + std::string bundleAbilityName = bundleAbilityNames.at(i); + std::string bundleName = bundleAbilityName.substr(0, bundleAbilityName.find_first_of(GetStiching())); + std::string abilityName = bundleAbilityName.substr(bundleAbilityName.find_last_of(GetStiching()) + 1); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s abilityName: %{public}s", bundleName.c_str(), abilityName.c_str()); + m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); } } -} - -void DrvBundleStateCallback::OnBundleDrvUpdated(int bundleStatus) -{ - OnBundleDrvAdded(bundleStatus); -} -void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) -{ - for (auto iter = allDrvInfos_.begin(); iter != allDrvInfos_.end();) { - if (iter->first.find(bundleName) != std::string::npos) { - if (m_pFun != nullptr) { - string abilityName = iter->first.substr(iter->first.find_last_of(GetStiching()) + 1); - EDM_LOGI(MODULE_PKG_MGR, "abilityName:%{public}s", abilityName.c_str()); - m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); - } - iter = allDrvInfos_.erase(iter); - } else { - ++iter; - } + int32_t ret = helper->DeleteRightRecord(bundleName); + if (ret < 0) { + EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str()); + return; } } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp new file mode 100644 index 0000000..a47c4fd --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp @@ -0,0 +1,194 @@ +/* + * 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 "pkg_database.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +std::shared_ptr PkgDataBase::instance_ = nullptr; + +PkgDataBase::PkgDataBase() +{ + std::string rightDatabaseName = PKG_DB_PATH + "pkg.db"; + int32_t errCode = OHOS::NativeRdb::E_OK; + OHOS::NativeRdb::RdbStoreConfig config(rightDatabaseName); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S1); + PkgDataBaseCallBack sqliteOpenHelperCallback; + store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode); + if (errCode != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore errCode :%{public}d", errCode); + } else { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore success :%{public}d", errCode); + } +} + +std::shared_ptr PkgDataBase::GetInstance() +{ + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDataBase reset to new instance"); + instance_.reset(new PkgDataBase()); + return instance_; + } + return instance_; +} + +int32_t PkgDataBase::BeginTransaction() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->BeginTransaction(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Commit() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Commit store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Commit(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::RollBack() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->RollBack(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int64_t PkgDataBase::Insert(const OHOS::NativeRdb::ValuesBucket &insertValues) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Insert store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int64_t outRowId = 0; + int32_t ret = store_->Insert(outRowId, PKG_TABLE_NAME, insertValues); + EDM_LOGI(MODULE_PKG_MGR, "Insert id=%{public}" PRIu64 "", outRowId); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return outRowId; +} + +int32_t PkgDataBase::Update( + int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const OHOS::NativeRdb::RdbPredicates &predicates) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, values, predicates); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, PKG_TABLE_NAME, values, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Delete( + int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Delete store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Delete(changedRows, PKG_TABLE_NAME, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +std::shared_ptr PkgDataBase::Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query(AbsRdbPredicates) store_ is nullptr"); + return nullptr; + } + return store_->Query(predicates, columns); +} + +int32_t PkgDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store) +{ + std::string sql = CREATE_PKG_TABLE; + int32_t ret = store.ExecuteSql(sql); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "OnCreate failed: %{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + EDM_LOGI(MODULE_PKG_MGR, "DB OnCreate Done: %{public}d", ret); + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnUpgrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnDowngrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} + +} // namespace USB +} // namespace OHOS diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg new file mode 100644 index 0000000..bd5c1b8 --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg @@ -0,0 +1,9 @@ +{ + "jobs" : [{ + "name" : "post-fs-data", + "cmds" : [ + "mkdir /data/pkg 0770 hdf_ext_devmgr hdf_ext_devmgr" + ] + } + ] +} diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp new file mode 100644 index 0000000..8e9d41c --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp @@ -0,0 +1,262 @@ +/* + * 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 "pkg_db_helper.h" + +#include + +#include "bundle_installer_interface.h" +#include "hilog_wrapper.h" +#include "pkg_database.h" + +using namespace OHOS::NativeRdb; + +namespace OHOS { +namespace ExternalDeviceManager { +std::shared_ptr PkgDbHelper::instance_; + +PkgDbHelper::PkgDbHelper() +{ + rightDatabase_ = PkgDataBase::GetInstance(); +} + +std::shared_ptr PkgDbHelper::GetInstance() +{ + static std::mutex instanceMutex; + std::lock_guard guard(instanceMutex); + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDbHelper reset to new instance"); + instance_.reset(new PkgDbHelper()); + } + return instance_; +} + +int32_t PkgDbHelper::DeleteAndNoOtherOperation( + const std::string &whereClause, const std::vector &whereArgs) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + int32_t changedRows = 0; + ret = rightDatabase_->Delete(changedRows, whereClause, whereArgs); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::DeleteRightRecord(const std::string &bundleName) +{ + std::lock_guard guard(databaseMutex_); + std::string whereClause = {"bundleName = ?"}; + std::vector whereArgs = {bundleName}; + int32_t ret = DeleteAndNoOtherOperation(whereClause, whereArgs); + if (ret != PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "failed: detale(uid, dev, app): %{public}d", ret); + } + return ret; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + int32_t ret = rightDatabase_->BeginTransaction(); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + bool isUpdate = false; + ret = CheckIfNeedUpdateEx(isUpdate, bundleAbility); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "check if need update error: %{public}d", ret); + return ret; + } + ret = AddOrUpdateRightRecordEx(isUpdate, bundleName, bundleAbility, driverInfo); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "add or update error: %{public}d", ret); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleAbility) +{ + std::vector columns; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.BeginWrap() + ->EqualTo("bundleAbility", bundleAbility) + ->EndWrap(); + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + int32_t rowCount = 0; + if (resultSet->GetRowCount(rowCount) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRowCount error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + isUpdate = (rowCount > 0 ? true : false); + + return PKG_OK; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecordEx(bool isUpdate, const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + int32_t ret = 0; + ValuesBucket values; + values.Clear(); + values.PutString("bundleName", bundleName); + values.PutString("bundleAbility", bundleAbility); + values.PutString("driverInfo", driverInfo); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s driverInfo: %{public}s", + bundleName.c_str(), driverInfo.c_str()); + if (isUpdate) { + int32_t changedRows = 0; + ret = rightDatabase_->Update(changedRows, values, "bundleAbility = ?", + std::vector {bundleAbility}); + } else { + ret = rightDatabase_->Insert(values); + } + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert or Update error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::QueryAllBundleInfoNames(const std::string &bundleAbility, std::vector &bundleInfoNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"driverInfo"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + // rdbPredicates.EqualTo("bundleAbility", bundleAbility)->Distinct(); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "driverInfo", bundleInfoNames); +} + +int32_t PkgDbHelper::QueryAllBundleAbilityNames(const std::string &bundleName,std::vector &bundleAbilityNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("bundleName", bundleName)->Distinct(); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", bundleAbilityNames); +} + +int32_t PkgDbHelper::QueryAllSize(std::vector &allBundleAbility) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", allBundleAbility); +} + +int32_t PkgDbHelper::QueryAndGetResultColumnValues(const RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + int32_t rowCount = 0; + int32_t columnIndex = 0; + if (resultSet->GetRowCount(rowCount) != E_OK || resultSet->GetColumnIndex(columnName, columnIndex) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "get table info failed"); + return PKG_RDB_EXECUTE_FAILTURE; + } + bool endFlag = false; + for (int32_t i = 0; (i < rowCount) && !endFlag; i++) { + if (resultSet->GoToRow(i) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GoToRow %{public}d", i); + return PKG_RDB_EXECUTE_FAILTURE; + } + std::string tempStr; + if (resultSet->GetString(columnIndex, tempStr) == E_OK) { + columnValues.push_back(tempStr); + } + resultSet->IsEnded(endFlag); + } + int32_t position = 0; + resultSet->GetRowIndex(position); + resultSet->IsEnded(endFlag); + EDM_LOGI(MODULE_PKG_MGR, "idx=%{public}d rows=%{public}d pos=%{public}d ret=%{public}zu end=%{public}s", + columnIndex, rowCount, position, columnValues.size(), (endFlag ? "yes" : "no")); + return columnValues.size(); +} + +std::string PkgDbHelper::QueryBundleInfoNames(const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("driverInfo", driverInfo)->Distinct(); + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return nullptr; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return nullptr; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return nullptr; + } + std::string s; + resultSet->GetString(0, s); + return s; +} + +} // namespace USB +} // namespace OHOS -- Gitee From 33c52f4642b6e336b775b432b45bb877fb199c49 Mon Sep 17 00:00:00 2001 From: zhoulong Date: Tue, 1 Aug 2023 10:59:11 +0800 Subject: [PATCH 2/5] database2 Signed-off-by: zhoulong Change-Id: I64ea95d988ab3186cdaf8c72153b949cd90ff371 --- bundle.json | 3 ++- utils/include/hilog_wrapper.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bundle.json b/bundle.json index 9d8b970..c7b8b4f 100644 --- a/bundle.json +++ b/bundle.json @@ -43,7 +43,8 @@ "safwk", "eventhandler", "ace_engine", - "access_token" + "access_token", + "relational_store" ], "third_party": [ "bounds_checking_function", diff --git a/utils/include/hilog_wrapper.h b/utils/include/hilog_wrapper.h index 58ed4dd..2167ff6 100644 --- a/utils/include/hilog_wrapper.h +++ b/utils/include/hilog_wrapper.h @@ -73,6 +73,17 @@ enum UsbMgrDomainId { EDM_BUTT, }; +enum PkgErrCode { + PKG_OK = 0, + PKG_FAILURE = -1, + PKG_RDB_EXECUTE_FAILTURE = -2, + PKG_RDB_NO_INIT = -3, + PKG_RDB_EMPTY = -4, + PKG_PERMISSION_DENIED = -5, + PKG_NOP = -6, + PKG_OVERFLOW = -7, +}; + constexpr OHOS::HiviewDFX::HiLogLabel EDM_MGR_LABEL[EDM_MODULE_BUTT] = { {LOG_CORE, EDM_FRAMEWORK_DOMAIN, "EdmFwk" }, {LOG_CORE, EDM_SERVICE_DOMAIN, "EdmService" }, -- Gitee From f2d0c49c75d816b9df55bc4b55717d85e1d3bce4 Mon Sep 17 00:00:00 2001 From: zhoulong Date: Tue, 1 Aug 2023 10:52:01 +0800 Subject: [PATCH 3/5] database Signed-off-by: zhoulong Change-Id: Ic3ee709ecfd341b062e77baad6d018058add8183 --- .../drv_bundle_state_callback.h | 2 +- .../drivers_pkg_manager/pkg_database.h | 79 ++++++ .../drivers_pkg_manager/pkg_db_helper.h | 87 ++++++ .../src/drivers_pkg_manager/BUILD.gn | 13 + .../driver_pkg_manager.cpp | 40 +-- .../drv_bundle_state_callback.cpp | 96 ++++--- .../src/drivers_pkg_manager/pkg_database.cpp | 194 +++++++++++++ .../src/drivers_pkg_manager/pkg_db.cfg | 9 + .../src/drivers_pkg_manager/pkg_db_helper.cpp | 262 ++++++++++++++++++ 9 files changed, 722 insertions(+), 60 deletions(-) create mode 100644 services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h create mode 100644 services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg create mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h index 13fff43..b63b523 100644 --- a/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/drv_bundle_state_callback.h @@ -78,7 +78,7 @@ public: virtual sptr AsObject() override; - bool GetAllDriverInfos(std::map &driverInfos); + bool GetAllDriverInfos(); bool CheckBundleMgrProxyPermission(); diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h new file mode 100644 index 0000000..01f005b --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h @@ -0,0 +1,79 @@ +/* + * 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 PKG_DATABASE_H +#define PKG_DATABASE_H + +#include + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +static std::string PKG_DB_PATH = "/test/"; + +constexpr const char *PKG_DB_NAME = "pkg.db"; +constexpr const char *PKG_TABLE_NAME = "pkgInfoTable"; +constexpr int32_t DATABASE_OPEN_VERSION = 1; +constexpr int32_t DATABASE_NEW_VERSION = 2; + +constexpr const char *CREATE_PKG_TABLE = "CREATE TABLE IF NOT EXISTS [pkgInfoTable](" + "[id] INTEGER PRIMARY KEY AUTOINCREMENT, " + "[bundleName] TEXT," + "[bundleAbility] TEXT," + "[driverInfo] TEXT );"; + +class PkgDataBase { +public: + static std::shared_ptr GetInstance(); + int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const OHOS::NativeRdb::RdbPredicates &predicates); + int32_t Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const std::string &whereClause, + const std::vector &whereArgs); + int32_t Delete(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + int32_t Delete(int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs); + std::shared_ptr Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns); + int32_t BeginTransaction(); + int32_t Commit(); + int32_t RollBack(); + +private: + PkgDataBase(); + DISALLOW_COPY_AND_MOVE(PkgDataBase); + + static std::shared_ptr instance_; + std::shared_ptr store_; +}; + +class PkgDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { +public: + int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; + int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; + int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USB_RIGHT_DATABASE_H diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h new file mode 100644 index 0000000..bd04ead --- /dev/null +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_db_helper.h @@ -0,0 +1,87 @@ +/* + * 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 PKG_DB_HELPER_H +#define PKG_DB_HELPER_H + +#include "data_ability_predicates.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_predicates.h" +#include "rdb_store.h" +#include "result_set.h" +#include "pkg_database.h" +#include "value_object.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +class PkgDbHelper { +public: + static std::shared_ptr GetInstance(); + /* query (user) record */ + /* query (user, device) record */ + /* query (user, device, app) record */ + /* query users */ + int32_t QueryRightRecordUids(std::vector &uids); + /* query apps */ + int32_t QueryRightRecordApps(int32_t uid, std::vector &apps); + + /* update (user, device, app) record */ + int32_t QueryAllBundleInfoNames(const std::string &bundleAbility, std::vector &bundleInfoNames); + /* add or update (user, device, app) record */ + int32_t AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo); + std::string QueryBundleInfoNames(const std::string &driverInfo); + int32_t QueryAllBundleAbilityNames(const std::string &bundleName,std::vector &bundleAbilityNames); + /* delete (user, device, app) record */ + int32_t DeleteRightRecord(const std::string &bundleName); + /* delete (user, device) record */ + int32_t DeleteDeviceRightRecord(int32_t uid, const std::string &deviceName); + /* delete (user, app) record */ + int32_t DeleteAppRightRecord(int32_t uid, const std::string &bundleName); + /* delete (user, apps) record */ + int32_t DeleteAppsRightRecord(int32_t uid, const std::vector &bundleNames); + /* delete (user) record */ + int32_t DeleteUidRightRecord(int32_t uid); + /* delete (user, time) expired record */ + int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime); + /* delete (validTime, device) record */ + int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string &deviceName); + int32_t CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleName); + int32_t QueryAllSize(std::vector &allBundleAbility); +private: + PkgDbHelper(); + DISALLOW_COPY_AND_MOVE(PkgDbHelper); + + + int32_t AddOrUpdateRightRecordEx(bool isUpdate, const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo); + int32_t QueryRightRecordCount(void); + int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues); + int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector &whereArgs); + int32_t DeleteAndNoOtherOperation(const OHOS::NativeRdb::RdbPredicates &rdbPredicates); + + static std::shared_ptr instance_; + std::mutex databaseMutex_; + std::shared_ptr rightDatabase_; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USB_RIGHT_DB_HELPER_H diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn index a5b424f..80efbcc 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn @@ -26,6 +26,8 @@ ohos_shared_library("drivers_pkg_manager") { "driver_info.cpp", "driver_pkg_manager.cpp", "drv_bundle_state_callback.cpp", + "pkg_database.cpp", + "pkg_db_helper.cpp", ] include_dirs = [ "../../include/drivers_pkg_manager", @@ -56,7 +58,18 @@ ohos_shared_library("drivers_pkg_manager") { "ipc:ipc_core", "os_account:os_account_innerkits", "samgr:samgr_proxy", + "relational_store:native_appdatafwk", + "relational_store:native_dataability", + "relational_store:native_rdb", + "init:libbegetutil", ] subsystem_name = "hdf" part_name = "external_device_manager" } + +ohos_prebuilt_etc("pkg_db.init") { + source = "pkg_db.cfg" + relative_install_dir = "init" + subsystem_name = "hdf" + part_name = "external_device_manager" +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp index ec27858..76d5c6a 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp @@ -24,6 +24,7 @@ #include "common_event_subscribe_info.h" #include "bus_extension_core.h" #include "driver_pkg_manager.h" +#include "pkg_db_helper.h" namespace OHOS { namespace ExternalDeviceManager { @@ -55,6 +56,7 @@ void DriverPkgManager::PrintTest() int32_t DriverPkgManager::Init() { + EDM_LOGE(MODULE_PKG_MGR, "begin Init 111111111"); EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); @@ -74,8 +76,7 @@ int32_t DriverPkgManager::Init() return EDM_ERR_INVALID_OBJECT; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "bundleStateCallback_ GetAllDriverInfos Err"); return EDM_ERR_NOT_SUPPORT; } @@ -90,39 +91,42 @@ shared_ptr DriverPkgManager::QueryMatchDriver(shared_ptr(); ret->bundleName.clear(); ret->abilityName.clear(); - if (bundleStateCallback_ == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver bundleStateCallback_ null"); return nullptr; } - std::map drvInfos_; - if (!bundleStateCallback_->GetAllDriverInfos(drvInfos_)) { + if (!bundleStateCallback_->GetAllDriverInfos()) { EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetAllDriverInfos Err"); return nullptr; } - if (drvInfos_.empty()) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver drvInfos_ Empty"); + std::vector apps; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleInfoNames("uid", apps); + if (retRdb <= 0) { + /* error or empty record */ return nullptr; } - - for (auto [key, val] : drvInfos_) { - extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(val.GetBusName()); + int32_t totalApps = static_cast(apps.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalApps: %{public}d", totalApps); + for (int32_t i = 0; i < totalApps; i++) { + std::string app = apps.at(i); + DriverInfo driverInfo; + driverInfo.UnSerialize(app); + + extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(driverInfo.GetBusName()); if (extInstance == nullptr) { - EDM_LOGE(MODULE_PKG_MGR, "QueryMatchDriver GetInstance at bus:%{public}s", val.GetBusName().c_str()); - continue; + return nullptr; } - if (extInstance->MatchDriver(val, *devInfo)) { - string bundleName = key; + if (extInstance->MatchDriver(driverInfo, *devInfo)) { + std::shared_ptr helper = PkgDbHelper::GetInstance(); + + string bundleName = helper->QueryBundleInfoNames(app); ret->bundleName = bundleName.substr(0, bundleName.find_first_of(bundleStateCallback_->GetStiching())); ret->abilityName = bundleName.substr(bundleName.find_last_of(bundleStateCallback_->GetStiching()) + 1); return ret; - } else { - string drvInfoStr; - val.Serialize(drvInfoStr); - EDM_LOGI(MODULE_PKG_MGR, "PKG Serialize:%{public}s", drvInfoStr.c_str()); } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp index f3ff7dc..e520d49 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/drv_bundle_state_callback.cpp @@ -23,6 +23,7 @@ #include "iservice_registry.h" #include "bundle_constants.h" #include "os_account_manager.h" +#include "pkg_db_helper.h" #include "hdf_log.h" #include "edm_errors.h" @@ -44,12 +45,10 @@ const string DRV_INFO_VERSION = "version"; DrvBundleStateCallback::DrvBundleStateCallback() { - allDrvInfos_.clear(); stiching.clear(); stiching += "########"; - std::map drvInfos_; - if (GetAllDriverInfos(drvInfos_)) { + if (GetAllDriverInfos()) { EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback OK"); } else { EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos in DrvBundleStateCallback ERR"); @@ -63,7 +62,14 @@ DrvBundleStateCallback::~DrvBundleStateCallback() void DrvBundleStateCallback::PrintTest() { - cout << "allDrvInfos_ size = " << allDrvInfos_.size() << endl; + std::vector allBundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t ret = helper->QueryAllSize(allBundleAbilityNames); + if (ret <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed"); + return; + } + cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl; } void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode, @@ -87,7 +93,7 @@ void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } FinishTrace(LABEL); } @@ -105,7 +111,7 @@ void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, cons } if (ParseBaseDriverInfo()) { - OnBundleDrvUpdated(BUNDLE_UPDATED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error"); } FinishTrace(LABEL); } @@ -128,10 +134,9 @@ sptr DrvBundleStateCallback::AsObject() return nullptr; } -bool DrvBundleStateCallback::GetAllDriverInfos(std::map &driverInfos) +bool DrvBundleStateCallback::GetAllDriverInfos() { if (initOnce) { - driverInfos = allDrvInfos_; return true; } @@ -139,7 +144,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri auto iBundleMgr = GetBundleMgrProxy(); if (iBundleMgr == nullptr) { EDM_LOGE(MODULE_PKG_MGR, "Can not get iBundleMgr"); - driverInfos = allDrvInfos_; return false; } std::vector bundleInfos; @@ -149,7 +153,6 @@ bool DrvBundleStateCallback::GetAllDriverInfos(std::map &dri static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA); if (!(iBundleMgr->GetBundleInfos(flags, bundleInfos, userId))) { EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfos err"); - driverInfos = allDrvInfos_; return false; } @@ -239,9 +242,6 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() string abilityName; bool ret = false; - // clear DriverInfos vector - innerDrvInfos_.clear(); - // parase infos to innerDrvInfos_ while (!extensionInfos_.empty()) { tmpDrvInfo.bus_.clear(); @@ -272,9 +272,29 @@ bool DrvBundleStateCallback::ParseBaseDriverInfo() EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null"); continue; } + string driverInfo; + tmpDrvInfo.Serialize(driverInfo); + string tempbundleName = bundleName; bundleName += stiching + abilityName; - innerDrvInfos_[bundleName] = tmpDrvInfo; + + std::shared_ptr helper = PkgDbHelper::GetInstance(); + bool flag = true; + helper->CheckIfNeedUpdateEx(flag, bundleName); + int32_t addOrUpdate = helper->AddOrUpdateRightRecord(tempbundleName, bundleName, driverInfo); + + if (addOrUpdate < 0) { + EDM_LOGE(MODULE_PKG_MGR, "add or update failed: %{public}s, addOrUpdate=%{public}d", bundleName.c_str(), addOrUpdate); + continue; + } + + if(m_pFun != nullptr) { + if(flag) { + m_pFun(BUNDLE_UPDATED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } else { + m_pFun(BUNDLE_ADDED, BusType::BUS_TYPE_USB, tempbundleName, abilityName); + } + } ret = true; } return ret; @@ -322,7 +342,6 @@ sptr DrvBundleStateCallback::GetBundleMgrProxy() void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bundleInfos) { - allDrvInfos_.clear(); while (!bundleInfos.empty()) { extensionInfos_.clear(); extensionInfos_ = bundleInfos.back().extensionInfos; @@ -332,41 +351,36 @@ void DrvBundleStateCallback::StorageHistoryDrvInfo(std::vector &bund } if (ParseBaseDriverInfo()) { - OnBundleDrvAdded(BUNDLE_ADDED); + EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error"); } } } -void DrvBundleStateCallback::OnBundleDrvAdded(int bundleStatus) +void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) { - for (auto ele : innerDrvInfos_) { - allDrvInfos_[ele.first] = innerDrvInfos_[ele.first]; + std::vector bundleAbilityNames; + std::shared_ptr helper = PkgDbHelper::GetInstance(); + int32_t retRdb = helper->QueryAllBundleAbilityNames(bundleName, bundleAbilityNames); + if (retRdb <= 0) { + EDM_LOGE(MODULE_PKG_MGR, "QueryAllBundleAbilityNames failed: %{public}s", bundleName.c_str()); + return; + } + int32_t totalNames = static_cast(bundleAbilityNames.size()); + EDM_LOGE(MODULE_PKG_MGR, "totalbundleAbilityNames: %{public}d", totalNames); + for (int32_t i = 0; i < totalNames; i++) { if (m_pFun != nullptr) { - string bundleName = ele.first.substr(0, ele.first.find_first_of(GetStiching())); - string abilityName = ele.first.substr(ele.first.find_last_of(GetStiching()) + 1); - m_pFun(bundleStatus, BusType::BUS_TYPE_USB, bundleName, abilityName); + std::string bundleAbilityName = bundleAbilityNames.at(i); + std::string bundleName = bundleAbilityName.substr(0, bundleAbilityName.find_first_of(GetStiching())); + std::string abilityName = bundleAbilityName.substr(bundleAbilityName.find_last_of(GetStiching()) + 1); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s abilityName: %{public}s", bundleName.c_str(), abilityName.c_str()); + m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); } } -} - -void DrvBundleStateCallback::OnBundleDrvUpdated(int bundleStatus) -{ - OnBundleDrvAdded(bundleStatus); -} -void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName) -{ - for (auto iter = allDrvInfos_.begin(); iter != allDrvInfos_.end();) { - if (iter->first.find(bundleName) != std::string::npos) { - if (m_pFun != nullptr) { - string abilityName = iter->first.substr(iter->first.find_last_of(GetStiching()) + 1); - EDM_LOGI(MODULE_PKG_MGR, "abilityName:%{public}s", abilityName.c_str()); - m_pFun(BUNDLE_REMOVED, BusType::BUS_TYPE_USB, bundleName, abilityName); - } - iter = allDrvInfos_.erase(iter); - } else { - ++iter; - } + int32_t ret = helper->DeleteRightRecord(bundleName); + if (ret < 0) { + EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str()); + return; } } } diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp new file mode 100644 index 0000000..a47c4fd --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_database.cpp @@ -0,0 +1,194 @@ +/* + * 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 "pkg_database.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace ExternalDeviceManager { + +std::shared_ptr PkgDataBase::instance_ = nullptr; + +PkgDataBase::PkgDataBase() +{ + std::string rightDatabaseName = PKG_DB_PATH + "pkg.db"; + int32_t errCode = OHOS::NativeRdb::E_OK; + OHOS::NativeRdb::RdbStoreConfig config(rightDatabaseName); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S1); + PkgDataBaseCallBack sqliteOpenHelperCallback; + store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode); + if (errCode != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore errCode :%{public}d", errCode); + } else { + EDM_LOGE(MODULE_PKG_MGR, "GetRdbStore success :%{public}d", errCode); + } +} + +std::shared_ptr PkgDataBase::GetInstance() +{ + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDataBase reset to new instance"); + instance_.reset(new PkgDataBase()); + return instance_; + } + return instance_; +} + +int32_t PkgDataBase::BeginTransaction() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->BeginTransaction(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Commit() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Commit store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Commit(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::RollBack() +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->RollBack(); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "RollBack fail :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int64_t PkgDataBase::Insert(const OHOS::NativeRdb::ValuesBucket &insertValues) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Insert store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int64_t outRowId = 0; + int32_t ret = store_->Insert(outRowId, PKG_TABLE_NAME, insertValues); + EDM_LOGI(MODULE_PKG_MGR, "Insert id=%{public}" PRIu64 "", outRowId); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return outRowId; +} + +int32_t PkgDataBase::Update( + int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, const OHOS::NativeRdb::RdbPredicates &predicates) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, values, predicates); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(RdbPredicates) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Update(int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, + const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Update(changedRows, PKG_TABLE_NAME, values, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Update(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +int32_t PkgDataBase::Delete( + int32_t &changedRows, const std::string &whereClause, const std::vector &whereArgs) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Delete store_ is nullptr"); + return PKG_RDB_NO_INIT; + } + int32_t ret = store_->Delete(changedRows, PKG_TABLE_NAME, whereClause, whereArgs); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete(whereClause) ret :%{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + return PKG_OK; +} + +std::shared_ptr PkgDataBase::Query( + const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector &columns) +{ + if (store_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query(AbsRdbPredicates) store_ is nullptr"); + return nullptr; + } + return store_->Query(predicates, columns); +} + +int32_t PkgDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store) +{ + std::string sql = CREATE_PKG_TABLE; + int32_t ret = store.ExecuteSql(sql); + if (ret != OHOS::NativeRdb::E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "OnCreate failed: %{public}d", ret); + return PKG_RDB_EXECUTE_FAILTURE; + } + EDM_LOGI(MODULE_PKG_MGR, "DB OnCreate Done: %{public}d", ret); + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnUpgrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} + +int32_t PkgDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) +{ + EDM_LOGI(MODULE_PKG_MGR, "DB OnDowngrade Enter"); + (void)store; + (void)oldVersion; + (void)newVersion; + return PKG_OK; +} + +} // namespace USB +} // namespace OHOS diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg new file mode 100644 index 0000000..bd5c1b8 --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg @@ -0,0 +1,9 @@ +{ + "jobs" : [{ + "name" : "post-fs-data", + "cmds" : [ + "mkdir /data/pkg 0770 hdf_ext_devmgr hdf_ext_devmgr" + ] + } + ] +} diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp new file mode 100644 index 0000000..8e9d41c --- /dev/null +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db_helper.cpp @@ -0,0 +1,262 @@ +/* + * 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 "pkg_db_helper.h" + +#include + +#include "bundle_installer_interface.h" +#include "hilog_wrapper.h" +#include "pkg_database.h" + +using namespace OHOS::NativeRdb; + +namespace OHOS { +namespace ExternalDeviceManager { +std::shared_ptr PkgDbHelper::instance_; + +PkgDbHelper::PkgDbHelper() +{ + rightDatabase_ = PkgDataBase::GetInstance(); +} + +std::shared_ptr PkgDbHelper::GetInstance() +{ + static std::mutex instanceMutex; + std::lock_guard guard(instanceMutex); + if (instance_ == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "PkgDbHelper reset to new instance"); + instance_.reset(new PkgDbHelper()); + } + return instance_; +} + +int32_t PkgDbHelper::DeleteAndNoOtherOperation( + const std::string &whereClause, const std::vector &whereArgs) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + int32_t changedRows = 0; + ret = rightDatabase_->Delete(changedRows, whereClause, whereArgs); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Delete error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::DeleteRightRecord(const std::string &bundleName) +{ + std::lock_guard guard(databaseMutex_); + std::string whereClause = {"bundleName = ?"}; + std::vector whereArgs = {bundleName}; + int32_t ret = DeleteAndNoOtherOperation(whereClause, whereArgs); + if (ret != PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "failed: detale(uid, dev, app): %{public}d", ret); + } + return ret; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecord( + const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + int32_t ret = rightDatabase_->BeginTransaction(); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + bool isUpdate = false; + ret = CheckIfNeedUpdateEx(isUpdate, bundleAbility); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "check if need update error: %{public}d", ret); + return ret; + } + ret = AddOrUpdateRightRecordEx(isUpdate, bundleName, bundleAbility, driverInfo); + + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "add or update error: %{public}d", ret); + return ret; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::CheckIfNeedUpdateEx( + bool &isUpdate, const std::string &bundleAbility) +{ + std::vector columns; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.BeginWrap() + ->EqualTo("bundleAbility", bundleAbility) + ->EndWrap(); + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + int32_t rowCount = 0; + if (resultSet->GetRowCount(rowCount) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GetRowCount error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + isUpdate = (rowCount > 0 ? true : false); + + return PKG_OK; +} + +int32_t PkgDbHelper::AddOrUpdateRightRecordEx(bool isUpdate, const std::string & bundleName, const std::string & bundleAbility, const std::string &driverInfo) +{ + int32_t ret = 0; + ValuesBucket values; + values.Clear(); + values.PutString("bundleName", bundleName); + values.PutString("bundleAbility", bundleAbility); + values.PutString("driverInfo", driverInfo); + EDM_LOGI(MODULE_PKG_MGR, "bundleName: %{public}s driverInfo: %{public}s", + bundleName.c_str(), driverInfo.c_str()); + if (isUpdate) { + int32_t changedRows = 0; + ret = rightDatabase_->Update(changedRows, values, "bundleAbility = ?", + std::vector {bundleAbility}); + } else { + ret = rightDatabase_->Insert(values); + } + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Insert or Update error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + } + return ret; +} + +int32_t PkgDbHelper::QueryAllBundleInfoNames(const std::string &bundleAbility, std::vector &bundleInfoNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"driverInfo"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + // rdbPredicates.EqualTo("bundleAbility", bundleAbility)->Distinct(); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "driverInfo", bundleInfoNames); +} + +int32_t PkgDbHelper::QueryAllBundleAbilityNames(const std::string &bundleName,std::vector &bundleAbilityNames) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("bundleName", bundleName)->Distinct(); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", bundleAbilityNames); +} + +int32_t PkgDbHelper::QueryAllSize(std::vector &allBundleAbility) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + return QueryAndGetResultColumnValues(rdbPredicates, columns, "bundleAbility", allBundleAbility); +} + +int32_t PkgDbHelper::QueryAndGetResultColumnValues(const RdbPredicates &rdbPredicates, + const std::vector &columns, const std::string &columnName, std::vector &columnValues) +{ + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return ret; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return PKG_RDB_EXECUTE_FAILTURE; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return ret; + } + int32_t rowCount = 0; + int32_t columnIndex = 0; + if (resultSet->GetRowCount(rowCount) != E_OK || resultSet->GetColumnIndex(columnName, columnIndex) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "get table info failed"); + return PKG_RDB_EXECUTE_FAILTURE; + } + bool endFlag = false; + for (int32_t i = 0; (i < rowCount) && !endFlag; i++) { + if (resultSet->GoToRow(i) != E_OK) { + EDM_LOGE(MODULE_PKG_MGR, "GoToRow %{public}d", i); + return PKG_RDB_EXECUTE_FAILTURE; + } + std::string tempStr; + if (resultSet->GetString(columnIndex, tempStr) == E_OK) { + columnValues.push_back(tempStr); + } + resultSet->IsEnded(endFlag); + } + int32_t position = 0; + resultSet->GetRowIndex(position); + resultSet->IsEnded(endFlag); + EDM_LOGI(MODULE_PKG_MGR, "idx=%{public}d rows=%{public}d pos=%{public}d ret=%{public}zu end=%{public}s", + columnIndex, rowCount, position, columnValues.size(), (endFlag ? "yes" : "no")); + return columnValues.size(); +} + +std::string PkgDbHelper::QueryBundleInfoNames(const std::string &driverInfo) +{ + std::lock_guard guard(databaseMutex_); + std::vector columns = {"bundleAbility"}; + RdbPredicates rdbPredicates(PKG_TABLE_NAME); + rdbPredicates.EqualTo("driverInfo", driverInfo)->Distinct(); + int32_t ret = rightDatabase_->BeginTransaction(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "BeginTransaction error: %{public}d", ret); + return nullptr; + } + auto resultSet = rightDatabase_->Query(rdbPredicates, columns); + if (resultSet == nullptr) { + EDM_LOGE(MODULE_PKG_MGR, "Query error"); + (void)rightDatabase_->RollBack(); + return nullptr; + } + ret = rightDatabase_->Commit(); + if (ret < PKG_OK) { + EDM_LOGE(MODULE_PKG_MGR, "Commit error: %{public}d", ret); + (void)rightDatabase_->RollBack(); + return nullptr; + } + std::string s; + resultSet->GetString(0, s); + return s; +} + +} // namespace USB +} // namespace OHOS -- Gitee From 7206829cb196d1246c7d12b1b8e13cec11e4ed9b Mon Sep 17 00:00:00 2001 From: zhoulong Date: Tue, 1 Aug 2023 10:59:11 +0800 Subject: [PATCH 4/5] database2 Signed-off-by: zhoulong Change-Id: I64ea95d988ab3186cdaf8c72153b949cd90ff371 --- bundle.json | 3 ++- utils/include/hilog_wrapper.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bundle.json b/bundle.json index 73104a4..c794248 100644 --- a/bundle.json +++ b/bundle.json @@ -43,7 +43,8 @@ "safwk", "eventhandler", "ace_engine", - "access_token" + "access_token", + "relational_store" ], "third_party": [ "bounds_checking_function", diff --git a/utils/include/hilog_wrapper.h b/utils/include/hilog_wrapper.h index 58ed4dd..2167ff6 100644 --- a/utils/include/hilog_wrapper.h +++ b/utils/include/hilog_wrapper.h @@ -73,6 +73,17 @@ enum UsbMgrDomainId { EDM_BUTT, }; +enum PkgErrCode { + PKG_OK = 0, + PKG_FAILURE = -1, + PKG_RDB_EXECUTE_FAILTURE = -2, + PKG_RDB_NO_INIT = -3, + PKG_RDB_EMPTY = -4, + PKG_PERMISSION_DENIED = -5, + PKG_NOP = -6, + PKG_OVERFLOW = -7, +}; + constexpr OHOS::HiviewDFX::HiLogLabel EDM_MGR_LABEL[EDM_MODULE_BUTT] = { {LOG_CORE, EDM_FRAMEWORK_DOMAIN, "EdmFwk" }, {LOG_CORE, EDM_SERVICE_DOMAIN, "EdmService" }, -- Gitee From 562f5d00caa4a749f85ded83ffa0f64ad5716a65 Mon Sep 17 00:00:00 2001 From: zhoulong Date: Mon, 11 Sep 2023 16:55:34 +0800 Subject: [PATCH 5/5] database 2.0 Signed-off-by: zhoulong Change-Id: Ide26bbf5b7d0deb8bbd1bfb6399946ba9cd6d608 --- sa_profile/hdf_ext_devmgr.cfg | 26 ++++++++++++++++++- .../drivers_pkg_manager/pkg_database.h | 2 +- .../src/drivers_pkg_manager/BUILD.gn | 7 ----- .../driver_pkg_manager.cpp | 1 - .../src/drivers_pkg_manager/pkg_db.cfg | 9 ------- 5 files changed, 26 insertions(+), 19 deletions(-) delete mode 100644 services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg diff --git a/sa_profile/hdf_ext_devmgr.cfg b/sa_profile/hdf_ext_devmgr.cfg index 52158de..11e4a6b 100644 --- a/sa_profile/hdf_ext_devmgr.cfg +++ b/sa_profile/hdf_ext_devmgr.cfg @@ -1,4 +1,13 @@ { + "jobs" : [{ + "name" : "services:hdf_ext_devmgr", + "cmds" : [ + "mkdir /data/service/el1/public/pkg_service 0770 hdf_ext_devmgr hdf_ext_devmgr", + "chown hdf_ext_devmgr hdf_ext_devmgr /data/service/el1/public/pkg_service", + "chmod 0770 /data/service/el1/public/pkg_service" + ] + } + ], "services" : [{ "name" : "hdf_ext_devmgr", "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"], @@ -6,8 +15,23 @@ "uid" : "hdf_ext_devmgr", "gid" : ["hdf_ext_devmgr", "shell", "uhid"], "permission" : [ - "ohos.permission.LISTEN_BUNDLE_CHANGE" + "ohos.permission.LISTEN_BUNDLE_CHANGE", + "ohos.permission.DISTRIBUTED_DATASYNC", + "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER", + "ohos.permission.INSTALL_BUNDLE", + "ohos.permission.GET_BUNDLE_INFO", + "ohos.permission.SET_TIME", + "ohos.permission.FACTORY_RESET", + "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.MANAGE_SECURE_SETTINGS", + "ohos.permission.ACCESS_CERT_MANAGER_INTERNAL", + "ohos.permission.ACCESS_CERT_MANAGER", + "ohos.permission.INSTALL_BUNDLE", + "ohos.permission.NETSYS_INTERNAL" ], + "jobs" : { + "on-start" : "services:hdf_ext_devmgr" + }, "apl" : "system_basic", "secon" : "u:r:hdf_ext_devmgr:s0" } diff --git a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h index 01f005b..d7a18c5 100644 --- a/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h +++ b/services/native/driver_extension_manager/include/drivers_pkg_manager/pkg_database.h @@ -29,7 +29,7 @@ namespace OHOS { namespace ExternalDeviceManager { -static std::string PKG_DB_PATH = "/test/"; +static std::string PKG_DB_PATH = "/data/service/el1/public/pkg_service/"; constexpr const char *PKG_DB_NAME = "pkg.db"; constexpr const char *PKG_TABLE_NAME = "pkgInfoTable"; diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn index 80efbcc..e430a52 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/BUILD.gn @@ -65,11 +65,4 @@ ohos_shared_library("drivers_pkg_manager") { ] subsystem_name = "hdf" part_name = "external_device_manager" -} - -ohos_prebuilt_etc("pkg_db.init") { - source = "pkg_db.cfg" - relative_install_dir = "init" - subsystem_name = "hdf" - part_name = "external_device_manager" } \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp index 76d5c6a..a75ef9d 100644 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp +++ b/services/native/driver_extension_manager/src/drivers_pkg_manager/driver_pkg_manager.cpp @@ -56,7 +56,6 @@ void DriverPkgManager::PrintTest() int32_t DriverPkgManager::Init() { - EDM_LOGE(MODULE_PKG_MGR, "begin Init 111111111"); EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); diff --git a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg b/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg deleted file mode 100644 index bd5c1b8..0000000 --- a/services/native/driver_extension_manager/src/drivers_pkg_manager/pkg_db.cfg +++ /dev/null @@ -1,9 +0,0 @@ -{ - "jobs" : [{ - "name" : "post-fs-data", - "cmds" : [ - "mkdir /data/pkg 0770 hdf_ext_devmgr hdf_ext_devmgr" - ] - } - ] -} -- Gitee