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"
28 #include "pkgmgrinfo_internal.hh"
30 namespace pkgmgr_server {
33 AppInfoDBHandler::AppInfoDBHandler(uid_t uid, int pid)
34 : AbstractDBHandler(uid, pid), filter_(nullptr), uid_(uid) {}
36 AppInfoDBHandler::~AppInfoDBHandler() {}
38 std::vector<std::shared_ptr<application_x>> AppInfoDBHandler::DetachAppHandle() {
39 return std::move(handle_list_);
42 void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
46 int AppInfoDBHandler::GetHandleFromDB(
47 std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
48 std::vector<std::shared_ptr<application_x>> list;
49 int ret = PMINFO_R_OK;
50 for (auto& conn : conn_list) {
51 ret = pkgmgr_server::internal::appinfo_internal_filter_get_list(conn.first,
52 filter_, conn.second, uid_, GetLocale().c_str(), list);
53 if (ret == PMINFO_R_ERROR) {
54 LOG(DEBUG) << "Failed to appinfo_internal_filter_get_list: " << ret;
60 ret = PMINFO_R_ENOENT;
62 if (ret == PMINFO_R_OK)
63 handle_list_ = std::move(list);
68 int AppInfoDBHandler::GetHandleFromCache(
69 std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
71 GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
72 int ret = PMINFO_R_OK;
73 std::string application;
75 for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
76 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
77 if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
78 application = node->value;
83 std::vector<std::shared_ptr<application_x>> app_list;
84 for (auto& conn : conn_list) {
85 app_list = DBHandleProvider::GetInst(conn.second)
87 GetPID(), is_write_op, filter_, application);
89 handle_list_.reserve(app_list.size() + handle_list_.size());
90 std::move(std::begin(app_list), std::end(app_list),
91 std::back_inserter(handle_list_));
94 if (handle_list_.empty())
95 ret = PMINFO_R_ENOENT;
100 int AppInfoDBHandler::Execute() {
101 std::shared_lock<std::shared_timed_mutex> s(lock_);
102 SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
103 SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
106 return PMINFO_R_ERROR;
108 std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
110 bool is_writer = false;
111 for (auto& conn : conn_list) {
112 if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
116 return GetHandleFromDB(conn_list);
118 if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
119 auto cache_lock = CacheFlag::GetReaderLock();
120 if (cache_lock.try_lock() &&
121 CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
122 return GetHandleFromCache(conn_list);
125 return GetHandleFromDB(conn_list);
128 } // namespace database
129 } // namespace pkgmgr_server