From 1c90ab16ebc2597d2734f94e5ebfeecd739b2232 Mon Sep 17 00:00:00 2001 From: Changgyu Choi Date: Fri, 28 May 2021 10:59:50 +0900 Subject: [PATCH] Improve performance Changes: - Fix some unnecessary copy. - Add ReadyChecker for booting time. Change-Id: I8454ff5ce28946f45375451a5ee2264a3f914024 Signed-off-by: Changgyu Choi --- src/client/pkginfo_client.cc | 5 +- src/common/database/abstract_db_handler.cc | 10 +- src/common/database/abstract_db_handler.hh | 4 +- src/common/database/appinfo_db_handler.cc | 2 +- src/common/database/depinfo_db_handler.cc | 4 +- src/common/ready_checker.cc | 138 +++++++++++++++++++++ src/common/ready_checker.hh | 58 +++++++++ .../request_handler/abstract_request_handler.hh | 3 +- .../request_handler/command_request_handler.cc | 2 +- .../request_handler/command_request_handler.hh | 3 +- .../request_handler/get_appinfo_request_handler.cc | 2 +- .../request_handler/get_appinfo_request_handler.hh | 3 +- .../request_handler/get_cert_request_handler.cc | 2 +- .../request_handler/get_cert_request_handler.hh | 3 +- .../request_handler/get_depinfo_request_handler.cc | 2 +- .../request_handler/get_depinfo_request_handler.hh | 3 +- .../request_handler/get_pkginfo_request_handler.cc | 2 +- .../request_handler/get_pkginfo_request_handler.hh | 3 +- .../request_handler/query_request_handler.cc | 2 +- .../request_handler/query_request_handler.hh | 3 +- .../request_handler/set_cert_request_handler.cc | 2 +- .../request_handler/set_cert_request_handler.hh | 3 +- .../request_handler/set_pkginfo_request_handler.cc | 2 +- .../request_handler/set_pkginfo_request_handler.hh | 3 +- src/logging.hh | 2 +- src/manager/pkginfo_manager.cc | 82 ++++++------ 26 files changed, 272 insertions(+), 76 deletions(-) create mode 100644 src/common/ready_checker.cc create mode 100644 src/common/ready_checker.hh diff --git a/src/client/pkginfo_client.cc b/src/client/pkginfo_client.cc index e877a66..22c1cc0 100644 --- a/src/client/pkginfo_client.cc +++ b/src/client/pkginfo_client.cc @@ -18,7 +18,7 @@ #include "set_cert_request_handler.hh" #include "set_pkginfo_request_handler.hh" #include "system_locale.hh" - +#include "ready_checker.hh" #include "pkgmgrinfo_debug.h" namespace pkgmgr_client { @@ -38,12 +38,13 @@ PkgInfoClient::PkgInfoClient( } bool PkgInfoClient::SendRequest() { + static pkgmgr_common::ReadyChecker check_server(SERVER_READY); if (socket_ == nullptr) { LOGE("Socket is not ready"); return false; } - if (access(SERVER_READY, F_OK) != 0) { + if (!check_server.IsReady()) { LOGW("Server is not ready, try to direct access"); is_offline_ = true; return RequestHandlerDirectAccess(); diff --git a/src/common/database/abstract_db_handler.cc b/src/common/database/abstract_db_handler.cc index 80c1546..11e89e0 100644 --- a/src/common/database/abstract_db_handler.cc +++ b/src/common/database/abstract_db_handler.cc @@ -231,7 +231,7 @@ AbstractDBHandler::AbstractDBHandler(uid_t uid, int pid) { } AbstractDBHandler::~AbstractDBHandler() { - for (auto db_handle : db_handle_list_) + for (auto& db_handle : db_handle_list_) sqlite3_close_v2(db_handle.first); } @@ -256,7 +256,7 @@ bool AbstractDBHandler::Connect() { auto dbpath_list = GetDBPath(); int ret = 0; sqlite3* db; - for (auto dbpath : dbpath_list) { + for (auto& dbpath : dbpath_list) { if (op_type_ == OPERATION_TYPE_READ) { ret = __open_read_db(dbpath.first.c_str(), &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI); @@ -284,12 +284,12 @@ void AbstractDBHandler::SetOpType(OperationType type) { op_type_ = type; } -std::string AbstractDBHandler::GetLocale() { return locale_; } +const std::string& AbstractDBHandler::GetLocale() { return locale_; } int AbstractDBHandler::GetPID() { return pid_; } -void AbstractDBHandler::SetLocale(const std::string& locale) { - locale_ = locale; +void AbstractDBHandler::SetLocale(std::string locale) { + locale_ = std::move(locale); } void AbstractDBHandler::SetDBType(DBType type) { db_type_ = type; } diff --git a/src/common/database/abstract_db_handler.hh b/src/common/database/abstract_db_handler.hh index 8590405..ec4cd6c 100644 --- a/src/common/database/abstract_db_handler.hh +++ b/src/common/database/abstract_db_handler.hh @@ -48,7 +48,7 @@ class EXPORT_API AbstractDBHandler { AbstractDBHandler(uid_t uid, pid_t pid); virtual ~AbstractDBHandler(); virtual int Execute() = 0; - void SetLocale(const std::string& locale); + void SetLocale(std::string locale); void SetDBType(DBType type); void SetOpType(OperationType type); OperationType GetOpType(); @@ -57,7 +57,7 @@ class EXPORT_API AbstractDBHandler { bool Connect(); int GetPID(); std::vector> GetConnection(); - std::string GetLocale(); + const std::string& GetLocale(); static std::shared_timed_mutex lock_; private: std::vector> GetDBPath(); diff --git a/src/common/database/appinfo_db_handler.cc b/src/common/database/appinfo_db_handler.cc index ee23cc8..668de72 100644 --- a/src/common/database/appinfo_db_handler.cc +++ b/src/common/database/appinfo_db_handler.cc @@ -66,7 +66,7 @@ int AppInfoDBHandler::Execute() { NULL, __free_applications); std::vector> conn_list = GetConnection(); int ret = PMINFO_R_OK; - for (auto conn : conn_list) { + for (auto& conn : conn_list) { ret = appinfo_internal_filter_get_list(conn.first, filter_, conn.second, uid_, GetLocale().c_str(), list); if (ret == PMINFO_R_ERROR) { diff --git a/src/common/database/depinfo_db_handler.cc b/src/common/database/depinfo_db_handler.cc index c032256..7660470 100644 --- a/src/common/database/depinfo_db_handler.cc +++ b/src/common/database/depinfo_db_handler.cc @@ -47,7 +47,7 @@ int DepInfoGetDBHandler::Execute() { _LOGE("Failed to connect database"); return PMINFO_R_ERROR; } - GList *list = nullptr; + GList* list = nullptr; std::vector> conn_list = GetConnection(); int ret = PMINFO_R_OK; for (auto& conn : conn_list) { @@ -57,7 +57,7 @@ int DepInfoGetDBHandler::Execute() { break; } - for (GList *tmp = list; tmp; tmp = tmp->next) + for (GList* tmp = list; tmp; tmp = tmp->next) dependency_list_.emplace_back(reinterpret_cast(tmp->data)); g_list_free(list); diff --git a/src/common/ready_checker.cc b/src/common/ready_checker.cc new file mode 100644 index 0000000..48e4c5c --- /dev/null +++ b/src/common/ready_checker.cc @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "ready_checker.hh" + +#include + +#include "pkgmgrinfo_debug.h" + +#undef LOG_TAG +#define LOG_TAG "PKGMGR_INFO" + +namespace pkgmgr_common { + +gboolean ReadyChecker::OnReceiveEvent(GIOChannel* channel, GIOCondition cond, + gpointer user_data) { + char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event)))); + char* ptr; + ssize_t len; + struct inotify_event* event; + auto ready_checker = reinterpret_cast(user_data); + int fd = g_io_channel_unix_get_fd(channel); + while ((len = read(fd, buf, sizeof(buf))) > 0) { + for (ptr = buf; ptr < buf + len; + ptr += sizeof(struct inotify_event) + event->len) { + event = reinterpret_cast(ptr); + char* nptr = ptr + sizeof(struct inotify_event) + event->len; + if (nptr > buf + len) + break; + + if (event->len) { + if (event->mask & IN_CREATE && + ready_checker->ready_file_ == event->name) { + _LOGI("server is ready (%s)", ready_checker->ready_file_.c_str()); + ready_checker->ready_ = true; + ready_checker->Dispose(); + return G_SOURCE_CONTINUE; + } + } + } + } + + return G_SOURCE_CONTINUE; +} + +ReadyChecker::ReadyChecker(const std::string& ready_path) { + if (access(ready_path.c_str(), F_OK) == 0) { + ready_ = true; + disposed_ = true; + return; + } + auto it = ready_path.find_last_of("/"); + if (it == ready_path.npos) { + _LOGE("Invalid path %s", ready_path.c_str()); + disposed_ = true; + return; + } + + fd_ = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + if (fd_ == -1) { + _LOGE("Failed to inotify_init. errno(%d)", errno); + disposed_ = true; + return; + } + ready_path_ = ready_path.substr(0, it); + ready_file_ = ready_path.substr(it + 1); + wd_ = inotify_add_watch(fd_, ready_path_.c_str(), IN_CREATE); + if (wd_ == -1) { + _LOGE("Failed to inotify_add_watch. errno(%d)", errno); + Dispose(); + return; + } + + channel_ = g_io_channel_unix_new(fd_); + if (channel_ == nullptr) { + _LOGE("Failed to create GIO channel"); + Dispose(); + return; + } + tag_ = g_io_add_watch(channel_, G_IO_IN, OnReceiveEvent, this); + if (tag_ == 0) { + _LOGE("Failed to add watch"); + Dispose(); + return; + } + + disposed_ = false; +} + +ReadyChecker::~ReadyChecker() { + Dispose(); +} + +bool ReadyChecker::IsReady() const { + return ready_; +} + +void ReadyChecker::Dispose() { + if (disposed_) + return; + + if (tag_) { + g_source_remove(tag_); + tag_ = 0; + } + + if (channel_ != nullptr) { + g_io_channel_unref(channel_); + channel_ = nullptr; + } + + if (wd_ > 0) { + inotify_rm_watch(fd_, wd_); + wd_ = 0; + } + + if (fd_ > 0) { + close(fd_); + fd_ = 0; + } + + disposed_ = true; +} + +} // namespace pkgmgr_common diff --git a/src/common/ready_checker.hh b/src/common/ready_checker.hh new file mode 100644 index 0000000..bd6a2a0 --- /dev/null +++ b/src/common/ready_checker.hh @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 +#include +#include + +#include + +#ifndef READY_CHECKER_HH_ +#define READY_CHECKER_HH_ + +namespace pkgmgr_common { + +#ifndef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) +#endif + +class EXPORT_API ReadyChecker { + public: + explicit ReadyChecker(const std::string& ready_path); + ~ReadyChecker(); + ReadyChecker(const ReadyChecker&) = delete; + ReadyChecker& operator = (const ReadyChecker&) = delete; + bool IsReady() const; + + private: + void Dispose(); + static gboolean OnReceiveEvent(GIOChannel* channel, GIOCondition cond, + gpointer user_data); + + private: + std::string ready_path_; + std::string ready_file_; + int fd_ = 0; + int wd_ = 0; + GIOChannel* channel_ = nullptr; + int tag_ = 0; + bool ready_ = false; + bool disposed_ = false; +}; + +} // namespace pkgmgr_common + +#endif // READY_CHECKER_HH_ diff --git a/src/common/request_handler/abstract_request_handler.hh b/src/common/request_handler/abstract_request_handler.hh index cda2c99..8285e80 100644 --- a/src/common/request_handler/abstract_request_handler.hh +++ b/src/common/request_handler/abstract_request_handler.hh @@ -21,7 +21,8 @@ namespace request_handler { class EXPORT_API AbstractRequestHandler { public: virtual ~AbstractRequestHandler() = default; - virtual bool HandleRequest(unsigned char* data, int size, std::string locale) = 0; + virtual bool HandleRequest(unsigned char* data, int size, + const std::string& locale) = 0; virtual std::vector ExtractResult() = 0; diff --git a/src/common/request_handler/command_request_handler.cc b/src/common/request_handler/command_request_handler.cc index b48a64a..f10fcd1 100644 --- a/src/common/request_handler/command_request_handler.cc +++ b/src/common/request_handler/command_request_handler.cc @@ -19,7 +19,7 @@ namespace pkgmgr_server { namespace request_handler { bool CommandRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/command_request_handler.hh b/src/common/request_handler/command_request_handler.hh index 04d7276..aa4cb4b 100644 --- a/src/common/request_handler/command_request_handler.hh +++ b/src/common/request_handler/command_request_handler.hh @@ -20,7 +20,8 @@ namespace request_handler { class EXPORT_API CommandRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/get_appinfo_request_handler.cc b/src/common/request_handler/get_appinfo_request_handler.cc index db17565..f4428ee 100644 --- a/src/common/request_handler/get_appinfo_request_handler.cc +++ b/src/common/request_handler/get_appinfo_request_handler.cc @@ -19,7 +19,7 @@ namespace pkgmgr_server { namespace request_handler { bool GetAppinfoRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/get_appinfo_request_handler.hh b/src/common/request_handler/get_appinfo_request_handler.hh index 56ea25e..8fac2d9 100644 --- a/src/common/request_handler/get_appinfo_request_handler.hh +++ b/src/common/request_handler/get_appinfo_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API GetAppinfoRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/get_cert_request_handler.cc b/src/common/request_handler/get_cert_request_handler.cc index 3857924..a42d216 100644 --- a/src/common/request_handler/get_cert_request_handler.cc +++ b/src/common/request_handler/get_cert_request_handler.cc @@ -18,7 +18,7 @@ namespace pkgmgr_server { namespace request_handler { bool GetCertRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/get_cert_request_handler.hh b/src/common/request_handler/get_cert_request_handler.hh index 45a95c4..4f9d9af 100644 --- a/src/common/request_handler/get_cert_request_handler.hh +++ b/src/common/request_handler/get_cert_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API GetCertRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/get_depinfo_request_handler.cc b/src/common/request_handler/get_depinfo_request_handler.cc index d820b72..6c1a26c 100644 --- a/src/common/request_handler/get_depinfo_request_handler.cc +++ b/src/common/request_handler/get_depinfo_request_handler.cc @@ -19,7 +19,7 @@ namespace pkgmgr_server { namespace request_handler { bool GetDepinfoRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/get_depinfo_request_handler.hh b/src/common/request_handler/get_depinfo_request_handler.hh index d0c9603..44bb7a9 100644 --- a/src/common/request_handler/get_depinfo_request_handler.hh +++ b/src/common/request_handler/get_depinfo_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API GetDepinfoRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/get_pkginfo_request_handler.cc b/src/common/request_handler/get_pkginfo_request_handler.cc index cdd166c..1630ea3 100644 --- a/src/common/request_handler/get_pkginfo_request_handler.cc +++ b/src/common/request_handler/get_pkginfo_request_handler.cc @@ -20,7 +20,7 @@ namespace pkgmgr_server { namespace request_handler { bool GetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/get_pkginfo_request_handler.hh b/src/common/request_handler/get_pkginfo_request_handler.hh index 037b0f8..0bf022f 100644 --- a/src/common/request_handler/get_pkginfo_request_handler.hh +++ b/src/common/request_handler/get_pkginfo_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API GetPkginfoRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/query_request_handler.cc b/src/common/request_handler/query_request_handler.cc index ea1a73c..7e5769a 100644 --- a/src/common/request_handler/query_request_handler.cc +++ b/src/common/request_handler/query_request_handler.cc @@ -19,7 +19,7 @@ namespace pkgmgr_server { namespace request_handler { bool QueryRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/query_request_handler.hh b/src/common/request_handler/query_request_handler.hh index 6799f84..7d6d267 100644 --- a/src/common/request_handler/query_request_handler.hh +++ b/src/common/request_handler/query_request_handler.hh @@ -20,7 +20,8 @@ namespace request_handler { class EXPORT_API QueryRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/set_cert_request_handler.cc b/src/common/request_handler/set_cert_request_handler.cc index 9ac455e..82fe6e4 100644 --- a/src/common/request_handler/set_cert_request_handler.cc +++ b/src/common/request_handler/set_cert_request_handler.cc @@ -19,7 +19,7 @@ namespace pkgmgr_server { namespace request_handler { bool SetCertRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/set_cert_request_handler.hh b/src/common/request_handler/set_cert_request_handler.hh index 477852e..dae2054 100644 --- a/src/common/request_handler/set_cert_request_handler.hh +++ b/src/common/request_handler/set_cert_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API SetCertRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/common/request_handler/set_pkginfo_request_handler.cc b/src/common/request_handler/set_pkginfo_request_handler.cc index 50837e4..cf04ccf 100644 --- a/src/common/request_handler/set_pkginfo_request_handler.cc +++ b/src/common/request_handler/set_pkginfo_request_handler.cc @@ -21,7 +21,7 @@ namespace pkgmgr_server { namespace request_handler { bool SetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size, - std::string locale) { + const std::string& locale) { auto abstract_parcel = pcp::ParcelableFactory::GetInst().CreateParcel(data, size); diff --git a/src/common/request_handler/set_pkginfo_request_handler.hh b/src/common/request_handler/set_pkginfo_request_handler.hh index 7fb1efd..8006920 100644 --- a/src/common/request_handler/set_pkginfo_request_handler.hh +++ b/src/common/request_handler/set_pkginfo_request_handler.hh @@ -19,7 +19,8 @@ namespace request_handler { class EXPORT_API SetPkginfoRequestHandler : public AbstractRequestHandler { public: - bool HandleRequest(unsigned char* data, int size, std::string locale) override; + bool HandleRequest(unsigned char* data, int size, + const std::string& locale) override; std::vector ExtractResult() override; diff --git a/src/logging.hh b/src/logging.hh index ddce05a..318c2d8 100644 --- a/src/logging.hh +++ b/src/logging.hh @@ -115,7 +115,7 @@ class LogCore { } void Log(LogLevel level, const std::string& tag, const std::string& log) { - for (auto backend : backend_list_) + for (auto& backend : backend_list_) backend->WriteLog(level, tag, log); } diff --git a/src/manager/pkginfo_manager.cc b/src/manager/pkginfo_manager.cc index e10d19d..218f29c 100644 --- a/src/manager/pkginfo_manager.cc +++ b/src/manager/pkginfo_manager.cc @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -96,7 +97,7 @@ extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid, LOG(DEBUG) << "No packages meets given condition for user " << uid; return PMINFO_R_ENOENT; } - for (auto &pkginfo : result_list) + for (auto pkginfo : result_list) g_hash_table_insert(packages, (gpointer)pkginfo->package, (gpointer)pkginfo); @@ -133,7 +134,7 @@ extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid, std::static_pointer_cast(ptr)); auto dependency_list = return_parcel->ExtractDependencyInfo(); - for (auto &dependency : dependency_list) + for (auto dependency : dependency_list) *dependencies = g_list_prepend(*dependencies, dependency); return PMINFO_R_OK; } @@ -214,10 +215,10 @@ extern "C" EXPORT_API char* _appinfo_get_localed_label( // result_list is vector of string vector char* label = nullptr; auto result_list = return_parcel->GetResult(); - for (auto result : result_list) { + for (auto& result : result_list) { // result is string vector // it only has one string or not. - if (result.front().empty() || result.front().length() == 0) + if (result.front().empty()) return nullptr; label = strdup(result.front().c_str()); if (label == nullptr) { @@ -266,11 +267,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info( auto result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { - if (result.size() != 2) - return PMINFO_R_ERROR; - if (result.front().empty() || result.front().size() == 0 || - result.back().empty() || result.back().size() == 0) + for (auto& result : result_list) { + if (result.size() != 2 || result.front().empty() || result.back().empty()) return PMINFO_R_ERROR; *appid = strdup(result.front().c_str()); *access = strdup(result.back().c_str()); @@ -319,10 +317,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_appid( auto result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { - if (result.size() != 1) - return PMINFO_R_ERROR; - if (result.front().empty() || result.front().size() == 0) + for (auto& result : result_list) { + if (result.size() != 1 || result.front().empty()) return PMINFO_R_ERROR; *appid = strdup(result.front().c_str()); if (*appid == nullptr) { @@ -372,11 +368,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info( auto result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { - if (result.size() != 2) - return PMINFO_R_ERROR; - if (result.front().empty() || result.front().size() == 0 || - result.back().empty() || result.back().size() == 0) + for (auto& result : result_list) { + if (result.size() != 2 || result.front().empty() || result.back().empty()) return PMINFO_R_ERROR; *appid = strdup(result.front().c_str()); *trusted = strdup(result.back().c_str()); @@ -427,10 +420,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges( if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { - if (result.size() != 1) - return PMINFO_R_ERROR; - if (result.front().empty() || result.front().size() == 0) + for (auto& result : result_list) { + if (result.size() != 1 || result.front().empty()) return PMINFO_R_ERROR; char* privilege = strdup(result.front().c_str()); if (privilege == nullptr) { @@ -480,14 +471,10 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges( if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { - if (result.size() != 2) - return PMINFO_R_ERROR; - if (result.front().empty() || result.front().size() == 0 || - result.back().empty() || result.back().size() == 0) + for (auto& result : result_list) { + if (result.size() != 2 || result.front().empty() || result.back().empty()) return PMINFO_R_ERROR; - std::string app_control = result.front(); - std::stringstream ss(app_control); + std::stringstream ss(result.front()); std::string token; while (std::getline(ss, token, '|')) { if (token.compare(std::string(operation))) { @@ -551,7 +538,7 @@ extern "C" EXPORT_API int _plugininfo_get_appids( if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { + for (auto& result : result_list) { if (result.size() != 1) { LOG(ERROR) << "Invalid result"; g_list_free_full(*list, free); @@ -643,7 +630,7 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid, if (result_list.size() == 0) return PMINFO_R_ENOENT; - for (auto result : result_list) { + for (auto& result : result_list) { if (result.size() != 3) { LOG(ERROR) << "Invalid result"; g_slist_free_full(*update_info_list, __free_update_info); @@ -763,18 +750,18 @@ extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid, result_map.insert(make_pair(std::string(l_pkgid), "-1")); result_map.insert(make_pair(std::string(r_pkgid), "-1")); - for (auto &certinfo : certinfo_list) + for (auto& certinfo : certinfo_list) result_map[certinfo.front()] = certinfo.back(); - if (result_map.find(std::string(l_pkgid))->second == "-1" && - result_map.find(std::string(r_pkgid))->second == "-1") + auto l_iter = result_map.find(l_pkgid); + auto r_iter = result_map.find(r_pkgid); + if (l_iter->second == "-1" && r_iter->second == "-1") *result = PMINFO_CERT_COMPARE_BOTH_NO_CERT; - else if (result_map.find(std::string(l_pkgid))->second == "-1") + else if (l_iter->second == "-1") *result = PMINFO_CERT_COMPARE_LHS_NO_CERT; - else if (result_map.find(std::string(r_pkgid))->second == "-1") + else if (r_iter->second == "-1") *result = PMINFO_CERT_COMPARE_RHS_NO_CERT; - else if (result_map.find(std::string(l_pkgid))->second == - result_map.find(std::string(r_pkgid))->second) + else if (l_iter->second == r_iter->second) *result = PMINFO_CERT_COMPARE_MATCH; else *result = PMINFO_CERT_COMPARE_MISMATCH; @@ -816,21 +803,24 @@ extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid, auto pkgid_list = return_parcel->GetResult(); std::map result_map; - for (auto &pkgid : pkgid_list) + for (auto& pkgid : pkgid_list) result_map.insert(make_pair(pkgid.front(), pkgid.back())); - if (result_map.find(std::string(l_appid)) == result_map.end()) { + auto l_iter = result_map.find(l_appid); + if (l_iter == result_map.end()) { LOG(ERROR) << "Cannot find pkgid of app " << l_appid << " for uid " << uid; return PMINFO_R_ENOENT; - } else if (result_map.find(std::string(r_appid)) == result_map.end()) { + } + auto r_iter = result_map.find(r_appid); + if (r_iter == result_map.end()) { LOG(ERROR) << "Cannot find pkgid of app " << r_appid << " for uid " << uid; return PMINFO_R_ENOENT; } - const char* l_pkgid = result_map.find(std::string(l_appid))->second.c_str(); - const char* r_pkgid = result_map.find(std::string(r_appid))->second.c_str(); + const char* l_pkgid = l_iter->second.c_str(); + const char* r_pkgid = r_iter->second.c_str(); return _certinfo_compare_pkg_certinfo(l_pkgid, r_pkgid, result); } @@ -1131,17 +1121,17 @@ extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid) { return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { + if (ptr == nullptr) { LOG(ERROR) << "Fail to get return parcelable"; return PMINFO_R_ERROR; } - if (ptr->GetRequestResult() != PMINFO_R_OK) { + if (ptr->GetRequestResult() != PMINFO_R_OK) { LOG(ERROR) << "Request fail"; return PMINFO_R_ERROR; } - if (ptr->GetType() != pcp::ParcelableType::Result) { + if (ptr->GetType() != pcp::ParcelableType::Result) { LOG(ERROR) << "Invalid parcelable type"; return PMINFO_R_ERROR; } -- 2.7.4