Merge branch 'master' of github.sec.samsung.net:appfw/pkgmgr-info into tizen
[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
23 #include "pkgmgrinfo_debug.h"
24 #include "pkgmgrinfo_internal.h"
25
26 #ifndef OWNER_ROOT
27 #define OWNER_ROOT 0
28 #endif
29 #ifndef GLOBAL_USER
30 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
31 #endif
32
33 namespace pkgmgr_server {
34 namespace database {
35
36 CreateDBHandler::CreateDBHandler(uid_t uid, int pid)
37     : AbstractDBHandler(uid, pid) {}
38
39 CreateDBHandler::~CreateDBHandler() {}
40
41 std::vector<std::vector<std::string>> CreateDBHandler::GetResult() {
42   return std::move(result_);
43 }
44
45 int CreateDBHandler::Execute() {
46   std::unique_lock<std::shared_timed_mutex> u(lock_);
47
48   if (CreateParserDB() < 0) {
49     LOGE("Failed to create parser db for uid [%d]", GetUID());
50     return PMINFO_R_ERROR;
51   }
52
53   if (GetUID() == GLOBAL_USER || GetUID() == OWNER_ROOT) {
54     if (CreateCertDB() < 0) {
55       LOGE("Failed to create cert db for uid [%d]", GetUID());
56       return PMINFO_R_ERROR;
57     }
58   }
59
60   return PMINFO_R_OK;
61 }
62
63 int CreateDBHandler::CreateParserDB() {
64   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
65   ClearDBHandle();
66
67   if (!Connect())
68     return PMINFO_R_ERROR;
69
70   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
71   sqlite3* conn = conn_list.front().first;
72   uid_t uid = conn_list.front().second;
73
74   return pkgmgr_parser_internal_initialize_db(conn, uid);
75 }
76
77 int CreateDBHandler::CreateCertDB() {
78   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
79   ClearDBHandle();
80
81   if (!Connect())
82     return PMINFO_R_ERROR;
83
84   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
85   sqlite3* conn = conn_list.front().first;
86   uid_t uid = conn_list.front().second;
87
88   return pkgmgr_parser_internal_initialize_db(conn, uid);
89 }
90
91 }  // namespace database
92 }  // namespace pkgmgr_server