diff --git a/services/common/src/bundle_active_config_reader.cpp b/services/common/src/bundle_active_config_reader.cpp index 13f93cfa2f9ceeee481bcab22bc908d4b4981b33..5a8467e268fdf1a91cfd5fef468962610d49a790 100644 --- a/services/common/src/bundle_active_config_reader.cpp +++ b/services/common/src/bundle_active_config_reader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -16,8 +16,7 @@ #include "bundle_active_config_reader.h" #include "config_policy_utils.h" #include "bundle_active_log.h" -#include -#include +#include "file_ex.h" using namespace std; namespace OHOS { @@ -89,29 +88,21 @@ void BundleActiveConfigReader::LoadApplicationUsePeriodically(const char *filePa bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, Json::Value &root) { - ifstream fin; std::string realPath; if (!BundleActiveConfigReader::ConvertFullPath(filePath, realPath)) { BUNDLE_ACTIVE_LOGE("Get real path failed %{private}s", filePath); return false; } BUNDLE_ACTIVE_LOGD("Read from %{private}s", realPath.c_str()); - fin.open(realPath, ios::in); - if (!fin.is_open()) { - BUNDLE_ACTIVE_LOGE("cannot open file %{private}s", realPath.c_str()); + std::string data; + LoadStringFromFile(realPath.c_str(), data); + if (data.empty()) { return false; } - char buffer[MAX_BUFFER]; - ostringstream os; - while (fin.getline(buffer, MAX_BUFFER)) { - os << buffer; - } - string data = os.str(); JSONCPP_STRING errs; Json::CharReaderBuilder readerBuilder; const unique_ptr jsonReader(readerBuilder.newCharReader()); bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &root, &errs); - fin.close(); if (!res || !errs.empty()) { BUNDLE_ACTIVE_LOGE("parse %{private}s json error", realPath.c_str()); return false; @@ -138,5 +129,4 @@ AppUsePeriodicallyConfig BundleActiveConfigReader::GetApplicationUsePeriodically }; } // namespace DeviceUsageStats -} // namespace OHOS - +} // namespace OHOS \ No newline at end of file diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index cf03ac8bdbd0f73124ba3344644e7c378ddeec9c..2a42d0af40df5baac3564c8d4577095145d8d0b9 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -13,8 +13,7 @@ * limitations under the License. */ -#include -#include +#include "file_ex.h" #include #include #include @@ -24,9 +23,7 @@ #include #include #include - #include "time_service_client.h" - #include "bundle_active_constant.h" #include "bundle_active_open_callback.h" #include "bundle_active_log.h" @@ -38,7 +35,6 @@ #include "bundle_active_account_helper.h" #include "hisysevent.h" #include "bundle_active_core.h" - namespace OHOS { namespace DeviceUsageStats { using namespace OHOS::NativeRdb; @@ -102,6 +98,7 @@ void BundleActiveUsageDatabase::InitUsageGroupDatabase(const int32_t databaseTyp bundleActiveResult->GetRowCount(tableNumber); if (tableNumber == TABLE_NOT_EXIST) { BUNDLE_ACTIVE_LOGE("table not exist"); + bundleActiveResult->Close(); return; } int32_t tableNameIndex; @@ -124,6 +121,7 @@ void BundleActiveUsageDatabase::InitUsageGroupDatabase(const int32_t databaseTyp } } } + bundleActiveResult->Close(); } int32_t BundleActiveUsageDatabase::CreateDatabasePath() @@ -224,6 +222,7 @@ shared_ptr WEAK_FUNC BundleActiveUsageDatabase::QueryStats } else { result = rdbStore->QueryByStep(sql, selectionArgs); } + result->Close(); return result; } @@ -242,6 +241,7 @@ void BundleActiveUsageDatabase::HandleTableInfo(uint32_t databaseType) bundleActiveResult->GetRowCount(tableNumber); if (tableNumber == TABLE_NOT_EXIST) { BUNDLE_ACTIVE_LOGE("table not exist"); + bundleActiveResult->Close(); return; } int32_t tableNameIndex; @@ -262,6 +262,7 @@ void BundleActiveUsageDatabase::HandleTableInfo(uint32_t databaseType) if ((databaseType == DAILY_DATABASE_INDEX) && !sortedTableArray_.at(databaseType).empty()) { size_t lastTableIndex = sortedTableArray_.at(databaseType).size(); if (lastTableIndex == 0) { + bundleActiveResult->Close(); return; } eventBeginTime_ = sortedTableArray_.at(databaseType).at(lastTableIndex -1); @@ -272,6 +273,7 @@ void BundleActiveUsageDatabase::HandleTableInfo(uint32_t databaseType) bundleActiveResult->GetString(tableNameIndex, eventTableName_); } } + bundleActiveResult->Close(); } void BundleActiveUsageDatabase::HandleAllTableName(const uint32_t databaseType, @@ -290,6 +292,7 @@ void BundleActiveUsageDatabase::HandleAllTableName(const uint32_t databaseType, bundleActiveResult->GetRowCount(tableNumber); if (tableNumber == TABLE_NOT_EXIST) { BUNDLE_ACTIVE_LOGE("table not exist"); + bundleActiveResult->Close(); return; } int32_t tableNameIndex; @@ -300,6 +303,7 @@ void BundleActiveUsageDatabase::HandleAllTableName(const uint32_t databaseType, bundleActiveResult->GetString(tableNameIndex, tableName); allTableName.at(databaseType).push_back(tableName); } + bundleActiveResult->Close(); } void BundleActiveUsageDatabase::DeleteExcessiveTableData(uint32_t databaseType) @@ -374,6 +378,7 @@ std::unique_ptr> BundleActiveUsageDatabase::GetOverdueTable bundleActiveResult->GetRowCount(tableNumber); if (tableNumber == 0) { BUNDLE_ACTIVE_LOGE("table does not exist"); + bundleActiveResult->Close(); return nullptr; } int32_t tableNameIndex; @@ -386,6 +391,7 @@ std::unique_ptr> BundleActiveUsageDatabase::GetOverdueTable overdueTableCreateTime->push_back(ParseStartTime(tableName)); } } + bundleActiveResult->Close(); return overdueTableCreateTime; } @@ -444,12 +450,8 @@ void BundleActiveUsageDatabase::CheckDatabaseVersion() if (oldVersion != BUNDLE_ACTIVE_FAIL && oldVersion < BUNDLE_ACTIVE_CURRENT_VERSION) { UpgradleDatabase(oldVersion, BUNDLE_ACTIVE_CURRENT_VERSION); } - ofstream openVersionFile; - openVersionFile.open(BUNDLE_ACTIVE_VERSION_DIRECTORY_PATH, ios::out); - if (openVersionFile) { - openVersionFile << "version : " << BUNDLE_ACTIVE_CURRENT_VERSION; - } - openVersionFile.close(); + std::string fileVersion = "version : " + std::to_string(BUNDLE_ACTIVE_CURRENT_VERSION); + SaveStringToFile(BUNDLE_ACTIVE_VERSION_DIRECTORY_PATH, fileVersion, true); } } @@ -457,14 +459,11 @@ int32_t BundleActiveUsageDatabase::GetOldDbVersion() { int32_t oldVersion = -1; if (access(BUNDLE_ACTIVE_DATABASE_DIR.c_str(), F_OK) == 0) { - ifstream openVersionFile; - openVersionFile.open(BUNDLE_ACTIVE_VERSION_DIRECTORY_PATH, ios::in); - if (openVersionFile) { - char str[FILE_VERSION_LINE_NUM] = {0}; - openVersionFile.getline(str, FILE_VERSION_LINE_NUM); - oldVersion = GetVersionByFileInput(str); + std::string data; + LoadStringFromFile(BUNDLE_ACTIVE_VERSION_DIRECTORY_PATH.c_str(), data); + if (!data.empty()) { + oldVersion = GetVersionByFileInput(data); } - openVersionFile.close(); } return oldVersion; } @@ -568,6 +567,7 @@ void BundleActiveUsageDatabase::UpdateOldDataUid(const shared_ptrClose(); } shared_ptr WEAK_FUNC BundleActiveUsageDatabase::GetBundleActiveRdbStore(uint32_t databaseType) @@ -911,6 +911,7 @@ shared_ptr>> BundleActiveUsag int32_t tableRowNumber; bundleActiveResult->GetRowCount(tableRowNumber); if (tableRowNumber == TABLE_ROW_ZERO) { + bundleActiveResult->Close(); return nullptr; } shared_ptr>> userUsageHistory = @@ -934,6 +935,7 @@ shared_ptr>> BundleActiveUsag userUsageHistory->insert(pair>(usageHistoryKey, usageHistory)); } + bundleActiveResult->Close(); return userUsageHistory; } @@ -981,6 +983,7 @@ pair BundleActiveUsageDatabase::GetDurationData() bundleActiveResult->GetLong(BOOT_BASED_DURATION_COLUMN_INDEX, durationData.first); bundleActiveResult->GetLong(SCREEN_ON_DURATION_COLUMN_INDEX, durationData.second); } + bundleActiveResult->Close(); return durationData; } @@ -1072,6 +1075,7 @@ shared_ptr BundleActiveUsageDatabase::GetCurrentUsageDa BundleActiveBundleMgrHelper::GetInstance()->InsertPackageUid(usageStats->bundleName_, usageStats->uid_); bundleStats.insert(pair>(bundleStatsKey, usageStats)); } + bundleActiveResult->Close(); intervalStats->bundleStats_ = bundleStats; if (databaseType == DAILY_DATABASE_INDEX) { eventBeginTime_ = currentPackageTime; @@ -1514,6 +1518,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseUsageSt auto bundleActiveResult = QueryStatsInfoByStep(databaseType, queryPackageSql, queryCondition); if (bundleActiveResult == nullptr) { + bundleActiveResult->Close(); return databaseUsageStats; } int32_t tableRowNumber; @@ -1540,6 +1545,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseUsageSt usageStats.userId_ = userId; databaseUsageStats.push_back(usageStats); } + bundleActiveResult->Close(); queryCondition.clear(); } return databaseUsageStats; @@ -1590,6 +1596,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseEvents(int64_t bundleActiveResult->GetInt(EVENT_UID_COLUMN_INDEX, event.uid_); databaseEvents.push_back(event); } + bundleActiveResult->Close(); return databaseEvents; } @@ -1820,6 +1827,7 @@ void BundleActiveUsageDatabase::LoadModuleData(const int32_t userId, std::mapuid_) + " " + oneModuleRecord->moduleName_; moduleRecords[combinedInfo] = oneModuleRecord; } + moduleRecordResult->Close(); } void BundleActiveUsageDatabase::LoadFormData(const int32_t userId, std::mapsecond->formRecords_.emplace_back(oneFormRecord); } } + formRecordResult->Close(); } void BundleActiveUsageDatabase::QueryDeviceEventStats(int32_t eventId, int64_t beginTime, @@ -1888,6 +1897,7 @@ void BundleActiveUsageDatabase::QueryDeviceEventStats(int32_t eventId, int64_t b int32_t tableRowNumber; bundleActiveResult->GetRowCount(tableRowNumber); if (tableRowNumber == 0) { + bundleActiveResult->Close(); return; } BundleActiveEventStats event; @@ -1895,6 +1905,7 @@ void BundleActiveUsageDatabase::QueryDeviceEventStats(int32_t eventId, int64_t b event.count_ = tableRowNumber; event.eventId_ = eventId; eventStats.insert(std::pair(event.name_, event)); + bundleActiveResult->Close(); } std::string BundleActiveUsageDatabase::GetSystemEventName(const int32_t userId) @@ -1947,6 +1958,7 @@ void BundleActiveUsageDatabase::QueryNotificationEventStats(int32_t eventId, int int32_t tableRowNumber; bundleActiveResult->GetRowCount(tableRowNumber); if (tableRowNumber == 0) { + bundleActiveResult->Close(); return; } BundleActiveEventStats event; @@ -1964,6 +1976,7 @@ void BundleActiveUsageDatabase::QueryNotificationEventStats(int32_t eventId, int notificationEventStats.insert(std::pair(event.name_, event)); } } + bundleActiveResult->Close(); } int32_t BundleActiveUsageDatabase::JudgeQueryCondition(const int64_t beginTime, @@ -1987,4 +2000,3 @@ int32_t BundleActiveUsageDatabase::JudgeQueryCondition(const int64_t beginTime, } } // namespace DeviceUsageStats } // namespace OHOS -