1e9a657dd19cab3ba43570c1bcb3e0da318a3648
[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_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 (CreateCertDB() < 0) {
55     LOG(ERROR) << "Failed to create cert db";
56     return PMINFO_R_ERROR;
57   }
58
59   return PMINFO_R_OK;
60 }
61
62 int CreateDBHandler::CreateParserDB() {
63   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
64   ClearDBHandle();
65
66   if (!Connect())
67     return PMINFO_R_ERROR;
68
69   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
70   sqlite3* conn = conn_list.front().first;
71   uid_t uid = conn_list.front().second;
72
73   return pkgmgr_parser_internal_initialize_db(conn, uid);
74 }
75
76 int CreateDBHandler::CreateCertDB() {
77   if (GetUID() != GLOBAL_USER && GetUID() != OWNER_ROOT)
78     return PMINFO_R_OK;
79
80   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
81   ClearDBHandle();
82
83   if (!Connect())
84     return PMINFO_R_ERROR;
85
86   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
87   sqlite3* conn = conn_list.front().first;
88   uid_t uid = conn_list.front().second;
89
90   return pkgmgr_parser_internal_initialize_db(conn, uid);
91 }
92
93 }  // namespace database
94 }  // namespace pkgmgr_server