Merge branch 'tizen' of ssh://review.tizen.org:29418/platform/core/appfw/pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.hh
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 #ifndef SERVER_DB_HANDLE_PROVIDER_HH_
18 #define SERVER_DB_HANDLE_PROVIDER_HH_
19
20 #include <sqlite3.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23
24 #include <string>
25 #include <memory>
26 #include <mutex>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <vector>
30
31 #include "shared_object.hh"
32
33 namespace pkgmgr_server {
34 namespace database {
35
36 #ifndef EXPORT_API
37 #define EXPORT_API __attribute__((visibility("default")))
38 #endif
39
40 class EXPORT_API DBHandleProvider {
41  public:
42   ~DBHandleProvider() = default;
43   static DBHandleProvider& GetInst(uid_t uid);
44   static bool IsCrashedWriteRequest();
45   std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid, bool write);
46   std::string GetCertDBPath(int pid, bool write);
47   void SetMemoryMode(pid_t pid, bool flag);
48
49  private:
50   DBHandleProvider(uid_t uid);
51   sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
52       const std::string& memorydb_path);
53
54  private:
55   static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
56   static bool is_memory_global_;
57   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
58       global_parser_memory_db_handle_;
59   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
60       cert_memory_db_handle_;
61   static std::string global_parser_memory_db_path_;
62   static std::string global_parser_file_db_path_;
63   static std::string cert_memory_db_path_;
64   static std::string cert_file_db_path_;
65   static std::unordered_set<pid_t> pid_list_;
66   static std::recursive_mutex lock_;
67
68   uid_t uid_;
69   bool is_memory_;
70   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
71   std::string parser_memory_db_path_;
72   std::string parser_file_db_path_;
73 };
74
75 }  // namespace database
76 }  // namespace pkgmgr_server
77
78 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_