2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "appinfo_db_handler.hh"
19 #include <shared_mutex>
22 #include "cache_flag.hh"
23 #include "db_handle_provider.hh"
24 #include "utils/logging.hh"
26 #include "pkgmgrinfo_basic.h"
27 #include "pkgmgrinfo_internal.h"
31 uid_t globaluser_uid = -1;
33 uid_t GetGlobalUID() {
34 if (globaluser_uid == (uid_t)-1)
35 globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
37 return globaluser_uid;
42 namespace pkgmgr_server {
45 AppInfoDBHandler::AppInfoDBHandler(uid_t uid, int pid)
46 : AbstractDBHandler(uid, pid), filter_(nullptr), uid_(uid) {}
48 AppInfoDBHandler::~AppInfoDBHandler() {}
50 std::vector<std::shared_ptr<application_x>> AppInfoDBHandler::DetachAppHandle() {
51 return std::move(handle_list_);
54 void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
58 int AppInfoDBHandler::GetHandleFromDB() {
60 LOG(ERROR) << "Failed to connect database";
61 return PMINFO_R_ERROR;
64 const auto& conn_list = GetConnection();
65 std::vector<std::shared_ptr<application_x>> list;
66 int ret = PMINFO_R_OK;
67 for (const auto& [db, uid] : conn_list) {
68 ret = internal::GetAppInfo(db, filter_, uid, uid_, GetLocale(), list);
69 if (ret == PMINFO_R_ERROR) {
70 LOG(DEBUG) << "Failed to GetAppInfo: " << ret;
76 ret = PMINFO_R_ENOENT;
78 if (ret == PMINFO_R_OK)
79 handle_list_ = std::move(list);
84 void AppInfoDBHandler::GetApplicationFromCache(uid_t uid,
85 const std::string& application) {
86 std::vector<std::shared_ptr<application_x>> app_list;
87 app_list = DBHandleProvider::GetInst(uid).GetApplications(GetPID(),
88 filter_, application);
90 handle_list_.reserve(app_list.size() + handle_list_.size());
91 std::move(std::begin(app_list), std::end(app_list),
92 std::back_inserter(handle_list_));
95 int AppInfoDBHandler::GetHandleFromCache() {
96 std::string application;
98 for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
99 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
100 if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
101 application = node->value;
106 if (uid_ > REGULAR_USER)
107 GetApplicationFromCache(uid_, application);
109 GetApplicationFromCache(GetGlobalUID(), application);
111 if (handle_list_.empty())
112 return PMINFO_R_ENOENT;
117 int AppInfoDBHandler::Execute() {
118 std::shared_lock<std::shared_mutex> s(lock_);
119 SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
120 SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
122 if (DBHandleProvider::IsWriter(GetPID()))
123 return GetHandleFromDB();
125 if (uid_ <= GetDefaultUser() &&
126 CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
127 auto cache_lock = CacheFlag::GetReaderLock();
128 if (cache_lock.try_lock() &&
129 CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
130 return GetHandleFromCache();
133 return GetHandleFromDB();
136 } // namespace database
137 } // namespace pkgmgr_server