Fix handling pending pkgs
[platform/core/appfw/pkgmgr-info.git] / src / server / database / pkg_set_db_handler.cc
index d4c46b1..639949f 100644 (file)
@@ -18,7 +18,9 @@
 
 #include <vector>
 
+#include "cache_flag.hh"
 #include "db_handle_provider.hh"
+#include "utils/logging.hh"
 
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_internal.h"
@@ -26,8 +28,8 @@
 namespace pkgmgr_server {
 namespace database {
 
-PkgSetDBHandler::PkgSetDBHandler(uid_t uid, int pid)
-    : AbstractDBHandler(uid, pid), uid_(uid) {}
+PkgSetDBHandler::PkgSetDBHandler(uid_t uid, int pid, bool is_offline)
+    : AbstractDBHandler(uid, pid), uid_(uid), is_offline_(is_offline) {}
 
 PkgSetDBHandler::~PkgSetDBHandler() {}
 
@@ -48,17 +50,20 @@ std::vector<std::vector<std::string>> PkgSetDBHandler::GetResult() {
 }
 
 int PkgSetDBHandler::Execute() {
-  std::unique_lock<std::shared_timed_mutex> u(lock_);
+  std::unique_lock<std::shared_mutex> u(lock_);
   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
 
   if (!Connect())
     return PMINFO_R_ERROR;
 
-  DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
+  if (!is_offline_)
+    DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
+
   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   sqlite3* conn = conn_list.front().first;
   int ret = 0;
+
   if (write_type_ == pkgmgr_common::PkgWriteType::Insert)
     ret = pkgmgr_parser_insert_pkg_info(conn, package_, uid_);
   else if (write_type_ == pkgmgr_common::PkgWriteType::Update)
@@ -66,11 +71,15 @@ int PkgSetDBHandler::Execute() {
   else if (write_type_ == pkgmgr_common::PkgWriteType::Delete)
     ret = pkgmgr_parser_delete_pkg_info(conn, package_->package, uid_);
   else
-    _LOGE("Unknown db write type");
+    LOG(ERROR) << "Unknown db write type";
 
-  if (ret != PM_PARSER_R_OK)
+  if (is_offline_ || ret != PMINFO_R_OK)
     return ret;
 
+  auto lock = CacheFlag::GetWriterLock();
+  if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
+    DBHandleProvider::GetInst(uid_).RegisterPendingPackageInfo(package_);
+
   return ret;
 }