da918fe24b69ced359b5f4357ca03f14cc031a86
[platform/core/appfw/pkgmgr-info.git] / src / server / database / create_db_handler.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "create_db_handler.hh"
18
19 #include <vector>
20
21 #include "db_handle_provider.hh"
22 #include "utils/logging.hh"
23
24 #include "pkgmgrinfo_debug.h"
25 #include "pkgmgrinfo_internal.h"
26
27 #ifndef OWNER_ROOT
28 #define OWNER_ROOT 0
29 #endif
30 #ifndef GLOBAL_USER
31 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
32 #endif
33
34 namespace pkgmgr_server {
35 namespace database {
36
37 CreateDBHandler::CreateDBHandler(uid_t uid, int pid)
38     : AbstractDBHandler(uid, pid) {}
39
40 CreateDBHandler::~CreateDBHandler() {}
41
42 std::vector<std::vector<std::string>> CreateDBHandler::GetResult() {
43   return std::move(result_);
44 }
45
46 int CreateDBHandler::Execute() {
47   std::unique_lock<std::shared_timed_mutex> u(lock_);
48
49   if (CreateParserDB() < 0) {
50     LOG(ERROR) << "Failed to create parser db for uid : " << GetUID();
51     return PMINFO_R_ERROR;
52   }
53
54   if (GetUID() == GLOBAL_USER || GetUID() == OWNER_ROOT) {
55     if (CreateCertDB() < 0) {
56       LOG(ERROR) << "Failed to create cert db";
57       return PMINFO_R_ERROR;
58     }
59   }
60
61   return PMINFO_R_OK;
62 }
63
64 int CreateDBHandler::CreateParserDB() {
65   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
66   ClearDBHandle();
67
68   if (!Connect())
69     return PMINFO_R_ERROR;
70
71   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
72   sqlite3* conn = conn_list.front().first;
73   uid_t uid = conn_list.front().second;
74
75   return pkgmgr_parser_internal_initialize_db(conn, uid);
76 }
77
78 int CreateDBHandler::CreateCertDB() {
79   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
80   ClearDBHandle();
81
82   if (!Connect())
83     return PMINFO_R_ERROR;
84
85   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
86   sqlite3* conn = conn_list.front().first;
87   uid_t uid = conn_list.front().second;
88
89   return pkgmgr_parser_internal_initialize_db(conn, uid);
90 }
91
92 }  // namespace database
93 }  // namespace pkgmgr_server