Release version 0.22.28
[platform/core/appfw/pkgmgr-info.git] / src / server / database / cache_provider.cc
1 /*
2  * Copyright (c) 2022 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 "cache_provider.hh"
18
19 #include <memory>
20 #include <mutex>
21 #include <shared_mutex>
22 #include <unordered_map>
23 #include <vector>
24
25 #include <glib.h>
26 #include <sys/types.h>
27 #include <sqlite3.h>
28 #include <tzplatform_config.h>
29
30 #include "cache.hh"
31 #include "filter_checker_provider.hh"
32 #include "utils/logging.hh"
33
34 #include "pkgmgrinfo_private.h"
35 #include "pkgmgrinfo_internal.h"
36 #include "pkgmgrinfo_internal.hh"
37
38 namespace {
39
40 uid_t globaluser_uid = -1;
41
42 uid_t GetGlobalUID() {
43   if (globaluser_uid == (uid_t)-1)
44     globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
45
46   return globaluser_uid;
47 }
48
49 uid_t ConvertUID(uid_t uid) {
50   if (uid < REGULAR_USER)
51     return GetGlobalUID();
52   else
53     return uid;
54 }
55
56 }  // namespace
57
58 namespace pkgmgr_server {
59 namespace database {
60
61 bool CacheProvider::IsCachePreparationNecessary() {
62   return Cache::GetInst(ConvertUID(uid_)).IsCachePreparationNecessary();
63 }
64
65 void CacheProvider::ReleaseCache() {
66   Cache::GetInst(ConvertUID(uid_)).ReleaseCache();
67 }
68
69 void CacheProvider::ReleaseAll() {
70   Cache::ReleaseAll();
71 }
72
73 bool CacheProvider::IsPrepared() {
74   return Cache::GetInst(ConvertUID(uid_)).IsPrepared();
75 }
76
77 bool CacheProvider::GetReadLock() {
78   reader_lock_ = Cache::GetInst(ConvertUID(uid_)).GetReaderLock();
79   if (reader_lock_.try_lock() && Cache::GetInst(ConvertUID(uid_)).IsPrepared())
80     return true;
81
82   reader_lock_.unlock();
83   return false;
84 }
85
86 void CacheProvider::SetUnprepared() {
87   Cache::GetInst(ConvertUID(uid_)).SetUnprepared();
88 }
89
90 int CacheProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
91     const std::string& locale) {
92   auto lock = Cache::GetInst(ConvertUID(uid_)).GetWriterLock();
93
94   int ret = Cache::GetInst(ConvertUID(uid_)).UpdateCache(
95       db, pid, uid, write, locale);
96
97   if (ret == PMINFO_R_OK)
98     Cache::GetInst(ConvertUID(uid_)).SetPrepared();
99   else
100     Cache::GetInst(ConvertUID(uid_)).SetUnprepared();
101
102   return ret;
103 }
104
105 std::vector<std::shared_ptr<package_x>> CacheProvider::GetPackages(
106     pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
107     const std::string& package) {
108   std::vector<std::shared_ptr<package_x>> ret;
109
110   uid_t target_uid = ConvertUID(uid_);
111   ret = Cache::GetInst(target_uid).GetPackages(pid, write, filter, package);
112
113   return ret;
114 }
115
116 std::vector<std::shared_ptr<application_x>> CacheProvider::GetApplications(
117     pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
118     const std::string& app) {
119   std::vector<std::shared_ptr<application_x>> ret;
120   uid_t target_uid = ConvertUID(uid_);
121   ret = Cache::GetInst(target_uid).GetApplications(pid, write, filter, app);
122
123   return ret;
124 }
125
126
127 }  // namespace database
128 }  // namespace pkgmgr_server