Add package paremeter to application filter
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.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 "db_handle_provider.hh"
18
19 #include <fcntl.h>
20 #include <glib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <tzplatform_config.h>
25
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29
30 #include "cache_flag.hh"
31 #include "utils/logging.hh"
32
33 #include "pkgmgr-info.h"
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_internal.h"
36 #include "pkgmgrinfo_private.h"
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif
41 #define LOG_TAG "PKGMGR_INFO"
42
43 namespace {
44
45 uid_t globaluser_uid = -1;
46
47 uid_t GetGlobalUID() {
48   if (globaluser_uid == (uid_t)-1)
49     globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
50
51   return globaluser_uid;
52 }
53
54 uid_t ConvertUID(uid_t uid) {
55   if (uid < REGULAR_USER)
56     return GetGlobalUID();
57   else
58     return uid;
59 }
60
61 bool GetModifiedTime(const char* dbpath, timespec* t) {
62   if (dbpath == nullptr || t == nullptr) {
63     LOG(ERROR) << "Invalid argument";
64     return false;
65   }
66
67   struct stat attr;
68   if (stat(dbpath, &attr)) {
69     LOG(ERROR) << "Fail to get status from file "
70         << dbpath << " errno : " << errno;
71     return false;
72   }
73
74   *t = attr.st_mtim;
75
76   return true;
77 }
78
79 static const std::string global_parser_memdb_path =
80     "file:parserdb?mode=memory&cache=shared";
81
82 static const std::string cert_memdb_path =
83     "file:certdb?mode=memory&cache=shared";
84
85 }  // namespace
86
87 namespace pkgmgr_server {
88 namespace database {
89
90 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
91     DBHandleProvider::provider_map_;
92 bool DBHandleProvider::is_global_memdb_set_ = false;
93 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
94     DBHandleProvider::global_parser_memdb_handle_(nullptr,
95                                                   sqlite3_close_v2);
96 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
97     DBHandleProvider::cert_memdb_handle_(nullptr, sqlite3_close_v2);
98 std::string DBHandleProvider::global_parser_filedb_path_;
99 std::string DBHandleProvider::cert_filedb_path_;
100 std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
101 std::recursive_mutex DBHandleProvider::lock_;
102 std::shared_mutex DBHandleProvider::pid_list_lock_;
103
104 DBHandleProvider::DBHandleProvider(uid_t uid)
105     : uid_(uid),
106       is_user_memdb_set_(false),
107       parser_memdb_handle_(nullptr, sqlite3_close_v2) {
108   char* tmp_path;
109
110   if (global_parser_filedb_path_.empty()) {
111     tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
112     global_parser_filedb_path_ = tmp_path;
113     free(tmp_path);
114   }
115
116   if (cert_filedb_path_.empty()) {
117     tmp_path = getUserPkgCertDBPath();
118     cert_filedb_path_ = tmp_path;
119     free(tmp_path);
120   }
121
122   tmp_path = getUserPkgParserDBPathUID(uid_);
123   user_parser_filedb_path_ = tmp_path;
124   free(tmp_path);
125
126   user_parser_memdb_path_ = "file:parserdb" +
127                             std::to_string(static_cast<int>(uid_)) +
128                             "?mode=memory&cache=shared";
129 }
130
131 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
132   static std::shared_mutex singleton_lock_;
133   std::shared_lock<std::shared_mutex> s(singleton_lock_);
134
135   uid = ConvertUID(uid);
136   auto it = provider_map_.find(uid);
137   if (it != provider_map_.end())
138     return *(it->second);
139
140   s.unlock();
141   std::unique_lock<std::shared_mutex> u(singleton_lock_);
142   auto& prov = provider_map_[uid];
143   if (prov == nullptr)
144     prov.reset(new DBHandleProvider(uid));
145
146   return *prov;
147 }
148
149 bool DBHandleProvider::IsCrashedWriteRequest() {
150   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
151
152   if (writer_pid_list_.empty())
153     return false;
154
155   bool ret = true;
156   LOG(DEBUG) << "Check process count : " << writer_pid_list_.size();
157   std::vector<pid_t> remove_pids;
158   for (pid_t pid : writer_pid_list_) {
159     std::string status_path = "/proc/" + std::to_string(pid) + "/status";
160
161     int fd = open(status_path.c_str(), O_RDONLY);
162     if (fd < 0) {
163       LOG(ERROR) << "Process is crashed : " << pid;
164       remove_pids.push_back(pid);
165     } else {
166       ret = false;
167       close(fd);
168     }
169   }
170
171   for (pid_t pid : remove_pids)
172     writer_pid_list_.erase(pid);
173
174   return ret;
175 }
176
177 std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
178     pid_t pid, bool write) {
179   std::unique_lock<std::recursive_mutex> u(lock_);
180   std::vector<std::pair<std::string, uid_t>> db_path_list;
181
182   if (is_user_memdb_set_ != is_global_memdb_set_)
183     is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
184
185   if (IsMemoryDBActive(pid, write)) {
186     if (uid_ > REGULAR_USER)
187       db_path_list.emplace_back(std::make_pair(user_parser_memdb_path_, uid_));
188
189     db_path_list.emplace_back(
190         std::make_pair(global_parser_memdb_path, GetGlobalUID()));
191   } else {
192     if (uid_ > REGULAR_USER)
193       db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
194
195     db_path_list.emplace_back(
196         std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
197   }
198
199   if (db_path_list.size() == 1) {
200     LOG(DEBUG) << "global db path : " << db_path_list[0].first;
201   } else {
202     LOG(DEBUG) << "local db path : " << db_path_list[0].first;
203     LOG(DEBUG) << "global db path : " << db_path_list[1].first;
204   }
205
206   return db_path_list;
207 }
208
209 std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
210   std::unique_lock<std::recursive_mutex> u(lock_);
211   if (is_user_memdb_set_ != is_global_memdb_set_)
212     is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
213
214   if (IsMemoryDBActive(pid, write))
215     return cert_memdb_path;
216   else
217     return cert_filedb_path_;
218 }
219
220 sqlite3* DBHandleProvider::CreateMemoryDBHandle(const std::string& filedb_path,
221                                              const std::string& memorydb_path) {
222   sqlite3* memorydb = nullptr;
223   sqlite3* filedb = nullptr;
224   int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
225                             SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
226   if (ret != SQLITE_OK) {
227     LOG(ERROR) << "Failed to open memory DB " << ret << ": " << memorydb_path;
228     return nullptr;
229   }
230
231   ret = sqlite3_open_v2(filedb_path.c_str(), &filedb, SQLITE_OPEN_READONLY,
232                         nullptr);
233   if (ret != SQLITE_OK) {
234     LOG(ERROR) << "Failed to open file DB " << ret << ": " << filedb_path;
235     sqlite3_close_v2(memorydb);
236     return nullptr;
237   }
238
239   sqlite3_backup* backup =
240       sqlite3_backup_init(memorydb, "main", filedb, "main");
241   if (backup == nullptr) {
242     LOG(ERROR) << "Failed to backup for memory DB";
243     sqlite3_close_v2(memorydb);
244     sqlite3_close_v2(filedb);
245     return nullptr;
246   }
247
248   sqlite3_backup_step(backup, -1);
249   sqlite3_backup_finish(backup);
250   sqlite3_close_v2(filedb);
251   return memorydb;
252 }
253
254 void DBHandleProvider::SetMemoryMode(pid_t pid) {
255   std::unique_lock<std::recursive_mutex> u(lock_);
256   if (is_global_memdb_set_ && is_user_memdb_set_)
257     return;
258
259   sqlite3* parser_db =
260       CreateMemoryDBHandle(user_parser_filedb_path_, user_parser_memdb_path_);
261   if (parser_db != nullptr)
262     parser_memdb_handle_.reset(parser_db);
263
264   if (is_user_memdb_set_ == is_global_memdb_set_) {
265     sqlite3* global_parser_file_db = CreateMemoryDBHandle(
266         global_parser_filedb_path_, global_parser_memdb_path);
267     if (global_parser_file_db)
268       global_parser_memdb_handle_.reset(global_parser_file_db);
269
270     sqlite3* cert_db =
271         CreateMemoryDBHandle(cert_filedb_path_, cert_memdb_path);
272     if (cert_db != nullptr)
273       cert_memdb_handle_.reset(cert_db);
274
275     InsertPID(pid);
276   }
277
278   is_user_memdb_set_ = true;
279   is_global_memdb_set_ = true;
280   LOG(DEBUG) << "Set Memory mode : Memory";
281 }
282
283 void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
284   std::unique_lock<std::recursive_mutex> u(lock_);
285   if (!is_global_memdb_set_ && !is_user_memdb_set_)
286     return;
287
288   parser_memdb_handle_.reset(nullptr);
289   cert_memdb_handle_.reset(nullptr);
290   global_parser_memdb_handle_.reset(nullptr);
291
292   if (!ErasePID(pid))
293     LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
294
295   is_user_memdb_set_ = false;
296   is_global_memdb_set_ = false;
297
298   LOG(DEBUG) << "Set Memory mode : File";
299 }
300
301 bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
302   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
303   if (!is_user_memdb_set_)
304     return false;
305
306   if (write)
307     return false;
308
309   if (writer_pid_list_.find(pid) != writer_pid_list_.end())
310     return false;
311
312   return true;
313 }
314
315 void DBHandleProvider::TrimCache() {
316   std::unique_lock<std::recursive_mutex> u(lock_);
317   if (!released_)
318     ReleaseCache();
319 }
320
321 void DBHandleProvider::ReleaseCache() {
322   auto lock = CacheFlag::GetWriterLock();
323   CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
324
325   app_map_.clear();
326   pkg_map_.clear();
327   pending_pkg_.clear();
328   pkg_app_map_.clear();
329   CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
330
331   released_ = true;
332 }
333
334 bool DBHandleProvider::IsWriter(pid_t pid) {
335   std::unique_lock<std::shared_mutex> s(pid_list_lock_);
336   return writer_pid_list_.find(pid) != writer_pid_list_.end();
337 }
338
339 int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
340     uid_t uid, bool write, const std::string& locale) {
341   pkg_map_.clear();
342   app_map_.clear();
343   pending_pkg_.clear();
344   pkg_app_map_.clear();
345
346   const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
347   bool is_inmemory_db = false;
348   if (dbpath == nullptr || strlen(dbpath) == 0) {
349     LOG(INFO) << "database is inmemory db";
350     is_inmemory_db = true;
351   }
352
353   timespec start_time = { 0, };
354   timespec end_time = { 0, };
355   if (!is_inmemory_db && !GetModifiedTime(dbpath, &start_time))
356     return PMINFO_R_ERROR;
357
358   pkgmgrinfo_filter_x tmp_filter = { 0, };
359   tmp_filter.cache_flag = true;
360   std::map<std::string, std::shared_ptr<package_x>> pkgs;
361   int ret = internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
362   if (ret == PMINFO_R_OK) {
363     for (auto& [key, val] : pkgs)
364       AddPackage(std::move(key), std::move(val));
365   }
366
367   if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
368     return PMINFO_R_ERROR;
369
370   if (start_time.tv_sec != end_time.tv_sec ||
371       start_time.tv_nsec != end_time.tv_nsec) {
372     LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
373     return PMINFO_R_ERROR;
374   }
375
376   std::vector<std::shared_ptr<application_x>> app_list;
377   ret = internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
378
379   if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
380     return PMINFO_R_ERROR;
381
382   if (start_time.tv_sec != end_time.tv_sec ||
383       start_time.tv_nsec != end_time.tv_nsec) {
384     LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
385     return PMINFO_R_ERROR;
386   }
387
388   if (ret == PMINFO_R_OK) {
389     for (auto& app : app_list) {
390       auto it = pkg_map_.find(app->package);
391       if (it == pkg_map_.end()) {
392         LOG(ERROR) << "Can not find package from pkg_map";
393         return PMINFO_R_ERROR;
394       }
395
396       app->privileges = it->second->privileges;
397       std::string appid = app->appid;
398       AddApplication(std::move(appid), std::move(app));
399     }
400   }
401
402   released_ = false;
403
404   return ret;
405 }
406
407 inline bool CheckMetadataFilter(GList* metadata_list,
408     const std::unordered_map<std::string, std::string>& metadata_map) {
409   for (auto* it = metadata_list; it != nullptr; it = g_list_next(it)) {
410     auto* node = reinterpret_cast<metadata_x*>(it->data);
411     if (node->key != nullptr) {
412       auto metadata = metadata_map.find(node->key);
413       if (metadata == metadata_map.end())
414         continue;
415
416       if (metadata->second.empty() ||
417           strcmp(node->value ? node->value : "", metadata->second.c_str()) == 0)
418         return true;
419     }
420   }
421   return false;
422 }
423
424 inline bool CheckPkgFilters(pkgmgrinfo_filter_x* filter,
425     const std::shared_ptr<package_x>& info,
426     const std::unordered_map<std::string, std::string>& metadata_map) {
427   for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
428     auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
429     auto* checker = FilterCheckerProvider::GetInst().
430         GetPkgFilterChecker(node->prop);
431     if (!checker->CheckFilter(node, info.get()))
432       return false;
433   }
434
435   bool pass = true;
436   if (!metadata_map.empty())
437     pass = CheckMetadataFilter(info->metadata, metadata_map);
438
439   return pass;
440 }
441
442 std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
443     pid_t pid, pkgmgrinfo_filter_x* filter,
444     const std::string& package) {
445   std::vector<std::shared_ptr<package_x>> ret;
446
447   /* make metadata filter map */
448   std::unordered_map<std::string, std::string> metadata_map;
449   for (auto* it = filter->list_pkg_metadata; it != nullptr;
450       it = g_slist_next(it)) {
451     auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
452     if (node->key == nullptr)
453       continue;
454     LOG(ERROR) << "add metadata filter";
455     metadata_map[node->key] = (node->value ? node->value : "");
456   }
457
458   if (internal::CheckPackageStorageStatus(filter)) {
459     if (pkgmgrinfo_pkginfo_filter_add_bool(filter,
460         PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE, true) != PMINFO_R_OK) {
461       LOG(ERROR) << "Fail to add check storage value to filter";
462       return {};
463     }
464   }
465   if (package.empty()) {
466     for (auto& info : pkg_map_) {
467       if (CheckPkgFilters(filter, info.second, metadata_map))
468         ret.push_back(info.second);
469     }
470   } else {
471     auto map_it = pkg_map_.find(package);
472     if (map_it != pkg_map_.end() && CheckPkgFilters(filter, map_it->second,
473         metadata_map))
474       ret.push_back(map_it->second);
475   }
476
477   return ret;
478 }
479
480 void DBHandleProvider::AddPackage(std::string package,
481     std::shared_ptr<package_x> info) {
482   pkg_map_[package] = std::move(info);
483 }
484
485 inline bool CheckAppFilters(pkgmgrinfo_filter_x* filter,
486   const std::shared_ptr<application_x>& app_info,
487   const std::shared_ptr<package_x>& pkg_info,
488   const std::unordered_map<std::string, std::string>& metadata_map) {
489   for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
490     auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
491     auto* checker = FilterCheckerProvider::GetInst().
492         GetAppFilterChecker(node->prop);
493     if (!checker->CheckFilter(node, app_info.get(), pkg_info.get()))
494       return false;
495   }
496
497   bool pass = true;
498   if (!metadata_map.empty())
499     pass = CheckMetadataFilter(app_info->metadata, metadata_map);
500   return pass;
501 }
502
503 std::shared_ptr<package_x> DBHandleProvider::GetPackageByApp(
504     const char* appid) {
505   auto it = pkg_map_.find(appid);
506   if (it == pkg_map_.end())
507     return nullptr;
508
509   return it->second;
510 }
511
512 std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
513     pid_t pid, pkgmgrinfo_filter_x* filter,
514     const std::string& app) {
515   /* make metadata filter map */
516   std::unordered_map<std::string, std::string> metadata_map;
517   for (auto* it = filter->list_metadata; it != nullptr; it = g_slist_next(it)) {
518     auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
519     if (node->key == nullptr)
520       continue;
521
522     metadata_map[node->key] = (node->value ? node->value : "");
523   }
524
525   std::vector<std::shared_ptr<application_x>> ret;
526   if (internal::CheckAppStorageStatus(filter)) {
527     if (pkgmgrinfo_appinfo_filter_add_bool(filter,
528         PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, true) != PMINFO_R_OK) {
529       LOG(ERROR) << "Fail to add check storage value to filter";
530       return {};
531     }
532   }
533
534   if (app.empty()) {
535     for (auto& info : app_map_) {
536       if (CheckAppFilters(filter, info.second,
537           GetPackageByApp(info.second->package), metadata_map))
538         ret.push_back(info.second);
539     }
540   } else {
541     auto map_it = app_map_.find(app);
542     if (map_it != app_map_.end() &&
543         CheckAppFilters(filter, map_it->second,
544             GetPackageByApp(map_it->second->package), metadata_map))
545       ret.push_back(map_it->second);
546   }
547
548   return ret;
549 }
550
551 void DBHandleProvider::AddApplication(std::string app,
552     std::shared_ptr<application_x> info) {
553   pkg_app_map_[info->package].emplace(app);
554   app_map_[app] = std::move(info);
555 }
556
557 void DBHandleProvider::InsertPID(pid_t pid) {
558   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
559
560   writer_pid_list_.insert(pid);
561 }
562
563 bool DBHandleProvider::ErasePID(pid_t pid) {
564   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
565
566   return writer_pid_list_.erase(pid) == 1;
567 }
568
569 void DBHandleProvider::RegisterPendingPackageInfo(package_x* info) {
570   if (!info || !info->package)
571     return;
572
573   pending_pkg_.emplace(info->package);
574 }
575
576 void DBHandleProvider::UpdatePendingPackageInfo(const tizen_base::Database& db,
577     pid_t pid, uid_t uid, const std::string& locale) {
578   pkgmgrinfo_filter_x tmp_filter = { 0, };
579   pkgmgrinfo_node_x node = {
580     .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID
581   };
582   tmp_filter.cache_flag = true;
583   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
584   std::map<std::string, std::shared_ptr<package_x>> pkgs;
585   for (const auto& pkg : pending_pkg_) {
586     pkg_map_.erase(pkg);
587     for (auto& appid : pkg_app_map_[pkg]) {
588       app_map_.erase(appid);
589     }
590
591     pkg_app_map_.erase(pkg);
592     node.value = const_cast<char*>(pkg.c_str());
593     internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
594   }
595
596   for (auto& [key, val] : pkgs)
597     AddPackage(key, val);
598
599   node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
600   for (auto& [key, val] : pkgs) {
601     node.value = val->package;
602     std::vector<std::shared_ptr<application_x>> app_list;
603     internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
604
605     for (auto& app : app_list) {
606       app->privileges = val->privileges;
607       std::string appid = app->appid;
608       AddApplication(std::move(appid), std::move(app));
609     }
610   }
611
612   g_slist_free(tmp_filter.list);
613   pending_pkg_.clear();
614 }
615
616 bool DBHandleProvider::UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
617     const std::string& pkgid, const std::string& locale) {
618   pkgmgrinfo_filter_x tmp_filter = { 0, };
619   pkgmgrinfo_node_x node = {
620     .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID,
621     .value = const_cast<char*>(pkgid.c_str())
622   };
623   tmp_filter.cache_flag = true;
624   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
625   std::map<std::string, std::shared_ptr<package_x>> pkgs;
626   internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
627   for (auto& [key, val] : pkgs) {
628     AddPackage(key, val);
629     for (auto& appid : pkg_app_map_[key]) {
630       app_map_.erase(appid);
631     }
632
633     pkg_app_map_.erase(key);
634     std::vector<std::shared_ptr<application_x>> app_list;
635     node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
636     node.value = const_cast<char*>(key.c_str());
637     internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
638     for (auto& app : app_list) {
639       app->privileges = val->privileges;
640       std::string appid = app->appid;
641       AddApplication(std::move(appid), std::move(app));
642     }
643   }
644
645   return true;
646 }
647
648 bool DBHandleProvider::UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
649     const std::string& appid, const std::string& locale) {
650   pkgmgrinfo_filter_x tmp_filter = { 0, };
651   pkgmgrinfo_node_x node = {
652     .prop = E_PMINFO_APPINFO_PROP_APP_ID,
653     .value = const_cast<char*>(appid.c_str())
654   };
655   tmp_filter.cache_flag = true;
656   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
657
658   std::vector<std::shared_ptr<application_x>> app_list;
659   app_map_.erase(appid);
660   internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
661   g_slist_free(tmp_filter.list);
662
663   for (auto& app : app_list) {
664     auto it = pkg_map_.find(app->package);
665     if (it == pkg_map_.end()) {
666       LOG(ERROR) << "Can not find package from pkg_map";
667       return false;
668     }
669
670     pkg_app_map_[app->package].erase(app->appid);
671     app->privileges = it->second->privileges;
672     std::string appid = app->appid;
673     AddApplication(std::move(appid), std::move(app));
674   }
675
676   return true;
677 }
678
679 bool DBHandleProvider::UpdateCacheAppByPkgid(const tizen_base::Database& db,
680     uid_t uid, const std::string& pkgid, const std::string& locale) {
681   auto it = pkg_map_.find(pkgid);
682   if (it == pkg_map_.end()) {
683     LOG(ERROR) << "Can not find package from pkg_map";
684     return false;
685   }
686
687   auto& pkg = it->second;
688   pkgmgrinfo_filter_x tmp_filter = { 0, };
689   pkgmgrinfo_node_x node = {
690     .prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE,
691     .value = pkg->package
692   };
693
694   tmp_filter.cache_flag = true;
695   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
696
697   std::vector<std::shared_ptr<application_x>> app_list;
698   internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
699
700   for (auto& appid : pkg_app_map_[pkgid]) {
701     app_map_.erase(appid);
702   }
703   pkg_app_map_.erase(pkgid);
704
705   for (auto& app : app_list) {
706     app->privileges = pkg->privileges;
707     std::string appid = app->appid;
708     AddApplication(std::move(appid), std::move(app));
709   }
710
711   g_slist_free(tmp_filter.list);
712   return true;
713 }
714
715 }  // namespace database
716 }  // namespace pkgmgr_server