Fix DBHandleProvider implementation (#118)
author최창규/Tizen Platform Lab(SR)/Engineer/삼성전자 <changyu.choi@samsung.com>
Fri, 5 Mar 2021 08:35:39 +0000 (17:35 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 5 Mar 2021 08:35:39 +0000 (17:35 +0900)
* Fix DBHandleProvider implementation

Changes:
 - Add flag "SQLITE_OPEN_URI" for open memory db
 - Add ConverUID for global app

Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/common/database/abstract_db_handler.cc
src/common/database/db_handle_provider.cc
src/common/database/db_handle_provider.hh

index 5404efe..707854f 100644 (file)
@@ -35,13 +35,6 @@ namespace {
 const char kMemoryDBPrefix[] = "file:memdb_";
 const char kMemoryDBPostFix[] = "?mode=memory&cache=shared";
 
-uid_t ConvertUID(uid_t uid) {
-  if (uid < REGULAR_USER)
-    return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
-  else
-    return uid;
-}
-
 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
 #define BUSY_WAITING_MAX 100 /* wait for max 5 sec */
 
@@ -248,7 +241,8 @@ bool AbstractDBHandler::Connect() {
   std::string db_path = GetDBPath();
   int ret = 0;
   if (op_type_ == OPERATION_TYPE_READ)
-    ret = __open_read_db(db_path.c_str(), &db_, SQLITE_OPEN_READONLY);
+    ret = __open_read_db(db_path.c_str(), &db_, SQLITE_OPEN_READONLY |
+        SQLITE_OPEN_URI);
   else
     ret = __open_write_db(uid_, db_path.c_str(), &db_,
         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
index a327041..c20ba70 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <tzplatform_config.h>
+
 #include "db_handle_provider.hh"
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_debug.h"
 #endif
 #define LOG_TAG "PKGMGR_INFO"
 
+namespace {
+
+constexpr uid_t REGULAR_USER = 5000;
+
+uid_t ConvertUID(uid_t uid) {
+  if (uid < REGULAR_USER)
+    return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+  else
+    return uid;
+}
+
+}  // namespace
+
 namespace pkgmgr_common {
 
-std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> DBHandleProvider::provider_;
+std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
+    DBHandleProvider::provider_;
 bool DBHandleProvider::is_memory_ = false;
 
 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
@@ -48,6 +64,7 @@ DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
   static std::mutex singleton_lock;
   std::unique_lock<std::mutex> u(singleton_lock);
+  uid = ConvertUID(uid);
   auto& prov = provider_[uid];
   if (prov == nullptr) {
     prov.reset(new DBHandleProvider(uid));
@@ -76,16 +93,17 @@ 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");
+  int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
+      SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
+  if (ret != SQLITE_OK) {
+    LOGE("Failed to open memory DB %d(%s)", ret, memorydb_path.c_str());
     return nullptr;
   }
 
-  if (sqlite3_open_v2(filedb_path.c_str(), &filedb,
-      SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
-    LOGE("Failed to open file DB");
+  ret = sqlite3_open_v2(filedb_path.c_str(), &filedb,
+      SQLITE_OPEN_READONLY, nullptr);
+  if (ret != SQLITE_OK) {
+    LOGE("Failed to open file DB %d(%s)", ret, filedb_path.c_str());
     sqlite3_close_v2(memorydb);
     return nullptr;
   }
@@ -100,7 +118,7 @@ sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
 
   sqlite3_backup_step(backup, -1);
   sqlite3_backup_finish(backup);
-  LOGD("Set memory DB");
+  LOGD("Set memory DB(%s)", memorydb_path.c_str());
   sqlite3_close_v2(filedb);
   return memorydb;
 }
index 59a7c6f..3a5d1e0 100644 (file)
 
 namespace pkgmgr_common {
 
-class DBHandleProvider {
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API DBHandleProvider {
  public:
   ~DBHandleProvider() = default;
   static DBHandleProvider& GetInst(uid_t uid);