Refactor pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / src / server / database / remove_cache_db_handler.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 "remove_cache_db_handler.hh"
18
19 #include "cache_flag.hh"
20 #include "db_handle_provider.hh"
21
22 namespace pkgmgr_server {
23 namespace database {
24
25 RemoveCacheDBHandler::RemoveCacheDBHandler(uid_t uid, int pid)
26     : AbstractDBHandler(uid, pid), uid_(uid) {}
27
28 int RemoveCacheDBHandler::Execute() {
29   std::unique_lock<std::shared_mutex> u(lock_);
30   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
31   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
32
33   database::DBHandleProvider::GetInst(GetUID()).UnsetMemoryMode(GetPID());
34
35   if (!Connect()) {
36     database::DBHandleProvider::GetInst(GetUID()).TrimCache();
37     return PMINFO_R_ERROR;
38   }
39
40   const auto& conn_list = GetConnection();
41   auto lock = CacheFlag::GetWriterLock();
42   if (CacheFlag::GetStatus() != CacheFlag::Status::PREPARED)
43     return PMINFO_R_OK;
44
45   CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
46   for (const auto& [db, uid] : conn_list) {
47     auto& provider = database::DBHandleProvider::GetInst(uid);
48     provider.UpdatePendingPackageInfo(db, GetPID(), uid_, GetLocale());
49   }
50
51   CacheFlag::SetStatus(CacheFlag::Status::PREPARED);
52
53   return PMINFO_R_OK;
54 }
55
56 }  // namespace database
57 }  // namespace pkgmgr_server