2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "cache_provider.hh"
21 #include <shared_mutex>
22 #include <unordered_map>
26 #include <sys/types.h>
28 #include <tzplatform_config.h>
31 #include "filter_checker_provider.hh"
32 #include "utils/logging.hh"
34 #include "pkgmgrinfo_private.h"
35 #include "pkgmgrinfo_internal.h"
36 #include "pkgmgrinfo_internal.hh"
40 uid_t globaluser_uid = -1;
42 uid_t GetGlobalUID() {
43 if (globaluser_uid == (uid_t)-1)
44 globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
46 return globaluser_uid;
49 uid_t ConvertUID(uid_t uid) {
50 if (uid < REGULAR_USER)
51 return GetGlobalUID();
58 namespace pkgmgr_server {
61 bool CacheProvider::IsCachePreparationNecessary() {
62 return Cache::GetInst(ConvertUID(uid_)).IsCachePreparationNecessary();
65 void CacheProvider::ReleaseCache() {
66 Cache::GetInst(ConvertUID(uid_)).ReleaseCache();
69 void CacheProvider::ReleaseAll() {
73 bool CacheProvider::IsPrepared() {
74 return Cache::GetInst(ConvertUID(uid_)).IsPrepared();
77 bool CacheProvider::GetReadLock() {
78 reader_lock_ = Cache::GetInst(ConvertUID(uid_)).GetReaderLock();
79 if (reader_lock_.try_lock() && Cache::GetInst(ConvertUID(uid_)).IsPrepared())
82 reader_lock_.unlock();
86 void CacheProvider::SetUnprepared() {
87 Cache::GetInst(ConvertUID(uid_)).SetUnprepared();
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();
94 int ret = Cache::GetInst(ConvertUID(uid_)).UpdateCache(
95 db, pid, uid, write, locale);
97 if (ret == PMINFO_R_OK)
98 Cache::GetInst(ConvertUID(uid_)).SetPrepared();
100 Cache::GetInst(ConvertUID(uid_)).SetUnprepared();
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;
110 uid_t target_uid = ConvertUID(uid_);
111 ret = Cache::GetInst(target_uid).GetPackages(pid, write, filter, package);
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);
127 } // namespace database
128 } // namespace pkgmgr_server