--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "db_handle_provider.hh"
+#include "pkgmgr-info.h"
+#include "pkgmgrinfo_debug.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "PKGMGR_INFO"
+
+namespace pkgmgr_common {
+
+std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> DBHandleProvider::provider_;
+
+const char kMemoryDBPrefix[] = "file:memdb_";
+const char kMemoryDBPostFix[] = "?mode=memory&cache=shared";
+DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid), is_memory_(false),
+ parser_memory_db_handle_(nullptr, sqlite3_close_v2),
+ cert_memory_db_handle_(nullptr, sqlite3_close_v2) {
+ parser_file_db_path_ = getUserPkgParserDBPathUID(uid_);
+ cert_file_db_path_ = getUserPkgCertDBPath();
+
+ parser_memory_db_path_ = "file:parserdb" +
+ std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
+ cert_memory_db_path_ = "file:certdb" +
+ std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
+}
+
+DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
+ static std::mutex singleton_lock;
+ std::unique_lock<std::mutex> u(singleton_lock);
+ auto& prov = provider_[uid];
+ if (prov == nullptr) {
+ prov.reset(new DBHandleProvider(uid));
+ }
+
+ return *prov;
+}
+
+std::string DBHandleProvider::GetParserDBPath() {
+ std::unique_lock<std::mutex> u(lock_);
+ if (is_memory_)
+ return parser_memory_db_path_;
+ else
+ return parser_file_db_path_;
+}
+
+std::string DBHandleProvider::GetCertDBPath() {
+ std::unique_lock<std::mutex> u(lock_);
+ if (is_memory_)
+ return cert_memory_db_path_;
+ else
+ return cert_file_db_path_;
+}
+
+sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
+ const std::string& memorydb_path) {
+ sqlite3* memorydb = nullptr;
+ sqlite3* filedb = nullptr;
+
+ if (sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
+ LOGE("Failed to open memory DB");
+ return nullptr;
+ }
+
+ if (sqlite3_open_v2(filedb_path.c_str(), &filedb,
+ SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+ LOGE("Failed to open file DB");
+ sqlite3_close_v2(memorydb);
+ return nullptr;
+ }
+ sqlite3_backup* backup = sqlite3_backup_init(memorydb, "main",
+ filedb, "main");
+ if (backup == nullptr) {
+ LOGE("Failed to backup for memory DB");
+ sqlite3_close_v2(memorydb);
+ sqlite3_close_v2(filedb);
+ return nullptr;
+ }
+
+ sqlite3_backup_step(backup, -1);
+ sqlite3_backup_finish(backup);
+ LOGD("Set memory DB");
+ sqlite3_close_v2(filedb);
+ return memorydb;
+}
+
+void DBHandleProvider::SetMemoryMode(bool flag) {
+ is_memory_ = false;
+ std::unique_lock<std::mutex> u(lock_);
+ if (is_memory_ == flag)
+ return;
+
+ is_memory_ = flag;
+ if (is_memory_ == true) {
+ sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
+ parser_memory_db_path_);
+ if (parser_db != nullptr)
+ parser_memory_db_handle_.reset(parser_db);
+ sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
+ cert_memory_db_path_);
+ if (cert_db != nullptr)
+ cert_memory_db_handle_.reset(cert_db);
+ } else {
+ parser_memory_db_handle_.reset(nullptr);
+ cert_memory_db_handle_.reset(nullptr);
+ }
+
+ LOGD("Set Memory mode : %s", flag ? "Memory" : "File");
+}
+
+} // namespace pkgmgr_common
--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SERVER_DB_HANDLE_PROVIDER_HH_
+#define SERVER_DB_HANDLE_PROVIDER_HH_
+
+#include <sqlite3.h>
+#include <stdlib.h>
+
+#include <string>
+#include <memory>
+#include <mutex>
+#include <unordered_map>
+
+#include "shared_object.hh"
+
+namespace pkgmgr_common {
+
+class DBHandleProvider {
+ public:
+ ~DBHandleProvider() = default;
+ static DBHandleProvider& GetInst(uid_t uid);
+ std::string GetParserDBPath();
+ std::string GetCertDBPath();
+ void SetMemoryMode(bool flag);
+
+ private:
+ DBHandleProvider(uid_t uid);
+ sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
+ const std::string& memorydb_path);
+
+ private:
+ static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
+ uid_t uid_;
+ std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
+ std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> cert_memory_db_handle_;
+ std::string parser_memory_db_path_;
+ std::string parser_file_db_path_;
+ std::string cert_memory_db_path_;
+ std::string cert_file_db_path_;
+ bool is_memory_;
+ std::mutex lock_;
+};
+
+} // namespace pkgmgr_common
+
+#endif // SERVER_DB_HANDLE_PROVIDER_HH_