Refactor pkgmgr-info
[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   const auto& [db, uid] = GetConnection().front();
70   return internal::InitializeDb(db, uid);
71 }
72
73 int CreateDBHandler::CreateCertDB() {
74   if (GetUID() != GLOBAL_USER && GetUID() != OWNER_ROOT)
75     return PMINFO_R_OK;
76
77   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
78   ClearDBHandle();
79
80   if (!Connect())
81     return PMINFO_R_ERROR;
82
83   const auto& [db, uid] = GetConnection().front();
84   return internal::InitializeDb(db, uid);
85 }
86
87 }  // namespace database
88 }  // namespace pkgmgr_server